I attempt to adapt a widget’s context menu dynamically depending on the current state.
There have been several questions about how to modify context menus or the main menu. This one may be most similar to what I want to achieve: Requesting help with how to remove menu items from the Jupyterlab Context menu
But the big difference is that I want to change (or remove and add modified) only those context menu entries that I have created in the same extension. I understand that one extension should not be able to modify the menus of unrelated extensions, which is also mentioned in the link above.
But is it possible to change context menu entries that I have created?
I add submenus with one or more entries like this:
let submenu = new Menu({ commands: app.commands });
submenu.title.label = "Submenu ...";
app.commands.addCommand(commandId, {
label: "Some command",
execute: () => {
// ...
}
});
submenu.addItem({
command: commandId
});
this.app.contextMenu.addItem({
type: "submenu",
submenu: submenu,
selector: '.mycontextmenu'
});
When the widget’s state changes, the context menu should show different submenus, which will have different items. How can I
- Remove previously created commands (from “app.commands.addCommand(…)”)
- or change the label of those commands?
- Remove items of previously created submenus?
- Remove previously created submenus?
As jasongrout wrote, adding menu entries is easy. But I can’t find a method to modify or remove them except for the “internal hack” from the same link above.
Thank you
Kevin