2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-06-22 17:45:01 +00:00
|
|
|
const roles = require('./menu-item-roles')
|
|
|
|
|
2016-06-21 00:14:16 +00:00
|
|
|
let nextCommandId = 0
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-06-21 22:41:37 +00:00
|
|
|
const MenuItem = function (options) {
|
2016-06-22 21:13:12 +00:00
|
|
|
const {Menu} = require('electron')
|
2016-06-21 00:16:34 +00:00
|
|
|
|
2016-06-21 22:41:37 +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
|
|
|
|
if (this.submenu != null && this.submenu.constructor !== Menu) {
|
|
|
|
this.submenu = Menu.buildFromTemplate(this.submenu)
|
|
|
|
}
|
|
|
|
if (this.type == null && this.submenu != null) {
|
|
|
|
this.type = 'submenu'
|
|
|
|
}
|
|
|
|
if (this.type === 'submenu' && (this.submenu == null || this.submenu.constructor !== Menu)) {
|
|
|
|
throw new Error('Invalid submenu')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:41:37 +00:00
|
|
|
this.overrideReadOnlyProperty('type', 'normal')
|
|
|
|
this.overrideReadOnlyProperty('role')
|
2016-07-02 02:47:40 +00:00
|
|
|
this.overrideReadOnlyProperty('accelerator')
|
2016-06-21 22:41:37 +00:00
|
|
|
this.overrideReadOnlyProperty('icon')
|
|
|
|
this.overrideReadOnlyProperty('submenu')
|
2016-05-25 23:20:49 +00:00
|
|
|
|
2016-06-22 17:45:01 +00:00
|
|
|
this.overrideProperty('label', roles.getDefaultLabel(this.role))
|
2016-06-21 22:41:37 +00:00
|
|
|
this.overrideProperty('sublabel', '')
|
|
|
|
this.overrideProperty('enabled', true)
|
|
|
|
this.overrideProperty('visible', true)
|
|
|
|
this.overrideProperty('checked', false)
|
|
|
|
|
|
|
|
if (!MenuItem.types.includes(this.type)) {
|
2016-06-21 22:59:02 +00:00
|
|
|
throw new Error(`Unknown menu item type: ${this.type}`)
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-06-22 17:07:02 +00:00
|
|
|
this.overrideReadOnlyProperty('commandId', ++nextCommandId)
|
2016-06-21 22:41:37 +00:00
|
|
|
|
2016-06-21 23:07:20 +00:00
|
|
|
const click = options.click
|
2016-06-22 16:35:11 +00:00
|
|
|
this.click = (event, focusedWindow) => {
|
2016-06-21 22:41:37 +00:00
|
|
|
// Manually flip the checked flags when clicked.
|
|
|
|
if (this.type === 'checkbox' || this.type === 'radio') {
|
|
|
|
this.checked = !this.checked
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-06-21 22:41:37 +00:00
|
|
|
|
2016-06-22 21:02:31 +00:00
|
|
|
if (!roles.execute(this.role, focusedWindow)) {
|
2016-06-22 20:48:26 +00:00
|
|
|
if (typeof click === 'function') {
|
|
|
|
click(this, focusedWindow, event)
|
|
|
|
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
|
|
|
|
Menu.sendActionToFirstResponder(this.selector)
|
2016-06-21 22:41:37 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-06-21 22:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-07-02 02:47:40 +00:00
|
|
|
MenuItem.prototype.getDefaultRoleAccelerator = function () {
|
|
|
|
return roles.getDefaultAccelerator(this.role)
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:41:37 +00:00
|
|
|
MenuItem.prototype.overrideProperty = function (name, defaultValue) {
|
|
|
|
if (defaultValue == null) {
|
|
|
|
defaultValue = null
|
|
|
|
}
|
|
|
|
if (this[name] == null) {
|
|
|
|
this[name] = defaultValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
|
|
|
|
this.overrideProperty(name, defaultValue)
|
|
|
|
Object.defineProperty(this, name, {
|
|
|
|
enumerable: true,
|
|
|
|
writable: false,
|
|
|
|
value: this[name]
|
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = MenuItem
|