Skip to main content

Class: MenuItem

Class: MenuItem

Add items to native application menus and context menus.将项目添加到本机应用程序菜单和上下文菜单。

Process:进程:Main

See Menu for examples.有关示例,请参阅Menu

new MenuItem(options)

  • options Object
    • click Function (optional) - Will be called with click(menuItem, browserWindow, event) when the menu item is clicked.单击菜单项时,将通click(menuItem, browserWindow, event)调用。
      • menuItem MenuItem
      • browserWindow BrowserWindow | undefined - This will not be defined if no window is open.如果没有打开任何窗口,则不会对此进行定义。
      • event KeyboardEvent
    • role string (optional) - 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, showSubstitutions, toggleSmartQuotes, toggleSmartDashes, toggleTextReplacement, startSpeaking, stopSpeaking, zoom, front, appMenu, fileMenu, editMenu, viewMenu, shareMenu, recentDocuments, toggleTabBar, selectNextTab, selectPreviousTab, mergeAllWindows, clearRecentDocuments, moveTabToNewWindow or windowMenu可以是undoredocutcopypastepasteAndMatchStyledeleteselectAllreloadforceReloadtoggleDevToolsresetZoomzoomInzoomOuttoggleSpellCheckertogglefullscreenwindowminimizeclosehelpaboutserviceshidehideOthersunhidequitshowSubstitutionstoggleSmartQuotestoggleSmartDashestoggleTextReplacementstartSpeakingstopSpeakingzoomfrontappMenufileMenueditMenuviewMenushareMenurecentDocumentstoggleTabBarselectNextTabselectPreviousTabmergeAllWindowsclearRecentDocumentsmoveTabToNewWindowwindowMenu - Define the action of the menu item, when specified the click property will be ignored. 定义菜单项的操作,当指定时,click属性将被忽略。See roles.请参阅角色
    • type string (optional) - Can be normal, separator, submenu, checkbox or radio.可以是normalseparatorsubmenucheckboxradio
    • label string (optional)
    • sublabel string (optional)
    • toolTip string (optional) macOS - Hover text for this menu item.悬停此菜单项的文本。
    • accelerator Accelerator (optional)
    • icon (NativeImage | string) (optional)
    • enabled boolean (optional) - If false, the menu item will be greyed out and unclickable.如果为false,则菜单项将变灰且不可点击。
    • acceleratorWorksWhenHidden boolean (optional) macOS - default is true, and when false will prevent the accelerator from triggering the item if the item is not visible.默认值为true,当为false时,如果项目不可见,将阻止加速器触发该项目。
    • visible boolean (optional) - If false, the menu item will be entirely hidden.如果为false,则菜单项将完全隐藏。
    • checked boolean (optional) - Should only be specified for checkbox or radio type menu items.应仅为checkboxradio类型菜单项指定。
    • registerAccelerator boolean (optional) Linux Windows - If false, the accelerator won't be registered with the system, but it will still be displayed. 如果为false,加速器将不会在系统中注册,但仍将显示。Defaults to true.默认为true
    • sharingItem SharingItem (optional) macOS - The item to share when the role is shareMenu.roleshareMenu时要共享的项目。
    • submenu (MenuItemConstructorOptions[] | Menu) (optional) - Should be specified for submenu type menu items. 应为submenu类型菜单项指定。If submenu is specified, the type: 'submenu' can be omitted. 如果指定了submenu,则可以省略类type: 'submenu'If the value is not a Menu then it will be automatically converted to one using Menu.buildFromTemplate.如果该值不是Menu,那么它将使用Menu.buildFromTemplate自动转换为Menu
    • 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.如果已定义,则可以通过位置属性将其用作对此项目的引用。
    • before string[] (optional) - Inserts this item before the item with the specified label. 在具有指定标签的项目之前插入此项目。If the referenced item doesn't exist the item will be inserted at the end of the menu. 如果引用的项目不存在,则该项目将插入到菜单的末尾。Also implies that the menu item in question should be placed in the same “group” as the item.也意味着有问题的菜单项应该与该菜单项放在同一个“组”中。
    • after string[] (optional) - Inserts this item after the item with the specified label. 在具有指定标签的项目之后插入此项目。If the referenced item doesn't exist the item will be inserted at the end of the menu.如果引用的项目不存在,则该项目将插入到菜单的末尾。
    • beforeGroupContaining string[] (optional) - Provides a means for a single context menu to declare the placement of their containing group before the containing group of the item with the specified label.为单个上下文菜单提供一种方法,用于在具有指定标签的项目的包含组之前声明其包含组的位置。
    • afterGroupContaining string[] (optional) - Provides a means for a single context menu to declare the placement of their containing group after the containing group of the item with the specified label.为单个上下文菜单提供一种方法,用于声明其包含组在具有指定标签的项目的包含组之后的位置。

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时,labelaccelerator值是可选的,并且将默认为每个平台的适当值。

Every menu item must have either a role, label, or in the case of a separator a type.每个菜单项都必须有一个rolelabel,如果是分隔符,则必须有一种类型。

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. 子菜单是共享菜单The sharingItem property must also be set to indicate the item to share.sharingItem属性也必须设置为指示要共享的项。

When specifying a role on macOS, label and accelerator are the only options that will affect the menu item. 在macOS上指定role时,labelaccelerator是唯一会影响菜单项的选项。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.enabledvisibility属性不适用于macOS托盘中的顶级菜单项。

Instance Properties实例属性

The following properties are available on instances of MenuItem:以下属性可用于MenuItem实例:

A string indicating the item's unique id, this property can be dynamically changed.一个表示项的唯一id的string,此属性可以动态更改。

A string indicating the item's visible label.一个表示项目的可见标签的string

A Function that is fired when the MenuItem receives a click event. MenuItem接收到单击事件时激发的FunctionIt can be called with menuItem.click(event, focusedWindow, focusedWebContents).它可以用menuItem.click(event, focusedWindow, focusedWebContents)调用。

A Menu (optional) containing the menu item's submenu, if present.包含菜单项的子菜单(如果存在)的Menu(可选)。

A string indicating the type of the item. 指示项目类型的stringCan be normal, separator, submenu, checkbox or radio.可以是normalseparatorsubmenucheckboxradio

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

An Accelerator (optional) indicating the item's accelerator, if set.一个Accelerator(可选),指示项目的加速器(如果已设置)。

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

A NativeImage | string (optional) indicating the item's icon, if set.一个NativeImage | string(可选),指示项目的图标(如果已设置)。

A string indicating the item's sublabel.指示项目子标签的string

A string indicating the item's hover text.指示项目悬停文本的string

A boolean indicating whether the item is enabled, this property can be dynamically changed.一个boolean,指示该项是否已启用,此属性可以动态更改。

A boolean indicating whether the item is visible, this property can be dynamically changed.指示项是否可见的boolean,此属性可以动态更改。

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函数。

A boolean indicating if the accelerator should be registered with the system or just displayed.一个boolean,指示加速器应该向系统注册还是只显示。

This property can be dynamically changed.此属性可以动态更改。

A SharingItem indicating the item to share when the role is shareMenu.一个SharingItem,指示roleshareMenu时要共享的项目。

This property can be dynamically changed.此属性可以动态更改。

A number indicating an item's sequential unique id.一个number,表示物品的顺序唯一id。

A Menu that the item is a part of.该项目所属的Menu