-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.ts
205 lines (178 loc) · 5.58 KB
/
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
import { Point, Rect } from "../util/types";
export type LayoutRect = {
stretch?: Point,
shrink?: Point,
base?: Point,
};
export type LayoutTree = LayoutTreeInt<unknown>;
export function packHoriz(...trees: LayoutTree[]): LayoutTree {
return { t: 'horiz', kids: trees };
}
export function packVert(...trees: LayoutTree[]): LayoutTree {
return { t: 'vert', kids: trees };
}
export function stretchRectX(stretch: number): LayoutTree {
return { t: 'rect', single: { stretch: { x: stretch, y: 0 } } };
}
export function stretchRectY(stretch: number): LayoutTree {
return { t: 'rect', single: { stretch: { y: stretch, x: 0 } } };
}
export function fixedRect(base: Point): LayoutTree {
return { t: 'rect', single: { base } };
}
export function nameRect(name: string, tree: LayoutTree): LayoutTree {
return { t: 'name', kid: tree, name };
}
export function padVert(pad: number, tree: LayoutTree): LayoutTree {
return packVert(fixedRect({ x: 0, y: pad }), tree, fixedRect({ x: 0, y: pad }));
}
export function padHoriz(pad: number, tree: LayoutTree): LayoutTree {
return packHoriz(fixedRect({ x: pad, y: 0 }), tree, fixedRect({ x: pad, y: 0 }));
}
export function padRect(pad: number, tree: LayoutTree): LayoutTree {
return padVert(pad, padHoriz(pad, tree));
}
export function centerX(tree: LayoutTree): LayoutTree {
return packHoriz(stretchRectX(1), tree, stretchRectX(1));
}
type LayoutTreeInt<A> =
| { t: 'horiz', kids: LayoutTreeWith<A>[] }
| { t: 'vert', kids: LayoutTreeWith<A>[] }
| { t: 'rect', single: LayoutRect }
| { t: 'name', kid: LayoutTreeWith<A>, name: string }
;
type LayoutTreeWith<A> = A & LayoutTreeInt<A>;
export type Layout = Record<string, Rect>;
export type Sizes = {
base: Point,
stretch: Point,
shrink: Point,
}
export type ResultRect = {
rect: Rect,
}
function sum(xs: number[]): number {
return xs.length == 0 ? 0 : xs.reduce((a, b) => a + b);
}
function max(xs: number[]): number {
return xs.length == 0 ? 0 : xs.reduce((a, b) => Math.max(a, b));
}
function zero(_xs: number[]): number {
return 0;
}
function aggregate(ps: Point[], xf: (x: number[]) => number, yf: (x: number[]) => number): Point {
return { x: xf(ps.map(p => p.x)), y: yf(ps.map(p => p.y)) };
}
function layoutFirst(tree: LayoutTreeWith<unknown>): LayoutTreeWith<Sizes> {
switch (tree.t) {
case 'horiz': {
const kids = tree.kids.map(layoutFirst);
return {
t: 'horiz',
base: aggregate(kids.map(x => x.base), sum, max),
shrink: aggregate(kids.map(x => x.shrink), sum, zero),
stretch: aggregate(kids.map(x => x.stretch), sum, zero),
kids: kids,
};
}
case 'vert': {
const kids = tree.kids.map(layoutFirst);
return {
t: 'vert',
base: aggregate(kids.map(x => x.base), max, sum),
shrink: aggregate(kids.map(x => x.shrink), zero, sum),
stretch: aggregate(kids.map(x => x.stretch), zero, sum),
kids: kids,
};
}
case 'rect': {
return {
t: 'rect',
single: tree.single,
base: tree.single.base ?? { x: 0, y: 0 },
shrink: tree.single.shrink ?? { x: 0, y: 0 },
stretch: tree.single.stretch ?? { x: 0, y: 0 },
};
}
case 'name': {
const kid = layoutFirst(tree.kid);
return {
t: 'name',
name: tree.name,
kid: kid,
base: kid.base,
shrink: kid.shrink,
stretch: kid.stretch,
};
}
}
}
function layoutStack(
container: Rect,
tree: Sizes & { kids: LayoutTreeWith<Sizes>[] },
dir: 'horiz' | 'vert'
): LayoutTreeWith<ResultRect> {
function maybeSwap(p: Point): Point {
return dir == 'horiz' ? p : { y: p.x, x: p.y };
}
const xx: 'x' | 'y' = dir == 'horiz' ? 'x' : 'y';
const yy: 'x' | 'y' = dir == 'horiz' ? 'y' : 'x';
const gap = container.sz[xx] - tree.base[xx];
const mode: 'stretch' | 'shrink' = gap > 0 ? 'stretch' : 'shrink';
const resultSize = tree.kids.map(k => k.base[xx] + (tree[mode][xx] == 0 ? 0 : ((k[mode][xx] / tree[mode][xx]) * gap)));
const kids: LayoutTreeWith<ResultRect>[] = [];
let x = container.p[xx];
for (let i = 0; i < tree.kids.length; i++) {
const rs = resultSize[i];
kids.push(layoutSecond({
p: maybeSwap({ x: x, y: container.p[yy] }),
sz: maybeSwap({ x: rs, y: container.sz[yy] })
}, tree.kids[i]));
x += rs;
}
return {
t: 'horiz',
kids,
rect: container
};
}
function layoutSecond(container: Rect, tree: LayoutTreeWith<Sizes>): LayoutTreeWith<ResultRect> {
switch (tree.t) {
case 'horiz': return layoutStack(container, tree, 'horiz');
case 'vert': return layoutStack(container, tree, 'vert');
case 'rect': {
return {
t: 'rect',
single: tree.single,
rect: { p: container.p, sz: container.sz },
};
}
case 'name': {
const kid = layoutSecond(container, tree.kid);
return {
t: 'name',
name: tree.name,
kid: kid,
rect: kid.rect,
};
}
}
}
function layoutThird(tree: LayoutTreeWith<ResultRect>): Record<string, Rect> {
const rv: Record<string, Rect> = {};
function xverse(tree: LayoutTreeWith<ResultRect>) {
switch (tree.t) {
case 'horiz': tree.kids.forEach(xverse); return;
case 'vert': tree.kids.forEach(xverse); return;
case 'rect': return;
case 'name': rv[tree.name] = tree.rect; xverse(tree.kid); return;
}
}
xverse(tree);
return rv;
}
export function layout(container: Rect, tree: LayoutTreeInt<unknown>): Record<string, Rect> {
const tree1 = layoutFirst(tree);
const tree2 = layoutSecond(container, tree1);
return layoutThird(tree2);
}