forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.js
More file actions
309 lines (282 loc) · 8.66 KB
/
menu.js
File metadata and controls
309 lines (282 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
'use strict'
const {BrowserWindow, MenuItem, webContents} = require('electron')
const EventEmitter = require('events').EventEmitter
const v8Util = process.atomBinding('v8_util')
const bindings = process.atomBinding('menu')
// Automatically generated radio menu item's group id.
var nextGroupId = 0
// Search between separators to find a radio menu item and return its group id,
// otherwise generate a group id.
var generateGroupId = function (items, pos) {
var i, item, j, k, ref1, ref2, ref3
if (pos > 0) {
for (i = j = ref1 = pos - 1; ref1 <= 0 ? j <= 0 : j >= 0; i = ref1 <= 0 ? ++j : --j) {
item = items[i]
if (item.type === 'radio') {
return item.groupId
}
if (item.type === 'separator') {
break
}
}
} 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]
if (item.type === 'radio') {
return item.groupId
}
if (item.type === 'separator') {
break
}
}
}
return ++nextGroupId
}
// Returns the index of item according to |id|.
var indexOfItemById = function (items, id) {
var i, item, j, len
for (i = j = 0, len = items.length; j < len; i = ++j) {
item = items[i]
if (item.id === id) {
return i
}
}
return -1
}
// Returns the index of where to insert the item according to |position|.
var indexToInsertByPosition = function (items, position) {
var insertIndex
if (!position) {
return items.length
}
const [query, id] = position.split('=')
insertIndex = indexOfItemById(items, id)
if (insertIndex === -1 && query !== 'endof') {
console.warn("Item with id '" + id + "' is not found")
return items.length
}
switch (query) {
case 'after':
insertIndex++
break
case 'endof':
// If the |id| doesn't exist, then create a new group with the |id|.
if (insertIndex === -1) {
items.push({
id: id,
type: 'separator'
})
insertIndex = items.length - 1
}
// Find the end of the group.
insertIndex++
while (insertIndex < items.length && items[insertIndex].type !== 'separator') {
insertIndex++
}
}
return insertIndex
}
const Menu = bindings.Menu
Object.setPrototypeOf(Menu.prototype, EventEmitter.prototype)
Menu.prototype._init = function () {
this.commandsMap = {}
this.groupsMap = {}
this.items = []
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
}
checked = true
break
}
if (!checked) {
v8Util.setHiddenValue(group[0], 'checked', true)
}
}
}
}
}
Menu.prototype.popup = function (window, x, y, positioningItem) {
if (typeof window !== 'object' || window.constructor !== BrowserWindow) {
// Shift.
positioningItem = y
y = x
x = window
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
this.popupAt(window, x, y, positioningItem)
}
Menu.prototype.append = function (item) {
return this.insert(this.getItemCount(), item)
}
Menu.prototype.insert = function (pos, item) {
var base, name
if ((item != null ? item.constructor : void 0) !== MenuItem) {
throw new TypeError('Invalid item')
}
switch (item.type) {
case 'normal':
this.insertItem(pos, item.commandId, item.label)
break
case 'checkbox':
this.insertCheckItem(pos, item.commandId, item.label)
break
case 'separator':
this.insertSeparator(pos)
break
case 'submenu':
this.insertSubMenu(pos, item.commandId, item.label, item.submenu)
break
case 'radio':
// Grouping radio menu items.
item.overrideReadOnlyProperty('groupId', generateGroupId(this.items, pos))
if ((base = this.groupsMap)[name = item.groupId] == null) {
base[name] = []
}
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: function () {
return v8Util.getHiddenValue(item, 'checked')
},
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)
}
}
return v8Util.setHiddenValue(item, 'checked', true)
}
})
this.insertRadioItem(pos, item.commandId, item.label, item.groupId)
}
if (item.sublabel != null) {
this.setSublabel(pos, item.sublabel)
}
if (item.icon != null) {
this.setIcon(pos, item.icon)
}
if (item.role != null) {
this.setRole(pos, item.role)
}
// Make menu accessable to items.
item.overrideReadOnlyProperty('menu', this)
// Remember the items.
this.items.splice(pos, 0, item)
this.commandsMap[item.commandId] = item
}
// Force menuWillShow to be called
Menu.prototype._callMenuWillShow = function () {
if (this.delegate != null) {
this.delegate.menuWillShow()
}
this.items.forEach(function (item) {
if (item.submenu != null) {
item.submenu._callMenuWillShow()
}
})
}
var applicationMenu = null
Menu.setApplicationMenu = function (menu) {
if (!(menu === null || menu.constructor === Menu)) {
throw new TypeError('Invalid menu')
}
// Keep a reference.
applicationMenu = menu
if (process.platform === 'darwin') {
if (menu === null) {
return
}
menu._callMenuWillShow()
bindings.setApplicationMenu(menu)
} else {
BrowserWindow.getAllWindows().forEach(function (window) {
window.setMenu(menu)
})
}
}
Menu.getApplicationMenu = function () {
return applicationMenu
}
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
Menu.buildFromTemplate = function (template) {
var insertIndex, item, j, k, key, len, len1, menu, menuItem, positionedTemplate
if (!Array.isArray(template)) {
throw new TypeError('Invalid template for Menu')
}
positionedTemplate = []
insertIndex = 0
for (j = 0, len = template.length; j < len; j++) {
item = template[j]
if (item.position) {
insertIndex = indexToInsertByPosition(positionedTemplate, item.position)
} else {
// If no |position| is specified, insert after last item.
insertIndex++
}
positionedTemplate.splice(insertIndex, 0, item)
}
menu = new Menu()
for (k = 0, len1 = positionedTemplate.length; k < len1; k++) {
item = positionedTemplate[k]
if (typeof item !== 'object') {
throw new TypeError('Invalid template for MenuItem')
}
menuItem = new MenuItem(item)
for (key in item) {
// Preserve extra fields specified by user
if (!menuItem.hasOwnProperty(key)) {
menuItem[key] = item[key]
}
}
menu.append(menuItem)
}
return menu
}
module.exports = Menu