Reputation: 1807
So, I'm building a softare using electron. Now I can add menu in the software from a template
var menu = Menu.buildFromTemplate([
{
label: 'Menu',
submenu: [
{label:'open'},
{label:'save'},
{label:'Exit'}
]
}
])
Menu.setApplicationMenu(menu);
But how do I modify a single menu item. For example, say, the save
menu is disabled by default and activated after the open
is clicked. Also say after clicking open
a new menu edit
appears. I can create the complete new template in full and just change the previous template with the new. But thats a bad way and can't be a practical solution when I'm using several menus with several submenus. So can I modify just one single menu item of my choice?
Upvotes: 3
Views: 6231
Reputation: 9988
You can get the menu items using:
import { Menu } from 'electron';
Menu.getApplicationMenu().items // all the items
Menu.getApplicationMenu().getMenuItemById('MENU_ITEM_ID') // get a single item by its id
After that you have several properties on the single menu item as:
- checked
- enabled
- visible
- label
- click
And you can customize your behavior as you want to.
Tested with electron 3.0.5
, before the 27 Sep 2017
the method getMenuItemById
was not there and you had to loop over all the items.
Upvotes: 7