动态更改Electron菜单项的状态 [英] Change Electron's menu item's status dynamically

查看:677
本文介绍了动态更改Electron菜单项的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像在任何标准本机应用程序中一样,我的电子应用程序也需要根据实时使用结果来更改几个菜单项的状态(启用/禁用)。

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.

我正在main.js中设置菜单:

I am setting up my menu in main.js:

function createWindow () {
...
...
  require('./menu/mainmenu');
}

我需要更改的MenuItem在mainmenu中定义:

The MenuItem I need to change is defined in mainmenu:

{ label: "Show Colors",  
        accelerator: 'CmdOrCtrl+1', 
        enabled: getStatus(),
        click() {getWebviewWebContents().send('switchToColors');} 
 },

其中 getStatus()是返回 false true

这在Electron中不起作用,因为菜单是在应用程序启动时创建的,根本无法修改。我相信这是一个严重的不足,因为动态菜单项非常常见(即:菜单复选框,启用/禁用等)。

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?

推荐答案

我已通过在菜单项上设置ID来解决此问题,

I have fixed this by setting an Id to the menu item,

{ label: "Show Colors",  
        id: 'color-scale',
        accelerator: 'CmdOrCtrl+1', 
        enabled: getStatus(),
        click() {getWebviewWebContents().send('switchToColors');} 
 },

并使用以下菜单项:

myItem = menu.getMenuItemById('color-scale')

然后,当我需要以编程方式启用/禁用它时,我正在使用:

Then, when I need to enable/disable it programmatically, I am using:

myItem.enabled = true

myItem.enabled = false

这篇关于动态更改Electron菜单项的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆