shell.ts 22.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*---------------------------------------------------------------------------------------------
 *  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/editor/css/vs-theme';
import 'vs/css!vs/editor/css/vs-dark-theme';
import 'vs/css!vs/editor/css/hc-black-theme';
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';

B
Benjamin Pasero 已提交
17
import {Promise, TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import {Dimension, Builder, $} from 'vs/base/browser/builder';
import objects = require('vs/base/common/objects');
import env = require('vs/base/common/flags');
import dom = require('vs/base/browser/dom');
import Event, { Emitter } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
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';
import {Storage} from 'vs/workbench/browser/storage';
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 已提交
35
import {WorkspaceStats} from 'vs/platform/telemetry/common/workspaceStats';
E
Erich Gamma 已提交
36 37 38 39
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 已提交
40
import {ConfigurationService} from 'vs/workbench/services/configuration/node/configurationService';
E
Erich Gamma 已提交
41 42 43
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';
44
import PluginWorkbenchKeybindingService from 'vs/workbench/services/keybinding/electron-browser/pluginKeybindingService';
E
Erich Gamma 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57
import {MainThreadService} from 'vs/workbench/services/thread/electron-browser/threadService';
import {MarkerService} from 'vs/platform/markers/common/markerService';
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';
import {MainProcessVSCodeAPIHelper} from 'vs/workbench/api/browser/pluginHost.api.impl';
import {MainProcessPluginService} from 'vs/platform/plugins/common/nativePluginService';
import {MainThreadDocuments} from 'vs/workbench/api/common/pluginHostDocuments';
import {MainProcessTextMateSyntax} from 'vs/editor/node/textMate/TMSyntax';
import {MainProcessTextMateSnippet} from 'vs/editor/node/textMate/TMSnippets';
M
Martin Aeschlimann 已提交
58
import {JSONValidationExtensionPoint} from 'vs/platform/jsonschemas/common/jsonValidationExtensionPoint';
E
Erich Gamma 已提交
59 60 61 62 63 64 65
import {LanguageConfigurationFileHandler} from 'vs/editor/node/languageConfiguration';
import {MainThreadFileSystemEventService} from 'vs/workbench/api/common/pluginHostFileSystemEventService';
import {MainThreadQuickOpen} from 'vs/workbench/api/browser/pluginHostQuickOpen';
import {MainThreadStatusBar} from 'vs/workbench/api/browser/pluginHostStatusBar';
import {MainThreadCommands} from 'vs/workbench/api/common/pluginHostCommands';
import {RemoteTelemetryServiceHelper} from 'vs/platform/telemetry/common/abstractRemoteTelemetryService';
import {MainThreadDiagnostics} from 'vs/workbench/api/common/pluginHostDiagnostics';
66
import {MainThreadOutputService} from 'vs/workbench/api/common/extHostOutputService';
E
Erich Gamma 已提交
67 68 69
import {MainThreadMessageService} from 'vs/workbench/api/common/pluginHostMessageService';
import {MainThreadLanguages} from 'vs/workbench/api/common/extHostLanguages';
import {MainThreadEditors} from 'vs/workbench/api/common/pluginHostEditors';
70
import {MainThreadWorkspace} from 'vs/workbench/api/common/pluginHostWorkspace';
E
Erich Gamma 已提交
71
import {MainThreadConfiguration} from 'vs/workbench/api/common/pluginHostConfiguration';
J
Johannes Rieken 已提交
72
import {MainThreadLanguageFeatures} from 'vs/workbench/api/common/extHostLanguageFeatures';
E
Erich Gamma 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
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';
import {create as createInstantiationService } from 'vs/platform/instantiation/common/instantiationService';
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';
import {IMarkerService, IMarkerData} from 'vs/platform/markers/common/markers';
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';
import {IPluginService} from 'vs/platform/plugins/common/plugins';
import {MainThreadModeServiceImpl} from 'vs/editor/common/services/modeServiceImpl';
import {IModeService} from 'vs/editor/common/services/modeService';
import {IUntitledEditorService, UntitledEditorService} from 'vs/workbench/services/untitled/browser/untitledEditorService';
import {CrashReporter} from 'vs/workbench/electron-browser/crashReporter';
import {IThemeService, ThemeService} from 'vs/workbench/services/themes/node/themeService';
import { IServiceCtor, isServiceEvent } from 'vs/base/common/service';
import { connect, Client } from 'vs/base/node/service.net';
import { IExtensionsService } from 'vs/workbench/parts/extensions/common/extensions';
B
Benjamin Pasero 已提交
101
import { ExtensionsService } from 'vs/workbench/parts/extensions/node/extensionsService';
B
Benjamin Pasero 已提交
102
import webFrame = require('web-frame');
E
Erich Gamma 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

/**
 * This ugly beast is needed because at the point when we need shared services
 * in the instantiation service, the connection to the shared process is not yet
 * completed. This create a delayed service wrapper that waits on that connection
 * and then relays all requests to the shared services.
 *
 * TODO@Joao remove
 */
export function getDelayedService<TService>(clientPromise: TPromise<Client>, serviceName: string, serviceCtor: IServiceCtor<TService>): TService {
	let _servicePromise: TPromise<TService>;
	let servicePromise = () => {
		if (!_servicePromise) {
			_servicePromise = clientPromise.then(client => client.getService(serviceName, serviceCtor));
		}
		return _servicePromise;
	};

	return Object.keys(serviceCtor.prototype)
		.filter(key => key !== 'constructor')
		.reduce((result, key) => {
			if (isServiceEvent(serviceCtor.prototype[key])) {
				let promise: Promise;
				let disposable: IDisposable;

				const emitter = new Emitter<any>({
					onFirstListenerAdd: () => {
						promise = servicePromise().then(service => {
							disposable = service[key](e => emitter.fire(e));
						});
					},
					onLastListenerRemove: () => {
						if (disposable) {
							disposable.dispose();
							disposable = null;
						}
						promise.cancel();
						promise = null;
					}
				});

				return objects.assign(result, { [key]: emitter.event });
			}

			return objects.assign(result, {
				[key]: (...args) => {
					return servicePromise().then(service => service[key](...args));
				}
			});
B
Benjamin Pasero 已提交
152
		}, <TService>{});
E
Erich Gamma 已提交
153 154 155 156 157 158 159
}

/**
 * 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 已提交
160 161 162 163 164
	private storageService: IStorageService;
	private messageService: IMessageService;
	private contextViewService: ContextViewService;
	private windowService: IWindowService;
	private threadService: MainThreadService;
E
Erich Gamma 已提交
165
	private themeService: IThemeService;
B
Benjamin Pasero 已提交
166 167 168
	private contextService: WorkspaceContextService;
	private telemetryService: ElectronTelemetryService;
	private keybindingService: PluginWorkbenchKeybindingService;
E
Erich Gamma 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

	private container: HTMLElement;
	private toUnbind: { (): void; }[];
	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 {

		// Workbench Container
		let workbenchContainer = $(parent).div();

		// Instantiation service with services
		let service = this.initInstantiationService();

		//crash reporting
		if (!!this.configuration.env.crashReporter) {
			let crashReporter = service.createInstance(CrashReporter, this.configuration.env.version, this.configuration.env.commitHash);
			crashReporter.start(this.configuration.env.crashReporter);
		}

		const sharedProcessClientPromise = connect(process.env['VSCODE_SHARED_IPC_HOOK']);
		sharedProcessClientPromise.done(null, errors.onUnexpectedError);
		service.addSingleton(IExtensionsService, getDelayedService<IExtensionsService>(sharedProcessClientPromise, 'ExtensionService', ExtensionsService));

		// Workbench
		this.workbench = new Workbench(workbenchContainer.getHTMLElement(), this.workspace, this.configuration, this.options, service);
		this.workbench.startup({
			onServicesCreated: () => {
				this.initPluginSystem();
B
Benjamin Pasero 已提交
217 218 219
			},
			onWorkbenchStarted: () => {
				this.onWorkbenchStarted();
E
Erich Gamma 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
			}
		});

		// 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 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	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,
				autoSaveEnabled: this.contextService.isAutoSaveEnabled && this.contextService.isAutoSaveEnabled(),
				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 已提交
265
	private initInstantiationService(): IInstantiationService {
B
Benjamin Pasero 已提交
266
		let eventService = new EventService();
E
Erich Gamma 已提交
267

B
Benjamin Pasero 已提交
268 269
		this.contextService = new WorkspaceContextService(eventService, this.workspace, this.configuration, this.options);
		this.contextService.getConfiguration().additionalWorkerServices = [
E
Erich Gamma 已提交
270 271 272
			{ serviceId: 'requestService', moduleName: 'vs/workbench/services/request/common/requestService', ctorName: 'WorkerRequestService' }
		];

B
Benjamin Pasero 已提交
273 274
		this.windowService = new WindowService();
		this.storageService = new Storage(this.contextService);
E
Erich Gamma 已提交
275 276 277

		// no telemetry in a window for plugin development!
		let enableTelemetry = this.configuration.env.isBuilt && !this.configuration.env.pluginDevelopmentPath ? !!this.configuration.env.enableTelemetry : false;
B
Benjamin Pasero 已提交
278
		this.telemetryService = new ElectronTelemetryService(this.storageService, { enableTelemetry: enableTelemetry, version: this.configuration.env.version, commitHash: this.configuration.env.commitHash });
E
Erich Gamma 已提交
279

B
Benjamin Pasero 已提交
280
		this.keybindingService = new PluginWorkbenchKeybindingService(this.contextService, eventService, this.telemetryService, <any>window);
E
Erich Gamma 已提交
281

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

B
Benjamin Pasero 已提交
285 286 287
		let configService = new ConfigurationService(
			this.contextService,
			eventService
E
Erich Gamma 已提交
288 289
		);

B
Benjamin Pasero 已提交
290 291 292 293
		let fileService = new FileService(
			configService,
			eventService,
			this.contextService
E
Erich Gamma 已提交
294 295
		);

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

B
Benjamin Pasero 已提交
298 299
		let lifecycleService = new LifecycleService(this.messageService, this.windowService);
		lifecycleService.onShutdown.add(() => fileService.dispose());
E
Erich Gamma 已提交
300

B
Benjamin Pasero 已提交
301 302
		this.threadService = new MainThreadService(this.contextService, this.messageService, this.windowService);
		lifecycleService.onShutdown.add(() => this.threadService.dispose());
E
Erich Gamma 已提交
303

B
Benjamin Pasero 已提交
304 305 306 307
		let requestService = new RequestService(
			this.contextService,
			configService,
			this.telemetryService
E
Erich Gamma 已提交
308
		);
B
Benjamin Pasero 已提交
309 310
		this.threadService.registerInstance(requestService);
		lifecycleService.onShutdown.add(() => requestService.dispose());
E
Erich Gamma 已提交
311

B
Benjamin Pasero 已提交
312
		let markerService = new MarkerService(this.threadService);
E
Erich Gamma 已提交
313

B
Benjamin Pasero 已提交
314 315
		let pluginService = new MainProcessPluginService(this.contextService, this.threadService, this.messageService, this.telemetryService);
		this.keybindingService.setPluginService(pluginService);
E
Erich Gamma 已提交
316

B
Benjamin Pasero 已提交
317 318
		let modelService = new ModelServiceImpl(this.threadService, markerService);
		let modeService = new MainThreadModeServiceImpl(this.threadService, pluginService, modelService);
E
Erich Gamma 已提交
319 320 321 322 323

		let untitledEditorService = new UntitledEditorService();
		this.themeService = new ThemeService(pluginService);

		let result = createInstantiationService();
B
Benjamin Pasero 已提交
324 325 326 327 328 329 330 331 332 333
		result.addSingleton(ITelemetryService, this.telemetryService);
		result.addSingleton(IEventService, eventService);
		result.addSingleton(IRequestService, requestService);
		result.addSingleton(IWorkspaceContextService, this.contextService);
		result.addSingleton(IContextViewService, this.contextViewService);
		result.addSingleton(IContextMenuService, new ContextMenuService(this.messageService, this.telemetryService));
		result.addSingleton(IMessageService, this.messageService);
		result.addSingleton(IStorageService, this.storageService);
		result.addSingleton(ILifecycleService, lifecycleService);
		result.addSingleton(IThreadService, this.threadService);
E
Erich Gamma 已提交
334 335
		result.addSingleton(IPluginService, pluginService);
		result.addSingleton(IModeService, modeService);
B
Benjamin Pasero 已提交
336
		result.addSingleton(IFileService, fileService);
E
Erich Gamma 已提交
337
		result.addSingleton(IUntitledEditorService, untitledEditorService);
B
Benjamin Pasero 已提交
338 339 340 341 342 343
		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 已提交
344 345
		result.addSingleton(ICodeEditorService, new CodeEditorServiceImpl());
		result.addSingleton(IThemeService, this.themeService);
B
Benjamin Pasero 已提交
346
		result.addSingleton(IActionsService, new ActionsService(pluginService, this.keybindingService));
E
Erich Gamma 已提交
347 348 349 350 351 352 353


		return result;
	}

	// TODO@Alex, TODO@Joh move this out of here?
	private initPluginSystem(): void {
B
Benjamin Pasero 已提交
354 355 356
		this.threadService.getRemotable(MainProcessVSCodeAPIHelper);
		this.threadService.getRemotable(MainThreadDocuments);
		this.threadService.getRemotable(RemoteTelemetryServiceHelper);
E
Erich Gamma 已提交
357 358
		this.workbench.getInstantiationService().createInstance(MainProcessTextMateSyntax);
		this.workbench.getInstantiationService().createInstance(MainProcessTextMateSnippet);
M
Martin Aeschlimann 已提交
359
		this.workbench.getInstantiationService().createInstance(JSONValidationExtensionPoint);
E
Erich Gamma 已提交
360
		this.workbench.getInstantiationService().createInstance(LanguageConfigurationFileHandler);
B
Benjamin Pasero 已提交
361 362 363
		this.threadService.getRemotable(MainThreadConfiguration);
		this.threadService.getRemotable(MainThreadQuickOpen);
		this.threadService.getRemotable(MainThreadStatusBar);
E
Erich Gamma 已提交
364
		this.workbench.getInstantiationService().createInstance(MainThreadFileSystemEventService);
B
Benjamin Pasero 已提交
365 366 367 368 369 370 371 372
		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 已提交
373
		this.threadService.getRemotable(MainThreadLanguageFeatures);
E
Erich Gamma 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	}

	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 已提交
402
		let themeId = this.storageService.get(Preferences.THEME, StorageScope.GLOBAL, null);
E
Erich Gamma 已提交
403 404
		if (!themeId) {
			themeId = themes.toId(themes.BaseTheme.VS_DARK);
B
Benjamin Pasero 已提交
405
			this.storageService.store(Preferences.THEME, themeId, StorageScope.GLOBAL);
E
Erich Gamma 已提交
406 407 408 409
		}

		this.setTheme(themeId, false);

B
Benjamin Pasero 已提交
410
		this.toUnbind.push(this.storageService.addListener(StorageEventType.STORAGE, (e: StorageEvent) => {
E
Erich Gamma 已提交
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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
			if (e.key === Preferences.THEME) {
				this.setTheme(e.newValue);
			}
		}));
	}

	private setTheme(themeId: string, layout = true): void {
		if (!themeId) {
			return;
		}
		var applyTheme = () => {
			if (this.currentTheme) {
				$(this.container).removeClass(this.currentTheme);
			}
			this.currentTheme = themeId;
			$(this.container).addClass(this.currentTheme);

			if (layout) {
				this.layout();
			}
		}

		if (!themes.getSyntaxThemeId(themeId)) {
			applyTheme();
		} else {
			this.themeService.getTheme(themeId).then(theme => {
				if (theme) {
					this.themeService.loadThemeCSS(themeId);
					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
B
Benjamin Pasero 已提交
524 525
		if (error.friendlyMessage && this.messageService) {
			this.messageService.show(Severity.Error, error.friendlyMessage);
E
Erich Gamma 已提交
526 527 528 529 530 531 532 533 534
		}
	}

	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 已提交
535
		this.contextViewService.layout();
E
Erich Gamma 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
		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 已提交
555 556
		this.contextViewService.dispose();
		this.storageService.dispose();
E
Erich Gamma 已提交
557 558 559 560 561 562 563 564 565 566

		// Listeners
		while (this.toUnbind.length) {
			this.toUnbind.pop()();
		}

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