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

@ -4,35 +4,6 @@ const roles = require('./menu-item-roles')
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 {app, Menu} = require('electron')
@ -83,22 +54,12 @@ const MenuItem = function (options) {
this.checked = !this.checked
}
if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && focusedWindow != null) {
const methodName = rolesMap[this.role]
if (methodInApp[methodName]) {
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
if (!roles.execute(this.role)) {
if (typeof click === 'function') {
click(this, focusedWindow, event)
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
Menu.sendActionToFirstResponder(this.selector)
}
} else if (typeof click === 'function') {
return click(this, focusedWindow, event)
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
return Menu.sendActionToFirstResponder(this.selector)
}
}
}