Class: MenuItem
Class: MenuItem
Add items to native application menus and context menus.将项目添加到本机应用程序菜单和上下文菜单。
Process:进程:Main
See Menu for examples.有关示例,请参阅Menu。
new MenuItem(options)
Note: acceleratorWorksWhenHidden
is specified as being macOS-only because accelerators always work when items are hidden on Windows and Linux. acceleratorWorksWhenHidden
仅被指定为macOS,因为加速器总是在项目隐藏在Windows和Linux上时工作。The option is exposed to users to give them the option to turn it off, as this is possible in native macOS development. 该选项暴露给用户,让他们可以选择关闭它,因为这在本机macOS开发中是可能的。This property is only usable on macOS High Sierra 10.13 or newer.此属性仅适用于macOS High Sierra 10.13或更新版本。
Roles角色
Roles allow menu items to have predefined behaviors.角色允许菜单项具有预定义的行为。
It is best to specify 最好为任何与标准role
for any menu item that matches a standard role, rather than trying to manually implement the behavior in a click
function. role
匹配的菜单项指定角色,而不是尝试在click
函数中手动实现行为。The built-in 内置的role
behavior will give the best native experience.role
行为将提供最佳的本地体验。
The 使用label
and accelerator
values are optional when using a role
and will default to appropriate values for each platform.role
时,label
和accelerator
值是可选的,并且将默认为每个平台的适当值。
Every menu item must have either a 每个菜单项都必须有一个role
, label
, or in the case of a separator a type
.role
、label
,如果是分隔符,则必须有一种类型。
The role
property can have following values:role
属性可以具有以下值:
undo
about
-Trigger a native about panel (custom message box on Window, which does not provide its own).触发本机about面板(Window上的自定义消息框,它不提供自己的消息框)。redo
cut
copy
paste
pasteAndMatchStyle
selectAll
delete
minimize
-Minimize current window.最小化当前窗口。close
-Close current window.关闭当前窗口。quit
-Quit the application.退出应用程序。reload
-Reload the current window.重新加载当前窗口。forceReload
-Reload the current window ignoring the cache.忽略缓存重新加载当前窗口。toggleDevTools
-Toggle developer tools in the current window.在当前窗口中切换开发人员工具。togglefullscreen
-Toggle full screen mode on the current window.在当前窗口上切换全屏模式。resetZoom
-Reset the focused page's zoom level to the original size.将聚焦页面的缩放级别重置为原始大小。zoomIn
-Zoom in the focused page by 10%.将聚焦页面放大10%。zoomOut
-Zoom out the focused page by 10%.将聚焦页面缩小10%。toggleSpellChecker
-Enable/disable builtin spell checker.启用/禁用内置拼写检查器。fileMenu
-Whole default "File" menu (Close / Quit)整个默认“文件”菜单(关闭/退出)editMenu
-Whole default "Edit" menu (Undo, Copy, etc.).整个默认的“编辑”菜单(撤消、复制等)。viewMenu
-Whole default "View" menu (Reload, Toggle Developer Tools, etc.)整个默认的“视图”菜单(重新加载、切换开发工具等)windowMenu
-Whole default "Window" menu (Minimize, Zoom, etc.).整个默认的“窗口”菜单(最小化、缩放等)。
The following additional roles are available on macOS:以下附加角色可在macOS上使用:
appMenu
-Whole default "App" menu (About, Services, etc.)整个默认的“应用程序”菜单(关于、服务等)hide
-Map to the映射到hide
action.hide
操作。hideOthers
-Map to the映射到hideOtherApplications
action.hideOtherApplications
操作。unhide
-Map to the映射到unhideAllApplications
action.unhideAllApplications
操作。showSubstitutions
-Map to the映射到orderFrontSubstitutionsPanel
action.orderFrontSubstitutionsPanel
操作。toggleSmartQuotes
-Map to the映射到toggleAutomaticQuoteSubstitution
action.toggleAutomaticQuoteSubstitution
操作。toggleSmartDashes
-Map to the映射到toggleAutomaticDashSubstitution
action.toggleAutomaticDashSubstitution
操作。toggleTextReplacement
-Map to the映射到toggleAutomaticTextReplacement
action.toggleAutomaticTextReplacement
操作。startSpeaking
-Map to the映射到startSpeaking
action.startSpeaking
操作。stopSpeaking
-Map to the映射到stopSpeaking
action.stopSpeaking
操作。front
-Map to the映射到arrangeInFront
action.arrangeInFront
操作。zoom
-Map to the映射到performZoom
action.performZoom
操作。toggleTabBar
-Map to the映射到toggleTabBar
action.toggleTabBar
操作。selectNextTab
-Map to the映射到selectNextTab
action.selectNextTab
操作。selectPreviousTab
-Map to the映射到selectPreviousTab
action.selectPreviousTab
操作。mergeAllWindows
-Map to the映射到mergeAllWindows
action.mergeAllWindows
操作。moveTabToNewWindow
-Map to the映射到moveTabToNewWindow
action.moveTabToNewWindow
操作。window
-The submenu is a "Window" menu.子菜单是一个“窗口”菜单。help
-The submenu is a "Help" menu.子菜单是一个“帮助”菜单。services
-The submenu is a "Services" menu.子菜单是"Services"菜单。This is only intended for use in the Application Menu and is not the same as the "Services" submenu used in context menus in macOS apps, which is not implemented in Electron.这仅用于应用程序菜单,与macOS应用程序上下文菜单中使用的“服务”子菜单不同,后者未在Electron中实现。recentDocuments
-The submenu is an "Open Recent" menu.子菜单是一个“打开最近的”菜单。clearRecentDocuments
-Map to the映射到clearRecentDocuments
action.clearRecentDocuments
操作。shareMenu
-The submenu is share menu.子菜单是共享菜单。ThesharingItem
property must also be set to indicate the item to share.sharingItem
属性也必须设置为指示要共享的项。
When specifying a 在macOS上指定role
on macOS, label
and accelerator
are the only options that will affect the menu item. role
时,label
和accelerator
是唯一会影响菜单项的选项。All other options will be ignored. Lowercase 所有其他选项都将被忽略。仍然支持小写role
, e.g. toggledevtools
, is still supported.role
,例如toggledevtools
。
Note: The enabled
and visibility
properties are not available for top-level menu items in the tray on macOS.enabled
和visibility
属性不适用于macOS托盘中的顶级菜单项。
Instance Properties实例属性
The following properties are available on instances of 以下属性可用于MenuItem
:MenuItem
实例:
menuItem.id
A 一个表示项的唯一id的string
indicating the item's unique id, this property can be dynamically changed.string
,此属性可以动态更改。
menuItem.label
A 一个表示项目的可见标签的string
indicating the item's visible label.string
。
menuItem.click
A 当Function
that is fired when the MenuItem receives a click event. MenuItem
接收到单击事件时激发的Function
。It can be called with 它可以用menuItem.click(event, focusedWindow, focusedWebContents)
.menuItem.click(event, focusedWindow, focusedWebContents)
调用。
event
KeyboardEventfocusedWindow
BrowserWindowfocusedWebContents
WebContents
menuItem.submenu
A 包含菜单项的子菜单(如果存在)的Menu
(optional) containing the menu item's submenu, if present.Menu
(可选)。
menuItem.type
A 指示项目类型的string
indicating the type of the item. string
。Can be 可以是normal
, separator
, submenu
, checkbox
or radio
.normal
、separator
、submenu
、checkbox
或radio
。
menuItem.role
A 一个string
(optional) indicating the item's role, if set. string
(可选),指示项目的角色(如果已设置)。Can be undo
, redo
, cut
, copy
, paste
, pasteAndMatchStyle
, delete
, selectAll
, reload
, forceReload
, toggleDevTools
, resetZoom
, zoomIn
, zoomOut
, toggleSpellChecker
, togglefullscreen
, window
, minimize
, close
, help
, about
, services
, hide
, hideOthers
, unhide
, quit
, startSpeaking
, stopSpeaking
, zoom
, front
, appMenu
, fileMenu
, editMenu
, viewMenu
, shareMenu
, recentDocuments
, toggleTabBar
, selectNextTab
, selectPreviousTab
, mergeAllWindows
, clearRecentDocuments
, moveTabToNewWindow
or windowMenu
menuItem.accelerator
An 一个Accelerator
(optional) indicating the item's accelerator, if set.Accelerator
(可选),指示项目的加速器(如果已设置)。
menuItem.userAccelerator
Readonly macOS
An Accelerator | null
indicating the item's user-assigned accelerator for the menu item.Accelerator | null
,指示菜单项的用户指定的加速器。
Note: This property is only initialized after the 只有在将MenuItem
has been added to a Menu
. MenuItem
添加到Menu
后,才会初始化此属性。Either via 通过Menu.buildFromTemplate
or via Menu.append()/insert()
. Menu.buildFromTemplate
或通过Menu.append()
、Menu.insert()
。Accessing before initialization will just return 初始化前的访问只会返回null
.null
。
menuItem.icon
A 一个NativeImage | string
(optional) indicating the item's icon, if set.NativeImage | string
(可选),指示项目的图标(如果已设置)。
menuItem.sublabel
A 指示项目子标签的string
indicating the item's sublabel.string
。
menuItem.toolTip
macOS
A 指示项目悬停文本的string
indicating the item's hover text.string
。
menuItem.enabled
A 一个boolean
indicating whether the item is enabled, this property can be dynamically changed.boolean
,指示该项是否已启用,此属性可以动态更改。
menuItem.visible
A 指示项是否可见的boolean
indicating whether the item is visible, this property can be dynamically changed.boolean
,此属性可以动态更改。
menuItem.checked
A 一个boolean
indicating whether the item is checked, this property can be dynamically changed.boolean
,指示是否选中该项,此属性可以动态更改。
A 选中checkbox
menu item will toggle the checked
property on and off when selected.checkbox
菜单项时,将打开和关闭checked
属性。
A 单击时,radio
menu item will turn on its checked
property when clicked, and will turn off that property for all adjacent items in the same menu.radio
菜单项将打开其checked
属性,并关闭同一菜单中所有相邻项的该属性。
You can add a 您可以为其他行为添加click
function for additional behavior.click
函数。
menuItem.registerAccelerator
A 一个boolean
indicating if the accelerator should be registered with the system or just displayed.boolean
,指示加速器应该向系统注册还是只显示。
This property can be dynamically changed.此属性可以动态更改。
menuItem.sharingItem
macOS
A 一个SharingItem
indicating the item to share when the role
is shareMenu
.SharingItem
,指示role
为shareMenu
时要共享的项目。
This property can be dynamically changed.此属性可以动态更改。
menuItem.commandId
A 一个number
indicating an item's sequential unique id.number
,表示物品的顺序唯一id。
menuItem.menu
A 该项目所属的Menu
that the item is a part of.Menu
。