Skip to content

Commit 29cbf57

Browse files
authored
aux window - disable main menu (microsoft#196697)
* aux window - disable main menu * 💄
1 parent 9eeb60b commit 29cbf57

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

src/vs/platform/auxiliaryWindow/electron-main/auxiliaryWindow.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ export class AuxiliaryWindow extends BaseWindow implements IAuxiliaryWindow {
3030
private _win: BrowserWindow | null = null;
3131
get win() {
3232
if (!this._win) {
33-
const window = BrowserWindow.fromWebContents(this.contents);
34-
if (window) {
35-
this._win = window;
36-
this.registerWindowListeners(window);
37-
}
33+
this.tryClaimWindow();
3834
}
3935

4036
return this._win;
@@ -62,6 +58,30 @@ export class AuxiliaryWindow extends BaseWindow implements IAuxiliaryWindow {
6258
if (this.environmentMainService.args['open-devtools'] === true) {
6359
this.contents.openDevTools({ mode: 'bottom' });
6460
}
61+
62+
// Try to claim now
63+
this.tryClaimWindow();
64+
}
65+
66+
tryClaimWindow(): void {
67+
if (this._win) {
68+
return; // already claimed
69+
}
70+
71+
if (this._store.isDisposed || this.contents.isDestroyed()) {
72+
return; // already disposed
73+
}
74+
75+
const window = BrowserWindow.fromWebContents(this.contents);
76+
if (window) {
77+
this._win = window;
78+
79+
// Disable Menu
80+
window.setMenu(null);
81+
82+
// Listeners
83+
this.registerWindowListeners(window);
84+
}
6585
}
6686

6787
private registerWindowListeners(window: BrowserWindow): void {

src/vs/platform/auxiliaryWindow/electron-main/auxiliaryWindows.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ export interface IAuxiliaryWindowsMainService {
2020

2121
getFocusedWindow(): IAuxiliaryWindow | undefined;
2222
getLastActiveWindow(): IAuxiliaryWindow | undefined;
23+
24+
getWindows(): readonly IAuxiliaryWindow[];
2325
}

src/vs/platform/auxiliaryWindow/electron-main/auxiliaryWindowsMainService.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { BrowserWindow, BrowserWindowConstructorOptions, WebContents } from 'electron';
6+
import { BrowserWindow, BrowserWindowConstructorOptions, WebContents, app } from 'electron';
77
import { Event } from 'vs/base/common/event';
88
import { FileAccess } from 'vs/base/common/network';
99
import { AuxiliaryWindow, IAuxiliaryWindow } from 'vs/platform/auxiliaryWindow/electron-main/auxiliaryWindow';
@@ -15,11 +15,29 @@ export class AuxiliaryWindowsMainService implements IAuxiliaryWindowsMainService
1515

1616
declare readonly _serviceBrand: undefined;
1717

18-
private readonly windows = new Map<number, IAuxiliaryWindow>();
18+
private readonly windows = new Map<number, AuxiliaryWindow>();
1919

2020
constructor(
2121
@IInstantiationService private readonly instantiationService: IInstantiationService
22-
) { }
22+
) {
23+
this.registerListeners();
24+
}
25+
26+
private registerListeners(): void {
27+
28+
// We have to ensure that an auxiliary window gets to know its
29+
// parent `BrowserWindow` so that it can apply listeners to it
30+
// Unfortunately we cannot rely on static `BrowserWindow` methods
31+
// because we might call the methods too early before the window
32+
// is created.
33+
34+
app.on('browser-window-created', (_event, browserWindow) => {
35+
const auxiliaryWindow = this.getWindowById(browserWindow.id);
36+
if (auxiliaryWindow) {
37+
auxiliaryWindow.tryClaimWindow();
38+
}
39+
});
40+
}
2341

2442
createWindow(): BrowserWindowConstructorOptions {
2543
return this.instantiationService.invokeFunction(defaultBrowserWindowOptions, undefined, {
@@ -36,7 +54,7 @@ export class AuxiliaryWindowsMainService implements IAuxiliaryWindowsMainService
3654
Event.once(auxiliaryWindow.onDidClose)(() => this.windows.delete(auxiliaryWindow.id));
3755
}
3856

39-
getWindowById(windowId: number): IAuxiliaryWindow | undefined {
57+
getWindowById(windowId: number): AuxiliaryWindow | undefined {
4058
return this.windows.get(windowId);
4159
}
4260

@@ -52,4 +70,8 @@ export class AuxiliaryWindowsMainService implements IAuxiliaryWindowsMainService
5270
getLastActiveWindow(): IAuxiliaryWindow | undefined {
5371
return getLastFocused(Array.from(this.windows.values()));
5472
}
73+
74+
getWindows(): readonly IAuxiliaryWindow[] {
75+
return Array.from(this.windows.values());
76+
}
5577
}

src/vs/platform/menubar/electron-main/menubar.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { mnemonicMenuLabel } from 'vs/base/common/labels';
1111
import { isMacintosh, language } from 'vs/base/common/platform';
1212
import { URI } from 'vs/base/common/uri';
1313
import * as nls from 'vs/nls';
14+
import { IAuxiliaryWindowsMainService } from 'vs/platform/auxiliaryWindow/electron-main/auxiliaryWindows';
1415
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
1516
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
1617
import { ILifecycleMainService } from 'vs/platform/lifecycle/electron-main/lifecycleMainService';
@@ -74,7 +75,8 @@ export class Menubar {
7475
@ILifecycleMainService private readonly lifecycleMainService: ILifecycleMainService,
7576
@ILogService private readonly logService: ILogService,
7677
@INativeHostMainService private readonly nativeHostMainService: INativeHostMainService,
77-
@IProductService private readonly productService: IProductService
78+
@IProductService private readonly productService: IProductService,
79+
@IAuxiliaryWindowsMainService private readonly auxiliaryWindowsMainService: IAuxiliaryWindowsMainService
7880
) {
7981
this.menuUpdater = new RunOnceScheduler(() => this.doUpdateMenu(), 0);
8082

@@ -255,7 +257,7 @@ export class Menubar {
255257
// If we don't have a menu yet, set it to null to avoid the electron menu.
256258
// This should only happen on the first launch ever
257259
if (Object.keys(this.menubarMenus).length === 0) {
258-
Menu.setApplicationMenu(isMacintosh ? new Menu() : null);
260+
this.doSetApplicationMenu(isMacintosh ? new Menu() : null);
259261
return;
260262
}
261263

@@ -358,15 +360,28 @@ export class Menubar {
358360
}
359361

360362
if (menubar.items && menubar.items.length > 0) {
361-
Menu.setApplicationMenu(menubar);
363+
this.doSetApplicationMenu(menubar);
362364
} else {
363-
Menu.setApplicationMenu(null);
365+
this.doSetApplicationMenu(null);
364366
}
365367

366368
// Dispose of older menus after some time
367369
this.menuGC.schedule();
368370
}
369371

372+
private doSetApplicationMenu(menu: (Menu) | (null)): void {
373+
374+
// Setting the application menu sets it to all opened windows,
375+
// but we currently do not support a menu in auxiliary windows,
376+
// so we need to unset it there.
377+
378+
Menu.setApplicationMenu(menu);
379+
380+
for (const window of this.auxiliaryWindowsMainService.getWindows()) {
381+
window.win?.setMenu(null);
382+
}
383+
}
384+
370385
private setMacApplicationMenu(macApplicationMenu: Menu): void {
371386
const about = this.createMenuItem(nls.localize('mAbout', "About {0}", this.productService.nameLong), 'workbench.action.showAboutDialog');
372387
const checkForUpdates = this.getUpdateMenuItems();

0 commit comments

Comments
 (0)