Skip to content

Commit 01c7675

Browse files
joaomorenobpasero
andauthored
Editors: have a new auto option for workbench.editor.splitSizing (microsoft#185761)
* splitview, grid AutoSizing * adjust editor setting for new `auto` option --------- Co-authored-by: Benjamin Pasero <[email protected]>
1 parent 26a1feb commit 01c7675

File tree

7 files changed

+83
-17
lines changed

7 files changed

+83
-17
lines changed

src/vs/base/browser/ui/grid/grid.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { Disposable } from 'vs/base/common/lifecycle';
1010
import 'vs/css!./gridview';
1111
import { Box, GridView, IGridViewOptions, IGridViewStyles, IView as IGridViewView, IViewSize, orthogonal, Sizing as GridViewSizing } from './gridview';
1212
import type { GridLocation } from 'vs/base/browser/ui/grid/gridview';
13-
///@ts-ignore
14-
import type { SplitView } from 'vs/base/browser/ui/splitview/splitview';
13+
import type { SplitView, AutoSizing as SplitViewAutoSizing } from 'vs/base/browser/ui/splitview/splitview';
1514

1615
export { IViewSize, LayoutPriority, Orientation, orthogonal } from './gridview';
1716

@@ -197,12 +196,14 @@ function getGridLocation(element: HTMLElement): GridLocation {
197196

198197
export type DistributeSizing = { type: 'distribute' };
199198
export type SplitSizing = { type: 'split' };
199+
export type AutoSizing = { type: 'auto' };
200200
export type InvisibleSizing = { type: 'invisible'; cachedVisibleSize: number };
201-
export type Sizing = DistributeSizing | SplitSizing | InvisibleSizing;
201+
export type Sizing = DistributeSizing | SplitSizing | AutoSizing | InvisibleSizing;
202202

203203
export namespace Sizing {
204204
export const Distribute: DistributeSizing = { type: 'distribute' };
205205
export const Split: SplitSizing = { type: 'split' };
206+
export const Auto: AutoSizing = { type: 'auto' };
206207
export function Invisible(cachedVisibleSize: number): InvisibleSizing { return { type: 'invisible', cachedVisibleSize }; }
207208
}
208209

@@ -403,6 +404,9 @@ export class Grid<T extends IView = IView> extends Disposable {
403404
viewSize = GridViewSizing.Split(index);
404405
} else if (size.type === 'distribute') {
405406
viewSize = GridViewSizing.Distribute;
407+
} else if (size.type === 'auto') {
408+
const [, index] = tail(referenceLocation);
409+
viewSize = GridViewSizing.Auto(index);
406410
} else {
407411
viewSize = size;
408412
}
@@ -445,7 +449,16 @@ export class Grid<T extends IView = IView> extends Disposable {
445449
}
446450

447451
const location = this.getViewLocation(view);
448-
this.gridview.removeView(location, (sizing && sizing.type === 'distribute') ? GridViewSizing.Distribute : undefined);
452+
453+
let gridViewSizing: DistributeSizing | SplitViewAutoSizing | undefined;
454+
455+
if (sizing?.type === 'distribute') {
456+
gridViewSizing = GridViewSizing.Distribute;
457+
} else if (sizing?.type === 'auto') {
458+
gridViewSizing = GridViewSizing.Auto(0);
459+
}
460+
461+
this.gridview.removeView(location, gridViewSizing);
449462
this.views.delete(view);
450463
}
451464

src/vs/base/browser/ui/grid/gridview.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { $ } from 'vs/base/browser/dom';
77
import { IBoundarySashes, Orientation, Sash } from 'vs/base/browser/ui/sash/sash';
8-
import { DistributeSizing, ISplitViewStyles, IView as ISplitView, LayoutPriority, Sizing, SplitView } from 'vs/base/browser/ui/splitview/splitview';
8+
import { DistributeSizing, ISplitViewStyles, IView as ISplitView, LayoutPriority, Sizing, AutoSizing, SplitView } from 'vs/base/browser/ui/splitview/splitview';
99
import { equals as arrayEquals, tail2 as tail } from 'vs/base/common/arrays';
1010
import { Color } from 'vs/base/common/color';
1111
import { Emitter, Event, Relay } from 'vs/base/common/event';
@@ -1227,7 +1227,7 @@ export class GridView implements IDisposable {
12271227
* @param location The {@link GridLocation location} of the {@link IView view}.
12281228
* @param sizing Whether to distribute other {@link IView view}'s sizes.
12291229
*/
1230-
removeView(location: GridLocation, sizing?: DistributeSizing): IView {
1230+
removeView(location: GridLocation, sizing?: DistributeSizing | AutoSizing): IView {
12311231
this.disposable2x2.dispose();
12321232
this.disposable2x2 = Disposable.None;
12331233

src/vs/base/browser/ui/splitview/splitview.ts

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ export type DistributeSizing = { type: 'distribute' };
343343
*/
344344
export type SplitSizing = { type: 'split'; index: number };
345345

346+
/**
347+
* When adding a view, use DistributeSizing when all pre-existing views are
348+
* distributed evenly, otherwise use SplitSizing.
349+
*/
350+
export type AutoSizing = { type: 'auto'; index: number };
351+
346352
/**
347353
* When adding or removing views, assume the view is invisible.
348354
*/
@@ -352,7 +358,7 @@ export type InvisibleSizing = { type: 'invisible'; cachedVisibleSize: number };
352358
* When adding or removing views, the sizing provides fine grained
353359
* control over how other views get resized.
354360
*/
355-
export type Sizing = DistributeSizing | SplitSizing | InvisibleSizing;
361+
export type Sizing = DistributeSizing | SplitSizing | AutoSizing | InvisibleSizing;
356362

357363
export namespace Sizing {
358364

@@ -368,6 +374,12 @@ export namespace Sizing {
368374
*/
369375
export function Split(index: number): SplitSizing { return { type: 'split', index }; }
370376

377+
/**
378+
* When adding a view, use DistributeSizing when all pre-existing views are
379+
* distributed evenly, otherwise use SplitSizing.
380+
*/
381+
export function Auto(index: number): AutoSizing { return { type: 'auto', index }; }
382+
371383
/**
372384
* When adding or removing views, assume the view is invisible.
373385
*/
@@ -646,6 +658,14 @@ export class SplitView<TLayoutContext = undefined> extends Disposable {
646658
throw new Error('Index out of bounds');
647659
}
648660

661+
if (sizing?.type === 'auto') {
662+
if (this.areViewsDistributed()) {
663+
sizing = { type: 'distribute' };
664+
} else {
665+
sizing = undefined;
666+
}
667+
}
668+
649669
// Remove view
650670
const viewItem = this.viewItems.splice(index, 1)[0];
651671
const view = viewItem.dispose();
@@ -1054,12 +1074,22 @@ export class SplitView<TLayoutContext = undefined> extends Disposable {
10541074

10551075
if (typeof size === 'number') {
10561076
viewSize = size;
1057-
} else if (size.type === 'split') {
1058-
viewSize = this.getViewSize(size.index) / 2;
1059-
} else if (size.type === 'invisible') {
1060-
viewSize = { cachedVisibleSize: size.cachedVisibleSize };
10611077
} else {
1062-
viewSize = view.minimumSize;
1078+
if (size.type === 'auto') {
1079+
if (this.areViewsDistributed()) {
1080+
size = { type: 'distribute' };
1081+
} else {
1082+
size = { type: 'split', index: size.index };
1083+
}
1084+
}
1085+
1086+
if (size.type === 'split') {
1087+
viewSize = this.getViewSize(size.index) / 2;
1088+
} else if (size.type === 'invisible') {
1089+
viewSize = { cachedVisibleSize: size.cachedVisibleSize };
1090+
} else {
1091+
viewSize = view.minimumSize;
1092+
}
10631093
}
10641094

10651095
const item = this.orientation === Orientation.VERTICAL
@@ -1382,6 +1412,21 @@ export class SplitView<TLayoutContext = undefined> extends Disposable {
13821412
return undefined;
13831413
}
13841414

1415+
private areViewsDistributed() {
1416+
let min = undefined, max = undefined;
1417+
1418+
for (const view of this.viewItems) {
1419+
min = min === undefined ? view.size : Math.min(min, view.size);
1420+
max = max === undefined ? view.size : Math.max(max, view.size);
1421+
1422+
if (max - min > 2) {
1423+
return false;
1424+
}
1425+
}
1426+
1427+
return true;
1428+
}
1429+
13851430
override dispose(): void {
13861431
this.sashDragState?.disposable.dispose();
13871432

src/vs/workbench/browser/parts/editor/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const DEFAULT_EDITOR_PART_OPTIONS: IEditorPartOptions = {
3939
openSideBySideDirection: 'right',
4040
closeEmptyGroups: true,
4141
labelFormat: 'default',
42-
splitSizing: 'distribute',
42+
splitSizing: 'auto',
4343
splitOnDragAndDrop: true,
4444
centeredLayoutFixedWidth: false,
4545
};

src/vs/workbench/browser/parts/editor/editorPart.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,14 @@ export class EditorPart extends Part implements IEditorGroupsService, IEditorGro
554554
}
555555

556556
private getSplitSizingStyle(): Sizing {
557-
return this._partOptions.splitSizing === 'split' ? Sizing.Split : Sizing.Distribute;
557+
switch (this._partOptions.splitSizing) {
558+
case 'distribute':
559+
return Sizing.Distribute;
560+
case 'split':
561+
return Sizing.Split;
562+
default:
563+
return Sizing.Auto;
564+
}
558565
}
559566

560567
private doCreateGroupView(from?: IEditorGroupView | ISerializedEditorGroupModel | null): IEditorGroupView {

src/vs/workbench/browser/workbench.contribution.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ const registry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Con
169169
},
170170
'workbench.editor.splitSizing': {
171171
'type': 'string',
172-
'enum': ['distribute', 'split'],
173-
'default': 'distribute',
172+
'enum': ['auto', 'distribute', 'split'],
173+
'default': 'auto',
174174
'enumDescriptions': [
175+
localize('workbench.editor.splitSizingAuto', "Splits all the editor groups to equal parts unless a part has been changed in size."),
175176
localize('workbench.editor.splitSizingDistribute', "Splits all the editor groups to equal parts."),
176177
localize('workbench.editor.splitSizingSplit', "Splits the active editor group to equal parts.")
177178
],

src/vs/workbench/common/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ interface IEditorPartConfiguration {
11131113
labelFormat?: 'default' | 'short' | 'medium' | 'long';
11141114
restoreViewState?: boolean;
11151115
splitInGroupLayout?: 'vertical' | 'horizontal';
1116-
splitSizing?: 'split' | 'distribute';
1116+
splitSizing?: 'auto' | 'split' | 'distribute';
11171117
splitOnDragAndDrop?: boolean;
11181118
centeredLayoutFixedWidth?: boolean;
11191119
limit?: {

0 commit comments

Comments
 (0)