browserHostService.ts 6.1 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
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
9
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
10 11
import { IResourceEditor, IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
12
import { IWindowSettings, IWindowOpenable, IOpenInWindowOptions, isFolderToOpen, isWorkspaceToOpen, isFileToOpen, IOpenEmptyWindowOptions } from 'vs/platform/windows/common/windows';
13 14 15
import { pathsToEditors } from 'vs/workbench/common/editor';
import { IFileService } from 'vs/platform/files/common/files';
import { ILabelService } from 'vs/platform/label/common/label';
16 17
import { trackFocus } from 'vs/base/browser/dom';
import { Disposable } from 'vs/base/common/lifecycle';
18

19
export class BrowserHostService extends Disposable implements IHostService {
20 21 22

	_serviceBrand: undefined;

23 24 25 26 27 28 29
	//#region Events

	get onDidChangeFocus(): Event<boolean> { return this._onDidChangeFocus; }
	private _onDidChangeFocus: Event<boolean>;

	//#endregion

30 31 32 33 34 35
	constructor(
		@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,
		@IEditorService private readonly editorService: IEditorService,
		@IConfigurationService private readonly configurationService: IConfigurationService,
		@IFileService private readonly fileService: IFileService,
		@ILabelService private readonly labelService: ILabelService
36 37 38
	) {
		super();

B
Benjamin Pasero 已提交
39 40 41 42 43 44
		this.registerListeners();
	}

	private registerListeners(): void {

		// Track Focus on Window
45 46 47 48 49 50
		const focusTracker = this._register(trackFocus(window));
		this._onDidChangeFocus = Event.any(
			Event.map(focusTracker.onDidFocus, () => this.hasFocus),
			Event.map(focusTracker.onDidBlur, () => this.hasFocus)
		);
	}
51

52 53 54 55
	//#region Window

	readonly windowCount = Promise.resolve(1);

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	async openInWindow(toOpen: IWindowOpenable[], options?: IOpenInWindowOptions): Promise<void> {
		// TODO@Ben delegate to embedder
		const { openFolderInNewWindow } = this.shouldOpenNewWindow(options);
		for (let i = 0; i < toOpen.length; i++) {
			const openable = toOpen[i];
			openable.label = openable.label || this.getRecentLabel(openable);

			// Folder
			if (isFolderToOpen(openable)) {
				const newAddress = `${document.location.origin}${document.location.pathname}?folder=${openable.folderUri.path}`;
				if (openFolderInNewWindow) {
					window.open(newAddress);
				} else {
					window.location.href = newAddress;
				}
			}

			// Workspace
			else if (isWorkspaceToOpen(openable)) {
				const newAddress = `${document.location.origin}${document.location.pathname}?workspace=${openable.workspaceUri.path}`;
				if (openFolderInNewWindow) {
					window.open(newAddress);
				} else {
					window.location.href = newAddress;
				}
			}

			// File
			else if (isFileToOpen(openable)) {
				const inputs: IResourceEditor[] = await pathsToEditors([openable], this.fileService);
				this.editorService.openEditors(inputs);
			}
		}
	}

	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);
	}

	private shouldOpenNewWindow(options: IOpenInWindowOptions = {}): { openFolderInNewWindow: boolean } {
		const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
		const openFolderInNewWindowConfig = (windowConfig && windowConfig.openFoldersInNewWindow) || 'default' /* default */;

		let openFolderInNewWindow = !!options.forceNewWindow && !options.forceReuseWindow;
		if (!options.forceNewWindow && !options.forceReuseWindow && (openFolderInNewWindowConfig === 'on' || openFolderInNewWindowConfig === 'off')) {
			openFolderInNewWindow = (openFolderInNewWindowConfig === 'on');
		}

		return { openFolderInNewWindow };
	}

115
	async openEmptyWindow(options?: IOpenEmptyWindowOptions): Promise<void> {
116 117 118 119 120 121 122 123 124
		// TODO@Ben delegate to embedder
		const targetHref = `${document.location.origin}${document.location.pathname}?ew=true`;
		if (options && options.reuse) {
			window.location.href = targetHref;
		} else {
			window.open(targetHref);
		}
	}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	async toggleFullScreen(): Promise<void> {
		const target = this.layoutService.getWorkbenchElement();

		// Chromium
		if (document.fullscreen !== undefined) {
			if (!document.fullscreen) {
				try {
					return await target.requestFullscreen();
				} catch (error) {
					console.warn('Toggle Full Screen failed'); // https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullscreen
				}
			} else {
				try {
					return await document.exitFullscreen();
				} catch (error) {
					console.warn('Exit Full Screen failed');
				}
			}
		}

		// Safari and Edge 14 are all using webkit prefix
		if ((<any>document).webkitIsFullScreen !== undefined) {
			try {
				if (!(<any>document).webkitIsFullScreen) {
					(<any>target).webkitRequestFullscreen(); // it's async, but doesn't return a real promise.
				} else {
					(<any>document).webkitExitFullscreen(); // it's async, but doesn't return a real promise.
				}
			} catch {
				console.warn('Enter/Exit Full Screen failed');
			}
		}
	}

159 160 161 162
	get hasFocus(): boolean {
		return document.hasFocus();
	}

163 164 165 166
	async focus(): Promise<void> {
		window.focus();
	}

167
	//#endregion
168 169

	async restart(): Promise<void> {
170 171 172 173
		this.reload();
	}

	async reload(): Promise<void> {
174 175
		window.location.reload();
	}
176 177 178 179

	async closeWorkspace(): Promise<void> {
		return this.openEmptyWindow({ reuse: true });
	}
180 181 182
}

registerSingleton(IHostService, BrowserHostService, true);