/
vbox-layout.ts
275 lines (253 loc) · 10.6 KB
/
vbox-layout.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/********************************************************************************
* Copyright (c) 2021-2023 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { injectable } from 'inversify';
import {
Bounds,
BoundsData,
Dimension,
GChildElement,
LayoutContainer,
Point,
GModelElement,
GParentElement,
StatefulLayouter,
VBoxLayoutOptions,
VBoxLayouter,
isBoundsAware,
isLayoutableChild
} from '@eclipse-glsp/sprotty';
export interface VBoxLayoutOptionsExt extends VBoxLayoutOptions {
hGrab: boolean;
vGrab: boolean;
prefWidth: number | null;
prefHeight: number | null;
}
/**
* Extends VBoxLayouter to support additional layout options
*/
@injectable()
export class VBoxLayouterExt extends VBoxLayouter {
static override KIND = VBoxLayouter.KIND;
override layout(container: GParentElement & LayoutContainer, layouter: StatefulLayouter): void {
const boundsData = layouter.getBoundsData(container);
const options = this.getLayoutOptions(container);
const childrenSize = this.getChildrenSize(container, options, layouter);
const fixedSize = this.getFixedContainerBounds(container, options, layouter);
const currentWidth = (boundsData.bounds?.width || 0) - options.paddingLeft - options.paddingRight;
const currentHeight = (boundsData.bounds?.height || 0) - options.paddingTop - options.paddingBottom;
const maxWidth =
options.paddingFactor *
(options.resizeContainer
? Math.max(fixedSize.width - options.paddingLeft - options.paddingRight, childrenSize.width)
: Math.max(0, fixedSize.width - options.paddingLeft - options.paddingRight));
const maxHeight =
options.paddingFactor *
(options.resizeContainer
? Math.max(fixedSize.height - options.paddingTop - options.paddingBottom, childrenSize.height)
: Math.max(0, fixedSize.height - options.paddingTop - options.paddingBottom));
const width = Math.max(currentWidth, maxWidth);
const height = Math.max(currentHeight, maxHeight);
// Remaining size that can be grabbed by children with the vGrab option
const grabHeight: number = height - childrenSize.height;
// Number of children that request vGrab
// FIXME: This approach works fine when only 1 child uses VGrab, but may cause rounding issues
// when the grabHeight can't be equally shared by all children.
const grabbingChildren = container.children
.map(child => this.getChildLayoutOptions(child, options))
.filter(opt => opt.vGrab).length;
if (maxWidth > 0 && maxHeight > 0) {
const offset = this.layoutChildren(container, layouter, options, width, height, grabHeight, grabbingChildren);
boundsData.bounds = this.getFinalContainerBounds(container, offset, options, childrenSize.width, childrenSize.height);
boundsData.boundsChanged = true;
}
}
protected override getChildrenSize(
container: GParentElement & LayoutContainer,
containerOptions: VBoxLayoutOptionsExt,
layouter: StatefulLayouter
): Dimension {
let maxWidth = -1;
let maxHeight = 0;
let isFirst = true;
container.children.forEach(child => {
if (isLayoutableChild(child)) {
const bounds = layouter.getBoundsData(child).bounds;
if (bounds !== undefined && Dimension.isValid(bounds)) {
maxHeight += bounds.height;
if (isFirst) {
isFirst = false;
} else {
maxHeight += containerOptions.vGap;
}
maxWidth = Math.max(maxWidth, bounds.width);
}
}
});
const result = {
width: maxWidth,
height: maxHeight
};
return result;
}
protected override layoutChildren(
container: GParentElement & LayoutContainer,
layouter: StatefulLayouter,
containerOptions: VBoxLayoutOptionsExt,
maxWidth: number,
maxHeight: number,
grabHeight?: number,
grabbingChildren?: number
): Point {
let currentOffset: Point = {
x: containerOptions.paddingLeft + 0.5 * (maxWidth - maxWidth / containerOptions.paddingFactor),
y: containerOptions.paddingTop + 0.5 * (maxHeight - maxHeight / containerOptions.paddingFactor)
};
container.children.forEach(child => {
if (isLayoutableChild(child)) {
const boundsData = layouter.getBoundsData(child);
const bounds = boundsData.bounds;
const childOptions = this.getChildLayoutOptions(child, containerOptions);
if (bounds !== undefined && Dimension.isValid(bounds)) {
currentOffset = this.layoutChild(
child,
boundsData,
bounds,
childOptions,
containerOptions,
currentOffset,
maxWidth,
maxHeight,
grabHeight,
grabbingChildren
);
}
}
});
return currentOffset;
}
protected override layoutChild(
child: GChildElement,
boundsData: BoundsData,
bounds: Bounds,
childOptions: VBoxLayoutOptionsExt,
containerOptions: VBoxLayoutOptionsExt,
currentOffset: Point,
maxWidth: number,
maxHeight: number,
grabHeight?: number,
grabbingChildren?: number
): Point {
const hAlign = childOptions.hGrab ? 'left' : childOptions.hAlign;
const dx = this.getDx(hAlign, bounds, maxWidth);
let offset = super.layoutChild(child, boundsData, bounds, childOptions, containerOptions, currentOffset, maxWidth, maxHeight);
boundsData.bounds = {
...boundsData.bounds!,
x: currentOffset.x + dx,
y: currentOffset.y
};
if (childOptions.hGrab) {
boundsData.bounds = {
x: boundsData.bounds!.x,
y: boundsData.bounds!.y,
width: maxWidth,
height: boundsData.bounds!.height
};
boundsData.boundsChanged = true;
}
if (childOptions.vGrab && grabHeight && grabbingChildren) {
const height = boundsData.bounds!.height + grabHeight / grabbingChildren;
boundsData.bounds = {
x: boundsData.bounds!.x,
y: boundsData.bounds!.y,
width: boundsData.bounds!.width,
height: height
};
boundsData.boundsChanged = true;
offset = { x: currentOffset.x, y: currentOffset.y + height };
}
return offset;
}
protected override getFixedContainerBounds(
container: GModelElement,
layoutOptions: VBoxLayoutOptionsExt,
layouter: StatefulLayouter
): Bounds {
const currentContainer = container;
// eslint-disable-next-line no-constant-condition
if (isBoundsAware(currentContainer)) {
const bounds = currentContainer.bounds;
const elementOptions = this.getElementLayoutOptions(currentContainer);
const width = elementOptions?.prefWidth ?? 0;
const height = elementOptions?.prefHeight ?? 0;
return { ...bounds, width, height };
}
return Bounds.EMPTY;
}
protected override getChildLayoutOptions(child: GChildElement, containerOptions: VBoxLayoutOptionsExt): VBoxLayoutOptionsExt {
return super.getChildLayoutOptions(child, this.filterContainerOptions(containerOptions)) as VBoxLayoutOptionsExt;
}
protected override getLayoutOptions(element: GModelElement): VBoxLayoutOptionsExt {
return super.getLayoutOptions(element) as VBoxLayoutOptionsExt;
}
protected getElementLayoutOptions(element: GModelElement): VBoxLayoutOptionsExt | undefined {
return (element as any).layoutOptions;
}
protected override getFinalContainerBounds(
container: GParentElement & LayoutContainer,
lastOffset: Point,
options: VBoxLayoutOptionsExt,
maxWidth: number,
maxHeight: number
): Bounds {
const elementOptions = this.getElementLayoutOptions(container);
const width = elementOptions?.prefWidth ?? options.minWidth;
const height = elementOptions?.prefHeight ?? options.minHeight;
const result = {
x: container.bounds.x,
y: container.bounds.y,
width: Math.max(width, maxWidth + options.paddingLeft + options.paddingRight),
height: Math.max(height, maxHeight + options.paddingTop + options.paddingBottom)
};
return result;
}
protected override getDefaultLayoutOptions(): VBoxLayoutOptionsExt {
return {
resizeContainer: true,
paddingTop: 5,
paddingBottom: 5,
paddingLeft: 5,
paddingRight: 5,
paddingFactor: 1,
vGap: 1,
hAlign: 'center',
minWidth: 0,
minHeight: 0,
hGrab: false,
vGrab: false,
// eslint-disable-next-line no-null/no-null
prefHeight: null,
// eslint-disable-next-line no-null/no-null
prefWidth: null
};
}
protected filterContainerOptions(containerOptions: VBoxLayoutOptionsExt): VBoxLayoutOptionsExt {
// Reset object-specific layout options to default before merging,
// to make sure they won't be inherited (grab, prefSize)
// eslint-disable-next-line no-null/no-null
const localOptions = { vGrab: false, hGrab: false, prefHeight: null, prefWidth: null };
return { ...containerOptions, ...localOptions };
}
}