desktopHostService.ts 3.8 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { Event } from 'vs/base/common/event';
7 8 9
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { IElectronService } from 'vs/platform/electron/node/electron';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
10 11
import { ILabelService } from 'vs/platform/label/common/label';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
12
import { IWindowOpenable, IOpenWindowOptions, isFolderToOpen, isWorkspaceToOpen, IOpenEmptyWindowOptions } from 'vs/platform/windows/common/windows';
13
import { Disposable } from 'vs/base/common/lifecycle';
14
import { IElectronEnvironmentService } from 'vs/workbench/services/electron/electron-browser/electronEnvironmentService';
15

16
export class DesktopHostService extends Disposable implements IHostService {
17 18 19

	_serviceBrand: undefined;

20 21 22
	constructor(
		@IElectronService private readonly electronService: IElectronService,
		@ILabelService private readonly labelService: ILabelService,
23 24
		@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
		@IElectronEnvironmentService private readonly electronEnvironmentService: IElectronEnvironmentService
25 26 27
	) {
		super();
	}
28

29 30
	get onDidChangeFocus(): Event<boolean> { return this._onDidChangeFocus; }
	private _onDidChangeFocus: Event<boolean> = Event.any(
B
Benjamin Pasero 已提交
31 32
		Event.map(Event.filter(this.electronService.onWindowFocus, id => id === this.electronEnvironmentService.windowId), () => this.hasFocus),
		Event.map(Event.filter(this.electronService.onWindowBlur, id => id === this.electronEnvironmentService.windowId), () => this.hasFocus)
33
	);
34

B
Benjamin Pasero 已提交
35 36 37
	get hasFocus(): boolean {
		return document.hasFocus();
	}
38

J
João Moreno 已提交
39 40 41 42 43 44 45 46 47 48
	async hadLastFocus(): Promise<boolean> {
		const activeWindowId = await this.electronService.getActiveWindowId();

		if (typeof activeWindowId === 'undefined') {
			return false;
		}

		return activeWindowId === this.electronEnvironmentService.windowId;
	}

49 50 51 52 53 54 55 56 57
	openWindow(options?: IOpenEmptyWindowOptions): Promise<void>;
	openWindow(toOpen: IWindowOpenable[], options?: IOpenWindowOptions): Promise<void>;
	openWindow(arg1?: IOpenEmptyWindowOptions | IWindowOpenable[], arg2?: IOpenWindowOptions): Promise<void> {
		if (Array.isArray(arg1)) {
			return this.doOpenWindow(arg1, arg2);
		}

		return this.doOpenEmptyWindow(arg1);
	}
58

59
	private doOpenWindow(toOpen: IWindowOpenable[], options?: IOpenWindowOptions): Promise<void> {
60 61 62 63
		if (!!this.environmentService.configuration.remoteAuthority) {
			toOpen.forEach(openable => openable.label = openable.label || this.getRecentLabel(openable));
		}

64
		return this.electronService.openWindow(toOpen, options);
65 66 67 68 69 70 71 72 73 74 75 76 77
	}

	private getRecentLabel(openable: IWindowOpenable): string {
		if (isFolderToOpen(openable)) {
			return this.labelService.getWorkspaceLabel(openable.folderUri, { verbose: true });
		}

		if (isWorkspaceToOpen(openable)) {
			return this.labelService.getWorkspaceLabel({ id: '', configPath: openable.workspaceUri }, { verbose: true });
		}

		return this.labelService.getUriLabel(openable.fileUri);
	}
78

79 80
	private doOpenEmptyWindow(options?: IOpenEmptyWindowOptions): Promise<void> {
		return this.electronService.openWindow(options);
81 82
	}

83 84 85 86
	toggleFullScreen(): Promise<void> {
		return this.electronService.toggleFullScreen();
	}

87 88 89 90
	focus(): Promise<void> {
		return this.electronService.focusWindow();
	}

91 92 93
	restart(): Promise<void> {
		return this.electronService.relaunch();
	}
94 95 96 97

	reload(): Promise<void> {
		return this.electronService.reload();
	}
98 99 100
}

registerSingleton(IHostService, DesktopHostService, true);