layout.ts 30.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'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 54 55
	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 已提交
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[];
E
Erich Gamma 已提交
67
	private workbenchSize: Dimension;
68 69
	private sashXOne: Sash;
	private sashXTwo: Sash;
I
isidor 已提交
70
	private sashY: Sash;
71
	private _sidebarWidth: number;
I
isidor 已提交
72
	private sidebarHeight: number;
73 74
	private titlebarHeight: number;
	private statusbarHeight: number;
75 76
	private panelSizeBeforeMaximized: number;
	private panelMaximized: boolean;
77 78
	private _panelHeight: number;
	private _panelWidth: number;
79
	private layoutEditorGroupsVertically: boolean;
E
Erich Gamma 已提交
80

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

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

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

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

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

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

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

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

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

		return 0;
	}

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

		return this._panelHeight;
	}

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

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

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

		return 0;
	}

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

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

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

I
isidor 已提交
240
		this.toUnbind.push(this.sashXOne.onDidStart((e: ISashEvent) => {
241
			startSidebarWidth = this.sidebarWidth;
E
Erich Gamma 已提交
242
			startX = e.startX;
243
		}));
E
Erich Gamma 已提交
244

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

I
isidor 已提交
250
		this.toUnbind.push(this.sashXTwo.onDidStart((e: ISashEvent) => {
251
			startPanelWidth = this.panelWidth;
252
			startXTwo = e.startX;
253
		}));
I
isidor 已提交
254

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

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

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

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

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

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

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

			// Panel visible
302
			if (isPanelVisible) {
303

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

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

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

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

I
isidor 已提交
333
		this.toUnbind.push(this.sashXTwo.onDidChange((e: ISashEvent) => {
334 335 336
			let doLayout = false;
			let isPanelVisible = this.partService.isVisible(Parts.PANEL_PART);
			let newSashWidth = startPanelWidth - (e.currentX - startXTwo);
337
			let promise = TPromise.wrap<void>(null);
338 339 340 341 342 343

			// Panel visible
			if (isPanelVisible) {

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

I
isidor 已提交
371
		this.toUnbind.push(this.sashXOne.onDidEnd(() => {
372 373 374
			this.storageService.store(WorkbenchLayout.sashXOneWidthSettingsKey, this.sidebarWidth, StorageScope.GLOBAL);
		}));

I
isidor 已提交
375
		this.toUnbind.push(this.sashY.onDidEnd(() => {
I
isidor 已提交
376
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
377 378
		}));

I
isidor 已提交
379
		this.toUnbind.push(this.sashXTwo.onDidEnd(() => {
380 381
			this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
		}));
M
Maxime Quandalle 已提交
382

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

			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 已提交
504
		}
505
		this.storageService.store(WorkbenchLayout.panelSizeBeforeMaximizedKey, this.panelSizeBeforeMaximized, StorageScope.GLOBAL);
I
isidor 已提交
506
		const panelDimension = new Dimension(panelWidth, panelHeight);
I
isidor 已提交
507

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

I
isidor 已提交
514 515
		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 已提交
516 517

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

I
isidor 已提交
685
		return this.workbenchSize.width - (this.partService.isVisible(Parts.PANEL_PART) ? this.panelWidth : 0) - (sidebarPosition === Position.RIGHT ? this.sidebarWidth + this.activitybarWidth : 0);
E
Erich Gamma 已提交
686 687 688
	}

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

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

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

		return this.sidebarWidth + this.activitybarWidth;
I
isidor 已提交
703 704 705
	}

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

709 710 711 712
	public isPanelMaximized(): boolean {
		return this.panelMaximized;
	}

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

B
Benjamin Pasero 已提交
719 720
		let doLayout = false;

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

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

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

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

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

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

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

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