MenuItem
添加项目到本机应用程序菜单和环境菜单。
过程:Main
Menu举例来看。
new MenuItem(options)
options目的click功能(可选) - 将click(menuItem, browserWindow, event)在菜单项被点击时调用。menuItem菜单项browserWindow浏览器窗口event事件
- `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和时,label和accelerator值是可选的,并且将默认为每个平台的适当值。
role属性可以具有以下值:
undoredocutcopypastepasteandmatchstyleselectalldeleteminimize- Minimize current windowclose- Close current windowquit- Quit the applicationreload- Reload the current windowforcereload- Reload the current window ignoring the cache.toggledevtools- Toggle developer tools in the current windowtogglefullscreen- Toggle full screen mode on the current windowresetzoom- Reset the focused page’s zoom level to the original sizezoomin- Zoom in the focused page by 10%zoomout- Zoom out the focused page by 10%editMenu- Whole default “Edit” menu (Undo, Copy, etc.)windowMenu- Whole default “Window” menu (Minimize, Close, etc.)
macOS 上提供以下其他角色:
about- 映射到orderFrontStandardAboutPanel行动hide- 映射到hide行动hideothers- 映射到hideOtherApplications行动unhide- 映射到unhideAllApplications行动startspeaking- 映射到startSpeaking行动stopspeaking- 映射到stopSpeaking行动front- 映射到arrangeInFront行动zoom- 映射到performZoom行动window- 子菜单是一个“窗口”菜单help- 子菜单是一个“帮助”菜单services- 子菜单是“服务”菜单
当在 MacOS 指定role,label和accelerator是会影响菜单项的唯一选择。所有其他选项将被忽略。
实例属性
以下属性可用于以下MenuItem实例:
menuItem.enabled
一个 Boolean指示是否启用该项目,此属性可以动态更改。
menuItem.visible
一个Boolean指示项目是否可见,此属性可以动态更改。
menuItem.checked
一个 Boolean表示是否选中该项目,该属性可以动态更改。
一个checkbox菜单项将切换checked上,并选择关闭属性。
一个radio菜单项checked在点击时会打开它的属性,并将关闭同一菜单中所有相邻项目的属性。
您可以添加一个click函数来执行其他行为。
menuItem.label
一个String代表菜单项可见标签
menuItem.click
一个Function,当菜单项获得的点击事件被触发
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

