|
Size: 1060
Comment:
|
Size: 2015
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 9: | Line 9: |
| == Usage == | == Menu == |
| Line 12: | Line 12: |
| const { app, BrowserWindow, menu, shell } = require("electron"); | const { Menu, shell } = require("electron"); |
| Line 40: | Line 40: |
| Line 46: | Line 47: |
| const { app, BrowserWindow, menu } = require("electron"); | |
| Line 67: | Line 67: |
| === Accelerators === To set a shortcut key combination on some menu item, use the `accelerator` attribute. {{{ const template = [ { label: 'File', submenu: [ { label: 'Save', accelerator: 'CmdOrCtrl+S', click: saveFile() } ] } ] }}} Apple computers have a unique keyboard. End users will expect the ''Command'' key to work like the ''Control'' key for everyone else. To simplify this common pattern, `CmdOrCtrl` is a valid accelerator (as seen above). Nonetheless, for a platform-specific shortcut, try: {{{ const isMac = process.platform === 'darwin'; const template = [ { label: 'File', submenu: [ { role: 'Save File', accelerator: isMac ? 'Cmd+S' : 'Ctrl+S', click: saveFile() } ] } ] }}} For alternatives, see [[Node/Electron/LocalShortCut|electron-localshortcut]]. ---- == See also == [[https://www.electronjs.org/docs/latest/api/menu|Electron API for Menu]] |
Electron Menu
Contents
Menu
const { 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 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 template = [
{
label: 'File',
submenu: [
{
label: 'Save',
accelerator: 'CmdOrCtrl+S',
click: saveFile()
}
]
}
]Apple computers have a unique keyboard. End users will expect the Command key to work like the Control key for everyone else. To simplify this common pattern, CmdOrCtrl is a valid accelerator (as seen above).
Nonetheless, for a platform-specific shortcut, try:
const isMac = process.platform === 'darwin';
const template = [
{
label: 'File',
submenu: [
{
role: 'Save File',
accelerator: isMac ? 'Cmd+S' : 'Ctrl+S',
click: saveFile()
}
]
}
]For alternatives, see electron-localshortcut.
