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

import { IHostService } from 'vs/workbench/services/host/browser/host';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
8
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
9 10 11 12 13 14
import { IResourceEditor, IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IWindowSettings, IWindowOpenable, IOpenInWindowOptions, isFolderToOpen, isWorkspaceToOpen, isFileToOpen } from 'vs/platform/windows/common/windows';
import { pathsToEditors } from 'vs/workbench/common/editor';
import { IFileService } from 'vs/platform/files/common/files';
import { ILabelService } from 'vs/platform/label/common/label';
15 16 17 18 19

export class BrowserHostService implements IHostService {

	_serviceBrand: undefined;

20 21 22 23 24 25 26
	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
	) { }
27

28 29 30 31
	//#region Window

	readonly windowCount = Promise.resolve(1);

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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
	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 };
	}

91
	async openEmptyWindow(options?: { reuse?: boolean, remoteAuthority?: string }): Promise<void> {
92 93 94 95 96 97 98 99 100
		// 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);
		}
	}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	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');
			}
		}
	}

135 136 137 138
	async focus(): Promise<void> {
		window.focus();
	}

139
	//#endregion
140 141

	async restart(): Promise<void> {
142 143 144 145
		this.reload();
	}

	async reload(): Promise<void> {
146 147
		window.location.reload();
	}
148 149 150 151

	async closeWorkspace(): Promise<void> {
		return this.openEmptyWindow({ reuse: true });
	}
152 153 154
}

registerSingleton(IHostService, BrowserHostService, true);