动态改变 Electron 的菜单项状态

Change Electron#39;s menu item#39;s status dynamically(动态改变 Electron 的菜单项状态)
本文介绍了动态改变 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() 是返回 falsetrue 的函数.

where getStatus() is function returning false or 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).

有什么解决方法吗?

推荐答案

我已经通过为菜单项设置一个 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');} 
 },

并通过以下方式获取菜单项:

and getting the menu item with:

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

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

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

myItem.enabled = true

myItem.enabled = false

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)