window.ts 6.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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');
import paths = require('vs/base/common/paths');
import uri from 'vs/base/common/uri';
import {Identifiers} from 'vs/workbench/common/constants';
12
import {EventType, EditorEvent} from 'vs/workbench/common/events';
E
Erich Gamma 已提交
13 14 15 16 17 18 19 20
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 dom = require('vs/base/browser/dom');
import {IStorageService} from 'vs/platform/storage/common/storage';
import {IEventService} from 'vs/platform/event/common/event';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';

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

23 24
const dialog = remote.dialog;

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

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

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

	private registerListeners(): void {

		// React to editor input changes (Mac only)
		if (platform.platform === platform.Platform.Mac) {
			this.eventService.addListener(EventType.EDITOR_INPUT_CHANGED, (e: EditorEvent) => {
56 57 58 59 60
				let fileInput = workbenchEditorCommon.asFileEditorInput(e.editorInput, true);
				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 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 115 116 117 118 119 120 121 122 123 124
			});
		}

		// Prevent a dropped file from opening as nw application
		window.document.body.addEventListener('dragover', (e: DragEvent) => {
			e.preventDefault();
		});

		// Let a dropped file open inside Monaco (only if dropped over editor area)
		window.document.body.addEventListener('drop', (e: DragEvent) => {
			e.preventDefault();

			let editorArea = window.document.getElementById(Identifiers.EDITOR_PART);
			if (dom.isAncestor(e.toElement, editorArea)) {
				let pathsOpened = false;

				// Check for native file transfer
				if (e.dataTransfer && e.dataTransfer.files) {
					let thepaths: string[] = [];
					for (let i = 0; i < e.dataTransfer.files.length; i++) {
						if (e.dataTransfer.files[i] && (<any>e.dataTransfer.files[i]).path) {
							thepaths.push((<any>e.dataTransfer.files[i]).path);
						}
					}

					if (thepaths.length) {
						pathsOpened = true;
						this.focus(); // make sure this window has focus so that the open call reaches the right window!
						this.open(thepaths);
					}
				}

				// Otherwise check for special webkit transfer
				if (!pathsOpened && e.dataTransfer && (<any>e).dataTransfer.items) {
					let items: { getAsString: (clb: (str: string) => void) => void; }[] = (<any>e).dataTransfer.items;
					if (items.length && typeof items[0].getAsString === 'function') {
						items[0].getAsString((str) => {
							try {
								let resource = uri.parse(str);
								if (resource.scheme === 'file') {

									// Do not allow to drop a child of the currently active workspace. This prevents an issue
									// where one would drop a folder from the explorer by accident into the editor area and
									// loose all the context.
									let workspace = this.contextService.getWorkspace();
									if (workspace && paths.isEqualOrParent(resource.fsPath, workspace.resource.fsPath)) {
										return;
									}

									this.focus(); // make sure this window has focus so that the open call reaches the right window!
									this.open([decodeURIComponent(resource.fsPath)]);
								}
							} catch (error) {
								// not a resource
							}
						});
					}
				}
			}
		});

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

B
Benjamin Pasero 已提交
128
			return null;
E
Erich Gamma 已提交
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
		};
	}

	public open(pathsToOpen: string[]): void;
	public open(fileResource: uri): void;
	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 {
			pathsToOpen = [(<uri>arg1).fsPath];
		}

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

156
	public reload(): void {
157
		ipc.send('vscode:reloadWindow', this.windowId);
158 159
	}

160
	public showMessageBox(options: Electron.Dialog.ShowMessageBoxOptions): number {
161
		return dialog.showMessageBox(this.win, options);
E
Erich Gamma 已提交
162 163
	}

164
	public showSaveDialog(options: Electron.Dialog.SaveDialogOptions, callback?: (fileName: string) => void): string {
165 166 167 168
		if (callback) {
			return dialog.showSaveDialog(this.win, options, callback);
		}

B
Benjamin Pasero 已提交
169
		return dialog.showSaveDialog(this.win, options); // https://github.com/atom/electron/issues/4936
E
Erich Gamma 已提交
170 171
	}

172 173
	public setFullScreen(fullscreen: boolean): void {
		ipc.send('vscode:setFullScreen', this.windowId, fullscreen); // handled from browser process
E
Erich Gamma 已提交
174 175
	}

176 177
	public openDevTools(): void {
		ipc.send('vscode:openDevTools', this.windowId); // handled from browser process
E
Erich Gamma 已提交
178 179 180
	}

	public setMenuBarVisibility(visible: boolean): void {
181
		ipc.send('vscode:setMenuBarVisibility', this.windowId, visible); // handled from browser process
E
Erich Gamma 已提交
182 183 184
	}

	public focus(): void {
185
		ipc.send('vscode:focusWindow', this.windowId); // handled from browser process
E
Erich Gamma 已提交
186 187 188
	}

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