跳转到主内容

菜单项

菜单项

添加菜单项到应用程序菜单和上下文菜单中

进程:主进程

有关示例, 请参见 Menu

new MenuItem(options)

  • 选项 对象
    • click 函数(可选) - 单击菜单项时,将用 click(menuItem, browserWindow, event) 调用。
      • menuItem MenuItem
      • browserWindow BrowserWindow |未定义 - 如果没有打开窗口,则无法定义此定义。
      • event KeyboardEvent
    • 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. 参见 roles
    • type string (可选) - 可以为 normal, separator, submenu, checkboxradio
    • label string (可选)
    • sublabel string (可选)
    • toolTip string (可选) macOS - 菜单项的悬浮文本。
    • accelerator Accelerator (可选)
    • icon (NativeImage | string) (可选)
    • enabled boolean (可选) - 如果为 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 boolean (可选) - 如果为 false,菜单项将完全的隐藏。
    • checked boolean (可选) - 应仅为指定 checkboxradio 类型的菜单项。
    • registerAccelerator boolean (可选) Linux Windows - 如果为 false,快捷键不会在系统中注册,但是任然会显示。 默认值为 true。
    • sharingItem SharingItem (可选) macOS - 当 roleshareMenu时指定要分享的项
    • submenu (MenuItemConstructorOptions[] | Menu) (可选) - 应该是固定类型菜单submenu 如果指定 submenu ,可以省略 type: 'submenu' 如果该值不属于Menu,它将被函数Menu.buildFromTemplate自动转换。
    • id string (可选) - 单个菜单内唯一。 若定义,则可以通过 position 位置属性找到此选项
    • before string[] (optional) - Inserts this item before the item with the specified id. 如果引用值不存在,那么该菜单项会插在这个菜单的尾部。 这还意味着,菜单项应该被放置在与引用项相同的组中。
    • after string[] (optional) - Inserts this item after the item with the specified 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 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.

注意: acceleratorWorksWhenHidden 这个选项只有 macOS 生效,因为在 Windows 和 Linux 系统下快捷键即使在隐藏情况下也会生效。 该选项让用户可以选择关闭,因为这是本地 macOS 开发中的可能。

角色

可以通过角色来为menu添加预定义行为。

最好给任何一个菜单指定 role去匹配一个标准角色, 而不是尝试在 click 函数中手动实现该行为。 内置的 role 行为将提供最佳的原生体验。

使用 role 时, labelaccelerator 值是可选的, 并为每个平台,将默认为适当值。

每个菜单项必须有一个 role, label, 或者一个分隔符一个 type

role 属性可以具有以下值:

  • undo - 撤销。
  • about - 触发原生信息面板 (在 Window 中自定义消息盒子, 本身不提供).
  • redo
  • cut - 剪切。
  • copy - 复制。
  • paste - 粘贴。
  • pasteAndMatchStyle
  • selectAll - 全选。
  • 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 the showAllTabs action.
  • 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 时, labelaccelerator 是影响菜单项的唯一选项。 所有其它选项都将被忽略。 不过,仍然支持小写的role,如toggledevtools

注意: 对于托盘上的顶级菜单项不可用的 enabledvisibility 属性

实例属性

以下为 MenuItem 实例的可用属性:

string 指定了该选项唯一的id,此属性可被动态更改。

string 表明选项的子标签

MenuItem接收到点击事件后自动触发的方法 Function 。 调用方法为 menuItem.click(event, focusedWindow, focusedWebContents).

A Menu (可选) 指定子菜单

string 表示菜单项的类型 可以是 normal, separator, submenu, checkboxradio.

一个 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

Accelerator (可选) 若存在则指向该项的快捷键

一个 Accelerator | null 值, 表示菜单项的 user-assigned accelerator 项。

注意: 此属性仅在 MenuItem 添加到 Menu 后才初始化。 通过 Menu.buildFromTemplateMenu.append()/insert()。 初始化前访问将只返回 null

NativeImage | string (可选) 若设置, 则从对应的值中找选项图标

string 表明选项的子标签

string 表明选项的子标签

boolean 表示是否启用该项,这个属性可以动态更改 。

boolean 表示是否启用该项,这个属性可以动态更改 。

boolean 表示是否启用该项,这个属性可以动态更改 。

checkbox 菜单项将在选中时切换 checked 的开关属性。

单选菜单项 将返回单击时checked属性, 并将关闭同一菜单中所有相邻项的属性。

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

一个 boolean 值 ,表示快捷键应用被系统注册或仅显示出来。

此属性可以动态更改。

SharingItem 表示了当 roleshareMenu 时要分享的项。

此属性可以动态更改。

number 表示一个项目的序列唯一ID。

多个选项组成一个 Menu