forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmenu-helpers.js
138 lines (123 loc) · 3.22 KB
/
menu-helpers.js
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
const _ = require('underscore-plus');
const ItemSpecificities = new WeakMap();
// Add an item to a menu, ensuring separators are not duplicated.
function addItemToMenu(item, menu) {
const lastMenuItem = _.last(menu);
const lastMenuItemIsSpearator =
lastMenuItem && lastMenuItem.type === 'separator';
if (!(item.type === 'separator' && lastMenuItemIsSpearator)) {
menu.push(item);
}
}
function merge(menu, item, itemSpecificity = Infinity) {
item = cloneMenuItem(item);
ItemSpecificities.set(item, itemSpecificity);
const matchingItemIndex = findMatchingItemIndex(menu, item);
if (matchingItemIndex === -1) {
addItemToMenu(item, menu);
return;
}
const matchingItem = menu[matchingItemIndex];
if (item.submenu != null) {
for (let submenuItem of item.submenu) {
merge(matchingItem.submenu, submenuItem, itemSpecificity);
}
} else if (
itemSpecificity &&
itemSpecificity >= ItemSpecificities.get(matchingItem)
) {
menu[matchingItemIndex] = item;
}
}
function unmerge(menu, item) {
item = cloneMenuItem(item);
const matchingItemIndex = findMatchingItemIndex(menu, item);
if (matchingItemIndex === -1) {
return;
}
const matchingItem = menu[matchingItemIndex];
if (item.submenu != null) {
for (let submenuItem of item.submenu) {
unmerge(matchingItem.submenu, submenuItem);
}
}
if (matchingItem.submenu == null || matchingItem.submenu.length === 0) {
menu.splice(matchingItemIndex, 1);
}
}
function findMatchingItemIndex(menu, { type, id, submenu }) {
if (type === 'separator') {
return -1;
}
for (let index = 0; index < menu.length; index++) {
const item = menu[index];
if (item.id === id && (item.submenu != null) === (submenu != null)) {
return index;
}
}
return -1;
}
function normalizeLabel(label) {
if (label == null) {
return;
}
return process.platform === 'darwin' ? label : label.replace(/&/g, '');
}
function cloneMenuItem(item) {
item = _.pick(
item,
'type',
'label',
'id',
'enabled',
'visible',
'command',
'submenu',
'commandDetail',
'role',
'accelerator',
'before',
'after',
'beforeGroupContaining',
'afterGroupContaining'
);
if (item.id === null || item.id === undefined) {
item.id = normalizeLabel(item.label);
}
if (item.submenu != null) {
item.submenu = item.submenu.map(submenuItem => cloneMenuItem(submenuItem));
}
return item;
}
// Determine the Electron accelerator for a given Atom keystroke.
//
// keystroke - The keystroke.
//
// Returns a String containing the keystroke in a format that can be interpreted
// by Electron to provide nice icons where available.
function acceleratorForKeystroke(keystroke) {
if (!keystroke) {
return null;
}
let modifiers = keystroke.split(/-(?=.)/);
const key = modifiers
.pop()
.toUpperCase()
.replace('+', 'Plus');
modifiers = modifiers.map(modifier =>
modifier
.replace(/shift/gi, 'Shift')
.replace(/cmd/gi, 'Command')
.replace(/ctrl/gi, 'Ctrl')
.replace(/alt/gi, 'Alt')
);
const keys = [...modifiers, key];
return keys.join('+');
}
module.exports = {
merge,
unmerge,
normalizeLabel,
cloneMenuItem,
acceleratorForKeystroke
};