integration.ts 11.8 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');
B
Benjamin Pasero 已提交
9
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
10
import errors = require('vs/base/common/errors');
B
Benjamin Pasero 已提交
11
import types = require('vs/base/common/types');
E
Erich Gamma 已提交
12
import arrays = require('vs/base/common/arrays');
13
import Severity from 'vs/base/common/severity';
B
Benjamin Pasero 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { IAction, Action } from 'vs/base/common/actions';
import { IPartService } from 'vs/workbench/services/part/common/partService';
import { IMessageService } from 'vs/platform/message/common/message';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IWindowService } from 'vs/workbench/services/window/electron-browser/windowService';
import { AutoSaveConfiguration } from 'vs/platform/files/common/files';
import { IConfigurationEditingService, ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
import { ElectronWindow } from 'vs/workbench/electron-browser/window';
29
import * as browser from 'vs/base/browser/browser';
B
Benjamin Pasero 已提交
30 31 32 33 34 35 36
import { DiffEditorInput, toDiffLabel } from 'vs/workbench/common/editor/diffEditorInput';
import { Position } from 'vs/platform/editor/common/editor';
import { EditorInput } from 'vs/workbench/common/editor';
import { IPath, IOpenFileRequest, IWindowConfiguration } from 'vs/workbench/electron-browser/common';
import { IResourceInput } from 'vs/platform/editor/common/editor';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
37
import URI from 'vs/base/common/uri';
E
Erich Gamma 已提交
38

B
Benjamin Pasero 已提交
39
import { ipcRenderer as ipc, webFrame, remote } from 'electron';
E
Erich Gamma 已提交
40

41 42
const currentWindow = remote.getCurrentWindow();

43 44 45 46 47 48 49 50 51 52 53
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))
];

E
Erich Gamma 已提交
54 55
export class ElectronIntegration {

B
Benjamin Pasero 已提交
56 57
	private static AUTO_SAVE_SETTING = 'files.autoSave';

E
Erich Gamma 已提交
58 59 60 61 62 63
	constructor(
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWindowService private windowService: IWindowService,
		@IPartService private partService: IPartService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@ITelemetryService private telemetryService: ITelemetryService,
B
Benjamin Pasero 已提交
64
		@IWorkspaceConfigurationService private configurationService: IWorkspaceConfigurationService,
65
		@ICommandService private commandService: ICommandService,
B
Benjamin Pasero 已提交
66
		@IConfigurationEditingService private configurationEditingService: IConfigurationEditingService,
E
Erich Gamma 已提交
67
		@IKeybindingService private keybindingService: IKeybindingService,
68
		@IMessageService private messageService: IMessageService,
69 70 71
		@IContextMenuService private contextMenuService: IContextMenuService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService
E
Erich Gamma 已提交
72 73 74 75 76 77
	) {
	}

	public integrate(shellContainer: HTMLElement): void {

		// Register the active window
B
Benjamin Pasero 已提交
78
		const activeWindow = this.instantiationService.createInstance(ElectronWindow, currentWindow, shellContainer);
E
Erich Gamma 已提交
79 80 81
		this.windowService.registerWindow(activeWindow);

		// Support runAction event
82
		ipc.on('vscode:runAction', (event, actionId: string) => {
83
			this.commandService.executeCommand(actionId, { from: 'menu' }).done(undefined, err => this.messageService.show(Severity.Error, err));
E
Erich Gamma 已提交
84 85 86
		});

		// Support resolve keybindings event
87
		ipc.on('vscode:resolveKeybindings', (event, rawActionIds: string) => {
E
Erich Gamma 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
			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
			this.resolveKeybindings(actionIds).done((keybindings) => {
				if (keybindings.length) {
					ipc.send('vscode:keybindingsResolved', JSON.stringify(keybindings));
				}
			}, () => errors.onUnexpectedError);
		});

103
		ipc.on('vscode:telemetry', (event, { eventName, data }) => {
E
Erich Gamma 已提交
104 105 106
			this.telemetryService.publicLog(eventName, data);
		});

107
		ipc.on('vscode:reportError', (event, error) => {
E
Erich Gamma 已提交
108
			if (error) {
B
Benjamin Pasero 已提交
109
				const errorParsed = JSON.parse(error);
E
Erich Gamma 已提交
110 111 112 113 114
				errorParsed.mainProcess = true;
				errors.onUnexpectedError(errorParsed);
			}
		});

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

E
Erich Gamma 已提交
118 119 120 121 122
		// Emit event when vscode has loaded
		this.partService.joinCreation().then(() => {
			ipc.send('vscode:workbenchLoaded', this.windowService.getWindowId());
		});

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

B
Benjamin Pasero 已提交
128 129 130 131 132
		// Support toggling auto save
		ipc.on('vscode.toggleAutoSave', (event) => {
			this.toggleAutoSave();
		});

133 134 135 136 137 138 139 140 141 142 143 144 145
		// Fullscreen Events
		ipc.on('vscode:enterFullScreen', (event) => {
			this.partService.joinCreation().then(() => {
				this.partService.addClass('fullscreen');
			});
		});

		ipc.on('vscode:leaveFullScreen', (event) => {
			this.partService.joinCreation().then(() => {
				this.partService.removeClass('fullscreen');
			});
		});

146 147 148
		// Ensure others can listen to zoom level changes
		browser.setZoomLevel(webFrame.getZoomLevel());

B
Benjamin Pasero 已提交
149
		// Configuration changes
150
		let previousConfiguredZoomLevel: number;
151
		this.configurationService.onDidUpdateConfiguration(e => {
B
Benjamin Pasero 已提交
152
			const windowConfig: IWindowConfiguration = e.config;
B
Benjamin Pasero 已提交
153 154 155 156

			let newZoomLevel = 0;
			if (windowConfig.window && typeof windowConfig.window.zoomLevel === 'number') {
				newZoomLevel = windowConfig.window.zoomLevel;
157 158 159 160 161 162 163

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

				previousConfiguredZoomLevel = newZoomLevel;
B
Benjamin Pasero 已提交
164 165 166 167
			}

			if (webFrame.getZoomLevel() !== newZoomLevel) {
				webFrame.setZoomLevel(newZoomLevel);
B
Benjamin Pasero 已提交
168
				browser.setZoomLevel(webFrame.getZoomLevel()); // Ensure others can listen to zoom level changes
B
Benjamin Pasero 已提交
169
			}
B
Benjamin Pasero 已提交
170
		});
171

172 173 174 175 176 177 178 179 180 181 182 183
		// Context menu support in input/textarea
		window.document.addEventListener('contextmenu', (e) => {
			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({
						getAnchor: () => target,
						getActions: () => TPromise.as(TextInputActions),
						getKeyBinding: (action) => {
B
Benjamin Pasero 已提交
184
							const opts = this.keybindingService.lookupKeybindings(action.id);
185 186 187 188 189 190 191 192 193 194
							if (opts.length > 0) {
								return opts[0]; // only take the first one
							}

							return null;
						}
					});
				}
			}
		});
E
Erich Gamma 已提交
195 196 197 198 199
	}

	private resolveKeybindings(actionIds: string[]): TPromise<{ id: string; binding: number; }[]> {
		return this.partService.joinCreation().then(() => {
			return arrays.coalesce(actionIds.map((id) => {
B
Benjamin Pasero 已提交
200
				const bindings = this.keybindingService.lookupKeybindings(id);
201 202 203

				// return the first binding that can be represented by electron
				for (let i = 0; i < bindings.length; i++) {
B
Benjamin Pasero 已提交
204 205
					const binding = bindings[i];
					const electronAccelerator = this.keybindingService.getElectronAcceleratorFor(binding);
206 207 208 209 210 211
					if (electronAccelerator) {
						return {
							id: id,
							binding: binding.value
						};
					}
E
Erich Gamma 已提交
212 213 214 215 216 217
				}

				return null;
			}));
		});
	}
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

	private onOpenFiles(request: IOpenFileRequest): void {
		let inputs: IResourceInput[] = [];
		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);
		}
	}

	private openResources(resources: IResourceInput[], diffMode: boolean): TPromise<any> {
		return this.partService.joinCreation().then(() => {

			// In diffMode we open 2 resources as diff
			if (diffMode) {
				return TPromise.join(resources.map(f => this.editorService.createInput(f))).then((inputs: EditorInput[]) => {
					return this.editorService.openEditor(new DiffEditorInput(toDiffLabel(resources[0].resource, resources[1].resource, this.contextService), null, inputs[0], inputs[1]));
				});
			}

			// 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,
260
					position: activeEditor ? activeEditor.position : Position.ONE
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
				};
			}));
		});
	}

	private toInputs(paths: IPath[], isNew: boolean): IResourceInput[] {
		return paths.map(p => {
			let input = <IResourceInput>{
				resource: isNew ? this.untitledEditorService.createOrGet(URI.file(p.filePath)).getResource() : URI.file(p.filePath),
				options: {
					pinned: true
				}
			};

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

			return input;
		});
	}
B
Benjamin Pasero 已提交
285 286

	private toggleAutoSave(): void {
B
Benjamin Pasero 已提交
287 288 289 290 291
		const setting = this.configurationService.lookup(ElectronIntegration.AUTO_SAVE_SETTING);
		let userAutoSaveConfig = setting.user;
		if (types.isUndefinedOrNull(userAutoSaveConfig)) {
			userAutoSaveConfig = setting.default; // use default if setting not defined
		}
B
Benjamin Pasero 已提交
292 293

		let newAutoSaveValue: string;
B
Benjamin Pasero 已提交
294
		if ([AutoSaveConfiguration.AFTER_DELAY, AutoSaveConfiguration.ON_FOCUS_CHANGE, AutoSaveConfiguration.ON_WINDOW_CHANGE].some(s => s === userAutoSaveConfig)) {
B
Benjamin Pasero 已提交
295
			newAutoSaveValue = AutoSaveConfiguration.OFF;
B
Benjamin Pasero 已提交
296 297
		} else {
			newAutoSaveValue = AutoSaveConfiguration.AFTER_DELAY;
B
Benjamin Pasero 已提交
298 299 300 301 302
		}

		this.configurationEditingService.writeConfiguration(ConfigurationTarget.USER, { key: ElectronIntegration.AUTO_SAVE_SETTING, value: newAutoSaveValue }).done(null, (error) => this.messageService.show(Severity.Error, error));
	}
}