MenuItem

类:MenuItem

添加项目到本机应用程序菜单和环境菜单。

过程:Main

Menu举例来看。

new MenuItem(options)

  • options 目的

- `role` String (optional) - Define the action of the menu item, when specified the `click` property will be ignored. See [roles](about:blank#roles). - `type` String (optional) - Can be `normal`, `separator`, `submenu`, `checkbox` or `radio`. - `label` String - (optional) - `sublabel` String - (optional) - `accelerator` [Accelerator](../accelerator/index) (optional) - `icon` ([NativeImage](../native-image/index) | String) (optional) - `enabled` Boolean (optional) - If false, the menu item will be greyed out and unclickable. - `visible` Boolean (optional) - If false, the menu item will be entirely hidden. - `checked` Boolean (optional) - Should only be specified for `checkbox` or `radio` type menu items. - `submenu` (MenuItemConstructorOptions[] | Menu) (optional) - Should be specified for `submenu` type menu items. If `submenu` is specified, the `type: 'submenu'` can be omitted. If the value is not a `Menu` then it will be automatically converted to one using `Menu.buildFromTemplate`. - `id` String (optional) - Unique within a single menu. If defined then it can be used as a reference to this item by the position attribute. - `position` String (optional) - This field allows fine-grained definition of the specific location within a given menu.

角色

角色允许菜单项具有预定义的行为。

最好指定role与标准角色相匹配的任何菜单项,而不是尝试手动实现click函数中的行为。内置role行为将提供最佳的本地体验。

在使用role和时,labelaccelerator值是可选的,并且将默认为每个平台的适当值。

role属性可以具有以下值:

  • undo

macOS 上提供以下其他角色:

  • about- 映射到orderFrontStandardAboutPanel行动

当在 MacOS 指定rolelabelaccelerator是会影响菜单项的唯一选择。所有其他选项将被忽略。

实例属性

以下属性可用于以下MenuItem实例:

menuItem.enabled

一个 Boolean指示是否启用该项目,此属性可以动态更改。

menuItem.visible

一个Boolean指示项目是否可见,此属性可以动态更改。

menuItem.checked

一个 Boolean表示是否选中该项目,该属性可以动态更改。

一个checkbox菜单项将切换checked上,并选择关闭属性。

一个radio菜单项checked在点击时会打开它的属性,并将关闭同一菜单中所有相邻项目的属性。

您可以添加一个click函数来执行其他行为。

menuItem.label

一个String代表菜单项可见标签

menuItem.click

一个Function,当菜单项获得的点击事件被触发