integration.ts 11.1 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 10 11
import {TPromise} from 'vs/base/common/winjs.base';
import errors = require('vs/base/common/errors');
import arrays = require('vs/base/common/arrays');
12
import Severity from 'vs/base/common/severity';
13 14
import {Separator} from 'vs/base/browser/ui/actionbar/actionbar';
import {IAction, Action} from 'vs/base/common/actions';
E
Erich Gamma 已提交
15
import {IPartService} from 'vs/workbench/services/part/common/partService';
B
Benjamin Pasero 已提交
16
import {IMessageService} from 'vs/platform/message/common/message';
E
Erich Gamma 已提交
17 18
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
19
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
20
import {ICommandService} from 'vs/platform/commands/common/commands';
21
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
22
import {IWorkspaceContextService}from 'vs/platform/workspace/common/workspace';
B
polish  
Benjamin Pasero 已提交
23
import {IWindowService} from 'vs/workbench/services/window/electron-browser/windowService';
B
Benjamin Pasero 已提交
24 25 26
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';
B
polish  
Benjamin Pasero 已提交
27
import {ElectronWindow} from 'vs/workbench/electron-browser/window';
28
import * as browser from 'vs/base/browser/browser';
29 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';
import URI from 'vs/base/common/uri';
E
Erich Gamma 已提交
37

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

40 41
const currentWindow = remote.getCurrentWindow();

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

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

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

	public integrate(shellContainer: HTMLElement): void {

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

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

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

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

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

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

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

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

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

132 133 134
		// Ensure others can listen to zoom level changes
		browser.setZoomLevel(webFrame.getZoomLevel());

B
Benjamin Pasero 已提交
135
		// Configuration changes
136
		let previousConfiguredZoomLevel: number;
137
		this.configurationService.onDidUpdateConfiguration(e => {
B
Benjamin Pasero 已提交
138
			const windowConfig: IWindowConfiguration = e.config;
B
Benjamin Pasero 已提交
139 140 141 142

			let newZoomLevel = 0;
			if (windowConfig.window && typeof windowConfig.window.zoomLevel === 'number') {
				newZoomLevel = windowConfig.window.zoomLevel;
143 144 145 146 147 148 149

				// 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 已提交
150 151 152 153
			}

			if (webFrame.getZoomLevel() !== newZoomLevel) {
				webFrame.setZoomLevel(newZoomLevel);
B
Benjamin Pasero 已提交
154
				browser.setZoomLevel(webFrame.getZoomLevel()); // Ensure others can listen to zoom level changes
B
Benjamin Pasero 已提交
155
			}
B
Benjamin Pasero 已提交
156
		});
157

158 159 160 161 162 163 164 165 166 167 168 169
		// 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 已提交
170
							const opts = this.keybindingService.lookupKeybindings(action.id);
171 172 173 174 175 176 177 178 179 180
							if (opts.length > 0) {
								return opts[0]; // only take the first one
							}

							return null;
						}
					});
				}
			}
		});
E
Erich Gamma 已提交
181 182 183 184 185
	}

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

				// return the first binding that can be represented by electron
				for (let i = 0; i < bindings.length; i++) {
B
Benjamin Pasero 已提交
190 191
					const binding = bindings[i];
					const electronAccelerator = this.keybindingService.getElectronAcceleratorFor(binding);
192 193 194 195 196 197
					if (electronAccelerator) {
						return {
							id: id,
							binding: binding.value
						};
					}
E
Erich Gamma 已提交
198 199 200 201 202 203
				}

				return null;
			}));
		});
	}
204 205 206 207 208 209 210 211 212 213 214 215 216 217 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 260 261 262 263 264 265 266 267 268 269 270

	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,
					position: activeEditor ? activeEditor.position : Position.LEFT
				};
			}));
		});
	}

	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 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284

	private toggleAutoSave(): void {
		const userAutoSaveConfig = this.configurationService.lookup(ElectronIntegration.AUTO_SAVE_SETTING).user;

		let newAutoSaveValue: string;
		if (userAutoSaveConfig === AutoSaveConfiguration.OFF) {
			newAutoSaveValue = AutoSaveConfiguration.AFTER_DELAY;
		} else {
			newAutoSaveValue = AutoSaveConfiguration.OFF;
		}

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