electron/lib/browser/api/menu.js

322 lines
9.1 KiB
JavaScript
Raw Normal View History

'use strict'
const {BrowserWindow, MenuItem, webContents} = require('electron')
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.
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) {
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) {
item = items[i]
2016-01-12 02:40:23 +00:00
if (item.type === 'radio') {
return item.groupId
2016-01-12 02:40:23 +00:00
}
if (item.type === 'separator') {
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) {
item = items[i]
2016-01-12 02:40:23 +00:00
if (item.type === 'radio') {
return item.groupId
2016-01-12 02:40:23 +00:00
}
if (item.type === 'separator') {
break
2016-01-12 02:40:23 +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
}
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') {
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-01-12 02:40:23 +00:00
const Menu = bindings.Menu
2016-01-12 02:40:23 +00:00
Object.setPrototypeOf(Menu.prototype, EventEmitter.prototype)
2016-01-12 02:40:23 +00:00
Menu.prototype._init = function () {
this.commandsMap = {}
this.groupsMap = {}
this.items = []
2016-03-29 00:40:40 +00:00
this.delegate = {
isCommandIdChecked: (commandId) => {
2017-10-23 04:16:35 +00:00
const command = this.commandsMap[commandId]
return command != null ? command.checked : undefined
},
isCommandIdEnabled: (commandId) => {
2017-10-23 04:16:35 +00:00
const command = this.commandsMap[commandId]
return command != null ? command.enabled : undefined
},
isCommandIdVisible: (commandId) => {
2017-10-23 04:16:35 +00:00
const command = this.commandsMap[commandId]
return command != null ? command.visible : undefined
},
getAcceleratorForCommandId: (commandId, useDefaultAccelerator) => {
const command = this.commandsMap[commandId]
if (command == null) return
if (command.accelerator != null) return command.accelerator
if (useDefaultAccelerator) return command.getDefaultRoleAccelerator()
},
getIconForCommandId: (commandId) => {
2017-10-23 04:16:35 +00:00
const command = this.commandsMap[commandId]
return command != null ? command.icon : undefined
},
executeCommand: (event, commandId) => {
const command = this.commandsMap[commandId]
if (command == null) return
command.click(event, BrowserWindow.getFocusedWindow(), webContents.getFocusedWebContents())
},
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
checked = true
break
}
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-01-12 02:40:23 +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]
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
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
if (typeof asyncPopup !== 'boolean') asyncPopup = false
2017-10-23 04:47:02 +00:00
this.popupAt(window, newX, newY, newPositioningItem, asyncPopup)
}
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
Menu.prototype.append = function (item) {
return this.insert(this.getItemCount(), item)
}
2016-01-12 02:40:23 +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) {
throw new TypeError('Invalid item')
2016-01-12 02:40:23 +00:00
}
switch (item.type) {
case 'normal':
this.insertItem(pos, item.commandId, item.label)
break
2016-01-12 02:40:23 +00:00
case 'checkbox':
this.insertCheckItem(pos, item.commandId, item.label)
break
2016-01-12 02:40:23 +00:00
case 'separator':
this.insertSeparator(pos)
break
2016-01-12 02:40:23 +00:00
case 'submenu':
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.
item.overrideReadOnlyProperty('groupId', generateGroupId(this.items, pos))
2016-01-12 02:40:23 +00:00
if ((base = this.groupsMap)[name = item.groupId] == null) {
base[name] = []
2016-01-12 02:40:23 +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.
v8Util.setHiddenValue(item, 'checked', item.checked)
2016-01-12 02:40:23 +00:00
Object.defineProperty(item, 'checked', {
enumerable: true,
get: function () {
return v8Util.getHiddenValue(item, 'checked')
2016-01-12 02:40:23 +00:00
},
set: () => {
var j, len, otherItem, ref1
ref1 = this.groupsMap[item.groupId]
for (j = 0, len = ref1.length; j < len; j++) {
otherItem = ref1[j]
if (otherItem !== item) {
v8Util.setHiddenValue(otherItem, 'checked', false)
2016-01-12 02:40:23 +00:00
}
}
return v8Util.setHiddenValue(item, 'checked', true)
}
})
this.insertRadioItem(pos, item.commandId, item.label, item.groupId)
2016-01-12 02:40:23 +00:00
}
if (item.sublabel != null) {
this.setSublabel(pos, item.sublabel)
2016-01-12 02:40:23 +00:00
}
if (item.icon != null) {
this.setIcon(pos, item.icon)
2016-01-12 02:40:23 +00:00
}
if (item.role != null) {
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.
item.overrideReadOnlyProperty('menu', this)
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Remember the items.
this.items.splice(pos, 0, item)
2016-03-29 00:40:40 +00:00
this.commandsMap[item.commandId] = item
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Force menuWillShow to be called
Menu.prototype._callMenuWillShow = function () {
if (this.delegate != null) {
this.delegate.menuWillShow()
2016-01-12 02:40:23 +00:00
}
this.items.forEach(function (item) {
2016-01-12 02:40:23 +00:00
if (item.submenu != null) {
item.submenu._callMenuWillShow()
2016-01-12 02:40:23 +00:00
}
})
}
2016-01-12 02:40:23 +00:00
var applicationMenu = null
2016-01-12 02:40:23 +00:00
Menu.setApplicationMenu = function (menu) {
2016-01-12 02:40:23 +00:00
if (!(menu === null || menu.constructor === Menu)) {
throw new TypeError('Invalid menu')
2016-01-12 02:40:23 +00:00
}
2016-01-14 18:35:29 +00:00
// Keep a reference.
applicationMenu = menu
2016-01-12 02:40:23 +00:00
if (process.platform === 'darwin') {
if (menu === null) {
return
2016-01-12 02:40:23 +00:00
}
menu._callMenuWillShow()
bindings.setApplicationMenu(menu)
2016-01-12 02:40:23 +00:00
} else {
BrowserWindow.getAllWindows().forEach(function (window) {
window.setMenu(menu)
})
2016-01-12 02:40:23 +00:00
}
}
2016-01-12 02:40:23 +00:00
Menu.getApplicationMenu = function () {
return applicationMenu
}
2016-01-12 02:40:23 +00:00
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
2016-01-12 02:40:23 +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)) {
throw new TypeError('Invalid template for Menu')
2016-01-12 02:40:23 +00:00
}
positionedTemplate = []
insertIndex = 0
2016-01-12 02:40:23 +00:00
for (j = 0, len = template.length; j < len; j++) {
item = template[j]
2016-01-12 02:40:23 +00:00
if (item.position) {
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.
insertIndex++
2016-01-12 02:40:23 +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++) {
item = positionedTemplate[k]
2016-01-12 02:40:23 +00:00
if (typeof item !== 'object') {
throw new TypeError('Invalid template for MenuItem')
2016-01-12 02:40:23 +00:00
}
menuItem = new MenuItem(item)
menu.append(menuItem)
2016-01-12 02:40:23 +00:00
}
return menu
}
2016-01-12 02:40:23 +00:00
module.exports = Menu