最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron modify a single menu item? - Stack Overflow

programmeradmin2浏览0评论

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 plete 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?

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 plete 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?

Share Improve this question asked Jan 9, 2019 at 7:38 EularEular 1,8175 gold badges29 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.

发布评论

评论列表(0)

  1. 暂无评论