Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of changing handle #234303

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
getActiveWindowNativeHandle(id: number): Promise<string | undefined>;

openWindow(options?: IOpenEmptyWindowOptions): Promise<void>;
openWindow(toOpen: IWindowOpenable[], options?: IOpenWindowOptions): Promise<void>;
Expand Down
4 changes: 4 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,10 @@ export class NativeHostMainService extends Disposable implements INativeHostMain
return undefined;
}

async getActiveWindowNativeHandle(windowId: number | undefined, id: number): Promise<string | undefined> {
return this.windowById(id, windowId)?.win?.getNativeWindowHandle().toString('base64');
}

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
17 changes: 14 additions & 3 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 { INativeHostService } from '../../../platform/native/common/native.js';

@extHostNamedCustomer(MainContext.MainThreadWindow)
export class MainThreadWindow implements MainThreadWindowShape {
Expand All @@ -21,6 +22,7 @@ export class MainThreadWindow implements MainThreadWindowShape {
constructor(
extHostContext: IExtHostContext,
@IHostService private readonly hostService: IHostService,
@INativeHostService private readonly nativeHostService: INativeHostService,
@IOpenerService private readonly openerService: IOpenerService,
@IUserActivityService private readonly userActivityService: IUserActivityService,
) {
Expand All @@ -29,17 +31,26 @@ 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.disposables.add(hostService.onDidChangeActiveWindow((id) => this.onDidChangeActiveWindow(id)));
}

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

$getInitialState() {
return Promise.resolve({
private onDidChangeActiveWindow(id: number): void {
this.nativeHostService.getActiveWindowNativeHandle(id).then(nativeHandle => {
this.proxy.$onDidChangeActiveWindowHandle(nativeHandle);
});
}

async $getInitialState() {
const nativeHandle = await this.nativeHostService.getActiveWindowNativeHandle(this.nativeHostService.windowId);
return {
isFocused: this.hostService.hasFocus,
isActive: this.userActivityService.isActive,
});
nativeHandle
};
}

async $openUri(uriComponents: UriComponents, uriString: string | undefined, options: IOpenUriOptions): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
},
get handle(): string | undefined {
checkProposedApiEnabled(extension, 'nativeWindowHandle');
return initData.handle;
return extHostWindow.nativeHandle;
}
};
if (!initData.environment.extensionTestsLocationURI) {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ export interface IOpenUriOptions {
}

export interface MainThreadWindowShape extends IDisposable {
$getInitialState(): Promise<{ isFocused: boolean; isActive: boolean }>;
$getInitialState(): Promise<{ isFocused: boolean; isActive: boolean; nativeHandle?: string }>;
$openUri(uri: UriComponents, uriString: string | undefined, options: IOpenUriOptions): Promise<boolean>;
$asExternalUri(uri: UriComponents, options: IOpenUriOptions): Promise<UriComponents>;
}
Expand Down Expand Up @@ -2596,6 +2596,7 @@ export interface ExtHostDecorationsShape {
export interface ExtHostWindowShape {
$onDidChangeWindowFocus(value: boolean): void;
$onDidChangeWindowActive(value: boolean): void;
$onDidChangeActiveWindowHandle(handle: string | undefined): void;
}

export interface ExtHostLogLevelServiceShape {
Expand Down
18 changes: 16 additions & 2 deletions src/vs/workbench/api/common/extHostWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createDecorator } from '../../../platform/instantiation/common/instanti
import { IExtHostRpcService } from './extHostRpcService.js';
import { WindowState } from 'vscode';
import { ExtHostWindowShape, IOpenUriOptions, MainContext, MainThreadWindowShape } from './extHost.protocol.js';
import { IExtHostInitDataService } from './extHostInitDataService.js';

export class ExtHostWindow implements ExtHostWindowShape {

Expand All @@ -24,6 +25,7 @@ export class ExtHostWindow implements ExtHostWindowShape {
private readonly _onDidChangeWindowState = new Emitter<WindowState>();
readonly onDidChangeWindowState: Event<WindowState> = this._onDidChangeWindowState.event;

private _nativeHandle: string | undefined = this._initData.handle;
private _state = ExtHostWindow.InitialState;

getState(): WindowState {
Expand All @@ -40,14 +42,26 @@ export class ExtHostWindow implements ExtHostWindowShape {
};
}

constructor(@IExtHostRpcService extHostRpc: IExtHostRpcService) {
constructor(
@IExtHostInitDataService private readonly _initData: IExtHostInitDataService,
@IExtHostRpcService extHostRpc: IExtHostRpcService
) {
this._proxy = extHostRpc.getProxy(MainContext.MainThreadWindow);
this._proxy.$getInitialState().then(({ isFocused, isActive }) => {
this._proxy.$getInitialState().then(({ isFocused, isActive, nativeHandle }) => {
this.onDidChangeWindowProperty('focused', isFocused);
this.onDidChangeWindowProperty('active', isActive);
this.$onDidChangeActiveWindowHandle(nativeHandle);
});
}

get nativeHandle(): string | undefined {
return this._nativeHandle;
}

$onDidChangeActiveWindowHandle(handle: string | undefined): void {
this._nativeHandle = handle;
}

$onDidChangeWindowFocus(value: boolean) {
this.onDidChangeWindowProperty('focused', value);
}
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 getActiveWindowNativeHandle(): Promise<string | undefined> { return undefined; }

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