-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathlayout.cpp
684 lines (526 loc) · 16.9 KB
/
layout.cpp
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#include "layout.h"
#include "view.h"
namespace Karm::Ui {
// MARK: Grow ------------------------------------------------------------------
struct Grow : public ProxyNode<Grow> {
isize _grow;
Grow(Child child)
: ProxyNode(child), _grow(1) {}
Grow(isize grow, Child child)
: ProxyNode(child), _grow(grow) {}
isize grow() const {
return _grow;
}
};
Child grow(Opt<Child> child) {
return makeRc<Grow>(
child.unwrapOrElse([] {
return empty();
})
);
}
Child grow(isize grow, Opt<Child> child) {
return makeRc<Grow>(
grow,
child.unwrapOrElse([] {
return empty();
})
);
}
// MARK: Empty -----------------------------------------------------------------
struct Empty : public View<Empty> {
Math::Vec2i _size;
Empty(Math::Vec2i size)
: _size(size) {}
void reconcile(Empty& o) override {
_size = o._size;
}
Math::Vec2i size(Math::Vec2i, Hint) override {
return _size;
}
void paint(Gfx::Canvas&, Math::Recti) override {}
};
Child empty(Math::Vec2i size) {
return makeRc<Empty>(size);
}
Child cond(bool cond, Child child) {
if (cond)
return child;
return empty();
}
// MARK: Bound -----------------------------------------------------------------
struct Bound : public ProxyNode<Bound> {
Math::Recti _bound;
Bound(Child child)
: ProxyNode(child) {}
Math::Recti bound() override {
return _bound;
}
void layout(Math::Recti bound) override {
_bound = bound;
child().layout(bound);
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
return child().size(s, hint);
}
};
Child bound(Child child) {
return makeRc<Bound>(child);
}
struct Placed : public ProxyNode<Placed> {
Math::Recti _bound;
Math::Recti _place;
Placed(Math::Recti place, Child child)
: ProxyNode(child), _place(place) {}
void reconcile(Placed& o) override {
_place = o._place;
ProxyNode<Placed>::reconcile(o);
}
Math::Recti bound() override {
return _bound;
}
void layout(Math::Recti bound) override {
_bound = bound;
auto place = _place;
place.xy = place.xy + _bound.xy;
child().layout(place);
}
Math::Vec2i size(Math::Vec2i s, Hint) override {
return s;
}
};
Child placed(Math::Recti place, Child child) {
return makeRc<Placed>(place, child);
}
// MARK: Separator -------------------------------------------------------------
struct Separator : public View<Separator> {
Math::Vec2i size(Math::Vec2i, Hint) override {
return {1};
}
void paint(Gfx::Canvas& g, Math::Recti) override {
g.push();
g.fillStyle(GRAY800);
g.fill(bound());
g.pop();
}
};
Child separator() {
return makeRc<Separator>();
}
// MARK: Align -----------------------------------------------------------------
struct Align : public ProxyNode<Align> {
Math::Align _align;
Align(Math::Align align, Child child) : ProxyNode(child), _align(align) {}
void layout(Math::Recti bound) override {
auto childSize = child().size(
bound.size(), _child.is<Grow>()
? Hint::MAX
: Hint::MIN
);
child()
.layout(_align.apply<isize>(
Math::Flow::LEFT_TO_RIGHT,
childSize,
bound
));
};
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
if (hint == Hint::MAX)
return _align.maxSize(child().size(s, hint), s);
return _align.minSize(child().size(s, hint));
}
};
Child align(Math::Align align, Child child) {
return makeRc<Align>(align, child);
}
Child center(Child child) {
return align(Math::Align::CENTER, child);
}
Child start(Child child) {
return align(Math::Align::START | Math::Align::VFILL, child);
}
Child end(Child child) {
return align(Math::Align::END | Math::Align::VFILL, child);
}
Child fit(Child child) {
return align(Math::Align::FIT, child);
}
Child cover(Child child) {
return align(Math::Align::COVER, child);
}
Child hcenter(Child child) {
return align(Math::Align::HCENTER | Math::Align::TOP, child);
}
Child vcenter(Child child) {
return align(Math::Align::VCENTER | Math::Align::START, child);
}
Child hcenterFill(Child child) {
return align(Math::Align::HCENTER | Math::Align::VFILL, child);
}
Child vcenterFill(Child child) {
return align(Math::Align::VCENTER | Math::Align::HFILL, child);
}
// MARK: Sizing ----------------------------------------------------------------
struct Sizing : public ProxyNode<Sizing> {
Math::Vec2i _min;
Math::Vec2i _max;
Math::Recti _rect;
Sizing(Math::Vec2i min, Math::Vec2i max, Child child)
: ProxyNode(child), _min(min), _max(max) {}
Math::Recti bound() override {
return _rect;
}
void reconcile(Sizing& o) override {
_min = o._min;
_max = o._max;
ProxyNode<Sizing>::reconcile(o);
}
void layout(Math::Recti bound) override {
_rect = bound;
child().layout(bound);
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
if (_max.x != UNCONSTRAINED) {
s.x = min(s.x, _max.x);
}
if (_max.y != UNCONSTRAINED) {
s.y = min(s.y, _max.y);
}
auto result = child().size(s, hint);
if (_min.x != UNCONSTRAINED) {
result.x = max(result.x, _min.x);
}
if (_min.y != UNCONSTRAINED) {
result.y = max(result.y, _min.y);
}
return result;
}
};
Child sizing(Math::Vec2i min, Math::Vec2i max, Child child) {
return makeRc<Sizing>(min, max, child);
}
Child minSize(Math::Vec2i size, Child child) {
return makeRc<Sizing>(size, UNCONSTRAINED, child);
}
Child minSize(isize size, Child child) {
return minSize(Math::Vec2i{size}, child);
}
Child maxSize(Math::Vec2i size, Child child) {
return makeRc<Sizing>(UNCONSTRAINED, size, child);
}
Child maxSize(isize size, Child child) {
return maxSize(Math::Vec2i{size}, child);
}
Child pinSize(Math::Vec2i size, Child child) {
return makeRc<Sizing>(size, size, child);
}
Child pinSize(isize size, Child child) {
return pinSize(Math::Vec2i{size}, child);
}
// MARK: Insets ---------------------------------------------------------------
struct Insets : public ProxyNode<Insets> {
Math::Insetsi _insets;
Insets(Math::Insetsi insets, Child child)
: ProxyNode(child), _insets(insets) {}
void reconcile(Insets& o) override {
_insets = o._insets;
ProxyNode<Insets>::reconcile(o);
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
child().paint(g, r);
}
void layout(Math::Recti rect) override {
child().layout(rect.shrink(_insets));
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
return child().size(s - _insets.all(), hint) + _insets.all();
}
Math::Recti bound() override {
return child().bound().grow(_insets);
}
};
Child insets(Math::Insetsi s, Child child) {
return makeRc<Insets>(s, child);
}
// MARK: Aspect Ratio ----------------------------------------------------------
struct AspectRatio : public ProxyNode<AspectRatio> {
f64 _ratio;
AspectRatio(f64 ratio, Child child)
: ProxyNode(child), _ratio(ratio) {}
void reconcile(AspectRatio& o) override {
_ratio = o._ratio;
ProxyNode<AspectRatio>::reconcile(o);
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
child().paint(g, r);
}
Math::Vec2i size(Math::Vec2i s, Hint) override {
if (s.x < s.y)
return {s.x, (isize)(s.x * _ratio)};
return {(isize)(s.y * _ratio), s.y};
}
Math::Recti bound() override {
return child().bound();
}
};
Child aspectRatio(f64 ratio, Child child) {
return makeRc<AspectRatio>(ratio, child);
}
// MARK: Stack -----------------------------------------------------------------
struct StackLayout : public GroupNode<StackLayout> {
using GroupNode::GroupNode;
void event(App::Event& e) override {
if (e.accepted())
return;
for (auto& child : mutIterRev(children())) {
child->event(e);
if (e.accepted())
return;
}
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
isize w{};
isize h{};
for (auto& child : children()) {
auto childSize = child->size(s, hint);
w = max(w, childSize.x);
h = max(h, childSize.y);
}
return {w, h};
}
};
Child stack(Children children) {
return makeRc<StackLayout>(children);
}
// MARK: Flow ------------------------------------------------------------------
struct FlowLayout : public GroupNode<FlowLayout> {
using GroupNode::GroupNode;
FlowStyle _style;
FlowLayout(FlowStyle style, Children children)
: GroupNode(children), _style(style) {}
void reconcile(FlowLayout& o) override {
_style = o._style;
GroupNode::reconcile(o);
}
f64 _computeGrowUnit(Math::Recti r) {
f64 total = 0;
f64 grows = 0;
for (auto& child : children()) {
if (child.is<Grow>()) {
grows += child.unwrap<Grow>().grow();
} else {
total += _style.flow.getX(child->size(r.size(), Hint::MIN));
}
}
f64 all = _style.flow.getWidth(r) - _style.gaps * (max(1uz, children().len()) - 1);
f64 growTotal = max(0, all - total);
return (growTotal) / max(1, grows);
}
void layout(Math::Recti r) override {
_bound = r;
f64 growUnit = _computeGrowUnit(r);
f64 start = _style.flow.getStart(r);
for (auto& child : children()) {
Math::Recti inner = {};
auto childSize = child->size(r.size(), Hint::MIN);
inner = _style.flow.setStart(inner, (isize)start);
if (child.is<Grow>()) {
inner = _style.flow.setWidth(inner, (isize)(growUnit * child.unwrap<Grow>().grow()));
} else {
inner = _style.flow.setWidth(inner, _style.flow.getX(childSize));
}
inner = _style.flow.setTop(inner, _style.flow.getTop(r));
inner = _style.flow.setBottom(inner, _style.flow.getBottom(r));
child->layout(_style.align.apply(_style.flow, Math::Recti{childSize}, inner));
start += _style.flow.getWidth(inner) + _style.gaps;
}
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
isize w{};
isize h{hint == Hint::MAX ? _style.flow.getY(s) : 0};
bool grow = false;
for (auto& child : children()) {
if (child.is<Grow>())
grow = true;
auto childSize = child->size(s, Hint::MIN);
w += _style.flow.getX(childSize);
h = max(h, _style.flow.getY(childSize));
}
w += _style.gaps * (max(1uz, children().len()) - 1);
if (grow and hint == Hint::MAX) {
w = max(_style.flow.getX(s), w);
}
return _style.flow.orien() == Math::Orien::HORIZONTAL
? Math::Vec2i{w, h}
: Math::Vec2i{h, w};
}
};
Child flow(FlowStyle style, Children children) {
return makeRc<FlowLayout>(style, children);
}
// MARK: Grid ------------------------------------------------------------------
struct Cell : public ProxyNode<Cell> {
Math::Vec2i _start{};
Math::Vec2i _end{};
Cell(Math::Vec2i start, Math::Vec2i end, Child child)
: ProxyNode(child), _start(start), _end(end) {}
Math::Vec2i start() const {
return _start;
}
Math::Vec2i end() const {
return _end;
}
};
Child cell(Math::Vec2i pos, Child child) {
return makeRc<Cell>(pos, pos, child);
}
Child cell(Math::Vec2i start, Math::Vec2i end, Child child) {
return makeRc<Cell>(start, end, child);
}
struct GridLayout : public GroupNode<GridLayout> {
struct _Dim {
isize start;
isize size;
isize end() const {
return start + size;
}
};
GridStyle _style;
Vec<_Dim> _rows;
Vec<_Dim> _columns;
GridLayout(GridStyle style, Children children)
: GroupNode(children), _style(style) {}
isize computeGapsRows() {
return _style.gaps.y * (max(1uz, _style.rows.len()) - 1);
}
isize computeGapsColumns() {
return _style.gaps.x * (max(1uz, _style.columns.len()) - 1);
}
isize computeGrowUnitRows(Math::Recti r) {
isize total = 0;
isize grows = 0;
for (auto& row : _style.rows) {
if (row.unit == GridUnit::GROW) {
grows += row.value;
} else {
total += row.value;
}
}
isize all = _style.flow.getHeight(r) - computeGapsRows();
isize growTotal = max(0, all - total);
return (growTotal) / max(1, grows);
}
isize computeGrowUnitColumns(Math::Recti r) {
isize total = 0;
isize grows = 0;
for (auto& column : _style.columns) {
if (column.unit == GridUnit::GROW) {
grows += column.value;
} else {
total += column.value;
}
}
isize all = _style.flow.getWidth(r) - computeGapsColumns();
isize growTotal = max(0, all - total);
return (growTotal) / max(1, grows);
}
void place(Child child, Math::Vec2i pos) {
place(child, pos, pos);
}
void place(Child child, Math::Vec2i start, Math::Vec2i end) {
auto startRow = _rows[start.y];
auto startColumn = _columns[start.x];
auto endRow = _rows[end.y];
auto endColumn = _columns[end.x];
auto childRect = Math::Recti{
startColumn.start,
startRow.start,
endColumn.end() - startColumn.start,
endRow.end() - startRow.start,
};
child->layout(childRect);
}
void layout(Math::Recti r) override {
_bound = r;
// compute the dimensions of the grid
_rows.clear();
isize growUnitRows = computeGrowUnitRows(r);
isize row = _style.flow.getTop(r);
for (auto& r : _style.rows) {
if (r.unit == GridUnit::GROW) {
_rows.pushBack({_Dim{row, growUnitRows * r.value}});
row += growUnitRows * r.value;
} else {
_rows.pushBack({_Dim{row, r.value}});
row += r.value;
}
row += _style.gaps.y;
}
_columns.clear();
isize growUnitColumns = computeGrowUnitColumns(r);
isize column = _style.flow.getStart(r);
for (auto& c : _style.columns) {
if (c.unit == GridUnit::GROW) {
_columns.pushBack({_Dim{column, growUnitColumns * c.value}});
column += growUnitColumns * c.value;
} else {
_columns.pushBack({_Dim{column, c.value}});
column += c.value;
}
column += _style.gaps.x;
}
// layout the children
isize index = 0;
for (auto& child : children()) {
if (child.is<Cell>()) {
auto& cell = child.unwrap<Cell>();
auto start = cell.start();
auto end = cell.end();
place(child, start, end);
index = end.y * _columns.len() + end.x;
} else {
isize row = index / _columns.len();
isize column = index % _columns.len();
place(child, {column, row});
}
index++;
}
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
isize row = 0;
bool rowGrow = false;
isize growUnitRows = computeGrowUnitRows(Math::Recti{0, s});
for (auto& r : _style.rows) {
if (r.unit == GridUnit::GROW) {
row += growUnitRows * r.value;
rowGrow = true;
} else {
row += r.value;
}
}
row += computeGapsRows();
if (rowGrow and hint == Hint::MAX) {
row = max(_style.flow.getY(s), row);
}
isize column = 0;
bool columnGrow = false;
isize growUnitColumns = computeGrowUnitColumns(Math::Recti{0, s});
for (auto& c : _style.columns) {
if (c.unit == GridUnit::GROW) {
column += growUnitColumns * c.value;
columnGrow = true;
} else {
column += c.value;
}
}
column += computeGapsColumns();
if (columnGrow and hint == Hint::MAX) {
column = max(_style.flow.getX(s), column);
}
return Math::Vec2i{column, row};
}
};
Child grid(GridStyle style, Children children) {
return makeRc<GridLayout>(style, children);
}
} // namespace Karm::Ui