window.ts 18.6 KB
Newer Older
E
Erich Gamma 已提交
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.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8
import nls = require('vs/nls');
E
Erich Gamma 已提交
9
import platform = require('vs/base/common/platform');
10
import URI from 'vs/base/common/uri';
11 12
import errors = require('vs/base/common/errors');
import types = require('vs/base/common/types');
J
Johannes Rieken 已提交
13 14
import { TPromise } from 'vs/base/common/winjs.base';
import { stat } from 'vs/base/node/pfs';
15
import arrays = require('vs/base/common/arrays');
16
import DOM = require('vs/base/browser/dom');
17 18 19
import Severity from 'vs/base/common/severity';
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { IAction, Action } from 'vs/base/common/actions';
20
import { extractResources } from 'vs/base/browser/dnd';
J
Johannes Rieken 已提交
21 22
import { Builder, $ } from 'vs/base/browser/builder';
import { IPartService } from 'vs/workbench/services/part/common/partService';
23
import { AutoSaveConfiguration } from 'vs/platform/files/common/files';
24
import { toResource } from 'vs/workbench/common/editor';
25
import { IWorkbenchEditorService, IResourceInputType } from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
26
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
27
import { IMessageService } from 'vs/platform/message/common/message';
J
Johannes Rieken 已提交
28
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
29
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
30
import { IWindowsService, IWindowService, IWindowSettings, IPath, IOpenFileRequest, IWindowsConfiguration } from 'vs/platform/windows/common/windows';
31 32 33 34
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IConfigurationEditingService, ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
B
Benjamin Pasero 已提交
35
import { ITitleService } from 'vs/workbench/services/title/common/titleService';
36
import { IWorkbenchThemeService, VS_HC_THEME, VS_DARK_THEME } from 'vs/workbench/services/themes/common/workbenchThemeService';
37 38 39
import * as browser from 'vs/base/browser/browser';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
N
Nick Snyder 已提交
40
import { Position, IResourceInput, IUntitledResourceInput, IEditor } from 'vs/platform/editor/common/editor';
41
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
A
Alex Dima 已提交
42
import { KeyboardMapperFactory } from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
43
import { Themable, EDITOR_DRAG_AND_DROP_BACKGROUND } from 'vs/workbench/common/theme';
E
Erich Gamma 已提交
44

45
import { ipcRenderer as ipc, webFrame } from 'electron';
46
import { activeContrastBorder } from 'vs/platform/theme/common/colorRegistry';
B
Benjamin Pasero 已提交
47 48
import { extname } from 'vs/base/common/paths';
import { WORKSPACE_EXTENSION } from 'vs/platform/workspaces/common/workspaces';
E
Erich Gamma 已提交
49

50 51 52 53 54 55 56 57 58 59 60
const TextInputActions: IAction[] = [
	new Action('undo', nls.localize('undo', "Undo"), null, true, () => document.execCommand('undo') && TPromise.as(true)),
	new Action('redo', nls.localize('redo', "Redo"), null, true, () => document.execCommand('redo') && TPromise.as(true)),
	new Separator(),
	new Action('editor.action.clipboardCutAction', nls.localize('cut', "Cut"), null, true, () => document.execCommand('cut') && TPromise.as(true)),
	new Action('editor.action.clipboardCopyAction', nls.localize('copy', "Copy"), null, true, () => document.execCommand('copy') && TPromise.as(true)),
	new Action('editor.action.clipboardPasteAction', nls.localize('paste', "Paste"), null, true, () => document.execCommand('paste') && TPromise.as(true)),
	new Separator(),
	new Action('editor.action.selectAll', nls.localize('selectAll', "Select All"), null, true, () => document.execCommand('selectAll') && TPromise.as(true))
];

61
export class ElectronWindow extends Themable {
62 63 64

	private static AUTO_SAVE_SETTING = 'files.autoSave';

E
Erich Gamma 已提交
65 66 67
	constructor(
		shellContainer: HTMLElement,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
68
		@IEditorGroupService private editorGroupService: IEditorGroupService,
69
		@IPartService private partService: IPartService,
70
		@IWindowsService private windowsService: IWindowsService,
B
Benjamin Pasero 已提交
71
		@IWindowService private windowService: IWindowService,
72 73
		@IWorkspaceConfigurationService private configurationService: IWorkspaceConfigurationService,
		@ITitleService private titleService: ITitleService,
74
		@IWorkbenchThemeService protected themeService: IWorkbenchThemeService,
75 76 77 78 79 80 81
		@IMessageService private messageService: IMessageService,
		@IConfigurationEditingService private configurationEditingService: IConfigurationEditingService,
		@ICommandService private commandService: ICommandService,
		@IExtensionService private extensionService: IExtensionService,
		@IViewletService private viewletService: IViewletService,
		@IContextMenuService private contextMenuService: IContextMenuService,
		@IKeybindingService private keybindingService: IKeybindingService,
J
Johannes Rieken 已提交
82 83
		@IEnvironmentService private environmentService: IEnvironmentService,
		@ITelemetryService private telemetryService: ITelemetryService
E
Erich Gamma 已提交
84
	) {
85 86
		super(themeService);

E
Erich Gamma 已提交
87
		this.registerListeners();
88
		this.setup();
E
Erich Gamma 已提交
89 90 91 92
	}

	private registerListeners(): void {

93 94 95
		// React to editor input changes
		this.editorGroupService.onEditorsChanged(() => {
			const file = toResource(this.editorService.getActiveEditorInput(), { supportSideBySide: true, filter: 'file' });
E
Erich Gamma 已提交
96

97 98
			this.titleService.setRepresentedFilename(file ? file.fsPath : '');
		});
E
Erich Gamma 已提交
99

100 101 102 103 104
		let draggedExternalResources: URI[];
		let dropOverlay: Builder;

		function cleanUp(): void {
			draggedExternalResources = void 0;
105

106 107 108 109 110 111 112 113 114 115 116
			if (dropOverlay) {
				dropOverlay.destroy();
				dropOverlay = void 0;
			}
		}

		// Detect resources dropped into Code from outside
		window.document.body.addEventListener(DOM.EventType.DRAG_OVER, (e: DragEvent) => {
			DOM.EventHelper.stop(e);

			if (!draggedExternalResources) {
117
				draggedExternalResources = extractResources(e, true /* external only */).map(d => d.resource);
118

119 120 121
				// Find out if folders/workspaces are dragged and show the appropiate feedback then
				this.shouldOpenAsWorkspace(draggedExternalResources).done(openAsWorkspace => {
					if (openAsWorkspace) {
122
						const activeContrastBorderColor = this.getColor(activeContrastBorder);
123
						dropOverlay = $(window.document.getElementById(this.partService.getWorkbenchElementId()))
124 125 126 127 128
							.div({
								id: 'monaco-workbench-drop-overlay'
							})
							.style({
								backgroundColor: this.getColor(EDITOR_DRAG_AND_DROP_BACKGROUND),
129 130 131 132
								outlineColor: activeContrastBorderColor,
								outlineOffset: activeContrastBorderColor ? '-2px' : null,
								outlineStyle: activeContrastBorderColor ? 'dashed' : null,
								outlineWidth: activeContrastBorderColor ? '2px' : null
133
							})
134 135 136
							.on(DOM.EventType.DROP, (e: DragEvent) => {
								DOM.EventHelper.stop(e, true);

137
								this.windowService.focusWindow(); // make sure this window has focus so that the open call reaches the right window!
B
Benjamin Pasero 已提交
138 139 140 141 142

								// Ask the user when opening a potential large number of folders
								let doOpen = true;
								if (draggedExternalResources.length > 20) {
									doOpen = this.messageService.confirm({
143
										message: nls.localize('confirmOpen', "Are you sure you want to open {0} workspaces?", draggedExternalResources.length),
B
Benjamin Pasero 已提交
144 145
										primaryButton: nls.localize({ key: 'confirmOpenButton', comment: ['&& denotes a mnemonic'] }, "&&Open"),
										type: 'question'
B
Benjamin Pasero 已提交
146 147 148 149 150 151
									});
								}

								if (doOpen) {
									this.windowsService.openWindow(draggedExternalResources.map(r => r.fsPath), { forceReuseWindow: true });
								}
152

B
Benjamin Pasero 已提交
153
								cleanUp();
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
							})
							.on([DOM.EventType.DRAG_LEAVE, DOM.EventType.DRAG_END], () => {
								cleanUp();
							}).once(DOM.EventType.MOUSE_OVER, () => {
								// Under some circumstances we have seen reports where the drop overlay is not being
								// cleaned up and as such the editor area remains under the overlay so that you cannot
								// type into the editor anymore. This seems related to using VMs and DND via host and
								// guest OS, though some users also saw it without VMs.
								// To protect against this issue we always destroy the overlay as soon as we detect a
								// mouse event over it. The delay is used to guarantee we are not interfering with the
								// actual DROP event that can also trigger a mouse over event.
								// See also: https://github.com/Microsoft/vscode/issues/10970
								setTimeout(() => {
									cleanUp();
								}, 300);
							});
					}
				});
172 173 174 175 176
			}
		});

		// Clear our map and overlay on any finish of DND outside the overlay
		[DOM.EventType.DROP, DOM.EventType.DRAG_END].forEach(event => {
177
			window.document.body.addEventListener(event, (e: DragEvent) => {
178
				if (!dropOverlay || e.target !== dropOverlay.getHTMLElement()) {
B
Benjamin Pasero 已提交
179
					cleanUp(); // only run cleanUp() if we are not over the overlay (because we are being called in capture phase)
180 181 182 183 184 185 186
				}
			}, true /* use capture because components within may preventDefault() when they accept the drop */);
		});

		// prevent opening a real URL inside the shell
		window.document.body.addEventListener(DOM.EventType.DROP, (e: DragEvent) => {
			DOM.EventHelper.stop(e);
E
Erich Gamma 已提交
187 188 189
		});

		// Handle window.open() calls
J
Joao Moreno 已提交
190
		const $this = this;
B
Benjamin Pasero 已提交
191
		(<any>window).open = function (url: string, target: string, features: string, replace: boolean) {
J
Joao Moreno 已提交
192
			$this.windowsService.openExternal(url);
B
Benjamin Pasero 已提交
193

B
Benjamin Pasero 已提交
194
			return null;
E
Erich Gamma 已提交
195 196 197
		};
	}

198 199 200 201
	private setup(): void {

		// Support runAction event
		ipc.on('vscode:runAction', (event, actionId: string) => {
J
Johannes Rieken 已提交
202 203 204 205 206
			this.commandService.executeCommand(actionId, { from: 'menu' }).done(_ => {
				this.telemetryService.publicLog('commandExecuted', { id: actionId, from: 'menu' });
			}, err => {
				this.messageService.show(Severity.Error, err);
			});
207 208 209 210 211 212 213 214 215 216 217 218
		});

		// Support resolve keybindings event
		ipc.on('vscode:resolveKeybindings', (event, rawActionIds: string) => {
			let actionIds: string[] = [];
			try {
				actionIds = JSON.parse(rawActionIds);
			} catch (error) {
				// should not happen
			}

			// Resolve keys using the keybinding service and send back to browser process
219
			this.resolveKeybindings(actionIds).done(keybindings => {
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
				if (keybindings.length) {
					ipc.send('vscode:keybindingsResolved', JSON.stringify(keybindings));
				}
			}, () => errors.onUnexpectedError);
		});

		// Send over all extension viewlets when extensions are ready
		this.extensionService.onReady().then(() => {
			ipc.send('vscode:extensionViewlets', JSON.stringify(this.viewletService.getViewlets().filter(v => !!v.extensionId).map(v => { return { id: v.id, label: v.name }; })));
		});

		ipc.on('vscode:reportError', (event, error) => {
			if (error) {
				const errorParsed = JSON.parse(error);
				errorParsed.mainProcess = true;
				errors.onUnexpectedError(errorParsed);
			}
		});

		// Support openFiles event for existing and new files
		ipc.on('vscode:openFiles', (event, request: IOpenFileRequest) => this.onOpenFiles(request));

		// Emit event when vscode has loaded
		this.partService.joinCreation().then(() => {
244
			ipc.send('vscode:workbenchLoaded', this.windowService.getCurrentWindowId());
245 246 247 248 249 250 251 252
		});

		// Message support
		ipc.on('vscode:showInfoMessage', (event, message: string) => {
			this.messageService.show(Severity.Info, message);
		});

		// Support toggling auto save
253
		ipc.on('vscode.toggleAutoSave', event => {
254 255 256 257
			this.toggleAutoSave();
		});

		// Fullscreen Events
258
		ipc.on('vscode:enterFullScreen', event => {
259 260 261 262 263
			this.partService.joinCreation().then(() => {
				browser.setFullscreen(true);
			});
		});

264
		ipc.on('vscode:leaveFullScreen', event => {
265 266 267 268 269 270
			this.partService.joinCreation().then(() => {
				browser.setFullscreen(false);
			});
		});

		// High Contrast Events
271
		ipc.on('vscode:enterHighContrast', event => {
272
			const windowConfig = this.configurationService.getConfiguration<IWindowSettings>('window');
273
			if (windowConfig && windowConfig.autoDetectHighContrast) {
274
				this.partService.joinCreation().then(() => {
M
Martin Aeschlimann 已提交
275
					this.themeService.setColorTheme(VS_HC_THEME, null);
276 277
				});
			}
278 279
		});

280
		ipc.on('vscode:leaveHighContrast', event => {
281
			const windowConfig = this.configurationService.getConfiguration<IWindowSettings>('window');
282
			if (windowConfig && windowConfig.autoDetectHighContrast) {
283
				this.partService.joinCreation().then(() => {
M
Martin Aeschlimann 已提交
284
					this.themeService.setColorTheme(VS_DARK_THEME, null);
285 286
				});
			}
287 288
		});

A
Alex Dima 已提交
289
		// keyboard layout changed event
290 291
		ipc.on('vscode:keyboardLayoutChanged', (event, isISOKeyboard: boolean) => {
			KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged(isISOKeyboard);
A
Alex Dima 已提交
292 293
		});

294 295
		// keyboard layout changed event
		ipc.on('vscode:accessibilitySupportChanged', (event, accessibilitySupportEnabled: boolean) => {
296
			browser.setAccessibilitySupport(accessibilitySupportEnabled ? platform.AccessibilitySupport.Enabled : platform.AccessibilitySupport.Disabled);
297 298
		});

299 300 301
		// Configuration changes
		let previousConfiguredZoomLevel: number;
		this.configurationService.onDidUpdateConfiguration(e => {
302
			const windowConfig: IWindowsConfiguration = this.configurationService.getConfiguration<IWindowsConfiguration>();
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

			let newZoomLevel = 0;
			if (windowConfig.window && typeof windowConfig.window.zoomLevel === 'number') {
				newZoomLevel = windowConfig.window.zoomLevel;

				// Leave early if the configured zoom level did not change (https://github.com/Microsoft/vscode/issues/1536)
				if (previousConfiguredZoomLevel === newZoomLevel) {
					return;
				}

				previousConfiguredZoomLevel = newZoomLevel;
			}

			if (webFrame.getZoomLevel() !== newZoomLevel) {
				webFrame.setZoomLevel(newZoomLevel);
				browser.setZoomFactor(webFrame.getZoomFactor());
319 320 321 322
				// See https://github.com/Microsoft/vscode/issues/26151
				// Cannot be trusted because the webFrame might take some time
				// until it really applies the new zoom level
				browser.setZoomLevel(webFrame.getZoomLevel(), /*isTrusted*/false);
323 324 325 326
			}
		});

		// Context menu support in input/textarea
327
		window.document.addEventListener('contextmenu', e => {
328 329 330 331 332 333 334
			if (e.target instanceof HTMLElement) {
				const target = <HTMLElement>e.target;
				if (target.nodeName && (target.nodeName.toLowerCase() === 'input' || target.nodeName.toLowerCase() === 'textarea')) {
					e.preventDefault();
					e.stopPropagation();

					this.contextMenuService.showContextMenu({
335
						getAnchor: () => e,
336
						getActions: () => TPromise.as(TextInputActions)
337 338 339 340 341 342
					});
				}
			}
		});
	}

343
	private resolveKeybindings(actionIds: string[]): TPromise<{ id: string; label: string, isNative: boolean; }[]> {
344
		return TPromise.join([this.partService.joinCreation(), this.extensionService.onReady()]).then(() => {
345
			return arrays.coalesce(actionIds.map(id => {
346
				const binding = this.keybindingService.lookupKeybinding(id);
347 348 349
				if (!binding) {
					return null;
				}
350

351 352 353 354 355
				// first try to resolve a native accelerator
				const electronAccelerator = binding.getElectronAccelerator();
				if (electronAccelerator) {
					return { id, label: electronAccelerator, isNative: true };
				}
356

357 358 359 360
				// we need this fallback to support keybindings that cannot show in electron menus (e.g. chords)
				const acceleratorLabel = binding.getLabel();
				if (acceleratorLabel) {
					return { id, label: acceleratorLabel, isNative: false };
361 362 363 364 365 366 367 368
				}

				return null;
			}));
		});
	}

	private onOpenFiles(request: IOpenFileRequest): void {
369
		let inputs: IResourceInputType[] = [];
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
		let diffMode = (request.filesToDiff.length === 2);

		if (!diffMode && request.filesToOpen) {
			inputs.push(...this.toInputs(request.filesToOpen, false));
		}

		if (!diffMode && request.filesToCreate) {
			inputs.push(...this.toInputs(request.filesToCreate, true));
		}

		if (diffMode) {
			inputs.push(...this.toInputs(request.filesToDiff, false));
		}

		if (inputs.length) {
			this.openResources(inputs, diffMode).done(null, errors.onUnexpectedError);
		}
	}

N
Nick Snyder 已提交
389 390 391
	private openResources(resources: (IResourceInput | IUntitledResourceInput)[], diffMode: boolean): TPromise<IEditor | IEditor[]> {
		return this.partService.joinCreation().then((): TPromise<IEditor | IEditor[]> => {

392 393

			// In diffMode we open 2 resources as diff
394
			if (diffMode && resources.length === 2) {
395
				return this.editorService.openEditor({ leftResource: resources[0].resource, rightResource: resources[1].resource, options: { pinned: true } });
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
			}

			// For one file, just put it into the current active editor
			if (resources.length === 1) {
				return this.editorService.openEditor(resources[0]);
			}

			// Otherwise open all
			const activeEditor = this.editorService.getActiveEditor();
			return this.editorService.openEditors(resources.map((r, index) => {
				return {
					input: r,
					position: activeEditor ? activeEditor.position : Position.ONE
				};
			}));
		});
	}

414
	private toInputs(paths: IPath[], isNew: boolean): IResourceInputType[] {
415
		return paths.map(p => {
416 417 418 419 420 421 422
			const resource = URI.file(p.filePath);
			let input: IResourceInput | IUntitledResourceInput;
			if (isNew) {
				input = { filePath: resource.fsPath, options: { pinned: true } } as IUntitledResourceInput;
			} else {
				input = { resource, options: { pinned: true } } as IResourceInput;
			}
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

			if (!isNew && p.lineNumber) {
				input.options.selection = {
					startLineNumber: p.lineNumber,
					startColumn: p.columnNumber
				};
			}

			return input;
		});
	}

	private toggleAutoSave(): void {
		const setting = this.configurationService.lookup(ElectronWindow.AUTO_SAVE_SETTING);
		let userAutoSaveConfig = setting.user;
		if (types.isUndefinedOrNull(userAutoSaveConfig)) {
			userAutoSaveConfig = setting.default; // use default if setting not defined
		}

		let newAutoSaveValue: string;
		if ([AutoSaveConfiguration.AFTER_DELAY, AutoSaveConfiguration.ON_FOCUS_CHANGE, AutoSaveConfiguration.ON_WINDOW_CHANGE].some(s => s === userAutoSaveConfig)) {
			newAutoSaveValue = AutoSaveConfiguration.OFF;
		} else {
			newAutoSaveValue = AutoSaveConfiguration.AFTER_DELAY;
		}

S
Sandeep Somavarapu 已提交
449
		this.configurationEditingService.writeConfiguration(ConfigurationTarget.USER, { key: ElectronWindow.AUTO_SAVE_SETTING, value: newAutoSaveValue });
450 451
	}

452
	private shouldOpenAsWorkspace(resources: URI[]): TPromise<boolean> {
453
		return TPromise.join(resources.map(resource => {
454 455 456 457 458
			if (extname(resource.fsPath) === `.${WORKSPACE_EXTENSION}`) {
				return TPromise.as(true); // Workspace
			}

			// Check for Folder
459 460
			return stat(resource.fsPath).then(stats => stats.isDirectory() ? true : false, error => false);
		})).then(res => res.some(res => !!res));
E
Erich Gamma 已提交
461
	}
J
Johannes Rieken 已提交
462
}