Skip to content

Commit

Permalink
Update the handle if the user uses aux windows (#234394)
Browse files Browse the repository at this point in the history
that way the broker always shows on top of the current focused window.
  • Loading branch information
TylerLeonhardt authored Nov 22, 2024
1 parent 685b14c commit 12f1491
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/vs/platform/native/common/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface ICommonNativeHostService {
getWindowCount(): Promise<number>;
getActiveWindowId(): Promise<number | undefined>;
getActiveWindowPosition(): Promise<IRectangle | undefined>;
getNativeWindowHandle(windowId: number): Promise<VSBuffer | undefined>;

openWindow(options?: IOpenEmptyWindowOptions): Promise<void>;
openWindow(toOpen: IWindowOpenable[], options?: IOpenWindowOptions): Promise<void>;
Expand Down
8 changes: 8 additions & 0 deletions src/vs/platform/native/electron-main/nativeHostMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ export class NativeHostMainService extends Disposable implements INativeHostMain
return undefined;
}

async getNativeWindowHandle(fallbackWindowId: number | undefined, windowId: number): Promise<VSBuffer | undefined> {
const window = this.windowById(windowId, fallbackWindowId);
if (window?.win) {
return VSBuffer.wrap(window.win.getNativeWindowHandle());
}
return undefined;
}

openWindow(windowId: number | undefined, options?: IOpenEmptyWindowOptions): Promise<void>;
openWindow(windowId: number | undefined, toOpen: IWindowOpenable[], options?: IOpenWindowOptions): Promise<void>;
openWindow(windowId: number | undefined, arg1?: IOpenEmptyWindowOptions | IWindowOpenable[], arg2?: IOpenWindowOptions): Promise<void> {
Expand Down
13 changes: 13 additions & 0 deletions src/vs/workbench/api/browser/mainThreadWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { extHostNamedCustomer, IExtHostContext } from '../../services/extensions
import { ExtHostContext, ExtHostWindowShape, IOpenUriOptions, MainContext, MainThreadWindowShape } from '../common/extHost.protocol.js';
import { IHostService } from '../../services/host/browser/host.js';
import { IUserActivityService } from '../../services/userActivity/common/userActivityService.js';
import { encodeBase64 } from '../../../base/common/buffer.js';

@extHostNamedCustomer(MainContext.MainThreadWindow)
export class MainThreadWindow implements MainThreadWindowShape {
Expand All @@ -29,12 +30,24 @@ export class MainThreadWindow implements MainThreadWindowShape {
Event.latch(hostService.onDidChangeFocus)
(this.proxy.$onDidChangeWindowFocus, this.proxy, this.disposables);
userActivityService.onDidChangeIsActive(this.proxy.$onDidChangeWindowActive, this.proxy, this.disposables);
this.registerNativeHandle();
}

dispose(): void {
this.disposables.dispose();
}

registerNativeHandle(): void {
Event.latch(this.hostService.onDidChangeActiveWindow)(
async windowId => {
const handle = await this.hostService.getNativeWindowHandle(windowId);
this.proxy.$onDidChangeActiveNativeWindowHandle(handle ? encodeBase64(handle) : undefined);
},
this,
this.disposables
);
}

$getInitialState() {
return Promise.resolve({
isFocused: this.hostService.hasFocus,
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,7 @@ export interface ExtHostDecorationsShape {
export interface ExtHostWindowShape {
$onDidChangeWindowFocus(value: boolean): void;
$onDidChangeWindowActive(value: boolean): void;
$onDidChangeActiveNativeWindowHandle(handle: string | undefined): void;
}

export interface ExtHostLogLevelServiceShape {
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/api/common/extHostWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class ExtHostWindow implements ExtHostWindowShape {
return this._nativeHandle;
}

$onDidChangeActiveNativeWindowHandle(handle: string | undefined): void {
this._nativeHandle = handle ? decodeBase64(handle).buffer : undefined;
}

$onDidChangeWindowFocus(value: boolean) {
this.onDidChangeWindowProperty('focused', value);
}
Expand Down
7 changes: 7 additions & 0 deletions src/vs/workbench/services/host/browser/browserHostService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@ export class BrowserHostService extends Disposable implements IHostService {

//#endregion

//#region Native Handle

async getNativeWindowHandle(_windowId: number) {
return undefined;
}

//#endregion
}

registerSingleton(IHostService, BrowserHostService, InstantiationType.Delayed);
10 changes: 10 additions & 0 deletions src/vs/workbench/services/host/browser/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { VSBuffer } from '../../../../base/common/buffer.js';
import { Event } from '../../../../base/common/event.js';
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
import { IWindowOpenable, IOpenWindowOptions, IOpenEmptyWindowOptions, IPoint, IRectangle } from '../../../../platform/window/common/window.js';
Expand Down Expand Up @@ -130,4 +131,13 @@ export interface IHostService {
getScreenshot(): Promise<ArrayBufferLike | undefined>;

//#endregion

//#region Native Handle

/**
* Get the native handle of the window.
*/
getNativeWindowHandle(windowId: number): Promise<VSBuffer | undefined>;

//#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { IMainProcessService } from '../../../../platform/ipc/common/mainProcess
import { disposableWindowInterval, getActiveDocument, getWindowId, getWindowsCount, hasWindow, onDidRegisterWindow } from '../../../../base/browser/dom.js';
import { memoize } from '../../../../base/common/decorators.js';
import { isAuxiliaryWindow } from '../../../../base/browser/window.js';
import { VSBuffer } from '../../../../base/common/buffer.js';

class WorkbenchNativeHostService extends NativeHostService {

Expand Down Expand Up @@ -193,6 +194,18 @@ class WorkbenchHostService extends Disposable implements IHostService {
}

//#endregion

//#region Native Handle

private _nativeWindowHandleCache = new Map<number, Promise<VSBuffer | undefined>>();
async getNativeWindowHandle(windowId: number): Promise<VSBuffer | undefined> {
if (!this._nativeWindowHandleCache.has(windowId)) {
this._nativeWindowHandleCache.set(windowId, this.nativeHostService.getNativeWindowHandle(windowId));
}
return this._nativeWindowHandleCache.get(windowId)!;
}

//#endregion
}

registerSingleton(IHostService, WorkbenchHostService, InstantiationType.Delayed);
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/test/browser/workbenchTestServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,8 @@ export class TestHostService implements IHostService {

async getScreenshot(): Promise<ArrayBufferLike | undefined> { return undefined; }

async getNativeWindowHandle(_windowId: number): Promise<VSBuffer | undefined> { return undefined; }

readonly colorScheme = ColorScheme.DARK;
onDidChangeColorScheme = Event.None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class TestNativeHostService implements INativeHostService {
async getWindows(): Promise<IOpenedMainWindow[]> { return []; }
async getActiveWindowId(): Promise<number | undefined> { return undefined; }
async getActiveWindowPosition(): Promise<IRectangle | undefined> { return undefined; }
async getNativeWindowHandle(windowId: number): Promise<VSBuffer | undefined> { return undefined; }

openWindow(options?: IOpenEmptyWindowOptions): Promise<void>;
openWindow(toOpen: IWindowOpenable[], options?: IOpenWindowOptions): Promise<void>;
Expand Down

0 comments on commit 12f1491

Please sign in to comment.