workbench.ts 29.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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/workbench';
9

10
import {TPromise, ValueCallback} from 'vs/base/common/winjs.base';
J
Joao Moreno 已提交
11
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
12 13
import strings = require('vs/base/common/strings');
import DOM = require('vs/base/browser/dom');
14
import {Box, Builder, $} from 'vs/base/browser/builder';
E
Erich Gamma 已提交
15 16 17 18 19
import {Delayer} from 'vs/base/common/async';
import assert = require('vs/base/common/assert');
import timer = require('vs/base/common/timer');
import errors = require('vs/base/common/errors');
import {Registry} from 'vs/platform/platform';
M
Martin Aeschlimann 已提交
20
import {Identifiers} from 'vs/workbench/common/constants';
21
import {isWindows, isLinux} from 'vs/base/common/platform';
E
Erich Gamma 已提交
22 23
import {IOptions} from 'vs/workbench/common/options';
import {IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions} from 'vs/workbench/common/contributions';
B
Benjamin Pasero 已提交
24 25
import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor';
import {IEditorRegistry, Extensions as EditorExtensions, TextEditorOptions, EditorInput, EditorOptions} from 'vs/workbench/common/editor';
E
Erich Gamma 已提交
26 27 28
import {Part} from 'vs/workbench/browser/part';
import {HistoryService} from 'vs/workbench/services/history/browser/history';
import {ActivitybarPart} from 'vs/workbench/browser/parts/activitybar/activitybarPart';
B
Benjamin Pasero 已提交
29
import {EditorPart} from 'vs/workbench/browser/parts/editor/editorPart';
E
Erich Gamma 已提交
30
import {SidebarPart} from 'vs/workbench/browser/parts/sidebar/sidebarPart';
I
isidor 已提交
31
import {PanelPart} from 'vs/workbench/browser/parts/panel/panelPart';
E
Erich Gamma 已提交
32 33 34
import {StatusbarPart} from 'vs/workbench/browser/parts/statusbar/statusbarPart';
import {WorkbenchLayout, LayoutOptions} from 'vs/workbench/browser/layout';
import {IActionBarRegistry, Extensions as ActionBarExtensions} from 'vs/workbench/browser/actionBarRegistry';
I
isidor 已提交
35
import {ViewletRegistry, Extensions as ViewletExtensions} from 'vs/workbench/browser/viewlet';
I
isidor 已提交
36
import {PanelRegistry, Extensions as PanelExtensions} from 'vs/workbench/browser/panel';
E
Erich Gamma 已提交
37
import {QuickOpenController} from 'vs/workbench/browser/parts/quickopen/quickOpenController';
38
import {DiffEditorInput, toDiffLabel} from 'vs/workbench/common/editor/diffEditorInput';
E
Erich Gamma 已提交
39
import {getServices} from 'vs/platform/instantiation/common/extensions';
40
import {IUntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
E
Erich Gamma 已提交
41 42 43
import {WorkbenchEditorService} from 'vs/workbench/services/editor/browser/editorService';
import {Position, Parts, IPartService} from 'vs/workbench/services/part/common/partService';
import {IWorkspaceContextService as IWorkbenchWorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService';
M
Martin Aeschlimann 已提交
44
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
45
import {ContextMenuService} from 'vs/workbench/services/contextview/electron-browser/contextmenuService';
A
Alex Dima 已提交
46
import {WorkbenchKeybindingService} from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
A
Alex Dima 已提交
47
import {ContextKeyService} from 'vs/platform/contextkey/browser/contextKeyService';
48
import {IWorkspace} from 'vs/platform/workspace/common/workspace';
49
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
A
Alex Dima 已提交
50
import {ContextKeyExpr, RawContextKey, IContextKeyService, IContextKey} from 'vs/platform/contextkey/common/contextkey';
E
Erich Gamma 已提交
51 52
import {IActivityService} from 'vs/workbench/services/activity/common/activityService';
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
I
isidor 已提交
53
import {IPanelService} from 'vs/workbench/services/panel/common/panelService';
E
Erich Gamma 已提交
54
import {WorkbenchMessageService} from 'vs/workbench/services/message/browser/messageService';
B
Benjamin Pasero 已提交
55
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
56
import {IQuickOpenService} from 'vs/workbench/services/quickopen/common/quickOpenService';
57
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
E
Erich Gamma 已提交
58 59 60
import {IHistoryService} from 'vs/workbench/services/history/common/history';
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
61
import {SyncDescriptor} from 'vs/platform/instantiation/common/descriptors';
62
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
E
Erich Gamma 已提交
63 64
import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle';
import {IMessageService} from 'vs/platform/message/common/message';
65
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
66
import {IStatusbarService} from 'vs/platform/statusbar/common/statusbar';
67
import {IMenuService} from 'vs/platform/actions/common/actions';
J
Johannes Rieken 已提交
68
import {MenuService} from 'vs/platform/actions/common/menuService';
69
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
B
Benjamin Pasero 已提交
70
import {IEnvironmentService} from 'vs/platform/environment/common/environment';
E
Erich Gamma 已提交
71

A
Alex Dima 已提交
72 73
export const MessagesVisibleContext = new RawContextKey<boolean>('globalMessageVisible', false);
export const EditorsVisibleContext = new RawContextKey<boolean>('editorIsOpen', false);
A
Alex Dima 已提交
74
export const NoEditorsVisibleContext:ContextKeyExpr = EditorsVisibleContext.toNegated();
A
Alex Dima 已提交
75

E
Erich Gamma 已提交
76 77 78
interface WorkbenchParams {
	workspace?: IWorkspace;
	options: IOptions;
79
	serviceCollection: ServiceCollection;
E
Erich Gamma 已提交
80 81 82 83
}

export interface IWorkbenchCallbacks {
	onServicesCreated?: () => void;
84
	onWorkbenchStarted?: (customKeybindingsCount: number) => void;
E
Erich Gamma 已提交
85 86 87
}

/**
B
Benjamin Pasero 已提交
88
 * The workbench creates and lays out all parts that make up the workbench.
E
Erich Gamma 已提交
89 90 91 92
 */
export class Workbench implements IPartService {

	private static sidebarPositionSettingKey = 'workbench.sidebar.position';
93
	private static statusbarHiddenSettingKey = 'workbench.statusbar.hidden';
E
Erich Gamma 已提交
94
	private static sidebarHiddenSettingKey = 'workbench.sidebar.hidden';
I
isidor 已提交
95
	private static panelHiddenSettingKey = 'workbench.panel.hidden';
E
Erich Gamma 已提交
96

97
	public _serviceBrand: any;
E
Erich Gamma 已提交
98 99 100 101 102 103 104 105

	private container: HTMLElement;
	private workbenchParams: WorkbenchParams;
	private workbenchContainer: Builder;
	private workbench: Builder;
	private workbenchStarted: boolean;
	private workbenchCreated: boolean;
	private workbenchShutdown: boolean;
106
	private editorService: WorkbenchEditorService;
107
	private contextKeyService: IContextKeyService;
108
	private keybindingService: IKeybindingService;
E
Erich Gamma 已提交
109 110
	private activitybarPart: ActivitybarPart;
	private sidebarPart: SidebarPart;
I
isidor 已提交
111
	private panelPart: PanelPart;
E
Erich Gamma 已提交
112 113 114 115 116 117 118 119 120 121
	private editorPart: EditorPart;
	private statusbarPart: StatusbarPart;
	private quickOpen: QuickOpenController;
	private workbenchLayout: WorkbenchLayout;
	private toDispose: IDisposable[];
	private toShutdown: { shutdown: () => void; }[];
	private callbacks: IWorkbenchCallbacks;
	private creationPromise: TPromise<boolean>;
	private creationPromiseComplete: ValueCallback;
	private sideBarHidden: boolean;
122
	private statusBarHidden: boolean;
E
Erich Gamma 已提交
123
	private sideBarPosition: Position;
I
isidor 已提交
124
	private panelHidden: boolean;
E
Erich Gamma 已提交
125
	private editorBackgroundDelayer: Delayer<void>;
A
Alex Dima 已提交
126 127
	private messagesVisibleContext: IContextKey<boolean>;
	private editorsVisibleContext: IContextKey<boolean>;
128
	private hasFilesToCreateOpenOrDiff: boolean;
E
Erich Gamma 已提交
129

130 131 132 133 134
	constructor(
		container: HTMLElement,
		workspace: IWorkspace,
		options: IOptions,
		serviceCollection: ServiceCollection,
135 136 137 138 139 140 141
		@IInstantiationService private instantiationService: IInstantiationService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
		@IEventService private eventService: IEventService,
		@IWorkbenchWorkspaceContextService private contextService: IWorkbenchWorkspaceContextService,
		@IStorageService private storageService: IStorageService,
		@ILifecycleService private lifecycleService: ILifecycleService,
		@IMessageService private messageService: IMessageService,
B
Benjamin Pasero 已提交
142 143
		@IThreadService private threadService: IThreadService,
		@IEnvironmentService private environmentService: IEnvironmentService
144
	) {
145
		this.container = container;
E
Erich Gamma 已提交
146 147 148

		this.workbenchParams = {
			workspace: workspace,
149
			options,
150
			serviceCollection
E
Erich Gamma 已提交
151 152
		};

153 154
		this.hasFilesToCreateOpenOrDiff = (options.filesToCreate && options.filesToCreate.length > 0) || (options.filesToOpen && options.filesToOpen.length > 0) || (options.filesToDiff && options.filesToDiff.length > 0);

E
Erich Gamma 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		this.toDispose = [];
		this.toShutdown = [];
		this.editorBackgroundDelayer = new Delayer<void>(50);

		this.creationPromise = new TPromise<boolean>((c, e, p) => {
			this.creationPromiseComplete = c;
		});
	}

	/**
	 * Starts the workbench and creates the HTML elements on the container. A workbench can only be started
	 * once. Use the shutdown function to free up resources created by the workbench on startup.
	 */
	public startup(callbacks?: IWorkbenchCallbacks): void {
		assert.ok(!this.workbenchStarted, 'Can not start a workbench that was already started');
		assert.ok(!this.workbenchShutdown, 'Can not start a workbench that was shutdown');

		try {
			this.workbenchStarted = true;
			this.callbacks = callbacks;

			// Create Workbench
			this.createWorkbench();

			// Services
			this.initServices();
			if (this.callbacks && this.callbacks.onServicesCreated) {
				this.callbacks.onServicesCreated();
			}

185
			// Contexts
186 187
			this.messagesVisibleContext = MessagesVisibleContext.bindTo(this.contextKeyService);
			this.editorsVisibleContext = EditorsVisibleContext.bindTo(this.contextKeyService);
188

E
Erich Gamma 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
			// Register Listeners
			this.registerListeners();

			// Settings
			this.initSettings();

			// Create Workbench and Parts
			this.renderWorkbench();

			// Workbench Layout
			this.createWorkbenchLayout();

			// Register Emitters
			this.registerEmitters();

I
isidor 已提交
204
			// Load composits and editors in parallel
B
Benjamin Pasero 已提交
205
			const compositeAndEditorPromises: TPromise<any>[] = [];
E
Erich Gamma 已提交
206

B
Benjamin Pasero 已提交
207
			// Load Viewlet
B
Benjamin Pasero 已提交
208
			const viewletRegistry = (<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets));
209
			let viewletId = viewletRegistry.getDefaultViewletId();
B
Benjamin Pasero 已提交
210
			if (!this.environmentService.isBuilt) {
211
				viewletId = this.storageService.get(SidebarPart.activeViewletSettingsKey, StorageScope.WORKSPACE, viewletRegistry.getDefaultViewletId()); // help developers and restore last view
212 213 214
			}

			if (!this.sideBarHidden && !!viewletId) {
B
Benjamin Pasero 已提交
215
				const viewletTimerEvent = timer.start(timer.Topic.STARTUP, strings.format('Opening Viewlet: {0}', viewletId));
I
isidor 已提交
216
				compositeAndEditorPromises.push(this.sidebarPart.openViewlet(viewletId, false).then(() => viewletTimerEvent.stop()));
E
Erich Gamma 已提交
217 218
			}

B
Benjamin Pasero 已提交
219
			// Load Panel
B
Benjamin Pasero 已提交
220
			const panelRegistry = (<PanelRegistry>Registry.as(PanelExtensions.Panels));
221 222
			const panelId = this.storageService.get(PanelPart.activePanelSettingsKey, StorageScope.WORKSPACE, panelRegistry.getDefaultPanelId());
			if (!this.panelHidden && !!panelId) {
I
isidor 已提交
223
				compositeAndEditorPromises.push(this.panelPart.openPanel(panelId, false));
224 225
			}

B
Benjamin Pasero 已提交
226
			// Load Editors
B
Benjamin Pasero 已提交
227
			const editorTimerEvent = timer.start(timer.Topic.STARTUP, strings.format('Restoring Editor(s)'));
B
Benjamin Pasero 已提交
228 229 230 231 232 233 234
			compositeAndEditorPromises.push(this.resolveEditorsToOpen().then((inputsWithOptions) => {
				let editorOpenPromise: TPromise<BaseEditor[]>;
				if (inputsWithOptions.length) {
					const editors = inputsWithOptions.map((inputWithOptions, index) => {
						return {
							input: inputWithOptions.input,
							options: inputWithOptions.options,
235
							position: Position.LEFT
B
Benjamin Pasero 已提交
236
						};
237 238
					});

B
Benjamin Pasero 已提交
239 240 241
					editorOpenPromise = this.editorPart.openEditors(editors);
				} else {
					editorOpenPromise = this.editorPart.restoreEditors();
242
				}
243 244

				return editorOpenPromise.then(() => {
245
					this.onEditorsChanged(); // make sure we show the proper background in the editor area
E
Erich Gamma 已提交
246 247 248 249 250
					editorTimerEvent.stop();
				});
			}));

			// Flag workbench as created once done
251
			const workbenchDone = (error?: Error) => {
E
Erich Gamma 已提交
252 253 254
				this.workbenchCreated = true;
				this.creationPromiseComplete(true);

B
Benjamin Pasero 已提交
255
				if (this.callbacks && this.callbacks.onWorkbenchStarted) {
256
					this.callbacks.onWorkbenchStarted(this.keybindingService.customKeybindingsCount());
B
Benjamin Pasero 已提交
257
				}
258 259 260 261 262 263

				if (error) {
					errors.onUnexpectedError(error);
				}
			};

B
polish  
Benjamin Pasero 已提交
264
			// Join viewlet, panel and editor promises
265
			TPromise.join(compositeAndEditorPromises).then(() => workbenchDone(), (error) => workbenchDone(error));
E
Erich Gamma 已提交
266 267 268 269 270 271 272 273 274 275
		} catch (error) {

			// Print out error
			console.error(errors.toErrorMessage(error, true));

			// Rethrow
			throw error;
		}
	}

B
Benjamin Pasero 已提交
276 277 278
	private resolveEditorsToOpen(): TPromise<{ input: EditorInput, options?: EditorOptions }[]> {

		// Files to open, diff or create
279 280
		if (this.hasFilesToCreateOpenOrDiff) {
			const wbopt = this.workbenchParams.options;
B
Benjamin Pasero 已提交
281 282 283
			const filesToCreate = wbopt.filesToCreate || [];
			const filesToOpen = wbopt.filesToOpen || [];
			const filesToDiff = wbopt.filesToDiff;
B
Benjamin Pasero 已提交
284 285 286

			// Files to diff is exclusive
			if (filesToDiff && filesToDiff.length) {
287
				return TPromise.join<EditorInput>(filesToDiff.map((resourceInput) => this.editorService.createInput(resourceInput))).then((inputsToDiff) => {
B
Benjamin Pasero 已提交
288 289 290 291 292 293
					return [{ input: new DiffEditorInput(toDiffLabel(filesToDiff[0].resource, filesToDiff[1].resource, this.contextService), null, inputsToDiff[0], inputsToDiff[1]) }];
				});
			}

			// Otherwise: Open/Create files
			else {
B
Benjamin Pasero 已提交
294 295
				const inputs: EditorInput[] = [];
				const options: EditorOptions[] = [];
B
Benjamin Pasero 已提交
296 297 298 299 300 301

				// Files to create
				inputs.push(...filesToCreate.map((resourceInput) => this.untitledEditorService.createOrGet(resourceInput.resource)));
				options.push(...filesToCreate.map(r => null)); // fill empty options for files to create because we dont have options there

				// Files to open
302
				return TPromise.join<EditorInput>(filesToOpen.map((resourceInput) => this.editorService.createInput(resourceInput))).then((inputsToOpen) => {
B
Benjamin Pasero 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
					inputs.push(...inputsToOpen);
					options.push(...filesToOpen.map(resourceInput => TextEditorOptions.from(resourceInput)));

					return inputs.map((input, index) => { return { input, options: options[index] }; });
				});
			}
		}

		// Empty workbench
		else if (!this.workbenchParams.workspace) {
			return TPromise.as([{ input: this.untitledEditorService.createOrGet() }]);
		}

		return TPromise.as([]);
	}

E
Erich Gamma 已提交
319
	private initServices(): void {
320 321
		const {serviceCollection} = this.workbenchParams;

322
		this.toDispose.push(this.lifecycleService.onShutdown(this.shutdownComponents, this));
E
Erich Gamma 已提交
323 324

		// Services we contribute
325
		serviceCollection.set(IPartService, this);
E
Erich Gamma 已提交
326

327 328 329 330 331 332 333
		// Status bar
		this.statusbarPart = this.instantiationService.createInstance(StatusbarPart, Identifiers.STATUSBAR_PART);
		this.toDispose.push(this.statusbarPart);
		this.toShutdown.push(this.statusbarPart);
		serviceCollection.set(IStatusbarService, this.statusbarPart);

		// Keybindings
A
Alex Dima 已提交
334
		this.contextKeyService = this.instantiationService.createInstance(ContextKeyService);
335
		serviceCollection.set(IContextKeyService, this.contextKeyService);
336

A
Alex Dima 已提交
337
		this.keybindingService = this.instantiationService.createInstance(WorkbenchKeybindingService, <any>window);
338
		serviceCollection.set(IKeybindingService, this.keybindingService);
A
Alex Dima 已提交
339

340
		// Context Menu
341
		serviceCollection.set(IContextMenuService, this.instantiationService.createInstance(ContextMenuService));
342

343 344
		// Menus/Actions
		serviceCollection.set(IMenuService, new SyncDescriptor(MenuService));
345

E
Erich Gamma 已提交
346
		// Viewlet service (sidebar part)
347
		this.sidebarPart = this.instantiationService.createInstance(SidebarPart, Identifiers.SIDEBAR_PART);
E
Erich Gamma 已提交
348 349
		this.toDispose.push(this.sidebarPart);
		this.toShutdown.push(this.sidebarPart);
350
		serviceCollection.set(IViewletService, this.sidebarPart);
E
Erich Gamma 已提交
351

I
isidor 已提交
352
		// Panel service (panel part)
353
		this.panelPart = this.instantiationService.createInstance(PanelPart, Identifiers.PANEL_PART);
I
isidor 已提交
354 355
		this.toDispose.push(this.panelPart);
		this.toShutdown.push(this.panelPart);
356
		serviceCollection.set(IPanelService, this.panelPart);
I
isidor 已提交
357

E
Erich Gamma 已提交
358
		// Activity service (activitybar part)
359
		this.activitybarPart = this.instantiationService.createInstance(ActivitybarPart, Identifiers.ACTIVITYBAR_PART);
E
Erich Gamma 已提交
360 361
		this.toDispose.push(this.activitybarPart);
		this.toShutdown.push(this.activitybarPart);
362
		serviceCollection.set(IActivityService, this.activitybarPart);
E
Erich Gamma 已提交
363 364

		// Editor service (editor part)
365
		this.editorPart = this.instantiationService.createInstance(EditorPart, Identifiers.EDITOR_PART, !this.hasFilesToCreateOpenOrDiff);
E
Erich Gamma 已提交
366 367
		this.toDispose.push(this.editorPart);
		this.toShutdown.push(this.editorPart);
368
		this.editorService = this.instantiationService.createInstance(WorkbenchEditorService, this.editorPart);
369
		serviceCollection.set(IWorkbenchEditorService, this.editorService);
370
		serviceCollection.set(IEditorGroupService, this.editorPart);
E
Erich Gamma 已提交
371

372
		// History
373
		serviceCollection.set(IHistoryService, this.instantiationService.createInstance(HistoryService));
374

E
Erich Gamma 已提交
375
		// Quick open service (quick open controller)
376
		this.quickOpen = this.instantiationService.createInstance(QuickOpenController);
E
Erich Gamma 已提交
377 378
		this.toDispose.push(this.quickOpen);
		this.toShutdown.push(this.quickOpen);
379
		serviceCollection.set(IQuickOpenService, this.quickOpen);
E
Erich Gamma 已提交
380

B
polish  
Benjamin Pasero 已提交
381
		// Contributed services
B
Benjamin Pasero 已提交
382
		const contributedServices = getServices();
E
Erich Gamma 已提交
383
		for (let contributedService of contributedServices) {
384
			serviceCollection.set(contributedService.id, contributedService.descriptor);
E
Erich Gamma 已提交
385 386 387 388 389 390 391 392 393 394 395 396
		}

		// Set the some services to registries that have been created eagerly
		<IActionBarRegistry>Registry.as(ActionBarExtensions.Actionbar).setInstantiationService(this.instantiationService);
		<IWorkbenchContributionsRegistry>Registry.as(WorkbenchExtensions.Workbench).setInstantiationService(this.instantiationService);
		<IEditorRegistry>Registry.as(EditorExtensions.Editors).setInstantiationService(this.instantiationService);
	}

	private initSettings(): void {

		// Sidebar visibility
		this.sideBarHidden = this.storageService.getBoolean(Workbench.sidebarHiddenSettingKey, StorageScope.WORKSPACE, false);
B
Benjamin Pasero 已提交
397
		if (!this.contextService.getWorkspace()) {
E
Erich Gamma 已提交
398 399 400
			this.sideBarHidden = true; // we hide sidebar in single-file-mode
		}

B
Benjamin Pasero 已提交
401
		const viewletRegistry = (<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets));
I
isidor 已提交
402
		if (!viewletRegistry.getDefaultViewletId()) {
B
Benjamin Pasero 已提交
403
			this.sideBarHidden = true; // can only hide sidebar if we dont have a default Viewlet id
E
Erich Gamma 已提交
404 405
		}

I
isidor 已提交
406
		// Panel part visibility
B
Benjamin Pasero 已提交
407
		const panelRegistry = (<PanelRegistry>Registry.as(PanelExtensions.Panels));
I
isidor 已提交
408
		this.panelHidden = this.storageService.getBoolean(Workbench.panelHiddenSettingKey, StorageScope.WORKSPACE, true);
B
Benjamin Pasero 已提交
409
		if (!this.contextService.getWorkspace() || !panelRegistry.getDefaultPanelId()) {
410
			this.panelHidden = true; // we hide panel part in single-file-mode or if there is no default panel
I
isidor 已提交
411
		}
I
isidor 已提交
412

E
Erich Gamma 已提交
413
		// Sidebar position
B
Benjamin Pasero 已提交
414
		const rawPosition = this.storageService.get(Workbench.sidebarPositionSettingKey, StorageScope.GLOBAL, 'left');
E
Erich Gamma 已提交
415
		this.sideBarPosition = (rawPosition === 'left') ? Position.LEFT : Position.RIGHT;
B
Benjamin Pasero 已提交
416 417

		// Statusbar visibility
418
		this.statusBarHidden = this.storageService.getBoolean(Workbench.statusbarHiddenSettingKey, StorageScope.GLOBAL, false);
E
Erich Gamma 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	}

	/**
	 * Returns whether the workbench has been started.
	 */
	public isStarted(): boolean {
		return this.workbenchStarted && !this.workbenchShutdown;
	}

	/**
	 * Returns whether the workbench has been fully created.
	 */
	public isCreated(): boolean {
		return this.workbenchCreated && this.workbenchStarted;
	}

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

	public hasFocus(part: Parts): boolean {
B
Benjamin Pasero 已提交
440
		const activeElement = document.activeElement;
E
Erich Gamma 已提交
441 442 443 444 445 446 447 448 449 450 451 452
		if (!activeElement) {
			return false;
		}

		let container: Builder = null;
		switch (part) {
			case Parts.ACTIVITYBAR_PART:
				container = this.activitybarPart.getContainer();
				break;
			case Parts.SIDEBAR_PART:
				container = this.sidebarPart.getContainer();
				break;
I
isidor 已提交
453 454 455
			case Parts.PANEL_PART:
				container = this.panelPart.getContainer();
				break;
E
Erich Gamma 已提交
456 457 458 459 460 461 462 463 464 465 466 467
			case Parts.EDITOR_PART:
				container = this.editorPart.getContainer();
				break;
			case Parts.STATUSBAR_PART:
				container = this.statusbarPart.getContainer();
				break;
		}

		return DOM.isAncestor(activeElement, container.getHTMLElement());
	}

	public isVisible(part: Parts): boolean {
B
Benjamin Pasero 已提交
468 469 470 471 472 473 474
		switch (part) {
			case Parts.SIDEBAR_PART:
				return !this.sideBarHidden;
			case Parts.PANEL_PART:
				return !this.panelHidden;
			case Parts.STATUSBAR_PART:
				return !this.statusBarHidden;
475
		}
E
Erich Gamma 已提交
476 477 478 479

		return true; // any other part cannot be hidden
	}

480 481 482 483 484 485 486 487 488 489 490
	public isStatusBarHidden(): boolean {
		return this.statusBarHidden;
	}

	public setStatusBarHidden(hidden: boolean, skipLayout?: boolean): void {
		this.statusBarHidden = hidden;

		// Layout
		if (!skipLayout) {
			this.workbenchLayout.layout(true);
		}
491

492
		this.storageService.store(Workbench.statusbarHiddenSettingKey, hidden ? 'true' : 'false');
493 494
	}

E
Erich Gamma 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	public isSideBarHidden(): boolean {
		return this.sideBarHidden;
	}

	public setSideBarHidden(hidden: boolean, skipLayout?: boolean): void {
		this.sideBarHidden = hidden;

		// Adjust CSS
		if (hidden) {
			this.workbench.addClass('nosidebar');
		} else {
			this.workbench.removeClass('nosidebar');
		}

		// Layout
		if (!skipLayout) {
			this.workbenchLayout.layout(true);
		}

B
Benjamin Pasero 已提交
514
		// If sidebar becomes hidden, also hide the current active Viewlet if any
E
Erich Gamma 已提交
515 516 517 518
		if (hidden && this.sidebarPart.getActiveViewlet()) {
			this.sidebarPart.hideActiveViewlet();

			// Pass Focus to Editor if Sidebar is now hidden
B
Benjamin Pasero 已提交
519
			const editor = this.editorPart.getActiveEditor();
E
Erich Gamma 已提交
520 521 522 523 524
			if (editor) {
				editor.focus();
			}
		}

B
Benjamin Pasero 已提交
525
		// If sidebar becomes visible, show last active Viewlet or default viewlet
E
Erich Gamma 已提交
526
		else if (!hidden && !this.sidebarPart.getActiveViewlet()) {
B
Benjamin Pasero 已提交
527 528
			const registry = (<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets));
			const viewletToOpen = this.sidebarPart.getLastActiveViewletId() || registry.getDefaultViewletId();
E
Erich Gamma 已提交
529 530 531 532 533 534 535 536 537
			if (viewletToOpen) {
				this.sidebarPart.openViewlet(viewletToOpen, true).done(null, errors.onUnexpectedError);
			}
		}

		// Remember in settings
		this.storageService.store(Workbench.sidebarHiddenSettingKey, hidden ? 'true' : 'false', StorageScope.WORKSPACE);
	}

I
isidor 已提交
538 539
	public isPanelHidden(): boolean {
		return this.panelHidden;
I
isidor 已提交
540 541
	}

B
Benjamin Pasero 已提交
542
	public setPanelHidden(hidden: boolean, skipLayout?: boolean): void {
I
isidor 已提交
543
		this.panelHidden = hidden;
I
isidor 已提交
544 545 546 547 548 549 550 551 552 553 554

		// Layout
		if (!skipLayout) {
			this.workbenchLayout.layout(true);
		}

		// If panel part becomes hidden, also hide the current active panel if any
		if (hidden && this.panelPart.getActivePanel()) {
			this.panelPart.hideActivePanel();

			// Pass Focus to Editor if Panel part is now hidden
B
Benjamin Pasero 已提交
555
			const editor = this.editorPart.getActiveEditor();
I
isidor 已提交
556 557 558 559 560 561 562
			if (editor) {
				editor.focus();
			}
		}

		// If panel part becomes visible, show last active panel or default panel
		else if (!hidden && !this.panelPart.getActivePanel()) {
B
Benjamin Pasero 已提交
563 564
			const registry = (<PanelRegistry>Registry.as(PanelExtensions.Panels));
			const panelToOpen = this.panelPart.getLastActivePanelId() || registry.getDefaultPanelId();
I
isidor 已提交
565 566 567 568 569 570
			if (panelToOpen) {
				this.panelPart.openPanel(panelToOpen, true).done(null, errors.onUnexpectedError);
			}
		}

		// Remember in settings
I
isidor 已提交
571
		this.storageService.store(Workbench.panelHiddenSettingKey, hidden ? 'true' : 'false', StorageScope.WORKSPACE);
I
isidor 已提交
572 573
	}

E
Erich Gamma 已提交
574 575 576 577 578 579 580 581 582
	public getSideBarPosition(): Position {
		return this.sideBarPosition;
	}

	public setSideBarPosition(position: Position): void {
		if (this.sideBarHidden) {
			this.setSideBarHidden(false, true /* Skip Layout */);
		}

B
Benjamin Pasero 已提交
583 584
		const newPositionValue = (position === Position.LEFT) ? 'left' : 'right';
		const oldPositionValue = (this.sideBarPosition === Position.LEFT) ? 'left' : 'right';
E
Erich Gamma 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
		this.sideBarPosition = position;

		// Adjust CSS
		this.activitybarPart.getContainer().removeClass(oldPositionValue);
		this.sidebarPart.getContainer().removeClass(oldPositionValue);
		this.activitybarPart.getContainer().addClass(newPositionValue);
		this.sidebarPart.getContainer().addClass(newPositionValue);

		// Layout
		this.workbenchLayout.layout(true);

		// Remember in settings
		this.storageService.store(Workbench.sidebarPositionSettingKey, position === Position.LEFT ? 'left' : 'right', StorageScope.GLOBAL);
	}

B
Benjamin Pasero 已提交
600
	public dispose(): void {
E
Erich Gamma 已提交
601
		if (this.isStarted()) {
B
Benjamin Pasero 已提交
602
			this.shutdownComponents();
E
Erich Gamma 已提交
603 604 605
			this.workbenchShutdown = true;
		}

J
Joao Moreno 已提交
606
		this.toDispose = dispose(this.toDispose);
E
Erich Gamma 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	}

	/**
	 * Asks the workbench and all its UI components inside to lay out according to
	 * the containers dimension the workbench is living in.
	 */
	public layout(): void {
		if (this.isStarted()) {
			this.workbenchLayout.layout();
		}
	}

	private shutdownComponents(): void {

		// Pass shutdown on to each participant
		this.toShutdown.forEach(s => s.shutdown());
	}

	private registerEmitters(): void {

		// Part Emitters
		this.hookPartListeners(this.activitybarPart);
		this.hookPartListeners(this.editorPart);
		this.hookPartListeners(this.sidebarPart);
I
isidor 已提交
631
		this.hookPartListeners(this.panelPart);
E
Erich Gamma 已提交
632 633 634
	}

	private hookPartListeners(part: Part): void {
A
Alex Dima 已提交
635
		this.toDispose.push(this.eventService.addEmitter2(part, part.getId()));
E
Erich Gamma 已提交
636 637 638 639 640
	}

	private registerListeners(): void {

		// Listen to editor changes
641
		this.toDispose.push(this.editorPart.onEditorsChanged(() => this.onEditorsChanged()));
642 643 644

		// Handle message service and quick open events
		if (this.messageService instanceof WorkbenchMessageService) {
645 646
			this.toDispose.push((<WorkbenchMessageService>this.messageService).onMessagesShowing(() => this.messagesVisibleContext.set(true)));
			this.toDispose.push((<WorkbenchMessageService>this.messageService).onMessagesCleared(() => this.messagesVisibleContext.reset()));
647 648 649 650

			this.toDispose.push(this.quickOpen.onShow(() => (<WorkbenchMessageService>this.messageService).suspend())); // when quick open is open, don't show messages behind
			this.toDispose.push(this.quickOpen.onHide(() => (<WorkbenchMessageService>this.messageService).resume()));  // resume messages once quick open is closed again
		}
E
Erich Gamma 已提交
651 652
	}

653
	private onEditorsChanged(): void {
B
Benjamin Pasero 已提交
654
		const visibleEditors = this.editorService.getVisibleEditors().length;
E
Erich Gamma 已提交
655 656 657 658

		// We update the editorpart class to indicate if an editor is opened or not
		// through a delay to accomodate for fast editor switching

659
		const editorContainer = this.editorPart.getContainer();
E
Erich Gamma 已提交
660
		if (visibleEditors === 0) {
661
			this.editorsVisibleContext.reset();
662
			this.editorBackgroundDelayer.trigger(() => editorContainer.addClass('empty'));
E
Erich Gamma 已提交
663
		} else {
664
			this.editorsVisibleContext.set(true);
665
			this.editorBackgroundDelayer.trigger(() => editorContainer.removeClass('empty'));
E
Erich Gamma 已提交
666 667 668 669
		}
	}

	private createWorkbenchLayout(): void {
B
Benjamin Pasero 已提交
670
		const options = new LayoutOptions();
E
Erich Gamma 已提交
671 672 673 674 675
		options.setMargin(new Box(0, 0, 0, 0));

		this.workbenchLayout = this.instantiationService.createInstance(WorkbenchLayout,
			$(this.container),							// Parent
			this.workbench,								// Workbench Container
I
isidor 已提交
676 677 678 679 680 681 682
			{
				activitybar: this.activitybarPart,		// Activity Bar
				editor: this.editorPart,				// Editor
				sidebar: this.sidebarPart,				// Sidebar
				panel: this.panelPart,					// Panel Part
				statusbar: this.statusbarPart,			// Statusbar
			},
E
Erich Gamma 已提交
683 684 685 686 687 688 689 690 691 692 693
			this.quickOpen,								// Quickopen
			options										// Layout Options
		);

		this.toDispose.push(this.workbenchLayout);
	}

	private createWorkbench(): void {

		// Create Workbench DIV Off-DOM
		this.workbenchContainer = $('.monaco-workbench-container');
694
		this.workbench = $().div({ 'class': 'monaco-workbench ' + (isWindows ? 'windows' : isLinux ? 'linux' : 'mac'), id: Identifiers.WORKBENCH_CONTAINER }).appendTo(this.workbenchContainer);
E
Erich Gamma 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	}

	private renderWorkbench(): void {

		// Apply sidebar state as CSS class
		if (this.sideBarHidden) {
			this.workbench.addClass('nosidebar');
		}

		// Apply no-workspace state as CSS class
		if (!this.workbenchParams.workspace) {
			this.workbench.addClass('no-workspace');
		}

		// Create Parts
		this.createActivityBarPart();
		this.createSidebarPart();
		this.createEditorPart();
713
		this.createPanelPart();
E
Erich Gamma 已提交
714 715 716 717 718 719 720
		this.createStatusbarPart();

		// Add Workbench to DOM
		this.workbenchContainer.build(this.container);
	}

	private createActivityBarPart(): void {
B
Benjamin Pasero 已提交
721
		const activitybarPartContainer = $(this.workbench)
E
Erich Gamma 已提交
722 723
			.div({
				'class': ['part', 'activitybar', this.sideBarPosition === Position.LEFT ? 'left' : 'right'],
724 725
				id: Identifiers.ACTIVITYBAR_PART,
				role: 'navigation'
E
Erich Gamma 已提交
726 727 728 729 730 731
			});

		this.activitybarPart.create(activitybarPartContainer);
	}

	private createSidebarPart(): void {
B
Benjamin Pasero 已提交
732
		const sidebarPartContainer = $(this.workbench)
E
Erich Gamma 已提交
733 734
			.div({
				'class': ['part', 'sidebar', this.sideBarPosition === Position.LEFT ? 'left' : 'right'],
735 736
				id: Identifiers.SIDEBAR_PART,
				role: 'complementary'
E
Erich Gamma 已提交
737 738 739 740 741
			});

		this.sidebarPart.create(sidebarPartContainer);
	}

I
isidor 已提交
742
	private createPanelPart(): void {
B
Benjamin Pasero 已提交
743
		const panelPartContainer = $(this.workbench)
I
isidor 已提交
744
			.div({
745
				'class': ['part', 'panel', 'monaco-editor-background'],
746 747
				id: Identifiers.PANEL_PART,
				role: 'complementary'
I
isidor 已提交
748 749 750 751 752
			});

		this.panelPart.create(panelPartContainer);
	}

E
Erich Gamma 已提交
753
	private createEditorPart(): void {
B
Benjamin Pasero 已提交
754
		const editorContainer = $(this.workbench)
E
Erich Gamma 已提交
755
			.div({
756
				'class': ['part', 'editor', 'monaco-editor-background'],
757 758
				id: Identifiers.EDITOR_PART,
				role: 'main'
E
Erich Gamma 已提交
759 760 761 762 763 764
			});

		this.editorPart.create(editorContainer);
	}

	private createStatusbarPart(): void {
B
Benjamin Pasero 已提交
765
		const statusbarContainer = $(this.workbench).div({
E
Erich Gamma 已提交
766
			'class': ['part', 'statusbar'],
767 768
			id: Identifiers.STATUSBAR_PART,
			role: 'contentinfo'
E
Erich Gamma 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
		});

		this.statusbarPart.create(statusbarContainer);
	}

	public getEditorPart(): EditorPart {
		assert.ok(this.workbenchStarted, 'Workbench is not started. Call startup() first.');

		return this.editorPart;
	}

	public getSidebarPart(): SidebarPart {
		assert.ok(this.workbenchStarted, 'Workbench is not started. Call startup() first.');

		return this.sidebarPart;
	}

I
isidor 已提交
786 787 788 789 790 791
	public getPanelPart(): PanelPart {
		assert.ok(this.workbenchStarted, 'Workbench is not started. Call startup() first.');

		return this.panelPart;
	}

E
Erich Gamma 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
	public getInstantiationService(): IInstantiationService {
		assert.ok(this.workbenchStarted, 'Workbench is not started. Call startup() first.');

		return this.instantiationService;
	}

	public addClass(clazz: string): void {
		if (this.workbench) {
			this.workbench.addClass(clazz);
		}
	}

	public removeClass(clazz: string): void {
		if (this.workbench) {
			this.workbench.removeClass(clazz);
		}
	}
}