Skip to content

Commit fc5e15f

Browse files
committed
feat: first extension that implements a custom user options menu
1 parent b018571 commit fc5e15f

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/gui/src/UI/UIContextMenu.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
* https://github.com/kamens/jQuery-menu-aim
9191
*/
9292
(function ($) {
93-
9493
$.fn.menuAim = function (opts) {
9594
// Initialize menu-aim for all elements in jQuery collection
9695
this.each(function () {
@@ -365,6 +364,10 @@ function UIContextMenu(options){
365364
$('.window-active .window-app-iframe').css('pointer-events', 'none');
366365

367366
const menu_id = window.global_element_id++;
367+
368+
// Dispatch 'ctxmenu-will-open' event
369+
window.dispatchEvent(new CustomEvent('ctxmenu-will-open', { detail: { options: options} }));
370+
368371
let h = '';
369372
h += `<div
370373
id="context-menu-${menu_id}"
@@ -424,10 +427,12 @@ function UIContextMenu(options){
424427
h += `</div>`
425428
$('body').append(h)
426429

430+
427431
const contextMenu = document.getElementById(`context-menu-${menu_id}`);
428432
const menu_width = $(contextMenu).width();
429433
const menu_height = $(contextMenu).outerHeight();
430434
let start_x, start_y;
435+
431436
//--------------------------------
432437
// Auto position
433438
//--------------------------------
@@ -505,7 +510,7 @@ function UIContextMenu(options){
505510
};
506511

507512
// An item is clicked
508-
$(`#context-menu-${menu_id} > li:not(.context-menu-item-disabled)`).on('click', function (e) {
513+
$(document).on('click', `#context-menu-${menu_id} > li:not(.context-menu-item-disabled)`, function (e) {
509514

510515
// onClick
511516
if(options.items[$(this).attr("data-action")].onClick && typeof options.items[$(this).attr("data-action")].onClick === 'function'){

src/gui/src/UI/UIDesktop.js

+1
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,7 @@ $(document).on('click', '.user-options-menu-btn', async function(e){
14111411
//--------------------------------------------------
14121412
{
14131413
html: i18n('contact_us'),
1414+
id: 'contact_us',
14141415
onClick: async function(){
14151416
UIWindowFeedback();
14161417
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
$(window).on('ctxmenu-will-open', (event) => {
2+
if(event.detail.options?.id === 'user-options-menu'){
3+
// Define array of new menu items
4+
const newMenuItems = [
5+
// Separator
6+
'-',
7+
// 'Developer', opens developer site in new tab
8+
{
9+
id: 'go_to_developer_site',
10+
html: 'Developer<svg style="width: 11px; height: 11px; margin-left:2px;" height="32" viewBox="0 0 32 32" width="32" xmlns="http://www.w3.org/2000/svg"><path d="m26 28h-20a2.0027 2.0027 0 0 1 -2-2v-20a2.0027 2.0027 0 0 1 2-2h10v2h-10v20h20v-10h2v10a2.0027 2.0027 0 0 1 -2 2z"/><path d="m20 2v2h6.586l-8.586 8.586 1.414 1.414 8.586-8.586v6.586h2v-10z"/><path d="m0 0h32v32h-32z" fill="none"/></svg>',
11+
html_active: 'Developer<svg style="width: 11px; height: 11px; margin-left:2px;" height="32" viewBox="0 0 32 32" width="32" xmlns="http://www.w3.org/2000/svg"> <path d="m26 28h-20a2.0027 2.0027 0 0 1 -2-2v-20a2.0027 2.0027 0 0 1 2-2h10v2h-10v20h20v-10h2v10a2.0027 2.0027 0 0 1 -2 2z" style="fill: rgb(255, 255, 255);"/> <path d="m20 2v2h6.586l-8.586 8.586 1.414 1.414 8.586-8.586v6.586h2v-10z" style="fill: rgb(255, 255, 255);"/> <path d="m0 0h32v32h-32z" fill="none"/> </svg>',
12+
action: function(){
13+
window.open('https://developer.puter.com', '_blank');
14+
}
15+
},
16+
];
17+
18+
// Find the position of 'contact_us'
19+
const items = event.detail.options.items;
20+
const insertBeforeIndex = items.findIndex(item => item.id === 'contact_us');
21+
22+
// Insert all new items before 'contact_us'
23+
const firstHalf = items.slice(0, insertBeforeIndex);
24+
const secondHalf = items.slice(insertBeforeIndex);
25+
event.detail.options.items = [...firstHalf, ...newMenuItems, ...secondHalf];
26+
}
27+
});

0 commit comments

Comments
 (0)