Menu
进程:
创建新菜单。
Menu
类有以下方法:
menu
Menu | null
在macOS上将 menu
设置成应用内菜单 在windows和Linux上,menu
将会被设置成窗口顶部菜单
在Windows和Linux中,可以在菜单的顶层标签的某个字母前添加&
以绑定快捷键。 例如,使用&File
后可以使用Alt-F
呼出File的子选项。 The indicated character in the button label then gets an underline, and the &
character is not displayed on the button label.
In order to escape the &
character in an item name, add a proceeding &
. For example, &&File
would result in &File
displayed on the button label.
传递 null
值可以禁用默认菜单。 在 Windows 和 Linux 上,使用此方法移除窗口上的菜单栏可能会有额外的效果。
注释:如果应用没有设置菜单的话,系统会生成一个默认菜单。 默认生成的菜单中包含了一些初始选项,例如 文件
,编辑
, 视图
,窗口
,帮助
。
返回 Menu | null
- 如果有设置, 则返回应用程序菜单, 如果没设置,则返回 null
。
注释:返回的 Menu
实例不支持动态添加或删除菜单项, 但仍然可以动态修改 实例属性 。
Menu.sendActionToFirstResponder(action)
macOS
将 action
发送到应用程序的第一个响应方。 这用于模拟默认的 macOS 菜单行为。 通常你可以使用的role
属性
有关 macOS 的本地操作的详细信息, 请参阅 。
template
(MenuItemConstructorOptions | MenuItem)[]
返回 Menu
一般来说, template
是一个options
类型的数组,用于构建MenuItem。 使用方法可参考前文。
You can also attach other fields to the element of the template
and they will become properties of the constructed menu items.
实例方法
menu
对象具有以下实例方法:
options
Object (可选)window
(可选) - 默认为选中窗口.x
Number (可选) - 默认为当前鼠标的位置。 如果指定了y
,则该选项必选。y
Number (可选) - 默认为当前鼠标的位置。 如果指定了x
,则该选项必选。positioningItem
Number (optional) macOS - The index of the menu item to be positioned under the mouse cursor at the specified coordinates. 默认值为 -1。callback
Function (optional) - 会在菜单关闭后被调用.
menu.closePopup([browserWindow])
browserWindow
(可选) - 默认为选中窗口.
关闭 browserWindow
中的上下文菜单。
menu.append(menuItem)
menuItem
将 menuItem
追加到菜单。
menu.getMenuItemById(id)
id
String
返回具有指定id
项的MenuItem | null
menu.insert(pos, menuItem)
pos
IntegermenuItem
菜单项
将 menuItem
插入菜单的 pos
位置。
Objects created with new Menu
or returned by Menu.buildFromTemplate
emit the following events:
注意: 某些事件仅在特定的操作系统上可用, 这些方法会被标记出来。
事件: ‘menu-will-show’
返回:
event
Event
调用menu.popup()
事件时触发该事件。
事件: ‘menu-will-close’
返回:
event
Event
手动关闭弹出,或使用 menu.closePopup()
方法关闭弹出时,触发该事件。
实例属性
menu
对象还具有以下属性:
menu.items
包含菜单项的 MenuItem []
数组。
每个 菜单
由多个 组成, 每个 MenuItem
可以有子菜单。
An example of creating the application menu with the simple template API:
渲染进程
To create menus initiated by the renderer process, send the required information to the main process using IPC and have the main process display the menu on behalf of the renderer.
Below is an example of showing a menu when the user right clicks the page:
// renderer
window.addEventListener('contextmenu', (e) => {
e.preventDefault()
ipcRenderer.send('show-context-menu')
})
ipcRenderer.on('context-menu-command', (e, command) => {
// ...
})
// main
const template = [
{
click: () => { event.sender.send('context-menu-command', 'menu-item-1') }
},
{ type: 'separator' },
{ label: 'Menu Item 2', type: 'checkbox', checked: true }
]
const menu = Menu.buildFromTemplate(template)
menu.popup(BrowserWindow.fromWebContents(event.sender))
})
MacOS有一些系统预定义的菜单,像是 and Windows
。 让你的菜单更像MacOS标准菜单,只需设置菜单role
值为如下示之一,Electron便会自动认出并设置成标准菜单,:
window
help
services
标准菜单项操作
macOS 已经为某些菜单项提供了标准操作, 如 about xxx
、 Hide xxx
和 Hide Others
。 若要将菜单项的操作设置为标准操作, 应设置菜单项的 role
属性。
主菜单的名称
在 macOS 中应用程序菜单的第一个项目的标签总是你的应用程序的名字, 无论你设置什么标签。 如要更改它, 请修改应用程序包的 Info. plist
文件。 See About Information Property List Files for more information.
The of browser windows can set the menu of certain browser windows.
你可以使用 before
, after
, beforeGroupContaining
, afterGroupContaining
和 id
来控制由 Menu.buildFromTemplate
生成的菜单项的位置.
before
- 在指定的标签之前插入菜单项。 如果引用值不存在,那么该菜单项会插在这个菜单的尾部。 这还意味着,菜单项应该被放置在与引用项相同的组中。after
- 在指定的标签之后插入菜单项。 如果引用值不存在,那么该菜单项会插在这个菜单的尾部。 这还意味着,菜单项应该被放置在与引用项相同的组中。beforeGroupContaining
- 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 label.afterGroupContaining
- 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 label.
默认情况下,除非有位置相关的属性,所有的菜单项会按照模板中的顺序排放。
模板:
菜单:
- 1
- 2
- 3
- 4
模板:
菜单:
- 3
- 4
- ---
- 1
- ---
- 2
模板:
菜单:
- ---
- 3
- 2