-
-
Notifications
You must be signed in to change notification settings - Fork 428
/
Copy pathelectron-main-menu-factory.ts
300 lines (277 loc) · 9.68 KB
/
electron-main-menu-factory.ts
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
import * as remote from '@theia/core/electron-shared/@electron/remote';
import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
import {
CommandMenuNode,
CompoundMenuNode,
CompoundMenuNodeRole,
MenuNode,
MenuPath,
} from '@theia/core/lib/common/menu';
import { isOSX } from '@theia/core/lib/common/os';
import {
ElectronMainMenuFactory as TheiaElectronMainMenuFactory,
ElectronMenuItemRole,
ElectronMenuOptions,
} from '@theia/core/lib/electron-browser/menu/electron-main-menu-factory';
import { inject, injectable } from '@theia/core/shared/inversify';
import {
ArduinoMenus,
PlaceholderMenuNode,
} from '../../../browser/menu/arduino-menus';
@injectable()
export class ElectronMainMenuFactory extends TheiaElectronMainMenuFactory {
@inject(FrontendApplicationStateService)
private readonly appStateService: FrontendApplicationStateService;
private appReady = false;
private updateWhenReady = false;
override postConstruct(): void {
super.postConstruct();
this.appStateService.reachedState('ready').then(() => {
this.appReady = true;
if (this.updateWhenReady) {
this.setMenuBar();
}
});
}
override createElectronMenuBar(): Electron.Menu | null {
this._toggledCommands.clear(); // https://github.com/eclipse-theia/theia/issues/8977
return super.createElectronMenuBar();
}
override async setMenuBar(): Promise<void> {
// Avoid updating menu items when the app is not ready.
// Getting the current electron window is not free and synchronous.
// Here, we defer all menu update requests, and fire one when the app is ready.
if (!this.appReady) {
this.updateWhenReady = true;
return;
}
return super.setMenuBar();
}
override createElectronContextMenu(
menuPath: MenuPath,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args?: any[],
context?: HTMLElement,
showDisabled?: boolean
): Electron.Menu {
const menuModel = this.menuProvider.getMenu(menuPath);
const template = this.fillMenuTemplate([], menuModel, args, {
showDisabled,
context,
rootMenuPath: menuPath,
});
return remote.Menu.buildFromTemplate(this.escapeAmpersand(template));
}
// TODO: remove after https://github.com/eclipse-theia/theia/pull/9231
private escapeAmpersand(
template: Electron.MenuItemConstructorOptions[]
): Electron.MenuItemConstructorOptions[] {
for (const option of template) {
if (option.label) {
option.label = option.label.replace(/\&+/g, '&$&');
}
if (option.submenu) {
this.escapeAmpersand(
option.submenu as Electron.MenuItemConstructorOptions[]
);
}
}
return template;
}
protected override createOSXMenu(): Electron.MenuItemConstructorOptions {
const { submenu } = super.createOSXMenu();
const label = FrontendApplicationConfigProvider.get().applicationName;
if (!!submenu && Array.isArray(submenu)) {
const [, , /* about */ /* preferences */ ...rest] = submenu;
const about = this.fillMenuTemplate(
[],
this.menuProvider.getMenu(ArduinoMenus.HELP__ABOUT_GROUP),
[],
{ rootMenuPath: ArduinoMenus.HELP__ABOUT_GROUP }
);
const preferences = this.fillMenuTemplate(
[],
this.menuProvider.getMenu(ArduinoMenus.FILE__PREFERENCES_GROUP),
[],
{ rootMenuPath: ArduinoMenus.FILE__PREFERENCES_GROUP }
);
const advanced = this.fillMenuTemplate(
[],
this.menuProvider.getMenu(ArduinoMenus.FILE__ADVANCED_GROUP),
[],
{ rootMenuPath: ArduinoMenus.FILE__ADVANCED_GROUP }
);
return {
label,
submenu: [
...about,
{ type: 'separator' },
...preferences,
...advanced,
{ type: 'separator' },
...rest,
],
};
}
return { label, submenu };
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, unused-imports/no-unused-vars
protected override roleFor(id: string): ElectronMenuItemRole | undefined {
// MenuItem `roles` are completely broken on macOS:
// - https://github.com/eclipse-theia/theia/issues/11217,
// - https://github.com/arduino/arduino-ide/issues/969
// IDE2 uses commands instead.
return undefined;
}
protected override fillMenuTemplate(
parentItems: Electron.MenuItemConstructorOptions[],
menuModel: MenuNode,
args: unknown[] | undefined,
options: ElectronMenuOptions
): Electron.MenuItemConstructorOptions[] {
if (menuModel instanceof PlaceholderMenuNode) {
parentItems.push({
label: menuModel.label,
enabled: false,
visible: true,
});
} else {
this.superFillMenuTemplate(parentItems, menuModel, args, options);
}
return parentItems;
}
// Copied from 1.31.1 Theia as is to customize the enablement of the menu items.
// Source: https://github.com/eclipse-theia/theia/blob/5e641750af83383f2ce0cb3432ec333df70778a8/packages/core/src/electron-browser/menu/electron-main-menu-factory.ts#L132-L203
// See https://github.com/arduino/arduino-ide/issues/1533
private superFillMenuTemplate(
parentItems: Electron.MenuItemConstructorOptions[],
menu: MenuNode,
args: unknown[] = [],
options: ElectronMenuOptions
): Electron.MenuItemConstructorOptions[] {
const showDisabled = options?.showDisabled !== false;
if (
CompoundMenuNode.is(menu) &&
this.visibleSubmenu(menu) && // customization for #569 and #655
this.undefinedOrMatch(menu.when, options.context)
) {
const role = CompoundMenuNode.getRole(menu);
if (role === CompoundMenuNodeRole.Group && menu.id === 'inline') {
return parentItems;
}
const children = CompoundMenuNode.getFlatChildren(menu.children);
const myItems: Electron.MenuItemConstructorOptions[] = [];
children.forEach((child) =>
this.fillMenuTemplate(myItems, child, args, options)
);
if (myItems.length === 0) {
// customization for #569 and #655
if (!this.visibleLeafSubmenu(menu)) {
return parentItems;
}
}
if (role === CompoundMenuNodeRole.Submenu) {
parentItems.push({
label: menu.label,
submenu: myItems,
enabled: !this.visibleLeafSubmenu(menu), // customization for #569 and #655
});
} else if (role === CompoundMenuNodeRole.Group && menu.id !== 'inline') {
if (
parentItems.length &&
parentItems[parentItems.length - 1].type !== 'separator'
) {
parentItems.push({ type: 'separator' });
}
parentItems.push(...myItems);
parentItems.push({ type: 'separator' });
}
} else if (menu.command) {
const node =
menu.altNode && this.context.altPressed
? menu.altNode
: (menu as MenuNode & CommandMenuNode);
const commandId = node.command;
// That is only a sanity check at application startup.
if (!this.commandRegistry.getCommand(commandId)) {
console.debug(
`Skipping menu item with missing command: "${commandId}".`
);
return parentItems;
}
if (
!this.menuCommandExecutor.isVisible(
options.rootMenuPath,
commandId,
...args
) ||
!this.undefinedOrMatch(node.when, options.context)
) {
return parentItems;
}
// We should omit rendering context-menu items which are disabled.
if (
!showDisabled &&
!this.menuCommandExecutor.isEnabled(
options.rootMenuPath,
commandId,
...args
)
) {
return parentItems;
}
const bindings =
this.keybindingRegistry.getKeybindingsForCommand(commandId);
const accelerator = bindings[0] && this.acceleratorFor(bindings[0]);
const menuItem: Electron.MenuItemConstructorOptions = {
id: node.id,
label: node.label,
type: this.commandRegistry.getToggledHandler(commandId, ...args)
? 'checkbox'
: 'normal',
checked: this.commandRegistry.isToggled(commandId, ...args),
enabled: this.commandRegistry.isEnabled(commandId, ...args), // Unlike Theia https://github.com/eclipse-theia/theia/blob/v1.31.1/packages/core/src/electron-browser/menu/electron-main-menu-factory.ts#L183
visible: true,
accelerator,
click: () => this.execute(commandId, args, options.rootMenuPath),
};
if (isOSX) {
const role = this.roleFor(node.id);
if (role) {
menuItem.role = role;
delete menuItem.click;
}
}
parentItems.push(menuItem);
if (this.commandRegistry.getToggledHandler(commandId, ...args)) {
this._toggledCommands.add(commandId);
}
}
return parentItems;
}
/**
* `true` if either has at least `children`, or was forced to be visible.
*/
private visibleSubmenu(node: MenuNode & CompoundMenuNode): boolean {
return node.children.length > 0 || this.visibleLeafSubmenu(node);
}
/**
* The node is a visible submenu if is a compound node but has zero children.
*/
private visibleLeafSubmenu(node: MenuNode): boolean {
if (CompoundMenuNode.is(node)) {
return (
node.children.length === 0 &&
AlwaysVisibleSubmenus.findIndex(
(menuPath) => menuPath[menuPath.length - 1] === node.id
) >= 0
);
}
return false;
}
}
const AlwaysVisibleSubmenus: MenuPath[] = [
ArduinoMenus.TOOLS__PORTS_SUBMENU, // #655
ArduinoMenus.FILE__SKETCHBOOK_SUBMENU, // #569
];