shell.ts 21.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./media/shell';

import 'vs/css!vs/workbench/browser/media/vs-theme';
import 'vs/css!vs/workbench/browser/media/vs-dark-theme';
import 'vs/css!vs/workbench/browser/media/hc-black-theme';

14
import * as nls from 'vs/nls';
15
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
16 17 18
import {Dimension, Builder, $} from 'vs/base/browser/builder';
import objects = require('vs/base/common/objects');
import dom = require('vs/base/browser/dom');
19
import aria = require('vs/base/browser/ui/aria/aria');
20
import {disposeAll, IDisposable} from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
21 22 23 24 25 26
import errors = require('vs/base/common/errors');
import {ContextViewService} from 'vs/platform/contextview/browser/contextViewService';
import {ContextMenuService} from 'vs/workbench/services/contextview/electron-browser/contextmenuService';
import {Preferences} from 'vs/workbench/common/constants';
import timer = require('vs/base/common/timer');
import {Workbench} from 'vs/workbench/browser/workbench';
27
import {Storage, inMemoryLocalStorageInstance} from 'vs/workbench/common/storage';
E
Erich Gamma 已提交
28 29 30 31
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {ElectronTelemetryService} from  'vs/platform/telemetry/electron-browser/electronTelemetryService';
import {ElectronIntegration} from 'vs/workbench/electron-browser/integration';
import {Update} from 'vs/workbench/electron-browser/update';
B
Benjamin Pasero 已提交
32
import {WorkspaceStats} from 'vs/platform/telemetry/common/workspaceStats';
E
Erich Gamma 已提交
33 34 35 36
import {IWindowService, WindowService} from 'vs/workbench/services/window/electron-browser/windowService';
import {MessageService} from 'vs/workbench/services/message/electron-browser/messageService';
import {RequestService} from 'vs/workbench/services/request/node/requestService';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
B
Benjamin Pasero 已提交
37
import {ConfigurationService} from 'vs/workbench/services/configuration/node/configurationService';
E
Erich Gamma 已提交
38 39 40
import {FileService} from 'vs/workbench/services/files/electron-browser/fileService';
import {SearchService} from 'vs/workbench/services/search/node/searchService';
import {LifecycleService} from 'vs/workbench/services/lifecycle/electron-browser/lifecycleService';
41
import {WorkbenchKeybindingService} from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
E
Erich Gamma 已提交
42
import {MainThreadService} from 'vs/workbench/services/thread/electron-browser/threadService';
43
import {MainProcessMarkerService} from 'vs/platform/markers/common/markerService';
E
Erich Gamma 已提交
44 45 46 47 48 49
import {IActionsService} from 'vs/platform/actions/common/actions';
import ActionsService from 'vs/platform/actions/common/actionsService';
import {IModelService} from 'vs/editor/common/services/modelService';
import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl';
import {CodeEditorServiceImpl} from 'vs/editor/browser/services/codeEditorServiceImpl';
import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService';
50 51
import {EditorWorkerServiceImpl} from 'vs/editor/common/services/editorWorkerServiceImpl';
import {IEditorWorkerService} from 'vs/editor/common/services/editorWorkerService';
J
Johannes Rieken 已提交
52
import {MainProcessVSCodeAPIHelper} from 'vs/workbench/api/node/extHost.api.impl';
53
import {MainProcessExtensionService} from 'vs/platform/extensions/common/nativeExtensionService';
J
Johannes Rieken 已提交
54
import {MainThreadDocuments} from 'vs/workbench/api/node/extHostDocuments';
E
Erich Gamma 已提交
55 56
import {MainProcessTextMateSyntax} from 'vs/editor/node/textMate/TMSyntax';
import {MainProcessTextMateSnippet} from 'vs/editor/node/textMate/TMSnippets';
M
Martin Aeschlimann 已提交
57
import {JSONValidationExtensionPoint} from 'vs/platform/jsonschemas/common/jsonValidationExtensionPoint';
E
Erich Gamma 已提交
58
import {LanguageConfigurationFileHandler} from 'vs/editor/node/languageConfiguration';
J
Johannes Rieken 已提交
59 60 61 62
import {MainThreadFileSystemEventService} from 'vs/workbench/api/node/extHostFileSystemEventService';
import {MainThreadQuickOpen} from 'vs/workbench/api/node/extHostQuickOpen';
import {MainThreadStatusBar} from 'vs/workbench/api/node/extHostStatusBar';
import {MainThreadCommands} from 'vs/workbench/api/node/extHostCommands';
E
Erich Gamma 已提交
63
import {RemoteTelemetryServiceHelper} from 'vs/platform/telemetry/common/abstractRemoteTelemetryService';
J
Johannes Rieken 已提交
64 65 66 67 68 69 70 71
import {MainThreadDiagnostics} from 'vs/workbench/api/node/extHostDiagnostics';
import {MainThreadOutputService} from 'vs/workbench/api/node/extHostOutputService';
import {MainThreadMessageService} from 'vs/workbench/api/node/extHostMessageService';
import {MainThreadLanguages} from 'vs/workbench/api/node/extHostLanguages';
import {MainThreadEditors} from 'vs/workbench/api/node/extHostEditors';
import {MainThreadWorkspace} from 'vs/workbench/api/node/extHostWorkspace';
import {MainThreadConfiguration} from 'vs/workbench/api/node/extHostConfiguration';
import {MainThreadLanguageFeatures} from 'vs/workbench/api/node/extHostLanguageFeatures';
E
Erich Gamma 已提交
72 73 74 75 76 77 78
import {EventService} from 'vs/platform/event/common/eventService';
import {IOptions} from 'vs/workbench/common/options';
import themes = require('vs/platform/theme/common/themes');
import {WorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService';
import {IStorageService, StorageScope, StorageEvent, StorageEventType} from 'vs/platform/storage/common/storage';
import {MainThreadStorage} from 'vs/platform/storage/common/remotable.storage';
import {IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
A
Alex Dima 已提交
79
import {createInstantiationService as createInstantiationService } from 'vs/platform/instantiation/common/instantiationService';
E
Erich Gamma 已提交
80 81 82 83 84
import {IContextViewService, IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEventService} from 'vs/platform/event/common/event';
import {IFileService} from 'vs/platform/files/common/files';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle';
B
Benjamin Pasero 已提交
85
import {IMarkerService} from 'vs/platform/markers/common/markers';
E
Erich Gamma 已提交
86 87 88 89 90
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {IRequestService} from 'vs/platform/request/common/request';
import {ISearchService} from 'vs/platform/search/common/search';
import {IThreadService} from 'vs/platform/thread/common/thread';
import {IWorkspaceContextService, IConfiguration, IWorkspace} from 'vs/platform/workspace/common/workspace';
A
Alex Dima 已提交
91
import {IExtensionService} from 'vs/platform/extensions/common/extensions';
E
Erich Gamma 已提交
92 93
import {MainThreadModeServiceImpl} from 'vs/editor/common/services/modeServiceImpl';
import {IModeService} from 'vs/editor/common/services/modeService';
94
import {IUntitledEditorService, UntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
E
Erich Gamma 已提交
95
import {CrashReporter} from 'vs/workbench/electron-browser/crashReporter';
96 97
import {IThemeService, DEFAULT_THEME_ID} from 'vs/workbench/services/themes/common/themeService';
import {ThemeService} from 'vs/workbench/services/themes/node/themeService';
98 99 100 101 102
import {getService } from 'vs/base/common/service';
import {connect} from 'vs/base/node/service.net';
import {IExtensionsService} from 'vs/workbench/parts/extensions/common/extensions';
import {ExtensionsService} from 'vs/workbench/parts/extensions/node/extensionsService';
import {ReloadWindowAction} from 'vs/workbench/electron-browser/actions';
E
Erich Gamma 已提交
103 104 105 106 107 108

/**
 * The Monaco Workbench Shell contains the Monaco workbench with a rich header containing navigation and the activity bar.
 * With the Shell being the top level element in the page, it is also responsible for driving the layouting.
 */
export class WorkbenchShell {
B
Benjamin Pasero 已提交
109 110 111 112 113
	private storageService: IStorageService;
	private messageService: IMessageService;
	private contextViewService: ContextViewService;
	private windowService: IWindowService;
	private threadService: MainThreadService;
E
Erich Gamma 已提交
114
	private themeService: IThemeService;
B
Benjamin Pasero 已提交
115 116
	private contextService: WorkspaceContextService;
	private telemetryService: ElectronTelemetryService;
117
	private keybindingService: WorkbenchKeybindingService;
E
Erich Gamma 已提交
118 119

	private container: HTMLElement;
120
	private toUnbind: IDisposable[];
E
Erich Gamma 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	private previousErrorValue: string;
	private previousErrorTime: number;
	private content: HTMLElement;
	private contentsContainer: Builder;
	private currentTheme: string;

	private configuration: IConfiguration;
	private workspace: IWorkspace;
	private options: IOptions;
	private workbench: Workbench;

	constructor(container: HTMLElement, workspace: IWorkspace, configuration: IConfiguration, options: IOptions) {
		this.container = container;

		this.workspace = workspace;
		this.configuration = configuration;
		this.options = objects.mixin({}, options);

		this.toUnbind = [];
		this.previousErrorTime = 0;
	}

	private createContents(parent: Builder): Builder {

145
		// ARIA
B
Benjamin Pasero 已提交
146
		aria.setARIAContainer(document.body);
147

E
Erich Gamma 已提交
148 149 150 151
		// Workbench Container
		let workbenchContainer = $(parent).div();

		// Instantiation service with services
152
		let instantiationService = this.initInstantiationService();
E
Erich Gamma 已提交
153 154 155

		//crash reporting
		if (!!this.configuration.env.crashReporter) {
156
			let crashReporter = instantiationService.createInstance(CrashReporter, this.configuration.env.version, this.configuration.env.commitHash);
E
Erich Gamma 已提交
157 158 159 160
			crashReporter.start(this.configuration.env.crashReporter);
		}

		const sharedProcessClientPromise = connect(process.env['VSCODE_SHARED_IPC_HOOK']);
161 162 163
		sharedProcessClientPromise.done(service => {
			service.onClose(() => {
				this.messageService.show(Severity.Error, {
164
					message: nls.localize('sharedProcessCrashed', "The shared process terminated unexpectedly. Please reload the window to recover."),
165 166 167 168 169
					actions: [instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, ReloadWindowAction.LABEL)]
				});
			});
		}, errors.onUnexpectedError);

J
Joao Moreno 已提交
170
		instantiationService.addSingleton(IExtensionsService, getService<IExtensionsService>(sharedProcessClientPromise, 'ExtensionService', ExtensionsService));
E
Erich Gamma 已提交
171 172

		// Workbench
173
		this.workbench = new Workbench(workbenchContainer.getHTMLElement(), this.workspace, this.configuration, this.options, instantiationService);
E
Erich Gamma 已提交
174 175
		this.workbench.startup({
			onServicesCreated: () => {
A
Alex Dima 已提交
176
				this.initExtensionSystem();
B
Benjamin Pasero 已提交
177 178 179
			},
			onWorkbenchStarted: () => {
				this.onWorkbenchStarted();
E
Erich Gamma 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
			}
		});

		// Electron integration
		this.workbench.getInstantiationService().createInstance(ElectronIntegration).integrate(this.container);

		// Update
		this.workbench.getInstantiationService().createInstance(Update);

		// Handle case where workbench is not starting up properly
		let timeoutHandle = setTimeout(() => {
			console.warn('Workbench did not finish loading in 10 seconds, that might be a problem that should be reported.');
		}, 10000);

		this.workbench.joinCreation().then(() => {
			clearTimeout(timeoutHandle);
		});

		return workbenchContainer;
	}

B
Benjamin Pasero 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	private onWorkbenchStarted(): void {

		// Log to telemetry service
		let windowSize = {
			innerHeight: window.innerHeight,
			innerWidth: window.innerWidth,
			outerHeight: window.outerHeight,
			outerWidth: window.outerWidth
		};

		this.telemetryService.publicLog('workspaceLoad',
			{
				userAgent: navigator.userAgent,
				windowSize: windowSize,
				emptyWorkbench: !this.contextService.getWorkspace(),
				customKeybindingsCount: this.keybindingService.customKeybindingsCount(),
				theme: this.currentTheme
			});

		let workspaceStats: WorkspaceStats = <WorkspaceStats>this.workbench.getInstantiationService().createInstance(WorkspaceStats);
		workspaceStats.reportWorkspaceTags();
	}

E
Erich Gamma 已提交
224
	private initInstantiationService(): IInstantiationService {
B
Benjamin Pasero 已提交
225
		let eventService = new EventService();
E
Erich Gamma 已提交
226

B
Benjamin Pasero 已提交
227
		this.contextService = new WorkspaceContextService(eventService, this.workspace, this.configuration, this.options);
E
Erich Gamma 已提交
228

B
Benjamin Pasero 已提交
229
		this.windowService = new WindowService();
230

A
Alex Dima 已提交
231
		let disableWorkspaceStorage = this.configuration.env.extensionTestsPath || (!this.workspace && !this.configuration.env.extensionDevelopmentPath); // without workspace or in any extension test, we use inMemory storage unless we develop an extension where we want to preserve state
232
		this.storageService = new Storage(this.contextService, window.localStorage, disableWorkspaceStorage ? inMemoryLocalStorageInstance : window.localStorage);
E
Erich Gamma 已提交
233

234 235 236 237 238
		let configService = new ConfigurationService(
			this.contextService,
			eventService
		);

A
Alex Dima 已提交
239
		// no telemetry in a window for extension development!
A
Alex Dima 已提交
240
		let enableTelemetry = this.configuration.env.isBuilt && !this.configuration.env.extensionDevelopmentPath ? !!this.configuration.env.enableTelemetry : false;
241
		this.telemetryService = new ElectronTelemetryService(configService, this.storageService, { enableTelemetry: enableTelemetry, version: this.configuration.env.version, commitHash: this.configuration.env.commitHash });
E
Erich Gamma 已提交
242

243
		this.keybindingService = new WorkbenchKeybindingService(this.contextService, eventService, this.telemetryService, <any>window);
E
Erich Gamma 已提交
244

B
Benjamin Pasero 已提交
245 246
		this.messageService = new MessageService(this.contextService, this.windowService, this.telemetryService, this.keybindingService);
		this.keybindingService.setMessageService(this.messageService);
E
Erich Gamma 已提交
247

B
Benjamin Pasero 已提交
248 249 250 251
		let fileService = new FileService(
			configService,
			eventService,
			this.contextService
E
Erich Gamma 已提交
252 253
		);

B
Benjamin Pasero 已提交
254
		this.contextViewService = new ContextViewService(this.container, this.telemetryService, this.messageService);
E
Erich Gamma 已提交
255

B
Benjamin Pasero 已提交
256
		let lifecycleService = new LifecycleService(this.messageService, this.windowService);
257
		lifecycleService.onShutdown(() => fileService.dispose());
E
Erich Gamma 已提交
258

B
Benjamin Pasero 已提交
259
		this.threadService = new MainThreadService(this.contextService, this.messageService, this.windowService);
260
		lifecycleService.onShutdown(() => this.threadService.dispose());
E
Erich Gamma 已提交
261

B
Benjamin Pasero 已提交
262 263 264 265
		let requestService = new RequestService(
			this.contextService,
			configService,
			this.telemetryService
E
Erich Gamma 已提交
266
		);
267
		lifecycleService.onShutdown(() => requestService.dispose());
E
Erich Gamma 已提交
268

269
		let markerService = new MainProcessMarkerService(this.threadService);
E
Erich Gamma 已提交
270

A
Alex Dima 已提交
271
		let extensionService = new MainProcessExtensionService(this.contextService, this.threadService, this.messageService, this.telemetryService);
A
Alex Dima 已提交
272
		this.keybindingService.setExtensionService(extensionService);
E
Erich Gamma 已提交
273

274
		let modeService = new MainThreadModeServiceImpl(this.threadService, extensionService, configService);
A
Alex Dima 已提交
275
		let modelService = new ModelServiceImpl(this.threadService, markerService, modeService, configService, this.messageService);
276
		let editorWorkerService = new EditorWorkerServiceImpl(modelService);
E
Erich Gamma 已提交
277 278

		let untitledEditorService = new UntitledEditorService();
A
Alex Dima 已提交
279
		this.themeService = new ThemeService(extensionService);
E
Erich Gamma 已提交
280 281

		let result = createInstantiationService();
B
Benjamin Pasero 已提交
282 283 284 285 286
		result.addSingleton(ITelemetryService, this.telemetryService);
		result.addSingleton(IEventService, eventService);
		result.addSingleton(IRequestService, requestService);
		result.addSingleton(IWorkspaceContextService, this.contextService);
		result.addSingleton(IContextViewService, this.contextViewService);
287
		result.addSingleton(IContextMenuService, new ContextMenuService(this.messageService, this.telemetryService, this.keybindingService));
B
Benjamin Pasero 已提交
288 289 290 291
		result.addSingleton(IMessageService, this.messageService);
		result.addSingleton(IStorageService, this.storageService);
		result.addSingleton(ILifecycleService, lifecycleService);
		result.addSingleton(IThreadService, this.threadService);
A
Alex Dima 已提交
292
		result.addSingleton(IExtensionService, extensionService);
E
Erich Gamma 已提交
293
		result.addSingleton(IModeService, modeService);
B
Benjamin Pasero 已提交
294
		result.addSingleton(IFileService, fileService);
E
Erich Gamma 已提交
295
		result.addSingleton(IUntitledEditorService, untitledEditorService);
B
Benjamin Pasero 已提交
296 297 298 299 300 301
		result.addSingleton(ISearchService, new SearchService(modelService, untitledEditorService, this.contextService, configService));
		result.addSingleton(IWindowService, this.windowService);
		result.addSingleton(IConfigurationService, configService);
		result.addSingleton(IKeybindingService, this.keybindingService);
		result.addSingleton(IMarkerService, markerService);
		result.addSingleton(IModelService, modelService);
E
Erich Gamma 已提交
302
		result.addSingleton(ICodeEditorService, new CodeEditorServiceImpl());
303
		result.addSingleton(IEditorWorkerService, editorWorkerService);
E
Erich Gamma 已提交
304
		result.addSingleton(IThemeService, this.themeService);
A
Alex Dima 已提交
305
		result.addSingleton(IActionsService, new ActionsService(extensionService, this.keybindingService));
E
Erich Gamma 已提交
306 307 308 309 310 311


		return result;
	}

	// TODO@Alex, TODO@Joh move this out of here?
A
Alex Dima 已提交
312
	private initExtensionSystem(): void {
B
Benjamin Pasero 已提交
313 314 315
		this.threadService.getRemotable(MainProcessVSCodeAPIHelper);
		this.threadService.getRemotable(MainThreadDocuments);
		this.threadService.getRemotable(RemoteTelemetryServiceHelper);
E
Erich Gamma 已提交
316 317
		this.workbench.getInstantiationService().createInstance(MainProcessTextMateSyntax);
		this.workbench.getInstantiationService().createInstance(MainProcessTextMateSnippet);
M
Martin Aeschlimann 已提交
318
		this.workbench.getInstantiationService().createInstance(JSONValidationExtensionPoint);
E
Erich Gamma 已提交
319
		this.workbench.getInstantiationService().createInstance(LanguageConfigurationFileHandler);
B
Benjamin Pasero 已提交
320 321 322
		this.threadService.getRemotable(MainThreadConfiguration);
		this.threadService.getRemotable(MainThreadQuickOpen);
		this.threadService.getRemotable(MainThreadStatusBar);
E
Erich Gamma 已提交
323
		this.workbench.getInstantiationService().createInstance(MainThreadFileSystemEventService);
B
Benjamin Pasero 已提交
324 325 326 327 328 329 330 331
		this.threadService.getRemotable(MainThreadCommands);
		this.threadService.getRemotable(MainThreadOutputService);
		this.threadService.getRemotable(MainThreadDiagnostics);
		this.threadService.getRemotable(MainThreadMessageService);
		this.threadService.getRemotable(MainThreadLanguages);
		this.threadService.getRemotable(MainThreadWorkspace);
		this.threadService.getRemotable(MainThreadEditors);
		this.threadService.getRemotable(MainThreadStorage);
J
Johannes Rieken 已提交
332
		this.threadService.getRemotable(MainThreadLanguageFeatures);
E
Erich Gamma 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	}

	public open(): void {

		// Listen on unexpected errors
		errors.setUnexpectedErrorHandler((error: any) => {
			this.onUnexpectedError(error);
		});

		// Shell Class for CSS Scoping
		$(this.container).addClass('monaco-shell');

		// Controls
		this.content = $('.monaco-shell-content').appendTo(this.container).getHTMLElement();

		// Handle Load Performance Timers
		this.writeTimers();

		// Create Contents
		this.contentsContainer = this.createContents($(this.content));

		// Layout
		this.layout();

		// Listeners
		this.registerListeners();

		// Enable theme support
B
Benjamin Pasero 已提交
361
		let themeId = this.storageService.get(Preferences.THEME, StorageScope.GLOBAL, null);
E
Erich Gamma 已提交
362
		if (!themeId) {
363
			themeId = DEFAULT_THEME_ID;
B
Benjamin Pasero 已提交
364
			this.storageService.store(Preferences.THEME, themeId, StorageScope.GLOBAL);
E
Erich Gamma 已提交
365 366 367 368
		}

		this.setTheme(themeId, false);

369
		this.toUnbind.push(this.storageService.addListener2(StorageEventType.STORAGE, (e: StorageEvent) => {
E
Erich Gamma 已提交
370 371 372 373 374 375 376 377 378 379
			if (e.key === Preferences.THEME) {
				this.setTheme(e.newValue);
			}
		}));
	}

	private setTheme(themeId: string, layout = true): void {
		if (!themeId) {
			return;
		}
B
Benjamin Pasero 已提交
380
		let applyTheme = () => {
E
Erich Gamma 已提交
381 382 383 384 385 386 387 388 389
			if (this.currentTheme) {
				$(this.container).removeClass(this.currentTheme);
			}
			this.currentTheme = themeId;
			$(this.container).addClass(this.currentTheme);

			if (layout) {
				this.layout();
			}
B
Benjamin Pasero 已提交
390
		};
E
Erich Gamma 已提交
391 392 393 394

		if (!themes.getSyntaxThemeId(themeId)) {
			applyTheme();
		} else {
395
			this.themeService.loadTheme(themeId).then(theme => {
E
Erich Gamma 已提交
396
				if (theme) {
397
					this.themeService.applyThemeCSS(themeId);
E
Erich Gamma 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
					applyTheme();
				}
			}, error => {
				errors.onUnexpectedError(error);
			});
		}
	}

	private registerListeners(): void {

		// Resize
		$(window).on(dom.EventType.RESIZE, () => this.layout(), this.toUnbind);
	}

	private writeTimers(): void {
		let timers = (<any>window).MonacoEnvironment.timers;
		if (timers) {
			let events: timer.IExistingTimerEvent[] = [];

			// Program
			if (timers.beforeProgram) {
				events.push({
					startTime: timers.beforeProgram,
					stopTime: timers.afterProgram,
					topic: 'Startup',
					name: 'Program Start',
					description: 'Time it takes to pass control to VSCodes main method'
				});
			}

			// Window
			if (timers.vscodeStart) {
				events.push({
					startTime: timers.vscodeStart,
					stopTime: timers.beforeLoad,
					topic: 'Startup',
					name: 'VSCode Startup',
					description: 'Time it takes to create a window and startup VSCode'
				});
			}

			// Load
			events.push({
				startTime: timers.beforeLoad,
				stopTime: timers.afterLoad,
				topic: 'Startup',
				name: 'Load Modules',
				description: 'Time it takes to load VSCodes main modules'
			});

			// Ready
			events.push({
				startTime: timers.beforeReady,
				stopTime: timers.afterReady,
				topic: 'Startup',
				name: 'Event DOMContentLoaded',
				description: 'Time it takes for the DOM to emit DOMContentLoaded event'
			});

			// Write to Timer
			timer.getTimeKeeper().setInitialCollectedEvents(events, timers.start);
		}
	}

	public onUnexpectedError(error: any): void {
		let errorMsg = errors.toErrorMessage(error, true);
		if (!errorMsg) {
			return;
		}

		let now = new Date().getTime();
		if (errorMsg === this.previousErrorValue && now - this.previousErrorTime <= 1000) {
			return; // Return if error message identical to previous and shorter than 1 second
		}

		this.previousErrorTime = now;
		this.previousErrorValue = errorMsg;

		// Log to console
		console.error(errorMsg);

		// Show to user if friendly message provided
480
		if (error && error.friendlyMessage && this.messageService) {
B
Benjamin Pasero 已提交
481
			this.messageService.show(Severity.Error, error.friendlyMessage);
E
Erich Gamma 已提交
482 483 484 485 486 487 488 489 490
		}
	}

	public layout(): void {
		let clArea = $(this.container).getClientArea();

		let contentsSize = new Dimension(clArea.width, clArea.height);
		this.contentsContainer.size(contentsSize.width, contentsSize.height);

B
Benjamin Pasero 已提交
491
		this.contextViewService.layout();
E
Erich Gamma 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
		this.workbench.layout();
	}

	public joinCreation(): TPromise<boolean> {
		return this.workbench.joinCreation();
	}

	public dispose(force?: boolean): void {

		// Workbench
		if (this.workbench) {
			let veto = this.workbench.shutdown(force);

			// If Workbench vetos dispose, return early
			if (veto) {
				return;
			}
		}

B
Benjamin Pasero 已提交
511 512
		this.contextViewService.dispose();
		this.storageService.dispose();
E
Erich Gamma 已提交
513 514

		// Listeners
515
		this.toUnbind = disposeAll(this.toUnbind);
E
Erich Gamma 已提交
516 517 518 519 520

		// Container
		$(this.container).empty();
	}
}