Differences between revisions 3 and 4
Revision 3 as of 2023-01-08 17:43:30
Size: 1060
Comment:
Revision 4 as of 2023-01-08 17:45:45
Size: 1511
Comment:
Deletions are marked like this. Additions are marked like this.
Line 67: Line 67:
=== Accelerators ===

To set a shortcut key combination on some menu item, use the `accelerator` attribute.

{{{
const { app, BrowserWindow, menu } = require("electron");

const template = [
  {
    label: 'File',
    submenu: [
      {
        label: 'Save',
        accelerator: 'CmdOrCtrl+S',
        click: saveFile()
      }
    ]
  }
]
}}}

For alternatives, see [[Node/Electron/LocalShortCut|electron-localshortcut]].

Electron Menu


Usage

const { app, BrowserWindow, menu, shell } = require("electron");

const template = [
  {
    label: 'File',
    submenu: [
      {
        label: 'Exit',
        click() {
          app.quit()
        }
      }
    ]
  },
  {
    label: 'Help',
    submenu: [
      {
        label: 'Learn More',
        click: async () => {
          await shell.openExternal('https://example.org');
        }
      }
    ]
  }
];

const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

For a platform-specific conditional menu, try:

const { app, BrowserWindow, menu } = require("electron");
const isMac = process.platform === 'darwin';

const template = [
  ...(isMac ? [{
    label: app.name,
    submenu: [
      { role: 'about' },
      { role: 'quit' }
    ]
  }] : []),
  {
    label: 'File',
    submenu: [
      isMac ? { role: 'close' } : { role: 'quit' }
    ]
  }
];

Accelerators

To set a shortcut key combination on some menu item, use the accelerator attribute.

const { app, BrowserWindow, menu } = require("electron");

const template = [
  {
    label: 'File',
    submenu: [
      {
        label: 'Save',
        accelerator: 'CmdOrCtrl+S',
        click: saveFile()
      }
    ]
  }
]

For alternatives, see electron-localshortcut.


CategoryRicottone

Node/Electron/Menu (last edited 2023-03-02 17:36:05 by DominicRicottone)