Menu

Class: Menu

Create native application menus and context menus.

Process: Main

new Menu()

Creates a new menu.

Static Methods

The Menu class has the following static methods:

Menu.setApplicationMenu(menu)

  • menu Menu | 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, Window and Help.

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.

Instance Methods

The menu object has the following instance methods:

menu.popup([options])

  • options Object (optional)
    • window BrowserWindow (optional) - Default is the focused window.
    • x number (optional) - Default is the current mouse cursor position. Must be declared if y is declared.
    • y number (optional) - Default is the current mouse cursor position. Must be declared if x is declared.
    • 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 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 Function (optional) - Called when menu is closed.

Pops up this menu as a context menu in the BrowserWindow.

menu.closePopup([browserWindow])

  • browserWindow BrowserWindow (optional) - Default is the focused window.

Closes the context menu in the browserWindow.

menu.append(menuItem)

Appends the menuItem to the menu.

menu.getMenuItemById(id)

  • id string

Returns MenuItem | null the item with the specified id

menu.insert(pos, menuItem)

Inserts the menuItem to the pos position of the menu.

Instance Events

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’

Returns:

  • event Event

Emitted when menu.popup() is called.

Event: ‘menu-will-close’

Returns:

  • event Event

Emitted when a popup is closed either manually or with menu.closePopup().

Instance Properties

menu objects also have the following properties:

menu.items

A MenuItem[] array containing the menu’s items.

Each Menu consists of multiple MenuItems and each MenuItem can have a submenu.

Examples

An example of creating the application menu with the simple template API:

  1. const { app, Menu } = require('electron')
  2. const isMac = process.platform === 'darwin'
  3. const template = [
  4. // { role: 'appMenu' }
  5. ...(isMac
  6. ? [{
  7. label: app.name,
  8. submenu: [
  9. { role: 'about' },
  10. { type: 'separator' },
  11. { role: 'services' },
  12. { type: 'separator' },
  13. { role: 'hide' },
  14. { role: 'hideOthers' },
  15. { role: 'unhide' },
  16. { type: 'separator' },
  17. { role: 'quit' }
  18. ]
  19. }]
  20. : []),
  21. // { role: 'fileMenu' }
  22. {
  23. label: 'File',
  24. submenu: [
  25. isMac ? { role: 'close' } : { role: 'quit' }
  26. ]
  27. },
  28. // { role: 'editMenu' }
  29. {
  30. label: 'Edit',
  31. submenu: [
  32. { role: 'undo' },
  33. { role: 'redo' },
  34. { type: 'separator' },
  35. { role: 'cut' },
  36. { role: 'copy' },
  37. { role: 'paste' },
  38. ...(isMac
  39. ? [
  40. { role: 'pasteAndMatchStyle' },
  41. { role: 'delete' },
  42. { role: 'selectAll' },
  43. { type: 'separator' },
  44. {
  45. label: 'Speech',
  46. submenu: [
  47. { role: 'startSpeaking' },
  48. { role: 'stopSpeaking' }
  49. ]
  50. }
  51. ]
  52. : [
  53. { role: 'delete' },
  54. { type: 'separator' },
  55. { role: 'selectAll' }
  56. ])
  57. ]
  58. },
  59. // { role: 'viewMenu' }
  60. {
  61. label: 'View',
  62. submenu: [
  63. { role: 'reload' },
  64. { role: 'forceReload' },
  65. { role: 'toggleDevTools' },
  66. { type: 'separator' },
  67. { role: 'resetZoom' },
  68. { role: 'zoomIn' },
  69. { role: 'zoomOut' },
  70. { type: 'separator' },
  71. { role: 'togglefullscreen' }
  72. ]
  73. },
  74. // { role: 'windowMenu' }
  75. {
  76. label: 'Window',
  77. submenu: [
  78. { role: 'minimize' },
  79. { role: 'zoom' },
  80. ...(isMac
  81. ? [
  82. { type: 'separator' },
  83. { role: 'front' },
  84. { type: 'separator' },
  85. { role: 'window' }
  86. ]
  87. : [
  88. { role: 'close' }
  89. ])
  90. ]
  91. },
  92. {
  93. role: 'help',
  94. submenu: [
  95. {
  96. label: 'Learn More',
  97. click: async () => {
  98. const { shell } = require('electron')
  99. await shell.openExternal('https://electronjs.org')
  100. }
  101. }
  102. ]
  103. }
  104. ]
  105. const menu = Menu.buildFromTemplate(template)
  106. Menu.setApplicationMenu(menu)

Render process

To create menus initiated by the renderer process, send the required information to the main process using IPC and have the main process display the menu on behalf of the renderer.

Below is an example of showing a menu when the user right clicks the page:

  1. // renderer
  2. window.addEventListener('contextmenu', (e) => {
  3. e.preventDefault()
  4. ipcRenderer.send('show-context-menu')
  5. })
  6. ipcRenderer.on('context-menu-command', (e, command) => {
  7. // ...
  8. })
  9. // main
  10. ipcMain.on('show-context-menu', (event) => {
  11. const template = [
  12. {
  13. label: 'Menu Item 1',
  14. click: () => { event.sender.send('context-menu-command', 'menu-item-1') }
  15. },
  16. { type: 'separator' },
  17. { label: 'Menu Item 2', type: 'checkbox', checked: true }
  18. ]
  19. const menu = Menu.buildFromTemplate(template)
  20. menu.popup({ window: BrowserWindow.fromWebContents(event.sender) })
  21. })

Notes on macOS Application Menu

macOS has a completely different style of application menu from Windows and Linux. Here are some notes on making your app’s menu more native-like.

Standard Menus

On macOS there are many system-defined standard menus, like the Services and Windows menus. To make your menu a standard menu, you should set your menu’s role to one of the following and Electron will recognize them and make them become standard menus:

  • window
  • help
  • services

Standard Menu Item Actions

macOS has provided standard actions for some menu items, like About xxx, Hide xxx, and Hide Others. To set the action of a menu item to a standard action, you should set the role attribute of the menu item.

Main Menu’s Name

On macOS the label of the application menu’s first item is always your app’s name, no matter what label you set. To change it, modify your app bundle’s Info.plist file. See About Information Property List Files for more information.

Setting Menu for Specific Browser Window (Linux Windows)

The setMenu method of browser windows can set the menu of certain browser windows.

Menu Item Position

You can make use of before, after, beforeGroupContaining, afterGroupContaining and id to control how the item will be placed when building a menu with Menu.buildFromTemplate.

  • before - Inserts this item before the item with the specified label. If the referenced item doesn’t exist the item will be inserted at the end of the menu. Also implies that the menu item in question should be placed in the same “group” as the item.
  • after - Inserts this item after the item with the specified label. If the referenced item doesn’t exist the item will be inserted at the end of the menu. Also implies that the menu item in question should be placed in the same “group” as the item.
  • beforeGroupContaining - Provides a means for a single context menu to declare the placement of their containing group before the containing group of the item with the specified label.
  • afterGroupContaining - Provides a means for a single context menu to declare the placement of their containing group after the containing group of the item with the specified label.

By default, items will be inserted in the order they exist in the template unless one of the specified positioning keywords is used.

Examples

Template:

  1. [
  2. { id: '1', label: 'one' },
  3. { id: '2', label: 'two' },
  4. { id: '3', label: 'three' },
  5. { id: '4', label: 'four' }
  6. ]

Menu:

  1. - 1
  2. - 2
  3. - 3
  4. - 4

Template:

  1. [
  2. { id: '1', label: 'one' },
  3. { type: 'separator' },
  4. { id: '3', label: 'three', beforeGroupContaining: ['1'] },
  5. { id: '4', label: 'four', afterGroupContaining: ['2'] },
  6. { type: 'separator' },
  7. { id: '2', label: 'two' }
  8. ]

Menu:

  1. - 3
  2. - 4
  3. - ---
  4. - 1
  5. - ---
  6. - 2

Template:

  1. [
  2. { id: '1', label: 'one', after: ['3'] },
  3. { id: '2', label: 'two', before: ['1'] },
  4. { id: '3', label: 'three' }
  5. ]

Menu:

  1. - ---
  2. - 3
  3. - 2
  4. - 1