Skip to main content

MenuItem

类:MenuItem

¥Class: MenuItem

将项目添加到原生应用菜单和上下文菜单。

¥Add items to native application menus and context menus.

进程:主进程

¥Process: Main

示例请参见 Menu

¥See Menu for examples.

[!警告] Electron 的内置类无法在用户代码中进行子类化。更多信息,请参阅 常见问题解答

¥[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.

new MenuItem(options)

  • options 对象

    ¥options Object

    • click 功能(可选) - 单击菜单项时将使用 click(menuItem, window, event) 进行调用。

      ¥click Function (optional) - Will be called with click(menuItem, window, event) when the menu item is clicked.

      • menuItem MenuItem

      • window BaseWindow | 不明确的 - 如果没有打开窗口,则不会定义此值。

        ¥window BaseWindow | undefined - This will not be defined if no window is open.

      • event KeyboardEvent

    • role 字符串(可选) - 可以是 undoredocutcopypastepasteAndMatchStyledeleteselectAllreloadforceReloadtoggleDevToolsresetZoomzoomInzoomOuttoggleSpellCheckertogglefullscreenwindowminimizeclosehelpaboutserviceshidehideOthersunhidequitshowSubstitutionstoggleSmartQuotestoggleSmartDashestoggleTextReplacementstartSpeakingstopSpeakingzoomfrontappMenufileMenueditMenuviewMenushareMenurecentDocumentstoggleTabBarselectNextTabselectPreviousTabshowAllTabsmergeAllWindowsclearRecentDocumentsmoveTabToNewWindowwindowMenu - 定义菜单项的操作,指定时 click 属性将被忽略。参见 roles

      ¥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, showAllTabs, mergeAllWindows, clearRecentDocuments, moveTabToNewWindow or windowMenu - Define the action of the menu item, when specified the click property will be ignored. See roles.

    • type 字符串(可选)

      ¥type string (optional)

      • normal

      • separator

      • submenu

      • checkbox

      • radio

      • header - 仅适用于 macOS 14 及更高版本。

        ¥header - Only available on macOS 14 and up.

      • palette - 仅适用于 macOS 14 及更高版本。

        ¥palette - Only available on macOS 14 and up.

    • label 字符串(可选)

      ¥label string (optional)

    • sublabel 字符串(可选)macOS - 适用于 macOS >= 14.4

      ¥sublabel string (optional) macOS - Available in macOS >= 14.4

    • toolTip 字符串(可选)macOS - 悬停此菜单项的文本。

      ¥toolTip string (optional) macOS - Hover text for this menu item.

    • accelerator 字符串(可选) - 加速器 字符串。

      ¥accelerator string (optional) - An Accelerator string.

    • iconNativeImage | 字符串)(可选)

      ¥icon (NativeImage | string) (optional)

    • enabled 布尔值(可选) - 如果为 false,菜单项将显示为灰色且不可单击。

      ¥enabled boolean (optional) - If false, the menu item will be greyed out and unclickable.

    • acceleratorWorksWhenHidden 布尔值(可选)macOS - 默认为 true,当 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.

    • visible 布尔值(可选) - 如果为 false,则菜单项将完全隐藏。

      ¥visible boolean (optional) - If false, the menu item will be entirely hidden.

    • checked 布尔值(可选) - 只能为 checkboxradio 类型的菜单项指定。

      ¥checked boolean (optional) - Should only be specified for checkbox or radio type menu items.

    • registerAccelerator 布尔值(可选) Linux Windows - 如果为 false,则加速器不会在系统中注册,但仍会显示。默认为 true。

      ¥registerAccelerator boolean (optional) Linux Windows - If false, the accelerator won't be registered with the system, but it will still be displayed. Defaults to true.

    • sharingItem 共享项目(可选)macOS - 当 roleshareMenu 时要共享的项目。

      ¥sharingItem SharingItem (optional) macOS - The item to share when the role is shareMenu.

    • submenu (MenuItemConstructorOptions[] | Menu)(可选) - 应为 submenu 类型菜单项指定。如果指定了 submenu,则可以省略 type: 'submenu'。如果该值不是 Menu,则会使用 Menu.buildFromTemplate 自动转换为值。

      ¥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 字符串(可选) - 在单一菜单中独一无二。如果定义了,则可以通过位置属性将其用作对此项目的引用。

      ¥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 字符串[](可选) - 将此项目插入到具有指定 id 的项目之前。如果引用的项目不存在,该项目将被插入到菜单的末尾。还意味着相关菜单项应放置在与该项目相同的“组”中。

      ¥before string[] (optional) - Inserts this item before the item with the specified id. 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 字符串[](可选) - 将此项目插入到具有指定 id 的项目之后。如果引用的项目不存在,该项目将被插入到菜单的末尾。

      ¥after string[] (optional) - Inserts this item after the item with the specified id. If the referenced item doesn't exist the item will be inserted at the end of the menu.

    • beforeGroupContaining 字符串[](可选) - 为单个上下文菜单提供一种方法,以声明其包含组位于具有指定 id 的项目的包含组之前。

      ¥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 id.

    • afterGroupContaining 字符串[](可选) - 为单个上下文菜单提供一种方法,以声明其包含组在具有指定 id 的项目的包含组之后的位置。

      ¥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 id.

note

acceleratorWorksWhenHidden 被指定为仅适用于 macOS,因为在 Windows 和 Linux 上,当项目隐藏时,加速器始终有效。该选项向用户公开,让他们可以选择关闭它,因为这在原生 macOS 开发中是可能的。

¥[!NOTE] acceleratorWorksWhenHidden is specified as being macOS-only because accelerators always work when items are hidden on Windows and Linux. The option is exposed to users to give them the option to turn it off, as this is possible in native macOS development.

实例属性

¥Instance Properties

以下属性在 MenuItem 的实例上可用:

¥The following properties are available on instances of MenuItem:

string 表示菜单项的唯一 ID。该属性可以动态更改。

¥A string indicating the item's unique id. This property can be dynamically changed.

string 表示该项目的可见标签。

¥A string indicating the item's visible label.

当 MenuItem 收到单击事件时触发的 Function。可以用 menuItem.click(event, focusedWindow, focusedWebContents) 来调用。

¥A Function that is fired when the MenuItem receives a click event. It can be called with menuItem.click(event, focusedWindow, focusedWebContents).

包含菜单项的子菜单(如果存在)的 Menu(可选)。

¥A Menu (optional) containing the menu item's submenu, if present.

string 指示项目的类型。可以是 normalseparatorsubmenucheckboxradioheaderpalette

¥A string indicating the type of the item. Can be normal, separator, submenu, checkbox, radio, header or palette.

note

headerpalette 仅适用于 macOS 14 及更高版本。

¥[!NOTE] header and palette are only available on macOS 14 and up.

string(可选)指示项目的角色(如果已设置)。可以是 undoredocutcopypastepasteAndMatchStyledeleteselectAllreloadforceReloadtoggleDevToolsresetZoomzoomInzoomOuttoggleSpellCheckertogglefullscreenwindowminimizeclosehelpaboutserviceshidehideOthersunhidequitstartSpeakingstopSpeakingzoomfrontappMenufileMenueditMenuviewMenushareMenurecentDocumentstoggleTabBarselectNextTabselectPreviousTabshowAllTabsmergeAllWindowsclearRecentDocumentsmoveTabToNewWindowwindowMenu

¥A string (optional) indicating the item's role, if set. 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, showAllTabs, mergeAllWindows, clearRecentDocuments, moveTabToNewWindow or windowMenu

Accelerator(可选)指示项目的加速器(如果已设置)。

¥An Accelerator (optional) indicating the item's accelerator, if set.

¥menuItem.userAccelerator Readonly macOS

Accelerator | null 指示菜单项的 用户指定的加速器

¥An Accelerator | null indicating the item's user-assigned accelerator for the menu item.

[!NOTE] 此属性仅在将 MenuItem 添加到 Menu 后初始化。通过 Menu.buildFromTemplate 或通过 Menu.append()/insert()。初始化之前访问只会返回 null

¥[!NOTE] This property is only initialized after the MenuItem has been added to a Menu. Either via Menu.buildFromTemplate or via Menu.append()/insert(). Accessing before initialization will just return null.

NativeImage | string(可选)指示项目的图标(如果已设置)。

¥A NativeImage | string (optional) indicating the item's icon, if set.

string 表示该项目的子标签。

¥A string indicating the item's sublabel.

string 指示项目的悬停文本。

¥A string indicating the item's hover text.

boolean 表示菜单项是否已启用。该属性可以动态更改。

¥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.

选中 checkbox 菜单项后,将打开和关闭 checked 属性。

¥A checkbox menu item will toggle the checked property on and off when selected.

单击 radio 菜单项将打开其 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.

你可以添加 click 函数以实现其他行为。

¥You can add a click function for additional behavior.

boolean 指示加速器是否应在系统中注册或仅显示。

¥A boolean indicating if the accelerator should be registered with the system or just displayed.

该属性可以动态更改。

¥This property can be dynamically changed.

SharingItem 表示当 roleshareMenu 时要共享的项目。

¥A SharingItem indicating the item to share when the role is shareMenu.

该属性可以动态更改。

¥This property can be dynamically changed.

number 指示项目的连续唯一 ID。

¥A number indicating an item's sequential unique id.

该项目所属的 Menu

¥A Menu that the item is a part of.