integration.ts 13.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 { Registry } from 'vs/platform/platform';
B
Benjamin Pasero 已提交
10
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
11 12
import { IWorkbenchActionRegistry, Extensions } from 'vs/workbench/common/actionRegistry';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
E
Erich Gamma 已提交
13
import errors = require('vs/base/common/errors');
B
Benjamin Pasero 已提交
14
import types = require('vs/base/common/types');
E
Erich Gamma 已提交
15
import arrays = require('vs/base/common/arrays');
16
import Severity from 'vs/base/common/severity';
B
Benjamin Pasero 已提交
17 18 19 20 21 22 23 24 25 26
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';
J
Joao Moreno 已提交
27
import { IWindowIPCService } from 'vs/workbench/services/window/electron-browser/windowService';
B
Benjamin Pasero 已提交
28 29 30 31
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';
32
import * as browser from 'vs/base/browser/browser';
B
Benjamin Pasero 已提交
33 34 35 36 37 38 39
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';
40
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
41
import { ReloadWindowAction, ToggleDevToolsAction, ShowStartupPerformance, OpenRecentAction } from 'vs/workbench/electron-browser/actions';
B
Benjamin Pasero 已提交
42 43
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
E
Erich Gamma 已提交
44

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

47 48
const currentWindow = remote.getCurrentWindow();

49 50 51 52 53 54 55 56 57 58 59
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 已提交
60 61
export class ElectronIntegration {

B
Benjamin Pasero 已提交
62 63
	private static AUTO_SAVE_SETTING = 'files.autoSave';

E
Erich Gamma 已提交
64 65
	constructor(
		@IInstantiationService private instantiationService: IInstantiationService,
J
Joao Moreno 已提交
66
		@IWindowIPCService private windowService: IWindowIPCService,
E
Erich Gamma 已提交
67 68 69
		@IPartService private partService: IPartService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@ITelemetryService private telemetryService: ITelemetryService,
B
Benjamin Pasero 已提交
70
		@IWorkspaceConfigurationService private configurationService: IWorkspaceConfigurationService,
71
		@ICommandService private commandService: ICommandService,
B
Benjamin Pasero 已提交
72
		@IConfigurationEditingService private configurationEditingService: IConfigurationEditingService,
E
Erich Gamma 已提交
73
		@IKeybindingService private keybindingService: IKeybindingService,
74
		@IMessageService private messageService: IMessageService,
75 76
		@IContextMenuService private contextMenuService: IContextMenuService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
B
Benjamin Pasero 已提交
77 78
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
		@IEnvironmentService private environmentService: IEnvironmentService
E
Erich Gamma 已提交
79 80 81 82 83 84
	) {
	}

	public integrate(shellContainer: HTMLElement): void {

		// Register the active window
B
Benjamin Pasero 已提交
85
		const activeWindow = this.instantiationService.createInstance(ElectronWindow, currentWindow, shellContainer);
E
Erich Gamma 已提交
86 87 88
		this.windowService.registerWindow(activeWindow);

		// Support runAction event
89
		ipc.on('vscode:runAction', (event, actionId: string) => {
90
			this.commandService.executeCommand(actionId, { from: 'menu' }).done(undefined, err => this.messageService.show(Severity.Error, err));
E
Erich Gamma 已提交
91 92 93
		});

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

110
		ipc.on('vscode:telemetry', (event, { eventName, data }) => {
E
Erich Gamma 已提交
111 112 113
			this.telemetryService.publicLog(eventName, data);
		});

114
		ipc.on('vscode:reportError', (event, error) => {
E
Erich Gamma 已提交
115
			if (error) {
B
Benjamin Pasero 已提交
116
				const errorParsed = JSON.parse(error);
E
Erich Gamma 已提交
117 118 119 120 121
				errorParsed.mainProcess = true;
				errors.onUnexpectedError(errorParsed);
			}
		});

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

E
Erich Gamma 已提交
125 126 127 128 129
		// Emit event when vscode has loaded
		this.partService.joinCreation().then(() => {
			ipc.send('vscode:workbenchLoaded', this.windowService.getWindowId());
		});

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

B
Benjamin Pasero 已提交
135 136 137 138 139
		// Support toggling auto save
		ipc.on('vscode.toggleAutoSave', (event) => {
			this.toggleAutoSave();
		});

140 141 142
		// Fullscreen Events
		ipc.on('vscode:enterFullScreen', (event) => {
			this.partService.joinCreation().then(() => {
143
				browser.setFullscreen(true);
144 145 146 147 148
			});
		});

		ipc.on('vscode:leaveFullScreen', (event) => {
			this.partService.joinCreation().then(() => {
149
				browser.setFullscreen(false);
150 151 152
			});
		});

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

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

				// 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 已提交
168 169 170 171
			}

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

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

							return null;
						}
					});
				}
			}
		});
B
Benjamin Pasero 已提交
200 201 202 203 204 205 206 207 208 209

		// Developer related actions
		const developerCategory = nls.localize('developer', "Developer");
		const workbenchActionsRegistry = Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions);
		const isDeveloping = !this.environmentService.isBuilt || !!this.environmentService.extensionDevelopmentPath;
		workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(ReloadWindowAction, ReloadWindowAction.ID, ReloadWindowAction.LABEL, isDeveloping ? { primary: KeyMod.CtrlCmd | KeyCode.KEY_R } : void 0), 'Reload Window');
		workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(ToggleDevToolsAction, ToggleDevToolsAction.ID, ToggleDevToolsAction.LABEL, isDeveloping ? { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_I, mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_I } } : void 0), 'Developer: Toggle Developer Tools', developerCategory);
		if (this.environmentService.performance) {
			workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(ShowStartupPerformance, ShowStartupPerformance.ID, ShowStartupPerformance.LABEL), 'Developer: Startup Performance', developerCategory);
		}
B
Benjamin Pasero 已提交
210 211 212 213

		// Action registered here to prevent a keybinding conflict with reload window
		const fileCategory = nls.localize('file', "File");
		workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(OpenRecentAction, OpenRecentAction.ID, OpenRecentAction.LABEL, { primary: isDeveloping ? null : KeyMod.CtrlCmd | KeyCode.KEY_R, mac: { primary: KeyMod.WinCtrl | KeyCode.KEY_R } }), 'File: Open Recent', fileCategory);
E
Erich Gamma 已提交
214 215 216 217 218
	}

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

				// return the first binding that can be represented by electron
				for (let i = 0; i < bindings.length; i++) {
B
Benjamin Pasero 已提交
223 224
					const binding = bindings[i];
					const electronAccelerator = this.keybindingService.getElectronAcceleratorFor(binding);
225 226 227 228 229 230
					if (electronAccelerator) {
						return {
							id: id,
							binding: binding.value
						};
					}
E
Erich Gamma 已提交
231 232 233 234 235 236
				}

				return null;
			}));
		});
	}
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 271 272 273 274 275 276 277 278

	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,
279
					position: activeEditor ? activeEditor.position : Position.ONE
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
				};
			}));
		});
	}

	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 已提交
304 305

	private toggleAutoSave(): void {
B
Benjamin Pasero 已提交
306 307 308 309 310
		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 已提交
311 312

		let newAutoSaveValue: string;
B
Benjamin Pasero 已提交
313
		if ([AutoSaveConfiguration.AFTER_DELAY, AutoSaveConfiguration.ON_FOCUS_CHANGE, AutoSaveConfiguration.ON_WINDOW_CHANGE].some(s => s === userAutoSaveConfig)) {
B
Benjamin Pasero 已提交
314
			newAutoSaveValue = AutoSaveConfiguration.OFF;
B
Benjamin Pasero 已提交
315 316
		} else {
			newAutoSaveValue = AutoSaveConfiguration.AFTER_DELAY;
B
Benjamin Pasero 已提交
317 318 319 320 321
		}

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