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

146 lines
3 KiB
JavaScript
Raw Normal View History

const roles = {
2016-06-22 20:15:20 +00:00
about: {
get label () {
const {app} = require('electron')
return `About ${app.getName()}`
}
2016-06-22 17:29:49 +00:00
},
2016-06-22 20:15:20 +00:00
close: {
label: 'Close',
accelerator: 'CmdOrCtrl+W',
2016-06-22 20:48:26 +00:00
windowMethod: 'close'
2016-06-22 20:15:20 +00:00
},
copy: {
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'copy'
2016-06-22 17:29:49 +00:00
},
cut: {
label: 'Cut',
accelerator: 'CmdOrCtrl+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',
accelerator: 'Delete',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'delete'
2016-06-22 17:29:49 +00:00
},
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 () {
const {app} = require('electron')
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',
accelerator: 'CmdOrCtrl+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',
accelerator: 'CmdOrCtrl+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',
accelerator: 'Shift+Command+V',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'pasteAndMatchStyle'
2016-06-22 17:29:49 +00:00
},
quit: {
get label () {
const {app} = require('electron')
return process.platform === 'win32' ? 'Exit' : `Quit ${app.getName()}`
},
accelerator: process.platform === 'win32' ? null : 'Command+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: 'Shift+CmdOrCtrl+Z',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'redo'
2016-06-22 20:15:20 +00:00
},
selectall: {
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
2016-06-22 20:48:26 +00:00
webContentsMethod: 'selectAll'
2016-06-22 20:15:20 +00:00
},
services: {
label: 'Services'
},
2016-06-22 17:29:49 +00:00
togglefullscreen: {
label: 'Toggle Full Screen',
accelerator: process.platform === 'darwin' ? 'Ctrl+Command+F' : 'F11',
2016-06-22 20:48:26 +00:00
windowMethod: function (window) {
window.setFullScreen(!window.isFullScreen())
}
2016-06-22 17:47:25 +00:00
},
2016-06-22 20:15:20 +00:00
undo: {
label: 'Undo',
accelerator: 'CmdOrCtrl+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-06-22 17:29:49 +00:00
}
}
exports.getDefaultLabel = function (role) {
if (roles.hasOwnProperty(role)) {
return roles[role].label
} else {
return ''
}
}
exports.getDefaultAccelerator = function (role) {
if (roles.hasOwnProperty(role)) return roles[role].accelerator
}
2016-06-22 20:48:26 +00:00
exports.execute = function (role, focusedWindow) {
if (!roles.hasOwnProperty(role)) return false
if (process.platform === 'darwin') return false
const {appMethod, webContentsMethod, windowMethod} = roles[role]
if (appMethod) {
app[appMethod]()
return true
}
if (focusedWindow != null) {
if (windowMethod) {
if (typeof windowMethod === 'function') {
windowMethod(focusedWindow)
} else {
focusedWindow[windowMethod]()
}
return true
} else if (webContentsMethod) {
const {webContents} = focusedWindow
if (webContents) {
webContents[webContentsMethod]()
}
return true
}
}
return false
}