Menu
Klasse: Menü
Create native application menus and context menus.
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()
Neues Menü anlegen.
Static Methods
Die Menu
Klasse hat folgende statische Methoden:
Menu.setApplicationMenu(menu)
menu
Menu | null
Setzt menu
als Anwendungsmenü auf macOS. On Windows and Linux, the menu
will be set as each window's top menu.
Unter Windows und Linux kannst du auch ein &
im Namen des Top-Level-Elements verwenden, um anzugeben, welcher Buchstabe einen generierten Beschleuniger erhalten soll. 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
,Window
andHelp
.
Menu.getApplicationMenu()
Returns Menu | null
- The application menu, if set, or null
, if not set.
[!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
-String
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.
Beispiel Methoden
Das menu
Objekt hat die folgenden Instanz Methoden:
menu.popup([options])
Pops up this menu as a context menu in the BaseWindow
.
[!TIP] For more details, see the Context Menu guide.
menu.closePopup([window])
window
BaseWindow (optional) - Default is the focused window.
Closes the context menu in the window
.
menu.append(menuItem)
menuItem
MenuItem
Fügt dem Menü das menuItem
hinzu.
menu.getMenuItemById(id)
id
string
Returns MenuItem | null
the item with the specified id
menu.insert(pos, menuItem)
pos
IntegermenuItem
MenuItem
Füre das menuItem
an der pos
Position im Menü ein.
Instanz-Ereignisse
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'
Kehrt zurück:
event
Event
Emitted when menu.popup()
is called.
Event: 'menu-will-close'
Kehrt zurück:
event
Event
Emitiert wenn ein Popup manuell oder mit menu.closePopup()
geschlossen wird.
Instanz Eigenschaften
menu
objects also have the following properties:
menu.items
A MenuItem[]
array containing the menu's items.
Each Menu
consists of multiple MenuItem
instances and each MenuItem
can nest a Menu
into its submenu
property.