-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathelectron-main-menu-factory.ts
377 lines (353 loc) · 11.9 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { ContextMatcher } from '@theia/core/lib/browser/context-key-service';
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,
MAIN_MENU_BAR,
MenuNode,
MenuPath,
} from '@theia/core/lib/common/menu';
import { isOSX } from '@theia/core/lib/common/os';
import {
ElectronMenuOptions,
ElectronMainMenuFactory as TheiaElectronMainMenuFactory,
} from '@theia/core/lib/electron-browser/menu/electron-main-menu-factory';
import type {
MenuDto,
MenuRole,
} from '@theia/core/lib/electron-common/electron-api';
import { inject, injectable } from '@theia/core/shared/inversify';
import {
ArduinoMenus,
PlaceholderMenuNode,
} from '../../../browser/menu/arduino-menus';
import debounce from 'lodash.debounce';
@injectable()
export class ElectronMainMenuFactory extends TheiaElectronMainMenuFactory {
@inject(FrontendApplicationStateService)
private readonly appStateService: FrontendApplicationStateService;
private appReady = false;
private updateWhenReady = false;
override postConstruct(): void {
// #region Theia `postConstruct` customizations with calling IDE2 `setMenu`
this.preferencesService.onPreferenceChanged(
debounce((e) => {
if (e.preferenceName === 'window.menuBarVisibility') {
this.setMenuBar();
}
if (this._menu) {
for (const cmd of this._toggledCommands) {
const menuItem = this.findMenuById(this._menu, cmd);
if (menuItem) {
menuItem.checked = this.commandRegistry.isToggled(cmd);
}
}
window.electronArduino.setMenu(this._menu); // calls the IDE2-specific implementation
}
}, 10)
);
this.keybindingRegistry.onKeybindingsChanged(() => {
this.setMenuBar();
});
// #endregion Theia `postConstruct`
this.appStateService.reachedState('ready').then(() => {
this.appReady = true;
if (this.updateWhenReady) {
this.setMenuBar();
}
});
}
override createElectronMenuBar(): MenuDto[] {
this._toggledCommands.clear(); // https://github.com/eclipse-theia/theia/issues/8977
const menuModel = this.menuProvider.getMenu(MAIN_MENU_BAR);
const menu = this.fillMenuTemplate([], menuModel, [], {
rootMenuPath: MAIN_MENU_BAR,
});
if (isOSX) {
menu.unshift(this.createOSXMenu());
}
const escapedMenu = this.escapeAmpersand(menu);
this._menu = escapedMenu;
return escapedMenu;
}
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;
}
await this.preferencesService.ready;
const createdMenuBar = this.createElectronMenuBar();
window.electronArduino.setMenu(createdMenuBar);
}
override createElectronContextMenu(
menuPath: MenuPath,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args?: any[],
context?: HTMLElement,
contextKeyService?: ContextMatcher,
showDisabled?: boolean
): MenuDto[] {
const menuModel = this.menuProvider.getMenu(menuPath);
return this.fillMenuTemplate([], menuModel, args, {
showDisabled,
context,
rootMenuPath: menuPath,
contextKeyService,
});
}
protected override async execute(
commandId: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any[],
menuPath: MenuPath
): Promise<void> {
try {
// This is workaround for https://github.com/eclipse-theia/theia/issues/446.
// Electron menus do not update based on the `isEnabled`, `isVisible` property of the command.
// We need to check if we can execute it.
if (this.menuCommandExecutor.isEnabled(menuPath, commandId, ...args)) {
await this.menuCommandExecutor.executeCommand(
menuPath,
commandId,
...args
);
if (
this._menu &&
this.menuCommandExecutor.isVisible(menuPath, commandId, ...args)
) {
const item = this.findMenuById(this._menu, commandId);
if (item) {
item.checked = this.menuCommandExecutor.isToggled(
menuPath,
commandId,
...args
);
window.electronArduino.setMenu(this._menu); // overridden to call the IDE2-specific implementation.
}
}
}
} catch {
// no-op
}
}
// TODO: remove after https://github.com/eclipse-theia/theia/pull/9231
private escapeAmpersand(template: MenuDto[]): MenuDto[] {
for (const option of template) {
if (option.label) {
option.label = option.label.replace(/\&+/g, '&$&');
}
if (option.submenu) {
this.escapeAmpersand(option.submenu);
}
}
return template;
}
protected override createOSXMenu(): MenuDto {
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): MenuRole | 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: MenuDto[],
menuModel: MenuNode,
args: unknown[] | undefined,
options: ElectronMenuOptions
): MenuDto[] {
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: MenuDto[],
menu: MenuNode,
args: unknown[] = [],
options: ElectronMenuOptions
): MenuDto[] {
const showDisabled = options?.showDisabled !== false;
if (
CompoundMenuNode.is(menu) &&
this.visibleSubmenu(menu) && // customization for #569 and #655
this.undefinedOrMatch(
options.contextKeyService ?? this.contextKeyService,
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: MenuDto[] = [];
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(
options.contextKeyService ?? this.contextKeyService,
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: MenuDto = {
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,
execute: () => this.execute(commandId, args, options.rootMenuPath),
};
if (isOSX) {
const role = this.roleFor(node.id);
if (role) {
menuItem.role = role;
delete menuItem.execute;
}
}
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
];