Skip to main content

Menu

类:Menu

🌐 Class: Menu

创建原生应用菜单和上下文菜单。

进程:主进程

🌐 Process: Main

warning

Electron 内置的类不能在用户代码中被继承。 欲了解更多信息,请参见 常见问题

new Menu()

创建一个新菜单。

🌐 Creates a new menu.

静态方法

🌐 Static Methods

Menu 类具有以下静态方法:

🌐 The Menu class has the following static methods:

在 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.

note

如果应用没有设置菜单,默认菜单将自动创建。 它包含标准项目,例如 FileEditViewWindowHelp

返回 Menu | null - 如果设置了应用菜单,则返回应用菜单,否则返回 null

🌐 Returns Menu | null - The application menu, if set, or null, if not set.

note

返回的 Menu 实例不支持菜单项的动态添加或删除。实例属性 仍然可以被动态修改。

  • action 字符串

action 发送给应用的第一个响应者。这用于模拟 macOS 默认菜单的行为。通常你会使用 MenuItemrole 属性。

🌐 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.

  • 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:

  • options 对象(可选)
    • window BaseWindow(可选)- 默认是当前聚焦的窗口。
    • frame WebFrameMain(可选)- 如果你希望某些操作系统级功能(例如 macOS 上的写作工具)正常工作,请提供相关的框架。通常,这应为来自 WebContents 的 context-menu 事件params.frame,或 WebContents 的 focusedFrame 属性
    • x 数字(可选)- 默认是当前鼠标光标位置。如果声明了 y,必须声明它。
    • y 数字(可选)- 默认是当前鼠标光标位置。如果声明了 x,必须声明它。
    • positioningItem 数字(可选)macOS - 要在指定坐标下放置鼠标光标的菜单项索引。默认值为 -1。
    • sourceType 字符串(可选) Windows Linux - 这应映射到由 context-menu 事件提供的 menuSourceType。不建议手动设置此值,只提供从其他 API 接收到的值,或保持为 undefined。可以是 nonemousekeyboardtouchtouchMenulongPresslongTaptouchHandlestylusadjustSelectionadjustSelectionReset
    • callback 函数(可选)- 在菜单关闭时调用。

BaseWindow 中以上下文菜单的形式弹出此菜单。

🌐 Pops up this menu as a context menu in the BaseWindow.

tip

有关更多详情,请参阅右键菜单指南。

  • window BaseWindow(可选)- 默认是当前聚焦的窗口。

关闭 window 中的上下文菜单。

🌐 Closes the context menu in the window.

menuItem 添加到菜单中。

🌐 Appends the menuItem to the menu.

  • id 字符串

返回具有指定 idMenuItem | null

🌐 Returns MenuItem | null the item with the specified id

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

一些事件仅在特定操作系统上可用,并且会被标注为此。

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 对象还有以下属性:

一个包含菜单项的 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.