workbench.ts 60.1 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 { localize } from 'vs/nls';
11
import { TPromise } from 'vs/base/common/winjs.base';
12
import { IDisposable, dispose, toDisposable, Disposable } from 'vs/base/common/lifecycle';
M
Matt Bierner 已提交
13
import { Event, Emitter } from 'vs/base/common/event';
14
import * as DOM from 'vs/base/browser/dom';
15
import { RunOnceScheduler } from 'vs/base/common/async';
16
import * as browser from 'vs/base/browser/browser';
J
Johannes Rieken 已提交
17
import * as perf from 'vs/base/common/performance';
18
import * as errors from 'vs/base/common/errors';
B
Benjamin Pasero 已提交
19
import { BackupFileService, InMemoryBackupFileService } from 'vs/workbench/services/backup/node/backupFileService';
20
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
21
import { Registry } from 'vs/platform/registry/common/platform';
B
Benjamin Pasero 已提交
22
import { isWindows, isLinux, isMacintosh } from 'vs/base/common/platform';
23
import { IResourceInput } from 'vs/platform/editor/common/editor';
J
Johannes Rieken 已提交
24
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions';
25
import { IEditorInputFactoryRegistry, Extensions as EditorExtensions, TextCompareEditorVisibleContext, TEXT_DIFF_EDITOR_ID, EditorsVisibleContext, InEditorZenModeContext, ActiveEditorGroupEmptyContext, MultipleEditorGroupsContext, IUntitledResourceInput, IResourceDiffInput, SplitEditorsVertically, TextCompareEditorActiveContext } from 'vs/workbench/common/editor';
26
import { HistoryService } from 'vs/workbench/services/history/electron-browser/history';
J
Johannes Rieken 已提交
27 28 29 30
import { ActivitybarPart } from 'vs/workbench/browser/parts/activitybar/activitybarPart';
import { SidebarPart } from 'vs/workbench/browser/parts/sidebar/sidebarPart';
import { PanelPart } from 'vs/workbench/browser/parts/panel/panelPart';
import { StatusbarPart } from 'vs/workbench/browser/parts/statusbar/statusbarPart';
B
Benjamin Pasero 已提交
31
import { TitlebarPart } from 'vs/workbench/browser/parts/titlebar/titlebarPart';
32
import { EditorPart } from 'vs/workbench/browser/parts/editor/editorPart';
B
Benjamin Pasero 已提交
33
import { WorkbenchLayout } from 'vs/workbench/browser/layout';
34
import { IActionBarRegistry, Extensions as ActionBarExtensions } from 'vs/workbench/browser/actions';
J
Johannes Rieken 已提交
35 36
import { PanelRegistry, Extensions as PanelExtensions } from 'vs/workbench/browser/panel';
import { QuickOpenController } from 'vs/workbench/browser/parts/quickopen/quickOpenController';
C
Christof Marti 已提交
37 38
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { QuickInputService } from 'vs/workbench/browser/parts/quickinput/quickInput';
J
Johannes Rieken 已提交
39
import { getServices } from 'vs/platform/instantiation/common/extensions';
40
import { Position, Parts, IPartService, ILayoutOptions, IDimension } from 'vs/workbench/services/part/common/partService';
41
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
J
Johannes Rieken 已提交
42
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
S
SteVen Batten 已提交
43 44
import { ContextMenuService as NativeContextMenuService } from 'vs/workbench/services/contextview/electron-browser/contextmenuService';
import { ContextMenuService as HTMLContextMenuService } from 'vs/platform/contextview/browser/contextMenuService';
J
Johannes Rieken 已提交
45 46
import { WorkbenchKeybindingService } from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
47
import { WorkspaceService, DefaultConfigurationExportHelper } from 'vs/workbench/services/configuration/node/configurationService';
48 49
import { IJSONEditingService } from 'vs/workbench/services/configuration/common/jsonEditing';
import { JSONEditingService } from 'vs/workbench/services/configuration/node/jsonEditingService';
J
Johannes Rieken 已提交
50
import { ContextKeyService } from 'vs/platform/contextkey/browser/contextKeyService';
51 52
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IKeybindingEditingService, KeybindingsEditingService } from 'vs/workbench/services/keybinding/common/keybindingEditing';
53
import { RawContextKey, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
I
isidor 已提交
54
import { IActivityService } from 'vs/workbench/services/activity/common/activity';
B
Benjamin Pasero 已提交
55
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
P
Pine Wu 已提交
56
import { ViewletService } from 'vs/workbench/services/viewlet/browser/viewletService';
57
import { RemoteFileService } from 'vs/workbench/services/files/electron-browser/remoteFileService';
J
Johannes Rieken 已提交
58 59
import { IFileService } from 'vs/platform/files/common/files';
import { IConfigurationResolverService } from 'vs/workbench/services/configurationResolver/common/configurationResolver';
60
import { ConfigurationResolverService } from 'vs/workbench/services/configurationResolver/electron-browser/configurationResolverService';
J
Johannes Rieken 已提交
61
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
B
Benjamin Pasero 已提交
62
import { ITitleService } from 'vs/workbench/services/title/common/titleService';
J
Johannes Rieken 已提交
63
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
64 65
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { ClipboardService } from 'vs/platform/clipboard/electron-browser/clipboardService';
J
Johannes Rieken 已提交
66 67 68 69 70
import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { TextFileService } from 'vs/workbench/services/textfile/electron-browser/textFileService';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
J
Joao Moreno 已提交
71 72
import { ISCMService } from 'vs/workbench/services/scm/common/scm';
import { SCMService } from 'vs/workbench/services/scm/common/scmService';
73
import { IProgressService2 } from 'vs/workbench/services/progress/common/progress';
74
import { ProgressService2 } from 'vs/workbench/services/progress/browser/progressService2';
75
import { TextModelResolverService } from 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
76
import { ITextModelService } from 'vs/editor/common/services/resolverService';
J
Johannes Rieken 已提交
77
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
B
Benjamin Pasero 已提交
78
import { ShutdownReason } from 'vs/platform/lifecycle/common/lifecycle';
79
import { LifecycleService } from 'vs/platform/lifecycle/electron-browser/lifecycleService';
80
import { IWindowService, IWindowConfiguration as IWindowSettings, IWindowConfiguration, IPath, MenuBarVisibility } from 'vs/platform/windows/common/windows';
J
Johannes Rieken 已提交
81
import { IStatusbarService } from 'vs/platform/statusbar/common/statusbar';
82
import { IMenuService, SyncActionDescriptor } from 'vs/platform/actions/common/actions';
83
import { MenuService } from 'vs/workbench/services/actions/common/menuService';
S
SteVen Batten 已提交
84
import { IContextMenuService, IContextViewService } from 'vs/platform/contextview/browser/contextView';
J
Johannes Rieken 已提交
85
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
B
Benjamin Pasero 已提交
86
import { IWorkbenchActionRegistry, Extensions } from 'vs/workbench/common/actions';
87
import { OpenRecentAction, ToggleDevToolsAction, ReloadWindowAction, ShowPreviousWindowTab, MoveWindowTabToNewWindow, MergeAllWindowTabs, ShowNextWindowTab, ToggleWindowTabsBar, ReloadWindowWithExtensionsDisabledAction, NewWindowTab } from 'vs/workbench/electron-browser/actions';
88
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
89
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
B
Benjamin Pasero 已提交
90
import { WorkspaceEditingService } from 'vs/workbench/services/workspace/node/workspaceEditingService';
91
import { FileDecorationsService } from 'vs/workbench/services/decorations/browser/decorationsService';
J
Johannes Rieken 已提交
92
import { IDecorationsService } from 'vs/workbench/services/decorations/browser/decorations';
I
isidor 已提交
93
import { ActivityService } from 'vs/workbench/services/activity/browser/activityService';
B
Benjamin Pasero 已提交
94
import URI from 'vs/base/common/uri';
J
Joao Moreno 已提交
95
import { IListService, ListService } from 'vs/platform/list/browser/listService';
J
Joao Moreno 已提交
96
import { InputFocusedContext } from 'vs/platform/workbench/common/contextkeys';
97
import { IViewsService } from 'vs/workbench/common/views';
S
Sandeep Somavarapu 已提交
98
import { ViewsService } from 'vs/workbench/browser/parts/views/views';
99 100
import { INotificationService } from 'vs/platform/notification/common/notification';
import { NotificationService } from 'vs/workbench/services/notification/common/notificationService';
101 102
import { NotificationsCenter } from 'vs/workbench/browser/parts/notifications/notificationsCenter';
import { NotificationsAlerts } from 'vs/workbench/browser/parts/notifications/notificationsAlerts';
103
import { NotificationsStatus } from 'vs/workbench/browser/parts/notifications/notificationsStatus';
104
import { registerNotificationCommands } from 'vs/workbench/browser/parts/notifications/notificationsCommands';
105
import { NotificationsToasts } from 'vs/workbench/browser/parts/notifications/notificationsToasts';
J
Joao Moreno 已提交
106 107
import { IPCClient } from 'vs/base/parts/ipc/common/ipc';
import { registerWindowDriver } from 'vs/platform/driver/electron-browser/driver';
S
Sandeep Somavarapu 已提交
108 109
import { IPreferencesService } from 'vs/workbench/services/preferences/common/preferences';
import { PreferencesService } from 'vs/workbench/services/preferences/browser/preferencesService';
110
import { IEditorService, IResourceEditor } from 'vs/workbench/services/editor/common/editorService';
I
isidor 已提交
111
import { IEditorGroupsService, GroupDirection, preferredSideBySideGroupDirection } from 'vs/workbench/services/group/common/editorGroupsService';
112
import { EditorService } from 'vs/workbench/services/editor/browser/editorService';
J
Joao Moreno 已提交
113
import { IExtensionUrlHandler, ExtensionUrlHandler } from 'vs/platform/url/electron-browser/inactiveExtensionUrlHandler';
S
SteVen Batten 已提交
114
import { ContextViewService } from 'vs/platform/contextview/browser/contextViewService';
S
SteVen Batten 已提交
115 116
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { TelemetryService } from 'vs/platform/telemetry/common/telemetryService';
A
Alex Dima 已提交
117 118
import { WorkbenchThemeService } from 'vs/workbench/services/themes/electron-browser/workbenchThemeService';
import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService';
I
isidor 已提交
119
import { IUriLabelService, UriLabelService } from 'vs/platform/uriLabel/common/uriLabel';
120

E
Erich Gamma 已提交
121
interface WorkbenchParams {
122
	configuration: IWindowConfiguration;
123
	serviceCollection: ServiceCollection;
E
Erich Gamma 已提交
124 125
}

I
isidor 已提交
126 127
interface IZenModeSettings {
	fullScreen: boolean;
128
	centerLayout: boolean;
I
isidor 已提交
129
	hideTabs: boolean;
130
	hideActivityBar: boolean;
I
isidor 已提交
131
	hideStatusBar: boolean;
I
isidor 已提交
132
	restore: boolean;
I
isidor 已提交
133 134
}

135 136 137
export interface IWorkbenchStartedInfo {
	customKeybindingsCount: number;
	pinnedViewlets: string[];
138
	restoredViewlet: string;
139
	restoredEditorsCount: number;
140 141
}

142 143
type FontAliasingOption = 'default' | 'antialiased' | 'none' | 'auto';

B
Benjamin Pasero 已提交
144 145
const fontAliasingValues: FontAliasingOption[] = ['antialiased', 'none', 'auto'];

146 147
const Identifiers = {
	WORKBENCH_CONTAINER: 'workbench.main.container',
B
Benjamin Pasero 已提交
148
	TITLEBAR_PART: 'workbench.parts.titlebar',
149 150 151 152
	ACTIVITYBAR_PART: 'workbench.parts.activitybar',
	SIDEBAR_PART: 'workbench.parts.sidebar',
	PANEL_PART: 'workbench.parts.panel',
	EDITOR_PART: 'workbench.parts.editor',
S
SteVen Batten 已提交
153
	STATUSBAR_PART: 'workbench.parts.statusbar'
154 155
};

J
Joao Moreno 已提交
156 157 158 159 160 161 162 163
function getWorkbenchStateString(state: WorkbenchState): string {
	switch (state) {
		case WorkbenchState.EMPTY: return 'empty';
		case WorkbenchState.FOLDER: return 'folder';
		case WorkbenchState.WORKSPACE: return 'workspace';
	}
}

164 165 166 167 168 169 170 171 172 173
interface IZenMode {
	active: boolean;
	transitionedToFullScreen: boolean;
	transitionedToCenteredEditorLayout: boolean;
	transitionDisposeables: IDisposable[];
	wasSideBarVisible: boolean;
	wasPanelVisible: boolean;
}

export class Workbench extends Disposable implements IPartService {
E
Erich Gamma 已提交
174

175
	private static readonly sidebarHiddenStorageKey = 'workbench.sidebar.hidden';
176
	private static readonly menubarVisibilityConfigurationKey = 'window.menuBarVisibility';
177 178 179
	private static readonly sidebarRestoreStorageKey = 'workbench.sidebar.restore';
	private static readonly panelHiddenStorageKey = 'workbench.panel.hidden';
	private static readonly zenModeActiveStorageKey = 'workbench.zenmode.active';
180
	private static readonly centeredEditorLayoutActiveStorageKey = 'workbench.centerededitorlayout.active';
181
	private static readonly panelPositionStorageKey = 'workbench.panel.location';
182
	private static readonly defaultPanelPositionStorageKey = 'workbench.panel.defaultLocation';
183 184 185 186 187
	private static readonly sidebarPositionConfigurationKey = 'workbench.sideBar.location';
	private static readonly statusbarVisibleConfigurationKey = 'workbench.statusBar.visible';
	private static readonly activityBarVisibleConfigurationKey = 'workbench.activityBar.visible';
	private static readonly closeWhenEmptyConfigurationKey = 'window.closeWhenEmpty';
	private static readonly fontAliasingConfigurationKey = 'workbench.fontAliasing';
188

189
	_serviceBrand: any;
190

E
Erich Gamma 已提交
191
	private workbenchParams: WorkbenchParams;
B
Benjamin Pasero 已提交
192
	private workbench: HTMLElement;
E
Erich Gamma 已提交
193 194 195
	private workbenchStarted: boolean;
	private workbenchCreated: boolean;
	private workbenchShutdown: boolean;
196

197 198
	private editorService: EditorService;
	private editorGroupService: IEditorGroupsService;
P
Pine Wu 已提交
199
	private viewletService: IViewletService;
J
Joao Moreno 已提交
200
	private contextViewService: ContextViewService;
201
	private contextKeyService: IContextKeyService;
202
	private keybindingService: IKeybindingService;
203
	private backupFileService: IBackupFileService;
204
	private fileService: IFileService;
205 206 207 208
	private quickInput: QuickInputService;

	private workbenchLayout: WorkbenchLayout;

B
Benjamin Pasero 已提交
209
	private titlebarPart: TitlebarPart;
E
Erich Gamma 已提交
210 211
	private activitybarPart: ActivitybarPart;
	private sidebarPart: SidebarPart;
I
isidor 已提交
212
	private panelPart: PanelPart;
E
Erich Gamma 已提交
213 214 215
	private editorPart: EditorPart;
	private statusbarPart: StatusbarPart;
	private quickOpen: QuickOpenController;
216
	private notificationsCenter: NotificationsCenter;
217
	private notificationsToasts: NotificationsToasts;
218

E
Erich Gamma 已提交
219
	private sideBarHidden: boolean;
220
	private statusBarHidden: boolean;
S
Sanders Lauture 已提交
221
	private activityBarHidden: boolean;
222
	private menubarToggled: boolean;
E
Erich Gamma 已提交
223
	private sideBarPosition: Position;
I
isidor 已提交
224
	private panelPosition: Position;
I
isidor 已提交
225
	private panelHidden: boolean;
S
SteVen Batten 已提交
226
	private menubarVisibility: MenuBarVisibility;
227 228
	private zenMode: IZenMode;
	private fontAliasing: FontAliasingOption;
229
	private hasInitialFilesToOpen: boolean;
230

I
isidor 已提交
231
	private inZenMode: IContextKey<boolean>;
232
	private sideBarVisibleContext: IContextKey<boolean>;
233

B
Benjamin Pasero 已提交
234
	private closeEmptyWindowScheduler: RunOnceScheduler = this._register(new RunOnceScheduler(() => this.onAllEditorsClosed(), 50));
E
Erich Gamma 已提交
235

236
	constructor(
237
		private container: HTMLElement,
J
Joao Moreno 已提交
238
		private configuration: IWindowConfiguration,
239
		serviceCollection: ServiceCollection,
240
		private lifecycleService: LifecycleService,
J
Joao Moreno 已提交
241
		private mainProcessClient: IPCClient,
242
		@IInstantiationService private instantiationService: IInstantiationService,
243
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
244
		@IStorageService private storageService: IStorageService,
245
		@IConfigurationService private configurationService: WorkspaceService,
A
Alex Dima 已提交
246
		@IWorkbenchThemeService private themeService: WorkbenchThemeService,
I
isidor 已提交
247
		@IEnvironmentService private environmentService: IEnvironmentService,
248
		@IWindowService private windowService: IWindowService,
S
SteVen Batten 已提交
249
		@INotificationService private notificationService: NotificationService,
S
SteVen Batten 已提交
250
		@ITelemetryService private telemetryService: TelemetryService
251
	) {
252
		super();
E
Erich Gamma 已提交
253

254
		this.workbenchParams = { configuration, serviceCollection };
E
Erich Gamma 已提交
255

256
		this.hasInitialFilesToOpen =
B
Benjamin Pasero 已提交
257 258 259
			(configuration.filesToCreate && configuration.filesToCreate.length > 0) ||
			(configuration.filesToOpen && configuration.filesToOpen.length > 0) ||
			(configuration.filesToDiff && configuration.filesToDiff.length > 0);
260 261
	}

262
	startup(): TPromise<IWorkbenchStartedInfo> {
263
		this.workbenchStarted = true;
E
Erich Gamma 已提交
264

B
Benjamin Pasero 已提交
265
		// Create Workbench Container
266
		this.createWorkbench();
E
Erich Gamma 已提交
267

268 269
		// Install some global actions
		this.createGlobalActions();
270

271 272
		// Services
		this.initServices();
E
Erich Gamma 已提交
273

274 275
		// Context Keys
		this.handleContextKeys();
J
Joao Moreno 已提交
276

277 278
		// Register Listeners
		this.registerListeners();
E
Erich Gamma 已提交
279

280 281
		// Settings
		this.initSettings();
E
Erich Gamma 已提交
282

283 284
		// Create Workbench and Parts
		this.renderWorkbench();
285

286 287
		// Workbench Layout
		this.createWorkbenchLayout();
B
polish  
Benjamin Pasero 已提交
288

J
Joao Moreno 已提交
289 290
		// Driver
		if (this.environmentService.driverHandle) {
291
			registerWindowDriver(this.mainProcessClient, this.configuration.windowId, this.instantiationService).then(disposable => this._register(disposable));
J
Joao Moreno 已提交
292 293
		}

294
		// Restore Parts
B
Benjamin Pasero 已提交
295 296
		return this.restoreParts();
	}
297

B
Benjamin Pasero 已提交
298
	private createWorkbench(): void {
B
Benjamin Pasero 已提交
299 300 301
		this.workbench = document.createElement('div');
		this.workbench.id = Identifiers.WORKBENCH_CONTAINER;
		DOM.addClasses(this.workbench, 'monaco-workbench', isWindows ? 'windows' : isLinux ? 'linux' : 'mac');
S
SteVen Batten 已提交
302

B
Benjamin Pasero 已提交
303 304 305
		this._register(DOM.addDisposableListener(this.workbench, DOM.EventType.SCROLL, () => {
			this.workbench.scrollTop = 0; // Prevent workbench from scrolling #55456
		}));
306
	}
E
Erich Gamma 已提交
307

308
	private createGlobalActions(): void {
B
Benjamin Pasero 已提交
309
		const isDeveloping = !this.environmentService.isBuilt || this.environmentService.isExtensionDevelopment;
310

B
Benjamin Pasero 已提交
311
		// Actions registered here to adjust for developing vs built workbench
312 313 314 315 316
		const registry = Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions);
		registry.registerWorkbenchAction(new SyncActionDescriptor(ReloadWindowAction, ReloadWindowAction.ID, ReloadWindowAction.LABEL, isDeveloping ? { primary: KeyMod.CtrlCmd | KeyCode.KEY_R } : void 0), 'Reload Window');
		registry.registerWorkbenchAction(new SyncActionDescriptor(ToggleDevToolsAction, ToggleDevToolsAction.ID, ToggleDevToolsAction.LABEL, isDeveloping ? { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_I, mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_I } } : void 0), 'Developer: Toggle Developer Tools', localize('developer', "Developer"));
		registry.registerWorkbenchAction(new SyncActionDescriptor(OpenRecentAction, OpenRecentAction.ID, OpenRecentAction.LABEL, { primary: isDeveloping ? null : KeyMod.CtrlCmd | KeyCode.KEY_R, mac: { primary: KeyMod.WinCtrl | KeyCode.KEY_R } }), 'File: Open Recent...', localize('file', "File"));
		registry.registerWorkbenchAction(new SyncActionDescriptor(ReloadWindowWithExtensionsDisabledAction, ReloadWindowWithExtensionsDisabledAction.ID, ReloadWindowWithExtensionsDisabledAction.LABEL), 'Reload Window Without Extensions');
317

318
		// Actions for macOS native tabs management (only when enabled)
319
		const windowConfig = this.configurationService.getValue<IWindowConfiguration>();
320
		if (windowConfig && windowConfig.window && windowConfig.window.nativeTabs) {
321
			registry.registerWorkbenchAction(new SyncActionDescriptor(NewWindowTab, NewWindowTab.ID, NewWindowTab.LABEL), 'New Window Tab');
322 323 324 325 326
			registry.registerWorkbenchAction(new SyncActionDescriptor(ShowPreviousWindowTab, ShowPreviousWindowTab.ID, ShowPreviousWindowTab.LABEL), 'Show Previous Window Tab');
			registry.registerWorkbenchAction(new SyncActionDescriptor(ShowNextWindowTab, ShowNextWindowTab.ID, ShowNextWindowTab.LABEL), 'Show Next Window Tab');
			registry.registerWorkbenchAction(new SyncActionDescriptor(MoveWindowTabToNewWindow, MoveWindowTabToNewWindow.ID, MoveWindowTabToNewWindow.LABEL), 'Move Window Tab to New Window');
			registry.registerWorkbenchAction(new SyncActionDescriptor(MergeAllWindowTabs, MergeAllWindowTabs.ID, MergeAllWindowTabs.LABEL), 'Merge All Windows');
			registry.registerWorkbenchAction(new SyncActionDescriptor(ToggleWindowTabsBar, ToggleWindowTabsBar.ID, ToggleWindowTabsBar.LABEL), 'Toggle Window Tabs Bar');
327
		}
328 329
	}

E
Erich Gamma 已提交
330
	private initServices(): void {
B
Benjamin Pasero 已提交
331
		const { serviceCollection } = this.workbenchParams;
332

E
Erich Gamma 已提交
333
		// Services we contribute
334
		serviceCollection.set(IPartService, this);
E
Erich Gamma 已提交
335

336 337 338
		// Clipboard
		serviceCollection.set(IClipboardService, new ClipboardService());

339
		// Uri Display
I
isidor 已提交
340 341 342
		const uriLabelService = new UriLabelService(this.environmentService, this.contextService);
		serviceCollection.set(IUriLabelService, uriLabelService);
		this.configurationService.acquireUriLabelService(uriLabelService);
343

344 345
		// Status bar
		this.statusbarPart = this.instantiationService.createInstance(StatusbarPart, Identifiers.STATUSBAR_PART);
346
		this._register(toDisposable(() => this.statusbarPart.shutdown()));
347 348
		serviceCollection.set(IStatusbarService, this.statusbarPart);

349 350 351
		// Progress 2
		serviceCollection.set(IProgressService2, new SyncDescriptor(ProgressService2));

352
		// Keybindings
A
Alex Dima 已提交
353
		this.contextKeyService = this.instantiationService.createInstance(ContextKeyService);
354
		serviceCollection.set(IContextKeyService, this.contextKeyService);
355

B
Benjamin Pasero 已提交
356
		this.keybindingService = this.instantiationService.createInstance(WorkbenchKeybindingService, window);
357
		serviceCollection.set(IKeybindingService, this.keybindingService);
A
Alex Dima 已提交
358

359 360 361
		// List
		serviceCollection.set(IListService, this.instantiationService.createInstance(ListService));

J
Joao Moreno 已提交
362
		// Context view service
B
Benjamin Pasero 已提交
363
		this.contextViewService = this.instantiationService.createInstance(ContextViewService, this.workbench);
J
Joao Moreno 已提交
364 365
		serviceCollection.set(IContextViewService, this.contextViewService);

S
SteVen Batten 已提交
366
		// Use themable context menus when custom titlebar is enabled to match custom menubar
B
Benjamin Pasero 已提交
367
		if (!isMacintosh && this.getCustomTitleBarStyle() === 'custom') {
S
SteVen Batten 已提交
368
			serviceCollection.set(IContextMenuService, new SyncDescriptor(HTMLContextMenuService, null, this.telemetryService, this.notificationService, this.contextViewService));
S
SteVen Batten 已提交
369 370 371
		} else {
			serviceCollection.set(IContextMenuService, new SyncDescriptor(NativeContextMenuService));
		}
372

373 374
		// Menus/Actions
		serviceCollection.set(IMenuService, new SyncDescriptor(MenuService));
375

P
Pine Wu 已提交
376
		// Sidebar part
377
		this.sidebarPart = this.instantiationService.createInstance(SidebarPart, Identifiers.SIDEBAR_PART);
378
		this._register(toDisposable(() => this.sidebarPart.shutdown()));
P
Pine Wu 已提交
379 380 381 382

		// Viewlet service
		this.viewletService = this.instantiationService.createInstance(ViewletService, this.sidebarPart);
		serviceCollection.set(IViewletService, this.viewletService);
E
Erich Gamma 已提交
383

I
isidor 已提交
384
		// Panel service (panel part)
385
		this.panelPart = this.instantiationService.createInstance(PanelPart, Identifiers.PANEL_PART);
386
		this._register(toDisposable(() => this.panelPart.shutdown()));
387
		serviceCollection.set(IPanelService, this.panelPart);
I
isidor 已提交
388

389 390 391
		// views service
		const viewsService = this.instantiationService.createInstance(ViewsService);
		serviceCollection.set(IViewsService, viewsService);
S
Sandeep Somavarapu 已提交
392

E
Erich Gamma 已提交
393
		// Activity service (activitybar part)
394
		this.activitybarPart = this.instantiationService.createInstance(ActivitybarPart, Identifiers.ACTIVITYBAR_PART);
395
		this._register(toDisposable(() => this.activitybarPart.shutdown()));
I
isidor 已提交
396 397
		const activityService = this.instantiationService.createInstance(ActivityService, this.activitybarPart, this.panelPart);
		serviceCollection.set(IActivityService, activityService);
E
Erich Gamma 已提交
398

399 400 401
		// File Service
		this.fileService = this.instantiationService.createInstance(RemoteFileService);
		serviceCollection.set(IFileService, this.fileService);
S
#47154  
Sandeep Somavarapu 已提交
402
		this.configurationService.acquireFileService(this.fileService);
A
Alex Dima 已提交
403
		this.themeService.acquireFileService(this.fileService);
404

405 406
		// Editor and Group services
		const restorePreviousEditorState = !this.hasInitialFilesToOpen;
407
		this.editorPart = this.instantiationService.createInstance(EditorPart, Identifiers.EDITOR_PART, restorePreviousEditorState);
408
		this._register(toDisposable(() => this.editorPart.shutdown()));
B
Benjamin Pasero 已提交
409
		this.editorGroupService = this.editorPart;
410 411 412
		serviceCollection.set(IEditorGroupsService, this.editorPart);
		this.editorService = this.instantiationService.createInstance(EditorService);
		serviceCollection.set(IEditorService, this.editorService);
E
Erich Gamma 已提交
413

414 415
		// Title bar
		this.titlebarPart = this.instantiationService.createInstance(TitlebarPart, Identifiers.TITLEBAR_PART);
416
		this._register(toDisposable(() => this.titlebarPart.shutdown()));
417
		serviceCollection.set(ITitleService, this.titlebarPart);
B
Benjamin Pasero 已提交
418

419
		// History
420
		serviceCollection.set(IHistoryService, new SyncDescriptor(HistoryService));
421

422
		// Backup File Service
B
Benjamin Pasero 已提交
423 424 425 426 427
		if (this.workbenchParams.configuration.backupPath) {
			this.backupFileService = this.instantiationService.createInstance(BackupFileService, this.workbenchParams.configuration.backupPath);
		} else {
			this.backupFileService = new InMemoryBackupFileService();
		}
428
		serviceCollection.set(IBackupFileService, this.backupFileService);
429

430
		// Text File Service
431
		serviceCollection.set(ITextFileService, new SyncDescriptor(TextFileService));
432

433
		// File Decorations
J
Johannes Rieken 已提交
434
		serviceCollection.set(IDecorationsService, new SyncDescriptor(FileDecorationsService));
435

J
Joao Moreno 已提交
436
		// SCM Service
437
		serviceCollection.set(ISCMService, new SyncDescriptor(SCMService));
438

J
Joao Moreno 已提交
439
		// Inactive extension URL handler
J
Joao Moreno 已提交
440
		serviceCollection.set(IExtensionUrlHandler, new SyncDescriptor(ExtensionUrlHandler));
J
Joao Moreno 已提交
441

B
Benjamin Pasero 已提交
442
		// Text Model Resolver Service
443
		serviceCollection.set(ITextModelService, new SyncDescriptor(TextModelResolverService));
B
Benjamin Pasero 已提交
444

445 446 447 448
		// JSON Editing
		const jsonEditingService = this.instantiationService.createInstance(JSONEditingService);
		serviceCollection.set(IJSONEditingService, jsonEditingService);

449 450 451
		// Workspace Editing
		serviceCollection.set(IWorkspaceEditingService, new SyncDescriptor(WorkspaceEditingService));

452 453 454
		// Keybinding Editing
		serviceCollection.set(IKeybindingEditingService, this.instantiationService.createInstance(KeybindingsEditingService));

455
		// Configuration Resolver
B
Benjamin Pasero 已提交
456
		serviceCollection.set(IConfigurationResolverService, new SyncDescriptor(ConfigurationResolverService, process.env));
457

E
Erich Gamma 已提交
458
		// Quick open service (quick open controller)
459
		this.quickOpen = this.instantiationService.createInstance(QuickOpenController);
460
		this._register(toDisposable(() => this.quickOpen.shutdown()));
461
		serviceCollection.set(IQuickOpenService, this.quickOpen);
462

C
Christof Marti 已提交
463 464
		// Quick input service
		this.quickInput = this.instantiationService.createInstance(QuickInputService);
465
		this._register(toDisposable(() => this.quickInput.shutdown()));
C
Christof Marti 已提交
466 467
		serviceCollection.set(IQuickInputService, this.quickInput);

S
Sandeep Somavarapu 已提交
468 469 470
		// PreferencesService
		serviceCollection.set(IPreferencesService, this.instantiationService.createInstance(PreferencesService));

B
polish  
Benjamin Pasero 已提交
471
		// Contributed services
B
Benjamin Pasero 已提交
472
		const contributedServices = getServices();
E
Erich Gamma 已提交
473
		for (let contributedService of contributedServices) {
474
			serviceCollection.set(contributedService.id, contributedService.descriptor);
E
Erich Gamma 已提交
475 476 477
		}

		// Set the some services to registries that have been created eagerly
478
		Registry.as<IActionBarRegistry>(ActionBarExtensions.Actionbar).setInstantiationService(this.instantiationService);
479
		Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).start(this.instantiationService, this.lifecycleService);
480
		Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories).setInstantiationService(this.instantiationService);
481

482
		this.instantiationService.createInstance(DefaultConfigurationExportHelper);
483

S
#47154  
Sandeep Somavarapu 已提交
484
		this.configurationService.acquireInstantiationService(this.getInstantiationService());
E
Erich Gamma 已提交
485 486
	}

487
	//#region event handling
E
Erich Gamma 已提交
488

489
	private registerListeners(): void {
I
isidor 已提交
490

491
		// Listen to visible editor changes
492
		this._register(this.editorService.onDidVisibleEditorsChange(() => this.onDidVisibleEditorsChange()));
B
Benjamin Pasero 已提交
493

494 495 496
		// Listen to editor closing (if we run with --wait)
		const filesToWait = this.workbenchParams.configuration.filesToWait;
		if (filesToWait) {
497
			const resourcesToWaitFor = filesToWait.paths.map(p => p.fileUri);
498
			const waitMarkerFile = URI.file(filesToWait.waitMarkerFilePath);
499
			const listenerDispose = this.editorService.onDidCloseEditor(() => this.onEditorClosed(listenerDispose, resourcesToWaitFor, waitMarkerFile));
I
isidor 已提交
500

501
			this._register(listenerDispose);
502
		}
S
Sanders Lauture 已提交
503

504
		// Configuration changes
505
		this._register(this.configurationService.onDidChangeConfiguration(() => this.onDidUpdateConfiguration()));
I
isidor 已提交
506

507
		// Fullscreen changes
508
		this._register(browser.onDidChangeFullscreen(() => this.onFullscreenChanged()));
509
	}
510

511 512 513 514
	private onFullscreenChanged(): void {
		if (!this.isCreated) {
			return; // we need to be ready
		}
S
SrTobi 已提交
515

516 517 518
		// Apply as CSS class
		const isFullscreen = browser.isFullscreen();
		if (isFullscreen) {
B
Benjamin Pasero 已提交
519
			DOM.addClass(this.workbench, 'fullscreen');
520
		} else {
B
Benjamin Pasero 已提交
521 522
			DOM.removeClass(this.workbench, 'fullscreen');

523 524 525 526
			if (this.zenMode.transitionedToFullScreen && this.zenMode.active) {
				this.toggleZenMode();
			}
		}
E
Erich Gamma 已提交
527

528 529 530 531 532 533
		// Changing fullscreen state of the window has an impact on custom title bar visibility, so we need to update
		const hasCustomTitle = this.getCustomTitleBarStyle() === 'custom';
		if (hasCustomTitle) {
			this._onTitleBarVisibilityChange.fire();
			this.layout(); // handle title bar when fullscreen changes
		}
534 535
	}

536 537 538 539 540 541 542 543 544 545
	private onMenubarToggled(visible: boolean) {
		if (visible !== this.menubarToggled) {
			this.menubarToggled = visible;

			if (this.menubarVisibility === 'toggle' || (browser.isFullscreen() && this.menubarVisibility === 'default')) {
				this.layout();
			}
		}
	}

546
	private onEditorClosed(listenerDispose: IDisposable, resourcesToWaitFor: URI[], waitMarkerFile: URI): void {
E
Erich Gamma 已提交
547

548 549 550
		// In wait mode, listen to changes to the editors and wait until the files
		// are closed that the user wants to wait for. When this happens we delete
		// the wait marker file to signal to the outside that editing is done.
551
		if (resourcesToWaitFor.every(resource => !this.editorService.isOpen({ resource }))) {
552 553 554
			listenerDispose.dispose();
			this.fileService.del(waitMarkerFile).done(null, errors.onUnexpectedError);
		}
E
Erich Gamma 已提交
555 556
	}

557
	private onDidVisibleEditorsChange(): void {
558
		const visibleEditors = this.editorService.visibleControls;
E
Erich Gamma 已提交
559

560 561 562 563 564 565 566 567 568
		// Close when empty: check if we should close the window based on the setting
		// Overruled by: window has a workspace opened or this window is for extension development
		// or setting is disabled. Also enabled when running with --wait from the command line.
		if (visibleEditors.length === 0 && this.contextService.getWorkbenchState() === WorkbenchState.EMPTY && !this.environmentService.isExtensionDevelopment) {
			const closeWhenEmpty = this.configurationService.getValue<boolean>(Workbench.closeWhenEmptyConfigurationKey);
			if (closeWhenEmpty || this.environmentService.args.wait) {
				this.closeEmptyWindowScheduler.schedule();
			}
		}
569 570
	}

571
	private onAllEditorsClosed(): void {
572
		const visibleEditors = this.editorService.visibleControls.length;
573 574
		if (visibleEditors === 0) {
			this.windowService.closeWindow();
E
Erich Gamma 已提交
575 576 577
		}
	}

578 579 580 581 582
	private onDidUpdateConfiguration(skipLayout?: boolean): void {
		const newSidebarPositionValue = this.configurationService.getValue<string>(Workbench.sidebarPositionConfigurationKey);
		const newSidebarPosition = (newSidebarPositionValue === 'right') ? Position.RIGHT : Position.LEFT;
		if (newSidebarPosition !== this.getSideBarPosition()) {
			this.setSideBarPosition(newSidebarPosition);
583
		}
E
Erich Gamma 已提交
584

585
		this.setPanelPositionFromStorageOrConfig();
E
Erich Gamma 已提交
586

587 588 589
		const fontAliasing = this.configurationService.getValue<FontAliasingOption>(Workbench.fontAliasingConfigurationKey);
		if (fontAliasing !== this.fontAliasing) {
			this.setFontAliasing(fontAliasing);
590 591
		}

592 593 594 595 596
		if (!this.zenMode.active) {
			const newStatusbarHiddenValue = !this.configurationService.getValue<boolean>(Workbench.statusbarVisibleConfigurationKey);
			if (newStatusbarHiddenValue !== this.statusBarHidden) {
				this.setStatusBarHidden(newStatusbarHiddenValue, skipLayout);
			}
597

598 599 600 601
			const newActivityBarHiddenValue = !this.configurationService.getValue<boolean>(Workbench.activityBarVisibleConfigurationKey);
			if (newActivityBarHiddenValue !== this.activityBarHidden) {
				this.setActivityBarHidden(newActivityBarHiddenValue, skipLayout);
			}
B
Benjamin Pasero 已提交
602
		}
S
SteVen Batten 已提交
603

604
		const newMenubarVisibility = this.configurationService.getValue<MenuBarVisibility>(Workbench.menubarVisibilityConfigurationKey);
S
SteVen Batten 已提交
605
		this.setMenubarVisibility(newMenubarVisibility, skipLayout);
606
	}
B
Benjamin Pasero 已提交
607

608
	//#endregion
B
Benjamin Pasero 已提交
609

610 611
	private handleContextKeys(): void {
		this.inZenMode = InEditorZenModeContext.bindTo(this.contextKeyService);
612

613 614 615 616
		(new RawContextKey<boolean>('isMac', isMacintosh)).bindTo(this.contextKeyService);
		(new RawContextKey<boolean>('isLinux', isLinux)).bindTo(this.contextKeyService);
		(new RawContextKey<boolean>('isWindows', isWindows)).bindTo(this.contextKeyService);

617 618
		const sidebarVisibleContextRaw = new RawContextKey<boolean>('sidebarVisible', false);
		this.sideBarVisibleContext = sidebarVisibleContextRaw.bindTo(this.contextKeyService);
619

620 621
		const editorsVisibleContext = EditorsVisibleContext.bindTo(this.contextKeyService);
		const textCompareEditorVisible = TextCompareEditorVisibleContext.bindTo(this.contextKeyService);
622
		const textCompareEditorActive = TextCompareEditorActiveContext.bindTo(this.contextKeyService);
623 624
		const activeEditorGroupEmpty = ActiveEditorGroupEmptyContext.bindTo(this.contextKeyService);
		const multipleEditorGroups = MultipleEditorGroupsContext.bindTo(this.contextKeyService);
625

626
		const updateEditorContextKeys = () => {
627
			const activeControl = this.editorService.activeControl;
628
			const visibleEditors = this.editorService.visibleControls;
E
Erich Gamma 已提交
629

630
			textCompareEditorActive.set(activeControl && activeControl.getId() === TEXT_DIFF_EDITOR_ID);
631
			textCompareEditorVisible.set(visibleEditors.some(control => control.getId() === TEXT_DIFF_EDITOR_ID));
I
isidor 已提交
632

633 634 635 636
			if (visibleEditors.length > 0) {
				editorsVisibleContext.set(true);
			} else {
				editorsVisibleContext.reset();
637
			}
B
Benjamin Pasero 已提交
638

639
			if (!this.editorService.activeEditor) {
640 641 642
				activeEditorGroupEmpty.set(true);
			} else {
				activeEditorGroupEmpty.reset();
643
			}
644

B
Benjamin Pasero 已提交
645
			if (this.editorGroupService.count > 1) {
646 647 648 649 650
				multipleEditorGroups.set(true);
			} else {
				multipleEditorGroups.reset();
			}
		};
B
Benjamin Pasero 已提交
651

652
		this.editorPart.whenRestored.then(() => updateEditorContextKeys());
653 654
		this._register(this.editorService.onDidActiveEditorChange(() => updateEditorContextKeys()));
		this._register(this.editorService.onDidVisibleEditorsChange(() => updateEditorContextKeys()));
B
Benjamin Pasero 已提交
655 656
		this._register(this.editorGroupService.onDidAddGroup(() => updateEditorContextKeys()));
		this._register(this.editorGroupService.onDidRemoveGroup(() => updateEditorContextKeys()));
657

658 659 660 661 662 663 664 665 666 667
		const inputFocused = InputFocusedContext.bindTo(this.contextKeyService);
		this._register(DOM.addDisposableListener(window, 'focusin', () => {
			inputFocused.set(document.activeElement && (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA'));
		}, true));

		const workbenchStateRawContext = new RawContextKey<string>('workbenchState', getWorkbenchStateString(this.configurationService.getWorkbenchState()));
		const workbenchStateContext = workbenchStateRawContext.bindTo(this.contextKeyService);
		this._register(this.configurationService.onDidChangeWorkbenchState(() => {
			workbenchStateContext.set(getWorkbenchStateString(this.configurationService.getWorkbenchState()));
		}));
I
isidor 已提交
668

669 670 671 672 673
		const workspaceFolderCountRawContext = new RawContextKey<number>('workspaceFolderCount', this.configurationService.getWorkspace().folders.length);
		const workspaceFolderCountContext = workspaceFolderCountRawContext.bindTo(this.contextKeyService);
		this._register(this.configurationService.onDidChangeWorkspaceFolders(() => {
			workspaceFolderCountContext.set(this.configurationService.getWorkspace().folders.length);
		}));
674

675
		const splitEditorsVerticallyContext = SplitEditorsVertically.bindTo(this.contextKeyService);
S
Sanders Lauture 已提交
676

677 678 679 680
		const updateSplitEditorsVerticallyContext = () => {
			const direction = preferredSideBySideGroupDirection(this.configurationService);
			splitEditorsVerticallyContext.set(direction === GroupDirection.DOWN);
		};
S
Sanders Lauture 已提交
681

682 683 684 685 686
		this._register(this.configurationService.onDidChangeConfiguration(e => {
			if (e.affectsConfiguration('workbench.editor.openSideBySideDirection')) {
				updateSplitEditorsVerticallyContext();
			}
		}));
E
Erich Gamma 已提交
687

688
		updateSplitEditorsVerticallyContext();
689
	}
E
Erich Gamma 已提交
690

691 692
	private restoreParts(): TPromise<IWorkbenchStartedInfo> {
		const restorePromises: Thenable<any>[] = [];
B
Benjamin Pasero 已提交
693

694 695
		// Restore Editorpart
		perf.mark('willRestoreEditors');
696
		restorePromises.push(this.editorPart.whenRestored.then(() => {
697 698
			return this.resolveEditorsToOpen().then(inputs => {
				if (inputs.length) {
699
					return this.editorService.openEditors(inputs);
700
				}
E
Erich Gamma 已提交
701

702 703 704 705 706
				return TPromise.as(void 0);
			});
		}).then(() => {
			perf.mark('didRestoreEditors');
		}));
E
Erich Gamma 已提交
707

708 709 710 711
		// Restore Sidebar
		let viewletIdToRestore: string;
		if (!this.sideBarHidden) {
			this.sideBarVisibleContext.set(true);
712

713 714
			if (this.shouldRestoreLastOpenedViewlet()) {
				viewletIdToRestore = this.storageService.get(SidebarPart.activeViewletSettingsKey, StorageScope.WORKSPACE);
715
			}
716

717 718
			if (!viewletIdToRestore) {
				viewletIdToRestore = this.viewletService.getDefaultViewletId();
719
			}
E
Erich Gamma 已提交
720

721
			perf.mark('willRestoreViewlet');
722 723 724 725 726
			restorePromises.push(this.viewletService.openViewlet(viewletIdToRestore)
				.then(viewlet => viewlet || this.viewletService.openViewlet(this.viewletService.getDefaultViewletId()))
				.then(() => {
					perf.mark('didRestoreViewlet');
				}));
727 728
		}

729 730 731 732
		// Restore Panel
		const panelRegistry = Registry.as<PanelRegistry>(PanelExtensions.Panels);
		const panelId = this.storageService.get(PanelPart.activePanelSettingsKey, StorageScope.WORKSPACE, panelRegistry.getDefaultPanelId());
		if (!this.panelHidden && !!panelId) {
733
			perf.mark('willRestorePanel');
734
			const isPanelToRestoreEnabled = !!this.panelPart.getPanels().filter(p => p.id === panelId).length;
735 736
			const panelIdToRestore = isPanelToRestoreEnabled ? panelId : panelRegistry.getDefaultPanelId();
			restorePromises.push(this.panelPart.openPanel(panelIdToRestore, false).then(() => perf.mark('didRestorePanel')));
I
isidor 已提交
737 738
		}

739
		// Restore Zen Mode if active
I
isidor 已提交
740
		if (this.storageService.getBoolean(Workbench.zenModeActiveStorageKey, StorageScope.WORKSPACE, false)) {
I
isidor 已提交
741
			this.toggleZenMode(true, true);
I
isidor 已提交
742 743
		}

B
Benjamin Pasero 已提交
744
		// Restore Forced Editor Center Mode
745
		if (this.storageService.getBoolean(Workbench.centeredEditorLayoutActiveStorageKey, StorageScope.WORKSPACE, false)) {
I
isidor 已提交
746
			this.centerEditorLayout(true);
S
SrTobi 已提交
747
		}
748

749
		const onRestored = (error?: Error): IWorkbenchStartedInfo => {
B
Benjamin Pasero 已提交
750
			this.workbenchCreated = true;
751

752 753
			if (error) {
				errors.onUnexpectedError(error);
754
			}
I
isidor 已提交
755

756 757 758 759
			return {
				customKeybindingsCount: this.keybindingService.customKeybindingsCount(),
				pinnedViewlets: this.activitybarPart.getPinned(),
				restoredViewlet: viewletIdToRestore,
760
				restoredEditorsCount: this.editorService.visibleEditors.length
761 762
			};
		};
763

764
		return TPromise.join(restorePromises).then(() => onRestored(), error => onRestored(error));
E
Erich Gamma 已提交
765 766
	}

767 768 769
	private shouldRestoreLastOpenedViewlet(): boolean {
		if (!this.environmentService.isBuilt) {
			return true; // always restore sidebar when we are in development mode
E
Erich Gamma 已提交
770 771
		}

772 773 774 775
		const restore = this.storageService.getBoolean(Workbench.sidebarRestoreStorageKey, StorageScope.WORKSPACE);
		if (restore) {
			this.storageService.remove(Workbench.sidebarRestoreStorageKey, StorageScope.WORKSPACE); // only support once
		}
776

777
		return restore;
E
Erich Gamma 已提交
778 779
	}

780
	private resolveEditorsToOpen(): TPromise<IResourceEditor[]> {
781
		const config = this.workbenchParams.configuration;
I
isidor 已提交
782

B
Benjamin Pasero 已提交
783
		// Files to open, diff or create
784
		if (this.hasInitialFilesToOpen) {
I
isidor 已提交
785

B
Benjamin Pasero 已提交
786
			// Files to diff is exclusive
787
			const filesToDiff = this.toInputs(config.filesToDiff, false);
788
			if (filesToDiff && filesToDiff.length === 2) {
789 790 791 792 793
				return TPromise.as([<IResourceDiffInput>{
					leftResource: filesToDiff[0].resource,
					rightResource: filesToDiff[1].resource,
					options: { pinned: true }
				}]);
B
Benjamin Pasero 已提交
794
			}
I
isidor 已提交
795

796 797
			const filesToCreate = this.toInputs(config.filesToCreate, true);
			const filesToOpen = this.toInputs(config.filesToOpen, false);
I
isidor 已提交
798

799 800
			// Otherwise: Open/Create files
			return TPromise.as([...filesToOpen, ...filesToCreate]);
B
Benjamin Pasero 已提交
801
		}
I
isidor 已提交
802

803
		// Empty workbench
804
		else if (this.contextService.getWorkbenchState() === WorkbenchState.EMPTY && this.openUntitledFile()) {
B
Benjamin Pasero 已提交
805
			const isEmpty = this.editorGroupService.count === 1 && this.editorGroupService.activeGroup.count === 0;
806 807
			if (!isEmpty) {
				return TPromise.as([]); // do not open any empty untitled file if we restored editors from previous session
808
			}
B
Benjamin Pasero 已提交
809

810 811 812 813
			return this.backupFileService.hasBackups().then(hasBackups => {
				if (hasBackups) {
					return TPromise.as([]); // do not open any empty untitled file if we have backups to restore
				}
B
Benjamin Pasero 已提交
814

815
				return TPromise.as([<IUntitledResourceInput>{}]);
816
			});
817
		}
818

B
Benjamin Pasero 已提交
819 820
		return TPromise.as([]);
	}
821

822
	private toInputs(paths: IPath[], isNew: boolean): (IResourceInput | IUntitledResourceInput)[] {
B
Benjamin Pasero 已提交
823 824
		if (!paths || !paths.length) {
			return [];
825
		}
I
isidor 已提交
826

B
Benjamin Pasero 已提交
827
		return paths.map(p => {
828
			const resource = p.fileUri;
829 830 831 832 833
			let input: IResourceInput | IUntitledResourceInput;
			if (isNew) {
				input = { filePath: resource.fsPath, options: { pinned: true } } as IUntitledResourceInput;
			} else {
				input = { resource, options: { pinned: true } } as IResourceInput;
834
			}
E
Erich Gamma 已提交
835

836
			if (!isNew && p.lineNumber) {
B
Benjamin Pasero 已提交
837 838 839 840 841
				input.options.selection = {
					startLineNumber: p.lineNumber,
					startColumn: p.columnNumber
				};
			}
842

B
Benjamin Pasero 已提交
843 844
			return input;
		});
845 846
	}

847
	private openUntitledFile() {
848
		const startupEditor = this.configurationService.inspect('workbench.startupEditor');
B
Benjamin Pasero 已提交
849 850

		// Fallback to previous workbench.welcome.enabled setting in case startupEditor is not defined
851
		if (!startupEditor.user && !startupEditor.workspace) {
852 853 854
			const welcomeEnabledValue = this.configurationService.getValue('workbench.welcome.enabled');
			if (typeof welcomeEnabledValue === 'boolean') {
				return !welcomeEnabledValue;
855
			}
856
		}
E
Erich Gamma 已提交
857

858
		return startupEditor.value === 'newUntitledFile';
859
	}
E
Erich Gamma 已提交
860 861

	private initSettings(): void {
862

E
Erich Gamma 已提交
863
		// Sidebar visibility
I
isidor 已提交
864
		this.sideBarHidden = this.storageService.getBoolean(Workbench.sidebarHiddenStorageKey, StorageScope.WORKSPACE, this.contextService.getWorkbenchState() === WorkbenchState.EMPTY);
865

I
isidor 已提交
866
		// Panel part visibility
867
		const panelRegistry = Registry.as<PanelRegistry>(PanelExtensions.Panels);
I
isidor 已提交
868
		this.panelHidden = this.storageService.getBoolean(Workbench.panelHiddenStorageKey, StorageScope.WORKSPACE, true);
869 870
		if (!panelRegistry.getDefaultPanelId()) {
			this.panelHidden = true; // we hide panel part if there is no default panel
871 872
		}

E
Erich Gamma 已提交
873
		// Sidebar position
874
		const sideBarPosition = this.configurationService.getValue<string>(Workbench.sidebarPositionConfigurationKey);
875
		this.sideBarPosition = (sideBarPosition === 'right') ? Position.RIGHT : Position.LEFT;
876

I
isidor 已提交
877
		// Panel position
878
		this.setPanelPositionFromStorageOrConfig();
879

880
		// Menubar visibility
881
		const menuBarVisibility = this.configurationService.getValue<MenuBarVisibility>(Workbench.menubarVisibilityConfigurationKey);
S
SteVen Batten 已提交
882
		this.setMenubarVisibility(menuBarVisibility, true);
883

B
Benjamin Pasero 已提交
884
		// Statusbar visibility
885
		const statusBarVisible = this.configurationService.getValue<string>(Workbench.statusbarVisibleConfigurationKey);
886
		this.statusBarHidden = !statusBarVisible;
887

S
Sanders Lauture 已提交
888
		// Activity bar visibility
889
		const activityBarVisible = this.configurationService.getValue<string>(Workbench.activityBarVisibleConfigurationKey);
S
Sanders Lauture 已提交
890
		this.activityBarHidden = !activityBarVisible;
891

892
		// Font aliasing
893
		this.fontAliasing = this.configurationService.getValue<FontAliasingOption>(Workbench.fontAliasingConfigurationKey);
894

I
isidor 已提交
895 896
		// Zen mode
		this.zenMode = {
I
isidor 已提交
897
			active: false,
898
			transitionedToFullScreen: false,
899
			transitionedToCenteredEditorLayout: false,
900
			wasSideBarVisible: false,
901 902
			wasPanelVisible: false,
			transitionDisposeables: []
I
isidor 已提交
903
		};
E
Erich Gamma 已提交
904 905
	}

906 907 908 909 910
	private setPanelPositionFromStorageOrConfig() {
		const defaultPanelPosition = this.configurationService.getValue<string>(Workbench.defaultPanelPositionStorageKey);
		const panelPosition = this.storageService.get(Workbench.panelPositionStorageKey, StorageScope.WORKSPACE, defaultPanelPosition);
		this.panelPosition = (panelPosition === 'right') ? Position.RIGHT : Position.BOTTOM;
	}
911

912 913
	private getCustomTitleBarStyle(): 'custom' {
		const isDev = !this.environmentService.isBuilt || this.environmentService.isExtensionDevelopment;
B
Benjamin Pasero 已提交
914
		if (isMacintosh && isDev) {
915 916
			return null; // not enabled when developing due to https://github.com/electron/electron/issues/3647
		}
E
Erich Gamma 已提交
917

918 919 920 921 922 923 924 925 926 927
		const windowConfig = this.configurationService.getValue<IWindowSettings>();
		if (windowConfig && windowConfig.window) {
			const useNativeTabs = windowConfig.window.nativeTabs;
			if (useNativeTabs) {
				return null; // native tabs on sierra do not work with custom title style
			}

			const style = windowConfig.window.titleBarStyle;
			if (style === 'custom') {
				return style;
928 929 930
			}
		}

931
		return null;
B
Benjamin Pasero 已提交
932 933
	}

934 935
	private setStatusBarHidden(hidden: boolean, skipLayout?: boolean): void {
		this.statusBarHidden = hidden;
B
Benjamin Pasero 已提交
936

937 938
		// Adjust CSS
		if (hidden) {
B
Benjamin Pasero 已提交
939
			DOM.addClass(this.workbench, 'nostatusbar');
E
Erich Gamma 已提交
940
		} else {
B
Benjamin Pasero 已提交
941
			DOM.removeClass(this.workbench, 'nostatusbar');
E
Erich Gamma 已提交
942 943
		}

944 945 946
		// Layout
		if (!skipLayout) {
			this.workbenchLayout.layout();
947 948 949
		}
	}

950
	private setFontAliasing(aliasing: FontAliasingOption) {
951
		this.fontAliasing = aliasing;
S
Sanders Lauture 已提交
952

B
Benjamin Pasero 已提交
953 954 955 956 957
		// Remove all
		document.body.classList.remove(...fontAliasingValues.map(value => `monaco-font-aliasing-${value}`));

		// Add specific
		if (fontAliasingValues.some(option => option === aliasing)) {
958
			document.body.classList.add(`monaco-font-aliasing-${aliasing}`);
S
Sanders Lauture 已提交
959
		}
960 961
	}

E
Erich Gamma 已提交
962
	private createWorkbenchLayout(): void {
963 964
		this.workbenchLayout = this.instantiationService.createInstance(
			WorkbenchLayout,
965
			this.container,
B
Benjamin Pasero 已提交
966
			this.workbench,
I
isidor 已提交
967
			{
968 969 970 971 972 973
				titlebar: this.titlebarPart,
				activitybar: this.activitybarPart,
				editor: this.editorPart,
				sidebar: this.sidebarPart,
				panel: this.panelPart,
				statusbar: this.statusbarPart,
I
isidor 已提交
974
			},
975 976 977 978
			this.quickOpen,
			this.quickInput,
			this.notificationsCenter,
			this.notificationsToasts
E
Erich Gamma 已提交
979 980 981 982 983 984 985
		);
	}

	private renderWorkbench(): void {

		// Apply sidebar state as CSS class
		if (this.sideBarHidden) {
B
Benjamin Pasero 已提交
986
			DOM.addClass(this.workbench, 'nosidebar');
E
Erich Gamma 已提交
987
		}
B
Benjamin Pasero 已提交
988

989
		if (this.panelHidden) {
B
Benjamin Pasero 已提交
990
			DOM.addClass(this.workbench, 'nopanel');
991
		}
B
Benjamin Pasero 已提交
992

993
		if (this.statusBarHidden) {
B
Benjamin Pasero 已提交
994
			DOM.addClass(this.workbench, 'nostatusbar');
995
		}
E
Erich Gamma 已提交
996

B
Benjamin Pasero 已提交
997
		// Apply font aliasing
998 999
		this.setFontAliasing(this.fontAliasing);

1000 1001
		// Apply fullscreen state
		if (browser.isFullscreen()) {
B
Benjamin Pasero 已提交
1002
			DOM.addClass(this.workbench, 'fullscreen');
B
Benjamin Pasero 已提交
1003 1004
		}

E
Erich Gamma 已提交
1005
		// Create Parts
B
Benjamin Pasero 已提交
1006
		this.createTitlebarPart();
E
Erich Gamma 已提交
1007 1008 1009
		this.createActivityBarPart();
		this.createSidebarPart();
		this.createEditorPart();
1010
		this.createPanelPart();
E
Erich Gamma 已提交
1011 1012
		this.createStatusbarPart();

1013 1014
		// Notification Handlers
		this.createNotificationsHandlers();
1015

1016 1017 1018 1019 1020 1021

		// Menubar visibility changes
		if ((isWindows || isLinux) && this.getCustomTitleBarStyle() === 'custom') {
			this.titlebarPart.onMenubarVisibilityChange()(e => this.onMenubarToggled(e));
		}

E
Erich Gamma 已提交
1022
		// Add Workbench to DOM
B
Benjamin Pasero 已提交
1023
		this.container.appendChild(this.workbench);
E
Erich Gamma 已提交
1024 1025
	}

B
Benjamin Pasero 已提交
1026
	private createTitlebarPart(): void {
B
Benjamin Pasero 已提交
1027
		const titlebarContainer = this.createPart(Identifiers.TITLEBAR_PART, ['part', 'titlebar'], 'contentinfo');
B
Benjamin Pasero 已提交
1028

B
Benjamin Pasero 已提交
1029
		this.titlebarPart.create(titlebarContainer);
B
Benjamin Pasero 已提交
1030 1031
	}

E
Erich Gamma 已提交
1032
	private createActivityBarPart(): void {
B
Benjamin Pasero 已提交
1033
		const activitybarPartContainer = this.createPart(Identifiers.ACTIVITYBAR_PART, ['part', 'activitybar', this.sideBarPosition === Position.LEFT ? 'left' : 'right'], 'navigation');
E
Erich Gamma 已提交
1034

B
Benjamin Pasero 已提交
1035
		this.activitybarPart.create(activitybarPartContainer);
E
Erich Gamma 已提交
1036 1037 1038
	}

	private createSidebarPart(): void {
B
Benjamin Pasero 已提交
1039
		const sidebarPartContainer = this.createPart(Identifiers.SIDEBAR_PART, ['part', 'sidebar', this.sideBarPosition === Position.LEFT ? 'left' : 'right'], 'complementary');
E
Erich Gamma 已提交
1040

B
Benjamin Pasero 已提交
1041
		this.sidebarPart.create(sidebarPartContainer);
E
Erich Gamma 已提交
1042 1043
	}

I
isidor 已提交
1044
	private createPanelPart(): void {
B
Benjamin Pasero 已提交
1045
		const panelPartContainer = this.createPart(Identifiers.PANEL_PART, ['part', 'panel', this.panelPosition === Position.BOTTOM ? 'bottom' : 'right'], 'complementary');
I
isidor 已提交
1046

B
Benjamin Pasero 已提交
1047
		this.panelPart.create(panelPartContainer);
I
isidor 已提交
1048 1049
	}

E
Erich Gamma 已提交
1050
	private createEditorPart(): void {
B
Benjamin Pasero 已提交
1051
		const editorContainer = this.createPart(Identifiers.EDITOR_PART, ['part', 'editor'], 'main');
E
Erich Gamma 已提交
1052

B
Benjamin Pasero 已提交
1053
		this.editorPart.create(editorContainer);
E
Erich Gamma 已提交
1054 1055 1056
	}

	private createStatusbarPart(): void {
B
Benjamin Pasero 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
		const statusbarContainer = this.createPart(Identifiers.STATUSBAR_PART, ['part', 'statusbar'], 'contentinfo');

		this.statusbarPart.create(statusbarContainer);
	}

	private createPart(id: string, classes: string[], role: string): HTMLElement {
		const part = document.createElement('div');
		classes.forEach(clazz => DOM.addClass(part, clazz));
		part.id = id;
		part.setAttribute('role', role);

		this.workbench.appendChild(part);
E
Erich Gamma 已提交
1069

B
Benjamin Pasero 已提交
1070
		return part;
E
Erich Gamma 已提交
1071 1072
	}

1073 1074
	private createNotificationsHandlers(): void {

1075
		// Notifications Center
B
Benjamin Pasero 已提交
1076
		this.notificationsCenter = this._register(this.instantiationService.createInstance(NotificationsCenter, this.workbench, this.notificationService.model));
1077

1078
		// Notifications Toasts
B
Benjamin Pasero 已提交
1079
		this.notificationsToasts = this._register(this.instantiationService.createInstance(NotificationsToasts, this.workbench, this.notificationService.model));
1080

1081
		// Notifications Alerts
1082
		this._register(this.instantiationService.createInstance(NotificationsAlerts, this.notificationService.model));
1083 1084 1085 1086

		// Notifications Status
		const notificationsStatus = this.instantiationService.createInstance(NotificationsStatus, this.notificationService.model);

1087
		// Eventing
1088
		this._register(this.notificationsCenter.onDidChangeVisibility(() => {
1089 1090 1091 1092 1093 1094 1095 1096

			// Update status
			notificationsStatus.update(this.notificationsCenter.isVisible);

			// Update toasts
			this.notificationsToasts.update(this.notificationsCenter.isVisible);
		}));

1097
		// Register Commands
1098
		registerNotificationCommands(this.notificationsCenter, this.notificationsToasts);
1099 1100
	}

1101
	getInstantiationService(): IInstantiationService {
E
Erich Gamma 已提交
1102 1103 1104
		return this.instantiationService;
	}

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
	dispose(reason = ShutdownReason.QUIT): void {
		super.dispose();

		// Restore sidebar if we are being shutdown as a matter of a reload
		if (reason === ShutdownReason.RELOAD) {
			this.storageService.store(Workbench.sidebarRestoreStorageKey, 'true', StorageScope.WORKSPACE);
		}

		// Preserve zen mode only on reload. Real quit gets out of zen mode so novice users do not get stuck in zen mode.
		const zenConfig = this.configurationService.getValue<IZenModeSettings>('zenMode');
		const restoreZenMode = this.zenMode.active && (zenConfig.restore || reason === ShutdownReason.RELOAD);
		if (restoreZenMode) {
			this.storageService.store(Workbench.zenModeActiveStorageKey, true, StorageScope.WORKSPACE);
		} else {
			if (this.zenMode.active) {
				this.toggleZenMode(true);
			}
			this.storageService.remove(Workbench.zenModeActiveStorageKey, StorageScope.WORKSPACE);
		}

		this.workbenchShutdown = true;
	}

	//#region IPartService

B
Benjamin Pasero 已提交
1130
	private _onTitleBarVisibilityChange: Emitter<void> = this._register(new Emitter<void>());
1131
	get onTitleBarVisibilityChange(): Event<void> { return this._onTitleBarVisibilityChange.event; }
1132

1133
	get onEditorLayout(): Event<IDimension> { return this.editorPart.onDidLayout; }
1134

1135
	isCreated(): boolean {
1136
		return !!(this.workbenchCreated && this.workbenchStarted);
1137
	}
1138

1139 1140 1141 1142 1143
	hasFocus(part: Parts): boolean {
		const activeElement = document.activeElement;
		if (!activeElement) {
			return false;
		}
1144

1145 1146 1147
		const container = this.getContainer(part);
		return DOM.isAncestor(activeElement, container);
	}
1148

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	getContainer(part: Parts): HTMLElement {
		let container: HTMLElement = null;
		switch (part) {
			case Parts.TITLEBAR_PART:
				container = this.titlebarPart.getContainer();
				break;
			case Parts.ACTIVITYBAR_PART:
				container = this.activitybarPart.getContainer();
				break;
			case Parts.SIDEBAR_PART:
				container = this.sidebarPart.getContainer();
				break;
			case Parts.PANEL_PART:
				container = this.panelPart.getContainer();
				break;
			case Parts.EDITOR_PART:
				container = this.editorPart.getContainer();
				break;
			case Parts.STATUSBAR_PART:
				container = this.statusbarPart.getContainer();
				break;
		}
1171

1172 1173
		return container;
	}
1174

1175 1176 1177
	isVisible(part: Parts): boolean {
		switch (part) {
			case Parts.TITLEBAR_PART:
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
				if (this.getCustomTitleBarStyle() !== 'custom') {
					return false;
				} else if (!browser.isFullscreen()) {
					return true;
				} else if (isMacintosh) {
					return false;
				} else if (this.menubarVisibility === 'visible') {
					return true;
				} else if (this.menubarVisibility === 'toggle' || this.menubarVisibility === 'default') {
					return this.menubarToggled;
				}

				return false;
1191 1192 1193 1194 1195 1196 1197 1198
			case Parts.SIDEBAR_PART:
				return !this.sideBarHidden;
			case Parts.PANEL_PART:
				return !this.panelHidden;
			case Parts.STATUSBAR_PART:
				return !this.statusBarHidden;
			case Parts.ACTIVITYBAR_PART:
				return !this.activityBarHidden;
E
Erich Gamma 已提交
1199
		}
1200

1201
		return true; // any other part cannot be hidden
E
Erich Gamma 已提交
1202 1203
	}

1204 1205 1206
	getTitleBarOffset(): number {
		let offset = 0;
		if (this.isVisible(Parts.TITLEBAR_PART)) {
S
SteVen Batten 已提交
1207
			offset = this.workbenchLayout.partLayoutInfo.titlebar.height;
1208 1209 1210
			if (isMacintosh || this.menubarVisibility === 'hidden') {
				offset /= browser.getZoomFactor();
			}
E
Erich Gamma 已提交
1211
		}
1212 1213

		return offset;
E
Erich Gamma 已提交
1214
	}
1215

1216
	getWorkbenchElementId(): string {
1217 1218
		return Identifiers.WORKBENCH_CONTAINER;
	}
1219

I
isidor 已提交
1220
	toggleZenMode(skipLayout?: boolean, restoring = false): void {
I
isidor 已提交
1221
		this.zenMode.active = !this.zenMode.active;
1222
		this.zenMode.transitionDisposeables = dispose(this.zenMode.transitionDisposeables);
1223

1224 1225
		// Check if zen mode transitioned to full screen and if now we are out of zen mode
		// -> we need to go out of full screen (same goes for the centered editor layout)
1226
		let toggleFullScreen = false;
1227 1228

		// Zen Mode Active
I
isidor 已提交
1229
		if (this.zenMode.active) {
1230
			const config = this.configurationService.getValue<IZenModeSettings>('zenMode');
1231

I
isidor 已提交
1232
			toggleFullScreen = !browser.isFullscreen() && config.fullScreen;
I
isidor 已提交
1233
			this.zenMode.transitionedToFullScreen = restoring ? config.fullScreen : toggleFullScreen;
1234
			this.zenMode.transitionedToCenteredEditorLayout = !this.isEditorLayoutCentered() && config.centerLayout;
1235 1236
			this.zenMode.wasSideBarVisible = this.isVisible(Parts.SIDEBAR_PART);
			this.zenMode.wasPanelVisible = this.isVisible(Parts.PANEL_PART);
1237

1238 1239
			this.setPanelHidden(true, true).done(void 0, errors.onUnexpectedError);
			this.setSideBarHidden(true, true).done(void 0, errors.onUnexpectedError);
I
isidor 已提交
1240

1241 1242 1243
			if (config.hideActivityBar) {
				this.setActivityBarHidden(true, true);
			}
1244

I
isidor 已提交
1245
			if (config.hideStatusBar) {
I
isidor 已提交
1246 1247
				this.setStatusBarHidden(true, true);
			}
1248

1249 1250
			if (config.hideTabs && this.editorPart.partOptions.showTabs) {
				this.zenMode.transitionDisposeables.push(this.editorPart.enforcePartOptions({ showTabs: false }));
I
isidor 已提交
1251
			}
1252 1253 1254 1255

			if (config.centerLayout) {
				this.centerEditorLayout(true, true);
			}
1256 1257 1258 1259
		}

		// Zen Mode Inactive
		else {
1260
			if (this.zenMode.wasPanelVisible) {
1261
				this.setPanelHidden(false, true).done(void 0, errors.onUnexpectedError);
1262
			}
1263

1264
			if (this.zenMode.wasSideBarVisible) {
1265
				this.setSideBarHidden(false, true).done(void 0, errors.onUnexpectedError);
1266
			}
1267

1268 1269 1270
			if (this.zenMode.transitionedToCenteredEditorLayout) {
				this.centerEditorLayout(false, true);
			}
1271

1272 1273
			// Status bar and activity bar visibility come from settings -> update their visibility.
			this.onDidUpdateConfiguration(true);
1274

B
Benjamin Pasero 已提交
1275
			this.editorGroupService.activeGroup.focus();
1276

1277
			toggleFullScreen = this.zenMode.transitionedToFullScreen && browser.isFullscreen();
I
isidor 已提交
1278
		}
1279

1280
		this.inZenMode.set(this.zenMode.active);
I
isidor 已提交
1281

I
isidor 已提交
1282
		if (!skipLayout) {
1283
			this.layout();
I
isidor 已提交
1284
		}
1285

1286
		if (toggleFullScreen) {
1287
			this.windowService.toggleFullScreen().done(void 0, errors.onUnexpectedError);
1288
		}
I
isidor 已提交
1289 1290
	}

1291
	layout(options?: ILayoutOptions): void {
J
Joao Moreno 已提交
1292 1293
		this.contextViewService.layout();

1294 1295 1296 1297 1298
		if (this.workbenchStarted && !this.workbenchShutdown) {
			this.workbenchLayout.layout(options);
		}
	}

1299
	isEditorLayoutCentered(): boolean {
I
isidor 已提交
1300
		return this.editorPart.isLayoutCentered();
S
SrTobi 已提交
1301 1302
	}

1303
	centerEditorLayout(active: boolean, skipLayout?: boolean): void {
I
isidor 已提交
1304
		this.storageService.store(Workbench.centeredEditorLayoutActiveStorageKey, active, StorageScope.WORKSPACE);
B
Benjamin Pasero 已提交
1305

1306
		// Enter Centered Editor Layout
I
isidor 已提交
1307
		this.editorPart.centerLayout(active);
1308

1309 1310 1311
		if (!skipLayout) {
			this.layout();
		}
S
SrTobi 已提交
1312 1313
	}

1314
	resizePart(part: Parts, sizeChange: number): void {
1315 1316 1317 1318 1319 1320 1321
		switch (part) {
			case Parts.SIDEBAR_PART:
			case Parts.PANEL_PART:
			case Parts.EDITOR_PART:
				this.workbenchLayout.resizePart(part, sizeChange);
				break;
			default:
B
Benjamin Pasero 已提交
1322
				return; // Cannot resize other parts
1323 1324 1325
		}
	}

1326 1327
	setActivityBarHidden(hidden: boolean, skipLayout?: boolean): void {
		this.activityBarHidden = hidden;
1328

1329 1330 1331
		// Layout
		if (!skipLayout) {
			this.workbenchLayout.layout();
1332
		}
1333
	}
1334

1335 1336 1337 1338 1339 1340
	setSideBarHidden(hidden: boolean, skipLayout?: boolean): TPromise<void> {
		this.sideBarHidden = hidden;
		this.sideBarVisibleContext.set(!hidden);

		// Adjust CSS
		if (hidden) {
B
Benjamin Pasero 已提交
1341
			DOM.addClass(this.workbench, 'nosidebar');
1342
		} else {
B
Benjamin Pasero 已提交
1343
			DOM.removeClass(this.workbench, 'nosidebar');
1344 1345
		}

1346 1347 1348 1349 1350 1351 1352 1353 1354
		// If sidebar becomes hidden, also hide the current active Viewlet if any
		let promise = TPromise.wrap<any>(null);
		if (hidden && this.sidebarPart.getActiveViewlet()) {
			promise = this.sidebarPart.hideActiveViewlet().then(() => {
				const activePanel = this.panelPart.getActivePanel();

				// Pass Focus to Editor or Panel if Sidebar is now hidden
				if (this.hasFocus(Parts.PANEL_PART) && activePanel) {
					activePanel.focus();
B
Benjamin Pasero 已提交
1355 1356
				} else {
					this.editorGroupService.activeGroup.focus();
1357 1358 1359 1360 1361 1362 1363 1364
				}
			});
		}

		// If sidebar becomes visible, show last active Viewlet or default viewlet
		else if (!hidden && !this.sidebarPart.getActiveViewlet()) {
			const viewletToOpen = this.sidebarPart.getLastActiveViewletId();
			if (viewletToOpen) {
1365 1366
				promise = this.viewletService.openViewlet(viewletToOpen, true)
					.then(viewlet => viewlet || this.viewletService.openViewlet(this.viewletService.getDefaultViewletId(), true));
1367
			}
1368 1369
		}

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
		return promise.then(() => {

			// Remember in settings
			const defaultHidden = this.contextService.getWorkbenchState() === WorkbenchState.EMPTY;
			if (hidden !== defaultHidden) {
				this.storageService.store(Workbench.sidebarHiddenStorageKey, hidden ? 'true' : 'false', StorageScope.WORKSPACE);
			} else {
				this.storageService.remove(Workbench.sidebarHiddenStorageKey, StorageScope.WORKSPACE);
			}

			// Layout
			if (!skipLayout) {
				this.workbenchLayout.layout();
			}
		});
1385
	}
1386 1387 1388 1389 1390 1391

	setPanelHidden(hidden: boolean, skipLayout?: boolean): TPromise<void> {
		this.panelHidden = hidden;

		// Adjust CSS
		if (hidden) {
B
Benjamin Pasero 已提交
1392
			DOM.addClass(this.workbench, 'nopanel');
1393
		} else {
B
Benjamin Pasero 已提交
1394
			DOM.removeClass(this.workbench, 'nopanel');
1395 1396 1397 1398 1399 1400
		}

		// If panel part becomes hidden, also hide the current active panel if any
		let promise = TPromise.wrap<any>(null);
		if (hidden && this.panelPart.getActivePanel()) {
			promise = this.panelPart.hideActivePanel().then(() => {
B
Benjamin Pasero 已提交
1401
				this.editorGroupService.activeGroup.focus(); // Pass focus to editor group if panel part is now hidden
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
			});
		}

		// If panel part becomes visible, show last active panel or default panel
		else if (!hidden && !this.panelPart.getActivePanel()) {
			const panelToOpen = this.panelPart.getLastActivePanelId();
			if (panelToOpen) {
				promise = this.panelPart.openPanel(panelToOpen, true);
			}
		}

		return promise.then(() => {

			// Remember in settings
			if (!hidden) {
				this.storageService.store(Workbench.panelHiddenStorageKey, 'false', StorageScope.WORKSPACE);
			} else {
				this.storageService.remove(Workbench.panelHiddenStorageKey, StorageScope.WORKSPACE);
			}

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

	toggleMaximizedPanel(): void {
		this.workbenchLayout.layout({ toggleMaximizedPanel: true, source: Parts.PANEL_PART });
	}

	isPanelMaximized(): boolean {
		return this.workbenchLayout.isPanelMaximized();
	}

	getSideBarPosition(): Position {
		return this.sideBarPosition;
	}

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

		const newPositionValue = (position === Position.LEFT) ? 'left' : 'right';
		const oldPositionValue = (this.sideBarPosition === Position.LEFT) ? 'left' : 'right';
		this.sideBarPosition = position;

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

		// Update Styles
		this.activitybarPart.updateStyles();
		this.sidebarPart.updateStyles();

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

S
SteVen Batten 已提交
1464
	setMenubarVisibility(visibility: MenuBarVisibility, skipLayout: boolean): void {
S
SteVen Batten 已提交
1465 1466
		if (this.menubarVisibility !== visibility) {
			this.menubarVisibility = visibility;
1467

S
SteVen Batten 已提交
1468 1469 1470
			if (!skipLayout) {
				this.workbenchLayout.layout();
			}
1471 1472 1473
		}
	}

S
SteVen Batten 已提交
1474 1475 1476 1477
	getMenubarVisibility(): MenuBarVisibility {
		return this.menubarVisibility;
	}

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
	getPanelPosition(): Position {
		return this.panelPosition;
	}

	setPanelPosition(position: Position): TPromise<void> {
		return (this.panelHidden ? this.setPanelHidden(false, true /* Skip Layout */) : TPromise.as(undefined)).then(() => {
			const newPositionValue = (position === Position.BOTTOM) ? 'bottom' : 'right';
			const oldPositionValue = (this.panelPosition === Position.BOTTOM) ? 'bottom' : 'right';
			this.panelPosition = position;
			this.storageService.store(Workbench.panelPositionStorageKey, Position[this.panelPosition].toLowerCase(), StorageScope.WORKSPACE);

			// Adjust CSS
			DOM.removeClass(this.panelPart.getContainer(), oldPositionValue);
			DOM.addClass(this.panelPart.getContainer(), newPositionValue);

			// Update Styles
			this.panelPart.updateStyles();

			// Layout
			this.workbenchLayout.layout();
		});
1499
	}
1500 1501

	//#endregion
I
isidor 已提交
1502
}