Skip to content

Commit

Permalink
- SMP Dynamic menu: fixed multiple errors on dynamic menus (un)regist…
Browse files Browse the repository at this point in the history
…ering.
  • Loading branch information
regorxxx committed Feb 13, 2025
1 parent 6f839f5 commit d68797a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- Helpers: general code cleanup on menus internal code. Please report any bug on extra separators or menu entries not working as expected.
### Removed
### Fixed
- SMP Dynamic menu: fixed multiple errors on dynamic menus (un)registering.
- Images: workaround for some images not being fully processed in some cases by nconvert when creating the pdf (images appeared corrupted).
- Report: multiple layout fixes and improvements according to latex engine used.
- UI: '&' being displayed as '_' on tooltips.
Expand Down
52 changes: 28 additions & 24 deletions main/main_menu/main_menu_custom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
//21/03/24
//13/02/25

/* exported onMainMenuEntries, bindDynamicMenus */

Expand All @@ -17,19 +17,21 @@ const onMainMenuEntries = [
const onMainMenuDynamicEntries = [];

function deleteMainMenuDynamic(parent) {
onMainMenuDynamicEntries.forEach((_, i) => {
if (typeof parent === 'undefined' || _.parent === parent) {
onMainMenuDynamicEntries.forEach((entry, i) => {
if (entry && (typeof parent === 'undefined' || entry.parent === parent)) {
fb.UnregisterMainMenuCommand(i);
}
});
if (typeof parent === 'undefined') {onMainMenuDynamicEntries.splice(0, onMainMenuDynamicEntries.length);}
if (typeof parent === 'undefined') { onMainMenuDynamicEntries.length = 0; }
else {
const idx = [];
for (let i = 0; i < onMainMenuDynamicEntries.length; i++) {
if (onMainMenuDynamicEntries[i].parent === parent) {idx.push(i);}
}
onMainMenuDynamicEntries.forEach((entry, i) => {
if (entry && entry.parent === parent) { idx.push(i); }
});
// Don't remove unused entries from list, since other menu idxs are not reset until panel reload
if (idx.length) {
idx.reverse().forEach(i => onMainMenuDynamicEntries.splice(i, 1)); // NOSONAR
idx.reverse().forEach((i) => onMainMenuDynamicEntries[i] = null); // NOSONAR
if (onMainMenuDynamicEntries.every((entry) => !entry)) { onMainMenuDynamicEntries.length = 0; }
}
}
}
Expand All @@ -39,17 +41,18 @@ function bindDynamicMenus({
parentName = window.Name,
withFlag = true, /* execute only menu entries with bDynamicMenu flag set */
args = null, /* pass args object to menu func */
entryCallback = void(0), /* return entry name */
descrCallback = void(0) /* return entry description */
entryCallback = void (0), /* return entry name */
descrCallback = void (0) /* return entry description */
} = {}) {
callbacksListener.checkPanelNames();
if (!menu) {throw new Error('No parentMenu');}
const menuSimul = menu({bSimulate: true});
if (!menu) { throw new Error('No parentMenu'); }
const menuSimul = menu({ bSimulate: true });
const mainMenu = menuSimul.getMainMenuName();
menuSimul.getEntries().forEach((entry, index) => {
if (entry && (!withFlag || (entry.data && entry.data.bDynamicMenu))) {
let name = (entry.menuName === mainMenu ? '' : entry.menuName + '\\') + entry.entryText.replace(/\t.*/, '');
const idx = onMainMenuDynamicEntries.push({name, parent: parentName, parentMenu: menu, args}) - 1;
if (typeof entry.entryText !== 'string') { console.log('bindDynamicMenus: menu entry is not a static string\n\t ' + entry.entryText.toString()); }
let name = (entry.menuName === mainMenu ? '' : entry.menuName + '\\') + entry.entryText.replace(/\t.*/, '');
const idx = onMainMenuDynamicEntries.push({ name, parent: parentName, parentMenu: menu, args }) - 1;
name = entryCallback ? entryCallback(entry, index) : entry.entryText.replace(/\t.*/, '').replace(/&&/g, '&');
const descr = descrCallback ? descrCallback(entry, index) : entry.entryText.replace(/\t.*/, '').replace(/&&/g, '&');
fb.RegisterMainMenuCommand(idx, name, descr);
Expand All @@ -61,30 +64,31 @@ function bindDynamicMenus({
addEventListener('on_main_menu_dynamic', (idx) => {
if (idx < onMainMenuDynamicEntries.length) {
const entry = onMainMenuDynamicEntries[idx];
if (!entry) { return; }
if (entry.onMainMenuEntries) {
console.log('SMP main menu ' + (idx + 1) + ': ' + entry.name);
if (Object.hasOwn(entry, 'path')) {
if (entry.path.length) {
try {include(entry.path.replace('.\\', folders.xxx));}
catch (e) {console.popup(e.message.split(/\r\n|\n\r|\n|\r/).join('\n\t '), 'SMP Dynamic menu');}
try { include(entry.path.replace('.\\', folders.xxx)); }
catch (e) { console.popup(e.message.split(/\r\n|\n\r|\n|\r/).join('\n\t '), 'SMP Dynamic menu'); }
entry.path = '';
}
try {eval(entry.funcName)();}
catch (e) {console.popup('Error evaluating: ' + entry.funcName + ' from script (' + (entry.path.length ? entry.path : 'parent') + ').', 'SMP Dynamic menu');}
try { eval(entry.funcName)(); }
catch (e) { console.popup('Error evaluating: ' + entry.funcName + ' from script (' + (entry.path.length ? entry.path : 'parent') + ').', 'SMP Dynamic menu'); }
} else if (Object.hasOwn(entry, 'menuName')) {
try {eval(entry.menuName).btn_up(void(0), void(0), void(0), entry.funcName);}
catch (e) {console.popup('Error evaluating: ' + entry.funcName + ' from menu (' + entry.menuName + ').', 'SMP Dynamic menu');}
try { eval(entry.menuName).btn_up(void (0), void (0), void (0), entry.funcName); }
catch (e) { console.popup('Error evaluating: ' + entry.funcName + ' from menu (' + entry.menuName + ').', 'SMP Dynamic menu'); }
}
} else {
const isFunction = (obj) => !!(obj && obj.constructor && obj.call && obj.apply);
const name = isFunction(entry.name) ? entry.name() : entry.name;
if (Object.hasOwn(entry, 'parentMenu') && entry.parentMenu) { // Other buttons
try {
(entry.args ? entry.parentMenu(entry.args) : entry.parentMenu()).btn_up(void(0), void(0), void(0), name);
} catch (e) {console.popup('Error evaluating: ' + name + '.', 'SMP Dynamic menu');}
(entry.args ? entry.parentMenu(entry.args) : entry.parentMenu()).btn_up(void (0), void (0), void (0), name);
} catch (e) { console.popup('Error evaluating: ' + name + '.', 'SMP Dynamic menu'); }
} else { // Playlist Tools
try {menu.btn_up(void(0), void(0), void(0), name);}
catch (e) {console.popup('Error evaluating: ' + name + '.', 'SMP Dynamic menu');}
try { menu.btn_up(void (0), void (0), void (0), name); }
catch (e) { console.popup('Error evaluating: ' + name + '.', 'SMP Dynamic menu'); }
}
}
}
Expand Down

0 comments on commit d68797a

Please sign in to comment.