Merge pull request #6182 from electron/format-menu-item

Reformat MenuItem class
This commit is contained in:
Kevin Sawicki 2016-06-22 10:00:01 -07:00 committed by GitHub
commit c6db5f6baa
2 changed files with 106 additions and 86 deletions

View file

@ -31,97 +31,94 @@ const methodInApp = {
quit: true quit: true
} }
const MenuItem = (function () { const MenuItem = function (options) {
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'] const {app, Menu} = require('electron')
function MenuItem (options) { this.selector = options.selector
const {app, Menu} = require('electron') 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
const click = options.click this.submenu = options.submenu
this.selector = options.selector if (this.submenu != null && this.submenu.constructor !== Menu) {
this.type = options.type this.submenu = Menu.buildFromTemplate(this.submenu)
this.role = options.role }
this.label = options.label if (this.type == null && this.submenu != null) {
this.sublabel = options.sublabel this.type = 'submenu'
this.accelerator = options.accelerator }
this.icon = options.icon if (this.type === 'submenu' && (this.submenu == null || this.submenu.constructor !== Menu)) {
this.enabled = options.enabled throw new Error('Invalid submenu')
this.visible = options.visible }
this.checked = options.checked
this.submenu = options.submenu this.overrideReadOnlyProperty('type', 'normal')
if ((this.submenu != null) && this.submenu.constructor !== Menu) { this.overrideReadOnlyProperty('role')
this.submenu = Menu.buildFromTemplate(this.submenu) 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)
if (!MenuItem.types.includes(this.type)) {
throw new Error(`Unknown menu item type: ${this.type}`)
}
this.commandId = ++nextCommandId
const click = options.click
this.click = (event, focusedWindow) => {
// Manually flip the checked flags when clicked.
if (this.type === 'checkbox' || this.type === 'radio') {
this.checked = !this.checked
} }
if ((this.type == null) && (this.submenu != null)) {
this.type = 'submenu' if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && focusedWindow != null) {
} const methodName = rolesMap[this.role]
if (this.type === 'submenu' && (this.submenu != null ? this.submenu.constructor : void 0) !== Menu) { if (methodInApp[methodName]) {
throw new Error('Invalid submenu') return app[methodName]()
} } else if (typeof methodInBrowserWindow[methodName] === 'function') {
this.overrideReadOnlyProperty('type', 'normal') return methodInBrowserWindow[methodName](focusedWindow)
this.overrideReadOnlyProperty('role') } else if (methodInBrowserWindow[methodName]) {
this.overrideReadOnlyProperty('accelerator') return focusedWindow[methodName]()
this.overrideReadOnlyProperty('icon') } else {
this.overrideReadOnlyProperty('submenu') const {webContents} = focusedWindow
this.overrideProperty('label', '') return webContents != null ? webContents[methodName]() : void 0
this.overrideProperty('sublabel', '')
this.overrideProperty('enabled', true)
this.overrideProperty('visible', true)
this.overrideProperty('checked', false)
if (MenuItem.types.indexOf(this.type) === -1) {
throw new Error('Unknown menu type ' + this.type)
}
this.commandId = ++nextCommandId
this.click = (event, focusedWindow) => {
// Manually flip the checked flags when clicked.
if (this.type === 'checkbox' || this.type === 'radio') {
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
}
} else if (typeof click === 'function') {
return click(this, focusedWindow, event)
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
return 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)
} }
} }
}
MenuItem.prototype.overrideProperty = function (name, defaultValue) { MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
if (defaultValue == null) {
defaultValue = null
}
this[name] != null ? this[name] : this[name] = defaultValue
return this[name] MenuItem.prototype.overrideProperty = function (name, defaultValue) {
if (defaultValue == null) {
defaultValue = null
} }
if (this[name] == null) {
MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) { this[name] = defaultValue
if (defaultValue == null) {
defaultValue = null
}
if (this[name] == null) {
this[name] = defaultValue
}
return Object.defineProperty(this, name, {
enumerable: true,
writable: false,
value: this[name]
})
} }
}
return MenuItem MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
})() this.overrideProperty(name, defaultValue)
Object.defineProperty(this, name, {
enumerable: true,
writable: false,
value: this[name]
})
}
module.exports = MenuItem module.exports = MenuItem

View file

@ -1,10 +1,7 @@
const assert = require('assert') const assert = require('assert')
const remote = require('electron').remote const {ipcRenderer, remote} = require('electron')
const ipcRenderer = require('electron').ipcRenderer const {Menu, MenuItem} = remote
const Menu = remote.require('electron').Menu
const MenuItem = remote.require('electron').MenuItem
describe('menu module', function () { describe('menu module', function () {
describe('Menu.buildFromTemplate', function () { describe('Menu.buildFromTemplate', function () {
@ -359,4 +356,30 @@ describe('menu module', function () {
} }
}) })
}) })
describe('MenuItem with invalid type', function () {
it('throws an exception', function () {
assert.throws(function () {
var menu = Menu.buildFromTemplate([
{
label: 'text',
type: 'not-a-type'
}
])
}, /Unknown menu item type: not-a-type/)
})
})
describe('MenuItem with submenu type and missing submenu', function () {
it('throws an exception', function () {
assert.throws(function () {
var menu = Menu.buildFromTemplate([
{
label: 'text',
type: 'submenu'
}
])
}, /Invalid submenu/)
})
})
}) })