electron/lib/browser/api/menu-item-roles.js

289 lines
6 KiB
JavaScript
Raw Normal View History

'use strict'
2018-09-13 16:10:51 +00:00
const { app } = require('electron')
2016-06-22 22:21:45 +00:00
const roles = {
2016-06-22 20:15:20 +00:00
about: {
get label () {
2016-06-22 22:26:17 +00:00
return process.platform === 'linux' ? 'About' : `About ${app.getName()}`
2016-06-22 20:15:20 +00:00
}
2016-06-22 17:29:49 +00:00
},
2016-06-22 20:15:20 +00:00
close: {
label: process.platform === 'darwin' ? 'Close Window' : 'Close',
2016-06-22 22:26:17 +00:00
accelerator: 'CommandOrControl+W',
2016-06-22 20:48:26 +00:00
windowMethod: 'close'
2016-06-22 20:15:20 +00:00
},
copy: {
label: 'Copy',
2016-06-22 22:26:17 +00:00
accelerator: 'CommandOrControl+C',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'copy'
2016-06-22 17:29:49 +00:00
},
cut: {
label: 'Cut',
2016-06-22 22:26:17 +00:00
accelerator: 'CommandOrControl+X',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'cut'
2016-06-22 17:29:49 +00:00
},
2016-06-22 20:15:20 +00:00
delete: {
label: 'Delete',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'delete'
2016-06-22 17:29:49 +00:00
},
2017-02-02 19:18:35 +00:00
forcereload: {
label: 'Force Reload',
accelerator: 'Shift+CmdOrCtrl+R',
nonNativeMacOSRole: true,
windowMethod: (window) => {
window.webContents.reloadIgnoringCache()
}
},
2016-06-22 20:15:20 +00:00
front: {
label: 'Bring All to Front'
2016-06-22 17:29:49 +00:00
},
2016-06-22 20:15:20 +00:00
help: {
label: 'Help'
2016-06-22 17:29:49 +00:00
},
2016-06-22 20:15:20 +00:00
hide: {
get label () {
return `Hide ${app.getName()}`
},
accelerator: 'Command+H'
},
hideothers: {
label: 'Hide Others',
accelerator: 'Command+Alt+H'
2016-06-22 17:29:49 +00:00
},
minimize: {
label: 'Minimize',
2016-06-22 22:26:17 +00:00
accelerator: 'CommandOrControl+M',
2016-06-22 20:48:26 +00:00
windowMethod: 'minimize'
2016-06-22 17:29:49 +00:00
},
2016-06-22 20:15:20 +00:00
paste: {
label: 'Paste',
2016-06-22 22:26:17 +00:00
accelerator: 'CommandOrControl+V',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'paste'
2016-06-22 17:29:49 +00:00
},
2016-06-22 20:15:20 +00:00
pasteandmatchstyle: {
label: 'Paste and Match Style',
2016-06-22 22:26:17 +00:00
accelerator: 'Shift+CommandOrControl+V',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'pasteAndMatchStyle'
2016-06-22 17:29:49 +00:00
},
quit: {
get label () {
2016-06-22 22:26:17 +00:00
switch (process.platform) {
case 'darwin': return `Quit ${app.getName()}`
case 'win32': return 'Exit'
default: return 'Quit'
}
2016-06-22 17:29:49 +00:00
},
accelerator: process.platform === 'win32' ? null : 'CommandOrControl+Q',
2016-06-22 20:48:26 +00:00
appMethod: 'quit'
2016-06-22 17:29:49 +00:00
},
2016-06-22 20:15:20 +00:00
redo: {
label: 'Redo',
accelerator: process.platform === 'win32' ? 'Control+Y' : 'Shift+CommandOrControl+Z',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'redo'
2016-06-22 20:15:20 +00:00
},
2016-11-15 18:49:51 +00:00
reload: {
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
2017-02-02 19:18:35 +00:00
nonNativeMacOSRole: true,
2016-11-15 18:49:51 +00:00
windowMethod: 'reload'
},
2016-08-08 17:09:45 +00:00
resetzoom: {
label: 'Actual Size',
accelerator: 'CommandOrControl+0',
2017-02-02 19:18:35 +00:00
nonNativeMacOSRole: true,
2016-08-08 17:09:45 +00:00
webContentsMethod: (webContents) => {
webContents.setZoomLevel(0)
}
},
2016-06-22 20:15:20 +00:00
selectall: {
label: 'Select All',
2016-06-22 22:26:17 +00:00
accelerator: 'CommandOrControl+A',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'selectAll'
2016-06-22 20:15:20 +00:00
},
services: {
label: 'Services'
},
2017-11-21 00:31:21 +00:00
recentdocuments: {
label: 'Open Recent'
},
2017-11-21 00:31:21 +00:00
clearrecentdocuments: {
label: 'Clear Menu'
},
startspeaking: {
label: 'Start Speaking'
},
stopspeaking: {
label: 'Stop Speaking'
},
2016-11-15 18:52:03 +00:00
toggledevtools: {
label: 'Toggle Developer Tools',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
2017-02-02 19:18:35 +00:00
nonNativeMacOSRole: true,
2016-11-15 18:52:03 +00:00
windowMethod: 'toggleDevTools'
},
2016-06-22 17:29:49 +00:00
togglefullscreen: {
label: 'Toggle Full Screen',
2016-06-22 23:56:45 +00:00
accelerator: process.platform === 'darwin' ? 'Control+Command+F' : 'F11',
2016-08-08 17:13:21 +00:00
windowMethod: (window) => {
2016-06-22 20:48:26 +00:00
window.setFullScreen(!window.isFullScreen())
}
2016-06-22 17:47:25 +00:00
},
2016-06-22 20:15:20 +00:00
undo: {
label: 'Undo',
2016-06-22 22:26:17 +00:00
accelerator: 'CommandOrControl+Z',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'undo'
2016-06-22 20:15:20 +00:00
},
unhide: {
label: 'Show All'
2016-06-22 17:47:25 +00:00
},
window: {
label: 'Window'
2016-06-22 20:09:39 +00:00
},
zoom: {
label: 'Zoom'
2016-08-08 17:09:45 +00:00
},
zoomin: {
label: 'Zoom In',
accelerator: 'CommandOrControl+Plus',
2017-02-02 19:18:35 +00:00
nonNativeMacOSRole: true,
2016-08-08 17:09:45 +00:00
webContentsMethod: (webContents) => {
webContents.getZoomLevel((zoomLevel) => {
webContents.setZoomLevel(zoomLevel + 0.5)
})
}
},
zoomout: {
label: 'Zoom Out',
accelerator: 'CommandOrControl+-',
2017-02-02 19:18:35 +00:00
nonNativeMacOSRole: true,
2016-08-08 17:09:45 +00:00
webContentsMethod: (webContents) => {
webContents.getZoomLevel((zoomLevel) => {
webContents.setZoomLevel(zoomLevel - 0.5)
})
}
},
2017-03-29 19:27:59 +00:00
// Edit submenu (should fit both Mac & Windows)
2017-12-28 05:22:39 +00:00
editmenu: {
label: 'Edit',
submenu: [
{
role: 'undo'
},
{
role: 'redo'
},
{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
2017-03-13 13:26:34 +00:00
process.platform === 'darwin' ? {
2017-12-28 05:22:39 +00:00
role: 'pasteAndMatchStyle'
} : null,
{
role: 'delete'
},
2017-03-13 13:26:34 +00:00
process.platform === 'win32' ? {
type: 'separator'
} : null,
{
2017-12-28 05:22:39 +00:00
role: 'selectAll'
}
]
},
2017-03-29 19:27:59 +00:00
// Window submenu should be used for Mac only
2017-12-28 05:22:39 +00:00
windowmenu: {
label: 'Window',
submenu: [
{
role: 'minimize'
},
{
role: 'close'
},
2017-03-13 13:26:34 +00:00
process.platform === 'darwin' ? {
type: 'separator'
} : null,
2017-03-13 13:26:34 +00:00
process.platform === 'darwin' ? {
role: 'front'
} : null
]
2016-06-22 17:29:49 +00:00
}
}
2016-08-08 17:09:45 +00:00
const canExecuteRole = (role) => {
if (!roles.hasOwnProperty(role)) return false
if (process.platform !== 'darwin') return true
2017-02-02 19:18:35 +00:00
// macOS handles all roles natively except for a few
return roles[role].nonNativeMacOSRole
2016-08-08 17:09:45 +00:00
}
2016-06-22 22:26:17 +00:00
exports.getDefaultLabel = (role) => {
return roles.hasOwnProperty(role) ? roles[role].label : ''
}
2016-06-22 22:26:17 +00:00
exports.getDefaultAccelerator = (role) => {
if (roles.hasOwnProperty(role)) return roles[role].accelerator
}
2016-06-22 20:48:26 +00:00
exports.getDefaultSubmenu = (role) => {
2017-03-29 19:27:59 +00:00
if (!roles.hasOwnProperty(role)) return
2018-09-13 16:10:51 +00:00
let { submenu } = roles[role]
2017-03-29 19:27:59 +00:00
// remove null items from within the submenu
if (Array.isArray(submenu)) {
submenu = submenu.filter((item) => item != null)
}
2017-03-29 19:27:59 +00:00
return submenu
}
exports.execute = (role, focusedWindow, focusedWebContents) => {
2016-08-08 17:09:45 +00:00
if (!canExecuteRole(role)) return false
2016-06-22 20:48:26 +00:00
2018-09-13 16:10:51 +00:00
const { appMethod, webContentsMethod, windowMethod } = roles[role]
2016-06-22 20:48:26 +00:00
if (appMethod) {
app[appMethod]()
return true
}
2016-06-22 21:14:32 +00:00
if (windowMethod && focusedWindow != null) {
if (typeof windowMethod === 'function') {
windowMethod(focusedWindow)
} else {
focusedWindow[windowMethod]()
2016-06-22 20:48:26 +00:00
}
2016-06-22 21:14:32 +00:00
return true
}
if (webContentsMethod && focusedWebContents != null) {
2016-08-08 17:09:45 +00:00
if (typeof webContentsMethod === 'function') {
webContentsMethod(focusedWebContents)
} else {
focusedWebContents[webContentsMethod]()
}
2016-06-22 21:14:32 +00:00
return true
2016-06-22 20:48:26 +00:00
}
return false
}