layout.ts 31.0 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.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import { Dimension, Builder } from 'vs/base/browser/builder';
8 9
import { TPromise } from 'vs/base/common/winjs.base';
import * as errors from 'vs/base/common/errors';
J
Johannes Rieken 已提交
10 11 12 13
import { Part } from 'vs/workbench/browser/part';
import { QuickOpenController } from 'vs/workbench/browser/parts/quickopen/quickOpenController';
import { Sash, ISashEvent, IVerticalSashLayoutProvider, IHorizontalSashLayoutProvider, Orientation } from 'vs/base/browser/ui/sash/sash';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
14
import { IPartService, Position, ILayoutOptions, Parts } from 'vs/workbench/services/part/common/partService';
B
Benjamin Pasero 已提交
15
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
J
Johannes Rieken 已提交
16 17 18 19
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
20
import { getZoomFactor } from 'vs/base/browser/browser';
21
import { IThemeService } from 'vs/platform/theme/common/themeService';
22
import { memoize } from 'vs/base/common/decorators';
23
import { NotificationsCenter } from 'vs/workbench/browser/parts/notifications/notificationsCenter';
24
import { NotificationsToasts } from 'vs/workbench/browser/parts/notifications/notificationsToasts';
E
Erich Gamma 已提交
25

26 27 28 29
const MIN_SIDEBAR_PART_WIDTH = 170;
const MIN_EDITOR_PART_HEIGHT = 70;
const MIN_EDITOR_PART_WIDTH = 220;
const MIN_PANEL_PART_HEIGHT = 77;
I
isidor 已提交
30
const MIN_PANEL_PART_WIDTH = 300;
I
isidor 已提交
31
const DEFAULT_PANEL_SIZE_COEFFICIENT = 0.4;
32
const PANEL_SIZE_BEFORE_MAXIMIZED_BOUNDARY = 0.7;
E
Erich Gamma 已提交
33
const HIDE_SIDEBAR_WIDTH_THRESHOLD = 50;
I
isidor 已提交
34
const HIDE_PANEL_HEIGHT_THRESHOLD = 50;
35
const HIDE_PANEL_WIDTH_THRESHOLD = 100;
36 37 38
const TITLE_BAR_HEIGHT = 22;
const STATUS_BAR_HEIGHT = 22;
const ACTIVITY_BAR_WIDTH = 50;
E
Erich Gamma 已提交
39

40
interface PartLayoutInfo {
41 42
	titlebar: { height: number; };
	activitybar: { width: number; };
E
Erich Gamma 已提交
43
	sidebar: { minWidth: number; };
I
isidor 已提交
44
	panel: { minHeight: number; minWidth: number; };
45
	editor: { minWidth: number; minHeight: number; };
E
Erich Gamma 已提交
46 47 48 49
	statusbar: { height: number; };
}

/**
B
Benjamin Pasero 已提交
50
 * The workbench layout is responsible to lay out all parts that make the Workbench.
E
Erich Gamma 已提交
51
 */
I
isidor 已提交
52
export class WorkbenchLayout implements IVerticalSashLayoutProvider, IHorizontalSashLayoutProvider {
E
Erich Gamma 已提交
53

54 55 56 57
	private static readonly sashXOneWidthSettingsKey = 'workbench.sidebar.width';
	private static readonly sashXTwoWidthSettingsKey = 'workbench.panel.width';
	private static readonly sashYHeightSettingsKey = 'workbench.panel.height';
	private static readonly panelSizeBeforeMaximizedKey = 'workbench.panel.sizeBeforeMaximized';
E
Erich Gamma 已提交
58 59 60

	private parent: Builder;
	private workbenchContainer: Builder;
61
	private titlebar: Part;
E
Erich Gamma 已提交
62 63 64
	private activitybar: Part;
	private editor: Part;
	private sidebar: Part;
B
Benjamin Pasero 已提交
65
	private panel: Part;
E
Erich Gamma 已提交
66 67
	private statusbar: Part;
	private quickopen: QuickOpenController;
68
	private notificationsCenter: NotificationsCenter;
69
	private notificationsToasts: NotificationsToasts;
M
Martin Aeschlimann 已提交
70
	private toUnbind: IDisposable[];
E
Erich Gamma 已提交
71
	private workbenchSize: Dimension;
72 73
	private sashXOne: Sash;
	private sashXTwo: Sash;
I
isidor 已提交
74
	private sashY: Sash;
75
	private _sidebarWidth: number;
I
isidor 已提交
76
	private sidebarHeight: number;
77 78
	private titlebarHeight: number;
	private statusbarHeight: number;
79 80
	private panelSizeBeforeMaximized: number;
	private panelMaximized: boolean;
81 82
	private _panelHeight: number;
	private _panelWidth: number;
83
	private layoutEditorGroupsVertically: boolean;
E
Erich Gamma 已提交
84

I
isidor 已提交
85
	// Take parts as an object bag since instatation service does not have typings for constructors with 9+ arguments
E
Erich Gamma 已提交
86 87 88
	constructor(
		parent: Builder,
		workbenchContainer: Builder,
I
isidor 已提交
89
		parts: {
90
			titlebar: Part,
I
isidor 已提交
91 92 93 94 95 96
			activitybar: Part,
			editor: Part,
			sidebar: Part,
			panel: Part,
			statusbar: Part
		},
E
Erich Gamma 已提交
97
		quickopen: QuickOpenController,
98
		notificationsCenter: NotificationsCenter,
99
		notificationsToasts: NotificationsToasts,
E
Erich Gamma 已提交
100 101 102
		@IStorageService private storageService: IStorageService,
		@IContextViewService private contextViewService: IContextViewService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
103
		@IEditorGroupService private editorGroupService: IEditorGroupService,
M
Martin Aeschlimann 已提交
104
		@IPartService private partService: IPartService,
M
Maxime Quandalle 已提交
105
		@IViewletService private viewletService: IViewletService,
106
		@IThemeService themeService: IThemeService
E
Erich Gamma 已提交
107 108 109
	) {
		this.parent = parent;
		this.workbenchContainer = workbenchContainer;
110
		this.titlebar = parts.titlebar;
I
isidor 已提交
111 112 113
		this.activitybar = parts.activitybar;
		this.editor = parts.editor;
		this.sidebar = parts.sidebar;
B
Benjamin Pasero 已提交
114
		this.panel = parts.panel;
I
isidor 已提交
115
		this.statusbar = parts.statusbar;
E
Erich Gamma 已提交
116
		this.quickopen = quickopen;
117
		this.notificationsCenter = notificationsCenter;
118
		this.notificationsToasts = notificationsToasts;
E
Erich Gamma 已提交
119
		this.toUnbind = [];
120
		this.panelSizeBeforeMaximized = this.storageService.getInteger(WorkbenchLayout.panelSizeBeforeMaximizedKey, StorageScope.GLOBAL, 0);
121
		this.panelMaximized = false;
122

123 124 125 126 127
		this.sashXOne = new Sash(this.workbenchContainer.getHTMLElement(), this, {
			baseSize: 5
		});

		this.sashXTwo = new Sash(this.workbenchContainer.getHTMLElement(), this, {
E
Erich Gamma 已提交
128 129
			baseSize: 5
		});
130

I
isidor 已提交
131
		this.sashY = new Sash(this.workbenchContainer.getHTMLElement(), this, {
I
isidor 已提交
132
			baseSize: 4,
I
isidor 已提交
133 134
			orientation: Orientation.HORIZONTAL
		});
E
Erich Gamma 已提交
135

136 137 138
		this._sidebarWidth = this.storageService.getInteger(WorkbenchLayout.sashXOneWidthSettingsKey, StorageScope.GLOBAL, -1);
		this._panelHeight = this.storageService.getInteger(WorkbenchLayout.sashYHeightSettingsKey, StorageScope.GLOBAL, 0);
		this._panelWidth = this.storageService.getInteger(WorkbenchLayout.sashXTwoWidthSettingsKey, StorageScope.GLOBAL, 0);
E
Erich Gamma 已提交
139

140
		this.layoutEditorGroupsVertically = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
141

142
		this.toUnbind.push(themeService.onThemeChange(_ => this.layout()));
143
		this.toUnbind.push(editorGroupService.onEditorsChanged(() => this.onEditorsChanged()));
144
		this.toUnbind.push(editorGroupService.onGroupOrientationChanged(e => this.onGroupOrientationChanged()));
M
Martin Aeschlimann 已提交
145

E
Erich Gamma 已提交
146 147 148
		this.registerSashListeners();
	}

149 150 151 152 153 154 155 156
	private get editorCountForHeight(): number {
		return Math.max(1, this.editorGroupService.getGroupOrientation() === 'horizontal' ? this.editorGroupService.getStacksModel().groups.length : 1);
	}

	private get editorCountForWidth(): number {
		return Math.max(1, this.editorGroupService.getGroupOrientation() === 'vertical' ? this.editorGroupService.getStacksModel().groups.length : 1);
	}

I
isidor 已提交
157 158 159 160 161 162 163 164
	private get activitybarWidth(): number {
		if (this.partService.isVisible(Parts.ACTIVITYBAR_PART)) {
			return this.partLayoutInfo.activitybar.width;
		}

		return 0;
	}

165 166 167 168 169 170 171 172 173 174
	private get panelHeight(): number {
		const panelPosition = this.partService.getPanelPosition();
		if (panelPosition === Position.RIGHT) {
			return this.sidebarHeight;
		}

		return this._panelHeight;
	}

	private set panelHeight(value: number) {
175
		this._panelHeight = Math.min(this.computeMaxPanelHeight(), Math.max(this.partLayoutInfo.panel.minHeight, value));
176 177 178 179 180 181 182 183 184 185 186 187
	}

	private get panelWidth(): number {
		const panelPosition = this.partService.getPanelPosition();
		if (panelPosition === Position.BOTTOM) {
			return this.workbenchSize.width - this.activitybarWidth - this.sidebarWidth;
		}

		return this._panelWidth;
	}

	private set panelWidth(value: number) {
188 189 190 191 192 193 194 195 196 197
		this._panelWidth = Math.min(this.computeMaxPanelWidth(), Math.max(this.partLayoutInfo.panel.minWidth, value));
	}

	private computeMaxPanelWidth(): number {
		const minSidebarSize = this.partService.isVisible(Parts.SIDEBAR_PART) ? (this.partService.getSideBarPosition() === Position.LEFT ? this.partLayoutInfo.sidebar.minWidth : this.sidebarWidth) : 0;
		return Math.max(this.partLayoutInfo.panel.minWidth, this.workbenchSize.width - this.editorCountForWidth * this.partLayoutInfo.editor.minWidth - minSidebarSize - this.activitybarWidth);
	}

	private computeMaxPanelHeight(): number {
		return Math.max(this.partLayoutInfo.panel.minHeight, this.sidebarHeight - this.editorCountForHeight * this.partLayoutInfo.editor.minHeight);
198 199 200 201 202 203 204 205 206 207 208
	}

	private get sidebarWidth(): number {
		if (this.partService.isVisible(Parts.SIDEBAR_PART)) {
			return this._sidebarWidth;
		}

		return 0;
	}

	private set sidebarWidth(value: number) {
209 210
		const panelMinWidth = this.partService.getPanelPosition() === Position.RIGHT && this.partService.isVisible(Parts.PANEL_PART) ? this.partLayoutInfo.panel.minWidth : 0;
		const maxSidebarWidth = this.workbenchSize.width - this.activitybarWidth - this.editorCountForWidth * this.partLayoutInfo.editor.minWidth - panelMinWidth;
I
isidor 已提交
211
		this._sidebarWidth = Math.max(this.partLayoutInfo.sidebar.minWidth, Math.min(maxSidebarWidth, value));
212 213
	}

214 215
	@memoize
	private get partLayoutInfo(): PartLayoutInfo {
216 217 218 219 220 221 222 223 224 225 226
		return {
			titlebar: {
				height: TITLE_BAR_HEIGHT
			},
			activitybar: {
				width: ACTIVITY_BAR_WIDTH
			},
			sidebar: {
				minWidth: MIN_SIDEBAR_PART_WIDTH
			},
			panel: {
I
isidor 已提交
227 228
				minHeight: MIN_PANEL_PART_HEIGHT,
				minWidth: MIN_PANEL_PART_WIDTH
229 230 231 232 233 234 235 236 237 238 239
			},
			editor: {
				minWidth: MIN_EDITOR_PART_WIDTH,
				minHeight: MIN_EDITOR_PART_HEIGHT
			},
			statusbar: {
				height: STATUS_BAR_HEIGHT
			}
		};
	}

E
Erich Gamma 已提交
240 241
	private registerSashListeners(): void {
		let startX: number = 0;
I
isidor 已提交
242
		let startY: number = 0;
243
		let startXTwo: number = 0;
244 245 246
		let startSidebarWidth: number;
		let startPanelHeight: number;
		let startPanelWidth: number;
E
Erich Gamma 已提交
247

I
isidor 已提交
248
		this.toUnbind.push(this.sashXOne.onDidStart((e: ISashEvent) => {
249
			startSidebarWidth = this.sidebarWidth;
E
Erich Gamma 已提交
250
			startX = e.startX;
251
		}));
E
Erich Gamma 已提交
252

I
isidor 已提交
253
		this.toUnbind.push(this.sashY.onDidStart((e: ISashEvent) => {
254
			startPanelHeight = this.panelHeight;
I
isidor 已提交
255
			startY = e.startY;
256 257
		}));

I
isidor 已提交
258
		this.toUnbind.push(this.sashXTwo.onDidStart((e: ISashEvent) => {
259
			startPanelWidth = this.panelWidth;
260
			startXTwo = e.startX;
261
		}));
I
isidor 已提交
262

I
isidor 已提交
263
		this.toUnbind.push(this.sashXOne.onDidChange((e: ISashEvent) => {
E
Erich Gamma 已提交
264 265
			let doLayout = false;
			let sidebarPosition = this.partService.getSideBarPosition();
266
			let isSidebarVisible = this.partService.isVisible(Parts.SIDEBAR_PART);
267
			let newSashWidth = (sidebarPosition === Position.LEFT) ? startSidebarWidth + e.currentX - startX : startSidebarWidth - e.currentX + startX;
268
			let promise = TPromise.wrap<void>(null);
E
Erich Gamma 已提交
269 270

			// Sidebar visible
271
			if (isSidebarVisible) {
E
Erich Gamma 已提交
272 273

				// Automatically hide side bar when a certain threshold is met
274
				if (newSashWidth + HIDE_SIDEBAR_WIDTH_THRESHOLD < this.partLayoutInfo.sidebar.minWidth) {
275
					let dragCompensation = this.partLayoutInfo.sidebar.minWidth - HIDE_SIDEBAR_WIDTH_THRESHOLD;
276
					promise = this.partService.setSideBarHidden(true);
277
					startX = (sidebarPosition === Position.LEFT) ? Math.max(this.activitybarWidth, e.currentX - dragCompensation) : Math.min(e.currentX + dragCompensation, this.workbenchSize.width - this.activitybarWidth);
278
					this.sidebarWidth = startSidebarWidth; // when restoring sidebar, restore to the sidebar width we started from
E
Erich Gamma 已提交
279 280 281 282
				}

				// Otherwise size the sidebar accordingly
				else {
283 284
					this.sidebarWidth = Math.max(this.partLayoutInfo.sidebar.minWidth, newSashWidth); // Sidebar can not become smaller than MIN_PART_WIDTH
					doLayout = newSashWidth >= this.partLayoutInfo.sidebar.minWidth;
E
Erich Gamma 已提交
285 286 287 288 289
				}
			}

			// Sidebar hidden
			else {
290 291
				if ((sidebarPosition === Position.LEFT && e.currentX - startX >= this.partLayoutInfo.sidebar.minWidth) ||
					(sidebarPosition === Position.RIGHT && startX - e.currentX >= this.partLayoutInfo.sidebar.minWidth)) {
292
					startSidebarWidth = this.partLayoutInfo.sidebar.minWidth - (sidebarPosition === Position.LEFT ? e.currentX - startX : startX - e.currentX);
293
					this.sidebarWidth = this.partLayoutInfo.sidebar.minWidth;
294
					promise = this.partService.setSideBarHidden(false);
E
Erich Gamma 已提交
295 296 297 298
				}
			}

			if (doLayout) {
299
				promise.done(() => this.layout({ source: Parts.SIDEBAR_PART }), errors.onUnexpectedError);
E
Erich Gamma 已提交
300
			}
301
		}));
E
Erich Gamma 已提交
302

I
isidor 已提交
303
		this.toUnbind.push(this.sashY.onDidChange((e: ISashEvent) => {
I
isidor 已提交
304
			let doLayout = false;
305
			let isPanelVisible = this.partService.isVisible(Parts.PANEL_PART);
306
			let newSashHeight = startPanelHeight - (e.currentY - startY);
307
			let promise = TPromise.wrap<void>(null);
I
isidor 已提交
308 309

			// Panel visible
310
			if (isPanelVisible) {
311

312
				// Automatically hide panel when a certain threshold is met
313
				if (newSashHeight + HIDE_PANEL_HEIGHT_THRESHOLD < this.partLayoutInfo.panel.minHeight) {
314
					let dragCompensation = this.partLayoutInfo.panel.minHeight - HIDE_PANEL_HEIGHT_THRESHOLD;
315
					promise = this.partService.setPanelHidden(true);
316
					startY = Math.min(this.sidebarHeight - this.statusbarHeight - this.titlebarHeight, e.currentY + dragCompensation);
317
					this.panelHeight = startPanelHeight; // when restoring panel, restore to the panel height we started from
318 319 320 321
				}

				// Otherwise size the panel accordingly
				else {
322 323
					this.panelHeight = Math.max(this.partLayoutInfo.panel.minHeight, newSashHeight); // Panel can not become smaller than MIN_PART_HEIGHT
					doLayout = newSashHeight >= this.partLayoutInfo.panel.minHeight;
324 325
				}
			}
I
isidor 已提交
326

327 328
			// Panel hidden
			else {
329
				if (startY - e.currentY >= this.partLayoutInfo.panel.minHeight) {
330
					startPanelHeight = 0;
331
					this.panelHeight = this.partLayoutInfo.panel.minHeight;
332
					promise = this.partService.setPanelHidden(false);
333
				}
I
isidor 已提交
334
			}
335

I
isidor 已提交
336
			if (doLayout) {
337
				promise.done(() => this.layout({ source: Parts.PANEL_PART }), errors.onUnexpectedError);
I
isidor 已提交
338
			}
339
		}));
I
isidor 已提交
340

I
isidor 已提交
341
		this.toUnbind.push(this.sashXTwo.onDidChange((e: ISashEvent) => {
342 343 344
			let doLayout = false;
			let isPanelVisible = this.partService.isVisible(Parts.PANEL_PART);
			let newSashWidth = startPanelWidth - (e.currentX - startXTwo);
345
			let promise = TPromise.wrap<void>(null);
346 347 348 349 350 351

			// Panel visible
			if (isPanelVisible) {

				// Automatically hide panel when a certain threshold is met
				if (newSashWidth + HIDE_PANEL_WIDTH_THRESHOLD < this.partLayoutInfo.panel.minWidth) {
352
					let dragCompensation = this.partLayoutInfo.panel.minWidth - HIDE_PANEL_WIDTH_THRESHOLD;
353 354 355 356 357 358 359
					promise = this.partService.setPanelHidden(true);
					startXTwo = Math.min(this.workbenchSize.width - this.activitybarWidth, e.currentX + dragCompensation);
					this.panelWidth = startPanelWidth; // when restoring panel, restore to the panel height we started from
				}

				// Otherwise size the panel accordingly
				else {
360
					this.panelWidth = newSashWidth;
361 362 363 364 365 366 367 368 369 370 371 372 373 374
					doLayout = newSashWidth >= this.partLayoutInfo.panel.minWidth;
				}
			}

			// Panel hidden
			else {
				if (startXTwo - e.currentX >= this.partLayoutInfo.panel.minWidth) {
					startPanelWidth = 0;
					this.panelWidth = this.partLayoutInfo.panel.minWidth;
					promise = this.partService.setPanelHidden(false);
				}
			}

			if (doLayout) {
375
				promise.done(() => this.layout({ source: Parts.PANEL_PART }), errors.onUnexpectedError);
376
			}
377
		}));
M
Maxime Quandalle 已提交
378

I
isidor 已提交
379
		this.toUnbind.push(this.sashXOne.onDidEnd(() => {
380 381 382
			this.storageService.store(WorkbenchLayout.sashXOneWidthSettingsKey, this.sidebarWidth, StorageScope.GLOBAL);
		}));

I
isidor 已提交
383
		this.toUnbind.push(this.sashY.onDidEnd(() => {
I
isidor 已提交
384
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
385 386
		}));

I
isidor 已提交
387
		this.toUnbind.push(this.sashXTwo.onDidEnd(() => {
388 389
			this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
		}));
M
Maxime Quandalle 已提交
390

I
isidor 已提交
391
		this.toUnbind.push(this.sashY.onDidReset(() => {
I
isidor 已提交
392
			this.panelHeight = this.sidebarHeight * DEFAULT_PANEL_SIZE_COEFFICIENT;
M
Maxime Quandalle 已提交
393
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
394 395
			this.layout();
		}));
M
Maxime Quandalle 已提交
396

I
isidor 已提交
397
		this.toUnbind.push(this.sashXOne.onDidReset(() => {
M
Maxime Quandalle 已提交
398 399
			let activeViewlet = this.viewletService.getActiveViewlet();
			let optimalWidth = activeViewlet && activeViewlet.getOptimalWidth();
400
			this.sidebarWidth = optimalWidth || 0;
401
			this.storageService.store(WorkbenchLayout.sashXOneWidthSettingsKey, this.sidebarWidth, StorageScope.GLOBAL);
402
			this.partService.setSideBarHidden(false).done(() => this.layout(), errors.onUnexpectedError);
403 404
		}));

I
isidor 已提交
405
		this.toUnbind.push(this.sashXTwo.onDidReset(() => {
406
			this.panelWidth = (this.workbenchSize.width - this.sidebarWidth - this.activitybarWidth) * DEFAULT_PANEL_SIZE_COEFFICIENT;
407 408 409
			this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
			this.layout();
		}));
E
Erich Gamma 已提交
410 411
	}

412
	private onEditorsChanged(): void {
E
Erich Gamma 已提交
413

414
		// Make sure that we layout properly in case we detect that the sidebar or panel is large enought to cause
E
Erich Gamma 已提交
415 416
		// multiple opened editors to go below minimal size. The fix is to trigger a layout for any editor
		// input change that falls into this category.
417
		if (this.workbenchSize && (this.sidebarWidth || this.panelHeight)) {
E
Erich Gamma 已提交
418
			let visibleEditors = this.editorService.getVisibleEditors().length;
I
isidor 已提交
419
			const panelVertical = this.partService.getPanelPosition() === Position.RIGHT;
420
			if (visibleEditors > 1) {
421 422
				const sidebarOverflow = this.layoutEditorGroupsVertically && (this.workbenchSize.width - this.sidebarWidth < visibleEditors * this.partLayoutInfo.editor.minWidth);
				const panelOverflow = !this.layoutEditorGroupsVertically && !panelVertical && (this.workbenchSize.height - this.panelHeight < visibleEditors * this.partLayoutInfo.editor.minHeight) ||
I
isidor 已提交
423
					panelVertical && this.layoutEditorGroupsVertically && (this.workbenchSize.width - this.panelWidth - this.sidebarWidth < visibleEditors * this.partLayoutInfo.editor.minWidth);
424 425 426 427

				if (sidebarOverflow || panelOverflow) {
					this.layout();
				}
E
Erich Gamma 已提交
428 429 430 431
			}
		}
	}

432 433
	private onGroupOrientationChanged(): void {
		const newLayoutEditorGroupsVertically = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
434

435 436
		const doLayout = this.layoutEditorGroupsVertically !== newLayoutEditorGroupsVertically;
		this.layoutEditorGroupsVertically = newLayoutEditorGroupsVertically;
437 438 439 440 441 442

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

443
	public layout(options?: ILayoutOptions): void {
B
Benjamin Pasero 已提交
444
		this.workbenchSize = this.parent.getClientArea();
E
Erich Gamma 已提交
445

446
		const isActivityBarHidden = !this.partService.isVisible(Parts.ACTIVITYBAR_PART);
447 448 449 450
		const isTitlebarHidden = !this.partService.isVisible(Parts.TITLEBAR_PART);
		const isPanelHidden = !this.partService.isVisible(Parts.PANEL_PART);
		const isStatusbarHidden = !this.partService.isVisible(Parts.STATUSBAR_PART);
		const isSidebarHidden = !this.partService.isVisible(Parts.SIDEBAR_PART);
I
isidor 已提交
451
		const sidebarPosition = this.partService.getSideBarPosition();
452
		const panelPosition = this.partService.getPanelPosition();
E
Erich Gamma 已提交
453 454

		// Sidebar
I
isidor 已提交
455
		if (this.sidebarWidth === -1) {
456
			this.sidebarWidth = this.workbenchSize.width / 5;
E
Erich Gamma 已提交
457
		}
B
Benjamin Pasero 已提交
458

459 460
		this.statusbarHeight = isStatusbarHidden ? 0 : this.partLayoutInfo.statusbar.height;
		this.titlebarHeight = isTitlebarHidden ? 0 : this.partLayoutInfo.titlebar.height / getZoomFactor(); // adjust for zoom prevention
E
Erich Gamma 已提交
461

462
		this.sidebarHeight = this.workbenchSize.height - this.statusbarHeight - this.titlebarHeight;
463
		let sidebarSize = new Dimension(this.sidebarWidth, this.sidebarHeight);
E
Erich Gamma 已提交
464 465

		// Activity Bar
466
		let activityBarSize = new Dimension(this.activitybarWidth, sidebarSize.height);
E
Erich Gamma 已提交
467

I
isidor 已提交
468 469
		// Panel part
		let panelHeight: number;
I
isidor 已提交
470
		let panelWidth: number;
471 472
		const maxPanelHeight = this.computeMaxPanelHeight();
		const maxPanelWidth = this.computeMaxPanelWidth();
I
isidor 已提交
473

I
isidor 已提交
474
		if (isPanelHidden) {
I
isidor 已提交
475
			panelHeight = 0;
I
isidor 已提交
476 477
			panelWidth = 0;
		} else if (panelPosition === Position.BOTTOM) {
478
			if (this.panelHeight > 0) {
I
isidor 已提交
479 480 481 482 483
				panelHeight = Math.min(maxPanelHeight, Math.max(this.partLayoutInfo.panel.minHeight, this.panelHeight));
			} else {
				panelHeight = sidebarSize.height * DEFAULT_PANEL_SIZE_COEFFICIENT;
			}
			panelWidth = this.workbenchSize.width - sidebarSize.width - activityBarSize.width;
484 485 486 487 488 489 490 491 492

			if (options && options.toggleMaximizedPanel) {
				panelHeight = this.panelMaximized ? Math.max(this.partLayoutInfo.panel.minHeight, Math.min(this.panelSizeBeforeMaximized, maxPanelHeight)) : maxPanelHeight;
			}

			this.panelMaximized = panelHeight === maxPanelHeight;
			if (panelHeight / maxPanelHeight < PANEL_SIZE_BEFORE_MAXIMIZED_BOUNDARY) {
				this.panelSizeBeforeMaximized = panelHeight;
			}
I
isidor 已提交
493
		} else {
I
isidor 已提交
494 495 496 497 498 499
			panelHeight = sidebarSize.height;
			if (this.panelWidth > 0) {
				panelWidth = Math.min(maxPanelWidth, Math.max(this.partLayoutInfo.panel.minWidth, this.panelWidth));
			} else {
				panelWidth = (this.workbenchSize.width - activityBarSize.width - sidebarSize.width) * DEFAULT_PANEL_SIZE_COEFFICIENT;
			}
500 501 502 503 504 505 506 507 508

			if (options && options.toggleMaximizedPanel) {
				panelWidth = this.panelMaximized ? Math.max(this.partLayoutInfo.panel.minWidth, Math.min(this.panelSizeBeforeMaximized, maxPanelWidth)) : maxPanelWidth;
			}

			this.panelMaximized = panelWidth === maxPanelWidth;
			if (panelWidth / maxPanelWidth < PANEL_SIZE_BEFORE_MAXIMIZED_BOUNDARY) {
				this.panelSizeBeforeMaximized = panelWidth;
			}
I
isidor 已提交
509
		}
510
		this.storageService.store(WorkbenchLayout.panelSizeBeforeMaximizedKey, this.panelSizeBeforeMaximized, StorageScope.GLOBAL);
I
isidor 已提交
511
		const panelDimension = new Dimension(panelWidth, panelHeight);
I
isidor 已提交
512

E
Erich Gamma 已提交
513 514 515
		// Editor
		let editorSize = {
			width: 0,
I
isidor 已提交
516
			height: 0
E
Erich Gamma 已提交
517 518
		};

I
isidor 已提交
519 520
		editorSize.width = this.workbenchSize.width - sidebarSize.width - activityBarSize.width - (panelPosition === Position.RIGHT ? panelDimension.width : 0);
		editorSize.height = sidebarSize.height - (panelPosition === Position.BOTTOM ? panelDimension.height : 0);
E
Erich Gamma 已提交
521 522

		// Assert Sidebar and Editor Size to not overflow
523 524
		let editorMinWidth = this.partLayoutInfo.editor.minWidth;
		let editorMinHeight = this.partLayoutInfo.editor.minHeight;
E
Erich Gamma 已提交
525 526
		let visibleEditorCount = this.editorService.getVisibleEditors().length;
		if (visibleEditorCount > 1) {
527
			if (this.layoutEditorGroupsVertically) {
528 529 530 531
				editorMinWidth *= visibleEditorCount; // when editors layout vertically, multiply the min editor width by number of visible editors
			} else {
				editorMinHeight *= visibleEditorCount; // when editors layout horizontally, multiply the min editor height by number of visible editors
			}
E
Erich Gamma 已提交
532
		}
B
Benjamin Pasero 已提交
533

534 535 536 537 538
		if (editorSize.width < editorMinWidth) {
			let diff = editorMinWidth - editorSize.width;
			editorSize.width = editorMinWidth;
			if (panelPosition === Position.BOTTOM) {
				panelDimension.width = editorMinWidth;
539
			} else if (panelDimension.width >= diff && (!options || options.source !== Parts.PANEL_PART)) {
I
isidor 已提交
540
				const oldWidth = panelDimension.width;
541
				panelDimension.width = Math.max(this.partLayoutInfo.panel.minWidth, panelDimension.width - diff);
I
isidor 已提交
542
				diff = diff - (oldWidth - panelDimension.width);
543 544
			}

I
isidor 已提交
545 546
			if (sidebarSize.width >= diff) {
				sidebarSize.width -= diff;
547
				sidebarSize.width = Math.max(this.partLayoutInfo.sidebar.minWidth, sidebarSize.width);
I
isidor 已提交
548
			}
549 550 551 552 553 554 555
		}

		if (editorSize.height < editorMinHeight && panelPosition === Position.BOTTOM) {
			let diff = editorMinHeight - editorSize.height;
			editorSize.height = editorMinHeight;

			panelDimension.height -= diff;
556
			panelDimension.height = Math.max(this.partLayoutInfo.panel.minHeight, panelDimension.height);
557
		}
E
Erich Gamma 已提交
558 559 560

		if (!isSidebarHidden) {
			this.sidebarWidth = sidebarSize.width;
561
			this.storageService.store(WorkbenchLayout.sashXOneWidthSettingsKey, this.sidebarWidth, StorageScope.GLOBAL);
E
Erich Gamma 已提交
562 563
		}

I
isidor 已提交
564
		if (!isPanelHidden) {
565 566 567 568 569 570 571
			if (panelPosition === Position.BOTTOM) {
				this.panelHeight = panelDimension.height;
				this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
			} else {
				this.panelWidth = panelDimension.width;
				this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
			}
I
isidor 已提交
572 573
		}

E
Erich Gamma 已提交
574 575
		// Workbench
		this.workbenchContainer
576
			.position(0, 0, 0, 0, 'relative')
E
Erich Gamma 已提交
577 578
			.size(this.workbenchSize.width, this.workbenchSize.height);

B
Benjamin Pasero 已提交
579 580 581 582 583 584 585
		// Bug on Chrome: Sometimes Chrome wants to scroll the workbench container on layout changes. The fix is to reset scrolling in this case.
		const workbenchContainer = this.workbenchContainer.getHTMLElement();
		if (workbenchContainer.scrollTop > 0) {
			workbenchContainer.scrollTop = 0;
		}
		if (workbenchContainer.scrollLeft > 0) {
			workbenchContainer.scrollLeft = 0;
E
Erich Gamma 已提交
586 587
		}

588 589 590 591 592 593 594
		// Title Part
		if (isTitlebarHidden) {
			this.titlebar.getContainer().hide();
		} else {
			this.titlebar.getContainer().show();
		}

I
isidor 已提交
595
		// Editor Part and Panel part
E
Erich Gamma 已提交
596
		this.editor.getContainer().size(editorSize.width, editorSize.height);
B
Benjamin Pasero 已提交
597
		this.panel.getContainer().size(panelDimension.width, panelDimension.height);
E
Erich Gamma 已提交
598

I
isidor 已提交
599 600 601 602 603 604 605 606
		if (panelPosition === Position.BOTTOM) {
			if (sidebarPosition === Position.LEFT) {
				this.editor.getContainer().position(this.titlebarHeight, 0, this.statusbarHeight + panelDimension.height, sidebarSize.width + activityBarSize.width);
				this.panel.getContainer().position(editorSize.height + this.titlebarHeight, 0, this.statusbarHeight, sidebarSize.width + activityBarSize.width);
			} else {
				this.editor.getContainer().position(this.titlebarHeight, sidebarSize.width, this.statusbarHeight + panelDimension.height, 0);
				this.panel.getContainer().position(editorSize.height + this.titlebarHeight, sidebarSize.width, this.statusbarHeight, 0);
			}
E
Erich Gamma 已提交
607
		} else {
I
isidor 已提交
608 609 610 611 612 613 614
			if (sidebarPosition === Position.LEFT) {
				this.editor.getContainer().position(this.titlebarHeight, panelDimension.width, this.statusbarHeight, sidebarSize.width + activityBarSize.width);
				this.panel.getContainer().position(this.titlebarHeight, 0, this.statusbarHeight, sidebarSize.width + activityBarSize.width + editorSize.width);
			} else {
				this.editor.getContainer().position(this.titlebarHeight, sidebarSize.width + activityBarSize.width + panelWidth, this.statusbarHeight, 0);
				this.panel.getContainer().position(this.titlebarHeight, sidebarSize.width + activityBarSize.width, this.statusbarHeight, editorSize.width);
			}
E
Erich Gamma 已提交
615 616 617
		}

		// Activity Bar Part
B
Benjamin Pasero 已提交
618
		this.activitybar.getContainer().size(null, activityBarSize.height);
E
Erich Gamma 已提交
619 620
		if (sidebarPosition === Position.LEFT) {
			this.activitybar.getContainer().getHTMLElement().style.right = '';
621
			this.activitybar.getContainer().position(this.titlebarHeight, null, 0, 0);
E
Erich Gamma 已提交
622 623
		} else {
			this.activitybar.getContainer().getHTMLElement().style.left = '';
624
			this.activitybar.getContainer().position(this.titlebarHeight, 0, 0, null);
E
Erich Gamma 已提交
625
		}
B
Benjamin Pasero 已提交
626 627 628 629 630
		if (isActivityBarHidden) {
			this.activitybar.getContainer().hide();
		} else {
			this.activitybar.getContainer().show();
		}
E
Erich Gamma 已提交
631 632 633

		// Sidebar Part
		this.sidebar.getContainer().size(sidebarSize.width, sidebarSize.height);
634
		const editorAndPanelWidth = editorSize.width + (panelPosition === Position.RIGHT ? panelWidth : 0);
E
Erich Gamma 已提交
635
		if (sidebarPosition === Position.LEFT) {
636
			this.sidebar.getContainer().position(this.titlebarHeight, editorAndPanelWidth, this.statusbarHeight, activityBarSize.width);
E
Erich Gamma 已提交
637
		} else {
638
			this.sidebar.getContainer().position(this.titlebarHeight, activityBarSize.width, this.statusbarHeight, editorAndPanelWidth);
E
Erich Gamma 已提交
639 640 641
		}

		// Statusbar Part
642
		this.statusbar.getContainer().position(this.workbenchSize.height - this.statusbarHeight);
643 644 645 646 647
		if (isStatusbarHidden) {
			this.statusbar.getContainer().hide();
		} else {
			this.statusbar.getContainer().show();
		}
E
Erich Gamma 已提交
648 649 650 651

		// Quick open
		this.quickopen.layout(this.workbenchSize);

652 653
		// Notifications
		this.notificationsCenter.layout(this.workbenchSize);
654
		this.notificationsToasts.layout(this.workbenchSize);
655

I
isidor 已提交
656
		// Sashes
657 658 659
		this.sashXOne.layout();
		if (panelPosition === Position.BOTTOM) {
			this.sashXTwo.hide();
660 661
			this.sashY.layout();
			this.sashY.show();
662 663 664
		} else {
			this.sashY.hide();
			this.sashXTwo.layout();
665
			this.sashXTwo.show();
666
		}
E
Erich Gamma 已提交
667 668

		// Propagate to Part Layouts
669
		this.titlebar.layout(new Dimension(this.workbenchSize.width, this.titlebarHeight));
E
Erich Gamma 已提交
670 671
		this.editor.layout(new Dimension(editorSize.width, editorSize.height));
		this.sidebar.layout(sidebarSize);
B
Benjamin Pasero 已提交
672
		this.panel.layout(panelDimension);
673
		this.activitybar.layout(activityBarSize);
E
Erich Gamma 已提交
674 675

		// Propagate to Context View
B
Benjamin Pasero 已提交
676
		this.contextViewService.layout();
E
Erich Gamma 已提交
677 678 679
	}

	public getVerticalSashTop(sash: Sash): number {
680
		return this.titlebarHeight;
E
Erich Gamma 已提交
681 682 683 684
	}

	public getVerticalSashLeft(sash: Sash): number {
		let sidebarPosition = this.partService.getSideBarPosition();
685
		if (sash === this.sashXOne) {
E
Erich Gamma 已提交
686

687
			if (sidebarPosition === Position.LEFT) {
688
				return this.sidebarWidth + this.activitybarWidth;
689 690
			}

691
			return this.workbenchSize.width - this.sidebarWidth - this.activitybarWidth;
E
Erich Gamma 已提交
692 693
		}

694
		return this.workbenchSize.width - this.panelWidth - (sidebarPosition === Position.RIGHT ? this.sidebarWidth + this.activitybarWidth : 0);
E
Erich Gamma 已提交
695 696 697
	}

	public getVerticalSashHeight(sash: Sash): number {
698 699 700 701
		if (sash === this.sashXTwo && !this.partService.isVisible(Parts.PANEL_PART)) {
			return 0;
		}

I
isidor 已提交
702
		return this.sidebarHeight;
E
Erich Gamma 已提交
703 704
	}

I
isidor 已提交
705
	public getHorizontalSashTop(sash: Sash): number {
706 707
		// Horizontal sash should be a bit lower than the editor area, thus add 2px #5524
		return 2 + (this.partService.isVisible(Parts.PANEL_PART) ? this.sidebarHeight - this.panelHeight + this.titlebarHeight : this.sidebarHeight + this.titlebarHeight);
I
isidor 已提交
708 709 710
	}

	public getHorizontalSashLeft(sash: Sash): number {
711 712 713 714 715
		if (this.partService.getSideBarPosition() === Position.RIGHT) {
			return 0;
		}

		return this.sidebarWidth + this.activitybarWidth;
I
isidor 已提交
716 717 718
	}

	public getHorizontalSashWidth(sash: Sash): number {
I
isidor 已提交
719
		return this.panelWidth;
I
isidor 已提交
720 721
	}

722 723 724 725
	public isPanelMaximized(): boolean {
		return this.panelMaximized;
	}

726
	// change part size along the main axis
B
Benjamin Pasero 已提交
727
	public resizePart(part: Parts, sizeChange: number): void {
728
		const panelPosition = this.partService.getPanelPosition();
729 730 731
		const sizeChangePxWidth = this.workbenchSize.width * (sizeChange / 100);
		const sizeChangePxHeight = this.workbenchSize.height * (sizeChange / 100);

B
Benjamin Pasero 已提交
732 733
		let doLayout = false;

734 735
		switch (part) {
			case Parts.SIDEBAR_PART:
736
				this.sidebarWidth = this.sidebarWidth + sizeChangePxWidth; // Sidebar can not become smaller than MIN_PART_WIDTH
737

I
isidor 已提交
738 739
				if (this.layoutEditorGroupsVertically && (this.workbenchSize.width - this.sidebarWidth < this.editorCountForWidth * MIN_EDITOR_PART_WIDTH)) {
					this.sidebarWidth = (this.workbenchSize.width - this.editorCountForWidth * MIN_EDITOR_PART_WIDTH);
740 741 742 743 744
				}

				doLayout = true;
				break;
			case Parts.PANEL_PART:
745 746 747 748 749 750
				if (panelPosition === Position.BOTTOM) {
					this.panelHeight = this.panelHeight + sizeChangePxHeight;
				} else if (panelPosition === Position.RIGHT) {
					this.panelWidth = this.panelWidth + sizeChangePxWidth;
				}

751 752 753 754
				doLayout = true;
				break;
			case Parts.EDITOR_PART:
				// If we have one editor we can cheat and resize sidebar with the negative delta
755
				// If the sidebar is not visible and panel is, resize panel main axis with negative Delta
I
isidor 已提交
756
				if (this.editorCountForWidth === 1) {
C
cleidigh 已提交
757
					if (this.partService.isVisible(Parts.SIDEBAR_PART)) {
758 759
						this.sidebarWidth = this.sidebarWidth - sizeChangePxWidth;
						doLayout = true;
C
cleidigh 已提交
760
					} else if (this.partService.isVisible(Parts.PANEL_PART)) {
761 762 763 764 765 766 767 768
						if (panelPosition === Position.BOTTOM) {
							this.panelHeight = this.panelHeight - sizeChangePxHeight;
						} else if (panelPosition === Position.RIGHT) {
							this.panelWidth = this.panelWidth - sizeChangePxWidth;
						}
						doLayout = true;
					}

B
Benjamin Pasero 已提交
769 770 771
				} else {
					const stacks = this.editorGroupService.getStacksModel();
					const activeGroup = stacks.positionOfGroup(stacks.activeGroup);
772 773 774 775 776 777 778

					this.editorGroupService.resizeGroup(activeGroup, sizeChangePxWidth);
					doLayout = false;
				}
		}

		if (doLayout) {
B
Benjamin Pasero 已提交
779
			this.layout();
780 781 782
		}
	}

E
Erich Gamma 已提交
783
	public dispose(): void {
M
Martin Aeschlimann 已提交
784 785 786
		if (this.toUnbind) {
			dispose(this.toUnbind);
			this.toUnbind = null;
E
Erich Gamma 已提交
787 788
		}
	}
B
Benjamin Pasero 已提交
789
}