2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-07-13 19:18:44 +00:00
|
|
|
const {BrowserWindow, MenuItem, webContents} = require('electron')
|
2016-03-24 20:15:04 +00:00
|
|
|
const EventEmitter = require('events').EventEmitter
|
|
|
|
const v8Util = process.atomBinding('v8_util')
|
|
|
|
const bindings = process.atomBinding('menu')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Automatically generated radio menu item's group id.
|
2016-03-24 20:15:04 +00:00
|
|
|
var nextGroupId = 0
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-08 04:22:03 +00:00
|
|
|
// Search between separators to find a radio menu item and return its group id,
|
2016-01-14 18:35:29 +00:00
|
|
|
// otherwise generate a group id.
|
2017-10-23 03:51:33 +00:00
|
|
|
function generateGroupId (items, pos) {
|
2016-03-24 20:15:04 +00:00
|
|
|
var i, item, j, k, ref1, ref2, ref3
|
2016-01-12 02:40:23 +00:00
|
|
|
if (pos > 0) {
|
|
|
|
for (i = j = ref1 = pos - 1; ref1 <= 0 ? j <= 0 : j >= 0; i = ref1 <= 0 ? ++j : --j) {
|
2016-03-24 20:15:04 +00:00
|
|
|
item = items[i]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (item.type === 'radio') {
|
2016-03-24 20:15:04 +00:00
|
|
|
return item.groupId
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (item.type === 'separator') {
|
2016-03-24 20:15:04 +00:00
|
|
|
break
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (pos < items.length) {
|
|
|
|
for (i = k = ref2 = pos, ref3 = items.length - 1; ref2 <= ref3 ? k <= ref3 : k >= ref3; i = ref2 <= ref3 ? ++k : --k) {
|
2016-03-24 20:15:04 +00:00
|
|
|
item = items[i]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (item.type === 'radio') {
|
2016-03-24 20:15:04 +00:00
|
|
|
return item.groupId
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (item.type === 'separator') {
|
2016-03-24 20:15:04 +00:00
|
|
|
break
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return ++nextGroupId
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Returns the index of item according to |id|.
|
2017-10-23 03:51:33 +00:00
|
|
|
function indexOfItemById (items, id) {
|
|
|
|
for (let idx = 0; idx < items.length; idx += 1) {
|
|
|
|
const item = items[idx]
|
|
|
|
if (item.id === id) return idx
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return -1
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Returns the index of where to insert the item according to |position|.
|
2017-10-23 03:57:23 +00:00
|
|
|
function indexToInsertByPosition (items, pos) {
|
|
|
|
if (!pos) return items.length
|
|
|
|
|
|
|
|
const [query, id] = pos.split('=')
|
|
|
|
let idx = indexOfItemById(items, id)
|
|
|
|
|
|
|
|
if (idx === -1 && query !== 'endof') {
|
2016-03-24 20:15:04 +00:00
|
|
|
console.warn("Item with id '" + id + "' is not found")
|
|
|
|
return items.length
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 03:57:23 +00:00
|
|
|
if (query === 'after') {
|
|
|
|
idx += 1
|
|
|
|
} else if (query === 'endof') {
|
|
|
|
// create new group with id if none exists
|
|
|
|
if (idx === -1) {
|
|
|
|
items.push({
|
|
|
|
id,
|
|
|
|
type: 'separator'
|
|
|
|
})
|
|
|
|
idx = items.length - 1
|
|
|
|
}
|
|
|
|
idx += 1
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-23 03:57:23 +00:00
|
|
|
// search for end of group
|
|
|
|
while (idx < items.length && items[idx].type !== 'separator') {
|
|
|
|
idx += 1
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-23 03:57:23 +00:00
|
|
|
return idx
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const Menu = bindings.Menu
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-29 00:51:11 +00:00
|
|
|
Object.setPrototypeOf(Menu.prototype, EventEmitter.prototype)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.prototype._init = function () {
|
|
|
|
this.commandsMap = {}
|
|
|
|
this.groupsMap = {}
|
|
|
|
this.items = []
|
2016-03-29 00:40:40 +00:00
|
|
|
this.delegate = {
|
2016-03-10 19:54:17 +00:00
|
|
|
isCommandIdChecked: (commandId) => {
|
2017-10-23 04:16:35 +00:00
|
|
|
const command = this.commandsMap[commandId]
|
2016-03-24 20:15:04 +00:00
|
|
|
return command != null ? command.checked : undefined
|
2016-03-10 19:54:17 +00:00
|
|
|
},
|
|
|
|
isCommandIdEnabled: (commandId) => {
|
2017-10-23 04:16:35 +00:00
|
|
|
const command = this.commandsMap[commandId]
|
2016-03-24 20:15:04 +00:00
|
|
|
return command != null ? command.enabled : undefined
|
2016-03-10 19:54:17 +00:00
|
|
|
},
|
|
|
|
isCommandIdVisible: (commandId) => {
|
2017-10-23 04:16:35 +00:00
|
|
|
const command = this.commandsMap[commandId]
|
2016-03-24 20:15:04 +00:00
|
|
|
return command != null ? command.visible : undefined
|
2016-03-10 19:54:17 +00:00
|
|
|
},
|
2016-07-02 02:47:40 +00:00
|
|
|
getAcceleratorForCommandId: (commandId, useDefaultAccelerator) => {
|
|
|
|
const command = this.commandsMap[commandId]
|
|
|
|
if (command == null) return
|
|
|
|
if (command.accelerator != null) return command.accelerator
|
|
|
|
if (useDefaultAccelerator) return command.getDefaultRoleAccelerator()
|
2016-03-10 19:54:17 +00:00
|
|
|
},
|
|
|
|
getIconForCommandId: (commandId) => {
|
2017-10-23 04:16:35 +00:00
|
|
|
const command = this.commandsMap[commandId]
|
2016-03-24 20:15:04 +00:00
|
|
|
return command != null ? command.icon : undefined
|
2016-03-10 19:54:17 +00:00
|
|
|
},
|
2016-06-22 02:22:14 +00:00
|
|
|
executeCommand: (event, commandId) => {
|
2016-07-13 19:18:44 +00:00
|
|
|
const command = this.commandsMap[commandId]
|
|
|
|
if (command == null) return
|
|
|
|
command.click(event, BrowserWindow.getFocusedWindow(), webContents.getFocusedWebContents())
|
2016-03-10 19:54:17 +00:00
|
|
|
},
|
|
|
|
menuWillShow: () => {
|
2017-10-23 04:16:35 +00:00
|
|
|
// Make sure radio groups have at least one menu item selected
|
|
|
|
for (let id in this.groupsMap) {
|
|
|
|
const group = this.groupsMap[id]
|
|
|
|
const checked = false
|
|
|
|
for (let idx = 0; idx < group.length; idx++) {
|
|
|
|
const radioItem = group[idx]
|
|
|
|
if (!radioItem.checked) continue
|
2016-03-24 20:15:04 +00:00
|
|
|
checked = true
|
|
|
|
break
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2017-10-23 04:16:35 +00:00
|
|
|
if (!checked) v8Util.setHiddenValue(group[0], 'checked', true)
|
2016-03-11 19:12:47 +00:00
|
|
|
}
|
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
|
|
|
Menu.prototype.popup = function (window, x, y, positioningItem) {
|
2017-10-23 04:47:02 +00:00
|
|
|
let [newX, newY, newPositioningItem] = [x, y, positioningItem]
|
2017-03-21 03:24:01 +00:00
|
|
|
let asyncPopup
|
2017-02-15 23:57:36 +00:00
|
|
|
|
|
|
|
// menu.popup(x, y, positioningItem)
|
2017-10-23 04:47:02 +00:00
|
|
|
if (window != null) {
|
|
|
|
if (typeof window !== 'object' || window.constructor !== BrowserWindow) {
|
|
|
|
[newPositioningItem, newY, newX] = [y, x, window]
|
|
|
|
window = null
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-01-22 18:59:08 +00:00
|
|
|
|
2017-02-15 23:57:36 +00:00
|
|
|
// menu.popup(window, {})
|
2017-02-22 18:49:25 +00:00
|
|
|
if (x != null && typeof x === 'object') {
|
2017-10-23 04:47:02 +00:00
|
|
|
[newX, newY, newPositioningItem] = [x.x, x.y, x.positioningItem]
|
|
|
|
asyncPopup = x.async
|
2017-02-15 23:57:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 04:47:02 +00:00
|
|
|
// set up defaults
|
2017-02-22 20:53:29 +00:00
|
|
|
if (window == null) window = BrowserWindow.getFocusedWindow()
|
2017-10-23 04:47:02 +00:00
|
|
|
if (typeof x !== 'number') newX = -1
|
|
|
|
if (typeof y !== 'number') newY = -1
|
|
|
|
if (typeof positioningItem !== 'number') newPositioningItem = -1
|
2017-03-21 03:24:01 +00:00
|
|
|
if (typeof asyncPopup !== 'boolean') asyncPopup = false
|
|
|
|
|
2017-10-23 04:47:02 +00:00
|
|
|
this.popupAt(window, newX, newY, newPositioningItem, asyncPopup)
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-02-16 18:58:02 +00:00
|
|
|
Menu.prototype.closePopup = function (window) {
|
|
|
|
if (window == null || window.constructor !== BrowserWindow) {
|
|
|
|
window = BrowserWindow.getFocusedWindow()
|
|
|
|
}
|
|
|
|
if (window != null) {
|
|
|
|
this.closePopupAt(window.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 16:05:45 +00:00
|
|
|
Menu.prototype.getMenuItemById = function (id) {
|
2017-09-26 18:38:34 +00:00
|
|
|
const items = this.items
|
2017-09-26 16:05:45 +00:00
|
|
|
|
2017-09-27 00:35:14 +00:00
|
|
|
let found = items.find(item => item.id === id) || null
|
2017-09-26 13:50:47 +00:00
|
|
|
for (let i = 0, length = items.length; !found && i < length; i++) {
|
2017-09-26 13:47:13 +00:00
|
|
|
if (items[i].submenu) {
|
2017-09-26 16:05:45 +00:00
|
|
|
found = items[i].submenu.getMenuItemById(id)
|
2017-09-26 13:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 13:50:47 +00:00
|
|
|
return found
|
|
|
|
}
|
2017-09-26 13:47:13 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.prototype.append = function (item) {
|
|
|
|
return this.insert(this.getItemCount(), item)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.prototype.insert = function (pos, item) {
|
|
|
|
var base, name
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((item != null ? item.constructor : void 0) !== MenuItem) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Invalid item')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
switch (item.type) {
|
|
|
|
case 'normal':
|
2016-03-24 20:15:04 +00:00
|
|
|
this.insertItem(pos, item.commandId, item.label)
|
|
|
|
break
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'checkbox':
|
2016-03-24 20:15:04 +00:00
|
|
|
this.insertCheckItem(pos, item.commandId, item.label)
|
|
|
|
break
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'separator':
|
2016-03-24 20:15:04 +00:00
|
|
|
this.insertSeparator(pos)
|
|
|
|
break
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'submenu':
|
2016-03-24 20:15:04 +00:00
|
|
|
this.insertSubMenu(pos, item.commandId, item.label, item.submenu)
|
|
|
|
break
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'radio':
|
2016-01-14 18:35:29 +00:00
|
|
|
// Grouping radio menu items.
|
2016-03-24 20:15:04 +00:00
|
|
|
item.overrideReadOnlyProperty('groupId', generateGroupId(this.items, pos))
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((base = this.groupsMap)[name = item.groupId] == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
base[name] = []
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
this.groupsMap[item.groupId].push(item)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Setting a radio menu item should flip other items in the group.
|
2016-03-24 20:15:04 +00:00
|
|
|
v8Util.setHiddenValue(item, 'checked', item.checked)
|
2016-01-12 02:40:23 +00:00
|
|
|
Object.defineProperty(item, 'checked', {
|
|
|
|
enumerable: true,
|
2016-03-24 20:15:04 +00:00
|
|
|
get: function () {
|
|
|
|
return v8Util.getHiddenValue(item, 'checked')
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2016-03-10 19:54:17 +00:00
|
|
|
set: () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
var j, len, otherItem, ref1
|
|
|
|
ref1 = this.groupsMap[item.groupId]
|
2016-03-10 19:54:17 +00:00
|
|
|
for (j = 0, len = ref1.length; j < len; j++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
otherItem = ref1[j]
|
2016-03-10 19:54:17 +00:00
|
|
|
if (otherItem !== item) {
|
2016-03-24 20:15:04 +00:00
|
|
|
v8Util.setHiddenValue(otherItem, 'checked', false)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return v8Util.setHiddenValue(item, 'checked', true)
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
|
|
|
this.insertRadioItem(pos, item.commandId, item.label, item.groupId)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (item.sublabel != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.setSublabel(pos, item.sublabel)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (item.icon != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.setIcon(pos, item.icon)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (item.role != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.setRole(pos, item.role)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Make menu accessable to items.
|
2016-03-24 20:15:04 +00:00
|
|
|
item.overrideReadOnlyProperty('menu', this)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Remember the items.
|
2016-03-24 20:15:04 +00:00
|
|
|
this.items.splice(pos, 0, item)
|
2016-03-29 00:40:40 +00:00
|
|
|
this.commandsMap[item.commandId] = item
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Force menuWillShow to be called
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.prototype._callMenuWillShow = function () {
|
2016-03-10 21:29:57 +00:00
|
|
|
if (this.delegate != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.delegate.menuWillShow()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
this.items.forEach(function (item) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (item.submenu != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
item.submenu._callMenuWillShow()
|
2016-01-12 02:40:23 +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
|
|
|
var applicationMenu = null
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.setApplicationMenu = function (menu) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (!(menu === null || menu.constructor === Menu)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Invalid menu')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Keep a reference.
|
2016-03-24 20:15:04 +00:00
|
|
|
applicationMenu = menu
|
2016-01-12 02:40:23 +00:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
if (menu === null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
menu._callMenuWillShow()
|
|
|
|
bindings.setApplicationMenu(menu)
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.getAllWindows().forEach(function (window) {
|
|
|
|
window.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
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.getApplicationMenu = function () {
|
|
|
|
return applicationMenu
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.buildFromTemplate = function (template) {
|
2016-10-10 22:38:27 +00:00
|
|
|
var insertIndex, item, j, k, len, len1, menu, menuItem, positionedTemplate
|
2016-01-12 02:40:23 +00:00
|
|
|
if (!Array.isArray(template)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Invalid template for Menu')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
positionedTemplate = []
|
|
|
|
insertIndex = 0
|
2016-01-12 02:40:23 +00:00
|
|
|
for (j = 0, len = template.length; j < len; j++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
item = template[j]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (item.position) {
|
2016-03-24 20:15:04 +00:00
|
|
|
insertIndex = indexToInsertByPosition(positionedTemplate, item.position)
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-01-14 18:35:29 +00:00
|
|
|
// If no |position| is specified, insert after last item.
|
2016-03-24 20:15:04 +00:00
|
|
|
insertIndex++
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
positionedTemplate.splice(insertIndex, 0, item)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-29 00:40:40 +00:00
|
|
|
menu = new Menu()
|
2016-01-12 02:40:23 +00:00
|
|
|
for (k = 0, len1 = positionedTemplate.length; k < len1; k++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
item = positionedTemplate[k]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (typeof item !== 'object') {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Invalid template for MenuItem')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
menuItem = new MenuItem(item)
|
|
|
|
menu.append(menuItem)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return menu
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = Menu
|