Menu
类:Menu
¥Class: Menu
创建原生应用菜单和上下文菜单。
¥Create native application menus and context menus.
进程:主进程
¥Process: Main
[!TIP] 另请参阅:关于如何在应用中实现菜单的详细指南。
¥[!TIP] See also: A detailed guide about how to implement menus in your application.
[!警告] Electron 的内置类无法在用户代码中进行子类化。更多信息,请参阅 常见问题解答。
¥[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new Menu()
创建一个新菜单。
¥Creates a new menu.
静态方法
¥Static Methods
Menu
类具有以下静态方法:
¥The Menu
class has the following static methods:
Menu.setApplicationMenu(menu)
menu
Menu | null
将 menu
设置为 macOS 上的应用菜单。在 Windows 和 Linux 上,menu
将被设置为每个窗口的顶部菜单。
¥Sets menu
as the application menu on macOS. On Windows and Linux, the
menu
will be set as each window's top menu.
此外,在 Windows 和 Linux 上,你可以在顶层项目名称中使用 &
来指示哪个字母应获得生成的加速器。例如,使用 &File
作为文件菜单将生成一个打开关联菜单的 Alt-F
加速器。然后,按钮标签中指示的字符将带有下划线,并且 &
字符不会显示在按钮标签上。
¥Also on Windows and Linux, you can use a &
in the top-level item name to
indicate which letter should get a generated accelerator. For example, using
&File
for the file menu would result in a generated Alt-F
accelerator that
opens the associated menu. The indicated character in the button label then gets an
underline, and the &
character is not displayed on the button label.
为了转义项目名称中的 &
字符,请添加前面的 &
。例如,&&File
将导致 &File
显示在按钮标签上。
¥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 上,这具有从窗口中删除菜单栏的附加效果。
¥Passing null
will suppress the default menu. On Windows and Linux,
this has the additional effect of removing the menu bar from the window.
[!NOTE] 如果应用未设置默认菜单,则会自动创建默认菜单。它包含
File
、Edit
、View
、Window
和Help
等标准项目。¥[!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
(如果未设置)。
¥Returns Menu | null
- The application menu, if set, or null
, if not set.
[!NOTE] 返回的
Menu
实例不支持动态添加或删除菜单项。实例属性 仍然可以动态修改。¥[!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
字符串¥
action
string
将 action
发送给应用的第一响应者。这用于模拟默认的 macOS 菜单行为。通常你会使用 MenuItem
的 role
属性。
¥Sends the action
to the first responder of application. This is used for
emulating default macOS menu behaviors. Usually you would use the
role
property of a MenuItem
.
有关 macOS 原生操作的更多信息,请参阅 macOS Cocoa 事件处理指南。
¥See the macOS Cocoa Event Handling Guide for more information on macOS' native actions.
Menu.buildFromTemplate(template)
-
template
(菜单项构造选项 | 菜单项)[]¥
template
(MenuItemConstructorOptions | MenuItem)[]
返回 Menu
¥Returns Menu
一般情况下,template
是 options
的数组,用于构造 MenuItem。用法可以参考上面。
¥Generally, the template
is an array of options
for constructing a
MenuItem. The usage can be referenced above.
你还可以将其他字段附加到 template
的元素,它们将成为构造的菜单项的属性。
¥You can also attach other fields to the element of the template
and they will become properties of the constructed menu items.
实例方法
¥Instance Methods
menu
对象具有以下实例方法:
¥The menu
object has the following instance methods:
menu.popup([options])
弹出此菜单作为 BaseWindow
中的上下文菜单。
¥Pops up this menu as a context menu in the BaseWindow
.
[!TIP] 更多详情,请参阅 上下文菜单 指南。
¥[!TIP] For more details, see the Context Menu guide.
menu.closePopup([window])
-
window
BaseWindow(可选) - 默认是聚焦窗口。¥
window
BaseWindow (optional) - Default is the focused window.
关闭 window
中的上下文菜单。
¥Closes the context menu in the window
.
menu.append(menuItem)
menuItem
MenuItem
将 menuItem
添加到菜单。
¥Appends the menuItem
to the menu.
menu.getMenuItemById(id)
-
id
字符串¥
id
string
返回 MenuItem | null
具有指定 id
的项目
¥Returns MenuItem | null
the item with the specified id
menu.insert(pos, menuItem)
-
pos
整数¥
pos
Integer -
menuItem
MenuItem
将 menuItem
插入到菜单的 pos
位置。
¥Inserts the menuItem
to the pos
position of the menu.
实例事件
¥Instance Events
使用 new Menu
创建或由 Menu.buildFromTemplate
返回的对象会发出以下事件:
¥Objects created with new Menu
or returned by Menu.buildFromTemplate
emit the following events:
[!NOTE] 某些事件仅在特定操作系统上可用,并会进行相应的标记。
¥[!NOTE] Some events are only available on specific operating systems and are labeled as such.
事件:'menu-will-show'
¥Event: 'menu-will-show'
返回:
¥Returns:
-
event
事件¥
event
Event
调用 menu.popup()
时发出。
¥Emitted when menu.popup()
is called.
事件:'menu-will-close'
¥Event: 'menu-will-close'
返回:
¥Returns:
-
event
事件¥
event
Event
当手动或使用 menu.closePopup()
关闭弹出窗口时发出。
¥Emitted when a popup is closed either manually or with menu.closePopup()
.
实例属性
¥Instance Properties
menu
对象还具有以下属性:
¥menu
objects also have the following properties:
menu.items
包含菜单项的 MenuItem[]
数组。
¥A MenuItem[]
array containing the menu's items.
每个 Menu
实例由多个 MenuItem
实例组成,每个 MenuItem
实例可以将 Menu
嵌套到其 submenu
属性中。
¥Each Menu
consists of multiple MenuItem
instances and each MenuItem
can nest a Menu
into its submenu
property.