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)
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:
menuItem.id
string 表示菜单项的唯一 ID。该属性可以动态更改。
¥A string indicating the item's unique id. This property can be
dynamically changed.
menuItem.label
string 表示该项目的可见标签。
¥A string indicating the item's visible label.
menuItem.click
当 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).
-
eventKeyboardEvent -
focusedWindowBaseWindow -
focusedWebContentsWebContents
menuItem.submenu
包含菜单项的子菜单(如果存在)的 Menu(可选)。
¥A Menu (optional) containing the menu
item's submenu, if present.
menuItem.type
string 指示项目的类型。可以是 normal、separator、submenu、checkbox、radio、header 或 palette。
¥A string indicating the type of the item. Can be normal, separator, submenu, checkbox, radio, header or palette.
header 和 palette 仅适用于 macOS 14 及更高版本。
¥[!NOTE]
header and palette are only available on macOS 14 and up.
menuItem.role
string(可选)指示项目的角色(如果已设置)。可以是 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 或 windowMenu
¥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
menuItem.accelerator
Accelerator(可选)指示项目的加速器(如果已设置)。
¥An Accelerator (optional) indicating the item's accelerator, if set.
menuItem.userAccelerator 只读 macOS
¥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
MenuItemhas been added to aMenu. Either viaMenu.buildFromTemplateor viaMenu.append()/insert(). Accessing before initialization will just returnnull.
menuItem.icon
NativeImage | string(可选)指示项目的图标(如果已设置)。
¥A NativeImage | string (optional) indicating the
item's icon, if set.
menuItem.sublabel
string 表示该项目的子标签。
¥A string indicating the item's sublabel.
menuItem.toolTip macOS
string 指示项目的悬停文本。
¥A string indicating the item's hover text.
menuItem.enabled
boolean 表示菜单项是否已启用。该属性可以动态更改。
¥A boolean indicating whether the item is enabled. This property can be
dynamically changed.
menuItem.visible
boolean 表示菜单项是否可见。该属性可以动态更改。
¥A boolean indicating whether the item is visible. This property can be
dynamically changed.
menuItem.checked
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.
menuItem.registerAccelerator
boolean 指示加速器是否应在系统中注册或仅显示。
¥A boolean indicating if the accelerator should be registered with the
system or just displayed.
该属性可以动态更改。
¥This property can be dynamically changed.
menuItem.sharingItem macOS
SharingItem 表示当 role 为 shareMenu 时要共享的项目。
¥A SharingItem indicating the item to share when the role is shareMenu.
该属性可以动态更改。
¥This property can be dynamically changed.
menuItem.commandId
number 指示项目的连续唯一 ID。
¥A number indicating an item's sequential unique id.
menuItem.menu
该项目所属的 Menu。
¥A Menu that the item is a part of.