Skip to main content

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 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] 如果应用未设置默认菜单,则会自动创建默认菜单。它包含 FileEditViewWindowHelp 等标准项目。

¥[!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 and Help.

返回 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.

  • action 字符串

    ¥action string

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 原生操作的更多信息,请参阅 macOS Cocoa 事件处理指南

¥See the macOS Cocoa Event Handling Guide for more information on macOS' native actions.

  • template(菜单项构造选项 | 菜单项)[]

    ¥template (MenuItemConstructorOptions | MenuItem)[]

返回 Menu

¥Returns Menu

一般情况下,templateoptions 的数组,用于构造 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 对象(可选)

    ¥options Object (optional)

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

      ¥window BaseWindow (optional) - Default is the focused window.

    • frame Web 框架主(可选) - 如果你希望某些操作系统级别的功能(例如 macOS 上的写作工具)正常运行,请提供相关的框架。通常,这应该是 WebContents 上 context-menu 事件params.frame,或者是 WebContents 上的 focusedFrame 属性

      ¥frame WebFrameMain (optional) - Provide the relevant frame if you want certain OS-level features such as Writing Tools on macOS to function correctly. Typically, this should be params.frame from the context-menu event on a WebContents, or the focusedFrame property of a WebContents.

    • x 号码(可选) - 默认为当前鼠标光标位置。如果声明了 y,则必须声明。

      ¥x number (optional) - Default is the current mouse cursor position. Must be declared if y is declared.

    • y 号码(可选) - 默认为当前鼠标光标位置。如果声明了 x,则必须声明。

      ¥y number (optional) - Default is the current mouse cursor position. Must be declared if x is declared.

    • positioningItem 数字(可选)macOS - 要定位在鼠标光标下方指定坐标处的菜单项的索引。默认值为-1。

      ¥positioningItem number (optional) macOS - The index of the menu item to be positioned under the mouse cursor at the specified coordinates. Default is -1.

    • sourceType 字符串(可选) Windows Linux - 这应该映射到 context-menu 事件提供的 menuSourceType。不建议手动设置此值,仅提供从其他 API 收到的值或保留 undefined。可以是 nonemousekeyboardtouchtouchMenulongPresslongTaptouchHandlestylusadjustSelectionadjustSelectionReset

      ¥sourceType string (optional) Windows Linux - This should map to the menuSourceType provided by the context-menu event. It is not recommended to set this value manually, only provide values you receive from other APIs or leave it undefined. Can be none, mouse, keyboard, touch, touchMenu, longPress, longTap, touchHandle, stylus, adjustSelection, or adjustSelectionReset.

    • callback 功能(可选) - 菜单关闭时调用。

      ¥callback Function (optional) - Called when menu is closed.

弹出此菜单作为 BaseWindow 中的上下文菜单。

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

[!TIP] 更多详情,请参阅 上下文菜单 指南。

¥[!TIP] For more details, see the Context Menu guide.

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

    ¥window BaseWindow (optional) - Default is the focused window.

关闭 window 中的上下文菜单。

¥Closes the context menu in the window.

menuItem 添加到菜单。

¥Appends the menuItem to the menu.

  • id 字符串

    ¥id string

返回 MenuItem | null 具有指定 id 的项目

¥Returns MenuItem | null the item with the specified id

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

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