integration.ts 12.2 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';
J
Joao Moreno 已提交
37
import { IExtensionGalleryService } from 'vs/platform/extensionManagement/common/extensionManagement';
38
import URI from 'vs/base/common/uri';
E
Erich Gamma 已提交
39

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

42 43
const currentWindow = remote.getCurrentWindow();

44 45 46 47 48 49 50 51 52 53 54
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 已提交
55 56
export class ElectronIntegration {

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

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

	public integrate(shellContainer: HTMLElement): void {

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

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

		// Support resolve keybindings event
89
		ipc.on('vscode:resolveKeybindings', (event, rawActionIds: string) => {
E
Erich Gamma 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
			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);
		});

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

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

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

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

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

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

135 136 137 138 139 140 141 142 143 144 145 146 147
		// 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');
			});
		});

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

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

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

				// 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 已提交
166 167 168 169
			}

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

174 175 176 177 178 179 180 181 182 183 184 185
		// 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 已提交
186
							const opts = this.keybindingService.lookupKeybindings(action.id);
187 188 189 190 191 192 193 194 195 196
							if (opts.length > 0) {
								return opts[0]; // only take the first one
							}

							return null;
						}
					});
				}
			}
		});
J
Joao Moreno 已提交
197 198 199 200 201

		this.extensionGalleryService.getRequestHeaders().done(headers => {
			const urls = ['https://marketplace.visualstudio.com/*', 'https://*.vsassets.io/*'];
			ipc.send('vscode:setHeaders', this.windowService.getWindowId(), urls, headers);
		});
E
Erich Gamma 已提交
202 203 204 205 206
	}

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

				// return the first binding that can be represented by electron
				for (let i = 0; i < bindings.length; i++) {
B
Benjamin Pasero 已提交
211 212
					const binding = bindings[i];
					const electronAccelerator = this.keybindingService.getElectronAcceleratorFor(binding);
213 214 215 216 217 218
					if (electronAccelerator) {
						return {
							id: id,
							binding: binding.value
						};
					}
E
Erich Gamma 已提交
219 220 221 222 223 224
				}

				return null;
			}));
		});
	}
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 260 261 262 263 264 265 266

	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,
267
					position: activeEditor ? activeEditor.position : Position.ONE
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
				};
			}));
		});
	}

	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 已提交
292 293

	private toggleAutoSave(): void {
B
Benjamin Pasero 已提交
294 295 296 297 298
		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 已提交
299 300

		let newAutoSaveValue: string;
B
Benjamin Pasero 已提交
301
		if ([AutoSaveConfiguration.AFTER_DELAY, AutoSaveConfiguration.ON_FOCUS_CHANGE, AutoSaveConfiguration.ON_WINDOW_CHANGE].some(s => s === userAutoSaveConfig)) {
B
Benjamin Pasero 已提交
302
			newAutoSaveValue = AutoSaveConfiguration.OFF;
B
Benjamin Pasero 已提交
303 304
		} else {
			newAutoSaveValue = AutoSaveConfiguration.AFTER_DELAY;
B
Benjamin Pasero 已提交
305 306 307 308 309
		}

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