Like in any standard native application, also my electron's application needs to change the status (enabled/dsabled) of several menu item, based on live usage results.
I am setting up my menu in main.js:
function createWindow () {
...
...
require('./menu/mainmenu');
}
The MenuItem I need to change is defined in mainmenu:
{ label: "Show Colors",
accelerator: 'CmdOrCtrl+1',
enabled: getStatus(),
click() {getWebviewWebContents().send('switchToColors');}
},
where getStatus()
is function returning false
or true
.
All this is not working in Electron, as the menu is created at application start and it can't be modified at all. I believe this is a serious lack, as dynamic menu items are very common (i.e.: menu checkboxes, enabled/disabled, etc).
Is there any workaround for this?
The only workaround so far I aware and using is reconstruct whole menu each time menuitem changes. This is not very ergonomics friendly, but works suffeciently enough and doesn't cause lot of overhead.
I have fixed this by setting an Id to the menu item,
and getting the menu item with:
Then, when I need to enable/disable it programmatically, I am using:
or
Here is my solution:
main.js (main process)