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

6
import 'vs/workbench/browser/style';
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
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';
64
import { TextFileService } from 'vs/workbench/services/textfile/common/textFileService';
J
Johannes Rieken 已提交
65
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
66
import { IProgressService2 } from 'vs/platform/progress/common/progress';
67
import { ProgressService2 } from 'vs/workbench/services/progress/browser/progressService2';
68
import { TextModelResolverService } from 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
69
import { ITextModelService } from 'vs/editor/common/services/resolverService';
J
Johannes Rieken 已提交
70
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
71
import { LifecyclePhase, StartupKind } from 'vs/platform/lifecycle/common/lifecycle';
72
import { LifecycleService } from 'vs/platform/lifecycle/electron-browser/lifecycleService';
B
Benjamin Pasero 已提交
73
import { IWindowService, IWindowConfiguration, IPath, MenuBarVisibility, getTitleBarStyle } from 'vs/platform/windows/common/windows';
J
Johannes Rieken 已提交
74
import { IStatusbarService } from 'vs/platform/statusbar/common/statusbar';
75
import { IMenuService, SyncActionDescriptor } from 'vs/platform/actions/common/actions';
76
import { MenuService } from 'vs/platform/actions/common/menuService';
S
SteVen Batten 已提交
77
import { IContextMenuService, IContextViewService } from 'vs/platform/contextview/browser/contextView';
J
Johannes Rieken 已提交
78
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
B
Benjamin Pasero 已提交
79
import { IWorkbenchActionRegistry, Extensions } from 'vs/workbench/common/actions';
80
import { ShowPreviousWindowTab, MoveWindowTabToNewWindow, MergeAllWindowTabs, ShowNextWindowTab, ToggleWindowTabsBar, NewWindowTab, OpenRecentAction, ReloadWindowAction, ReloadWindowWithExtensionsDisabledAction } from 'vs/workbench/electron-browser/actions/windowActions';
81
import { ToggleDevToolsAction } from 'vs/workbench/electron-browser/actions/developerActions';
82
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
83
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
B
Benjamin Pasero 已提交
84
import { WorkspaceEditingService } from 'vs/workbench/services/workspace/node/workspaceEditingService';
85
import { FileDecorationsService } from 'vs/workbench/services/decorations/browser/decorationsService';
J
Johannes Rieken 已提交
86
import { IDecorationsService } from 'vs/workbench/services/decorations/browser/decorations';
I
isidor 已提交
87
import { ActivityService } from 'vs/workbench/services/activity/browser/activityService';
88
import { URI } from 'vs/base/common/uri';
J
Joao Moreno 已提交
89
import { IListService, ListService } from 'vs/platform/list/browser/listService';
A
Alex Ross 已提交
90
import { InputFocusedContext, IsMacContext, IsLinuxContext, IsWindowsContext, SupportsOpenFileFolderContext, SupportsWorkspacesContext } from 'vs/platform/contextkey/common/contextkeys';
91
import { IViewsService } from 'vs/workbench/common/views';
S
Sandeep Somavarapu 已提交
92
import { ViewsService } from 'vs/workbench/browser/parts/views/views';
93 94
import { INotificationService } from 'vs/platform/notification/common/notification';
import { NotificationService } from 'vs/workbench/services/notification/common/notificationService';
95 96
import { NotificationsCenter } from 'vs/workbench/browser/parts/notifications/notificationsCenter';
import { NotificationsAlerts } from 'vs/workbench/browser/parts/notifications/notificationsAlerts';
97
import { NotificationsStatus } from 'vs/workbench/browser/parts/notifications/notificationsStatus';
98
import { registerNotificationCommands } from 'vs/workbench/browser/parts/notifications/notificationsCommands';
99
import { NotificationsToasts } from 'vs/workbench/browser/parts/notifications/notificationsToasts';
J
Joao Moreno 已提交
100
import { IPCClient } from 'vs/base/parts/ipc/node/ipc';
J
Joao Moreno 已提交
101
import { registerWindowDriver } from 'vs/platform/driver/electron-browser/driver';
S
Sandeep Somavarapu 已提交
102 103
import { IPreferencesService } from 'vs/workbench/services/preferences/common/preferences';
import { PreferencesService } from 'vs/workbench/services/preferences/browser/preferencesService';
104
import { IEditorService, IResourceEditor } from 'vs/workbench/services/editor/common/editorService';
105
import { IEditorGroupsService, GroupDirection, preferredSideBySideGroupDirection } from 'vs/workbench/services/editor/common/editorGroupsService';
106
import { EditorService } from 'vs/workbench/services/editor/browser/editorService';
J
Joao Moreno 已提交
107
import { IExtensionUrlHandler, ExtensionUrlHandler } from 'vs/workbench/services/extensions/electron-browser/inactiveExtensionUrlHandler';
S
SteVen Batten 已提交
108
import { ContextViewService } from 'vs/platform/contextview/browser/contextViewService';
A
Alex Dima 已提交
109 110
import { WorkbenchThemeService } from 'vs/workbench/services/themes/electron-browser/workbenchThemeService';
import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService';
M
Martin Aeschlimann 已提交
111
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
A
Alex Ross 已提交
112
import { RemoteFileDialogService } from 'vs/workbench/services/dialogs/electron-browser/dialogService';
113
import { LogStorageAction } from 'vs/platform/storage/node/storageService';
S
SteVen Batten 已提交
114
import { Sizing, Direction, Grid, View } from 'vs/base/browser/ui/grid/grid';
I
isidor 已提交
115
import { IEditor } from 'vs/editor/common/editorCommon';
116
import { WorkbenchLayout } from 'vs/workbench/browser/layout';
117

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

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

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

140 141
type FontAliasingOption = 'default' | 'antialiased' | 'none' | 'auto';

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

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

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

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

171 172 173 174 175 176
interface IWorkbenchUIState {
	lastPanelHeight?: number;
	lastPanelWidth?: number;
	lastSidebarDimension?: number;
}

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

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

192
	_serviceBrand: any;
193

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

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

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

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

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

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

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

I
isidor 已提交
248
	private inZenMode: IContextKey<boolean>;
249
	private sideBarVisibleContext: IContextKey<boolean>;
250

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

253
	constructor(
254
		private container: HTMLElement,
J
Joao Moreno 已提交
255
		private configuration: IWindowConfiguration,
256
		serviceCollection: ServiceCollection,
257
		private lifecycleService: LifecycleService,
J
Joao Moreno 已提交
258
		private mainProcessClient: IPCClient,
259 260 261 262 263 264 265 266
		@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
267
	) {
268
		super();
E
Erich Gamma 已提交
269

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

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

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

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

284 285
		// Install some global actions
		this.createGlobalActions();
286

287 288
		// Services
		this.initServices();
E
Erich Gamma 已提交
289

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

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

296 297
		// Settings
		this.initSettings();
E
Erich Gamma 已提交
298

299 300
		// Create Workbench and Parts
		this.renderWorkbench();
301

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

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

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

B
Benjamin Pasero 已提交
314
	private createWorkbench(): void {
B
Benjamin Pasero 已提交
315 316
		this.workbench = document.createElement('div');
		this.workbench.id = Identifiers.WORKBENCH_CONTAINER;
317 318

		DOM.addClasses(this.workbench, 'monaco-workbench', isWindows ? 'windows' : isLinux ? 'linux' : 'mac');
S
SteVen Batten 已提交
319

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

M
Martin Aeschlimann 已提交
428
		// File Dialogs
A
Alex Ross 已提交
429
		serviceCollection.set(IFileDialogService, new SyncDescriptor(RemoteFileDialogService));
M
Martin Aeschlimann 已提交
430

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

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

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

J
Joao Moreno 已提交
445
		// Inactive extension URL handler
J
Joao Moreno 已提交
446
		serviceCollection.set(IExtensionUrlHandler, new SyncDescriptor(ExtensionUrlHandler));
J
Joao Moreno 已提交
447

B
Benjamin Pasero 已提交
448
		// Text Model Resolver Service
449
		serviceCollection.set(ITextModelService, new SyncDescriptor(TextModelResolverService));
B
Benjamin Pasero 已提交
450

451 452 453 454
		// JSON Editing
		const jsonEditingService = this.instantiationService.createInstance(JSONEditingService);
		serviceCollection.set(IJSONEditingService, jsonEditingService);

455 456 457
		// Workspace Editing
		serviceCollection.set(IWorkspaceEditingService, new SyncDescriptor(WorkspaceEditingService));

458 459 460
		// Keybinding Editing
		serviceCollection.set(IKeybindingEditingService, this.instantiationService.createInstance(KeybindingsEditingService));

461
		// Configuration Resolver
462
		serviceCollection.set(IConfigurationResolverService, new SyncDescriptor(ConfigurationResolverService, [process.env]));
463

E
Erich Gamma 已提交
464
		// Quick open service (quick open controller)
465 466
		this.quickOpen = this.instantiationService.createInstance(QuickOpenController);
		serviceCollection.set(IQuickOpenService, this.quickOpen);
467

C
Christof Marti 已提交
468 469 470 471
		// Quick input service
		this.quickInput = this.instantiationService.createInstance(QuickInputService);
		serviceCollection.set(IQuickInputService, this.quickInput);

S
Sandeep Somavarapu 已提交
472 473 474
		// PreferencesService
		serviceCollection.set(IPreferencesService, this.instantiationService.createInstance(PreferencesService));

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

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

486
		this.instantiationService.createInstance(DefaultConfigurationExportHelper);
487

S
#47154  
Sandeep Somavarapu 已提交
488
		this.configurationService.acquireInstantiationService(this.getInstantiationService());
E
Erich Gamma 已提交
489 490
	}

491
	//#region event handling
E
Erich Gamma 已提交
492

493
	private registerListeners(): void {
I
isidor 已提交
494

495
		// Storage
496
		this._register(this.storageService.onWillSaveState(e => this.saveState(e)));
497

498
		// Listen to visible editor changes
499
		this._register(this.editorService.onDidVisibleEditorsChange(() => this.onDidVisibleEditorsChange()));
B
Benjamin Pasero 已提交
500

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

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

511
			this._register(listenerDispose);
512
		}
S
Sanders Lauture 已提交
513

514
		// Configuration changes
515
		this._register(this.configurationService.onDidChangeConfiguration(() => this.onDidUpdateConfiguration()));
I
isidor 已提交
516

517
		// Fullscreen changes
518
		this._register(browser.onDidChangeFullscreen(() => this.onFullscreenChanged()));
B
Benjamin Pasero 已提交
519 520 521 522

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

525
	private onFullscreenChanged(): void {
S
SrTobi 已提交
526

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

534 535 536 537
			if (this.zenMode.transitionedToFullScreen && this.zenMode.active) {
				this.toggleZenMode();
			}
		}
E
Erich Gamma 已提交
538

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

546 547 548 549
	private onMenubarToggled(visible: boolean) {
		if (visible !== this.menubarToggled) {
			this.menubarToggled = visible;

550 551
			if (browser.isFullscreen() && (this.menubarVisibility === 'toggle' || this.menubarVisibility === 'default')) {
				this._onTitleBarVisibilityChange.fire();
552 553 554 555 556
				this.layout();
			}
		}
	}

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

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

568
	private onDidVisibleEditorsChange(): void {
569
		const visibleEditors = this.editorService.visibleControls;
E
Erich Gamma 已提交
570

571 572 573 574 575 576 577 578 579
		// 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();
			}
		}
580 581 582 583

		if (this.editorHidden) {
			this.setEditorHidden(false);
		}
584 585
	}

586
	private onAllEditorsClosed(): void {
587
		const visibleEditors = this.editorService.visibleControls.length;
588 589
		if (visibleEditors === 0) {
			this.windowService.closeWindow();
E
Erich Gamma 已提交
590 591 592
		}
	}

593 594 595 596 597
	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);
598
		}
E
Erich Gamma 已提交
599

600
		this.setPanelPositionFromStorageOrConfig();
E
Erich Gamma 已提交
601

602 603 604
		const fontAliasing = this.configurationService.getValue<FontAliasingOption>(Workbench.fontAliasingConfigurationKey);
		if (fontAliasing !== this.fontAliasing) {
			this.setFontAliasing(fontAliasing);
605 606
		}

607 608 609 610 611
		if (!this.zenMode.active) {
			const newStatusbarHiddenValue = !this.configurationService.getValue<boolean>(Workbench.statusbarVisibleConfigurationKey);
			if (newStatusbarHiddenValue !== this.statusBarHidden) {
				this.setStatusBarHidden(newStatusbarHiddenValue, skipLayout);
			}
612

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

619
		const newMenubarVisibility = this.configurationService.getValue<MenuBarVisibility>(Workbench.menubarVisibilityConfigurationKey);
S
SteVen Batten 已提交
620
		this.setMenubarVisibility(newMenubarVisibility, skipLayout);
621
	}
B
Benjamin Pasero 已提交
622

623
	//#endregion
B
Benjamin Pasero 已提交
624

625 626
	private handleContextKeys(): void {
		this.inZenMode = InEditorZenModeContext.bindTo(this.contextKeyService);
627

I
isidor 已提交
628 629 630
		IsMacContext.bindTo(this.contextKeyService);
		IsLinuxContext.bindTo(this.contextKeyService);
		IsWindowsContext.bindTo(this.contextKeyService);
A
Alex Ross 已提交
631 632 633 634 635 636
		const supportsOpenFileFolderContextKey = SupportsOpenFileFolderContext.bindTo(this.contextKeyService);
		const supportsWorkspacesContextKey = SupportsWorkspacesContext.bindTo(this.contextKeyService);
		if (this.windowService.getConfiguration().remoteAuthority) {
			supportsOpenFileFolderContextKey.set(true);
			supportsWorkspacesContextKey.set(false);
		}
637

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

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

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

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

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

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

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

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

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

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

		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 已提交
698
				Event.once(tracker.onDidBlur)(() => {
B
Benjamin Pasero 已提交
699 700 701 702 703 704 705 706
					inputFocused.set(activeElementIsInput());

					tracker.dispose();
				});
			}
		}

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

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

714 715 716 717 718
		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);
		}));
719

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

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

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

733
		updateSplitEditorsVerticallyContext();
734
	}
E
Erich Gamma 已提交
735

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

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

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

J
Johannes Rieken 已提交
748
				return Promise.resolve(undefined);
B
Benjamin Pasero 已提交
749 750 751 752 753 754 755 756 757 758
			}

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

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

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

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

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

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

790 791 792
		// 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);
793
		if (wasZenActive && zenConfig.restore) {
I
isidor 已提交
794
			this.toggleZenMode(true, true);
I
isidor 已提交
795 796
		}

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

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

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

808 809 810 811 812 813 814
			// 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);
815

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

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

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

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

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

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
880
		return [];
B
Benjamin Pasero 已提交
881
	}
882

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

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

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

B
Benjamin Pasero 已提交
904 905
			return input;
		});
906 907
	}

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

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

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

	private initSettings(): void {
923

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

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

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

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

I
isidor 已提交
941
		// Panel position
942
		this.setPanelPositionFromStorageOrConfig();
943

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

E
Erich Gamma 已提交
1029
	private createWorkbenchLayout(): void {
1030
		if (this.configurationService.getValue('workbench.useExperimentalGridLayout')) {
S
SteVen Batten 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039
			// 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 });
1040

1041
			this.workbench.prepend(this.workbenchGrid.element);
1042
			this.layout();
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
		} 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 已提交
1062 1063 1064 1065 1066 1067
	}

	private renderWorkbench(): void {

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

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

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

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

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

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

1095 1096
		// Notification Handlers
		this.createNotificationsHandlers();
1097

1098 1099

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

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

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

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

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

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

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

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

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

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

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

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

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

1150 1151 1152 1153 1154
		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 已提交
1155

B
Benjamin Pasero 已提交
1156
		return part;
E
Erich Gamma 已提交
1157 1158
	}

1159 1160
	private createNotificationsHandlers(): void {

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

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

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

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

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

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

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

1183
		// Register Commands
1184
		registerNotificationCommands(this.notificationsCenter, this.notificationsToasts);
1185 1186
	}

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

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

		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);
			}
		}
1205 1206
	}

1207
	dispose(): void {
1208
		super.dispose();
1209 1210 1211 1212 1213 1214

		this.workbenchShutdown = true;
	}

	//#region IPartService

M
Matt Bierner 已提交
1215
	private readonly _onTitleBarVisibilityChange: Emitter<void> = this._register(new Emitter<void>());
1216
	get onTitleBarVisibilityChange(): Event<void> { return this._onTitleBarVisibilityChange.event; }
1217

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

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

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

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

1234
	getContainer(part: Parts): HTMLElement | null {
1235 1236
		switch (part) {
			case Parts.TITLEBAR_PART:
1237
				return this.titlebarPart.getContainer();
1238
			case Parts.ACTIVITYBAR_PART:
1239
				return this.activitybarPart.getContainer();
1240
			case Parts.SIDEBAR_PART:
1241
				return this.sidebarPart.getContainer();
1242
			case Parts.PANEL_PART:
1243
				return this.panelPart.getContainer();
1244
			case Parts.EDITOR_PART:
1245
				return this.editorPart.getContainer();
1246
			case Parts.STATUSBAR_PART:
1247
				return this.statusbarPart.getContainer();
1248
		}
1249

1250
		return null;
1251
	}
1252

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

1281
		return true; // any other part cannot be hidden
E
Erich Gamma 已提交
1282 1283
	}

1284 1285 1286
	getTitleBarOffset(): number {
		let offset = 0;
		if (this.isVisible(Parts.TITLEBAR_PART)) {
S
SteVen Batten 已提交
1287 1288
			if (this.workbenchGrid instanceof Grid) {
				offset = this.gridHasView(this.titlebarPartView) ? this.workbenchGrid.getViewSize2(this.titlebarPartView).height : 0;
S
SteVen Batten 已提交
1289 1290 1291 1292
			} else {
				offset = this.workbenchGrid.partLayoutInfo.titlebar.height;
			}

1293 1294 1295
			if (isMacintosh || this.menubarVisibility === 'hidden') {
				offset /= browser.getZoomFactor();
			}
E
Erich Gamma 已提交
1296
		}
1297 1298

		return offset;
E
Erich Gamma 已提交
1299
	}
1300

1301 1302
	getWorkbenchElement(): HTMLElement {
		return this.workbench;
1303
	}
1304

I
isidor 已提交
1305
	toggleZenMode(skipLayout?: boolean, restoring = false): void {
I
isidor 已提交
1306
		this.zenMode.active = !this.zenMode.active;
1307
		this.zenMode.transitionDisposeables = dispose(this.zenMode.transitionDisposeables);
1308

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

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

I
isidor 已提交
1325
			toggleFullScreen = !browser.isFullscreen() && config.fullScreen;
I
isidor 已提交
1326
			this.zenMode.transitionedToFullScreen = restoring ? config.fullScreen : toggleFullScreen;
1327
			this.zenMode.transitionedToCenteredEditorLayout = !this.isEditorLayoutCentered() && config.centerLayout;
1328 1329
			this.zenMode.wasSideBarVisible = this.isVisible(Parts.SIDEBAR_PART);
			this.zenMode.wasPanelVisible = this.isVisible(Parts.PANEL_PART);
1330

1331 1332
			this.setPanelHidden(true, true);
			this.setSideBarHidden(true, true);
I
isidor 已提交
1333

1334 1335 1336
			if (config.hideActivityBar) {
				this.setActivityBarHidden(true, true);
			}
1337

I
isidor 已提交
1338
			if (config.hideStatusBar) {
I
isidor 已提交
1339 1340
				this.setStatusBarHidden(true, true);
			}
1341

I
isidor 已提交
1342 1343 1344 1345 1346
			if (config.hideLineNumbers) {
				setLineNumbers('off');
				this.zenMode.transitionDisposeables.push(this.editorService.onDidVisibleEditorsChange(() => setLineNumbers('off')));
			}

1347 1348
			if (config.hideTabs && this.editorPart.partOptions.showTabs) {
				this.zenMode.transitionDisposeables.push(this.editorPart.enforcePartOptions({ showTabs: false }));
I
isidor 已提交
1349
			}
1350 1351 1352 1353

			if (config.centerLayout) {
				this.centerEditorLayout(true, true);
			}
1354 1355 1356 1357
		}

		// Zen Mode Inactive
		else {
1358
			if (this.zenMode.wasPanelVisible) {
1359
				this.setPanelHidden(false, true);
1360
			}
1361

1362
			if (this.zenMode.wasSideBarVisible) {
1363
				this.setSideBarHidden(false, true);
1364
			}
1365

1366 1367 1368
			if (this.zenMode.transitionedToCenteredEditorLayout) {
				this.centerEditorLayout(false, true);
			}
I
isidor 已提交
1369
			setLineNumbers(this.configurationService.getValue('editor.lineNumbers'));
1370

1371 1372
			// Status bar and activity bar visibility come from settings -> update their visibility.
			this.onDidUpdateConfiguration(true);
1373

B
Benjamin Pasero 已提交
1374
			this.editorGroupService.activeGroup.focus();
1375

1376
			toggleFullScreen = this.zenMode.transitionedToFullScreen && browser.isFullscreen();
I
isidor 已提交
1377
		}
1378

1379
		this.inZenMode.set(this.zenMode.active);
I
isidor 已提交
1380

I
isidor 已提交
1381
		if (!skipLayout) {
1382
			this.layout();
I
isidor 已提交
1383
		}
1384

1385
		if (toggleFullScreen) {
1386
			this.windowService.toggleFullScreen();
1387
		}
I
isidor 已提交
1388 1389
	}

S
SteVen Batten 已提交
1390 1391
	private gridHasView(view: View): boolean {
		if (!(this.workbenchGrid instanceof Grid)) {
S
SteVen Batten 已提交
1392 1393
			return false;
		}
1394

S
SteVen Batten 已提交
1395 1396 1397 1398 1399
		try {
			this.workbenchGrid.getViewSize(view);
			return true;
		} catch {
			return false;
1400 1401 1402
		}
	}

S
SteVen Batten 已提交
1403
	private updateGrid(): void {
S
SteVen Batten 已提交
1404
		if (!(this.workbenchGrid instanceof Grid)) {
1405 1406 1407
			return;
		}

S
SteVen Batten 已提交
1408 1409 1410 1411 1412
		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);
1413

S
SteVen Batten 已提交
1414 1415 1416
		// Add parts to grid
		if (!statusBarInGrid) {
			this.workbenchGrid.addView(this.statusbarPartView, Sizing.Split, this.editorPartView, Direction.Down);
S
SteVen Batten 已提交
1417 1418 1419
			statusBarInGrid = true;
		}

1420
		if (!titlebarInGrid && this.useCustomTitleBarStyle()) {
S
SteVen Batten 已提交
1421 1422 1423
			this.workbenchGrid.addView(this.titlebarPartView, Sizing.Split, this.editorPartView, Direction.Up);
			titlebarInGrid = true;
		}
S
SteVen Batten 已提交
1424

S
SteVen Batten 已提交
1425 1426 1427 1428
		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 已提交
1429

S
SteVen Batten 已提交
1430 1431 1432 1433
		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 已提交
1434

S
SteVen Batten 已提交
1435 1436 1437
		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 已提交
1438 1439
		}

S
SteVen Batten 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
		// 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 已提交
1465
		// Show visible parts
S
SteVen Batten 已提交
1466 1467 1468 1469
		if (!this.editorHidden) {
			this.editorPartView.show();
		}

S
SteVen Batten 已提交
1470 1471 1472
		if (!this.statusBarHidden) {
			this.statusbarPartView.show();
		}
S
SteVen Batten 已提交
1473

S
SteVen Batten 已提交
1474 1475
		if (this.isVisible(Parts.TITLEBAR_PART)) {
			this.titlebarPartView.show();
S
SteVen Batten 已提交
1476 1477
		}

S
SteVen Batten 已提交
1478 1479 1480
		if (!this.activityBarHidden) {
			this.activitybarPartView.show();
		}
S
SteVen Batten 已提交
1481

S
SteVen Batten 已提交
1482 1483
		if (!this.sideBarHidden) {
			this.sidebarPartView.show();
S
SteVen Batten 已提交
1484 1485
		}

S
SteVen Batten 已提交
1486 1487
		if (!this.panelHidden) {
			this.panelPartView.show();
S
SteVen Batten 已提交
1488 1489 1490 1491 1492 1493 1494
		}
	}

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

		if (this.workbenchStarted && !this.workbenchShutdown) {
S
SteVen Batten 已提交
1495
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1496 1497 1498 1499
				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 已提交
1500
				// Layout the grid
S
SteVen Batten 已提交
1501
				this.workbenchGrid.layout(dimensions.width, dimensions.height);
S
SteVen Batten 已提交
1502 1503 1504 1505 1506 1507 1508 1509

				// 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
1510
				this.updateGrid();
S
SteVen Batten 已提交
1511 1512
			} else {
				this.workbenchGrid.layout(options);
1513
			}
1514 1515 1516
		}
	}

1517
	isEditorLayoutCentered(): boolean {
1518
		return this.shouldCenterLayout;
S
SrTobi 已提交
1519 1520
	}

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

1529
		// Enter Centered Editor Layout
1530 1531
		if (this.editorPart.isLayoutCentered() !== smartActive) {
			this.editorPart.centerLayout(smartActive);
1532

1533 1534 1535
			if (!skipLayout) {
				this.layout();
			}
1536
		}
S
SrTobi 已提交
1537 1538
	}

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

1559 1560
	setActivityBarHidden(hidden: boolean, skipLayout?: boolean): void {
		this.activityBarHidden = hidden;
1561

1562 1563
		// Layout
		if (!skipLayout) {
S
SteVen Batten 已提交
1564
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1565
				this.layout();
1566 1567 1568
			} else {
				this.workbenchGrid.layout();
			}
1569
		}
1570
	}
1571

S
SteVen Batten 已提交
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
	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();
		}
	}

1589
	setSideBarHidden(hidden: boolean, skipLayout?: boolean): void {
1590 1591 1592 1593 1594
		this.sideBarHidden = hidden;
		this.sideBarVisibleContext.set(!hidden);

		// Adjust CSS
		if (hidden) {
B
Benjamin Pasero 已提交
1595
			DOM.addClass(this.workbench, 'nosidebar');
1596
		} else {
B
Benjamin Pasero 已提交
1597
			DOM.removeClass(this.workbench, 'nosidebar');
1598 1599
		}

1600 1601
		// If sidebar becomes hidden, also hide the current active Viewlet if any
		if (hidden && this.sidebarPart.getActiveViewlet()) {
1602 1603 1604 1605 1606 1607 1608 1609 1610
			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();
			}
1611 1612 1613 1614 1615 1616
		}

		// 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 已提交
1617
				const viewlet = this.sidebarPart.openViewlet(viewletToOpen, true);
1618
				if (!viewlet) {
I
isidor 已提交
1619
					this.sidebarPart.openViewlet(this.sidebarPart.getDefaultViewletId(), true);
1620
				}
1621
			}
1622 1623
		}

1624

1625 1626 1627 1628 1629 1630 1631
		// 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);
		}
1632

1633 1634
		// Layout
		if (!skipLayout) {
S
SteVen Batten 已提交
1635
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1636
				this.layout();
1637 1638 1639
			} else {
				this.workbenchGrid.layout();
			}
1640
		}
1641
	}
1642

1643
	setPanelHidden(hidden: boolean, skipLayout?: boolean): void {
1644 1645 1646 1647
		this.panelHidden = hidden;

		// Adjust CSS
		if (hidden) {
B
Benjamin Pasero 已提交
1648
			DOM.addClass(this.workbench, 'nopanel');
1649
		} else {
B
Benjamin Pasero 已提交
1650
			DOM.removeClass(this.workbench, 'nopanel');
1651 1652 1653 1654
		}

		// If panel part becomes hidden, also hide the current active panel if any
		if (hidden && this.panelPart.getActivePanel()) {
1655 1656
			this.panelPart.hideActivePanel();
			this.editorGroupService.activeGroup.focus(); // Pass focus to editor group if panel part is now hidden
1657 1658 1659 1660 1661 1662
		}

		// 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) {
1663
				this.panelPart.openPanel(panelToOpen, true);
1664 1665 1666 1667
			}
		}


1668 1669 1670 1671 1672 1673
		// Remember in settings
		if (!hidden) {
			this.storageService.store(Workbench.panelHiddenStorageKey, 'false', StorageScope.WORKSPACE);
		} else {
			this.storageService.remove(Workbench.panelHiddenStorageKey, StorageScope.WORKSPACE);
		}
1674

S
SteVen Batten 已提交
1675 1676 1677 1678 1679
		// The editor and panel cannot be hiddne at the same time
		if (hidden && this.editorHidden) {
			this.setEditorHidden(false, true);
		}

1680 1681
		// Layout
		if (!skipLayout) {
S
SteVen Batten 已提交
1682
			if (this.workbenchGrid instanceof Grid) {
S
SteVen Batten 已提交
1683
				this.layout();
1684 1685 1686
			} else {
				this.workbenchGrid.layout();
			}
1687
		}
1688 1689 1690
	}

	toggleMaximizedPanel(): void {
S
SteVen Batten 已提交
1691 1692
		if (this.workbenchGrid instanceof Grid) {
			this.workbenchGrid.maximizeViewSize(this.panelPartView);
1693 1694 1695
		} else {
			this.workbenchGrid.layout({ toggleMaximizedPanel: true, source: Parts.PANEL_PART });
		}
1696 1697 1698
	}

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

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

	setSideBarPosition(position: Position): void {
1715 1716
		const wasHidden = this.sideBarHidden;

1717
		if (this.sideBarHidden) {
1718
			this.setSideBarHidden(false, true /* Skip Layout */);
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
		}

		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 已提交
1736
		if (this.workbenchGrid instanceof Grid) {
1737

S
SteVen Batten 已提交
1738
			if (!wasHidden) {
S
SteVen Batten 已提交
1739
				this.uiState.lastSidebarDimension = this.workbenchGrid.getViewSize(this.sidebarPartView);
1740 1741
			}

S
SteVen Batten 已提交
1742 1743
			this.workbenchGrid.removeView(this.sidebarPartView);
			this.workbenchGrid.removeView(this.activitybarPartView);
S
SteVen Batten 已提交
1744 1745 1746

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

S
SteVen Batten 已提交
1749
			this.layout();
1750 1751 1752
		} else {
			this.workbenchGrid.layout();
		}
1753 1754
	}

S
SteVen Batten 已提交
1755
	setMenubarVisibility(visibility: MenuBarVisibility, skipLayout: boolean): void {
S
SteVen Batten 已提交
1756 1757
		if (this.menubarVisibility !== visibility) {
			this.menubarVisibility = visibility;
1758

1759
			// Layout
S
SteVen Batten 已提交
1760
			if (!skipLayout) {
S
SteVen Batten 已提交
1761
				if (this.workbenchGrid instanceof Grid) {
1762 1763 1764 1765 1766
					const dimensions = DOM.getClientArea(this.container);
					this.workbenchGrid.layout(dimensions.width, dimensions.height);
				} else {
					this.workbenchGrid.layout();
				}
S
SteVen Batten 已提交
1767
			}
1768 1769 1770
		}
	}

S
SteVen Batten 已提交
1771 1772 1773 1774
	getMenubarVisibility(): MenuBarVisibility {
		return this.menubarVisibility;
	}

1775 1776 1777 1778
	getPanelPosition(): Position {
		return this.panelPosition;
	}

1779
	setPanelPosition(position: Position): void {
1780 1781
		const wasHidden = this.panelHidden;

1782 1783
		if (this.panelHidden) {
			this.setPanelHidden(false, true /* Skip Layout */);
1784 1785
		} else {
			this.saveLastPanelDimension();
1786
		}
1787

1788 1789 1790 1791
		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);
1792

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

1797 1798 1799 1800
		// Update Styles
		this.panelPart.updateStyles();

		// Layout
S
SteVen Batten 已提交
1801 1802 1803
		if (this.workbenchGrid instanceof Grid) {
			if (!wasHidden) {
				this.saveLastPanelDimension();
1804
			}
1805

1806
			this.workbenchGrid.removeView(this.panelPartView);
S
SteVen Batten 已提交
1807
			this.layout();
1808 1809 1810 1811
		} else {
			this.workbenchGrid.layout();
		}
	}
J
Johannes Rieken 已提交
1812
}