electron/lib/browser/api/menu.js

328 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.
var generateGroupId = function (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|.
var indexOfItemById = function (items, id) {
var i, item, j, len
2016-01-12 02:40:23 +00:00
for (i = j = 0, len = items.length; j < len; i = ++j) {
item = items[i]
2016-01-12 02:40:23 +00:00
if (item.id === id) {
return i
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|.
var indexToInsertByPosition = function (items, position) {
var insertIndex
2016-01-12 02:40:23 +00:00
if (!position) {
return items.length
2016-01-12 02:40:23 +00:00
}
const [query, id] = position.split('=')
insertIndex = indexOfItemById(items, id)
2016-01-12 02:40:23 +00:00
if (insertIndex === -1 && query !== 'endof') {
console.warn("Item with id '" + id + "' is not found")
return items.length
2016-01-12 02:40:23 +00:00
}
switch (query) {
case 'after':
insertIndex++
break
2016-01-12 02:40:23 +00:00
case 'endof':
2016-01-14 18:35:29 +00:00
// If the |id| doesn't exist, then create a new group with the |id|.
2016-01-12 02:40:23 +00:00
if (insertIndex === -1) {
items.push({
id: id,
type: 'separator'
})
insertIndex = items.length - 1
2016-01-12 02:40:23 +00:00
}
2016-01-14 18:35:29 +00:00
// Find the end of the group.
insertIndex++
2016-01-12 02:40:23 +00:00
while (insertIndex < items.length && items[insertIndex].type !== 'separator') {
insertIndex++
2016-01-12 02:40:23 +00:00
}
}
return insertIndex
}
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) => {
var command = this.commandsMap[commandId]
return command != null ? command.checked : undefined
},
isCommandIdEnabled: (commandId) => {
var command = this.commandsMap[commandId]
return command != null ? command.enabled : undefined
},
isCommandIdVisible: (commandId) => {
var 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) => {
var 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: () => {
// Make sure radio groups have at least one menu item seleted.
var checked, group, id, j, len, radioItem, ref1
ref1 = this.groupsMap
for (id in ref1) {
group = ref1[id]
checked = false
for (j = 0, len = group.length; j < len; j++) {
radioItem = group[j]
if (!radioItem.checked) {
continue
2016-01-12 02:40:23 +00:00
}
checked = true
break
}
if (!checked) {
v8Util.setHiddenValue(group[0], 'checked', true)
2016-01-12 02:40:23 +00:00
}
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-02-15 23:57:36 +00:00
let asyncPopup = false
// menu.popup(x, y, positioningItem)
if (window != null && (typeof window !== 'object' || window.constructor !== BrowserWindow)) {
2016-01-14 18:35:29 +00:00
// Shift.
positioningItem = y
y = x
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-02-15 23:57:36 +00:00
const options = x
x = options.x
y = options.y
positioningItem = options.positioningItem
asyncPopup = options.async
}
// Default to showing in focused window.
if (window == null) window = BrowserWindow.getFocusedWindow()
// Default to showing under mouse location.
if (typeof x !== 'number') x = -1
if (typeof y !== 'number') y = -1
// Default to not highlighting any item.
if (typeof positioningItem !== 'number') positioningItem = -1
2016-01-22 18:59:08 +00:00
2017-02-15 23:57:36 +00:00
this.popupAt(window, x, y, positioningItem, 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)
}
}
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