Menu
Class: Menu
Cria menus de aplicativo e menus de contexto nativos.
Process: Main
[!TIP] See also: A detailed guide about how to implement menus in your application.
[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new Menu()
Cria um novo menu.
Métodos estáticos
A classe Menu tem os seguintes métodos estáticos:
Menu.setApplicationMenu(menu)
menuMenu | null
Sets menu as the application menu on macOS. On Windows and Linux, the menu will be set as each window's top menu.
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.
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.
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] The default menu will be created automatically if the app does not set one. It contains standard items such as
File,Edit,View,WindowandHelp.
Menu.getApplicationMenu()
Retorna Menu | null - O menu do aplicativo, se definido, ou null, se não fora definido.
[!NOTE] The returned
Menuinstance doesn't support dynamic addition or removal of menu items. Instance properties can still be dynamically modified.
Menu.sendActionToFirstResponder(action) no macOS
actionstring
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.
See the macOS Cocoa Event Handling Guide for more information on macOS' native actions.
Menu.buildFromTemplate(template)
template(MenuItemConstructorOptions | MenuItem)[]
Returns Menu
Generally, the template is an array of options for constructing a MenuItem. The usage can be referenced above.
You can also attach other fields to the element of the template and they will become properties of the constructed menu items.
Métodos de Instância
O objeto menu possui os seguintes métodos de instância:
menu.popup([options])
- Objeto
options(opcional)windowBaseWindow (optional) - Default is the focused window.frameWebFrameMain (optional) - Provide the relevant frame if you want certain OS-level features such as Writing Tools on macOS to function correctly. Typically, this should beparams.framefrom thecontext-menuevent on a WebContents, or thefocusedFrameproperty of a WebContents.xnumber (optional) - Default is the current mouse cursor position. Must be declared ifyis declared.ynumber (optional) - Default is the current mouse cursor position. Must be declared ifxis declared.positioningItemnumber (optional) macOS - The index of the menu item to be positioned under the mouse cursor at the specified coordinates. Default is -1.sourceTypestring (optional) Windows Linux - This should map to themenuSourceTypeprovided by thecontext-menuevent. It is not recommended to set this value manually, only provide values you receive from other APIs or leave itundefined. Can benone,mouse,keyboard,touch,touchMenu,longPress,longTap,touchHandle,stylus,adjustSelection, oradjustSelectionReset.callbackFunction (optional) - Called when menu is closed.
Pops up this menu as a context menu in the BaseWindow.
[!TIP] For more details, see the Context Menu guide.
menu.closePopup([window])
windowBaseWindow (optional) - Default is the focused window.
Closes the context menu in the window.
menu.append(menuItem)
menuItemMenuItem
Acrescenta o menuItem ao menu.
menu.getMenuItemById(id)
idstring
Returns MenuItem | null the item with the specified id
menu.Insert(pos, menuItem)
posIntegermenuItemMenuItem
Insere o menuItem na posição pos do menu.
Eventos de instância
Objects created with new Menu or returned by Menu.buildFromTemplate emit the following events:
[!NOTE] Some events are only available on specific operating systems and are labeled as such.
Event: 'menu-will-show'
Retorna:
eventEvent
Emitted when menu.popup() is called.
Event: 'menu-will-close'
Retorna:
eventEvent
Emitted when a popup is closed either manually or with menu.closePopup().
Propriedades da Instância
Objetos menu também possuem as seguintes propriedades:
menu.items
Um array MenuItem[] contendo os itens do menu.
Each Menu consists of multiple MenuItem instances and each MenuItem can nest a Menu into its submenu property.