layout.ts 25.1 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';
I
isidor 已提交
14
import { IPartService, Position, 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 28
const MIN_PANEL_PART_WIDTH = 150;
const DEFAULT_PANEL_SIZE_COEFFICIENT = 0.4;
E
Erich Gamma 已提交
29
const HIDE_SIDEBAR_WIDTH_THRESHOLD = 50;
I
isidor 已提交
30
const HIDE_PANEL_HEIGHT_THRESHOLD = 50;
31 32 33
const TITLE_BAR_HEIGHT = 22;
const STATUS_BAR_HEIGHT = 22;
const ACTIVITY_BAR_WIDTH = 50;
E
Erich Gamma 已提交
34

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

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

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

	private parent: Builder;
	private workbenchContainer: Builder;
55
	private titlebar: Part;
E
Erich Gamma 已提交
56 57 58
	private activitybar: Part;
	private editor: Part;
	private sidebar: Part;
B
Benjamin Pasero 已提交
59
	private panel: Part;
E
Erich Gamma 已提交
60 61
	private statusbar: Part;
	private quickopen: QuickOpenController;
M
Martin Aeschlimann 已提交
62
	private toUnbind: IDisposable[];
63
	private partLayoutInfo: PartLayoutInfo;
E
Erich Gamma 已提交
64
	private workbenchSize: Dimension;
65 66
	private sashXOne: Sash;
	private sashXTwo: Sash;
I
isidor 已提交
67
	private sashY: Sash;
E
Erich Gamma 已提交
68
	private sidebarWidth: number;
I
isidor 已提交
69
	private sidebarHeight: number;
70 71 72
	private titlebarHeight: number;
	private activitybarWidth: number;
	private statusbarHeight: number;
I
isidor 已提交
73
	private panelHeight: number;
74
	private panelHeightBeforeMaximized: number;
I
isidor 已提交
75
	private panelWidth: number;
76
	private layoutEditorGroupsVertically: boolean;
E
Erich Gamma 已提交
77

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

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

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

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

125
		this.sidebarWidth = this.storageService.getInteger(WorkbenchLayout.sashXOneWidthSettingsKey, StorageScope.GLOBAL, -1);
I
isidor 已提交
126
		this.panelHeight = this.storageService.getInteger(WorkbenchLayout.sashYHeightSettingsKey, StorageScope.GLOBAL, 0);
127
		this.panelWidth = this.storageService.getInteger(WorkbenchLayout.sashXTwoWidthSettingsKey, StorageScope.GLOBAL, 0);
E
Erich Gamma 已提交
128

129
		this.layoutEditorGroupsVertically = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
130

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

E
Erich Gamma 已提交
135 136 137
		this.registerSashListeners();
	}

138 139 140 141 142 143 144 145 146 147 148 149
	private getPartLayoutInfo(): PartLayoutInfo {
		return {
			titlebar: {
				height: TITLE_BAR_HEIGHT
			},
			activitybar: {
				width: ACTIVITY_BAR_WIDTH
			},
			sidebar: {
				minWidth: MIN_SIDEBAR_PART_WIDTH
			},
			panel: {
I
isidor 已提交
150 151
				minHeight: MIN_PANEL_PART_HEIGHT,
				minWidth: MIN_PANEL_PART_WIDTH
152 153 154 155 156 157 158 159 160 161 162
			},
			editor: {
				minWidth: MIN_EDITOR_PART_WIDTH,
				minHeight: MIN_EDITOR_PART_HEIGHT
			},
			statusbar: {
				height: STATUS_BAR_HEIGHT
			}
		};
	}

E
Erich Gamma 已提交
163 164
	private registerSashListeners(): void {
		let startX: number = 0;
I
isidor 已提交
165
		let startY: number = 0;
166 167 168 169
		let startX2: number = 0;
		let startSidebarWidth: number;
		let startPanelHeight: number;
		let startPanelWidth: number;
E
Erich Gamma 已提交
170

171 172
		this.toUnbind.push(this.sashXOne.addListener('start', (e: ISashEvent) => {
			startSidebarWidth = this.sidebarWidth;
E
Erich Gamma 已提交
173
			startX = e.startX;
174
		}));
E
Erich Gamma 已提交
175

176 177
		this.toUnbind.push(this.sashY.addListener('start', (e: ISashEvent) => {
			startPanelHeight = this.panelHeight;
I
isidor 已提交
178
			startY = e.startY;
179 180 181 182 183 184
		}));

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

186
		this.toUnbind.push(this.sashXOne.addListener('change', (e: ISashEvent) => {
E
Erich Gamma 已提交
187 188
			let doLayout = false;
			let sidebarPosition = this.partService.getSideBarPosition();
189
			let isSidebarVisible = this.partService.isVisible(Parts.SIDEBAR_PART);
190
			let newSashWidth = (sidebarPosition === Position.LEFT) ? startSidebarWidth + e.currentX - startX : startSidebarWidth - e.currentX + startX;
191
			let promise = TPromise.as<void>(null);
E
Erich Gamma 已提交
192 193

			// Sidebar visible
194
			if (isSidebarVisible) {
E
Erich Gamma 已提交
195 196

				// Automatically hide side bar when a certain threshold is met
197 198
				if (newSashWidth + HIDE_SIDEBAR_WIDTH_THRESHOLD < this.partLayoutInfo.sidebar.minWidth) {
					let dragCompensation = MIN_SIDEBAR_PART_WIDTH - HIDE_SIDEBAR_WIDTH_THRESHOLD;
199
					promise = this.partService.setSideBarHidden(true);
200
					startX = (sidebarPosition === Position.LEFT) ? Math.max(this.activitybarWidth, e.currentX - dragCompensation) : Math.min(e.currentX + dragCompensation, this.workbenchSize.width - this.activitybarWidth);
201
					this.sidebarWidth = startSidebarWidth; // when restoring sidebar, restore to the sidebar width we started from
E
Erich Gamma 已提交
202 203 204 205
				}

				// Otherwise size the sidebar accordingly
				else {
206 207
					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 已提交
208 209 210 211 212
				}
			}

			// Sidebar hidden
			else {
213 214
				if ((sidebarPosition === Position.LEFT && e.currentX - startX >= this.partLayoutInfo.sidebar.minWidth) ||
					(sidebarPosition === Position.RIGHT && startX - e.currentX >= this.partLayoutInfo.sidebar.minWidth)) {
215
					startSidebarWidth = this.partLayoutInfo.sidebar.minWidth - (sidebarPosition === Position.LEFT ? e.currentX - startX : startX - e.currentX);
216
					this.sidebarWidth = this.partLayoutInfo.sidebar.minWidth;
217
					promise = this.partService.setSideBarHidden(false);
E
Erich Gamma 已提交
218 219 220 221
				}
			}

			if (doLayout) {
222
				promise.done(() => this.layout(), errors.onUnexpectedError);
E
Erich Gamma 已提交
223
			}
224
		}));
E
Erich Gamma 已提交
225

226
		this.toUnbind.push(this.sashY.addListener('change', (e: ISashEvent) => {
I
isidor 已提交
227
			let doLayout = false;
228
			let isPanelVisible = this.partService.isVisible(Parts.PANEL_PART);
229
			let newSashHeight = startPanelHeight - (e.currentY - startY);
230
			let promise = TPromise.as<void>(null);
I
isidor 已提交
231 232

			// Panel visible
233
			if (isPanelVisible) {
234

235
				// Automatically hide panel when a certain threshold is met
236 237
				if (newSashHeight + HIDE_PANEL_HEIGHT_THRESHOLD < this.partLayoutInfo.panel.minHeight) {
					let dragCompensation = MIN_PANEL_PART_HEIGHT - HIDE_PANEL_HEIGHT_THRESHOLD;
238
					promise = this.partService.setPanelHidden(true);
239
					startY = Math.min(this.sidebarHeight - this.statusbarHeight - this.titlebarHeight, e.currentY + dragCompensation);
240
					this.panelHeight = startPanelHeight; // when restoring panel, restore to the panel height we started from
241 242 243 244
				}

				// Otherwise size the panel accordingly
				else {
245 246
					this.panelHeight = Math.max(this.partLayoutInfo.panel.minHeight, newSashHeight); // Panel can not become smaller than MIN_PART_HEIGHT
					doLayout = newSashHeight >= this.partLayoutInfo.panel.minHeight;
247 248
				}
			}
I
isidor 已提交
249

250 251
			// Panel hidden
			else {
252
				if (startY - e.currentY >= this.partLayoutInfo.panel.minHeight) {
253
					startPanelHeight = 0;
254
					this.panelHeight = this.partLayoutInfo.panel.minHeight;
255
					promise = this.partService.setPanelHidden(false);
256
				}
I
isidor 已提交
257
			}
258

I
isidor 已提交
259
			if (doLayout) {
260
				promise.done(() => this.layout(), errors.onUnexpectedError);
I
isidor 已提交
261
			}
262
		}));
I
isidor 已提交
263

264 265 266 267
		this.toUnbind.push(this.sashXTwo.addListener('change', (e: ISashEvent) => {
			console.log('change');
			// TODO@Isidor
		}));
M
Maxime Quandalle 已提交
268

269 270 271 272 273
		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 已提交
274
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
275 276 277 278 279
		}));

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

281
		this.toUnbind.push(this.sashY.addListener('reset', () => {
I
isidor 已提交
282
			this.panelHeight = this.sidebarHeight * DEFAULT_PANEL_SIZE_COEFFICIENT;
M
Maxime Quandalle 已提交
283
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
284 285
			this.layout();
		}));
M
Maxime Quandalle 已提交
286

287
		this.toUnbind.push(this.sashXOne.addListener('reset', () => {
M
Maxime Quandalle 已提交
288 289
			let activeViewlet = this.viewletService.getActiveViewlet();
			let optimalWidth = activeViewlet && activeViewlet.getOptimalWidth();
290
			this.sidebarWidth = Math.max(MIN_SIDEBAR_PART_WIDTH, optimalWidth || 0);
291
			this.storageService.store(WorkbenchLayout.sashXOneWidthSettingsKey, this.sidebarWidth, StorageScope.GLOBAL);
292
			this.partService.setSideBarHidden(false).done(() => this.layout(), errors.onUnexpectedError);
293 294 295 296 297 298 299
		}));

		this.toUnbind.push(this.sashXTwo.addListener('reset', () => {
			this.panelWidth = 0; // TODO@ISIDOR
			this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
			this.layout();
		}));
E
Erich Gamma 已提交
300 301
	}

302
	private onEditorsChanged(): void {
E
Erich Gamma 已提交
303

304
		// Make sure that we layout properly in case we detect that the sidebar or panel is large enought to cause
E
Erich Gamma 已提交
305 306
		// 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.
307
		if (this.workbenchSize && (this.sidebarWidth || this.panelHeight)) {
E
Erich Gamma 已提交
308
			let visibleEditors = this.editorService.getVisibleEditors().length;
309
			if (visibleEditors > 1) {
310 311
				const sidebarOverflow = this.layoutEditorGroupsVertically && (this.workbenchSize.width - this.sidebarWidth < visibleEditors * MIN_EDITOR_PART_WIDTH);
				const panelOverflow = !this.layoutEditorGroupsVertically && (this.workbenchSize.height - this.panelHeight < visibleEditors * MIN_EDITOR_PART_HEIGHT);
312 313 314 315

				if (sidebarOverflow || panelOverflow) {
					this.layout();
				}
E
Erich Gamma 已提交
316 317 318 319
			}
		}
	}

320 321
	private onGroupOrientationChanged(): void {
		const newLayoutEditorGroupsVertically = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
322

323 324
		const doLayout = this.layoutEditorGroupsVertically !== newLayoutEditorGroupsVertically;
		this.layoutEditorGroupsVertically = newLayoutEditorGroupsVertically;
325 326 327 328 329 330

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

I
isidor 已提交
331
	public layout(): void {
B
Benjamin Pasero 已提交
332
		this.workbenchSize = this.parent.getClientArea();
E
Erich Gamma 已提交
333

334
		const isActivityBarHidden = !this.partService.isVisible(Parts.ACTIVITYBAR_PART);
335 336 337 338
		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 已提交
339
		const sidebarPosition = this.partService.getSideBarPosition();
340
		const panelPosition = this.partService.getPanelPosition();
E
Erich Gamma 已提交
341 342 343 344 345 346

		// Sidebar
		let sidebarWidth: number;
		if (isSidebarHidden) {
			sidebarWidth = 0;
		} else if (this.sidebarWidth !== -1) {
347
			sidebarWidth = Math.max(this.partLayoutInfo.sidebar.minWidth, this.sidebarWidth);
E
Erich Gamma 已提交
348 349 350 351
		} else {
			sidebarWidth = this.workbenchSize.width / 5;
			this.sidebarWidth = sidebarWidth;
		}
B
Benjamin Pasero 已提交
352

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

356
		const previousMaxPanelHeight = this.sidebarHeight - MIN_EDITOR_PART_HEIGHT;
357
		this.sidebarHeight = this.workbenchSize.height - this.statusbarHeight - this.titlebarHeight;
I
isidor 已提交
358
		let sidebarSize = new Dimension(sidebarWidth, this.sidebarHeight);
E
Erich Gamma 已提交
359 360

		// Activity Bar
361
		this.activitybarWidth = isActivityBarHidden ? 0 : this.partLayoutInfo.activitybar.width;
362
		let activityBarSize = new Dimension(this.activitybarWidth, sidebarSize.height);
E
Erich Gamma 已提交
363

I
isidor 已提交
364 365
		// Panel part
		let panelHeight: number;
I
isidor 已提交
366
		let panelWidth: number;
I
isidor 已提交
367 368
		const editorCountForHeight = this.editorGroupService.getGroupOrientation() === 'horizontal' ? this.editorGroupService.getStacksModel().groups.length : 1;
		const maxPanelHeight = sidebarSize.height - editorCountForHeight * MIN_EDITOR_PART_HEIGHT;
I
isidor 已提交
369 370
		const maxPanelWidth = this.workbenchSize.width - activityBarSize.width - sidebarSize.width - editorCountForHeight * MIN_EDITOR_PART_WIDTH;

I
isidor 已提交
371
		if (isPanelHidden) {
I
isidor 已提交
372
			panelHeight = 0;
I
isidor 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
			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;
			}
			if (panelHeight / maxPanelHeight < 0.7) {
				// Remember the previous height only if the panel size is not too large.
				// To get a nice minimize effect even if a user dragged the panel sash to maximum.
				this.panelHeightBeforeMaximized = panelHeight;
			}

			panelWidth = this.workbenchSize.width - sidebarSize.width - activityBarSize.width;
I
isidor 已提交
389
		} else {
I
isidor 已提交
390 391 392 393 394 395
			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;
			}
I
isidor 已提交
396
		}
I
isidor 已提交
397
		const panelDimension = new Dimension(panelWidth, panelHeight);
I
isidor 已提交
398

E
Erich Gamma 已提交
399 400 401
		// Editor
		let editorSize = {
			width: 0,
I
isidor 已提交
402
			height: 0
E
Erich Gamma 已提交
403 404
		};

I
isidor 已提交
405 406
		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 已提交
407 408 409

		// Sidebar hidden
		if (isSidebarHidden) {
410
			editorSize.width = this.workbenchSize.width - activityBarSize.width;
E
Erich Gamma 已提交
411 412 413
		}

		// Assert Sidebar and Editor Size to not overflow
414 415
		let editorMinWidth = this.partLayoutInfo.editor.minWidth;
		let editorMinHeight = this.partLayoutInfo.editor.minHeight;
E
Erich Gamma 已提交
416 417
		let visibleEditorCount = this.editorService.getVisibleEditors().length;
		if (visibleEditorCount > 1) {
418
			if (this.layoutEditorGroupsVertically) {
419 420 421 422
				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 已提交
423
		}
B
Benjamin Pasero 已提交
424

I
isidor 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
		// TODO@Isidor
		// if (editorSize.width < editorMinWidth) {
		// 	let diff = editorMinWidth - editorSize.width;
		// 	editorSize.width = editorMinWidth;
		// 	panelDimension.width = editorMinWidth;
		// 	sidebarSize.width -= diff;
		// 	sidebarSize.width = Math.max(MIN_SIDEBAR_PART_WIDTH, sidebarSize.width);
		// }

		// if (editorSize.height < editorMinHeight) {
		// 	let diff = editorMinHeight - editorSize.height;
		// 	editorSize.height = editorMinHeight;
		// 	panelDimension.height -= diff;
		// 	panelDimension.height = Math.max(MIN_PANEL_PART_HEIGHT, panelDimension.height);
		// }
E
Erich Gamma 已提交
440 441 442

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

I
isidor 已提交
446 447
		if (!isPanelHidden) {
			this.panelHeight = panelDimension.height;
448
			this.panelWidth = panelDimension.width;
I
isidor 已提交
449
			this.storageService.store(WorkbenchLayout.sashYHeightSettingsKey, this.panelHeight, StorageScope.GLOBAL);
450
			this.storageService.store(WorkbenchLayout.sashXTwoWidthSettingsKey, this.panelWidth, StorageScope.GLOBAL);
I
isidor 已提交
451 452
		}

E
Erich Gamma 已提交
453 454
		// Workbench
		this.workbenchContainer
455
			.position(0, 0, 0, 0, 'relative')
E
Erich Gamma 已提交
456 457
			.size(this.workbenchSize.width, this.workbenchSize.height);

B
Benjamin Pasero 已提交
458 459 460 461 462 463 464
		// 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 已提交
465 466
		}

467 468 469 470 471 472 473
		// Title Part
		if (isTitlebarHidden) {
			this.titlebar.getContainer().hide();
		} else {
			this.titlebar.getContainer().show();
		}

I
isidor 已提交
474
		// Editor Part and Panel part
E
Erich Gamma 已提交
475
		this.editor.getContainer().size(editorSize.width, editorSize.height);
B
Benjamin Pasero 已提交
476
		this.panel.getContainer().size(panelDimension.width, panelDimension.height);
E
Erich Gamma 已提交
477

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

		// Activity Bar Part
B
Benjamin Pasero 已提交
497
		this.activitybar.getContainer().size(null, activityBarSize.height);
E
Erich Gamma 已提交
498 499
		if (sidebarPosition === Position.LEFT) {
			this.activitybar.getContainer().getHTMLElement().style.right = '';
500
			this.activitybar.getContainer().position(this.titlebarHeight, null, 0, 0);
E
Erich Gamma 已提交
501 502
		} else {
			this.activitybar.getContainer().getHTMLElement().style.left = '';
503
			this.activitybar.getContainer().position(this.titlebarHeight, 0, 0, null);
E
Erich Gamma 已提交
504
		}
B
Benjamin Pasero 已提交
505 506 507 508 509
		if (isActivityBarHidden) {
			this.activitybar.getContainer().hide();
		} else {
			this.activitybar.getContainer().show();
		}
E
Erich Gamma 已提交
510 511 512 513 514

		// Sidebar Part
		this.sidebar.getContainer().size(sidebarSize.width, sidebarSize.height);

		if (sidebarPosition === Position.LEFT) {
515
			this.sidebar.getContainer().position(this.titlebarHeight, editorSize.width, 0, activityBarSize.width);
E
Erich Gamma 已提交
516
		} else {
517
			this.sidebar.getContainer().position(this.titlebarHeight, null, 0, editorSize.width);
E
Erich Gamma 已提交
518 519 520
		}

		// Statusbar Part
521
		this.statusbar.getContainer().position(this.workbenchSize.height - this.statusbarHeight);
522 523 524 525 526
		if (isStatusbarHidden) {
			this.statusbar.getContainer().hide();
		} else {
			this.statusbar.getContainer().show();
		}
E
Erich Gamma 已提交
527 528 529 530

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

I
isidor 已提交
531
		// Sashes
532 533 534 535 536 537 538 539
		this.sashXOne.layout();
		if (panelPosition === Position.BOTTOM) {
			this.sashY.layout();
			this.sashXTwo.hide();
		} else {
			this.sashY.hide();
			this.sashXTwo.layout();
		}
E
Erich Gamma 已提交
540 541

		// Propagate to Part Layouts
542
		this.titlebar.layout(new Dimension(this.workbenchSize.width, this.titlebarHeight));
E
Erich Gamma 已提交
543 544
		this.editor.layout(new Dimension(editorSize.width, editorSize.height));
		this.sidebar.layout(sidebarSize);
B
Benjamin Pasero 已提交
545
		this.panel.layout(panelDimension);
546
		this.activitybar.layout(activityBarSize);
E
Erich Gamma 已提交
547 548

		// Propagate to Context View
B
Benjamin Pasero 已提交
549
		this.contextViewService.layout();
E
Erich Gamma 已提交
550 551 552
	}

	public getVerticalSashTop(sash: Sash): number {
553
		return this.titlebarHeight;
E
Erich Gamma 已提交
554 555 556
	}

	public getVerticalSashLeft(sash: Sash): number {
557
		let isSidebarVisible = this.partService.isVisible(Parts.SIDEBAR_PART);
E
Erich Gamma 已提交
558
		let sidebarPosition = this.partService.getSideBarPosition();
559
		if (sash === this.sashXOne) {
E
Erich Gamma 已提交
560

561 562 563 564 565
			if (sidebarPosition === Position.LEFT) {
				return isSidebarVisible ? this.sidebarWidth + this.activitybarWidth : this.activitybarWidth;
			}

			return isSidebarVisible ? this.workbenchSize.width - this.sidebarWidth - this.activitybarWidth : this.workbenchSize.width - this.activitybarWidth;
E
Erich Gamma 已提交
566 567
		}

568
		return this.workbenchSize.width - this.panelWidth - (sidebarPosition === Position.RIGHT && isSidebarVisible ? this.sidebarWidth + this.activitybarWidth : 0);
E
Erich Gamma 已提交
569 570 571
	}

	public getVerticalSashHeight(sash: Sash): number {
I
isidor 已提交
572
		return this.sidebarHeight;
E
Erich Gamma 已提交
573 574
	}

I
isidor 已提交
575
	public getHorizontalSashTop(sash: Sash): number {
576 577
		// 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 已提交
578 579 580
	}

	public getHorizontalSashLeft(sash: Sash): number {
581
		return this.partService.getSideBarPosition() === Position.LEFT ? this.getVerticalSashLeft(sash) : 0;
I
isidor 已提交
582 583 584
	}

	public getHorizontalSashWidth(sash: Sash): number {
I
isidor 已提交
585
		return this.panelWidth;
I
isidor 已提交
586 587
	}

588
	// change part size along the main axis
B
Benjamin Pasero 已提交
589 590
	public resizePart(part: Parts, sizeChange: number): void {
		const visibleEditors = this.editorService.getVisibleEditors().length;
591 592 593
		const sizeChangePxWidth = this.workbenchSize.width * (sizeChange / 100);
		const sizeChangePxHeight = this.workbenchSize.height * (sizeChange / 100);

B
Benjamin Pasero 已提交
594 595 596
		let doLayout = false;
		let newSashSize: number = 0;

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
		switch (part) {
			case Parts.SIDEBAR_PART:
				newSashSize = this.sidebarWidth + sizeChangePxWidth;
				this.sidebarWidth = Math.max(this.partLayoutInfo.sidebar.minWidth, newSashSize); // Sidebar can not become smaller than MIN_PART_WIDTH

				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:
				newSashSize = this.panelHeight + sizeChangePxHeight;
				this.panelHeight = Math.max(this.partLayoutInfo.panel.minHeight, newSashSize);
				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 已提交
620 621 622
				} else {
					const stacks = this.editorGroupService.getStacksModel();
					const activeGroup = stacks.positionOfGroup(stacks.activeGroup);
623 624 625 626 627 628 629

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

		if (doLayout) {
B
Benjamin Pasero 已提交
630
			this.layout();
631 632 633
		}
	}

E
Erich Gamma 已提交
634
	public dispose(): void {
M
Martin Aeschlimann 已提交
635 636 637
		if (this.toUnbind) {
			dispose(this.toUnbind);
			this.toUnbind = null;
E
Erich Gamma 已提交
638 639
		}
	}
B
Benjamin Pasero 已提交
640
}