@@ -8,7 +8,13 @@ import {
8
8
} from '@theia/core/lib/electron-browser/menu/electron-menu-contribution' ;
9
9
import { MainMenuManager } from '../../../common/main-menu-manager' ;
10
10
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' ;
12
18
13
19
@injectable ( )
14
20
export class ElectronMenuContribution
@@ -37,15 +43,19 @@ export class ElectronMenuContribution
37
43
}
38
44
39
45
update ( ) : void {
40
- ( this as any ) . setMenu ( ) ;
41
46
// if (this.appReady) {
47
+ ( this as any ) . setMenu ( ) ;
42
48
// } else {
43
49
// this.updateWhenReady = true;
44
50
// }
45
51
}
46
52
53
+ override handleTitleBarStyling ( ) : void {
54
+ // NOOP
55
+ }
56
+
47
57
override registerCommands ( registry : CommandRegistry ) : void {
48
- super . registerCommands ( registry ) ;
58
+ this . theiaRegisterCommands ( registry ) ;
49
59
registry . unregisterCommand ( ElectronCommands . CLOSE_WINDOW ) ;
50
60
}
51
61
@@ -60,4 +70,81 @@ export class ElectronMenuContribution
60
70
registry . unregisterKeybinding ( ElectronCommands . ZOOM_IN . id ) ;
61
71
registry . unregisterKeybinding ( ElectronCommands . ZOOM_OUT . id ) ;
62
72
}
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
+ }
63
150
}
0 commit comments