Skip to content

Commit bc3aee6

Browse files
author
Akos Kitta
committed
Do not require the current window on IDE2 start.
It's expensive and sync. Signed-off-by: Akos Kitta <[email protected]>
1 parent 47620ef commit bc3aee6

File tree

1 file changed

+90
-3
lines changed

1 file changed

+90
-3
lines changed

arduino-ide-extension/src/electron-browser/theia/core/electron-menu-contribution.ts

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import {
88
} from '@theia/core/lib/electron-browser/menu/electron-menu-contribution';
99
import { MainMenuManager } from '../../../common/main-menu-manager';
1010
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
11-
import { FrontendApplication } from '@theia/core/lib/browser';
11+
import { FrontendApplication } from '@theia/core/lib/browser/frontend-application';
12+
import { ZoomLevel } from '@theia/core/lib/electron-browser/window/electron-window-preferences';
13+
import { PreferenceScope } from '@theia/core/lib/browser/preferences/preference-scope';
14+
import {
15+
getCurrentWindow,
16+
getCurrentWebContents,
17+
} from '@theia/core/electron-shared/@electron/remote';
1218

1319
@injectable()
1420
export class ElectronMenuContribution
@@ -37,15 +43,19 @@ export class ElectronMenuContribution
3743
}
3844

3945
update(): void {
40-
(this as any).setMenu();
4146
// if (this.appReady) {
47+
(this as any).setMenu();
4248
// } else {
4349
// this.updateWhenReady = true;
4450
// }
4551
}
4652

53+
override handleTitleBarStyling(): void {
54+
// NOOP
55+
}
56+
4757
override registerCommands(registry: CommandRegistry): void {
48-
super.registerCommands(registry);
58+
this.theiaRegisterCommands(registry);
4959
registry.unregisterCommand(ElectronCommands.CLOSE_WINDOW);
5060
}
5161

@@ -60,4 +70,81 @@ export class ElectronMenuContribution
6070
registry.unregisterKeybinding(ElectronCommands.ZOOM_IN.id);
6171
registry.unregisterKeybinding(ElectronCommands.ZOOM_OUT.id);
6272
}
73+
74+
// Copied from Theia: https://github.com/eclipse-theia/theia/blob/9ec8835cf35d5a46101a62ae93285aeb37a2f382/packages/core/src/electron-browser/menu/electron-menu-contribution.ts#L260-L314
75+
// Unlike the Theia implementation, this does not require synchronously the browser window, but use a function only when the command handler executes.
76+
private theiaRegisterCommands(registry: CommandRegistry): void {
77+
const currentWindow = () => getCurrentWindow();
78+
79+
registry.registerCommand(ElectronCommands.TOGGLE_DEVELOPER_TOOLS, {
80+
execute: () => {
81+
const webContent = getCurrentWebContents();
82+
if (!webContent.isDevToolsOpened()) {
83+
webContent.openDevTools();
84+
} else {
85+
webContent.closeDevTools();
86+
}
87+
},
88+
});
89+
90+
registry.registerCommand(ElectronCommands.RELOAD, {
91+
execute: () => this.windowService.reload(),
92+
});
93+
registry.registerCommand(ElectronCommands.CLOSE_WINDOW, {
94+
execute: () => currentWindow().close(),
95+
});
96+
97+
registry.registerCommand(ElectronCommands.ZOOM_IN, {
98+
execute: () => {
99+
const webContents = currentWindow().webContents;
100+
// When starting at a level that is not a multiple of 0.5, increment by at most 0.5 to reach the next highest multiple of 0.5.
101+
let zoomLevel =
102+
Math.floor(webContents.zoomLevel / ZoomLevel.VARIATION) *
103+
ZoomLevel.VARIATION +
104+
ZoomLevel.VARIATION;
105+
if (zoomLevel > ZoomLevel.MAX) {
106+
zoomLevel = ZoomLevel.MAX;
107+
return;
108+
}
109+
this.preferenceService.set(
110+
'window.zoomLevel',
111+
zoomLevel,
112+
PreferenceScope.User
113+
);
114+
},
115+
});
116+
registry.registerCommand(ElectronCommands.ZOOM_OUT, {
117+
execute: () => {
118+
const webContents = currentWindow().webContents;
119+
// When starting at a level that is not a multiple of 0.5, decrement by at most 0.5 to reach the next lowest multiple of 0.5.
120+
let zoomLevel =
121+
Math.ceil(webContents.zoomLevel / ZoomLevel.VARIATION) *
122+
ZoomLevel.VARIATION -
123+
ZoomLevel.VARIATION;
124+
if (zoomLevel < ZoomLevel.MIN) {
125+
zoomLevel = ZoomLevel.MIN;
126+
return;
127+
}
128+
this.preferenceService.set(
129+
'window.zoomLevel',
130+
zoomLevel,
131+
PreferenceScope.User
132+
);
133+
},
134+
});
135+
registry.registerCommand(ElectronCommands.RESET_ZOOM, {
136+
execute: () =>
137+
this.preferenceService.set(
138+
'window.zoomLevel',
139+
ZoomLevel.DEFAULT,
140+
PreferenceScope.User
141+
),
142+
});
143+
registry.registerCommand(ElectronCommands.TOGGLE_FULL_SCREEN, {
144+
isEnabled: () => currentWindow().isFullScreenable(),
145+
isVisible: () => currentWindow().isFullScreenable(),
146+
execute: () =>
147+
currentWindow().setFullScreen(!currentWindow().isFullScreen()),
148+
});
149+
}
63150
}

0 commit comments

Comments
 (0)