Skip to content

Commit

Permalink
Aux window - registration flawed when devtools is opened (fix #209073) (
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero authored Apr 8, 2024
1 parent ed6c6d3 commit 4ee85ac
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/vs/code/electron-main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ export class CodeApplication extends Disposable {
validatedIpcMain.on('vscode:reloadWindow', event => event.sender.reload());

validatedIpcMain.handle('vscode:notifyZoomLevel', async (event, zoomLevel: number | undefined) => {
const window = this.windowsMainService?.getWindowById(event.sender.id);
const window = this.windowsMainService?.getWindowByWebContents(event.sender);
if (window) {
window.notifyZoomLevel(zoomLevel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface IAuxiliaryWindowsMainService {
createWindow(details: HandlerDetails): BrowserWindowConstructorOptions;
registerWindow(webContents: WebContents): void;

getWindowById(windowId: number): IAuxiliaryWindow | undefined;
getWindowByWebContents(webContents: WebContents): IAuxiliaryWindow | undefined;

getFocusedWindow(): IAuxiliaryWindow | undefined;
getLastActiveWindow(): IAuxiliaryWindow | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class AuxiliaryWindowsMainService extends Disposable implements IAuxiliar
private readonly _onDidTriggerSystemContextMenu = this._register(new Emitter<{ window: IAuxiliaryWindow; x: number; y: number }>());
readonly onDidTriggerSystemContextMenu = this._onDidTriggerSystemContextMenu.event;

private readonly windows = new Map<number, AuxiliaryWindow>();
private readonly windows = new Map<number /* webContents ID */, AuxiliaryWindow>();

constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
Expand All @@ -51,7 +51,7 @@ export class AuxiliaryWindowsMainService extends Disposable implements IAuxiliar
// is created.

app.on('browser-window-created', (_event, browserWindow) => {
const auxiliaryWindow = this.getWindowById(browserWindow.id);
const auxiliaryWindow = this.getWindowByWebContents(browserWindow.webContents);
if (auxiliaryWindow) {
this.logService.trace('[aux window] app.on("browser-window-created"): Trying to claim auxiliary window');

Expand All @@ -60,7 +60,7 @@ export class AuxiliaryWindowsMainService extends Disposable implements IAuxiliar
});

validatedIpcMain.handle('vscode:registerAuxiliaryWindow', async (event, mainWindowId: number) => {
const auxiliaryWindow = this.getWindowById(event.sender.id);
const auxiliaryWindow = this.getWindowByWebContents(event.sender);
if (auxiliaryWindow) {
this.logService.trace('[aux window] vscode:registerAuxiliaryWindow: Registering auxiliary window to main window');

Expand Down Expand Up @@ -125,14 +125,14 @@ export class AuxiliaryWindowsMainService extends Disposable implements IAuxiliar
Event.once(auxiliaryWindow.onDidClose)(() => disposables.dispose());
}

getWindowById(windowId: number): AuxiliaryWindow | undefined {
return this.windows.get(windowId);
getWindowByWebContents(webContents: WebContents): AuxiliaryWindow | undefined {
return this.windows.get(webContents.id);
}

getFocusedWindow(): IAuxiliaryWindow | undefined {
const window = BrowserWindow.getFocusedWindow();
if (window) {
return this.getWindowById(window.id);
return this.getWindowByWebContents(window.webContents);
}

return undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/vs/platform/menubar/electron-main/menubar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export class Menubar {
}

const focusedWindow = BrowserWindow.getFocusedWindow();
this.noActiveMainWindow = !focusedWindow || !!this.auxiliaryWindowsMainService.getWindowById(focusedWindow.id);
this.noActiveMainWindow = !focusedWindow || !!this.auxiliaryWindowsMainService.getWindowByWebContents(focusedWindow.webContents);
this.scheduleUpdateMenu();
}

Expand Down Expand Up @@ -768,7 +768,7 @@ export class Menubar {
// actions via the main window.
let activeBrowserWindow = BrowserWindow.getFocusedWindow();
if (activeBrowserWindow) {
const auxiliaryWindowCandidate = this.auxiliaryWindowsMainService.getWindowById(activeBrowserWindow.id);
const auxiliaryWindowCandidate = this.auxiliaryWindowsMainService.getWindowByWebContents(activeBrowserWindow.webContents);
if (auxiliaryWindowCandidate) {
activeBrowserWindow = this.windowsMainService.getWindowById(auxiliaryWindowCandidate.parentId)?.win ?? null;
}
Expand Down
25 changes: 15 additions & 10 deletions src/vs/platform/native/electron-main/nativeHostMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { exec } from 'child_process';
import { app, BrowserWindow, clipboard, Display, Menu, MessageBoxOptions, MessageBoxReturnValue, OpenDevToolsOptions, OpenDialogOptions, OpenDialogReturnValue, powerMonitor, SaveDialogOptions, SaveDialogReturnValue, screen, shell } from 'electron';
import { app, BrowserWindow, clipboard, Display, Menu, MessageBoxOptions, MessageBoxReturnValue, OpenDevToolsOptions, OpenDialogOptions, OpenDialogReturnValue, powerMonitor, SaveDialogOptions, SaveDialogReturnValue, screen, shell, webContents } from 'electron';
import { arch, cpus, freemem, loadavg, platform, release, totalmem, type } from 'os';
import { promisify } from 'util';
import { memoize } from 'vs/base/common/decorators';
Expand Down Expand Up @@ -79,17 +79,17 @@ export class NativeHostMainService extends Disposable implements INativeHostMain
readonly onDidOpenMainWindow = Event.map(this.windowsMainService.onDidOpenWindow, window => window.id);

readonly onDidTriggerWindowSystemContextMenu = Event.any(
Event.filter(Event.map(this.windowsMainService.onDidTriggerSystemContextMenu, ({ window, x, y }) => { return { windowId: window.id, x, y }; }), ({ windowId }) => !!this.windowsMainService.getWindowById(windowId)),
Event.filter(Event.map(this.auxiliaryWindowsMainService.onDidTriggerSystemContextMenu, ({ window, x, y }) => { return { windowId: window.id, x, y }; }), ({ windowId }) => !!this.auxiliaryWindowsMainService.getWindowById(windowId))
Event.map(this.windowsMainService.onDidTriggerSystemContextMenu, ({ window, x, y }) => ({ windowId: window.id, x, y })),
Event.map(this.auxiliaryWindowsMainService.onDidTriggerSystemContextMenu, ({ window, x, y }) => ({ windowId: window.id, x, y }))
);

readonly onDidMaximizeWindow = Event.any(
Event.filter(Event.map(this.windowsMainService.onDidMaximizeWindow, window => window.id), windowId => !!this.windowsMainService.getWindowById(windowId)),
Event.filter(Event.map(this.auxiliaryWindowsMainService.onDidMaximizeWindow, window => window.id), windowId => !!this.auxiliaryWindowsMainService.getWindowById(windowId))
Event.map(this.windowsMainService.onDidMaximizeWindow, window => window.id),
Event.map(this.auxiliaryWindowsMainService.onDidMaximizeWindow, window => window.id)
);
readonly onDidUnmaximizeWindow = Event.any(
Event.filter(Event.map(this.windowsMainService.onDidUnmaximizeWindow, window => window.id), windowId => !!this.windowsMainService.getWindowById(windowId)),
Event.filter(Event.map(this.auxiliaryWindowsMainService.onDidUnmaximizeWindow, window => window.id), windowId => !!this.auxiliaryWindowsMainService.getWindowById(windowId))
Event.map(this.windowsMainService.onDidUnmaximizeWindow, window => window.id),
Event.map(this.auxiliaryWindowsMainService.onDidUnmaximizeWindow, window => window.id)
);

readonly onDidChangeWindowFullScreen = Event.any(
Expand All @@ -105,11 +105,11 @@ export class NativeHostMainService extends Disposable implements INativeHostMain

readonly onDidBlurMainOrAuxiliaryWindow = Event.any(
this.onDidBlurMainWindow,
Event.filter(Event.fromNodeEventEmitter(app, 'browser-window-blur', (event, window: BrowserWindow) => window.id), windowId => !!this.auxiliaryWindowsMainService.getWindowById(windowId))
Event.map(Event.filter(Event.fromNodeEventEmitter(app, 'browser-window-blur', (event, window: BrowserWindow) => this.auxiliaryWindowsMainService.getWindowByWebContents(window.webContents)), window => !!window), window => window!.id)
);
readonly onDidFocusMainOrAuxiliaryWindow = Event.any(
this.onDidFocusMainWindow,
Event.filter(Event.fromNodeEventEmitter(app, 'browser-window-focus', (event, window: BrowserWindow) => window.id), windowId => !!this.auxiliaryWindowsMainService.getWindowById(windowId))
Event.map(Event.filter(Event.fromNodeEventEmitter(app, 'browser-window-focus', (event, window: BrowserWindow) => this.auxiliaryWindowsMainService.getWindowByWebContents(window.webContents)), window => !!window), window => window!.id)
);

readonly onDidResumeOS = Event.fromNodeEventEmitter(powerMonitor, 'resume');
Expand Down Expand Up @@ -836,6 +836,11 @@ export class NativeHostMainService extends Disposable implements INativeHostMain
return undefined;
}

return this.auxiliaryWindowsMainService.getWindowById(windowId);
const contents = webContents.fromId(windowId);
if (!contents) {
return undefined;
}

return this.auxiliaryWindowsMainService.getWindowByWebContents(contents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic

const focusedWindow = BrowserWindow.getFocusedWindow();
if (focusedWindow && focusedWindow.id !== mainWindow.id) {
const auxiliaryWindowCandidate = this.auxiliaryWindowsMainService.getWindowById(focusedWindow.id);
const auxiliaryWindowCandidate = this.auxiliaryWindowsMainService.getWindowByWebContents(focusedWindow.webContents);
if (auxiliaryWindowCandidate && auxiliaryWindowCandidate.parentId === mainWindow.id) {
windowToFocus = auxiliaryWindowCandidate;
}
Expand Down

0 comments on commit 4ee85ac

Please sign in to comment.