Menu
菜单
创建原生应用菜单和上下文菜单。
Process: Main
[!TIP] See also: A detailed guide about how to implement menus in your application.
[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new Menu()
创建新菜单。
静态方法
Menu
类有以下方法:
Menu.setApplicationMenu(menu)
menu
Menu | null
在macOS上将 menu
设置成应用内菜单 在windows和Linux上,menu
将会被设置成窗口顶部菜单
在Windows和Linux中,可以在菜单的顶层标签的某个字母前添加&
以绑定快捷键。 例如,使用&File
后可以使用Alt-F
呼出File的子选项。 按钮标签中指定的字符会出现下划线, &
字符不会显示在按钮标签上。
要转义并在项名中显示 &
字符, 可以添加字符 &
. 比如, &&File
将在按钮标签上出现 &File
传递 null
值可以禁用默认菜单。 在 Windows 和 Linux 上,使用此方法移除窗口上的菜单栏可能会有额外的效果。
[!NOTE] The default menu will be created automatically if the app does not set one. It contains standard items such as
File
,Edit
,View
,Window
andHelp
.
Menu.getApplicationMenu()
返回 Menu | null
- 如果有设置, 则返回应用程序菜单, 如果没设置,则返回 null
。
[!NOTE] The returned
Menu
instance doesn't support dynamic addition or removal of menu items. Instance properties can still be dynamically modified.
Menu.sendActionToFirstResponder(action)
macOS
action
string
将 action
发送到应用程序的第一个响应方。 这用于模拟默认的 macOS 菜单行为。 Usually you would use the role
property of a MenuItem
.
See the macOS Cocoa Event Handling Guide for more information on macOS' native actions.
Menu.buildFromTemplate(template)
template
(MenuItemConstructorOptions | MenuItem)[]
Returns Menu
Generally, the template
is an array of options
for constructing a MenuItem. 使用方法可参考前文。
您还可以将其他字段附加到template
,它们将成为菜单项的属性。
实例方法
menu
对象具有以下实例方法:
menu.popup([options])
Pops up this menu as a context menu in the BaseWindow
.
[!TIP] For more details, see the Context Menu guide.
menu.closePopup([window])
window
BaseWindow (optional) - Default is the focused window.
Closes the context menu in the window
.
menu.append(menuItem)
menuItem
MenuItem
将 menuItem
追加到菜单。
menu.getMenuItemById(id)
id
字符串
返回具有指定id
项的MenuItem | null
menu.insert(pos, menuItem)
pos
IntegermenuItem
MenuItem
将 menuItem
插入菜单的 pos
位置。
实例事件
使用 new Menu
创建对象或通过 Menu.buildFromTemplate
返回对象均会触发下列事件:
[!NOTE] Some events are only available on specific operating systems and are labeled as such.
事件: 'menu-will-show'
返回:
event
Event
调用menu.popup()
事件时触发该事件。
事件: 'menu-will-close'
返回:
event
Event
手动关闭弹出,或使用 menu.closePopup()
方法关闭弹出时,触发该事件。
实例属性
menu
对象还具有以下属性:
menu.items
包含菜单项的 MenuItem []
数组。
Each Menu
consists of multiple MenuItem
instances and each MenuItem
can nest a Menu
into its submenu
property.