Menu
菜单
Create application menus and context menus.
ProcessMain
The presentation of menus varies depending on the operating system:
- Under Windows and Linux, menus are visually similar to Chromium.
- Under macOS, these will be native menus.
[!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)
menuMenu | null
在macOS上将 menu设置成应用内菜单在windows和Linux上,menu 将会被设置成窗口顶部菜单
On Linux, the menu bar uses GTK theme colors when GTK is available. If GTK cannot initialize, it uses Chromium's built-in light or dark menu colors, following nativeTheme.shouldUseDarkColors.
在Windows和Linux中,可以在菜单的顶层标签的某个字母前添加&以绑定快捷键。例如,使用&File后可以使用Alt-F呼出File的子选项。按钮标签中指定的字符会出现下划线, & 字符不会显示在按钮标签上。
In order to escape the & character in an item name, add a preceding &. 比如, &&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, andWindow.
Menu.getApplicationMenu()
返回 Menu | null - 如果有设置, 则返回应用程序菜单, 如果没设置,则返回 null。
[!NOTE] The returned
Menuinstance doesn't support dynamic addition or removal of menu items. Instance properties can still be dynamically modified.
Menu.setHighlightColors(options) Windows
History
| Version(s) | Changes |
|---|---|
None | API ADDED |
Sets the colors used to highlight the hovered or keyboard-focused item in every menu the app shows, including context menus and the menu bar's dropdowns. Light and dark colors are set separately, and any color left unset keeps the default for that mode. Each call replaces the colors set by any previous call. Passing null restores the defaults.
const { Menu } = require('electron')
Menu.setHighlightColors({
light: { background: '#0067c0', foreground: '#ffffff' },
dark: { background: '#4cc2ff', foreground: '#000000' }
})
Menus opened after the call use the new colors. The colors are not applied while Windows high contrast mode or forced colors are active, so the user's system colors take priority.
This method has no effect on macOS and Linux.
Menu.sendActionToFirstResponder(action) macOS
actionstring
将 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])
windowBaseWindow (optional) - Default is the focused window.
Closes the context menu in the window.
menu.append(menuItem)
menuItemMenuItem
将 menuItem 追加到菜单。
menu.getMenuItemById(id)
id字符串
Returns MenuItem | null - the item with the specified id
menu.insert(pos, menuItem)
posIntegermenuItemMenuItem
将 menuItem 插入菜单的 pos 位置。
实例事件
使用 new Menu 创建对象或通过 Menu.buildFromTemplate返回对象均会触发下列事件:
[!NOTE] Some events are only available on specific operating systems and are labeled as such.
事件: 'menu-will-show'
返回:
eventEvent
调用menu.popup()事件时触发该事件。
事件: 'menu-will-close'
返回:
eventEvent
手动关闭弹出,或使用 menu.closePopup()方法关闭弹出时,触发该事件。
实例属性
menu 对象还具有以下属性:
menu.items
A MenuItem[] array containing the menu's items.
Each Menu consists of multiple MenuItem instances and each MenuItem can nest a Menu into its submenu property.