workbench.ts 68.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import 'vs/css!./media/workbench';
7

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

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

I
isidor 已提交
125 126
interface IZenModeSettings {
	fullScreen: boolean;
127
	centerLayout: boolean;
I
isidor 已提交
128
	hideTabs: boolean;
129
	hideActivityBar: boolean;
I
isidor 已提交
130
	hideStatusBar: boolean;
I
isidor 已提交
131
	hideLineNumbers: 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
interface IZenMode {
	active: boolean;
	transitionedToFullScreen: boolean;
	transitionedToCenteredEditorLayout: boolean;
	transitionDisposeables: IDisposable[];
	wasSideBarVisible: boolean;
	wasPanelVisible: boolean;
}

173 174 175 176 177 178
interface IWorkbenchUIState {
	lastPanelHeight?: number;
	lastPanelWidth?: number;
	lastSidebarDimension?: number;
}

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

181
	private static readonly sidebarHiddenStorageKey = 'workbench.sidebar.hidden';
182
	private static readonly menubarVisibilityConfigurationKey = 'window.menuBarVisibility';
183 184
	private static readonly panelHiddenStorageKey = 'workbench.panel.hidden';
	private static readonly zenModeActiveStorageKey = 'workbench.zenmode.active';
185
	private static readonly centeredEditorLayoutActiveStorageKey = 'workbench.centerededitorlayout.active';
186
	private static readonly panelPositionStorageKey = 'workbench.panel.location';
187
	private static readonly defaultPanelPositionStorageKey = 'workbench.panel.defaultLocation';
188 189 190 191 192
	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';
193

194
	_serviceBrand: any;
195

E
Erich Gamma 已提交
196
	private workbenchParams: WorkbenchParams;
B
Benjamin Pasero 已提交
197
	private workbench: HTMLElement;
E
Erich Gamma 已提交
198
	private workbenchStarted: boolean;
B
Benjamin Pasero 已提交
199
	private workbenchRestored: boolean;
E
Erich Gamma 已提交
200
	private workbenchShutdown: boolean;
201

202 203
	private editorService: EditorService;
	private editorGroupService: IEditorGroupsService;
J
Joao Moreno 已提交
204
	private contextViewService: ContextViewService;
205
	private contextKeyService: IContextKeyService;
206
	private keybindingService: IKeybindingService;
207
	private backupFileService: IBackupFileService;
208
	private fileService: IFileService;
209 210
	private quickInput: QuickInputService;

S
SteVen Batten 已提交
211
	private workbenchGrid: Grid<View> | WorkbenchLayout;
212

B
Benjamin Pasero 已提交
213
	private titlebarPart: TitlebarPart;
E
Erich Gamma 已提交
214 215
	private activitybarPart: ActivitybarPart;
	private sidebarPart: SidebarPart;
I
isidor 已提交
216
	private panelPart: PanelPart;
E
Erich Gamma 已提交
217 218
	private editorPart: EditorPart;
	private statusbarPart: StatusbarPart;
S
SteVen Batten 已提交
219 220 221 222 223 224 225 226

	private titlebarPartView: View;
	private activitybarPartView: View;
	private sidebarPartView: View;
	private panelPartView: View;
	private editorPartView: View;
	private statusbarPartView: View;

E
Erich Gamma 已提交
227
	private quickOpen: QuickOpenController;
228
	private notificationsCenter: NotificationsCenter;
229
	private notificationsToasts: NotificationsToasts;
230

S
SteVen Batten 已提交
231
	private editorHidden: boolean;
E
Erich Gamma 已提交
232
	private sideBarHidden: boolean;
233
	private statusBarHidden: boolean;
S
Sanders Lauture 已提交
234
	private activityBarHidden: boolean;
235
	private menubarToggled: boolean;
E
Erich Gamma 已提交
236
	private sideBarPosition: Position;
I
isidor 已提交
237
	private panelPosition: Position;
I
isidor 已提交
238
	private panelHidden: boolean;
S
SteVen Batten 已提交
239
	private menubarVisibility: MenuBarVisibility;
240 241
	private zenMode: IZenMode;
	private fontAliasing: FontAliasingOption;
242
	private hasInitialFilesToOpen: boolean;
243
	private shouldCenterLayout = false;
244 245 246 247 248
	private uiState: IWorkbenchUIState = {
		lastPanelHeight: 350,
		lastPanelWidth: 350,
		lastSidebarDimension: 300,
	};
249

I
isidor 已提交
250
	private inZenMode: IContextKey<boolean>;
251
	private sideBarVisibleContext: IContextKey<boolean>;
252

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

255
	constructor(
256
		private container: HTMLElement,
J
Joao Moreno 已提交
257
		private configuration: IWindowConfiguration,
258
		serviceCollection: ServiceCollection,
259
		private lifecycleService: LifecycleService,
J
Joao Moreno 已提交
260
		private mainProcessClient: IPCClient,
261 262 263 264 265 266 267 268
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
		@IStorageService private readonly storageService: IStorageService,
		@IConfigurationService private readonly configurationService: WorkspaceService,
		@IWorkbenchThemeService private readonly themeService: WorkbenchThemeService,
		@IEnvironmentService private readonly environmentService: IEnvironmentService,
		@IWindowService private readonly windowService: IWindowService,
		@INotificationService private readonly notificationService: NotificationService
269
	) {
270
		super();
E
Erich Gamma 已提交
271

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

274
		this.hasInitialFilesToOpen =
B
Benjamin Pasero 已提交
275 276 277
			(configuration.filesToCreate && configuration.filesToCreate.length > 0) ||
			(configuration.filesToOpen && configuration.filesToOpen.length > 0) ||
			(configuration.filesToDiff && configuration.filesToDiff.length > 0);
278 279
	}

J
Johannes Rieken 已提交
280
	startup(): Promise<IWorkbenchStartedInfo> {
281
		this.workbenchStarted = true;
E
Erich Gamma 已提交
282

B
Benjamin Pasero 已提交
283
		// Create Workbench Container
284
		this.createWorkbench();
E
Erich Gamma 已提交
285

286 287
		// Install some global actions
		this.createGlobalActions();
288

289 290
		// Services
		this.initServices();
E
Erich Gamma 已提交
291

292 293
		// Context Keys
		this.handleContextKeys();
J
Joao Moreno 已提交
294

295 296
		// Register Listeners
		this.registerListeners();
E
Erich Gamma 已提交
297

298 299
		// Settings
		this.initSettings();
E
Erich Gamma 已提交
300

301 302
		// Create Workbench and Parts
		this.renderWorkbench();
303

304 305
		// Workbench Layout
		this.createWorkbenchLayout();
B
polish  
Benjamin Pasero 已提交
306

J
Joao Moreno 已提交
307 308
		// Driver
		if (this.environmentService.driverHandle) {
309
			registerWindowDriver(this.mainProcessClient, this.configuration.windowId, this.instantiationService).then(disposable => this._register(disposable));
J
Joao Moreno 已提交
310 311
		}

312
		// Restore Parts
B
Benjamin Pasero 已提交
313 314
		return this.restoreParts();
	}
315

B
Benjamin Pasero 已提交
316
	private createWorkbench(): void {
B
Benjamin Pasero 已提交
317 318
		this.workbench = document.createElement('div');
		this.workbench.id = Identifiers.WORKBENCH_CONTAINER;
319
		DOM.addClass(this.workbench, 'monaco-workbench');
S
SteVen Batten 已提交
320

B
Benjamin Pasero 已提交
321 322 323
		this._register(DOM.addDisposableListener(this.workbench, DOM.EventType.SCROLL, () => {
			this.workbench.scrollTop = 0; // Prevent workbench from scrolling #55456
		}));
324
	}
E
Erich Gamma 已提交
325

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

B
Benjamin Pasero 已提交
329
		// Actions registered here to adjust for developing vs built workbench
330
		const registry = Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions);
R
Rob Lourens 已提交
331 332
		registry.registerWorkbenchAction(new SyncActionDescriptor(ReloadWindowAction, ReloadWindowAction.ID, ReloadWindowAction.LABEL, isDeveloping ? { primary: KeyMod.CtrlCmd | KeyCode.KEY_R } : undefined), '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 } } : undefined), 'Developer: Toggle Developer Tools', localize('developer', "Developer"));
333 334
		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');
335
		registry.registerWorkbenchAction(new SyncActionDescriptor(LogStorageAction, LogStorageAction.ID, LogStorageAction.LABEL), 'Developer: Log Storage', localize('developer', "Developer"));
336

337
		// Actions for macOS native tabs management (only when enabled)
338
		const windowConfig = this.configurationService.getValue<IWindowConfiguration>();
339
		if (windowConfig && windowConfig.window && windowConfig.window.nativeTabs) {
340
			registry.registerWorkbenchAction(new SyncActionDescriptor(NewWindowTab, NewWindowTab.ID, NewWindowTab.LABEL), 'New Window Tab');
341 342 343 344 345
			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');
346
		}
347 348
	}

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

E
Erich Gamma 已提交
352
		// Services we contribute
353
		serviceCollection.set(IPartService, this);
E
Erich Gamma 已提交
354

355
		// Clipboard
J
Johannes Rieken 已提交
356
		serviceCollection.set(IClipboardService, new SyncDescriptor(ClipboardService));
357

358 359 360 361
		// Status bar
		this.statusbarPart = this.instantiationService.createInstance(StatusbarPart, Identifiers.STATUSBAR_PART);
		serviceCollection.set(IStatusbarService, this.statusbarPart);

362 363 364
		// Progress 2
		serviceCollection.set(IProgressService2, new SyncDescriptor(ProgressService2));

365
		// Keybindings
A
Alex Dima 已提交
366
		this.contextKeyService = this.instantiationService.createInstance(ContextKeyService);
367
		serviceCollection.set(IContextKeyService, this.contextKeyService);
368

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

372 373 374
		// List
		serviceCollection.set(IListService, this.instantiationService.createInstance(ListService));

J
Joao Moreno 已提交
375
		// Context view service
B
Benjamin Pasero 已提交
376
		this.contextViewService = this.instantiationService.createInstance(ContextViewService, this.workbench);
J
Joao Moreno 已提交
377 378
		serviceCollection.set(IContextViewService, this.contextViewService);

S
SteVen Batten 已提交
379
		// Use themable context menus when custom titlebar is enabled to match custom menubar
B
Benjamin Pasero 已提交
380
		if (!isMacintosh && this.useCustomTitleBarStyle()) {
J
fix npe  
Johannes Rieken 已提交
381
			serviceCollection.set(IContextMenuService, new SyncDescriptor(HTMLContextMenuService, [null]));
S
SteVen Batten 已提交
382 383 384
		} else {
			serviceCollection.set(IContextMenuService, new SyncDescriptor(NativeContextMenuService));
		}
385

386 387
		// Menus/Actions
		serviceCollection.set(IMenuService, new SyncDescriptor(MenuService));
388

P
Pine Wu 已提交
389
		// Sidebar part
390
		this.sidebarPart = this.instantiationService.createInstance(SidebarPart, Identifiers.SIDEBAR_PART);
P
Pine Wu 已提交
391 392

		// Viewlet service
I
isidor 已提交
393
		serviceCollection.set(IViewletService, this.sidebarPart);
E
Erich Gamma 已提交
394

I
isidor 已提交
395
		// Panel service (panel part)
396 397
		this.panelPart = this.instantiationService.createInstance(PanelPart, Identifiers.PANEL_PART);
		serviceCollection.set(IPanelService, this.panelPart);
I
isidor 已提交
398

399 400 401
		// views service
		const viewsService = this.instantiationService.createInstance(ViewsService);
		serviceCollection.set(IViewsService, viewsService);
S
Sandeep Somavarapu 已提交
402

E
Erich Gamma 已提交
403
		// Activity service (activitybar part)
404
		this.activitybarPart = this.instantiationService.createInstance(ActivitybarPart, Identifiers.ACTIVITYBAR_PART);
I
isidor 已提交
405 406
		const activityService = this.instantiationService.createInstance(ActivityService, this.activitybarPart, this.panelPart);
		serviceCollection.set(IActivityService, activityService);
E
Erich Gamma 已提交
407

408 409 410
		// File Service
		this.fileService = this.instantiationService.createInstance(RemoteFileService);
		serviceCollection.set(IFileService, this.fileService);
S
#47154  
Sandeep Somavarapu 已提交
411
		this.configurationService.acquireFileService(this.fileService);
A
Alex Dima 已提交
412
		this.themeService.acquireFileService(this.fileService);
413

414 415
		// Editor and Group services
		const restorePreviousEditorState = !this.hasInitialFilesToOpen;
416
		this.editorPart = this.instantiationService.createInstance(EditorPart, Identifiers.EDITOR_PART, restorePreviousEditorState);
B
Benjamin Pasero 已提交
417
		this.editorGroupService = this.editorPart;
418 419 420
		serviceCollection.set(IEditorGroupsService, this.editorPart);
		this.editorService = this.instantiationService.createInstance(EditorService);
		serviceCollection.set(IEditorService, this.editorService);
E
Erich Gamma 已提交
421

422 423 424
		// Title bar
		this.titlebarPart = this.instantiationService.createInstance(TitlebarPart, Identifiers.TITLEBAR_PART);
		serviceCollection.set(ITitleService, this.titlebarPart);
B
Benjamin Pasero 已提交
425

426
		// History
427
		serviceCollection.set(IHistoryService, new SyncDescriptor(HistoryService));
428

M
Martin Aeschlimann 已提交
429 430 431
		// File Dialogs
		serviceCollection.set(IFileDialogService, new SyncDescriptor(FileDialogService));

432
		// Backup File Service
B
Benjamin Pasero 已提交
433 434 435 436 437
		if (this.workbenchParams.configuration.backupPath) {
			this.backupFileService = this.instantiationService.createInstance(BackupFileService, this.workbenchParams.configuration.backupPath);
		} else {
			this.backupFileService = new InMemoryBackupFileService();
		}
438
		serviceCollection.set(IBackupFileService, this.backupFileService);
439

440
		// Text File Service
441
		serviceCollection.set(ITextFileService, new SyncDescriptor(TextFileService));
442

443
		// File Decorations
J
Johannes Rieken 已提交
444
		serviceCollection.set(IDecorationsService, new SyncDescriptor(FileDecorationsService));
445

J
Joao Moreno 已提交
446
		// SCM Service
447
		serviceCollection.set(ISCMService, new SyncDescriptor(SCMService));
448

J
Joao Moreno 已提交
449
		// Inactive extension URL handler
J
Joao Moreno 已提交
450
		serviceCollection.set(IExtensionUrlHandler, new SyncDescriptor(ExtensionUrlHandler));
J
Joao Moreno 已提交
451

B
Benjamin Pasero 已提交
452
		// Text Model Resolver Service
453
		serviceCollection.set(ITextModelService, new SyncDescriptor(TextModelResolverService));
B
Benjamin Pasero 已提交
454

455 456 457 458
		// JSON Editing
		const jsonEditingService = this.instantiationService.createInstance(JSONEditingService);
		serviceCollection.set(IJSONEditingService, jsonEditingService);

459 460 461
		// Workspace Editing
		serviceCollection.set(IWorkspaceEditingService, new SyncDescriptor(WorkspaceEditingService));

462 463 464
		// Keybinding Editing
		serviceCollection.set(IKeybindingEditingService, this.instantiationService.createInstance(KeybindingsEditingService));

465
		// Configuration Resolver
466
		serviceCollection.set(IConfigurationResolverService, new SyncDescriptor(ConfigurationResolverService, [process.env]));
467

E
Erich Gamma 已提交
468
		// Quick open service (quick open controller)
469 470
		this.quickOpen = this.instantiationService.createInstance(QuickOpenController);
		serviceCollection.set(IQuickOpenService, this.quickOpen);
471

C
Christof Marti 已提交
472 473 474 475
		// Quick input service
		this.quickInput = this.instantiationService.createInstance(QuickInputService);
		serviceCollection.set(IQuickInputService, this.quickInput);

S
Sandeep Somavarapu 已提交
476 477 478
		// PreferencesService
		serviceCollection.set(IPreferencesService, this.instantiationService.createInstance(PreferencesService));

B
polish  
Benjamin Pasero 已提交
479
		// Contributed services
B
Benjamin Pasero 已提交
480
		const contributedServices = getServices();
E
Erich Gamma 已提交
481
		for (let contributedService of contributedServices) {
482
			serviceCollection.set(contributedService.id, contributedService.descriptor);
E
Erich Gamma 已提交
483 484 485
		}

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

490
		this.instantiationService.createInstance(DefaultConfigurationExportHelper);
491

S
#47154  
Sandeep Somavarapu 已提交
492
		this.configurationService.acquireInstantiationService(this.getInstantiationService());
E
Erich Gamma 已提交
493 494
	}

495
	//#region event handling
E
Erich Gamma 已提交
496

497
	private registerListeners(): void {
I
isidor 已提交
498

499
		// Storage
500
		this._register(this.storageService.onWillSaveState(e => this.saveState(e)));
501

502
		// Listen to visible editor changes
503
		this._register(this.editorService.onDidVisibleEditorsChange(() => this.onDidVisibleEditorsChange()));
B
Benjamin Pasero 已提交
504

505 506 507
		// Listen to editor group activations when editor is hidden
		this._register(this.editorPart.onDidActivateGroup(() => { if (this.editorHidden) { this.setEditorHidden(false); } }));

508 509 510
		// Listen to editor closing (if we run with --wait)
		const filesToWait = this.workbenchParams.configuration.filesToWait;
		if (filesToWait) {
511
			const resourcesToWaitFor = filesToWait.paths.map(p => p.fileUri);
512
			const waitMarkerFile = URI.file(filesToWait.waitMarkerFilePath);
513
			const listenerDispose = this.editorService.onDidCloseEditor(() => this.onEditorClosed(listenerDispose, resourcesToWaitFor, waitMarkerFile));
I
isidor 已提交
514

515
			this._register(listenerDispose);
516
		}
S
Sanders Lauture 已提交
517

518
		// Configuration changes
519
		this._register(this.configurationService.onDidChangeConfiguration(() => this.onDidUpdateConfiguration()));
I
isidor 已提交
520

521
		// Fullscreen changes
522
		this._register(browser.onDidChangeFullscreen(() => this.onFullscreenChanged()));
B
Benjamin Pasero 已提交
523 524 525 526

		// Group changes
		this._register(this.editorGroupService.onDidAddGroup(() => this.centerEditorLayout(this.shouldCenterLayout)));
		this._register(this.editorGroupService.onDidRemoveGroup(() => this.centerEditorLayout(this.shouldCenterLayout)));
527
	}
528

529
	private onFullscreenChanged(): void {
S
SrTobi 已提交
530

531 532 533
		// Apply as CSS class
		const isFullscreen = browser.isFullscreen();
		if (isFullscreen) {
B
Benjamin Pasero 已提交
534
			DOM.addClass(this.workbench, 'fullscreen');
535
		} else {
B
Benjamin Pasero 已提交
536 537
			DOM.removeClass(this.workbench, 'fullscreen');

538 539 540 541
			if (this.zenMode.transitionedToFullScreen && this.zenMode.active) {
				this.toggleZenMode();
			}
		}
E
Erich Gamma 已提交
542

543
		// Changing fullscreen state of the window has an impact on custom title bar visibility, so we need to update
B
Benjamin Pasero 已提交
544
		if (this.useCustomTitleBarStyle()) {
545 546 547
			this._onTitleBarVisibilityChange.fire();
			this.layout(); // handle title bar when fullscreen changes
		}
548 549
	}

550 551 552 553
	private onMenubarToggled(visible: boolean) {
		if (visible !== this.menubarToggled) {
			this.menubarToggled = visible;

554 555
			if (browser.isFullscreen() && (this.menubarVisibility === 'toggle' || this.menubarVisibility === 'default')) {
				this._onTitleBarVisibilityChange.fire();
556 557 558 559 560
				this.layout();
			}
		}
	}

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

563 564 565
		// 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.
566
		if (resourcesToWaitFor.every(resource => !this.editorService.isOpen({ resource }))) {
567
			listenerDispose.dispose();
568
			this.fileService.del(waitMarkerFile);
569
		}
E
Erich Gamma 已提交
570 571
	}

572
	private onDidVisibleEditorsChange(): void {
573
		const visibleEditors = this.editorService.visibleControls;
E
Erich Gamma 已提交
574

575 576 577 578 579 580 581 582 583
		// 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();
			}
		}
584 585 586 587

		if (this.editorHidden) {
			this.setEditorHidden(false);
		}
588 589
	}

590
	private onAllEditorsClosed(): void {
591
		const visibleEditors = this.editorService.visibleControls.length;
592 593
		if (visibleEditors === 0) {
			this.windowService.closeWindow();
E
Erich Gamma 已提交
594 595 596
		}
	}

597 598 599 600 601
	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);
602
		}
E
Erich Gamma 已提交
603

604
		this.setPanelPositionFromStorageOrConfig();
E
Erich Gamma 已提交
605

606 607 608
		const fontAliasing = this.configurationService.getValue<FontAliasingOption>(Workbench.fontAliasingConfigurationKey);
		if (fontAliasing !== this.fontAliasing) {
			this.setFontAliasing(fontAliasing);
609 610
		}

611 612 613 614 615
		if (!this.zenMode.active) {
			const newStatusbarHiddenValue = !this.configurationService.getValue<boolean>(Workbench.statusbarVisibleConfigurationKey);
			if (newStatusbarHiddenValue !== this.statusBarHidden) {
				this.setStatusBarHidden(newStatusbarHiddenValue, skipLayout);
			}
616

617 618 619 620
			const newActivityBarHiddenValue = !this.configurationService.getValue<boolean>(Workbench.activityBarVisibleConfigurationKey);
			if (newActivityBarHiddenValue !== this.activityBarHidden) {
				this.setActivityBarHidden(newActivityBarHiddenValue, skipLayout);
			}
B
Benjamin Pasero 已提交
621
		}
S
SteVen Batten 已提交
622

623
		const newMenubarVisibility = this.configurationService.getValue<MenuBarVisibility>(Workbench.menubarVisibilityConfigurationKey);
S
SteVen Batten 已提交
624
		this.setMenubarVisibility(newMenubarVisibility, skipLayout);
625
	}
B
Benjamin Pasero 已提交
626

627
	//#endregion
B
Benjamin Pasero 已提交
628

629 630
	private handleContextKeys(): void {
		this.inZenMode = InEditorZenModeContext.bindTo(this.contextKeyService);
631

I
isidor 已提交
632 633 634
		IsMacContext.bindTo(this.contextKeyService);
		IsLinuxContext.bindTo(this.contextKeyService);
		IsWindowsContext.bindTo(this.contextKeyService);
635

636 637
		const sidebarVisibleContextRaw = new RawContextKey<boolean>('sidebarVisible', false);
		this.sideBarVisibleContext = sidebarVisibleContextRaw.bindTo(this.contextKeyService);
638

639
		const activeEditorContext = ActiveEditorContext.bindTo(this.contextKeyService);
640 641
		const editorsVisibleContext = EditorsVisibleContext.bindTo(this.contextKeyService);
		const textCompareEditorVisible = TextCompareEditorVisibleContext.bindTo(this.contextKeyService);
642
		const textCompareEditorActive = TextCompareEditorActiveContext.bindTo(this.contextKeyService);
643 644
		const activeEditorGroupEmpty = ActiveEditorGroupEmptyContext.bindTo(this.contextKeyService);
		const multipleEditorGroups = MultipleEditorGroupsContext.bindTo(this.contextKeyService);
645

646
		const updateEditorContextKeys = () => {
647
			const activeControl = this.editorService.activeControl;
648
			const visibleEditors = this.editorService.visibleControls;
E
Erich Gamma 已提交
649

650
			textCompareEditorActive.set(activeControl && activeControl.getId() === TEXT_DIFF_EDITOR_ID);
651
			textCompareEditorVisible.set(visibleEditors.some(control => control.getId() === TEXT_DIFF_EDITOR_ID));
I
isidor 已提交
652

653 654 655 656
			if (visibleEditors.length > 0) {
				editorsVisibleContext.set(true);
			} else {
				editorsVisibleContext.reset();
657
			}
B
Benjamin Pasero 已提交
658

659
			if (!this.editorService.activeEditor) {
660 661 662
				activeEditorGroupEmpty.set(true);
			} else {
				activeEditorGroupEmpty.reset();
663
			}
664

B
Benjamin Pasero 已提交
665
			if (this.editorGroupService.count > 1) {
666 667 668 669
				multipleEditorGroups.set(true);
			} else {
				multipleEditorGroups.reset();
			}
670 671 672 673 674 675

			if (activeControl) {
				activeEditorContext.set(activeControl.getId());
			} else {
				activeEditorContext.reset();
			}
676
		};
B
Benjamin Pasero 已提交
677

678
		this.editorPart.whenRestored.then(() => updateEditorContextKeys());
679 680
		this._register(this.editorService.onDidActiveEditorChange(() => updateEditorContextKeys()));
		this._register(this.editorService.onDidVisibleEditorsChange(() => updateEditorContextKeys()));
B
Benjamin Pasero 已提交
681 682
		this._register(this.editorGroupService.onDidAddGroup(() => updateEditorContextKeys()));
		this._register(this.editorGroupService.onDidRemoveGroup(() => updateEditorContextKeys()));
683

684
		const inputFocused = InputFocusedContext.bindTo(this.contextKeyService);
B
Benjamin Pasero 已提交
685 686 687 688 689 690 691 692 693 694 695

		function activeElementIsInput(): boolean {
			return document.activeElement && (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA');
		}

		function trackInputFocus(): void {
			const isInputFocused = activeElementIsInput();
			inputFocused.set(isInputFocused);

			if (isInputFocused) {
				const tracker = DOM.trackFocus(document.activeElement as HTMLElement);
J
Joao Moreno 已提交
696
				Event.once(tracker.onDidBlur)(() => {
B
Benjamin Pasero 已提交
697 698 699 700 701 702 703 704
					inputFocused.set(activeElementIsInput());

					tracker.dispose();
				});
			}
		}

		this._register(DOM.addDisposableListener(window, 'focusin', () => trackInputFocus(), true));
705 706 707 708 709 710

		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 已提交
711

712 713 714 715 716
		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);
		}));
717

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

720 721 722 723
		const updateSplitEditorsVerticallyContext = () => {
			const direction = preferredSideBySideGroupDirection(this.configurationService);
			splitEditorsVerticallyContext.set(direction === GroupDirection.DOWN);
		};
S
Sanders Lauture 已提交
724

725 726 727 728 729
		this._register(this.configurationService.onDidChangeConfiguration(e => {
			if (e.affectsConfiguration('workbench.editor.openSideBySideDirection')) {
				updateSplitEditorsVerticallyContext();
			}
		}));
E
Erich Gamma 已提交
730

731
		updateSplitEditorsVerticallyContext();
732
	}
E
Erich Gamma 已提交
733

J
Johannes Rieken 已提交
734 735
	private restoreParts(): Promise<IWorkbenchStartedInfo> {
		const restorePromises: Promise<any>[] = [];
B
Benjamin Pasero 已提交
736

737 738
		// Restore Editorpart
		perf.mark('willRestoreEditors');
739
		restorePromises.push(this.editorPart.whenRestored.then(() => {
B
Benjamin Pasero 已提交
740 741 742 743

			function openEditors(editors: IResourceEditor[], editorService: IEditorService) {
				if (editors.length) {
					return editorService.openEditors(editors);
744
				}
E
Erich Gamma 已提交
745

B
Benjamin Pasero 已提交
746 747 748 749 750 751 752 753 754 755 756
				return Promise.resolve();
			}

			const editorsToOpen = this.resolveEditorsToOpen();

			if (Array.isArray(editorsToOpen)) {
				return openEditors(editorsToOpen, this.editorService);
			}

			return editorsToOpen.then(editors => openEditors(editors, this.editorService));
		}).then(() => perf.mark('didRestoreEditors')));
E
Erich Gamma 已提交
757

758 759 760 761
		// Restore Sidebar
		let viewletIdToRestore: string;
		if (!this.sideBarHidden) {
			this.sideBarVisibleContext.set(true);
762

763
			if (this.shouldRestoreLastOpenedViewlet()) {
764
				viewletIdToRestore = this.storageService.get(SidebarPart.activeViewletSettingsKey, StorageScope.WORKSPACE);
765
			}
766

767
			if (!viewletIdToRestore) {
I
isidor 已提交
768
				viewletIdToRestore = this.sidebarPart.getDefaultViewletId();
769
			}
E
Erich Gamma 已提交
770

771
			perf.mark('willRestoreViewlet');
I
isidor 已提交
772 773
			restorePromises.push(this.sidebarPart.openViewlet(viewletIdToRestore)
				.then(viewlet => viewlet || this.sidebarPart.openViewlet(this.sidebarPart.getDefaultViewletId()))
B
Benjamin Pasero 已提交
774
				.then(() => perf.mark('didRestoreViewlet')));
775 776
		}

777 778
		// Restore Panel
		const panelRegistry = Registry.as<PanelRegistry>(PanelExtensions.Panels);
779
		const panelId = this.storageService.get(PanelPart.activePanelSettingsKey, StorageScope.WORKSPACE, panelRegistry.getDefaultPanelId());
780
		if (!this.panelHidden && !!panelId) {
781
			perf.mark('willRestorePanel');
782
			const isPanelToRestoreEnabled = !!this.panelPart.getPanels().filter(p => p.id === panelId).length;
783
			const panelIdToRestore = isPanelToRestoreEnabled ? panelId : panelRegistry.getDefaultPanelId();
784 785
			this.panelPart.openPanel(panelIdToRestore, false);
			perf.mark('didRestorePanel');
I
isidor 已提交
786 787
		}

788 789 790
		// Restore Zen Mode if active and supported for restore on startup
		const zenConfig = this.configurationService.getValue<IZenModeSettings>('zenMode');
		const wasZenActive = this.storageService.getBoolean(Workbench.zenModeActiveStorageKey, StorageScope.WORKSPACE, false);
791
		if (wasZenActive && zenConfig.restore) {
I
isidor 已提交
792
			this.toggleZenMode(true, true);
I
isidor 已提交
793 794
		}

B
Benjamin Pasero 已提交
795
		// Restore Forced Editor Center Mode
796
		if (this.storageService.getBoolean(Workbench.centeredEditorLayoutActiveStorageKey, StorageScope.WORKSPACE, false)) {
I
isidor 已提交
797
			this.centerEditorLayout(true);
S
SrTobi 已提交
798
		}
799

800
		const onRestored = (error?: Error): IWorkbenchStartedInfo => {
B
Benjamin Pasero 已提交
801 802 803 804 805
			this.workbenchRestored = true;

			// Set lifecycle phase to `Restored`
			this.lifecycleService.phase = LifecyclePhase.Restored;

806 807 808 809 810 811 812
			// Set lifecycle phase to `Eventually` after a short delay and when
			// idle (min 2.5sec, max 5sec)
			setTimeout(() => {
				this._register(runWhenIdle(() => {
					this.lifecycleService.phase = LifecyclePhase.Eventually;
				}, 2500));
			}, 2500);
813

814 815
			if (error) {
				errors.onUnexpectedError(error);
816
			}
I
isidor 已提交
817

818 819
			return {
				customKeybindingsCount: this.keybindingService.customKeybindingsCount(),
P
Pine Wu 已提交
820
				pinnedViewlets: this.activitybarPart.getPinnedViewletIds(),
821
				restoredViewlet: viewletIdToRestore,
822
				restoredEditorsCount: this.editorService.visibleEditors.length
823 824
			};
		};
825

B
Benjamin Pasero 已提交
826
		return Promise.all(restorePromises).then(() => onRestored(), error => onRestored(error));
E
Erich Gamma 已提交
827 828
	}

829 830 831
	private shouldRestoreLastOpenedViewlet(): boolean {
		if (!this.environmentService.isBuilt) {
			return true; // always restore sidebar when we are in development mode
E
Erich Gamma 已提交
832 833
		}

834 835
		// always restore sidebar when the window was reloaded
		return this.lifecycleService.startupKind === StartupKind.ReloadedWindow;
E
Erich Gamma 已提交
836 837
	}

J
Johannes Rieken 已提交
838
	private resolveEditorsToOpen(): Promise<IResourceEditor[]> | IResourceEditor[] {
839
		const config = this.workbenchParams.configuration;
I
isidor 已提交
840

B
Benjamin Pasero 已提交
841
		// Files to open, diff or create
842
		if (this.hasInitialFilesToOpen) {
I
isidor 已提交
843

B
Benjamin Pasero 已提交
844
			// Files to diff is exclusive
845
			const filesToDiff = this.toInputs(config.filesToDiff, false);
846
			if (filesToDiff && filesToDiff.length === 2) {
B
Benjamin Pasero 已提交
847
				return [<IResourceDiffInput>{
848 849
					leftResource: filesToDiff[0].resource,
					rightResource: filesToDiff[1].resource,
B
Benjamin Pasero 已提交
850
					options: { pinned: true },
B
Benjamin Pasero 已提交
851
					forceFile: true
B
Benjamin Pasero 已提交
852
				}];
B
Benjamin Pasero 已提交
853
			}
I
isidor 已提交
854

855 856
			const filesToCreate = this.toInputs(config.filesToCreate, true);
			const filesToOpen = this.toInputs(config.filesToOpen, false);
I
isidor 已提交
857

858
			// Otherwise: Open/Create files
B
Benjamin Pasero 已提交
859
			return [...filesToOpen, ...filesToCreate];
B
Benjamin Pasero 已提交
860
		}
I
isidor 已提交
861

862
		// Empty workbench
863
		else if (this.contextService.getWorkbenchState() === WorkbenchState.EMPTY && this.openUntitledFile()) {
B
Benjamin Pasero 已提交
864
			const isEmpty = this.editorGroupService.count === 1 && this.editorGroupService.activeGroup.count === 0;
865
			if (!isEmpty) {
B
Benjamin Pasero 已提交
866
				return []; // do not open any empty untitled file if we restored editors from previous session
867
			}
B
Benjamin Pasero 已提交
868

869 870
			return this.backupFileService.hasBackups().then(hasBackups => {
				if (hasBackups) {
B
Benjamin Pasero 已提交
871
					return []; // do not open any empty untitled file if we have backups to restore
872
				}
B
Benjamin Pasero 已提交
873

B
Benjamin Pasero 已提交
874
				return [<IUntitledResourceInput>{}];
875
			});
876
		}
877

B
Benjamin Pasero 已提交
878
		return [];
B
Benjamin Pasero 已提交
879
	}
880

881
	private toInputs(paths: IPath[], isNew: boolean): Array<IResourceInput | IUntitledResourceInput> {
B
Benjamin Pasero 已提交
882 883
		if (!paths || !paths.length) {
			return [];
884
		}
I
isidor 已提交
885

B
Benjamin Pasero 已提交
886
		return paths.map(p => {
887
			const resource = p.fileUri;
888 889 890 891
			let input: IResourceInput | IUntitledResourceInput;
			if (isNew) {
				input = { filePath: resource.fsPath, options: { pinned: true } } as IUntitledResourceInput;
			} else {
B
Benjamin Pasero 已提交
892
				input = { resource, options: { pinned: true }, forceFile: true } as IResourceInput;
893
			}
E
Erich Gamma 已提交
894

895
			if (!isNew && p.lineNumber) {
B
Benjamin Pasero 已提交
896 897 898 899 900
				input.options.selection = {
					startLineNumber: p.lineNumber,
					startColumn: p.columnNumber
				};
			}
901

B
Benjamin Pasero 已提交
902 903
			return input;
		});
904 905
	}

906
	private openUntitledFile() {
907
		const startupEditor = this.configurationService.inspect('workbench.startupEditor');
B
Benjamin Pasero 已提交
908 909

		// Fallback to previous workbench.welcome.enabled setting in case startupEditor is not defined
910
		if (!startupEditor.user && !startupEditor.workspace) {
911 912 913
			const welcomeEnabledValue = this.configurationService.getValue('workbench.welcome.enabled');
			if (typeof welcomeEnabledValue === 'boolean') {
				return !welcomeEnabledValue;
914
			}
915
		}
E
Erich Gamma 已提交
916

917
		return startupEditor.value === 'newUntitledFile';
918
	}
E
Erich Gamma 已提交
919 920

	private initSettings(): void {
921

S
SteVen Batten 已提交
922 923 924
		// Editor visiblity
		this.editorHidden = false;

E
Erich Gamma 已提交
925
		// Sidebar visibility
926
		this.sideBarHidden = this.storageService.getBoolean(Workbench.sidebarHiddenStorageKey, StorageScope.WORKSPACE, this.contextService.getWorkbenchState() === WorkbenchState.EMPTY);
927

I
isidor 已提交
928
		// Panel part visibility
929
		const panelRegistry = Registry.as<PanelRegistry>(PanelExtensions.Panels);
930
		this.panelHidden = this.storageService.getBoolean(Workbench.panelHiddenStorageKey, StorageScope.WORKSPACE, true);
931 932
		if (!panelRegistry.getDefaultPanelId()) {
			this.panelHidden = true; // we hide panel part if there is no default panel
933 934
		}

E
Erich Gamma 已提交
935
		// Sidebar position
936
		const sideBarPosition = this.configurationService.getValue<string>(Workbench.sidebarPositionConfigurationKey);
937
		this.sideBarPosition = (sideBarPosition === 'right') ? Position.RIGHT : Position.LEFT;
938

I
isidor 已提交
939
		// Panel position
940
		this.setPanelPositionFromStorageOrConfig();
941

942
		// Menubar visibility
943
		const menuBarVisibility = this.configurationService.getValue<MenuBarVisibility>(Workbench.menubarVisibilityConfigurationKey);
S
SteVen Batten 已提交
944
		this.setMenubarVisibility(menuBarVisibility, true);
945

B
Benjamin Pasero 已提交
946
		// Statusbar visibility
947
		const statusBarVisible = this.configurationService.getValue<string>(Workbench.statusbarVisibleConfigurationKey);
948
		this.statusBarHidden = !statusBarVisible;
949

S
Sanders Lauture 已提交
950
		// Activity bar visibility
951
		const activityBarVisible = this.configurationService.getValue<string>(Workbench.activityBarVisibleConfigurationKey);
S
Sanders Lauture 已提交
952
		this.activityBarHidden = !activityBarVisible;
953

954
		// Font aliasing
955
		this.fontAliasing = this.configurationService.getValue<FontAliasingOption>(Workbench.fontAliasingConfigurationKey);
956

I
isidor 已提交
957 958
		// Zen mode
		this.zenMode = {
I
isidor 已提交
959
			active: false,
960
			transitionedToFullScreen: false,
961
			transitionedToCenteredEditorLayout: false,
962
			wasSideBarVisible: false,
963 964
			wasPanelVisible: false,
			transitionDisposeables: []
I
isidor 已提交
965
		};
E
Erich Gamma 已提交
966 967
	}

968 969
	private setPanelPositionFromStorageOrConfig() {
		const defaultPanelPosition = this.configurationService.getValue<string>(Workbench.defaultPanelPositionStorageKey);
970
		const panelPosition = this.storageService.get(Workbench.panelPositionStorageKey, StorageScope.WORKSPACE, defaultPanelPosition);
971

972 973
		this.panelPosition = (panelPosition === 'right') ? Position.RIGHT : Position.BOTTOM;
	}
974

B
Benjamin Pasero 已提交
975 976
	private useCustomTitleBarStyle(): boolean {
		return getTitleBarStyle(this.configurationService, this.environmentService) === 'custom';
B
Benjamin Pasero 已提交
977 978
	}

979
	private saveLastPanelDimension(): void {
S
SteVen Batten 已提交
980
		if (!(this.workbenchGrid instanceof Grid)) {
981 982 983 984
			return;
		}

		if (this.panelPosition === Position.BOTTOM) {
S
SteVen Batten 已提交
985
			this.uiState.lastPanelHeight = this.workbenchGrid.getViewSize(this.panelPartView);
986
		} else {
S
SteVen Batten 已提交
987
			this.uiState.lastPanelWidth = this.workbenchGrid.getViewSize(this.panelPartView);
988 989 990 991 992 993 994
		}
	}

	private getLastPanelDimension(position: Position): number | undefined {
		return position === Position.BOTTOM ? this.uiState.lastPanelHeight : this.uiState.lastPanelWidth;
	}

995 996
	private setStatusBarHidden(hidden: boolean, skipLayout?: boolean): void {
		this.statusBarHidden = hidden;
B
Benjamin Pasero 已提交
997

998 999
		// Adjust CSS
		if (hidden) {
B
Benjamin Pasero 已提交
1000
			DOM.addClass(this.workbench, 'nostatusbar');
E
Erich Gamma 已提交
1001
		} else {
B
Benjamin Pasero 已提交
1002
			DOM.removeClass(this.workbench, 'nostatusbar');
E
Erich Gamma 已提交
1003 1004
		}

1005 1006
		// Layout
		if (!skipLayout) {
S
SteVen Batten 已提交
1007
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1008
				this.layout();
1009 1010 1011
			} else {
				this.workbenchGrid.layout();
			}
1012 1013 1014
		}
	}

1015
	private setFontAliasing(aliasing: FontAliasingOption) {
1016
		this.fontAliasing = aliasing;
S
Sanders Lauture 已提交
1017

B
Benjamin Pasero 已提交
1018 1019 1020 1021 1022
		// Remove all
		document.body.classList.remove(...fontAliasingValues.map(value => `monaco-font-aliasing-${value}`));

		// Add specific
		if (fontAliasingValues.some(option => option === aliasing)) {
1023
			document.body.classList.add(`monaco-font-aliasing-${aliasing}`);
S
Sanders Lauture 已提交
1024
		}
1025 1026
	}

E
Erich Gamma 已提交
1027
	private createWorkbenchLayout(): void {
1028
		if (this.configurationService.getValue('workbench.useExperimentalGridLayout')) {
S
SteVen Batten 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037
			// Create view wrappers for all parts
			this.titlebarPartView = new View(this.titlebarPart);
			this.sidebarPartView = new View(this.sidebarPart);
			this.activitybarPartView = new View(this.activitybarPart);
			this.editorPartView = new View(this.editorPart);
			this.panelPartView = new View(this.panelPart);
			this.statusbarPartView = new View(this.statusbarPart);

			this.workbenchGrid = new Grid(this.editorPartView, { proportionalLayout: false });
1038

1039
			this.workbench.prepend(this.workbenchGrid.element);
1040
			this.layout();
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
		} else {
			this.workbenchGrid = this.instantiationService.createInstance(
				WorkbenchLayout,
				this.container,
				this.workbench,
				{
					titlebar: this.titlebarPart,
					activitybar: this.activitybarPart,
					editor: this.editorPart,
					sidebar: this.sidebarPart,
					panel: this.panelPart,
					statusbar: this.statusbarPart,
				},
				this.quickOpen,
				this.quickInput,
				this.notificationsCenter,
				this.notificationsToasts
			);
		}
E
Erich Gamma 已提交
1060 1061 1062 1063 1064 1065
	}

	private renderWorkbench(): void {

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

1069
		if (this.panelHidden) {
B
Benjamin Pasero 已提交
1070
			DOM.addClass(this.workbench, 'nopanel');
1071
		}
B
Benjamin Pasero 已提交
1072

1073
		if (this.statusBarHidden) {
B
Benjamin Pasero 已提交
1074
			DOM.addClass(this.workbench, 'nostatusbar');
1075
		}
E
Erich Gamma 已提交
1076

B
Benjamin Pasero 已提交
1077
		// Apply font aliasing
1078 1079
		this.setFontAliasing(this.fontAliasing);

1080 1081
		// Apply fullscreen state
		if (browser.isFullscreen()) {
B
Benjamin Pasero 已提交
1082
			DOM.addClass(this.workbench, 'fullscreen');
B
Benjamin Pasero 已提交
1083 1084
		}

E
Erich Gamma 已提交
1085
		// Create Parts
B
Benjamin Pasero 已提交
1086
		this.createTitlebarPart();
E
Erich Gamma 已提交
1087 1088 1089
		this.createActivityBarPart();
		this.createSidebarPart();
		this.createEditorPart();
1090
		this.createPanelPart();
E
Erich Gamma 已提交
1091 1092
		this.createStatusbarPart();

1093 1094
		// Notification Handlers
		this.createNotificationsHandlers();
1095

1096 1097

		// Menubar visibility changes
B
Benjamin Pasero 已提交
1098
		if ((isWindows || isLinux) && this.useCustomTitleBarStyle()) {
1099 1100 1101
			this.titlebarPart.onMenubarVisibilityChange()(e => this.onMenubarToggled(e));
		}

E
Erich Gamma 已提交
1102
		// Add Workbench to DOM
B
Benjamin Pasero 已提交
1103
		this.container.appendChild(this.workbench);
E
Erich Gamma 已提交
1104 1105
	}

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

B
Benjamin Pasero 已提交
1109
		this.titlebarPart.create(titlebarContainer);
B
Benjamin Pasero 已提交
1110 1111
	}

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

B
Benjamin Pasero 已提交
1115
		this.activitybarPart.create(activitybarPartContainer);
E
Erich Gamma 已提交
1116 1117 1118
	}

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

B
Benjamin Pasero 已提交
1121
		this.sidebarPart.create(sidebarPartContainer);
E
Erich Gamma 已提交
1122 1123
	}

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

B
Benjamin Pasero 已提交
1127
		this.panelPart.create(panelPartContainer);
I
isidor 已提交
1128 1129
	}

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

B
Benjamin Pasero 已提交
1133
		this.editorPart.create(editorContainer);
E
Erich Gamma 已提交
1134 1135 1136
	}

	private createStatusbarPart(): void {
B
Benjamin Pasero 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
		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);

1148 1149 1150 1151 1152
		if (!this.configurationService.getValue('workbench.useExperimentalGridLayout')) {
			// Insert all workbench parts at the beginning. Issue #52531
			// This is primarily for the title bar to allow overriding -webkit-app-region
			this.workbench.insertBefore(part, this.workbench.lastChild);
		}
E
Erich Gamma 已提交
1153

B
Benjamin Pasero 已提交
1154
		return part;
E
Erich Gamma 已提交
1155 1156
	}

1157 1158
	private createNotificationsHandlers(): void {

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

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

1165
		// Notifications Alerts
1166
		this._register(this.instantiationService.createInstance(NotificationsAlerts, this.notificationService.model));
1167 1168 1169 1170

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

1171
		// Eventing
1172
		this._register(this.notificationsCenter.onDidChangeVisibility(() => {
1173 1174 1175 1176 1177 1178 1179 1180

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

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

1181
		// Register Commands
1182
		registerNotificationCommands(this.notificationsCenter, this.notificationsToasts);
1183 1184
	}

1185
	getInstantiationService(): IInstantiationService {
E
Erich Gamma 已提交
1186 1187 1188
		return this.instantiationService;
	}

1189
	private saveState(e: IWillSaveStateEvent): void {
1190
		if (this.zenMode.active) {
B
Benjamin Pasero 已提交
1191
			this.storageService.store(Workbench.zenModeActiveStorageKey, true, StorageScope.WORKSPACE);
1192
		} else {
B
Benjamin Pasero 已提交
1193
			this.storageService.remove(Workbench.zenModeActiveStorageKey, StorageScope.WORKSPACE);
1194
		}
1195 1196 1197 1198 1199 1200 1201 1202

		if (e.reason === WillSaveStateReason.SHUTDOWN && this.zenMode.active) {
			const zenConfig = this.configurationService.getValue<IZenModeSettings>('zenMode');
			if (!zenConfig.restore) {
				// We will not restore zen mode, need to clear all zen mode state changes
				this.toggleZenMode(true);
			}
		}
1203 1204
	}

1205
	dispose(): void {
1206
		super.dispose();
1207 1208 1209 1210 1211 1212

		this.workbenchShutdown = true;
	}

	//#region IPartService

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

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

B
Benjamin Pasero 已提交
1218 1219
	isRestored(): boolean {
		return !!(this.workbenchRestored && this.workbenchStarted);
1220
	}
1221

1222 1223 1224 1225 1226
	hasFocus(part: Parts): boolean {
		const activeElement = document.activeElement;
		if (!activeElement) {
			return false;
		}
1227

1228 1229 1230
		const container = this.getContainer(part);
		return DOM.isAncestor(activeElement, container);
	}
1231

1232
	getContainer(part: Parts): HTMLElement {
1233
		let container: HTMLElement | null = null;
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
		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;
		}
1254

1255 1256
		return container;
	}
1257

1258 1259 1260
	isVisible(part: Parts): boolean {
		switch (part) {
			case Parts.TITLEBAR_PART:
B
Benjamin Pasero 已提交
1261
				if (!this.useCustomTitleBarStyle()) {
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
					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;
1274 1275 1276 1277 1278 1279 1280 1281
			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;
1282 1283
			case Parts.EDITOR_PART:
				return this.workbenchGrid instanceof Grid ? !this.editorHidden : true;
E
Erich Gamma 已提交
1284
		}
1285

1286
		return true; // any other part cannot be hidden
E
Erich Gamma 已提交
1287 1288
	}

1289 1290 1291
	getTitleBarOffset(): number {
		let offset = 0;
		if (this.isVisible(Parts.TITLEBAR_PART)) {
S
SteVen Batten 已提交
1292 1293
			if (this.workbenchGrid instanceof Grid) {
				offset = this.gridHasView(this.titlebarPartView) ? this.workbenchGrid.getViewSize2(this.titlebarPartView).height : 0;
S
SteVen Batten 已提交
1294 1295 1296 1297
			} else {
				offset = this.workbenchGrid.partLayoutInfo.titlebar.height;
			}

1298 1299 1300
			if (isMacintosh || this.menubarVisibility === 'hidden') {
				offset /= browser.getZoomFactor();
			}
E
Erich Gamma 已提交
1301
		}
1302 1303

		return offset;
E
Erich Gamma 已提交
1304
	}
1305

1306 1307
	getWorkbenchElement(): HTMLElement {
		return this.workbench;
1308
	}
1309

I
isidor 已提交
1310
	toggleZenMode(skipLayout?: boolean, restoring = false): void {
I
isidor 已提交
1311
		this.zenMode.active = !this.zenMode.active;
1312
		this.zenMode.transitionDisposeables = dispose(this.zenMode.transitionDisposeables);
1313

1314 1315
		// 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)
1316
		let toggleFullScreen = false;
I
isidor 已提交
1317 1318 1319 1320 1321 1322 1323 1324
		const setLineNumbers = (lineNumbers: any) => {
			this.editorService.visibleControls.forEach(editor => {
				const control = <IEditor>editor.getControl();
				if (control) {
					control.updateOptions({ lineNumbers });
				}
			});
		};
1325 1326

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

I
isidor 已提交
1330
			toggleFullScreen = !browser.isFullscreen() && config.fullScreen;
I
isidor 已提交
1331
			this.zenMode.transitionedToFullScreen = restoring ? config.fullScreen : toggleFullScreen;
1332
			this.zenMode.transitionedToCenteredEditorLayout = !this.isEditorLayoutCentered() && config.centerLayout;
1333 1334
			this.zenMode.wasSideBarVisible = this.isVisible(Parts.SIDEBAR_PART);
			this.zenMode.wasPanelVisible = this.isVisible(Parts.PANEL_PART);
1335

1336 1337
			this.setPanelHidden(true, true);
			this.setSideBarHidden(true, true);
I
isidor 已提交
1338

1339 1340 1341
			if (config.hideActivityBar) {
				this.setActivityBarHidden(true, true);
			}
1342

I
isidor 已提交
1343
			if (config.hideStatusBar) {
I
isidor 已提交
1344 1345
				this.setStatusBarHidden(true, true);
			}
1346

I
isidor 已提交
1347 1348 1349 1350 1351
			if (config.hideLineNumbers) {
				setLineNumbers('off');
				this.zenMode.transitionDisposeables.push(this.editorService.onDidVisibleEditorsChange(() => setLineNumbers('off')));
			}

1352 1353
			if (config.hideTabs && this.editorPart.partOptions.showTabs) {
				this.zenMode.transitionDisposeables.push(this.editorPart.enforcePartOptions({ showTabs: false }));
I
isidor 已提交
1354
			}
1355 1356 1357 1358

			if (config.centerLayout) {
				this.centerEditorLayout(true, true);
			}
1359 1360 1361 1362
		}

		// Zen Mode Inactive
		else {
1363
			if (this.zenMode.wasPanelVisible) {
1364
				this.setPanelHidden(false, true);
1365
			}
1366

1367
			if (this.zenMode.wasSideBarVisible) {
1368
				this.setSideBarHidden(false, true);
1369
			}
1370

1371 1372 1373
			if (this.zenMode.transitionedToCenteredEditorLayout) {
				this.centerEditorLayout(false, true);
			}
I
isidor 已提交
1374
			setLineNumbers(this.configurationService.getValue('editor.lineNumbers'));
1375

1376 1377
			// Status bar and activity bar visibility come from settings -> update their visibility.
			this.onDidUpdateConfiguration(true);
1378

B
Benjamin Pasero 已提交
1379
			this.editorGroupService.activeGroup.focus();
1380

1381
			toggleFullScreen = this.zenMode.transitionedToFullScreen && browser.isFullscreen();
I
isidor 已提交
1382
		}
1383

1384
		this.inZenMode.set(this.zenMode.active);
I
isidor 已提交
1385

I
isidor 已提交
1386
		if (!skipLayout) {
1387
			this.layout();
I
isidor 已提交
1388
		}
1389

1390
		if (toggleFullScreen) {
1391
			this.windowService.toggleFullScreen();
1392
		}
I
isidor 已提交
1393 1394
	}

S
SteVen Batten 已提交
1395 1396
	private gridHasView(view: View): boolean {
		if (!(this.workbenchGrid instanceof Grid)) {
S
SteVen Batten 已提交
1397 1398
			return false;
		}
1399

S
SteVen Batten 已提交
1400 1401 1402 1403 1404
		try {
			this.workbenchGrid.getViewSize(view);
			return true;
		} catch {
			return false;
1405 1406 1407
		}
	}

S
SteVen Batten 已提交
1408
	private updateGrid(): void {
S
SteVen Batten 已提交
1409
		if (!(this.workbenchGrid instanceof Grid)) {
1410 1411 1412
			return;
		}

S
SteVen Batten 已提交
1413 1414 1415 1416 1417
		let panelInGrid = this.gridHasView(this.panelPartView);
		let sidebarInGrid = this.gridHasView(this.sidebarPartView);
		let activityBarInGrid = this.gridHasView(this.activitybarPartView);
		let statusBarInGrid = this.gridHasView(this.statusbarPartView);
		let titlebarInGrid = this.gridHasView(this.titlebarPartView);
1418

S
SteVen Batten 已提交
1419 1420 1421
		// Add parts to grid
		if (!statusBarInGrid) {
			this.workbenchGrid.addView(this.statusbarPartView, Sizing.Split, this.editorPartView, Direction.Down);
S
SteVen Batten 已提交
1422 1423 1424
			statusBarInGrid = true;
		}

1425
		if (!titlebarInGrid && this.useCustomTitleBarStyle()) {
S
SteVen Batten 已提交
1426 1427 1428
			this.workbenchGrid.addView(this.titlebarPartView, Sizing.Split, this.editorPartView, Direction.Up);
			titlebarInGrid = true;
		}
S
SteVen Batten 已提交
1429

S
SteVen Batten 已提交
1430 1431 1432 1433
		if (!activityBarInGrid) {
			this.workbenchGrid.addView(this.activitybarPartView, Sizing.Split, panelInGrid && this.sideBarPosition === this.panelPosition ? this.panelPartView : this.editorPartView, this.sideBarPosition === Position.RIGHT ? Direction.Right : Direction.Left);
			activityBarInGrid = true;
		}
S
SteVen Batten 已提交
1434

S
SteVen Batten 已提交
1435 1436 1437 1438
		if (!sidebarInGrid) {
			this.workbenchGrid.addView(this.sidebarPartView, this.uiState.lastSidebarDimension !== undefined ? this.uiState.lastSidebarDimension : Sizing.Split, this.activitybarPartView, this.sideBarPosition === Position.LEFT ? Direction.Right : Direction.Left);
			sidebarInGrid = true;
		}
S
SteVen Batten 已提交
1439

S
SteVen Batten 已提交
1440 1441 1442
		if (!panelInGrid) {
			this.workbenchGrid.addView(this.panelPartView, this.getLastPanelDimension(this.panelPosition) !== undefined ? this.getLastPanelDimension(this.panelPosition) : Sizing.Split, this.editorPartView, this.panelPosition === Position.BOTTOM ? Direction.Down : Direction.Right);
			panelInGrid = true;
S
SteVen Batten 已提交
1443 1444
		}

S
SteVen Batten 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
		// Hide parts
		if (this.panelHidden) {
			this.panelPartView.hide();
		}

		if (this.statusBarHidden) {
			this.statusbarPartView.hide();
		}

		if (!this.isVisible(Parts.TITLEBAR_PART)) {
			this.titlebarPartView.hide();
		}

		if (this.activityBarHidden) {
			this.activitybarPartView.hide();
		}

		if (this.sideBarHidden) {
			this.sidebarPartView.hide();
		}

		if (this.editorHidden) {
			this.editorPartView.hide();
		}

S
SteVen Batten 已提交
1470
		// Show visible parts
S
SteVen Batten 已提交
1471 1472 1473 1474
		if (!this.editorHidden) {
			this.editorPartView.show();
		}

S
SteVen Batten 已提交
1475 1476 1477
		if (!this.statusBarHidden) {
			this.statusbarPartView.show();
		}
S
SteVen Batten 已提交
1478

S
SteVen Batten 已提交
1479 1480
		if (this.isVisible(Parts.TITLEBAR_PART)) {
			this.titlebarPartView.show();
S
SteVen Batten 已提交
1481 1482
		}

S
SteVen Batten 已提交
1483 1484 1485
		if (!this.activityBarHidden) {
			this.activitybarPartView.show();
		}
S
SteVen Batten 已提交
1486

S
SteVen Batten 已提交
1487 1488
		if (!this.sideBarHidden) {
			this.sidebarPartView.show();
S
SteVen Batten 已提交
1489 1490
		}

S
SteVen Batten 已提交
1491 1492
		if (!this.panelHidden) {
			this.panelPartView.show();
S
SteVen Batten 已提交
1493 1494 1495 1496 1497 1498 1499
		}
	}

	layout(options?: ILayoutOptions): void {
		this.contextViewService.layout();

		if (this.workbenchStarted && !this.workbenchShutdown) {
S
SteVen Batten 已提交
1500
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1501 1502 1503 1504
				const dimensions = DOM.getClientArea(this.container);
				DOM.position(this.workbench, 0, 0, 0, 0, 'relative');
				DOM.size(this.workbench, dimensions.width, dimensions.height);

S
SteVen Batten 已提交
1505
				// Layout the grid
S
SteVen Batten 已提交
1506
				this.workbenchGrid.layout(dimensions.width, dimensions.height);
S
SteVen Batten 已提交
1507 1508 1509 1510 1511 1512 1513 1514

				// Layout non-view ui components
				this.quickInput.layout(dimensions);
				this.quickOpen.layout(dimensions);
				this.notificationsCenter.layout(dimensions);
				this.notificationsToasts.layout(dimensions);

				// Update grid view membership
1515
				this.updateGrid();
S
SteVen Batten 已提交
1516 1517
			} else {
				this.workbenchGrid.layout(options);
1518
			}
1519 1520 1521
		}
	}

1522
	isEditorLayoutCentered(): boolean {
1523
		return this.shouldCenterLayout;
S
SrTobi 已提交
1524 1525
	}

1526
	centerEditorLayout(active: boolean, skipLayout?: boolean): void {
B
Benjamin Pasero 已提交
1527
		this.storageService.store(Workbench.centeredEditorLayoutActiveStorageKey, active, StorageScope.WORKSPACE);
1528 1529
		this.shouldCenterLayout = active;
		let smartActive = active;
1530
		if (this.editorPart.groups.length > 1 && this.configurationService.getValue('workbench.editor.centeredLayoutAutoResize')) {
B
Benjamin Pasero 已提交
1531
			smartActive = false; // Respect the auto resize setting - do not go into centered layout if there is more than 1 group.
1532
		}
B
Benjamin Pasero 已提交
1533

1534
		// Enter Centered Editor Layout
1535 1536
		if (this.editorPart.isLayoutCentered() !== smartActive) {
			this.editorPart.centerLayout(smartActive);
1537

1538 1539 1540
			if (!skipLayout) {
				this.layout();
			}
1541
		}
S
SrTobi 已提交
1542 1543
	}

1544
	resizePart(part: Parts, sizeChange: number): void {
S
SteVen Batten 已提交
1545
		let view: View;
1546 1547
		switch (part) {
			case Parts.SIDEBAR_PART:
S
SteVen Batten 已提交
1548
				view = this.sidebarPartView;
1549
			case Parts.PANEL_PART:
S
SteVen Batten 已提交
1550
				view = this.panelPartView;
1551
			case Parts.EDITOR_PART:
S
SteVen Batten 已提交
1552 1553
				view = this.editorPartView;
				if (this.workbenchGrid instanceof Grid) {
1554 1555 1556 1557
					this.workbenchGrid.resizeView(view, this.workbenchGrid.getViewSize(view) + sizeChange);
				} else {
					this.workbenchGrid.resizePart(part, sizeChange);
				}
1558 1559
				break;
			default:
B
Benjamin Pasero 已提交
1560
				return; // Cannot resize other parts
1561 1562 1563
		}
	}

1564 1565
	setActivityBarHidden(hidden: boolean, skipLayout?: boolean): void {
		this.activityBarHidden = hidden;
1566

1567 1568
		// Layout
		if (!skipLayout) {
S
SteVen Batten 已提交
1569
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1570
				this.layout();
1571 1572 1573
			} else {
				this.workbenchGrid.layout();
			}
1574
		}
1575
	}
1576

S
SteVen Batten 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
	setEditorHidden(hidden: boolean, skipLayout?: boolean): void {
		if (!(this.workbenchGrid instanceof Grid)) {
			return;
		}

		this.editorHidden = hidden;

		// The editor and the panel cannot be hidden at the same time
		if (this.editorHidden && this.panelHidden) {
			this.setPanelHidden(false, true);
		}

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

1594
	setSideBarHidden(hidden: boolean, skipLayout?: boolean): void {
1595 1596 1597 1598 1599
		this.sideBarHidden = hidden;
		this.sideBarVisibleContext.set(!hidden);

		// Adjust CSS
		if (hidden) {
B
Benjamin Pasero 已提交
1600
			DOM.addClass(this.workbench, 'nosidebar');
1601
		} else {
B
Benjamin Pasero 已提交
1602
			DOM.removeClass(this.workbench, 'nosidebar');
1603 1604
		}

1605 1606
		// If sidebar becomes hidden, also hide the current active Viewlet if any
		if (hidden && this.sidebarPart.getActiveViewlet()) {
1607 1608 1609 1610 1611 1612 1613 1614 1615
			this.sidebarPart.hideActiveViewlet();
			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();
			} else {
				this.editorGroupService.activeGroup.focus();
			}
1616 1617 1618 1619 1620 1621
		}

		// If sidebar becomes visible, show last active Viewlet or default viewlet
		else if (!hidden && !this.sidebarPart.getActiveViewlet()) {
			const viewletToOpen = this.sidebarPart.getLastActiveViewletId();
			if (viewletToOpen) {
I
isidor 已提交
1622
				const viewlet = this.sidebarPart.openViewlet(viewletToOpen, true);
1623
				if (!viewlet) {
I
isidor 已提交
1624
					this.sidebarPart.openViewlet(this.sidebarPart.getDefaultViewletId(), true);
1625
				}
1626
			}
1627 1628
		}

1629

1630 1631 1632 1633 1634 1635 1636
		// 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);
		}
1637

1638 1639
		// Layout
		if (!skipLayout) {
S
SteVen Batten 已提交
1640
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1641
				this.layout();
1642 1643 1644
			} else {
				this.workbenchGrid.layout();
			}
1645
		}
1646
	}
1647

1648
	setPanelHidden(hidden: boolean, skipLayout?: boolean): void {
1649 1650 1651 1652
		this.panelHidden = hidden;

		// Adjust CSS
		if (hidden) {
B
Benjamin Pasero 已提交
1653
			DOM.addClass(this.workbench, 'nopanel');
1654
		} else {
B
Benjamin Pasero 已提交
1655
			DOM.removeClass(this.workbench, 'nopanel');
1656 1657 1658 1659
		}

		// If panel part becomes hidden, also hide the current active panel if any
		if (hidden && this.panelPart.getActivePanel()) {
1660 1661
			this.panelPart.hideActivePanel();
			this.editorGroupService.activeGroup.focus(); // Pass focus to editor group if panel part is now hidden
1662 1663 1664 1665 1666 1667
		}

		// 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) {
1668
				this.panelPart.openPanel(panelToOpen, true);
1669 1670 1671 1672
			}
		}


1673 1674 1675 1676 1677 1678
		// Remember in settings
		if (!hidden) {
			this.storageService.store(Workbench.panelHiddenStorageKey, 'false', StorageScope.WORKSPACE);
		} else {
			this.storageService.remove(Workbench.panelHiddenStorageKey, StorageScope.WORKSPACE);
		}
1679

S
SteVen Batten 已提交
1680 1681 1682 1683 1684
		// The editor and panel cannot be hiddne at the same time
		if (hidden && this.editorHidden) {
			this.setEditorHidden(false, true);
		}

1685 1686
		// Layout
		if (!skipLayout) {
S
SteVen Batten 已提交
1687
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1688
				this.layout();
1689 1690 1691
			} else {
				this.workbenchGrid.layout();
			}
1692
		}
1693 1694 1695
	}

	toggleMaximizedPanel(): void {
S
SteVen Batten 已提交
1696 1697
		if (this.workbenchGrid instanceof Grid) {
			this.workbenchGrid.maximizeViewSize(this.panelPartView);
1698 1699 1700
		} else {
			this.workbenchGrid.layout({ toggleMaximizedPanel: true, source: Parts.PANEL_PART });
		}
1701 1702 1703
	}

	isPanelMaximized(): boolean {
S
SteVen Batten 已提交
1704
		if (this.workbenchGrid instanceof Grid) {
1705
			try {
S
SteVen Batten 已提交
1706
				return this.workbenchGrid.getViewSize2(this.panelPartView).height === this.panelPart.maximumHeight;
1707 1708 1709 1710 1711 1712
			} catch (e) {
				return false;
			}
		} else {
			return this.workbenchGrid.isPanelMaximized();
		}
1713 1714 1715 1716 1717 1718 1719
	}

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

	setSideBarPosition(position: Position): void {
1720 1721
		const wasHidden = this.sideBarHidden;

1722
		if (this.sideBarHidden) {
1723
			this.setSideBarHidden(false, true /* Skip Layout */);
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
		}

		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
S
SteVen Batten 已提交
1741
		if (this.workbenchGrid instanceof Grid) {
1742

S
SteVen Batten 已提交
1743
			if (!wasHidden) {
S
SteVen Batten 已提交
1744
				this.uiState.lastSidebarDimension = this.workbenchGrid.getViewSize(this.sidebarPartView);
1745 1746
			}

S
SteVen Batten 已提交
1747 1748
			this.workbenchGrid.removeView(this.sidebarPartView);
			this.workbenchGrid.removeView(this.activitybarPartView);
S
SteVen Batten 已提交
1749 1750 1751

			if (!this.panelHidden && this.panelPosition === Position.BOTTOM) {
				this.workbenchGrid.removeView(this.panelPartView);
1752 1753
			}

S
SteVen Batten 已提交
1754
			this.layout();
1755 1756 1757
		} else {
			this.workbenchGrid.layout();
		}
1758 1759
	}

S
SteVen Batten 已提交
1760
	setMenubarVisibility(visibility: MenuBarVisibility, skipLayout: boolean): void {
S
SteVen Batten 已提交
1761 1762
		if (this.menubarVisibility !== visibility) {
			this.menubarVisibility = visibility;
1763

1764
			// Layout
S
SteVen Batten 已提交
1765
			if (!skipLayout) {
S
SteVen Batten 已提交
1766
				if (this.workbenchGrid instanceof Grid) {
1767 1768 1769 1770 1771
					const dimensions = DOM.getClientArea(this.container);
					this.workbenchGrid.layout(dimensions.width, dimensions.height);
				} else {
					this.workbenchGrid.layout();
				}
S
SteVen Batten 已提交
1772
			}
1773 1774 1775
		}
	}

S
SteVen Batten 已提交
1776 1777 1778 1779
	getMenubarVisibility(): MenuBarVisibility {
		return this.menubarVisibility;
	}

1780 1781 1782 1783
	getPanelPosition(): Position {
		return this.panelPosition;
	}

1784
	setPanelPosition(position: Position): void {
1785 1786
		const wasHidden = this.panelHidden;

1787 1788
		if (this.panelHidden) {
			this.setPanelHidden(false, true /* Skip Layout */);
1789 1790
		} else {
			this.saveLastPanelDimension();
1791
		}
1792

1793 1794 1795 1796
		const newPositionValue = (position === Position.BOTTOM) ? 'bottom' : 'right';
		const oldPositionValue = (this.panelPosition === Position.BOTTOM) ? 'bottom' : 'right';
		this.panelPosition = position;
		this.storageService.store(Workbench.panelPositionStorageKey, PositionToString(this.panelPosition).toLowerCase(), StorageScope.WORKSPACE);
1797

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

1802 1803 1804 1805
		// Update Styles
		this.panelPart.updateStyles();

		// Layout
S
SteVen Batten 已提交
1806 1807 1808
		if (this.workbenchGrid instanceof Grid) {
			if (!wasHidden) {
				this.saveLastPanelDimension();
1809
			}
1810

1811
			this.workbenchGrid.removeView(this.panelPartView);
S
SteVen Batten 已提交
1812
			this.layout();
1813 1814 1815 1816 1817
		} else {
			this.workbenchGrid.layout();
		}
	}
}