window.ts 4.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import platform = require('vs/base/common/platform');
9 10
import URI from 'vs/base/common/uri';
import DOM = require('vs/base/browser/dom');
E
Erich Gamma 已提交
11 12 13 14 15 16
import workbenchEditorCommon = require('vs/workbench/common/editor');
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IStorageService} from 'vs/platform/storage/common/storage';
import {IEventService} from 'vs/platform/event/common/event';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
17
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
E
Erich Gamma 已提交
18

19
import {ipcRenderer as ipc, shell, remote} from 'electron';
E
Erich Gamma 已提交
20

21 22
const dialog = remote.dialog;

B
Benjamin Pasero 已提交
23 24 25 26
export interface IWindowConfiguration {
	window: {
		openFilesInNewWindow: boolean;
		reopenFolders: string;
27
		restoreFullscreen: boolean;
B
Benjamin Pasero 已提交
28
		zoomLevel: number;
B
Benjamin Pasero 已提交
29
	};
B
Benjamin Pasero 已提交
30 31
}

E
Erich Gamma 已提交
32
export class ElectronWindow {
33
	private win: Electron.BrowserWindow;
34
	private windowId: number;
E
Erich Gamma 已提交
35 36

	constructor(
37
		win: Electron.BrowserWindow,
E
Erich Gamma 已提交
38 39 40 41 42
		shellContainer: HTMLElement,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IEventService private eventService: IEventService,
		@IStorageService private storageService: IStorageService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
43
		@IEditorGroupService private editorGroupService: IEditorGroupService,
E
Erich Gamma 已提交
44 45 46
		@IViewletService private viewletService: IViewletService
	) {
		this.win = win;
47
		this.windowId = win.id;
E
Erich Gamma 已提交
48 49 50 51 52 53 54
		this.registerListeners();
	}

	private registerListeners(): void {

		// React to editor input changes (Mac only)
		if (platform.platform === platform.Platform.Mac) {
55
			this.editorGroupService.onEditorsChanged(() => {
B
Benjamin Pasero 已提交
56
				let fileInput = workbenchEditorCommon.asFileEditorInput(this.editorService.getActiveEditorInput(), true);
57 58 59 60
				let representedFilename = '';
				if (fileInput) {
					representedFilename = fileInput.getResource().fsPath;
				}
E
Erich Gamma 已提交
61

62
				ipc.send('vscode:setRepresentedFilename', this.windowId, representedFilename);
E
Erich Gamma 已提交
63 64 65
			});
		}

66 67 68 69 70
		// Prevent a dropped link from opening within
		[DOM.EventType.DRAG_OVER, DOM.EventType.DROP].forEach(event => {
			window.document.body.addEventListener(event, (e: DragEvent) => {
				DOM.EventHelper.stop(e);
			});
E
Erich Gamma 已提交
71 72 73
		});

		// Handle window.open() calls
B
Benjamin Pasero 已提交
74
		(<any>window).open = function (url: string, target: string, features: string, replace: boolean) {
B
Benjamin Pasero 已提交
75
			shell.openExternal(url);
E
Erich Gamma 已提交
76

B
Benjamin Pasero 已提交
77
			return null;
E
Erich Gamma 已提交
78
		};
79 80 81 82

		// Patch focus to also focus the entire window
		const originalFocus = window.focus;
		const $this = this;
83
		window.focus = function () {
84 85 86
			originalFocus.call(this, arguments);
			$this.focus();
		};
E
Erich Gamma 已提交
87 88 89
	}

	public open(pathsToOpen: string[]): void;
90
	public open(fileResource: URI): void;
E
Erich Gamma 已提交
91 92 93 94 95 96 97 98
	public open(pathToOpen: string): void;
	public open(arg1: any): void {
		let pathsToOpen: string[];
		if (Array.isArray(arg1)) {
			pathsToOpen = arg1;
		} else if (typeof arg1 === 'string') {
			pathsToOpen = [arg1];
		} else {
99
			pathsToOpen = [(<URI>arg1).fsPath];
E
Erich Gamma 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
		}

		ipc.send('vscode:windowOpen', pathsToOpen); // handled from browser process
	}

	public openNew(): void {
		ipc.send('vscode:openNewWindow'); // handled from browser process
	}

	public close(): void {
		this.win.close();
	}

113
	public reload(): void {
114
		ipc.send('vscode:reloadWindow', this.windowId);
115 116
	}

117
	public showMessageBox(options: Electron.Dialog.ShowMessageBoxOptions): number {
118
		return dialog.showMessageBox(this.win, options);
E
Erich Gamma 已提交
119 120
	}

121
	public showSaveDialog(options: Electron.Dialog.SaveDialogOptions, callback?: (fileName: string) => void): string {
122 123 124 125
		if (callback) {
			return dialog.showSaveDialog(this.win, options, callback);
		}

M
Martin Aeschlimann 已提交
126
		return dialog.showSaveDialog(this.win, options); // https://github.com/electron/electron/issues/4936
E
Erich Gamma 已提交
127 128
	}

129 130
	public setFullScreen(fullscreen: boolean): void {
		ipc.send('vscode:setFullScreen', this.windowId, fullscreen); // handled from browser process
E
Erich Gamma 已提交
131 132
	}

133 134
	public openDevTools(): void {
		ipc.send('vscode:openDevTools', this.windowId); // handled from browser process
E
Erich Gamma 已提交
135 136 137
	}

	public setMenuBarVisibility(visible: boolean): void {
138
		ipc.send('vscode:setMenuBarVisibility', this.windowId, visible); // handled from browser process
E
Erich Gamma 已提交
139 140 141
	}

	public focus(): void {
142
		ipc.send('vscode:focusWindow', this.windowId); // handled from browser process
E
Erich Gamma 已提交
143 144 145
	}

	public flashFrame(): void {
146
		ipc.send('vscode:flashFrame', this.windowId); // handled from browser process
E
Erich Gamma 已提交
147 148
	}
}