window.ts 6.2 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

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

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

	constructor(
35
		win: Electron.BrowserWindow,
E
Erich Gamma 已提交
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 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 125
		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;
		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) => {
				// if we dont use setTimeout() here for some reason there is an issue when switching between 2 files side by side
				// with the mac trackpad where the editor would think the user wants to select. to reproduce, have 2 files, click
				// into the non-focussed one and move the mouse down and see the editor starts to select lines.
				setTimeout(() => {
					let fileInput = workbenchEditorCommon.asFileEditorInput(e.editorInput, true);
					if (fileInput) {
						this.win.setRepresentedFilename(fileInput.getResource().fsPath);
					} else {
						this.win.setRepresentedFilename('');
					}
				}, 0);

			});
		}

		// 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 已提交
126
		(<any>window).open = function(url: string, target: string, features: string, replace: boolean) {
B
Benjamin Pasero 已提交
127
			shell.openExternal(url);
E
Erich Gamma 已提交
128

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

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

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

161 162
	public showMessageBox(options: Electron.Dialog.ShowMessageBoxOptions): number {
		return remote.dialog.showMessageBox(this.win, options);
E
Erich Gamma 已提交
163 164 165 166 167 168 169
	}

	public setFullScreen(fullscreen: boolean): void {
		this.win.setFullScreen(fullscreen);
	}

	public openDevTools(): void {
170
		this.win.webContents.openDevTools();
E
Erich Gamma 已提交
171 172 173 174 175 176 177 178 179 180 181 182
	}

	public isFullScreen(): boolean {
		return this.win.isFullScreen();
	}

	public setMenuBarVisibility(visible: boolean): void {
		this.win.setMenuBarVisibility(visible);
	}

	public focus(): void {
		if (!this.win.isFocused()) {
183
			this.win.focus();
E
Erich Gamma 已提交
184 185 186 187 188 189 190
		}
	}

	public flashFrame(): void {
		this.win.flashFrame(!this.win.isFocused());
	}
}