2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-06-21 00:14:16 +00:00
|
|
|
let nextCommandId = 0
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Maps role to methods of webContents
|
2016-06-21 00:14:16 +00:00
|
|
|
const rolesMap = {
|
2016-01-12 02:40:23 +00:00
|
|
|
undo: 'undo',
|
|
|
|
redo: 'redo',
|
|
|
|
cut: 'cut',
|
|
|
|
copy: 'copy',
|
|
|
|
paste: 'paste',
|
2016-06-04 15:23:35 +00:00
|
|
|
pasteandmatchstyle: 'pasteAndMatchStyle',
|
2016-01-12 02:40:23 +00:00
|
|
|
selectall: 'selectAll',
|
|
|
|
minimize: 'minimize',
|
2016-03-07 23:50:33 +00:00
|
|
|
close: 'close',
|
2016-06-21 00:03:48 +00:00
|
|
|
delete: 'delete',
|
|
|
|
quit: 'quit'
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Maps methods that should be called directly on the BrowserWindow instance
|
2016-06-21 00:14:16 +00:00
|
|
|
const methodInBrowserWindow = {
|
2016-01-12 02:40:23 +00:00
|
|
|
minimize: true,
|
|
|
|
close: true
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-06-21 00:03:48 +00:00
|
|
|
const methodInApp = {
|
|
|
|
quit: true
|
|
|
|
}
|
|
|
|
|
2016-06-21 00:14:16 +00:00
|
|
|
const MenuItem = (function () {
|
2016-03-24 20:15:04 +00:00
|
|
|
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
function MenuItem (options) {
|
2016-06-21 00:03:48 +00:00
|
|
|
const {app, Menu} = require('electron')
|
2016-06-21 00:16:34 +00:00
|
|
|
|
|
|
|
const click = options.click
|
2016-03-29 01:00:30 +00:00
|
|
|
this.selector = options.selector
|
|
|
|
this.type = options.type
|
|
|
|
this.role = options.role
|
|
|
|
this.label = options.label
|
|
|
|
this.sublabel = options.sublabel
|
|
|
|
this.accelerator = options.accelerator
|
|
|
|
this.icon = options.icon
|
|
|
|
this.enabled = options.enabled
|
|
|
|
this.visible = options.visible
|
|
|
|
this.checked = options.checked
|
|
|
|
this.submenu = options.submenu
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((this.submenu != null) && this.submenu.constructor !== Menu) {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.submenu = Menu.buildFromTemplate(this.submenu)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if ((this.type == null) && (this.submenu != null)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.type = 'submenu'
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-06-21 00:16:34 +00:00
|
|
|
if (this.type === 'submenu' && (this.submenu != null ? this.submenu.constructor : void 0) !== Menu) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new Error('Invalid submenu')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
this.overrideReadOnlyProperty('type', 'normal')
|
|
|
|
this.overrideReadOnlyProperty('role')
|
|
|
|
this.overrideReadOnlyProperty('accelerator')
|
|
|
|
this.overrideReadOnlyProperty('icon')
|
|
|
|
this.overrideReadOnlyProperty('submenu')
|
|
|
|
this.overrideProperty('label', '')
|
|
|
|
this.overrideProperty('sublabel', '')
|
|
|
|
this.overrideProperty('enabled', true)
|
|
|
|
this.overrideProperty('visible', true)
|
|
|
|
this.overrideProperty('checked', false)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (MenuItem.types.indexOf(this.type) === -1) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new Error('Unknown menu type ' + this.type)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
this.commandId = ++nextCommandId
|
2016-03-10 19:54:17 +00:00
|
|
|
this.click = (focusedWindow) => {
|
|
|
|
// Manually flip the checked flags when clicked.
|
2016-06-21 00:16:34 +00:00
|
|
|
if (this.type === 'checkbox' || this.type === 'radio') {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.checked = !this.checked
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-06-21 00:16:34 +00:00
|
|
|
|
2016-03-10 19:54:17 +00:00
|
|
|
if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) {
|
2016-06-21 00:16:34 +00:00
|
|
|
const methodName = rolesMap[this.role]
|
2016-06-21 00:03:48 +00:00
|
|
|
if(methodInApp[methodName]) {
|
|
|
|
return app[methodName]()
|
|
|
|
} else if (methodInBrowserWindow[methodName]) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return focusedWindow[methodName]()
|
2016-03-10 19:54:17 +00:00
|
|
|
} else {
|
2016-06-21 00:16:34 +00:00
|
|
|
const {webContents} = focusedWindow
|
|
|
|
return webContents != null ? webContents[methodName]() : void 0
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-10 19:54:17 +00:00
|
|
|
} else if (typeof click === 'function') {
|
2016-03-24 20:15:04 +00:00
|
|
|
return click(this, focusedWindow)
|
2016-03-10 19:54:17 +00:00
|
|
|
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
|
2016-03-24 20:15:04 +00:00
|
|
|
return Menu.sendActionToFirstResponder(this.selector)
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
MenuItem.prototype.overrideProperty = function (name, defaultValue) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (defaultValue == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
defaultValue = null
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-05-25 23:20:49 +00:00
|
|
|
this[name] != null ? this[name] : this[name] = defaultValue
|
|
|
|
|
|
|
|
return this[name]
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (defaultValue == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
defaultValue = null
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (this[name] == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
this[name] = defaultValue
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
return Object.defineProperty(this, name, {
|
|
|
|
enumerable: true,
|
|
|
|
writable: false,
|
|
|
|
value: this[name]
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
return MenuItem
|
|
|
|
})()
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = MenuItem
|