Skip to main content

MenuItem

类:MenuItem

🌐 Class: MenuItem

将项目添加到本地应用菜单和上下文菜单。

进程:主进程

🌐 Process: Main

请参阅 Menu 了解示例。

🌐 See Menu for examples.

warning

Electron 内置的类不能在用户代码中被继承。 欲了解更多信息,请参见 常见问题

new MenuItem(options)

  • options 对象
    • click Function(optional)- 当菜单项被点击时,将调用 click(menuItem, window, event)
    • role string(optional)- 可以是 undoredocutcopypastepasteAndMatchStyledeleteselectAllreloadforceReloadtoggleDevToolsresetZoomzoomInzoomOuttoggleSpellCheckertogglefullscreenwindowminimizeclosehelpaboutserviceshidehideOthersunhidequitshowSubstitutionstoggleSmartQuotestoggleSmartDashestoggleTextReplacementstartSpeakingstopSpeakingzoomfrontappMenufileMenueditMenuviewMenushareMenurecentDocumentstoggleTabBarselectNextTabselectPreviousTabshowAllTabsmergeAllWindowsclearRecentDocumentsmoveTabToNewWindowwindowMenu - 定义菜单项的操作,当指定时,click 属性将被忽略。请参阅 roles
    • type 字符串(可选)
      • normal
      • separator
      • submenu
      • checkbox
      • radio
      • header - 仅适用于 macOS 14 及更高版本。
      • palette - 仅适用于 macOS 14 及更高版本。
    • label 字符串(可选)
    • sublabel string (optional) macOS - 适用于 macOS 版本 >= 14.4
    • toolTip string (optional) macOS - 此菜单项的悬停文本。
    • accelerator string(optional)- 一个 加速器 字符串。
    • icon本地图片 | 字符串)(可选))- 可以是 NativeImage 或图标的文件路径。
    • enabled boolean(optional)- 如果为假,菜单项将显示为灰色且不可点击。
    • acceleratorWorksWhenHidden boolean (optional) macOS - 默认值是 true,当 false 时,如果项目不可见,将阻止加速器触发该项目。
    • visible boolean(optional)- 如果为假,该菜单项将被完全隐藏。
    • checked boolean(optional)- 应仅为 checkboxradio 类型的菜单项指定。
    • registerAccelerator boolean (optional) Linux Windows - 如果为假,加速器将不会注册到系统中,但仍会显示。默认值为真。
    • sharingItem SharingItem (optional) macOS - 当 roleshareMenu 时要共享的项目。
    • submenuMenuItemConstructorOptions[] | Menu)(可选))- 应为 submenu 类型的菜单项指定。如果指定了 submenu,则可以省略 type: 'submenu'。如果值不是 Menu,则将使用 Menu.buildFromTemplate 自动将其转换为 Menu
    • id string(optional)- 在单个菜单中唯一。如果定义了,则可以通过 position 属性将其用作对该项目的引用。
    • before string[](optional)- 将此项目插入到具有指定 ID 的项目之前。如果参考的项目不存在,该项目将被插入到菜单的末尾。同时,这也意味着相关的菜单项应放置在与该项目相同的“组”中。
    • after string[](optional)- 将此项插入到具有指定ID的项之后。如果引用的项不存在,该项将被插入到菜单的末尾。
    • beforeGroupContaining string[](optional)- 为单个上下文菜单提供了一种方式,以在具有指定 id 的项目的包含组之前声明其包含组的位置。
    • afterGroupContaining string[](optional)- 为单个上下文菜单提供了一种方式,以在具有指定 id 的项目的包含组之后声明其包含组的位置。
note

acceleratorWorksWhenHidden 被指定为仅适用于 macOS,因为在 Windows 和 Linux 上,当菜单项被隐藏时,快捷键始终有效。向用户提供此选项,是为了让他们可以关闭它,因为在原生 macOS 开发中这是可行的。

实例属性

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

该属性可以动态更改。

🌐 This property can be dynamically changed.

当菜单项接收到点击事件时触发的 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 及以上版本可用。

一个可选的 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 | null 指示该项目的快捷键(如果已设置)。

🌐 An Accelerator | null 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.buildFromTemplateMenu.append()/insert() 添加。在初始化之前访问,只会返回 null

一个 NativeImage | string(可选),表示该物品的图标,如果已设置。

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

该属性可以动态更改。

🌐 This property can be dynamically changed.

string 表示该物品的子标签。

🌐 A string indicating the item's sublabel.

该属性可以动态更改。

🌐 This property can be dynamically changed.

一个 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 表示物品的顺序唯一标识符。

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

物品所属的 Menu

🌐 A Menu that the item is a part of.