browserHostService.ts 2.3 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

export class BrowserHostService implements IHostService {

	_serviceBrand: undefined;

14 15
	constructor(@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService) { }

16 17 18 19
	//#region Window

	readonly windowCount = Promise.resolve(1);

20 21 22 23 24 25 26 27 28 29
	async openEmptyWindow(options?: { reuse?: boolean }): Promise<void> {
		// 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);
		}
	}

30 31 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
	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');
			}
		}
	}

64
	//#endregion
65 66

	async restart(): Promise<void> {
67 68 69 70
		this.reload();
	}

	async reload(): Promise<void> {
71 72
		window.location.reload();
	}
73 74 75
}

registerSingleton(IHostService, BrowserHostService, true);