Menu
类:Menu
🌐 Class: Menu
创建原生应用菜单和上下文菜单。
进程:主进程
🌐 Process: Main
另请参见:关于如何在应用中实现菜单的详细指南。
Electron 内置的类不能在用户代码中被继承。 欲了解更多信息,请参见 常见问题。
new Menu()
创建一个新菜单。
🌐 Creates a new menu.
静态方法
🌐 Static Methods
Menu 类具有以下静态方法:
🌐 The Menu class has the following static methods:
Menu.setApplicationMenu(menu)
menu菜单 | null
在 macOS 上将 menu 设置为应用菜单。在 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 preceding &. 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.
如果应用没有设置菜单,默认菜单将自动创建。
它包含标准项目,例如 File、Edit、View、Window 和 Help。
Menu.getApplicationMenu()
返回 Menu | null - 如果设置了应用菜单,则返回应用菜单,否则返回 null。
🌐 Returns Menu | null - The application menu, if set, or null, if not set.
返回的 Menu 实例不支持菜单项的动态添加或删除。实例属性 仍然可以被动态修改。
Menu.sendActionToFirstResponder(action) macOS
action字符串
将 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 Cocoa 事件处理指南 以获取有关 macOS 本地操作的更多信息。
🌐 See the macOS Cocoa Event Handling Guide for more information on macOS' native actions.
Menu.buildFromTemplate(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.
有关更多详情,请参阅右键菜单指南。
menu.closePopup([window])
windowBaseWindow(可选)- 默认是当前聚焦的窗口。
关闭 window 中的上下文菜单。
🌐 Closes the context menu in the window.
menu.append(menuItem)
menuItem菜单项
将 menuItem 添加到菜单中。
🌐 Appends the menuItem to the menu.
menu.getMenuItemById(id)
id字符串
返回具有指定 id 的 MenuItem | null 项
🌐 Returns MenuItem | null the item with the specified id
menu.insert(pos, menuItem)
pos整数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:
一些事件仅在特定操作系统上可用,并且会被标注为此。
Event: 'menu-will-show'
返回:
🌐 Returns:
event活动
当调用 menu.popup() 时触发。
🌐 Emitted when menu.popup() is called.
Event: 'menu-will-close'
返回:
🌐 Returns:
event活动
当弹出窗口被手动关闭或使用 menu.closePopup() 关闭时触发。
🌐 Emitted when a popup is closed either manually or with menu.closePopup().
实例属性
🌐 Instance Properties
menu 对象还有以下属性:
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.