菜单项
菜单项
添加菜单项到应用程序菜单和上下文菜单中
Process: Main
See Menu for examples.
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. 参见 rolestypestring (可选) - 可以为normal,separator,submenu,checkbox或radio。labelstring (可选)sublabelstring (可选)toolTipstring (可选) macOS - 菜单项的悬浮文本。acceleratorAccelerator (optional)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 这个选项只有 macOS 生效,因为在 Windows 和 Linux 系统下快捷键即使在隐藏情况下也会生效。 该选项让用户可以选择关闭,因为这是本地 macOS 开发中的可能。
角色
可以通过角色来为menu添加预定义行为。
最好给任何一个菜单指定 role去匹配一个标准角色, 而不是尝试在 click 函数中手动实现该行为。 内置的 role 行为将提供最佳的原生体验。
使用 role 时, label 和 accelerator 值是可选的, 并为每个平台,将默认为适当值。
每个菜单项必须有一个 role, label, 或者一个分隔符一个 type
role 属性可以具有以下值:
undo - 撤销。about- 触发原生信息面板 (在 Window 中自定义消息盒子, 本身不提供).redocut - 剪切。copy - 复制。paste - 粘贴。pasteAndMatchStyleselectAll - 全选。delete -删除minimize- 最小化当前窗口。close- 关闭当前窗口quit- 退出程序reload- 重新加载当前窗口。forcereload- 忽略缓存,重新加载当前窗口。toggledevtools- 在当前窗口中隐藏/显示开发者工具。togglefullscreen- 将当前窗口切换至全屏模式。resetzoom- 将主页的缩放级别重置为初始大小.zoomin- 主页面放大 10%.zoomout-主页面缩小 10%.toggleSpellChecker- 启用/禁用内置拼写检查器。fileMenu- 默认的“文件”菜单(关闭/退出)editMenu-默认的 "编辑" 菜单 (包括撤消、复制等)viewMenu- 默认的“视图”菜单(包括重新加载、开发者工具等)windowMenu- 默认的“窗口”菜单(包括最小化、缩放等)
以下其他角色可在 macOS 上找到:
appMenu- 整个默认"App" menu (About, Services, 等)hide-映射到hide操作.hideOthers-映射到hideOtherApplications操作.unhide- 映射到unhideAllApplications动作showSubstitutions- 映射到orderFrontSubstitutionsPanel动作toggleSmartQuotes- 映射到toggleAutomaticQuoteSubstitution动作toggleSmartDashes- 映射到toggleAutomaticDashSubstitution动作toggleTextReplacement- 映射到toggleAutomaticTextReplacement动作startSpeaking-映射到startSpeaking操作.stopSpeaking-映射到stopSpeaking操作.front- 映射到arrangeInFront动作zoom- 映射到performZoom动作toggleTabBar-映射到toggleTabBar操作.selectNextTab- 映射到selectNextTab操作.selectPreviousTab- 映射到selectPreviousTab操作.showAllTabs- Map to theshowAllTabsaction.mergeAllWindows- 映射到mergeAllWindows操作.moveTabToNewWindow- 映射到moveTabToNewWindow操作.window- 这个子菜单是"Window" 菜单.help-这个子菜单是 "Help" 菜单.services- 子菜单是 "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.recentDocuments-这个子菜单是 "Open Recent" 菜单.clearRecentDocuments-映射到clearRecentDocuments操作.shareMenu- share menu的子菜单。sharingItem属性必须被设置, 用于指向被分享的项
在 macOS 上指定 role 时, label 和 accelerator 是影响菜单项的唯一选项。 所有其它选项都将被忽略。 不过,仍然支持小写的role,如toggledevtools。
注意: 对于托盘上的顶级菜单项不可用的 enabled 和 visibility 属性
实例属性
以下为 MenuItem 实例的可用属性:
menuItem.id
string 指定了该选项唯一的id,此属性可被动态更改。
menuItem.label
string 表明选项的子标签
menuItem.click
MenuItem接收到点击事件后自动触发的方法 Function 。 调用方法为 menuItem.click(event, focusedWindow, focusedWebContents).
eventKeyboardEventfocusedWindowBaseWindowfocusedWebContentsWebContents
menuItem.submenu
A Menu (可选) 指定子菜单
menuItem.type
string 表示菜单项的类型 可以是 normal, separator, submenu, checkbox 或 radio.
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 项。
注意: 此属性仅在 MenuItem 添加到 Menu 后才初始化。 通过 Menu.buildFromTemplate 或 Menu.append()/insert()。 初始化前访问将只返回 null。
menuItem.icon
NativeImage | string (可选) 若设置, 则从对应的值中找选项图标
menuItem.sublabel
string 表明选项的子标签
menuItem.toolTip macOS
string 表明选项的子标签
menuItem.enabled
boolean 表示是否启用该项,这个属性可以动态更改 。
menuItem.visible
boolean 表示是否启用该项,这个属性可以动态更改 。
menuItem.checked
boolean 表示是否启用该项,这个属性可以动态更改 。
checkbox 菜单项将在选中时切换 checked 的开关属性。
单选菜单项 将返回单击时checked属性, 并将关闭同一菜单中所有相邻项的属性。
你可以为其他行为添加click函数。
menuItem.registerAccelerator
一个 boolean 值 ,表示快捷键应用被系统注册或仅显示出来。
此属性可以动态更改。
menuItem.sharingItem macOS
SharingItem 表示了当 role 是 shareMenu 时要分享的项。
此属性可以动态更改。
menuItem.commandId
number 表示一个项目的序列唯一ID。
menuItem.menu
多个选项组成一个 Menu