2017-10-24 00:04:22 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
const {BrowserWindow, MenuItem, webContents} = require('electron')
|
|
|
|
const EventEmitter = require('events').EventEmitter
|
2016-03-24 20:15:04 +00:00
|
|
|
const v8Util = process.atomBinding('v8_util')
|
|
|
|
const bindings = process.atomBinding('menu')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
const {Menu} = bindings
|
|
|
|
let applicationMenu = null
|
2017-10-24 02:22:39 +00:00
|
|
|
let groupIdIndex = 0
|
2017-10-23 18:47:47 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
Object.setPrototypeOf(Menu.prototype, EventEmitter.prototype)
|
2017-10-23 18:47:47 +00:00
|
|
|
|
2017-10-24 04:07:39 +00:00
|
|
|
/* Instance Methods */
|
|
|
|
|
2017-10-23 22:35:16 +00:00
|
|
|
Menu.prototype._init = function () {
|
2017-10-23 18:47:47 +00:00
|
|
|
this.commandsMap = {}
|
|
|
|
this.groupsMap = {}
|
|
|
|
this.items = []
|
2017-10-24 00:04:22 +00:00
|
|
|
this.delegate = {
|
2017-10-24 22:52:12 +00:00
|
|
|
isCommandIdChecked: id => this.commandsMap[id] ? this.commandsMap[id].checked : undefined,
|
|
|
|
isCommandIdEnabled: id => this.commandsMap[id] ? this.commandsMap[id].enabled : undefined,
|
|
|
|
isCommandIdVisible: id => this.commandsMap[id] ? this.commandsMap[id].visible : undefined,
|
2017-10-24 02:33:23 +00:00
|
|
|
getAcceleratorForCommandId: (id, useDefaultAccelerator) => {
|
|
|
|
const command = this.commandsMap[id]
|
2017-10-24 22:52:12 +00:00
|
|
|
if (!command) return
|
|
|
|
if (command.accelerator) return command.accelerator
|
2017-10-24 00:04:22 +00:00
|
|
|
if (useDefaultAccelerator) return command.getDefaultRoleAccelerator()
|
|
|
|
},
|
2017-10-24 22:52:12 +00:00
|
|
|
getIconForCommandId: id => this.commandsMap[id] ? this.commandsMap[id].icon : undefined,
|
2017-10-24 02:33:23 +00:00
|
|
|
executeCommand: (event, id) => {
|
|
|
|
const command = this.commandsMap[id]
|
2017-10-24 22:52:12 +00:00
|
|
|
if (!command) return
|
2017-10-24 00:04:22 +00:00
|
|
|
command.click(event, BrowserWindow.getFocusedWindow(), webContents.getFocusedWebContents())
|
2017-10-23 18:47:47 +00:00
|
|
|
},
|
|
|
|
menuWillShow: () => {
|
2017-10-24 02:22:39 +00:00
|
|
|
// Ensure radio groups have at least one menu item seleted
|
2017-10-25 14:17:41 +00:00
|
|
|
for (const id in this.groupsMap) {
|
|
|
|
const found = this.groupsMap[id].find(item => item.checked) || null
|
2017-10-24 02:22:39 +00:00
|
|
|
if (!found) v8Util.setHiddenValue(this.groupsMap[id][0], 'checked', true)
|
2017-10-23 16:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-23 18:47:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 22:35:16 +00:00
|
|
|
Menu.prototype.popup = function (window, x, y, positioningItem) {
|
|
|
|
let asyncPopup
|
2017-10-24 04:07:39 +00:00
|
|
|
let [newX, newY, newPosition, newWindow] = [x, y, positioningItem, window]
|
2017-10-23 22:35:16 +00:00
|
|
|
|
|
|
|
// menu.popup(x, y, positioningItem)
|
2017-10-24 22:52:12 +00:00
|
|
|
if (!window) {
|
2017-10-24 04:07:39 +00:00
|
|
|
// shift over values
|
|
|
|
if (typeof window !== 'object' || window.constructor !== BrowserWindow) {
|
|
|
|
[newPosition, newY, newX, newWindow] = [y, x, window, null]
|
|
|
|
}
|
2017-10-23 18:47:47 +00:00
|
|
|
}
|
2017-10-23 22:35:16 +00:00
|
|
|
|
|
|
|
// menu.popup(window, {})
|
2017-10-24 22:52:12 +00:00
|
|
|
if (x && typeof x === 'object') {
|
2017-10-24 04:07:39 +00:00
|
|
|
const opts = x
|
|
|
|
newX = opts.x
|
|
|
|
newY = opts.y
|
|
|
|
newPosition = opts.positioningItem
|
|
|
|
asyncPopup = opts.async
|
2017-10-23 16:11:59 +00:00
|
|
|
}
|
2017-10-23 22:35:16 +00:00
|
|
|
|
2017-10-24 04:07:39 +00:00
|
|
|
// set defaults
|
|
|
|
if (typeof x !== 'number') newX = -1
|
|
|
|
if (typeof y !== 'number') newY = -1
|
|
|
|
if (typeof positioningItem !== 'number') newPosition = -1
|
2017-10-24 22:52:12 +00:00
|
|
|
if (!window) newWindow = BrowserWindow.getFocusedWindow()
|
2017-10-23 22:35:16 +00:00
|
|
|
if (typeof asyncPopup !== 'boolean') asyncPopup = false
|
|
|
|
|
2017-10-24 04:07:39 +00:00
|
|
|
this.popupAt(newWindow, newX, newY, newPosition, asyncPopup)
|
2017-10-23 18:47:47 +00:00
|
|
|
}
|
2017-10-23 16:11:59 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
Menu.prototype.closePopup = function (window) {
|
2017-10-24 22:52:12 +00:00
|
|
|
if (!window || window.constructor !== BrowserWindow) {
|
2017-10-24 00:04:22 +00:00
|
|
|
window = BrowserWindow.getFocusedWindow()
|
|
|
|
}
|
2017-10-24 22:52:12 +00:00
|
|
|
if (window) this.closePopupAt(window.id)
|
2017-10-24 00:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Menu.prototype.getMenuItemById = function (id) {
|
|
|
|
const items = this.items
|
|
|
|
|
|
|
|
let found = items.find(item => item.id === id) || null
|
2017-10-24 03:46:39 +00:00
|
|
|
for (let i = 0; !found && i < items.length; i++) {
|
2017-10-24 00:04:22 +00:00
|
|
|
if (items[i].submenu) {
|
|
|
|
found = items[i].submenu.getMenuItemById(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2017-10-23 22:35:16 +00:00
|
|
|
Menu.prototype.append = function (item) {
|
2017-10-23 18:47:47 +00:00
|
|
|
return this.insert(this.getItemCount(), item)
|
|
|
|
}
|
2017-10-23 16:11:59 +00:00
|
|
|
|
2017-10-23 22:35:16 +00:00
|
|
|
Menu.prototype.insert = function (pos, item) {
|
2017-10-24 22:52:12 +00:00
|
|
|
if ((item ? item.constructor : void 0) !== MenuItem) {
|
2017-10-23 18:47:47 +00:00
|
|
|
throw new TypeError('Invalid item')
|
|
|
|
}
|
|
|
|
|
2017-10-24 03:46:39 +00:00
|
|
|
// insert item depending on its type
|
|
|
|
insertItemByType.call(this, item, pos)
|
2017-10-23 16:11:59 +00:00
|
|
|
|
2017-10-24 03:46:39 +00:00
|
|
|
// set item properties
|
2017-10-24 22:52:12 +00:00
|
|
|
if (item.sublabel) this.setSublabel(pos, item.sublabel)
|
|
|
|
if (item.icon) this.setIcon(pos, item.icon)
|
|
|
|
if (item.role) this.setRole(pos, item.role)
|
2017-10-23 16:11:59 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
// Make menu accessable to items.
|
2017-10-23 18:47:47 +00:00
|
|
|
item.overrideReadOnlyProperty('menu', this)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
// Remember the items.
|
2017-10-23 18:47:47 +00:00
|
|
|
this.items.splice(pos, 0, item)
|
2017-10-24 00:04:22 +00:00
|
|
|
this.commandsMap[item.commandId] = item
|
2017-10-23 18:47:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 22:35:16 +00:00
|
|
|
Menu.prototype._callMenuWillShow = function () {
|
2017-10-24 22:52:12 +00:00
|
|
|
if (this.delegate) this.delegate.menuWillShow()
|
2017-10-23 22:35:16 +00:00
|
|
|
this.items.forEach(item => {
|
2017-10-24 22:52:12 +00:00
|
|
|
if (item.submenu) item.submenu._callMenuWillShow()
|
2017-10-23 22:35:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-24 04:07:39 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
2017-10-24 03:46:39 +00:00
|
|
|
Menu.getApplicationMenu = () => applicationMenu
|
|
|
|
|
|
|
|
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
|
|
|
|
|
|
|
|
// set application menu with a preexisting menu
|
2017-10-23 22:35:16 +00:00
|
|
|
Menu.setApplicationMenu = function (menu) {
|
2017-11-08 03:44:24 +00:00
|
|
|
if (menu && menu.constructor !== Menu) {
|
2017-10-23 22:35:16 +00:00
|
|
|
throw new TypeError('Invalid menu')
|
|
|
|
}
|
2017-10-23 18:47:47 +00:00
|
|
|
|
2017-10-23 22:35:16 +00:00
|
|
|
applicationMenu = menu
|
2017-10-23 18:47:47 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2017-10-24 22:52:12 +00:00
|
|
|
if (!menu) return
|
2017-10-23 18:47:47 +00:00
|
|
|
menu._callMenuWillShow()
|
2017-10-24 00:04:22 +00:00
|
|
|
bindings.setApplicationMenu(menu)
|
2017-10-23 18:47:47 +00:00
|
|
|
} else {
|
|
|
|
const windows = BrowserWindow.getAllWindows()
|
2017-10-24 00:04:22 +00:00
|
|
|
return windows.map(w => w.setMenu(menu))
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-23 22:35:16 +00:00
|
|
|
Menu.buildFromTemplate = function (template) {
|
|
|
|
if (!(template instanceof Array)) {
|
|
|
|
throw new TypeError('Invalid template for Menu')
|
|
|
|
}
|
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
const menu = new Menu()
|
2017-10-23 22:35:16 +00:00
|
|
|
const positioned = []
|
|
|
|
let idx = 0
|
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
// sort template by position
|
2017-10-23 22:35:16 +00:00
|
|
|
template.forEach(item => {
|
2017-10-24 00:04:22 +00:00
|
|
|
idx = (item.position) ? indexToInsertByPosition(positioned, item.position) : idx += 1
|
2017-10-23 22:35:16 +00:00
|
|
|
positioned.splice(idx, 0, item)
|
|
|
|
})
|
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
// add each item from positioned menu to application menu
|
2017-10-23 22:35:16 +00:00
|
|
|
positioned.forEach((item) => {
|
|
|
|
if (typeof item !== 'object') {
|
|
|
|
throw new TypeError('Invalid template for MenuItem')
|
|
|
|
}
|
|
|
|
menu.append(new MenuItem(item))
|
|
|
|
})
|
|
|
|
|
|
|
|
return menu
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-24 04:07:39 +00:00
|
|
|
/* Helper Functions */
|
2017-10-23 18:47:47 +00:00
|
|
|
|
2017-10-24 03:46:39 +00:00
|
|
|
// Search between separators to find a radio menu item and return its group id
|
2017-10-24 00:04:22 +00:00
|
|
|
function generateGroupId (items, pos) {
|
2017-10-23 22:35:16 +00:00
|
|
|
if (pos > 0) {
|
2017-10-24 04:24:57 +00:00
|
|
|
for (let idx = pos - 1; idx >= 0; idx--) {
|
|
|
|
if (items[idx].type === 'radio') return items[idx].groupId
|
|
|
|
if (items[idx].type === 'separator') break
|
2017-10-23 22:35:16 +00:00
|
|
|
}
|
|
|
|
} else if (pos < items.length) {
|
2017-10-24 04:24:57 +00:00
|
|
|
for (let idx = pos; idx <= items.length - 1; idx++) {
|
|
|
|
if (items[idx].type === 'radio') return items[idx].groupId
|
|
|
|
if (items[idx].type === 'separator') break
|
2017-10-23 03:57:23 +00:00
|
|
|
}
|
2017-10-23 18:47:47 +00:00
|
|
|
}
|
2017-10-24 03:46:39 +00:00
|
|
|
groupIdIndex += 1
|
|
|
|
return groupIdIndex
|
2017-10-23 22:35:16 +00:00
|
|
|
}
|
2017-10-23 18:47:47 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
function indexOfItemById (items, id) {
|
2017-10-24 22:52:12 +00:00
|
|
|
const foundItem = items.find(item => item.id === id) || -1
|
2017-10-24 00:04:22 +00:00
|
|
|
return items.indexOf(foundItem)
|
2017-10-23 22:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function indexToInsertByPosition (items, position) {
|
|
|
|
if (!position) return items.length
|
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
const [query, id] = position.split('=') // parse query and id from position
|
|
|
|
const idx = indexOfItemById(items, id) // calculate initial index of item
|
|
|
|
|
|
|
|
// warn if query doesn't exist
|
2017-10-23 22:35:16 +00:00
|
|
|
if (idx === -1 && query !== 'endof') {
|
2017-10-24 22:52:12 +00:00
|
|
|
console.warn(`Item with id ${id} is not found`)
|
2017-10-23 22:35:16 +00:00
|
|
|
return items.length
|
|
|
|
}
|
2017-10-23 18:47:47 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
// compute new index based on query
|
2017-10-23 22:35:16 +00:00
|
|
|
const queries = {
|
2017-10-25 14:17:41 +00:00
|
|
|
after: (index) => {
|
2017-10-24 02:35:42 +00:00
|
|
|
index += 1
|
|
|
|
return index
|
|
|
|
},
|
2017-10-25 14:17:41 +00:00
|
|
|
endof: (index) => {
|
2017-10-24 00:04:22 +00:00
|
|
|
if (index === -1) {
|
2017-10-23 22:35:16 +00:00
|
|
|
items.push({id, type: 'separator'})
|
2017-10-24 00:04:22 +00:00
|
|
|
index = items.length - 1
|
2017-10-23 22:35:16 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
index += 1
|
|
|
|
while (index < items.length && items[index].type !== 'separator') index += 1
|
|
|
|
return index
|
2017-10-23 03:57:23 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-23 18:47:47 +00:00
|
|
|
|
2017-10-24 00:04:22 +00:00
|
|
|
// return new index if needed, or original indexOfItemById
|
|
|
|
return (query in queries) ? queries[query](idx) : idx
|
2017-10-23 22:35:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 04:07:39 +00:00
|
|
|
function insertItemByType (item, pos) {
|
2017-10-24 03:46:39 +00:00
|
|
|
const types = {
|
2017-10-25 14:17:41 +00:00
|
|
|
normal: () => this.insertItem(pos, item.commandId, item.label),
|
|
|
|
checkbox: () => this.insertCheckItem(pos, item.commandId, item.label),
|
|
|
|
separator: () => this.insertSeparator(pos),
|
|
|
|
submenu: () => this.insertSubMenu(pos, item.commandId, item.label, item.submenu),
|
|
|
|
radio: () => {
|
2017-10-24 03:46:39 +00:00
|
|
|
// Grouping radio menu items
|
|
|
|
item.overrideReadOnlyProperty('groupId', generateGroupId(this.items, pos))
|
|
|
|
if (this.groupsMap[item.groupId] == null) {
|
|
|
|
this.groupsMap[item.groupId] = []
|
|
|
|
}
|
|
|
|
this.groupsMap[item.groupId].push(item)
|
|
|
|
|
|
|
|
// Setting a radio menu item should flip other items in the group.
|
|
|
|
v8Util.setHiddenValue(item, 'checked', item.checked)
|
|
|
|
Object.defineProperty(item, 'checked', {
|
|
|
|
enumerable: true,
|
|
|
|
get: () => v8Util.getHiddenValue(item, 'checked'),
|
|
|
|
set: () => {
|
|
|
|
this.groupsMap[item.groupId].forEach(other => {
|
|
|
|
if (other !== item) v8Util.setHiddenValue(other, 'checked', false)
|
|
|
|
})
|
|
|
|
v8Util.setHiddenValue(item, 'checked', true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.insertRadioItem(pos, item.commandId, item.label, item.groupId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
types[item.type]()
|
|
|
|
}
|
|
|
|
|
2017-10-24 02:35:42 +00:00
|
|
|
module.exports = Menu
|