layout.ts 29.8 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';
E
Erich Gamma 已提交
22

23 24 25 26
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 已提交
27
const MIN_PANEL_PART_WIDTH = 300;
I
isidor 已提交
28
const DEFAULT_PANEL_SIZE_COEFFICIENT = 0.4;
29
const PANEL_SIZE_BEFORE_MAXIMIZED_BOUNDARY = 0.7;
E
Erich Gamma 已提交
30
const HIDE_SIDEBAR_WIDTH_THRESHOLD = 50;
I
isidor 已提交
31
const HIDE_PANEL_HEIGHT_THRESHOLD = 50;
32
const HIDE_PANEL_WIDTH_THRESHOLD = 100;
33 34 35
const TITLE_BAR_HEIGHT = 22;
const STATUS_BAR_HEIGHT = 22;
const ACTIVITY_BAR_WIDTH = 50;
E
Erich Gamma 已提交
36

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

46 47
interface ILayoutInternalOptions extends ILayoutOptions { };

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

53 54
	private static sashXOneWidthSettingsKey = 'workbench.sidebar.width';
	private static sashXTwoWidthSettingsKey = 'workbench.panel.width';
I
isidor 已提交
55
	private static sashYHeightSettingsKey = 'workbench.panel.height';
E
Erich Gamma 已提交
56 57 58

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

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

117 118 119 120 121
		this.sashXOne = new Sash(this.workbenchContainer.getHTMLElement(), this, {
			baseSize: 5
		});

		this.sashXTwo = new Sash(this.workbenchContainer.getHTMLElement(), this, {
E
Erich Gamma 已提交
122 123
			baseSize: 5
		});
124

I
isidor 已提交
125
		this.sashY = new Sash(this.workbenchContainer.getHTMLElement(), this, {
I
isidor 已提交
126
			baseSize: 4,
I
isidor 已提交
127 128
			orientation: Orientation.HORIZONTAL
		});
E
Erich Gamma 已提交
129

130 131 132
		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 已提交
133

134
		this.layoutEditorGroupsVertically = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
135

136
		this.toUnbind.push(themeService.onThemeChange(_ => this.layout()));
137
		this.toUnbind.push(editorGroupService.onEditorsChanged(() => this.onEditorsChanged()));
138
		this.toUnbind.push(editorGroupService.onGroupOrientationChanged(e => this.onGroupOrientationChanged()));
M
Martin Aeschlimann 已提交
139

E
Erich Gamma 已提交
140 141 142
		this.registerSashListeners();
	}

I
isidor 已提交
143 144 145 146 147 148 149 150
	private get activitybarWidth(): number {
		if (this.partService.isVisible(Parts.ACTIVITYBAR_PART)) {
			return this.partLayoutInfo.activitybar.width;
		}

		return 0;
	}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	private get panelHeight(): number {
		const panelPosition = this.partService.getPanelPosition();
		if (panelPosition === Position.RIGHT) {
			return this.sidebarHeight;
		}

		return this._panelHeight;
	}

	private set panelHeight(value: number) {
		const editorCountForHeight = this.editorGroupService.getGroupOrientation() === 'horizontal' ? this.editorGroupService.getStacksModel().groups.length : 1;
		const maxPanelHeight = this.sidebarHeight - editorCountForHeight * MIN_EDITOR_PART_HEIGHT;
		this._panelHeight = Math.min(maxPanelHeight, Math.max(this.partLayoutInfo.panel.minHeight, value));
	}

	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) {
		const editorCountForWidth = this.editorGroupService.getGroupOrientation() === 'vertical' ? this.editorGroupService.getStacksModel().groups.length : 1;
I
isidor 已提交
177
		const maxPanelWidth = this.workbenchSize.width - editorCountForWidth * MIN_EDITOR_PART_WIDTH - MIN_SIDEBAR_PART_WIDTH - this.activitybarWidth;
178 179 180 181 182 183 184 185 186 187 188 189
		this._panelWidth = Math.min(maxPanelWidth, Math.max(this.partLayoutInfo.panel.minWidth, value));
	}

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

		return 0;
	}

	private set sidebarWidth(value: number) {
I
isidor 已提交
190 191
		const editorCountForWidth = this.editorGroupService.getGroupOrientation() === 'vertical' ? this.editorGroupService.getStacksModel().groups.length : 1;
		const panelMinWidth = this.partService.getPanelPosition() === Position.RIGHT && this.partService.isVisible(Parts.PANEL_PART) ? MIN_PANEL_PART_WIDTH : 0;
I
isidor 已提交
192 193
		const maxSidebarWidth = this.workbenchSize.width - this.activitybarWidth - editorCountForWidth * MIN_EDITOR_PART_WIDTH - panelMinWidth;
		this._sidebarWidth = Math.max(this.partLayoutInfo.sidebar.minWidth, Math.min(maxSidebarWidth, value));
194 195
	}

196 197 198 199 200 201 202 203 204 205 206 207
	private getPartLayoutInfo(): PartLayoutInfo {
		return {
			titlebar: {
				height: TITLE_BAR_HEIGHT
			},
			activitybar: {
				width: ACTIVITY_BAR_WIDTH
			},
			sidebar: {
				minWidth: MIN_SIDEBAR_PART_WIDTH
			},
			panel: {
I
isidor 已提交
208 209
				minHeight: MIN_PANEL_PART_HEIGHT,
				minWidth: MIN_PANEL_PART_WIDTH
210 211 212 213 214 215 216 217 218 219 220
			},
			editor: {
				minWidth: MIN_EDITOR_PART_WIDTH,
				minHeight: MIN_EDITOR_PART_HEIGHT
			},
			statusbar: {
				height: STATUS_BAR_HEIGHT
			}
		};
	}

E
Erich Gamma 已提交
221 222
	private registerSashListeners(): void {
		let startX: number = 0;
I
isidor 已提交
223
		let startY: number = 0;
224
		let startXTwo: number = 0;
225 226 227
		let startSidebarWidth: number;
		let startPanelHeight: number;
		let startPanelWidth: number;
E
Erich Gamma 已提交
228

229 230
		this.toUnbind.push(this.sashXOne.addListener('start', (e: ISashEvent) => {
			startSidebarWidth = this.sidebarWidth;
E
Erich Gamma 已提交
231
			startX = e.startX;
232
		}));
E
Erich Gamma 已提交
233

234 235
		this.toUnbind.push(this.sashY.addListener('start', (e: ISashEvent) => {
			startPanelHeight = this.panelHeight;
I
isidor 已提交
236
			startY = e.startY;
237 238 239 240
		}));

		this.toUnbind.push(this.sashXTwo.addListener('start', (e: ISashEvent) => {
			startPanelWidth = this.panelWidth;
241
			startXTwo = e.startX;
242
		}));
I
isidor 已提交
243

244
		this.toUnbind.push(this.sashXOne.addListener('change', (e: ISashEvent) => {
E
Erich Gamma 已提交
245 246
			let doLayout = false;
			let sidebarPosition = this.partService.getSideBarPosition();
247
			let isSidebarVisible = this.partService.isVisible(Parts.SIDEBAR_PART);
248
			let newSashWidth = (sidebarPosition === Position.LEFT) ? startSidebarWidth + e.currentX - startX : startSidebarWidth - e.currentX + startX;
249
			let promise = TPromise.as<void>(null);
E
Erich Gamma 已提交
250 251

			// Sidebar visible
252
			if (isSidebarVisible) {
E
Erich Gamma 已提交
253 254

				// Automatically hide side bar when a certain threshold is met
255 256
				if (newSashWidth + HIDE_SIDEBAR_WIDTH_THRESHOLD < this.partLayoutInfo.sidebar.minWidth) {
					let dragCompensation = MIN_SIDEBAR_PART_WIDTH - HIDE_SIDEBAR_WIDTH_THRESHOLD;
257
					promise = this.partService.setSideBarHidden(true);
258
					startX = (sidebarPosition === Position.LEFT) ? Math.max(this.activitybarWidth, e.currentX - dragCompensation) : Math.min(e.currentX + dragCompensation, this.workbenchSize.width - this.activitybarWidth);
259
					this.sidebarWidth = startSidebarWidth; // when restoring sidebar, restore to the sidebar width we started from
E
Erich Gamma 已提交
260 261 262 263
				}

				// Otherwise size the sidebar accordingly
				else {
264 265
					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 已提交
266 267 268 269 270
				}
			}

			// Sidebar hidden
			else {
271 272
				if ((sidebarPosition === Position.LEFT && e.currentX - startX >= this.partLayoutInfo.sidebar.minWidth) ||
					(sidebarPosition === Position.RIGHT && startX - e.currentX >= this.partLayoutInfo.sidebar.minWidth)) {
273
					startSidebarWidth = this.partLayoutInfo.sidebar.minWidth - (sidebarPosition === Position.LEFT ? e.currentX - startX : startX - e.currentX);
274
					this.sidebarWidth = this.partLayoutInfo.sidebar.minWidth;
275
					promise = this.partService.setSideBarHidden(false);
E
Erich Gamma 已提交
276 277 278 279
				}
			}

			if (doLayout) {
280
				promise.done(() => this.layout({ source: Parts.SIDEBAR_PART }), errors.onUnexpectedError);
E
Erich Gamma 已提交
281
			}
282
		}));
E
Erich Gamma 已提交
283

284
		this.toUnbind.push(this.sashY.addListener('change', (e: ISashEvent) => {
I
isidor 已提交
285
			let doLayout = false;
286
			let isPanelVisible = this.partService.isVisible(Parts.PANEL_PART);
287
			let newSashHeight = startPanelHeight - (e.currentY - startY);
288
			let promise = TPromise.as<void>(null);
I
isidor 已提交
289 290

			// Panel visible
291
			if (isPanelVisible) {
292

293
				// Automatically hide panel when a certain threshold is met
294 295
				if (newSashHeight + HIDE_PANEL_HEIGHT_THRESHOLD < this.partLayoutInfo.panel.minHeight) {
					let dragCompensation = MIN_PANEL_PART_HEIGHT - HIDE_PANEL_HEIGHT_THRESHOLD;
296
					promise = this.partService.setPanelHidden(true);
297
					startY = Math.min(this.sidebarHeight - this.statusbarHeight - this.titlebarHeight, e.currentY + dragCompensation);
298
					this.panelHeight = startPanelHeight; // when restoring panel, restore to the panel height we started from
299 300 301 302
				}

				// Otherwise size the panel accordingly
				else {
303 304
					this.panelHeight = Math.max(this.partLayoutInfo.panel.minHeight, newSashHeight); // Panel can not become smaller than MIN_PART_HEIGHT
					doLayout = newSashHeight >= this.partLayoutInfo.panel.minHeight;
305 306
				}
			}
I
isidor 已提交
307

308 309
			// Panel hidden
			else {
310
				if (startY - e.currentY >= this.partLayoutInfo.panel.minHeight) {
311
					startPanelHeight = 0;
312
					this.panelHeight = this.partLayoutInfo.panel.minHeight;
313
					promise = this.partService.setPanelHidden(false);
314
				}
I
isidor 已提交
315
			}
316

I
isidor 已提交
317
			if (doLayout) {
318
				promise.done(() => this.layout({ source: Parts.PANEL_PART }), errors.onUnexpectedError);
I
isidor 已提交
319
			}
320
		}));
I
isidor 已提交
321

322
		this.toUnbind.push(this.sashXTwo.addListener('change', (e: ISashEvent) => {
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
			let doLayout = false;
			let isPanelVisible = this.partService.isVisible(Parts.PANEL_PART);
			let newSashWidth = startPanelWidth - (e.currentX - startXTwo);
			let promise = TPromise.as<void>(null);

			// Panel visible
			if (isPanelVisible) {

				// Automatically hide panel when a certain threshold is met
				if (newSashWidth + HIDE_PANEL_WIDTH_THRESHOLD < this.partLayoutInfo.panel.minWidth) {
					let dragCompensation = MIN_PANEL_PART_WIDTH - HIDE_PANEL_WIDTH_THRESHOLD;
					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 {
341
					this.panelWidth = newSashWidth;
342 343 344 345 346 347 348 349 350 351 352 353 354 355
					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) {
356
				promise.done(() => this.layout({ source: Parts.PANEL_PART }), errors.onUnexpectedError);
357
			}
358
		}));
M
Maxime Quandalle 已提交
359

360 361 362 363 364
		this.toUnbind.push(this.sashXOne.addListener('end', () => {
			this.storageService.store(WorkbenchLayout.sashXOneWidthSettingsKey, this.sidebarWidth, StorageScope.GLOBAL);
		}));

		this.toUnbind.push(this.sashY.addListener('end', () => {
I
isidor 已提交
365
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
366 367 368 369 370
		}));

		this.toUnbind.push(this.sashXTwo.addListener('end', () => {
			this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
		}));
M
Maxime Quandalle 已提交
371

372
		this.toUnbind.push(this.sashY.addListener('reset', () => {
I
isidor 已提交
373
			this.panelHeight = this.sidebarHeight * DEFAULT_PANEL_SIZE_COEFFICIENT;
M
Maxime Quandalle 已提交
374
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
375 376
			this.layout();
		}));
M
Maxime Quandalle 已提交
377

378
		this.toUnbind.push(this.sashXOne.addListener('reset', () => {
M
Maxime Quandalle 已提交
379 380
			let activeViewlet = this.viewletService.getActiveViewlet();
			let optimalWidth = activeViewlet && activeViewlet.getOptimalWidth();
381
			this.sidebarWidth = optimalWidth || 0;
382
			this.storageService.store(WorkbenchLayout.sashXOneWidthSettingsKey, this.sidebarWidth, StorageScope.GLOBAL);
383
			this.partService.setSideBarHidden(false).done(() => this.layout(), errors.onUnexpectedError);
384 385 386
		}));

		this.toUnbind.push(this.sashXTwo.addListener('reset', () => {
387
			this.panelWidth = (this.workbenchSize.width - this.sidebarWidth - this.activitybarWidth) * DEFAULT_PANEL_SIZE_COEFFICIENT;
388 389 390
			this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
			this.layout();
		}));
E
Erich Gamma 已提交
391 392
	}

393
	private onEditorsChanged(): void {
E
Erich Gamma 已提交
394

395
		// Make sure that we layout properly in case we detect that the sidebar or panel is large enought to cause
E
Erich Gamma 已提交
396 397
		// 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.
398
		if (this.workbenchSize && (this.sidebarWidth || this.panelHeight)) {
E
Erich Gamma 已提交
399
			let visibleEditors = this.editorService.getVisibleEditors().length;
I
isidor 已提交
400
			const panelVertical = this.partService.getPanelPosition() === Position.RIGHT;
401
			if (visibleEditors > 1) {
402
				const sidebarOverflow = this.layoutEditorGroupsVertically && (this.workbenchSize.width - this.sidebarWidth < visibleEditors * MIN_EDITOR_PART_WIDTH);
I
isidor 已提交
403 404
				const panelOverflow = !this.layoutEditorGroupsVertically && !panelVertical && (this.workbenchSize.height - this.panelHeight < visibleEditors * MIN_EDITOR_PART_HEIGHT) ||
					panelVertical && this.layoutEditorGroupsVertically && (this.workbenchSize.width - this.panelWidth < visibleEditors * MIN_EDITOR_PART_WIDTH);
405 406 407 408

				if (sidebarOverflow || panelOverflow) {
					this.layout();
				}
E
Erich Gamma 已提交
409 410 411 412
			}
		}
	}

413 414
	private onGroupOrientationChanged(): void {
		const newLayoutEditorGroupsVertically = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
415

416 417
		const doLayout = this.layoutEditorGroupsVertically !== newLayoutEditorGroupsVertically;
		this.layoutEditorGroupsVertically = newLayoutEditorGroupsVertically;
418 419 420 421 422 423

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

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

427
		const isActivityBarHidden = !this.partService.isVisible(Parts.ACTIVITYBAR_PART);
428 429 430 431
		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 已提交
432
		const sidebarPosition = this.partService.getSideBarPosition();
433
		const panelPosition = this.partService.getPanelPosition();
E
Erich Gamma 已提交
434 435

		// Sidebar
I
isidor 已提交
436
		if (this.sidebarWidth === -1) {
437
			this.sidebarWidth = this.workbenchSize.width / 5;
E
Erich Gamma 已提交
438
		}
B
Benjamin Pasero 已提交
439

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

443
		const previousMaxPanelHeight = this.sidebarHeight - MIN_EDITOR_PART_HEIGHT;
444
		this.sidebarHeight = this.workbenchSize.height - this.statusbarHeight - this.titlebarHeight;
445
		let sidebarSize = new Dimension(this.sidebarWidth, this.sidebarHeight);
E
Erich Gamma 已提交
446 447

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

I
isidor 已提交
450 451
		// Panel part
		let panelHeight: number;
I
isidor 已提交
452
		let panelWidth: number;
I
isidor 已提交
453
		const editorCountForHeight = this.editorGroupService.getGroupOrientation() === 'horizontal' ? this.editorGroupService.getStacksModel().groups.length : 1;
454
		const editorCountForWidth = this.editorGroupService.getGroupOrientation() === 'vertical' ? this.editorGroupService.getStacksModel().groups.length : 1;
I
isidor 已提交
455
		const maxPanelHeight = Math.max(this.partLayoutInfo.panel.minHeight, sidebarSize.height - editorCountForHeight * MIN_EDITOR_PART_HEIGHT);
I
isidor 已提交
456
		const maxPanelWidth = Math.max(this.partLayoutInfo.panel.minWidth, this.workbenchSize.width - activityBarSize.width - MIN_SIDEBAR_PART_WIDTH - editorCountForWidth * MIN_EDITOR_PART_WIDTH);
I
isidor 已提交
457

I
isidor 已提交
458
		if (isPanelHidden) {
I
isidor 已提交
459
			panelHeight = 0;
I
isidor 已提交
460 461 462 463 464 465 466 467 468 469
			panelWidth = 0;
		} else if (panelPosition === Position.BOTTOM) {
			if (this.panelHeight === previousMaxPanelHeight) {
				panelHeight = maxPanelHeight;
			} else if (this.panelHeight > 0) {
				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;
470 471 472 473 474 475 476 477 478

			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 已提交
479
		} else {
I
isidor 已提交
480 481 482 483 484 485
			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;
			}
486 487 488 489 490 491 492 493 494

			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 已提交
495
		}
I
isidor 已提交
496
		const panelDimension = new Dimension(panelWidth, panelHeight);
I
isidor 已提交
497

E
Erich Gamma 已提交
498 499 500
		// Editor
		let editorSize = {
			width: 0,
I
isidor 已提交
501
			height: 0
E
Erich Gamma 已提交
502 503
		};

I
isidor 已提交
504 505
		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 已提交
506 507

		// Assert Sidebar and Editor Size to not overflow
508 509
		let editorMinWidth = this.partLayoutInfo.editor.minWidth;
		let editorMinHeight = this.partLayoutInfo.editor.minHeight;
E
Erich Gamma 已提交
510 511
		let visibleEditorCount = this.editorService.getVisibleEditors().length;
		if (visibleEditorCount > 1) {
512
			if (this.layoutEditorGroupsVertically) {
513 514 515 516
				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 已提交
517
		}
B
Benjamin Pasero 已提交
518

519 520 521 522 523
		if (editorSize.width < editorMinWidth) {
			let diff = editorMinWidth - editorSize.width;
			editorSize.width = editorMinWidth;
			if (panelPosition === Position.BOTTOM) {
				panelDimension.width = editorMinWidth;
524
			} else if (panelDimension.width >= diff && (!options || options.source !== Parts.PANEL_PART)) {
I
isidor 已提交
525 526
				const oldWidth = panelDimension.width;
				panelDimension.width = Math.max(MIN_PANEL_PART_WIDTH, panelDimension.width - diff);
I
isidor 已提交
527
				diff = diff - (oldWidth - panelDimension.width);
528 529
			}

I
isidor 已提交
530 531 532 533
			if (sidebarSize.width >= diff) {
				sidebarSize.width -= diff;
				sidebarSize.width = Math.max(MIN_SIDEBAR_PART_WIDTH, sidebarSize.width);
			}
534 535 536 537 538 539 540 541 542
		}

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

			panelDimension.height -= diff;
			panelDimension.height = Math.max(MIN_PANEL_PART_HEIGHT, panelDimension.height);
		}
E
Erich Gamma 已提交
543 544 545

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

I
isidor 已提交
549
		if (!isPanelHidden) {
550 551 552 553 554 555 556
			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 已提交
557 558
		}

E
Erich Gamma 已提交
559 560
		// Workbench
		this.workbenchContainer
561
			.position(0, 0, 0, 0, 'relative')
E
Erich Gamma 已提交
562 563
			.size(this.workbenchSize.width, this.workbenchSize.height);

B
Benjamin Pasero 已提交
564 565 566 567 568 569 570
		// 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 已提交
571 572
		}

573 574 575 576 577 578 579
		// Title Part
		if (isTitlebarHidden) {
			this.titlebar.getContainer().hide();
		} else {
			this.titlebar.getContainer().show();
		}

I
isidor 已提交
580
		// Editor Part and Panel part
E
Erich Gamma 已提交
581
		this.editor.getContainer().size(editorSize.width, editorSize.height);
B
Benjamin Pasero 已提交
582
		this.panel.getContainer().size(panelDimension.width, panelDimension.height);
E
Erich Gamma 已提交
583

I
isidor 已提交
584 585 586 587 588 589 590 591
		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 已提交
592
		} else {
I
isidor 已提交
593 594 595 596 597 598 599
			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 已提交
600 601 602
		}

		// Activity Bar Part
B
Benjamin Pasero 已提交
603
		this.activitybar.getContainer().size(null, activityBarSize.height);
E
Erich Gamma 已提交
604 605
		if (sidebarPosition === Position.LEFT) {
			this.activitybar.getContainer().getHTMLElement().style.right = '';
606
			this.activitybar.getContainer().position(this.titlebarHeight, null, 0, 0);
E
Erich Gamma 已提交
607 608
		} else {
			this.activitybar.getContainer().getHTMLElement().style.left = '';
609
			this.activitybar.getContainer().position(this.titlebarHeight, 0, 0, null);
E
Erich Gamma 已提交
610
		}
B
Benjamin Pasero 已提交
611 612 613 614 615
		if (isActivityBarHidden) {
			this.activitybar.getContainer().hide();
		} else {
			this.activitybar.getContainer().show();
		}
E
Erich Gamma 已提交
616 617 618

		// Sidebar Part
		this.sidebar.getContainer().size(sidebarSize.width, sidebarSize.height);
619
		const editorAndPanelWidth = editorSize.width + (panelPosition === Position.RIGHT ? panelWidth : 0);
E
Erich Gamma 已提交
620
		if (sidebarPosition === Position.LEFT) {
621
			this.sidebar.getContainer().position(this.titlebarHeight, editorAndPanelWidth, this.statusbarHeight, activityBarSize.width);
E
Erich Gamma 已提交
622
		} else {
623
			this.sidebar.getContainer().position(this.titlebarHeight, activityBarSize.width, this.statusbarHeight, editorAndPanelWidth);
E
Erich Gamma 已提交
624 625 626
		}

		// Statusbar Part
627
		this.statusbar.getContainer().position(this.workbenchSize.height - this.statusbarHeight);
628 629 630 631 632
		if (isStatusbarHidden) {
			this.statusbar.getContainer().hide();
		} else {
			this.statusbar.getContainer().show();
		}
E
Erich Gamma 已提交
633 634 635 636

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

I
isidor 已提交
637
		// Sashes
638 639 640
		this.sashXOne.layout();
		if (panelPosition === Position.BOTTOM) {
			this.sashXTwo.hide();
641 642
			this.sashY.layout();
			this.sashY.show();
643 644 645
		} else {
			this.sashY.hide();
			this.sashXTwo.layout();
646
			this.sashXTwo.show();
647
		}
E
Erich Gamma 已提交
648 649

		// Propagate to Part Layouts
650
		this.titlebar.layout(new Dimension(this.workbenchSize.width, this.titlebarHeight));
E
Erich Gamma 已提交
651 652
		this.editor.layout(new Dimension(editorSize.width, editorSize.height));
		this.sidebar.layout(sidebarSize);
B
Benjamin Pasero 已提交
653
		this.panel.layout(panelDimension);
654
		this.activitybar.layout(activityBarSize);
E
Erich Gamma 已提交
655 656

		// Propagate to Context View
B
Benjamin Pasero 已提交
657
		this.contextViewService.layout();
E
Erich Gamma 已提交
658 659 660
	}

	public getVerticalSashTop(sash: Sash): number {
661
		return this.titlebarHeight;
E
Erich Gamma 已提交
662 663 664 665
	}

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

668
			if (sidebarPosition === Position.LEFT) {
669
				return this.sidebarWidth + this.activitybarWidth;
670 671
			}

672
			return this.workbenchSize.width - this.sidebarWidth - this.activitybarWidth;
E
Erich Gamma 已提交
673 674
		}

675
		return this.workbenchSize.width - this.panelWidth - (sidebarPosition === Position.RIGHT ? this.sidebarWidth + this.activitybarWidth : 0);
E
Erich Gamma 已提交
676 677 678
	}

	public getVerticalSashHeight(sash: Sash): number {
I
isidor 已提交
679
		return this.sidebarHeight;
E
Erich Gamma 已提交
680 681
	}

I
isidor 已提交
682
	public getHorizontalSashTop(sash: Sash): number {
683 684
		// 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 已提交
685 686 687
	}

	public getHorizontalSashLeft(sash: Sash): number {
688 689 690 691 692
		if (this.partService.getSideBarPosition() === Position.RIGHT) {
			return 0;
		}

		return this.sidebarWidth + this.activitybarWidth;
I
isidor 已提交
693 694 695
	}

	public getHorizontalSashWidth(sash: Sash): number {
I
isidor 已提交
696
		return this.panelWidth;
I
isidor 已提交
697 698
	}

699 700 701 702
	public isPanelMaximized(): boolean {
		return this.panelMaximized;
	}

703
	// change part size along the main axis
B
Benjamin Pasero 已提交
704 705
	public resizePart(part: Parts, sizeChange: number): void {
		const visibleEditors = this.editorService.getVisibleEditors().length;
706 707 708
		const sizeChangePxWidth = this.workbenchSize.width * (sizeChange / 100);
		const sizeChangePxHeight = this.workbenchSize.height * (sizeChange / 100);

B
Benjamin Pasero 已提交
709 710
		let doLayout = false;

711 712
		switch (part) {
			case Parts.SIDEBAR_PART:
713
				this.sidebarWidth = this.sidebarWidth + sizeChangePxWidth; // Sidebar can not become smaller than MIN_PART_WIDTH
714 715 716 717 718 719 720 721

				if (this.layoutEditorGroupsVertically && (this.workbenchSize.width - this.sidebarWidth < visibleEditors * MIN_EDITOR_PART_WIDTH)) {
					this.sidebarWidth = (this.workbenchSize.width - visibleEditors * MIN_EDITOR_PART_WIDTH);
				}

				doLayout = true;
				break;
			case Parts.PANEL_PART:
722 723
				this.panelHeight = this.panelHeight + sizeChangePxHeight;
				this.panelWidth = this.panelWidth + sizeChangePxWidth;
724 725 726 727 728 729 730 731 732
				doLayout = true;
				break;
			case Parts.EDITOR_PART:
				// If we have one editor we can cheat and resize sidebar with the negative delta
				const visibleEditorCount = this.editorService.getVisibleEditors().length;

				if (visibleEditorCount === 1) {
					this.sidebarWidth = this.sidebarWidth - sizeChangePxWidth;
					doLayout = true;
B
Benjamin Pasero 已提交
733 734 735
				} else {
					const stacks = this.editorGroupService.getStacksModel();
					const activeGroup = stacks.positionOfGroup(stacks.activeGroup);
736 737 738 739 740 741 742

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

		if (doLayout) {
B
Benjamin Pasero 已提交
743
			this.layout();
744 745 746
		}
	}

E
Erich Gamma 已提交
747
	public dispose(): void {
M
Martin Aeschlimann 已提交
748 749 750
		if (this.toUnbind) {
			dispose(this.toUnbind);
			this.toUnbind = null;
E
Erich Gamma 已提交
751 752
		}
	}
B
Benjamin Pasero 已提交
753
}