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

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

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

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

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

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

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

114 115 116 117 118
		this.sashXOne = new Sash(this.workbenchContainer.getHTMLElement(), this, {
			baseSize: 5
		});

		this.sashXTwo = new Sash(this.workbenchContainer.getHTMLElement(), this, {
E
Erich Gamma 已提交
119 120
			baseSize: 5
		});
121

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

127 128 129
		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 已提交
130

131
		this.layoutEditorGroupsVertically = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
132

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

E
Erich Gamma 已提交
137 138 139
		this.registerSashListeners();
	}

140 141 142 143 144 145 146 147
	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 已提交
148 149 150 151 152 153 154 155
	private get activitybarWidth(): number {
		if (this.partService.isVisible(Parts.ACTIVITYBAR_PART)) {
			return this.partLayoutInfo.activitybar.width;
		}

		return 0;
	}

156 157 158 159 160 161 162 163 164 165
	private get panelHeight(): number {
		const panelPosition = this.partService.getPanelPosition();
		if (panelPosition === Position.RIGHT) {
			return this.sidebarHeight;
		}

		return this._panelHeight;
	}

	private set panelHeight(value: number) {
166
		this._panelHeight = Math.min(this.computeMaxPanelHeight(), Math.max(this.partLayoutInfo.panel.minHeight, value));
167 168 169 170 171 172 173 174 175 176 177 178
	}

	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) {
179 180 181 182 183 184 185 186 187 188
		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);
189 190 191 192 193 194 195 196 197 198 199
	}

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

		return 0;
	}

	private set sidebarWidth(value: number) {
200 201
		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 已提交
202
		this._sidebarWidth = Math.max(this.partLayoutInfo.sidebar.minWidth, Math.min(maxSidebarWidth, value));
203 204
	}

205 206
	@memoize
	private get partLayoutInfo(): PartLayoutInfo {
207 208 209 210 211 212 213 214 215 216 217
		return {
			titlebar: {
				height: TITLE_BAR_HEIGHT
			},
			activitybar: {
				width: ACTIVITY_BAR_WIDTH
			},
			sidebar: {
				minWidth: MIN_SIDEBAR_PART_WIDTH
			},
			panel: {
I
isidor 已提交
218 219
				minHeight: MIN_PANEL_PART_HEIGHT,
				minWidth: MIN_PANEL_PART_WIDTH
220 221 222 223 224 225 226 227 228 229 230
			},
			editor: {
				minWidth: MIN_EDITOR_PART_WIDTH,
				minHeight: MIN_EDITOR_PART_HEIGHT
			},
			statusbar: {
				height: STATUS_BAR_HEIGHT
			}
		};
	}

E
Erich Gamma 已提交
231 232
	private registerSashListeners(): void {
		let startX: number = 0;
I
isidor 已提交
233
		let startY: number = 0;
234
		let startXTwo: number = 0;
235 236 237
		let startSidebarWidth: number;
		let startPanelHeight: number;
		let startPanelWidth: number;
E
Erich Gamma 已提交
238

239 240
		this.toUnbind.push(this.sashXOne.addListener('start', (e: ISashEvent) => {
			startSidebarWidth = this.sidebarWidth;
E
Erich Gamma 已提交
241
			startX = e.startX;
242
		}));
E
Erich Gamma 已提交
243

244 245
		this.toUnbind.push(this.sashY.addListener('start', (e: ISashEvent) => {
			startPanelHeight = this.panelHeight;
I
isidor 已提交
246
			startY = e.startY;
247 248 249 250
		}));

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

254
		this.toUnbind.push(this.sashXOne.addListener('change', (e: ISashEvent) => {
E
Erich Gamma 已提交
255 256
			let doLayout = false;
			let sidebarPosition = this.partService.getSideBarPosition();
257
			let isSidebarVisible = this.partService.isVisible(Parts.SIDEBAR_PART);
258
			let newSashWidth = (sidebarPosition === Position.LEFT) ? startSidebarWidth + e.currentX - startX : startSidebarWidth - e.currentX + startX;
259
			let promise = TPromise.wrap<void>(null);
E
Erich Gamma 已提交
260 261

			// Sidebar visible
262
			if (isSidebarVisible) {
E
Erich Gamma 已提交
263 264

				// Automatically hide side bar when a certain threshold is met
265
				if (newSashWidth + HIDE_SIDEBAR_WIDTH_THRESHOLD < this.partLayoutInfo.sidebar.minWidth) {
266
					let dragCompensation = this.partLayoutInfo.sidebar.minWidth - HIDE_SIDEBAR_WIDTH_THRESHOLD;
267
					promise = this.partService.setSideBarHidden(true);
268
					startX = (sidebarPosition === Position.LEFT) ? Math.max(this.activitybarWidth, e.currentX - dragCompensation) : Math.min(e.currentX + dragCompensation, this.workbenchSize.width - this.activitybarWidth);
269
					this.sidebarWidth = startSidebarWidth; // when restoring sidebar, restore to the sidebar width we started from
E
Erich Gamma 已提交
270 271 272 273
				}

				// Otherwise size the sidebar accordingly
				else {
274 275
					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 已提交
276 277 278 279 280
				}
			}

			// Sidebar hidden
			else {
281 282
				if ((sidebarPosition === Position.LEFT && e.currentX - startX >= this.partLayoutInfo.sidebar.minWidth) ||
					(sidebarPosition === Position.RIGHT && startX - e.currentX >= this.partLayoutInfo.sidebar.minWidth)) {
283
					startSidebarWidth = this.partLayoutInfo.sidebar.minWidth - (sidebarPosition === Position.LEFT ? e.currentX - startX : startX - e.currentX);
284
					this.sidebarWidth = this.partLayoutInfo.sidebar.minWidth;
285
					promise = this.partService.setSideBarHidden(false);
E
Erich Gamma 已提交
286 287 288 289
				}
			}

			if (doLayout) {
290
				promise.done(() => this.layout({ source: Parts.SIDEBAR_PART }), errors.onUnexpectedError);
E
Erich Gamma 已提交
291
			}
292
		}));
E
Erich Gamma 已提交
293

294
		this.toUnbind.push(this.sashY.addListener('change', (e: ISashEvent) => {
I
isidor 已提交
295
			let doLayout = false;
296
			let isPanelVisible = this.partService.isVisible(Parts.PANEL_PART);
297
			let newSashHeight = startPanelHeight - (e.currentY - startY);
298
			let promise = TPromise.wrap<void>(null);
I
isidor 已提交
299 300

			// Panel visible
301
			if (isPanelVisible) {
302

303
				// Automatically hide panel when a certain threshold is met
304
				if (newSashHeight + HIDE_PANEL_HEIGHT_THRESHOLD < this.partLayoutInfo.panel.minHeight) {
305
					let dragCompensation = this.partLayoutInfo.panel.minHeight - HIDE_PANEL_HEIGHT_THRESHOLD;
306
					promise = this.partService.setPanelHidden(true);
307
					startY = Math.min(this.sidebarHeight - this.statusbarHeight - this.titlebarHeight, e.currentY + dragCompensation);
308
					this.panelHeight = startPanelHeight; // when restoring panel, restore to the panel height we started from
309 310 311 312
				}

				// Otherwise size the panel accordingly
				else {
313 314
					this.panelHeight = Math.max(this.partLayoutInfo.panel.minHeight, newSashHeight); // Panel can not become smaller than MIN_PART_HEIGHT
					doLayout = newSashHeight >= this.partLayoutInfo.panel.minHeight;
315 316
				}
			}
I
isidor 已提交
317

318 319
			// Panel hidden
			else {
320
				if (startY - e.currentY >= this.partLayoutInfo.panel.minHeight) {
321
					startPanelHeight = 0;
322
					this.panelHeight = this.partLayoutInfo.panel.minHeight;
323
					promise = this.partService.setPanelHidden(false);
324
				}
I
isidor 已提交
325
			}
326

I
isidor 已提交
327
			if (doLayout) {
328
				promise.done(() => this.layout({ source: Parts.PANEL_PART }), errors.onUnexpectedError);
I
isidor 已提交
329
			}
330
		}));
I
isidor 已提交
331

332
		this.toUnbind.push(this.sashXTwo.addListener('change', (e: ISashEvent) => {
333 334 335
			let doLayout = false;
			let isPanelVisible = this.partService.isVisible(Parts.PANEL_PART);
			let newSashWidth = startPanelWidth - (e.currentX - startXTwo);
336
			let promise = TPromise.wrap<void>(null);
337 338 339 340 341 342

			// Panel visible
			if (isPanelVisible) {

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

370 371 372 373 374
		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 已提交
375
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
376 377 378 379 380
		}));

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

382
		this.toUnbind.push(this.sashY.addListener('reset', () => {
I
isidor 已提交
383
			this.panelHeight = this.sidebarHeight * DEFAULT_PANEL_SIZE_COEFFICIENT;
M
Maxime Quandalle 已提交
384
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
385 386
			this.layout();
		}));
M
Maxime Quandalle 已提交
387

388
		this.toUnbind.push(this.sashXOne.addListener('reset', () => {
M
Maxime Quandalle 已提交
389 390
			let activeViewlet = this.viewletService.getActiveViewlet();
			let optimalWidth = activeViewlet && activeViewlet.getOptimalWidth();
391
			this.sidebarWidth = optimalWidth || 0;
392
			this.storageService.store(WorkbenchLayout.sashXOneWidthSettingsKey, this.sidebarWidth, StorageScope.GLOBAL);
393
			this.partService.setSideBarHidden(false).done(() => this.layout(), errors.onUnexpectedError);
394 395 396
		}));

		this.toUnbind.push(this.sashXTwo.addListener('reset', () => {
397
			this.panelWidth = (this.workbenchSize.width - this.sidebarWidth - this.activitybarWidth) * DEFAULT_PANEL_SIZE_COEFFICIENT;
398 399 400
			this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
			this.layout();
		}));
E
Erich Gamma 已提交
401 402
	}

403
	private onEditorsChanged(): void {
E
Erich Gamma 已提交
404

405
		// Make sure that we layout properly in case we detect that the sidebar or panel is large enought to cause
E
Erich Gamma 已提交
406 407
		// 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.
408
		if (this.workbenchSize && (this.sidebarWidth || this.panelHeight)) {
E
Erich Gamma 已提交
409
			let visibleEditors = this.editorService.getVisibleEditors().length;
I
isidor 已提交
410
			const panelVertical = this.partService.getPanelPosition() === Position.RIGHT;
411
			if (visibleEditors > 1) {
412 413
				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 已提交
414
					panelVertical && this.layoutEditorGroupsVertically && (this.workbenchSize.width - this.panelWidth - this.sidebarWidth < visibleEditors * this.partLayoutInfo.editor.minWidth);
415 416 417 418

				if (sidebarOverflow || panelOverflow) {
					this.layout();
				}
E
Erich Gamma 已提交
419 420 421 422
			}
		}
	}

423 424
	private onGroupOrientationChanged(): void {
		const newLayoutEditorGroupsVertically = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
425

426 427
		const doLayout = this.layoutEditorGroupsVertically !== newLayoutEditorGroupsVertically;
		this.layoutEditorGroupsVertically = newLayoutEditorGroupsVertically;
428 429 430 431 432 433

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

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

437
		const isActivityBarHidden = !this.partService.isVisible(Parts.ACTIVITYBAR_PART);
438 439 440 441
		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 已提交
442
		const sidebarPosition = this.partService.getSideBarPosition();
443
		const panelPosition = this.partService.getPanelPosition();
E
Erich Gamma 已提交
444 445

		// Sidebar
I
isidor 已提交
446
		if (this.sidebarWidth === -1) {
447
			this.sidebarWidth = this.workbenchSize.width / 5;
E
Erich Gamma 已提交
448
		}
B
Benjamin Pasero 已提交
449

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

453
		const previousMaxPanelHeight = this.sidebarHeight - this.partLayoutInfo.editor.minHeight;
454
		this.sidebarHeight = this.workbenchSize.height - this.statusbarHeight - this.titlebarHeight;
455
		let sidebarSize = new Dimension(this.sidebarWidth, this.sidebarHeight);
E
Erich Gamma 已提交
456 457

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

I
isidor 已提交
460 461
		// Panel part
		let panelHeight: number;
I
isidor 已提交
462
		let panelWidth: number;
463 464
		const maxPanelHeight = this.computeMaxPanelHeight();
		const maxPanelWidth = this.computeMaxPanelWidth();
I
isidor 已提交
465

I
isidor 已提交
466
		if (isPanelHidden) {
I
isidor 已提交
467
			panelHeight = 0;
I
isidor 已提交
468 469 470 471 472 473 474 475 476 477
			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;
478 479 480 481 482 483 484 485 486

			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 已提交
487
		} else {
I
isidor 已提交
488 489 490 491 492 493
			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;
			}
494 495 496 497 498 499 500 501 502

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

E
Erich Gamma 已提交
506 507 508
		// Editor
		let editorSize = {
			width: 0,
I
isidor 已提交
509
			height: 0
E
Erich Gamma 已提交
510 511
		};

I
isidor 已提交
512 513
		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 已提交
514 515

		// Assert Sidebar and Editor Size to not overflow
516 517
		let editorMinWidth = this.partLayoutInfo.editor.minWidth;
		let editorMinHeight = this.partLayoutInfo.editor.minHeight;
E
Erich Gamma 已提交
518 519
		let visibleEditorCount = this.editorService.getVisibleEditors().length;
		if (visibleEditorCount > 1) {
520
			if (this.layoutEditorGroupsVertically) {
521 522 523 524
				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 已提交
525
		}
B
Benjamin Pasero 已提交
526

527 528 529 530 531
		if (editorSize.width < editorMinWidth) {
			let diff = editorMinWidth - editorSize.width;
			editorSize.width = editorMinWidth;
			if (panelPosition === Position.BOTTOM) {
				panelDimension.width = editorMinWidth;
532
			} else if (panelDimension.width >= diff && (!options || options.source !== Parts.PANEL_PART)) {
I
isidor 已提交
533
				const oldWidth = panelDimension.width;
534
				panelDimension.width = Math.max(this.partLayoutInfo.panel.minWidth, panelDimension.width - diff);
I
isidor 已提交
535
				diff = diff - (oldWidth - panelDimension.width);
536 537
			}

I
isidor 已提交
538 539
			if (sidebarSize.width >= diff) {
				sidebarSize.width -= diff;
540
				sidebarSize.width = Math.max(this.partLayoutInfo.sidebar.minWidth, sidebarSize.width);
I
isidor 已提交
541
			}
542 543 544 545 546 547 548
		}

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

			panelDimension.height -= diff;
549
			panelDimension.height = Math.max(this.partLayoutInfo.panel.minHeight, panelDimension.height);
550
		}
E
Erich Gamma 已提交
551 552 553

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

I
isidor 已提交
557
		if (!isPanelHidden) {
558 559 560 561 562 563 564
			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 已提交
565 566
		}

E
Erich Gamma 已提交
567 568
		// Workbench
		this.workbenchContainer
569
			.position(0, 0, 0, 0, 'relative')
E
Erich Gamma 已提交
570 571
			.size(this.workbenchSize.width, this.workbenchSize.height);

B
Benjamin Pasero 已提交
572 573 574 575 576 577 578
		// 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 已提交
579 580
		}

581 582 583 584 585 586 587
		// Title Part
		if (isTitlebarHidden) {
			this.titlebar.getContainer().hide();
		} else {
			this.titlebar.getContainer().show();
		}

I
isidor 已提交
588
		// Editor Part and Panel part
E
Erich Gamma 已提交
589
		this.editor.getContainer().size(editorSize.width, editorSize.height);
B
Benjamin Pasero 已提交
590
		this.panel.getContainer().size(panelDimension.width, panelDimension.height);
E
Erich Gamma 已提交
591

I
isidor 已提交
592 593 594 595 596 597 598 599
		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 已提交
600
		} else {
I
isidor 已提交
601 602 603 604 605 606 607
			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 已提交
608 609 610
		}

		// Activity Bar Part
B
Benjamin Pasero 已提交
611
		this.activitybar.getContainer().size(null, activityBarSize.height);
E
Erich Gamma 已提交
612 613
		if (sidebarPosition === Position.LEFT) {
			this.activitybar.getContainer().getHTMLElement().style.right = '';
614
			this.activitybar.getContainer().position(this.titlebarHeight, null, 0, 0);
E
Erich Gamma 已提交
615 616
		} else {
			this.activitybar.getContainer().getHTMLElement().style.left = '';
617
			this.activitybar.getContainer().position(this.titlebarHeight, 0, 0, null);
E
Erich Gamma 已提交
618
		}
B
Benjamin Pasero 已提交
619 620 621 622 623
		if (isActivityBarHidden) {
			this.activitybar.getContainer().hide();
		} else {
			this.activitybar.getContainer().show();
		}
E
Erich Gamma 已提交
624 625 626

		// Sidebar Part
		this.sidebar.getContainer().size(sidebarSize.width, sidebarSize.height);
627
		const editorAndPanelWidth = editorSize.width + (panelPosition === Position.RIGHT ? panelWidth : 0);
E
Erich Gamma 已提交
628
		if (sidebarPosition === Position.LEFT) {
629
			this.sidebar.getContainer().position(this.titlebarHeight, editorAndPanelWidth, this.statusbarHeight, activityBarSize.width);
E
Erich Gamma 已提交
630
		} else {
631
			this.sidebar.getContainer().position(this.titlebarHeight, activityBarSize.width, this.statusbarHeight, editorAndPanelWidth);
E
Erich Gamma 已提交
632 633 634
		}

		// Statusbar Part
635
		this.statusbar.getContainer().position(this.workbenchSize.height - this.statusbarHeight);
636 637 638 639 640
		if (isStatusbarHidden) {
			this.statusbar.getContainer().hide();
		} else {
			this.statusbar.getContainer().show();
		}
E
Erich Gamma 已提交
641 642 643 644

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

I
isidor 已提交
645
		// Sashes
646 647 648
		this.sashXOne.layout();
		if (panelPosition === Position.BOTTOM) {
			this.sashXTwo.hide();
649 650
			this.sashY.layout();
			this.sashY.show();
651 652 653
		} else {
			this.sashY.hide();
			this.sashXTwo.layout();
654
			this.sashXTwo.show();
655
		}
E
Erich Gamma 已提交
656 657

		// Propagate to Part Layouts
658
		this.titlebar.layout(new Dimension(this.workbenchSize.width, this.titlebarHeight));
E
Erich Gamma 已提交
659 660
		this.editor.layout(new Dimension(editorSize.width, editorSize.height));
		this.sidebar.layout(sidebarSize);
B
Benjamin Pasero 已提交
661
		this.panel.layout(panelDimension);
662
		this.activitybar.layout(activityBarSize);
E
Erich Gamma 已提交
663 664

		// Propagate to Context View
B
Benjamin Pasero 已提交
665
		this.contextViewService.layout();
E
Erich Gamma 已提交
666 667 668
	}

	public getVerticalSashTop(sash: Sash): number {
669
		return this.titlebarHeight;
E
Erich Gamma 已提交
670 671 672 673
	}

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

676
			if (sidebarPosition === Position.LEFT) {
677
				return this.sidebarWidth + this.activitybarWidth;
678 679
			}

680
			return this.workbenchSize.width - this.sidebarWidth - this.activitybarWidth;
E
Erich Gamma 已提交
681 682
		}

683
		return this.workbenchSize.width - this.panelWidth - (sidebarPosition === Position.RIGHT ? this.sidebarWidth + this.activitybarWidth : 0);
E
Erich Gamma 已提交
684 685 686
	}

	public getVerticalSashHeight(sash: Sash): number {
I
isidor 已提交
687
		return this.sidebarHeight;
E
Erich Gamma 已提交
688 689
	}

I
isidor 已提交
690
	public getHorizontalSashTop(sash: Sash): number {
691 692
		// 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 已提交
693 694 695
	}

	public getHorizontalSashLeft(sash: Sash): number {
696 697 698 699 700
		if (this.partService.getSideBarPosition() === Position.RIGHT) {
			return 0;
		}

		return this.sidebarWidth + this.activitybarWidth;
I
isidor 已提交
701 702 703
	}

	public getHorizontalSashWidth(sash: Sash): number {
I
isidor 已提交
704
		return this.panelWidth;
I
isidor 已提交
705 706
	}

707 708 709 710
	public isPanelMaximized(): boolean {
		return this.panelMaximized;
	}

711
	// change part size along the main axis
B
Benjamin Pasero 已提交
712
	public resizePart(part: Parts, sizeChange: number): void {
713
		const panelPosition = this.partService.getPanelPosition();
714 715 716
		const sizeChangePxWidth = this.workbenchSize.width * (sizeChange / 100);
		const sizeChangePxHeight = this.workbenchSize.height * (sizeChange / 100);

B
Benjamin Pasero 已提交
717 718
		let doLayout = false;

719 720
		switch (part) {
			case Parts.SIDEBAR_PART:
721
				this.sidebarWidth = this.sidebarWidth + sizeChangePxWidth; // Sidebar can not become smaller than MIN_PART_WIDTH
722

I
isidor 已提交
723 724
				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);
725 726 727 728 729
				}

				doLayout = true;
				break;
			case Parts.PANEL_PART:
730 731 732 733 734 735
				if (panelPosition === Position.BOTTOM) {
					this.panelHeight = this.panelHeight + sizeChangePxHeight;
				} else if (panelPosition === Position.RIGHT) {
					this.panelWidth = this.panelWidth + sizeChangePxWidth;
				}

736 737 738 739
				doLayout = true;
				break;
			case Parts.EDITOR_PART:
				// If we have one editor we can cheat and resize sidebar with the negative delta
740
				// If the sidebar is not visible and panel is, resize panel main axis with negative Delta
I
isidor 已提交
741
				if (this.editorCountForWidth === 1) {
C
cleidigh 已提交
742
					if (this.partService.isVisible(Parts.SIDEBAR_PART)) {
743 744
						this.sidebarWidth = this.sidebarWidth - sizeChangePxWidth;
						doLayout = true;
C
cleidigh 已提交
745
					} else if (this.partService.isVisible(Parts.PANEL_PART)) {
746 747 748 749 750 751 752 753
						if (panelPosition === Position.BOTTOM) {
							this.panelHeight = this.panelHeight - sizeChangePxHeight;
						} else if (panelPosition === Position.RIGHT) {
							this.panelWidth = this.panelWidth - sizeChangePxWidth;
						}
						doLayout = true;
					}

B
Benjamin Pasero 已提交
754 755 756
				} else {
					const stacks = this.editorGroupService.getStacksModel();
					const activeGroup = stacks.positionOfGroup(stacks.activeGroup);
757 758 759 760 761 762 763

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

		if (doLayout) {
B
Benjamin Pasero 已提交
764
			this.layout();
765 766 767
		}
	}

E
Erich Gamma 已提交
768
	public dispose(): void {
M
Martin Aeschlimann 已提交
769 770 771
		if (this.toUnbind) {
			dispose(this.toUnbind);
			this.toUnbind = null;
E
Erich Gamma 已提交
772 773
		}
	}
B
Benjamin Pasero 已提交
774
}