2017-10-24 00:04:22 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { TopLevelWindow, MenuItem, webContents } = require('electron')
|
2018-09-20 03:43:26 +00:00
|
|
|
const { sortMenuItems } = require('@electron/internal/browser/api/menu-utils')
|
2017-10-24 00:04:22 +00:00
|
|
|
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
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { Menu } = bindings
|
2017-10-24 00:04:22 +00:00
|
|
|
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
|
|
|
|
2018-02-20 16:11:35 +00:00
|
|
|
// Menu Delegate.
|
|
|
|
// This object should hold no reference to |Menu| to avoid cyclic reference.
|
|
|
|
const delegate = {
|
|
|
|
isCommandIdChecked: (menu, id) => menu.commandsMap[id] ? menu.commandsMap[id].checked : undefined,
|
|
|
|
isCommandIdEnabled: (menu, id) => menu.commandsMap[id] ? menu.commandsMap[id].enabled : undefined,
|
2019-02-28 17:00:54 +00:00
|
|
|
shouldCommandIdWorkWhenHidden: (menu, id) => menu.commandsMap[id] ? menu.commandsMap[id].acceleratorWorksWhenHidden : undefined,
|
2018-02-20 16:11:35 +00:00
|
|
|
isCommandIdVisible: (menu, id) => menu.commandsMap[id] ? menu.commandsMap[id].visible : undefined,
|
|
|
|
getAcceleratorForCommandId: (menu, id, useDefaultAccelerator) => {
|
|
|
|
const command = menu.commandsMap[id]
|
|
|
|
if (!command) return
|
2018-03-28 00:32:55 +00:00
|
|
|
if (command.accelerator != null) return command.accelerator
|
2018-02-20 16:11:35 +00:00
|
|
|
if (useDefaultAccelerator) return command.getDefaultRoleAccelerator()
|
|
|
|
},
|
2018-11-26 18:43:55 +00:00
|
|
|
shouldRegisterAcceleratorForCommandId: (menu, id) => menu.commandsMap[id] ? menu.commandsMap[id].registerAccelerator : undefined,
|
2018-02-20 16:11:35 +00:00
|
|
|
executeCommand: (menu, event, id) => {
|
|
|
|
const command = menu.commandsMap[id]
|
|
|
|
if (!command) return
|
2018-04-17 08:14:49 +00:00
|
|
|
command.click(event, TopLevelWindow.getFocusedWindow(), webContents.getFocusedWebContents())
|
2018-02-20 16:11:35 +00:00
|
|
|
},
|
|
|
|
menuWillShow: (menu) => {
|
2019-02-20 12:20:24 +00:00
|
|
|
// Ensure radio groups have at least one menu item selected
|
2018-02-20 16:11:35 +00:00
|
|
|
for (const id in menu.groupsMap) {
|
|
|
|
const found = menu.groupsMap[id].find(item => item.checked) || null
|
|
|
|
if (!found) v8Util.setHiddenValue(menu.groupsMap[id][0], 'checked', true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 = []
|
2018-02-20 16:11:35 +00:00
|
|
|
this.delegate = delegate
|
2017-10-23 18:47:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 22:38:52 +00:00
|
|
|
Menu.prototype.popup = function (options = {}) {
|
2018-03-16 21:31:10 +00:00
|
|
|
if (options == null || typeof options !== 'object') {
|
|
|
|
throw new TypeError('Options must be an object')
|
|
|
|
}
|
2018-09-13 16:10:51 +00:00
|
|
|
let { window, x, y, positioningItem, callback } = options
|
2017-10-23 22:35:16 +00:00
|
|
|
|
2018-02-20 01:59:27 +00:00
|
|
|
// no callback passed
|
2018-02-20 16:10:53 +00:00
|
|
|
if (!callback || typeof callback !== 'function') callback = () => {}
|
2017-10-23 22:35:16 +00:00
|
|
|
|
2017-10-24 04:07:39 +00:00
|
|
|
// set defaults
|
2018-02-20 01:59:27 +00:00
|
|
|
if (typeof x !== 'number') x = -1
|
|
|
|
if (typeof y !== 'number') y = -1
|
2018-02-20 16:28:34 +00:00
|
|
|
if (typeof positioningItem !== 'number') positioningItem = -1
|
2018-02-20 01:59:27 +00:00
|
|
|
|
2018-02-21 00:35:39 +00:00
|
|
|
// find which window to use
|
2018-04-17 08:14:49 +00:00
|
|
|
const wins = TopLevelWindow.getAllWindows()
|
2018-02-21 00:35:39 +00:00
|
|
|
if (!wins || wins.indexOf(window) === -1) {
|
2018-04-17 08:14:49 +00:00
|
|
|
window = TopLevelWindow.getFocusedWindow()
|
2018-02-21 00:35:39 +00:00
|
|
|
if (!window && wins && wins.length > 0) {
|
|
|
|
window = wins[0]
|
2018-02-21 00:38:05 +00:00
|
|
|
}
|
|
|
|
if (!window) {
|
2018-04-17 08:14:49 +00:00
|
|
|
throw new Error(`Cannot open Menu without a TopLevelWindow present`)
|
2017-12-08 22:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 22:35:16 +00:00
|
|
|
|
2018-02-20 16:28:34 +00:00
|
|
|
this.popupAt(window, x, y, positioningItem, callback)
|
|
|
|
return { browserWindow: window, x, y, position: positioningItem }
|
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) {
|
2018-04-17 08:14:49 +00:00
|
|
|
if (window instanceof TopLevelWindow) {
|
2017-12-21 06:26:32 +00:00
|
|
|
this.closePopupAt(window.id)
|
|
|
|
} else {
|
|
|
|
// Passing -1 (invalid) would make closePopupAt close the all menu runners
|
|
|
|
// belong to this menu.
|
|
|
|
this.closePopupAt(-1)
|
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')
|
|
|
|
}
|
|
|
|
|
2019-03-18 14:58:42 +00:00
|
|
|
if (pos < 0) {
|
|
|
|
throw new RangeError(`Position ${pos} cannot be less than 0`)
|
|
|
|
} else if (pos > this.getItemCount()) {
|
|
|
|
throw new RangeError(`Position ${pos} cannot be greater than the total MenuItem count`)
|
|
|
|
}
|
|
|
|
|
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 () {
|
2018-02-20 16:11:35 +00:00
|
|
|
if (this.delegate) this.delegate.menuWillShow(this)
|
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
|
2019-01-15 20:35:53 +00:00
|
|
|
v8Util.setHiddenValue(global, 'applicationMenuSet', true)
|
|
|
|
|
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 {
|
2018-04-17 08:14:49 +00:00
|
|
|
const windows = TopLevelWindow.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) {
|
2018-04-24 15:21:26 +00:00
|
|
|
if (!Array.isArray(template)) {
|
2018-08-17 20:10:14 +00:00
|
|
|
throw new TypeError('Invalid template for Menu: Menu template must be an array')
|
2017-10-23 22:35:16 +00:00
|
|
|
}
|
2018-08-17 20:10:14 +00:00
|
|
|
if (!areValidTemplateItems(template)) {
|
|
|
|
throw new TypeError('Invalid template for MenuItem: must have at least one of label, role or type')
|
|
|
|
}
|
2018-02-05 17:55:12 +00:00
|
|
|
const filtered = removeExtraSeparators(template)
|
2018-05-05 16:37:29 +00:00
|
|
|
const sorted = sortTemplate(filtered)
|
2018-02-05 17:55:12 +00:00
|
|
|
|
2019-01-10 13:32:03 +00:00
|
|
|
const menu = new Menu()
|
2019-02-06 18:04:40 +00:00
|
|
|
sorted.forEach(item => {
|
|
|
|
if (item instanceof MenuItem) {
|
|
|
|
menu.append(item)
|
|
|
|
} else {
|
|
|
|
menu.append(new MenuItem(item))
|
|
|
|
}
|
|
|
|
})
|
2017-10-23 22:35:16 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-08-17 20:10:14 +00:00
|
|
|
// validate the template against having the wrong attribute
|
|
|
|
function areValidTemplateItems (template) {
|
|
|
|
return template.every(item =>
|
2019-02-06 18:04:40 +00:00
|
|
|
item != null &&
|
|
|
|
typeof item === 'object' &&
|
|
|
|
(item.hasOwnProperty('label') ||
|
|
|
|
item.hasOwnProperty('role') ||
|
|
|
|
item.type === 'separator'))
|
2018-08-17 20:10:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 16:37:29 +00:00
|
|
|
function sortTemplate (template) {
|
|
|
|
const sorted = sortMenuItems(template)
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const id in sorted) {
|
2018-05-05 16:37:29 +00:00
|
|
|
const item = sorted[id]
|
|
|
|
if (Array.isArray(item.submenu)) {
|
|
|
|
item.submenu = sortTemplate(item.submenu)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sorted
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-05 17:55:12 +00:00
|
|
|
function removeExtraSeparators (items) {
|
|
|
|
// fold adjacent separators together
|
2018-05-04 14:59:40 +00:00
|
|
|
let ret = items.filter((e, idx, arr) => {
|
|
|
|
if (e.visible === false) return true
|
|
|
|
return e.type !== 'separator' || idx === 0 || arr[idx - 1].type !== 'separator'
|
|
|
|
})
|
2018-02-05 17:55:12 +00:00
|
|
|
|
|
|
|
// remove edge separators
|
2018-05-04 14:59:40 +00:00
|
|
|
ret = ret.filter((e, idx, arr) => {
|
|
|
|
if (e.visible === false) return true
|
|
|
|
return e.type !== 'separator' || (idx !== 0 && idx !== arr.length - 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
return ret
|
2018-02-05 17:55:12 +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
|