MenuItem
菜单项
添加菜单项到应用程序菜单和上下文菜单中
Process: Main
See Menu for examples.
[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new MenuItem(options)
选项对象click函数(可选) - 单击菜单项时,将用click(menuItem, window, event)调用。menuItemMenuItemwindowBaseWindow | undefined - This will not be defined if no window is open.eventKeyboardEvent
rolestring (optional) - Can beundo,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,moveTabToNewWindoworwindowMenu- Define the action of the menu item, when specified theclickproperty will be ignored. See roles.typestring(可选)普通separatorsubmenu复选框radioheader- Only available on macOS 14 and up.palette- Only available on macOS 14 and up.
labelstring (可选)sublabelstring (optional) macOS - Available in macOS >= 14.4toolTipstring (可选) macOS - 菜单项的悬浮文本。acceleratorstring (optional) - An Accelerator string.icon(NativeImage | string) (optional)enabledboolean (可选) - 如果为 false,菜单项将置灰,同时不可点击。acceleratorWorksWhenHiddenboolean (optional) macOS - default istrue, and whenfalsewill prevent the accelerator from triggering the item if the item is not visible.visibleboolean (可选) - 如果为 false,菜单项将完全的隐藏。checkedboolean (可选) - 应仅为指定checkbox或radio类型的菜单项。registerAcceleratorboolean (可选) Linux Windows - 如果为 false,快捷键不会在系统中注册,但是任然会显示。 默认值为 true。sharingItemSharingItem (可选) macOS - 当role为shareMenu时指定要分享的项submenu(MenuItemConstructorOptions[] | Menu) (可选) - 应该是固定类型菜单submenu如果指定submenu,可以省略type: 'submenu'如果该值不属于Menu,它将被函数Menu.buildFromTemplate自动转换。idstring (可选) - 单个菜单内唯一。 若定义,则可以通过 position 位置属性找到此选项beforestring[] (optional) - Inserts this item before the item with the specified id. 如果引用值不存在,那么该菜单项会插在这个菜单的尾部。 这还意味着,菜单项应该被放置在与引用项相同的组中。afterstring[] (optional) - Inserts this item after the item with the specified id. 如果引用值不存在,那么该菜单项会插在这个菜单的尾部。beforeGroupContainingstring[] (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.afterGroupContainingstring[] (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.
acceleratorWorksWhenHidden is specified as being macOS-only because accelerators always work when items are hidden on Windows and Linux. 该选项让用户可以选择关闭,因为这是本地 macOS 开发中的可能。
实例属性
以下为 MenuItem 实例的可用属性:
menuItem.id
A string indicating the item's unique id. This property can be dynamically changed.
menuItem.label
string 表明选项的子标签
menuItem.click
MenuItem接收到点击事件后自动触发的方法 Function 。 调用方法为 menuItem.click(event, focusedWindow, focusedWebContents).
eventKeyboardEventfocusedWindowBaseWindowfocusedWebContentsWebContents
menuItem.submenu
A Menu (可选) 指定子菜单
menuItem.type
string 表示菜单项的类型 Can be normal, separator, submenu, checkbox, radio, header or palette.
header and palette are only available on macOS 14 and up.
menuItem.role
一个 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, showAllTabs, mergeAllWindows, clearRecentDocuments, moveTabToNewWindow or windowMenu
menuItem.accelerator
Accelerator (可选) 若存在则指向该项的快捷键
menuItem.userAccelerator 只读 macOS
一个 Accelerator | null 值, 表示菜单项的 user-assigned accelerator 项。
[!NOTE] This property is only initialized after the
MenuItemhas been added to aMenu. 通过Menu.buildFromTemplate或Menu.append()/insert()。 初始化前访问将只返回null。
menuItem.icon
NativeImage | string (可选) 若设置, 则从对应的值中找选项图标
menuItem.sublabel
string 表明选项的子标签
menuItem.toolTip macOS
string 表明选项的子标签
menuItem.enabled
A boolean indicating whether the item is enabled. This property can be dynamically changed.
menuItem.visible
A boolean indicating whether the item is visible. This property can be dynamically changed.
menuItem.checked
A boolean indicating whether the item is checked. This property can be dynamically changed.
checkbox 菜单项将在选中时切换 checked 的开关属性。
单选菜单项 将返回单击时checked属性, 并将关闭同一菜单中所有相邻项的属性。
你可以为其他行为添加click函数。
menuItem.registerAccelerator
一个 boolean 值 ,表示快捷键应用被系统注册或仅显示出来。
此属性可以动态更改。
menuItem.sharingItem macOS
SharingItem 表示了当 role 是 shareMenu 时要分享的项。
此属性可以动态更改。
menuItem.commandId
number 表示一个项目的序列唯一ID。
menuItem.menu
多个选项组成一个 Menu