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
属性可以具有以下值:
undo
redo
cut
copy
paste
pasteandmatchstyle
selectall
delete
minimize
- 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