actions.ts 14.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 URI from 'vs/base/common/uri';
9
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
10 11 12 13 14
import timer = require('vs/base/common/timer');
import paths = require('vs/base/common/paths');
import {Action} from 'vs/base/common/actions';
import {IWindowService} from 'vs/workbench/services/window/electron-browser/windowService';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
15
import {EditorInput} from 'vs/workbench/common/editor';
B
Benjamin Pasero 已提交
16
import {isMacintosh} from 'vs/base/common/platform';
17
import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput';
E
Erich Gamma 已提交
18 19
import nls = require('vs/nls');
import {IMessageService, Severity} from 'vs/platform/message/common/message';
20
import {IWindowConfiguration} from 'vs/workbench/electron-browser/window';
E
Erich Gamma 已提交
21
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
22 23
import {IQuickOpenService, IPickOpenEntry} from 'vs/workbench/services/quickopen/common/quickOpenService';
import {KeyMod} from 'vs/base/common/keyCodes';
24
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
25
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
26
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
27
import * as browser from 'vs/base/browser/browser';
E
Erich Gamma 已提交
28

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

31 32
// --- actions

E
Erich Gamma 已提交
33 34 35 36 37 38 39 40
export class CloseEditorAction extends Action {

	public static ID = 'workbench.action.closeActiveEditor';
	public static LABEL = nls.localize('closeActiveEditor', "Close Editor");

	constructor(
		id: string,
		label: string,
41
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
E
Erich Gamma 已提交
42 43 44 45
	) {
		super(id, label);
	}

46
	public run(): TPromise<any> {
E
Erich Gamma 已提交
47 48
		let activeEditor = this.editorService.getActiveEditor();
		if (activeEditor) {
49
			return this.editorService.closeEditor(activeEditor.position, activeEditor.input);
E
Erich Gamma 已提交
50 51
		}

A
Alex Dima 已提交
52
		return TPromise.as(false);
E
Erich Gamma 已提交
53 54 55 56 57 58 59 60 61 62 63 64
	}
}

export class CloseWindowAction extends Action {

	public static ID = 'workbench.action.closeWindow';
	public static LABEL = nls.localize('closeWindow', "Close Window");

	constructor(id: string, label: string, @IWindowService private windowService: IWindowService) {
		super(id, label);
	}

65
	public run(): TPromise<boolean> {
E
Erich Gamma 已提交
66 67
		this.windowService.getWindow().close();

A
Alex Dima 已提交
68
		return TPromise.as(true);
E
Erich Gamma 已提交
69 70 71 72 73 74 75 76 77 78 79 80
	}
}

export class CloseFolderAction extends Action {

	public static ID = 'workbench.action.closeFolder';
	public static LABEL = nls.localize('closeFolder', "Close Folder");

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
B
Benjamin Pasero 已提交
81 82
		@IMessageService private messageService: IMessageService,
		@IWindowService private windowService: IWindowService
E
Erich Gamma 已提交
83 84 85 86
	) {
		super(id, label);
	}

87
	public run(): TPromise<boolean> {
E
Erich Gamma 已提交
88
		if (this.contextService.getWorkspace()) {
B
Benjamin Pasero 已提交
89
			ipc.send('vscode:closeFolder', this.windowService.getWindowId()); // handled from browser process
E
Erich Gamma 已提交
90 91 92 93
		} else {
			this.messageService.show(Severity.Info, nls.localize('noFolderOpened', "There is currently no folder opened in this instance to close."));
		}

A
Alex Dima 已提交
94
		return TPromise.as(true);
E
Erich Gamma 已提交
95 96 97 98 99 100 101 102
	}
}

export class NewWindowAction extends Action {

	public static ID = 'workbench.action.newWindow';
	public static LABEL = nls.localize('newWindow', "New Window");

B
Benjamin Pasero 已提交
103 104 105 106 107
	constructor(
		id: string,
		label: string,
		@IWindowService private windowService: IWindowService
	) {
E
Erich Gamma 已提交
108 109 110
		super(id, label);
	}

111
	public run(): TPromise<boolean> {
E
Erich Gamma 已提交
112 113
		this.windowService.getWindow().openNew();

A
Alex Dima 已提交
114
		return TPromise.as(true);
E
Erich Gamma 已提交
115 116 117 118 119 120 121 122 123 124 125 126
	}
}

export class ToggleFullScreenAction extends Action {

	public static ID = 'workbench.action.toggleFullScreen';
	public static LABEL = nls.localize('toggleFullScreen', "Toggle Full Screen");

	constructor(id: string, label: string, @IWindowService private windowService: IWindowService) {
		super(id, label);
	}

127
	public run(): TPromise<boolean> {
E
Erich Gamma 已提交
128 129
		ipc.send('vscode:toggleFullScreen', this.windowService.getWindowId());

A
Alex Dima 已提交
130
		return TPromise.as(true);
E
Erich Gamma 已提交
131 132 133
	}
}

134 135 136 137 138 139 140 141 142
export class ToggleMenuBarAction extends Action {

	public static ID = 'workbench.action.toggleMenuBar';
	public static LABEL = nls.localize('toggleMenuBar', "Toggle Menu Bar");

	constructor(id: string, label: string, @IWindowService private windowService: IWindowService) {
		super(id, label);
	}

143
	public run(): TPromise<boolean> {
144 145
		ipc.send('vscode:toggleMenuBar', this.windowService.getWindowId());

A
Alex Dima 已提交
146
		return TPromise.as(true);
147 148 149
	}
}

E
Erich Gamma 已提交
150 151 152 153 154
export class ToggleDevToolsAction extends Action {

	public static ID = 'workbench.action.toggleDevTools';
	public static LABEL = nls.localize('toggleDevTools', "Toggle Developer Tools");

155
	constructor(id: string, label: string, @IWindowService private windowService: IWindowService) {
E
Erich Gamma 已提交
156 157 158
		super(id, label);
	}

159
	public run(): TPromise<boolean> {
160
		ipc.send('vscode:toggleDevTools', this.windowService.getWindowId());
E
Erich Gamma 已提交
161

A
Alex Dima 已提交
162
		return TPromise.as(true);
E
Erich Gamma 已提交
163 164 165 166 167 168
	}
}

export class ZoomInAction extends Action {

	public static ID = 'workbench.action.zoomIn';
B
Benjamin Pasero 已提交
169
	public static LABEL = nls.localize('zoomIn', "Zoom In");
E
Erich Gamma 已提交
170

171
	constructor(id: string, label: string) {
E
Erich Gamma 已提交
172 173 174
		super(id, label);
	}

175
	public run(): TPromise<boolean> {
E
Erich Gamma 已提交
176
		webFrame.setZoomLevel(webFrame.getZoomLevel() + 1);
B
Benjamin Pasero 已提交
177
		browser.setZoomLevel(webFrame.getZoomLevel()); // Ensure others can listen to zoom level changes
E
Erich Gamma 已提交
178

A
Alex Dima 已提交
179
		return TPromise.as(true);
E
Erich Gamma 已提交
180 181 182
	}
}

B
Benjamin Pasero 已提交
183
export class ZoomOutAction extends Action {
E
Erich Gamma 已提交
184 185

	public static ID = 'workbench.action.zoomOut';
B
Benjamin Pasero 已提交
186
	public static LABEL = nls.localize('zoomOut', "Zoom Out");
E
Erich Gamma 已提交
187

188 189
	constructor(
		id: string,
B
Benjamin Pasero 已提交
190
		label: string
191
	) {
B
Benjamin Pasero 已提交
192
		super(id, label);
E
Erich Gamma 已提交
193 194
	}

195
	public run(): TPromise<boolean> {
B
Benjamin Pasero 已提交
196 197
		webFrame.setZoomLevel(webFrame.getZoomLevel() - 1);
		browser.setZoomLevel(webFrame.getZoomLevel()); // Ensure others can listen to zoom level changes
198

199
		return TPromise.as(true);
E
Erich Gamma 已提交
200 201 202
	}
}

B
Benjamin Pasero 已提交
203
export class ZoomResetAction extends Action {
E
Erich Gamma 已提交
204 205 206 207

	public static ID = 'workbench.action.zoomReset';
	public static LABEL = nls.localize('zoomReset', "Reset Zoom");

208 209 210
	constructor(
		id: string,
		label: string,
B
Benjamin Pasero 已提交
211
		@IConfigurationService private configurationService: IConfigurationService
212
	) {
B
Benjamin Pasero 已提交
213
		super(id, label);
E
Erich Gamma 已提交
214 215
	}

216
	public run(): TPromise<boolean> {
217 218
		const level = this.getConfiguredZoomLevel();
		webFrame.setZoomLevel(level);
B
Benjamin Pasero 已提交
219
		browser.setZoomLevel(webFrame.getZoomLevel()); // Ensure others can listen to zoom level changes
220

221
		return TPromise.as(true);
E
Erich Gamma 已提交
222
	}
B
Benjamin Pasero 已提交
223 224 225 226 227 228 229 230 231

	private getConfiguredZoomLevel(): number {
		const windowConfig = this.configurationService.getConfiguration<IWindowConfiguration>();
		if (windowConfig.window && typeof windowConfig.window.zoomLevel === 'number') {
			return windowConfig.window.zoomLevel;
		}

		return 0; // default
	}
E
Erich Gamma 已提交
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
}

/* Copied from loader.ts */
enum LoaderEventType {
	LoaderAvailable = 1,

	BeginLoadingScript = 10,
	EndLoadingScriptOK = 11,
	EndLoadingScriptError = 12,

	BeginInvokeFactory = 21,
	EndInvokeFactory = 22,

	NodeBeginEvaluatingScript = 31,
	NodeEndEvaluatingScript = 32,

	NodeBeginNativeRequire = 33,
	NodeEndNativeRequire = 34
}
interface ILoaderEvent {
	type: LoaderEventType;
	timestamp: number;
	detail: string;
}
export class ShowStartupPerformance extends Action {

	public static ID = 'workbench.action.appPerf';
	public static LABEL = nls.localize('appPerf', "Startup Performance");

261 262 263 264 265 266
	constructor(
		id: string,
		label: string,
		@IWindowService private windowService: IWindowService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
	) {
E
Erich Gamma 已提交
267
		super(id, label);
268 269

		this.enabled = contextService.getConfiguration().env.enablePerformance;
E
Erich Gamma 已提交
270 271 272 273 274 275 276 277 278 279 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 _analyzeLoaderTimes(): any[] {
		let stats = <ILoaderEvent[]>(<any>require).getStats();
		let result = [];

		let total = 0;

		for (let i = 0, len = stats.length; i < len; i++) {
			if (stats[i].type === LoaderEventType.NodeEndNativeRequire) {
				if (stats[i - 1].type === LoaderEventType.NodeBeginNativeRequire && stats[i - 1].detail === stats[i].detail) {
					let entry: any = {};
					entry['Event'] = 'nodeRequire ' + stats[i].detail;
					entry['Took (ms)'] = (stats[i].timestamp - stats[i - 1].timestamp);
					total += (stats[i].timestamp - stats[i - 1].timestamp);
					entry['Start (ms)'] = '**' + stats[i - 1].timestamp;
					entry['End (ms)'] = '**' + stats[i - 1].timestamp;
					result.push(entry);
				}
			}
		}

		if (total > 0) {
			let entry: any = {};
			entry['Event'] = '===nodeRequire TOTAL';
			entry['Took (ms)'] = total;
			entry['Start (ms)'] = '**';
			entry['End (ms)'] = '**';
			result.push(entry);
		}

		return result;
	}

304
	public run(): TPromise<boolean> {
E
Erich Gamma 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
		let table: any[] = [];
		table.push(...this._analyzeLoaderTimes());

		let start = Math.round(remote.getGlobal('programStart') || remote.getGlobal('vscodeStart'));
		let windowShowTime = Math.round(remote.getGlobal('windowShow'));

		let lastEvent: timer.ITimerEvent;
		let events = timer.getTimeKeeper().getCollectedEvents();
		events.forEach((e) => {
			if (e.topic === 'Startup') {
				lastEvent = e;
				let entry: any = {};

				entry['Event'] = e.name;
				entry['Took (ms)'] = e.stopTime.getTime() - e.startTime.getTime();
				entry['Start (ms)'] = Math.max(e.startTime.getTime() - start, 0);
				entry['End (ms)'] = e.stopTime.getTime() - start;

				table.push(entry);
			}
		});

		table.push({ Event: '---------------------------' });

		let windowShowEvent: any = {};
		windowShowEvent['Event'] = 'Show Window at';
		windowShowEvent['Start (ms)'] = windowShowTime - start;
		table.push(windowShowEvent);

		let sum: any = {};
		sum['Event'] = 'Total';
		sum['Took (ms)'] = lastEvent.stopTime.getTime() - start;
		table.push(sum);


		// Show dev tools
		this.windowService.getWindow().openDevTools();

		// Print to console
		setTimeout(() => {
			console.warn('Run the action again if you do not see the numbers!');
			(<any>console).table(table);
		}, 1000);

A
Alex Dima 已提交
349
		return TPromise.as(true);
E
Erich Gamma 已提交
350 351 352 353 354 355 356 357 358 359 360 361
	}
}

export class ReloadWindowAction extends Action {

	public static ID = 'workbench.action.reloadWindow';
	public static LABEL = nls.localize('reloadWindow', "Reload Window");

	constructor(id: string, label: string, @IWindowService private windowService: IWindowService) {
		super(id, label);
	}

362
	public run(): TPromise<boolean> {
363
		this.windowService.getWindow().reload();
E
Erich Gamma 已提交
364

365
		return TPromise.as(true);
E
Erich Gamma 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	}
}

export class OpenRecentAction extends Action {

	public static ID = 'workbench.action.openRecent';
	public static LABEL = nls.localize('openRecent', "Open Recent");

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IQuickOpenService private quickOpenService: IQuickOpenService
	) {
		super(id, label);
	}

383
	public run(): TPromise<boolean> {
384 385 386
		const recentFolders = this.contextService.getConfiguration().env.recentFolders;
		const recentFiles = this.contextService.getConfiguration().env.recentFiles;

387
		let folderPicks: IPickOpenEntry[] = recentFolders.map((p, index) => {
388 389 390 391
			return {
				label: paths.basename(p),
				description: paths.dirname(p),
				path: p,
392 393
				separator: index === 0 ? { label: nls.localize('folders', "folders") } : void 0,
				run: (context) => this.runPick(p, context)
394 395 396
			};
		});

397
		let filePicks: IPickOpenEntry[] = recentFiles.map((p, index) => {
E
Erich Gamma 已提交
398 399 400
			return {
				label: paths.basename(p),
				description: paths.dirname(p),
401
				path: p,
402 403
				separator: index === 0 ? { label: nls.localize('files', "files"), border: true } : void 0,
				run: (context) => this.runPick(p, context)
B
Benjamin Pasero 已提交
404
			};
E
Erich Gamma 已提交
405 406
		});

407 408
		const hasWorkspace = !!this.contextService.getWorkspace();

409
		return this.quickOpenService.pick(folderPicks.concat(...filePicks), {
410
			autoFocus: { autoFocusFirstEntry: !hasWorkspace, autoFocusSecondEntry: hasWorkspace },
B
Benjamin Pasero 已提交
411
			placeHolder: isMacintosh ? nls.localize('openRecentPlaceHolderMac', "Select a path (hold Cmd-key to open in new window)") : nls.localize('openRecentPlaceHolder', "Select a path to open (hold Ctrl-key to open in new window)"),
E
Erich Gamma 已提交
412
			matchOnDescription: true
413 414
		}).then(p => true);
	}
415

416 417 418 419
	private runPick(path, context): void {
		let newWindow = context.keymods.indexOf(KeyMod.CtrlCmd) >= 0;

		ipc.send('vscode:windowOpen', [path], newWindow);
E
Erich Gamma 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	}
}

export class CloseMessagesAction extends Action {

	public static ID = 'workbench.action.closeMessages';
	public static LABEL = nls.localize('closeMessages', "Close Notification Messages");

	constructor(
		id: string,
		label: string,
		@IMessageService private messageService: IMessageService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(id, label);
	}

437
	public run(): TPromise<boolean> {
E
Erich Gamma 已提交
438 439 440 441 442 443 444 445 446 447

		// Close any Message if visible
		this.messageService.hideAll();

		// Restore focus if we got an editor
		const editor = this.editorService.getActiveEditor();
		if (editor) {
			editor.focus();
		}

448
		return TPromise.as(true);
E
Erich Gamma 已提交
449
	}
450 451 452 453
}

// --- commands

454 455 456 457 458 459
CommandsRegistry.registerCommand('_workbench.ipc', function (accessor: ServicesAccessor, ipcMessage: string, ipcArgs: any[]) {
	if (ipcMessage && Array.isArray(ipcArgs)) {
		ipc.send(ipcMessage, ...ipcArgs);
	} else {
		ipc.send(ipcMessage);
	}
460 461
});

462
CommandsRegistry.registerCommand('_workbench.diff', function (accessor: ServicesAccessor, args: [URI, URI, string]) {
463

464 465
	const editorService = accessor.get(IWorkbenchEditorService);
	let [left, right, label] = args;
466

467 468 469
	if (!label) {
		label = nls.localize('diffLeftRightLabel', "{0} ⟷ {1}", left.toString(true), right.toString(true));
	}
470

471 472
	return TPromise.join([editorService.createInput({ resource: left }), editorService.createInput({ resource: right })]).then(inputs => {
		const [left, right] = inputs;
473

474 475 476 477 478
		const diff = new DiffEditorInput(label, undefined, <EditorInput>left, <EditorInput>right);
		return editorService.openEditor(diff);
	}).then(() => {
		return void 0;
	});
479 480
});

481
CommandsRegistry.registerCommand('_workbench.open', function (accessor: ServicesAccessor, args: [URI, number]) {
482

483 484
	const editorService = accessor.get(IWorkbenchEditorService);
	let [resource, column] = args;
485

486 487 488
	return editorService.openEditor({ resource }, column).then(() => {
		return void 0;
	});
489
});