-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlayouter.ts
137 lines (128 loc) · 4.9 KB
/
layouter.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
/********************************************************************************
* Copyright (c) 2022-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,
ILogger,
LayoutContainer,
LayoutRegistry,
Layouter,
GModelElement,
GParentElement,
StatefulLayouter,
isLayoutContainer
} from '@eclipse-glsp/sprotty';
@injectable()
export class LayouterExt extends Layouter {
override layout(element2boundsData: Map<GModelElement, BoundsData>): void {
new StatefulLayouterExt(element2boundsData, this.layoutRegistry, this.logger).layout();
}
}
// 2-pass layout:
// Step 1: Find "rendered size" of each element (may take resizeContainer into account)
// Child-to-parent layout
// Step 2: Extend parents as necessary, then use the adjusted parent size to properly
// align children (center/end alignments, hGrab/vGrab)
// Parent-to-children layout
export class StatefulLayouterExt extends StatefulLayouter {
protected toBeLayouted2: (GParentElement & LayoutContainer)[];
/**
*
* @param elementToBoundsData The map of element to bounds data. Bounds Data are computed from the hidden
* SVG rendering pass.
* @param layoutRegistry2 The registry of available layouts.
* @param log The log.
*/
constructor(
protected readonly elementToBoundsData: Map<GModelElement, BoundsData>,
protected readonly layoutRegistry2: LayoutRegistry,
log: ILogger
) {
super(elementToBoundsData, layoutRegistry2, log);
this.toBeLayouted2 = [];
elementToBoundsData.forEach((data, element) => {
if (isLayoutContainer(element)) {
this.toBeLayouted2.push(element);
}
});
for (const element of this.toBeLayouted2) {
// Clear previous layout information for dynamic-layout objects
elementToBoundsData.delete(element);
}
}
override getBoundsData(element: GModelElement): BoundsData {
let boundsData = this.elementToBoundsData.get(element);
let bounds = (element as any).bounds;
if (isLayoutContainer(element) && this.toBeLayouted2.indexOf(element) >= 0) {
bounds = this.doLayout(element);
} else if (isLayoutContainer(element)) {
bounds = {
x: 0,
y: 0,
width: -1,
height: -1
};
}
if (!boundsData) {
boundsData = {
bounds: bounds,
boundsChanged: false,
alignmentChanged: false
};
this.elementToBoundsData.set(element, boundsData);
}
return boundsData;
}
override layout(): void {
// First pass: apply layout with cleared container data. Will get
// preferred size for all elements (Children first, then parents)
while (this.toBeLayouted2.length > 0) {
const element = this.toBeLayouted2[0];
this.doLayout(element);
}
this.toBeLayouted2 = [];
this.elementToBoundsData.forEach((data, element) => {
if (isLayoutContainer(element)) {
this.toBeLayouted2.push(element);
}
});
// Second pass: apply layout with initial size data for all
// nodes. Update the position/size of all elements, taking
// vGrab/hGrab into account (parent -> children).
while (this.toBeLayouted2.length > 0) {
const element = this.toBeLayouted2[0];
this.doLayout(element);
}
}
protected override doLayout(element: GParentElement & LayoutContainer): Bounds {
const index = this.toBeLayouted2.indexOf(element);
if (index >= 0) {
this.toBeLayouted2.splice(index, 1);
}
const layout = this.layoutRegistry2.get(element.layout);
if (layout) {
layout.layout(element, this);
}
const boundsData = this.elementToBoundsData.get(element);
if (boundsData !== undefined && boundsData.bounds !== undefined) {
return boundsData.bounds;
} else {
this.log.error(element, 'Layout failed');
return Bounds.EMPTY;
}
}
}