window.ts 17.1 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as nls from 'vs/nls';
7
import { URI } from 'vs/base/common/uri';
8 9 10
import * as errors from 'vs/base/common/errors';
import * as objects from 'vs/base/common/objects';
import * as DOM from 'vs/base/browser/dom';
11 12
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { IAction, Action } from 'vs/base/common/actions';
S
SteVen Batten 已提交
13
import { IFileService } from 'vs/platform/files/common/files';
B
Benjamin Pasero 已提交
14
import { toResource, IUntitledResourceInput } from 'vs/workbench/common/editor';
15
import { IEditorService, IResourceEditor } from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
16
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
17
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
M
Martin Aeschlimann 已提交
18
import { IWindowsService, IWindowService, IWindowSettings, IOpenFileRequest, IWindowsConfiguration, IAddFoldersRequest, IRunActionInWindowRequest, IPathData } from 'vs/platform/windows/common/windows';
19
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
B
Benjamin Pasero 已提交
20
import { ITitleService } from 'vs/workbench/services/title/common/titleService';
21
import { IWorkbenchThemeService, VS_HC_THEME } from 'vs/workbench/services/themes/common/workbenchThemeService';
22 23
import * as browser from 'vs/base/browser/browser';
import { ICommandService } from 'vs/platform/commands/common/commands';
B
Benjamin Pasero 已提交
24
import { IResourceInput } from 'vs/platform/editor/common/editor';
A
Alex Dima 已提交
25
import { KeyboardMapperFactory } from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
26
import { Themable } from 'vs/workbench/common/theme';
27
import { ipcRenderer as ipc, webFrame } from 'electron';
28
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
29 30
import { IMenuService, MenuId, IMenu, MenuItemAction, ICommandAction } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
31
import { fillInActionBarActions } from 'vs/platform/actions/browser/menuItemActionItem';
32 33
import { RunOnceScheduler } from 'vs/base/common/async';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
34
import { LifecyclePhase, ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
35
import { IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
36 37 38
import { IIntegrityService } from 'vs/platform/integrity/common/integrity';
import { AccessibilitySupport, isRootUser, isWindows, isMacintosh } from 'vs/base/common/platform';
import product from 'vs/platform/node/product';
39
import { INotificationService } from 'vs/platform/notification/common/notification';
B
Benjamin Pasero 已提交
40
import { EditorServiceImpl } from 'vs/workbench/browser/parts/editor/editor';
E
Erich Gamma 已提交
41

42
const TextInputActions: IAction[] = [
B
Benjamin Pasero 已提交
43 44
	new Action('undo', nls.localize('undo', "Undo"), null, true, () => document.execCommand('undo') && Promise.resolve(true)),
	new Action('redo', nls.localize('redo', "Redo"), null, true, () => document.execCommand('redo') && Promise.resolve(true)),
45
	new Separator(),
B
Benjamin Pasero 已提交
46 47 48
	new Action('editor.action.clipboardCutAction', nls.localize('cut', "Cut"), null, true, () => document.execCommand('cut') && Promise.resolve(true)),
	new Action('editor.action.clipboardCopyAction', nls.localize('copy', "Copy"), null, true, () => document.execCommand('copy') && Promise.resolve(true)),
	new Action('editor.action.clipboardPasteAction', nls.localize('paste', "Paste"), null, true, () => document.execCommand('paste') && Promise.resolve(true)),
49
	new Separator(),
B
Benjamin Pasero 已提交
50
	new Action('editor.action.selectAll', nls.localize('selectAll', "Select All"), null, true, () => document.execCommand('selectAll') && Promise.resolve(true))
51 52
];

53
export class ElectronWindow extends Themable {
54

55 56
	private touchBarMenu: IMenu;
	private touchBarUpdater: RunOnceScheduler;
57 58 59 60 61
	private touchBarDisposables: IDisposable[];
	private lastInstalledTouchedBar: ICommandAction[][];

	private previousConfiguredZoomLevel: number;

62
	private addFoldersScheduler: RunOnceScheduler;
63
	private pendingFoldersToAdd: URI[];
64

E
Erich Gamma 已提交
65
	constructor(
B
Benjamin Pasero 已提交
66
		@IEditorService private editorService: EditorServiceImpl,
67
		@IWindowsService private windowsService: IWindowsService,
B
Benjamin Pasero 已提交
68
		@IWindowService private windowService: IWindowService,
69 70
		@IWorkspaceConfigurationService private configurationService: IWorkspaceConfigurationService,
		@ITitleService private titleService: ITitleService,
71
		@IWorkbenchThemeService protected themeService: IWorkbenchThemeService,
72
		@INotificationService private notificationService: INotificationService,
73 74
		@ICommandService private commandService: ICommandService,
		@IContextMenuService private contextMenuService: IContextMenuService,
75
		@ITelemetryService private telemetryService: ITelemetryService,
76
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
77
		@IFileService private fileService: IFileService,
78
		@IMenuService private menuService: IMenuService,
79 80
		@ILifecycleService private lifecycleService: ILifecycleService,
		@IIntegrityService private integrityService: IIntegrityService
E
Erich Gamma 已提交
81
	) {
82 83
		super(themeService);

84 85
		this.touchBarDisposables = [];

86
		this.pendingFoldersToAdd = [];
B
Benjamin Pasero 已提交
87
		this.addFoldersScheduler = this._register(new RunOnceScheduler(() => this.doAddFolders(), 100));
88

E
Erich Gamma 已提交
89
		this.registerListeners();
90
		this.create();
E
Erich Gamma 已提交
91 92 93 94
	}

	private registerListeners(): void {

95
		// React to editor input changes
B
Benjamin Pasero 已提交
96
		this._register(this.editorService.onDidActiveEditorChange(() => this.updateTouchbarMenu()));
E
Erich Gamma 已提交
97

98
		// prevent opening a real URL inside the shell
99 100 101 102
		[DOM.EventType.DRAG_OVER, DOM.EventType.DROP].forEach(event => {
			window.document.body.addEventListener(event, (e: DragEvent) => {
				DOM.EventHelper.stop(e);
			});
E
Erich Gamma 已提交
103 104
		});

105
		// Support runAction event
106
		ipc.on('vscode:runAction', (event: any, request: IRunActionInWindowRequest) => {
107
			const args: any[] = request.args || [];
108 109 110 111

			// If we run an action from the touchbar, we fill in the currently active resource
			// as payload because the touch bar items are context aware depending on the editor
			if (request.from === 'touchbar') {
B
Benjamin Pasero 已提交
112
				const activeEditor = this.editorService.activeEditor;
113
				if (activeEditor) {
B
Benjamin Pasero 已提交
114
					const resource = toResource(activeEditor, { supportSideBySide: true });
115 116 117 118 119 120 121 122
					if (resource) {
						args.push(resource);
					}
				}
			} else {
				args.push({ from: request.from }); // TODO@telemetry this is a bit weird to send this to every action?
			}

123
			this.commandService.executeCommand(request.id, ...args).then(_ => {
K
kieferrm 已提交
124
				/* __GDPR__
K
kieferrm 已提交
125 126 127 128 129
					"commandExecuted" : {
						"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
						"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
					}
				*/
130
				this.telemetryService.publicLog('commandExecuted', { id: request.id, from: request.from });
J
Johannes Rieken 已提交
131
			}, err => {
132
				this.notificationService.error(err);
J
Johannes Rieken 已提交
133
			});
134 135
		});

B
Benjamin Pasero 已提交
136
		// Error reporting from main
137
		ipc.on('vscode:reportError', (event: any, error: string) => {
138 139 140 141 142 143 144 145
			if (error) {
				const errorParsed = JSON.parse(error);
				errorParsed.mainProcess = true;
				errors.onUnexpectedError(errorParsed);
			}
		});

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

148
		// Support addFolders event if we have a workspace opened
149
		ipc.on('vscode:addFolders', (event: any, request: IAddFoldersRequest) => this.onAddFoldersRequest(request));
150

151
		// Message support
152
		ipc.on('vscode:showInfoMessage', (event: any, message: string) => {
153
			this.notificationService.info(message);
154 155 156
		});

		// Fullscreen Events
157
		ipc.on('vscode:enterFullScreen', () => {
B
Benjamin Pasero 已提交
158
			this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
159 160 161 162
				browser.setFullscreen(true);
			});
		});

163
		ipc.on('vscode:leaveFullScreen', () => {
B
Benjamin Pasero 已提交
164
			this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
165 166 167 168 169
				browser.setFullscreen(false);
			});
		});

		// High Contrast Events
170
		ipc.on('vscode:enterHighContrast', () => {
171
			const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
172
			if (windowConfig && windowConfig.autoDetectHighContrast) {
B
Benjamin Pasero 已提交
173
				this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
M
Martin Aeschlimann 已提交
174
					this.themeService.setColorTheme(VS_HC_THEME, null);
175 176
				});
			}
177 178
		});

179
		ipc.on('vscode:leaveHighContrast', () => {
180
			const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
181
			if (windowConfig && windowConfig.autoDetectHighContrast) {
B
Benjamin Pasero 已提交
182
				this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
183
					this.themeService.restoreColorTheme();
184 185
				});
			}
186 187
		});

A
Alex Dima 已提交
188
		// keyboard layout changed event
189
		ipc.on('vscode:keyboardLayoutChanged', () => {
190
			KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged();
A
Alex Dima 已提交
191 192
		});

193
		// keyboard layout changed event
194
		ipc.on('vscode:accessibilitySupportChanged', (event: any, accessibilitySupportEnabled: boolean) => {
195
			browser.setAccessibilitySupport(accessibilitySupportEnabled ? AccessibilitySupport.Enabled : AccessibilitySupport.Disabled);
196 197
		});

S
Sandeep Somavarapu 已提交
198 199
		// Zoom level changes
		this.updateWindowZoomLevel();
B
Benjamin Pasero 已提交
200
		this._register(this.configurationService.onDidChangeConfiguration(e => {
S
Sandeep Somavarapu 已提交
201 202 203 204
			if (e.affectsConfiguration('window.zoomLevel')) {
				this.updateWindowZoomLevel();
			}
		}));
205

206 207 208
		// Context menu support in input/textarea
		window.document.addEventListener('contextmenu', e => this.onContextMenu(e));
	}
209

210
	private onContextMenu(e: MouseEvent): void {
211 212 213
		if (e.target instanceof HTMLElement) {
			const target = <HTMLElement>e.target;
			if (target.nodeName && (target.nodeName.toLowerCase() === 'input' || target.nodeName.toLowerCase() === 'textarea')) {
B
Benjamin Pasero 已提交
214
				DOM.EventHelper.stop(e, true);
215

216 217
				this.contextMenuService.showContextMenu({
					getAnchor: () => e,
218
					getActions: () => TextInputActions,
B
Benjamin Pasero 已提交
219
					onHide: () => target.focus() // fixes https://github.com/Microsoft/vscode/issues/52948
220
				});
221
			}
222 223 224
		}
	}

S
Sandeep Somavarapu 已提交
225
	private updateWindowZoomLevel(): void {
226
		const windowConfig: IWindowsConfiguration = this.configurationService.getValue<IWindowsConfiguration>();
227 228 229 230

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

232 233 234
			// Leave early if the configured zoom level did not change (https://github.com/Microsoft/vscode/issues/1536)
			if (this.previousConfiguredZoomLevel === newZoomLevel) {
				return;
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

			this.previousConfiguredZoomLevel = newZoomLevel;
		}

		if (webFrame.getZoomLevel() !== newZoomLevel) {
			webFrame.setZoomLevel(newZoomLevel);
			browser.setZoomFactor(webFrame.getZoomFactor());
			// 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);
		}
	}

	private create(): void {

		// Handle window.open() calls
		const $this = this;
		(<any>window).open = function (url: string, target: string, features: string, replace: boolean): any {
			$this.windowsService.openExternal(url);

			return null;
		};

B
Benjamin Pasero 已提交
260 261 262
		// Emit event when vscode is ready
		this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
			ipc.send('vscode:workbenchReady', this.windowService.getCurrentWindowId());
263 264
		});

265 266 267 268
		// Integrity warning
		this.integrityService.isPure().then(res => this.titleService.updateProperties({ isPure: res.isPure }));

		// Root warning
269
		this.lifecycleService.when(LifecyclePhase.Restored).then(() => {
270 271 272 273
			let isAdminPromise: Promise<boolean>;
			if (isWindows) {
				isAdminPromise = import('native-is-elevated').then(isElevated => isElevated());
			} else {
A
Alex Dima 已提交
274
				isAdminPromise = Promise.resolve(isRootUser());
275 276 277 278 279 280 281
			}

			return isAdminPromise.then(isAdmin => {

				// Update title
				this.titleService.updateProperties({ isAdmin });

282 283
				// Show warning message (unix only)
				if (isAdmin && !isWindows) {
284
					this.notificationService.warn(nls.localize('runningAsRoot', "It is not recommended to run {0} as root user.", product.nameShort));
285 286 287
				}
			});
		});
288 289 290

		// Touchbar menu (if enabled)
		this.updateTouchbarMenu();
291 292 293
	}

	private updateTouchbarMenu(): void {
294 295 296 297 298
		if (
			!isMacintosh || // macOS only
			!this.configurationService.getValue<boolean>('keyboard.touchbar.enabled') // disabled via setting
		) {
			return;
299 300
		}

301 302
		// Dispose old
		this.touchBarDisposables = dispose(this.touchBarDisposables);
303
		this.touchBarMenu = void 0;
304

305 306 307 308
		// Create new (delayed)
		this.touchBarUpdater = new RunOnceScheduler(() => this.doUpdateTouchbarMenu(), 300);
		this.touchBarDisposables.push(this.touchBarUpdater);
		this.touchBarUpdater.schedule();
309 310
	}

311 312 313 314 315 316 317
	private doUpdateTouchbarMenu(): void {
		if (!this.touchBarMenu) {
			this.touchBarMenu = this.editorService.invokeWithinEditorContext(accessor => this.menuService.createMenu(MenuId.TouchBarContext, accessor.get(IContextKeyService)));
			this.touchBarDisposables.push(this.touchBarMenu);
			this.touchBarDisposables.push(this.touchBarMenu.onDidChange(() => this.touchBarUpdater.schedule()));
		}

318 319 320
		const actions: (MenuItemAction | Separator)[] = [];

		// Fill actions into groups respecting order
321
		fillInActionBarActions(this.touchBarMenu, void 0, actions);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

		// Convert into command action multi array
		const items: ICommandAction[][] = [];
		let group: ICommandAction[] = [];
		for (let i = 0; i < actions.length; i++) {
			const action = actions[i];

			// Command
			if (action instanceof MenuItemAction) {
				group.push(action.item);
			}

			// Separator
			else if (action instanceof Separator) {
				if (group.length) {
					items.push(group);
338
				}
339 340

				group = [];
341
			}
342 343 344 345 346 347 348 349 350 351 352
		}

		if (group.length) {
			items.push(group);
		}

		// Only update if the actions have changed
		if (!objects.equals(this.lastInstalledTouchedBar, items)) {
			this.lastInstalledTouchedBar = items;
			this.windowService.updateTouchBar(items);
		}
353 354
	}

355 356 357
	private onAddFoldersRequest(request: IAddFoldersRequest): void {

		// Buffer all pending requests
358
		this.pendingFoldersToAdd.push(...request.foldersToAdd.map(f => URI.revive(f)));
359 360 361 362 363 364 365 366 367 368

		// Delay the adding of folders a bit to buffer in case more requests are coming
		if (!this.addFoldersScheduler.isScheduled()) {
			this.addFoldersScheduler.schedule();
		}
	}

	private doAddFolders(): void {
		const foldersToAdd: IWorkspaceFolderCreationData[] = [];

369 370
		this.pendingFoldersToAdd.forEach(folder => {
			foldersToAdd.push(({ uri: folder }));
371 372 373
		});

		this.pendingFoldersToAdd = [];
374

375
		this.workspaceEditingService.addFolders(foldersToAdd);
376 377
	}

378
	private onOpenFiles(request: IOpenFileRequest): void {
B
Benjamin Pasero 已提交
379
		const inputs: IResourceEditor[] = [];
380
		const diffMode = request.filesToDiff && (request.filesToDiff.length === 2);
381 382 383 384 385 386 387 388 389 390 391 392 393 394

		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) {
395
			this.openResources(inputs, diffMode).then(null, errors.onUnexpectedError);
396
		}
397 398 399 400 401

		if (request.filesToWait && inputs.length) {
			// In wait mode, listen to changes to the editors and wait until the files
			// are closed that the user wants to wait for. When this happens we delete
			// the wait marker file to signal to the outside that editing is done.
M
Martin Aeschlimann 已提交
402
			const resourcesToWaitFor = request.filesToWait.paths.map(p => URI.revive(p.fileUri));
403
			const waitMarkerFile = URI.file(request.filesToWait.waitMarkerFilePath);
B
Benjamin Pasero 已提交
404 405
			const unbind = this.editorService.onDidCloseEditor(() => {
				if (resourcesToWaitFor.every(resource => !this.editorService.isOpen({ resource }))) {
406
					unbind.dispose();
407
					this.fileService.del(waitMarkerFile);
408 409 410
				}
			});
		}
411 412
	}

B
Benjamin Pasero 已提交
413
	private openResources(resources: (IResourceInput | IUntitledResourceInput)[], diffMode: boolean): Thenable<any> {
B
Benjamin Pasero 已提交
414
		return this.lifecycleService.when(LifecyclePhase.Restored).then(() => {
N
Nick Snyder 已提交
415

416
			// In diffMode we open 2 resources as diff
417
			if (diffMode && resources.length === 2) {
418
				return this.editorService.openEditor({ leftResource: resources[0].resource, rightResource: resources[1].resource, options: { pinned: true } });
419 420 421 422 423 424 425 426
			}

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

			// Otherwise open all
B
Benjamin Pasero 已提交
427
			return this.editorService.openEditors(resources);
428 429 430
		});
	}

M
Martin Aeschlimann 已提交
431
	private toInputs(paths: IPathData[], isNew: boolean): IResourceEditor[] {
432
		return paths.map(p => {
M
Martin Aeschlimann 已提交
433
			const resource = URI.revive(p.fileUri);
434 435 436 437 438 439
			let input: IResourceInput | IUntitledResourceInput;
			if (isNew) {
				input = { filePath: resource.fsPath, options: { pinned: true } } as IUntitledResourceInput;
			} else {
				input = { resource, options: { pinned: true } } as IResourceInput;
			}
440 441 442 443 444 445 446 447 448 449 450 451

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

			return input;
		});
	}

B
Benjamin Pasero 已提交
452
	dispose(): void {
453 454 455 456
		this.touchBarDisposables = dispose(this.touchBarDisposables);

		super.dispose();
	}
J
Johannes Rieken 已提交
457
}