Add execute helper to roles file

This commit is contained in:
Kevin Sawicki 2016-06-22 13:48:26 -07:00
parent 653370974a
commit 66f2fb2fe4
2 changed files with 50 additions and 56 deletions

View file

@ -8,22 +8,22 @@ const roles = {
close: { close: {
label: 'Close', label: 'Close',
accelerator: 'CmdOrCtrl+W', accelerator: 'CmdOrCtrl+W',
method: 'close' windowMethod: 'close'
}, },
copy: { copy: {
label: 'Copy', label: 'Copy',
accelerator: 'CmdOrCtrl+C', accelerator: 'CmdOrCtrl+C',
method: 'copy' webContentsMethod: 'copy'
}, },
cut: { cut: {
label: 'Cut', label: 'Cut',
accelerator: 'CmdOrCtrl+X', accelerator: 'CmdOrCtrl+X',
method: 'cut' webContentsMethod: 'cut'
}, },
delete: { delete: {
label: 'Delete', label: 'Delete',
accelerator: 'Delete', accelerator: 'Delete',
method: 'delete' webContentsMethod: 'delete'
}, },
front: { front: {
label: 'Bring All to Front' label: 'Bring All to Front'
@ -45,17 +45,17 @@ const roles = {
minimize: { minimize: {
label: 'Minimize', label: 'Minimize',
accelerator: 'CmdOrCtrl+M', accelerator: 'CmdOrCtrl+M',
method: 'minimize' windowMethod: 'minimize'
}, },
paste: { paste: {
label: 'Paste', label: 'Paste',
accelerator: 'CmdOrCtrl+V', accelerator: 'CmdOrCtrl+V',
method: 'paste' webContentsMethod: 'paste'
}, },
pasteandmatchstyle: { pasteandmatchstyle: {
label: 'Paste and Match Style', label: 'Paste and Match Style',
accelerator: 'Shift+Command+V', accelerator: 'Shift+Command+V',
method: 'pasteAndMatchStyle' webContentsMethod: 'pasteAndMatchStyle'
}, },
quit: { quit: {
get label () { get label () {
@ -63,17 +63,17 @@ const roles = {
return process.platform === 'win32' ? 'Exit' : `Quit ${app.getName()}` return process.platform === 'win32' ? 'Exit' : `Quit ${app.getName()}`
}, },
accelerator: process.platform === 'win32' ? null : 'Command+Q', accelerator: process.platform === 'win32' ? null : 'Command+Q',
method: 'quit' appMethod: 'quit'
}, },
redo: { redo: {
label: 'Redo', label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z', accelerator: 'Shift+CmdOrCtrl+Z',
method: 'redo' webContentsMethod: 'redo'
}, },
selectall: { selectall: {
label: 'Select All', label: 'Select All',
accelerator: 'CmdOrCtrl+A', accelerator: 'CmdOrCtrl+A',
method: 'selectAll' webContentsMethod: 'selectAll'
}, },
services: { services: {
label: 'Services' label: 'Services'
@ -81,12 +81,14 @@ const roles = {
togglefullscreen: { togglefullscreen: {
label: 'Toggle Full Screen', label: 'Toggle Full Screen',
accelerator: process.platform === 'darwin' ? 'Ctrl+Command+F' : 'F11', accelerator: process.platform === 'darwin' ? 'Ctrl+Command+F' : 'F11',
method: 'toggleFullScreen' windowMethod: function (window) {
window.setFullScreen(!window.isFullScreen())
}
}, },
undo: { undo: {
label: 'Undo', label: 'Undo',
accelerator: 'CmdOrCtrl+Z', accelerator: 'CmdOrCtrl+Z',
method: 'undo' webContentsMethod: 'undo'
}, },
unhide: { unhide: {
label: 'Show All' label: 'Show All'
@ -110,3 +112,34 @@ exports.getDefaultLabel = function (role) {
exports.getDefaultAccelerator = function (role) { exports.getDefaultAccelerator = function (role) {
if (roles.hasOwnProperty(role)) return roles[role].accelerator if (roles.hasOwnProperty(role)) return roles[role].accelerator
} }
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
}

View file

@ -4,35 +4,6 @@ const roles = require('./menu-item-roles')
let nextCommandId = 0 let nextCommandId = 0
// Maps role to methods of webContents
const rolesMap = {
undo: 'undo',
redo: 'redo',
cut: 'cut',
copy: 'copy',
paste: 'paste',
pasteandmatchstyle: 'pasteAndMatchStyle',
selectall: 'selectAll',
minimize: 'minimize',
close: 'close',
delete: 'delete',
quit: 'quit',
togglefullscreen: 'toggleFullScreen'
}
// Maps methods that should be called directly on the BrowserWindow instance
const methodInBrowserWindow = {
minimize: true,
close: true,
toggleFullScreen: function (window) {
window.setFullScreen(!window.isFullScreen())
}
}
const methodInApp = {
quit: true
}
const MenuItem = function (options) { const MenuItem = function (options) {
const {app, Menu} = require('electron') const {app, Menu} = require('electron')
@ -83,22 +54,12 @@ const MenuItem = function (options) {
this.checked = !this.checked this.checked = !this.checked
} }
if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && focusedWindow != null) { if (!roles.execute(this.role)) {
const methodName = rolesMap[this.role] if (typeof click === 'function') {
if (methodInApp[methodName]) { click(this, focusedWindow, event)
return app[methodName]()
} else if (typeof methodInBrowserWindow[methodName] === 'function') {
return methodInBrowserWindow[methodName](focusedWindow)
} else if (methodInBrowserWindow[methodName]) {
return focusedWindow[methodName]()
} else {
const {webContents} = focusedWindow
return webContents != null ? webContents[methodName]() : void 0
}
} else if (typeof click === 'function') {
return click(this, focusedWindow, event)
} else if (typeof this.selector === 'string' && process.platform === 'darwin') { } else if (typeof this.selector === 'string' && process.platform === 'darwin') {
return Menu.sendActionToFirstResponder(this.selector) Menu.sendActionToFirstResponder(this.selector)
}
} }
} }
} }