In one of my extension, I have this options definition:
const optionsModel = {
// ...
menus: {
title: 'Menus',
default: true,
children: {
unloadOtherTabs: {
default: 'tab tools_menu',
restrict: { type: 'string', },
input: { type: 'menulist', options: [
{ value: 'tools_menu', label: `only in the Tools menu`, },
{ value: 'tab tools_menu', label: `also in the Tab context menu`, },
], prefix: `Show <b>Unload Other Tabs</b>`, },
},
unloadAllTabs: {
default: 'tools_menu',
restrict: { type: 'string', },
input: { type: 'menulist', options: [
{ value: 'tools_menu', label: `only in the Tools menu`, },
{ value: 'tab tools_menu', label: `also in the Tab context menu`, },
], prefix: `Show <b>Unload in All Windows</b>`, },
},
},
},
// ...
};
(It shouldn’t really matter how they are wrapped in getters/update events/UI, the API should be self-explining.)
And use it like this:
const menus = {
// ...
unloadOtherTabs: {
title: 'Unload Other Tabs',
icons: { 32: 'many.png', },
contexts: options.menus.children.unloadOtherTabs.value.split(' '),
},
unloadAllTabs: {
title: 'Unload in All Windows',
contexts: options.menus.children.unloadAllTabs.value.split(' '),
},
}; Object.keys(menus).forEach(id => (menus[id].id = id));
Object.values(menus).forEach(menu => Menus.create(menu));
options.menus.children.unloadOtherTabs.onChange(updateMenu);
options.menus.children.unloadAllTabs.onChange(updateMenu);
function updateMenu([ value, ], _, { name, }) {
menus[name].contexts = value.split(' ');
Menus.update(name, { contexts: value.split(' '), });
}
Works like a charm.
I am not sure though what happens if you set contextx
to an empty array. Might just always leave it in the tools menu.