day-core 0.6.2

Declarative app development API using native UI toolkits
Documentation
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! The layout engine (DESIGN.md §7): parent-proposes/child-chooses with a proposal-keyed
//! measure cache. Layout impls are ours or user-provided; they never run reactive user code,
//! so the engine holds the single tree borrow for the whole pass.

use std::rc::Rc;

day_reactive::tls_slots! {
    layout;
    /// Nodes already warned about (see the report site below): once per node, not per
    /// frame. A rebuilt page gets a fresh node and may report again, which is fine.
    static REPORTED: std::cell::RefCell<std::collections::HashSet<RNode>> =
        std::cell::RefCell::new(std::collections::HashSet::new());
}

use day_spec::*;

use crate::tree::{Flex, RNode, Tree, TreeOps};

/// Open layout protocol (§7.2). `children` are the node's direct children; group nodes
/// (`when`/`each` anchors) are layout-transparent: stacks expand them inline.
pub trait Layout: 'static {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size;
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect);
    /// Where this node's own first text baseline falls, measured from the top of a box of
    /// `size` (docs/baseline.md). `None` (the default) means the node has no baseline to
    /// offer, so a baseline-aligned parent falls back to box alignment for it.
    ///
    /// A container answers on behalf of its content: a column reports its first
    /// baseline-bearing child's, shifted by where that child sits. Leaves never reach here;
    /// [`LayoutOps::baseline_of`] asks the toolkit for those.
    fn baseline(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], _size: Size) -> Option<f64> {
        None
    }
}

/// The engine surface visible to `Layout` implementations.
pub trait LayoutOps {
    fn measure_child(&mut self, child: RNode, p: Proposal) -> Size;
    fn place_child(&mut self, child: RNode, rect: Rect);
    /// Place a child whose on-screen frame is native-owned (nav pages in splitter panes /
    /// nav-controller views): never direction-mirrored, because the toolkit positions it.
    fn place_child_native(&mut self, child: RNode, rect: Rect) {
        self.place_child(child, rect);
    }
    fn flex_of(&self, child: RNode) -> Flex;
    fn children_of(&self, node: RNode) -> Vec<RNode>;
    /// Native intrinsic measurement of the current node (leaves).
    fn measure_leaf(&mut self, p: Proposal) -> Size;
    /// A child's first text baseline at `size`, from the top of its box (docs/baseline.md):
    /// the toolkit answers for a native leaf, the child's own [`Layout::baseline`] for a
    /// container. `None` ⇒ no baseline to align to.
    fn baseline_of(&mut self, child: RNode, size: Size) -> Option<f64>;
    /// Report scroll content size for the current node (§7.6).
    fn set_scroll_content(&mut self, content: Size);
    /// The room the current scroll node's scroll bar takes from its content area while the
    /// content overflows (`Toolkit::scroll_bar_inset`); 0 where bars overlay the content.
    fn scroll_bar_inset(&mut self, _vertical: bool) -> f64 {
        0.0
    }
    /// Report that the current node's children outgrew the bounds its `place()` was given
    /// (`needed` vs `available` main-axis points). A diagnostic hook, not a layout input:
    /// the engine logs it once per node in debug builds and ignores it in release, and
    /// placement proceeds unchanged either way. Scroll containers never report, since
    /// content larger than the viewport is their normal state.
    fn report_overflow(&mut self, _needed: f64, _available: f64) {}
}

pub struct EngineCx<'a, B: Toolkit> {
    pub(crate) tree: &'a mut Tree<B>,
    pub(crate) offset: Point,
    pub(crate) current: RNode,
    /// The bounds the current node's `place()` was given: the mirroring axis for RTL.
    pub(crate) parent_size: Size,
}

impl<B: Toolkit> LayoutOps for EngineCx<'_, B> {
    fn measure_child(&mut self, child: RNode, p: Proposal) -> Size {
        measure_node(self.tree, child, p)
    }
    fn place_child(&mut self, child: RNode, rect: Rect) {
        // RTL (docs/localization): layouts compute LTR ("leading" = left); under a
        // right-to-left locale every horizontal placement mirrors around the parent's
        // width, so leading means right everywhere (rows reverse, padding swaps sides,
        // alignment flips) without any layout impl knowing about direction. Leaf content
        // (canvas drawing, text runs) is not mirrored; native text handles RTL itself.
        let rect = if crate::layout_direction() == day_geometry::LayoutDirection::Rtl {
            Rect::new(
                self.parent_size.width - rect.origin.x - rect.size.width,
                rect.origin.y,
                rect.size.width,
                rect.size.height,
            )
        } else {
            rect
        };
        place_node(self.tree, child, rect, self.offset, false);
    }
    fn place_child_native(&mut self, child: RNode, rect: Rect) {
        place_node(self.tree, child, rect, self.offset, false);
    }
    fn flex_of(&self, child: RNode) -> Flex {
        self.tree.node(child).map(|n| n.flex).unwrap_or_default()
    }
    fn children_of(&self, node: RNode) -> Vec<RNode> {
        self.tree
            .node(node)
            .map(|n| n.children.clone())
            .unwrap_or_default()
    }
    fn measure_leaf(&mut self, p: Proposal) -> Size {
        let Some(n) = self.tree.node(self.current) else {
            return Size::ZERO;
        };
        let kind = n.kind;
        let Some(h) = n.handle.clone() else {
            return Size::ZERO;
        };
        self.tree.toolkit.measure(&h, kind, p)
    }
    fn baseline_of(&mut self, child: RNode, size: Size) -> Option<f64> {
        baseline_node(self.tree, child, size)
    }
    fn set_scroll_content(&mut self, content: Size) {
        let current = self.current;
        let Some(n) = self.tree.node_mut(current) else {
            return;
        };
        // Cache for scroll_to_target (§7.6): edge targets need content-minus-viewport math.
        n.scroll_content = Some(content);
        let Some(h) = n.handle.clone() else { return };
        self.tree.toolkit.set_scroll_content(&h, content);
    }
    fn scroll_bar_inset(&mut self, vertical: bool) -> f64 {
        let Some(h) = self.tree.node(self.current).and_then(|n| n.handle.clone()) else {
            return 0.0;
        };
        self.tree.toolkit.scroll_bar_inset(&h, vertical).max(0.0)
    }

    #[cfg(debug_assertions)]
    fn report_overflow(&mut self, needed: f64, available: f64) {
        // Once per node, not per frame: layout re-runs constantly, and this is meant as a
        // greppable hint, not a firehose. A rebuilt page gets a fresh node and may report
        // again, which is fine.
        if REPORTED.with(|r| !r.borrow_mut().insert(self.current)) {
            return;
        }
        // Name the container by the dayscript ids in reach, because an id is the one thing a
        // developer can grep straight to the source. Ids usually sit a wrapper or two below
        // the overflowing stack (`.id` tags the piece, decorators wrap it), so this walks the
        // subtree depth-first and keeps the first few it meets.
        let mut ids: Vec<String> = Vec::new();
        let mut stack = vec![self.current];
        while let Some(node) = stack.pop() {
            if ids.len() >= 6 {
                break;
            }
            if let Some(n) = self.tree.node(node) {
                ids.extend(n.id.clone());
                stack.extend(n.children.iter().rev());
            }
        }
        let who = if ids.is_empty() {
            String::from("no .id in reach")
        } else {
            ids.join(", ")
        };
        log::warn!(
            "day layout: children overflow their container by {:.0}pt ({needed:.0} needed, \
             {available:.0} available; {who}) — give the row a fit policy: \
             .fit(RowFit::Wrap/Scroll) or adaptive(..) (docs/size-classes.md)",
            needed - available,
        );
    }
}

pub(crate) fn measure_node<B: Toolkit>(tree: &mut Tree<B>, node: RNode, p: Proposal) -> Size {
    let key = p.cache_key();
    let (layout, children) = {
        let Some(n) = tree.node(node) else {
            return Size::ZERO;
        };
        if !n.needs_measure
            && let Some(&(_, s)) = n.cache.iter().find(|(k, _)| *k == key)
        {
            return s;
        }
        (n.layout.clone(), n.children.clone())
    };
    let mut cx = EngineCx {
        tree,
        offset: Point::ZERO,
        current: node,
        parent_size: Size::ZERO, // placement never happens during measure
    };
    let size = layout.measure(&mut cx, &children, p);
    if let Some(n) = tree.node_mut(node) {
        n.needs_measure = false;
        if n.cache.len() >= 4 {
            n.cache.clear();
        }
        n.cache.push((key, size));
    }
    size
}

/// A node's first text baseline at `size`, from the top of its box (docs/baseline.md). A native
/// leaf asks the toolkit; anything else asks its own `Layout`, which answers for its content.
///
/// Cached per size and invalidated with the measure cache, so a baseline-aligned row costs one
/// toolkit call per child per measure generation rather than one per layout pass.
pub(crate) fn baseline_node<B: Toolkit>(
    tree: &mut Tree<B>,
    node: RNode,
    size: Size,
) -> Option<f64> {
    let key = Proposal::exact(size).cache_key();
    let (layout, children, handle, kind) = {
        let n = tree.node(node)?;
        if !n.needs_measure
            && let Some((k, b)) = n.baseline_cache
            && k == key
        {
            return b;
        }
        (
            n.layout.clone(),
            n.children.clone(),
            n.handle.clone(),
            n.kind,
        )
    };
    // A realized leaf's baseline is the toolkit's to report; a container's is its layout's.
    let baseline = match handle {
        Some(h) if children.is_empty() => tree.toolkit.first_baseline(&h, kind, size),
        _ => {
            let mut cx = EngineCx {
                tree,
                offset: Point::ZERO,
                current: node,
                parent_size: Size::ZERO,
            };
            layout.baseline(&mut cx, &children, size)
        }
    };
    if let Some(n) = tree.node_mut(node) {
        n.baseline_cache = Some((key, baseline));
    }
    baseline
}

/// `rect` is in the parent node's coordinates; `offset` is the parent's origin in the nearest
/// native ancestor's coordinates (§7.1, accumulated through layout-only nodes).
pub(crate) fn place_node<B: Toolkit>(
    tree: &mut Tree<B>,
    node: RNode,
    rect: Rect,
    offset: Point,
    is_root: bool,
) {
    let abs = Rect {
        origin: rect.origin.offset(offset.x, offset.y),
        size: rect.size,
    };
    let (layout, children, has_handle) = {
        let Some(n) = tree.node(node) else { return };
        (n.layout.clone(), n.children.clone(), n.handle.is_some())
    };
    // A cover's frame is the backend's (docs/cover.md): presented, it is re-homed over the
    // window and follows the window's resizing; parked, it is hidden. Where it sits in
    // the tree it measures zero (`CoverLayout`), so applying the parent's placement (a
    // zero-size rect at the parent's center) would collapse a presented cover to a line
    // the moment anything re-lays the parent out. A window resize did exactly that on
    // macOS (Games Fair, 2026-09-11): the game vanished into a one-pixel strip over the home
    // page. Its content is still placed below, at the size the backend reported.
    let native_frame = tree
        .node(node)
        .map(|n| n.kind == day_spec::kinds::COVER)
        .unwrap_or(false);
    let child_offset = if has_handle {
        if !is_root && !native_frame {
            let changed = tree
                .node(node)
                .map(|n| {
                    n.last_native_frame
                        .map(|f| !f.approx_eq(&abs, 0.25))
                        .unwrap_or(true)
                })
                .unwrap_or(false);
            if changed {
                let h = tree.node(node).and_then(|n| n.handle.clone());
                if let Some(h) = h {
                    let anim = tree.resolve_anim(node);
                    tree.toolkit.set_frame(&h, abs, anim.as_ref());
                }
                if tree
                    .node(node)
                    .map(|n| n.kind == day_spec::kinds::CANVAS)
                    .unwrap_or(false)
                {
                    // Queue-only (§8.3): canvases re-record against the new size after layout.
                    crate::tree::enqueue_event(
                        crate::tree::rnode_to_id(node),
                        day_spec::Event::FrameChanged(abs.size),
                    );
                }
            }
        }
        Point::ZERO
    } else {
        abs.origin
    };
    // A list whose width changes re-lays its active cells in this pass: the native table
    // resizes the physical cell views, but each cell's day content keeps the old width's
    // placement until laid out again (a trailing control would sit clipped after a
    // narrowing). It runs synchronously because interactive live-resize runs inside a
    // native tracking loop where deferred main-thread work may not drain until the drag
    // ends.
    let relayout_cells = tree
        .node(node)
        .map(|n| {
            n.kind == day_spec::kinds::LIST
                && n.last_native_frame
                    .map(|f| (f.size.width - abs.size.width).abs() > 0.25)
                    .unwrap_or(false)
        })
        .unwrap_or(false);
    if let Some(n) = tree.node_mut(node) {
        n.last_native_frame = Some(abs);
        n.needs_layout = false;
    }
    if relayout_cells {
        for key in tree.list_cell_keys(node) {
            tree.list_layout_cell(node, key);
        }
    }
    let mut cx = EngineCx {
        tree,
        offset: child_offset,
        current: node,
        parent_size: rect.size,
    };
    layout.place(&mut cx, &children, Rect::from_size(rect.size));
}

// ---------------------------------------------------------------------------
// Built-in layouts
// ---------------------------------------------------------------------------

/// A single-child wrapper's baseline is its child's, moved by where the child sits inside it
/// (docs/baseline.md).
///
/// Every wrapper below places its one child at its own top-left, so `dy` is 0 for all of them
/// but padding. Without this a decorated piece reports no baseline, and since decorators are
/// invisible in the source (`.width(90)` on a label is still "a label" to the reader), a row
/// would silently center the very children the author asked to align. That is exactly what
/// happened to the showcase's first baseline demo.
fn wrapper_baseline(
    cx: &mut dyn LayoutOps,
    children: &[RNode],
    child_size: Size,
    dy: f64,
) -> Option<f64> {
    let &c = children.first()?;
    Some(cx.baseline_of(c, child_size)? + dy)
}

/// Single-child pass-through (root, wrappers, group fallback): top-leading.
pub struct PassThrough;

impl Layout for PassThrough {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        match children.first() {
            Some(&c) => cx.measure_child(c, p),
            None => Size::ZERO,
        }
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            let s = cx.measure_child(c, Proposal::exact(bounds.size));
            cx.place_child(c, Rect::from_size(s));
        }
    }
}

/// A recycled tree cell's anchor layout (docs/tree.md): row content hugs its own height, so
/// center it in the cell's fixed row height; [`PassThrough`] pins it to the top, which
/// reads as a misaligned row whenever the content is shorter than the row (a 16pt label in
/// a 28pt row sat 6pt high). Width still fills; a content taller than the row clamps to it.
pub struct CellCenter;

impl Layout for CellCenter {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        match children.first() {
            Some(&c) => cx.measure_child(c, p),
            None => Size::ZERO,
        }
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            let s = cx.measure_child(c, Proposal::exact(bounds.size));
            let y = ((bounds.size.height - s.height) / 2.0).max(0.0);
            cx.place_child(
                c,
                Rect::new(0.0, y, s.width, s.height.min(bounds.size.height)),
            );
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        PassThrough::forward(cx, children, size)
    }
}

/// A list cell's anchor layout (docs/list.md "Row metrics"): the row sits inside the platform's
/// row margins and the cell is at least the platform's row height, content centered in it.
/// With zero metrics it is [`PassThrough`].
pub struct CellMetrics(pub day_spec::RowMetrics);

impl CellMetrics {
    /// The metrics for `child`. The platform's own whole row keeps the list's side margins
    /// (they line up with the separators) and sizes its height itself.
    fn for_child(&self, cx: &dyn LayoutOps, child: RNode) -> day_spec::RowMetrics {
        // Through the single-child wrappers a row's decorators and builder add around it.
        let mut node = child;
        for _ in 0..8 {
            if cx.flex_of(node).whole_row {
                let i = self.0.insets;
                return day_spec::RowMetrics {
                    insets: day_spec::Insets {
                        top: 0.0,
                        leading: i.leading,
                        bottom: 0.0,
                        trailing: i.trailing,
                    },
                    min_height: 0.0,
                };
            }
            match cx.children_of(node)[..] {
                [only] => node = only,
                _ => break,
            }
        }
        self.0
    }
}

impl Layout for CellMetrics {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let Some(&c) = children.first() else {
            return Size::ZERO;
        };
        let m = self.for_child(cx, c);
        let i = m.insets;
        let (dx, dy) = (i.leading + i.trailing, i.top + i.bottom);
        let s = cx.measure_child(
            c,
            Proposal::new(
                p.width.map(|w| (w - dx).max(0.0)),
                p.height.map(|h| (h - dy).max(0.0)),
            ),
        );
        Size::new(s.width + dx, (s.height + dy).max(m.min_height))
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        let Some(&c) = children.first() else {
            return;
        };
        let i = self.for_child(cx, c).insets;
        let inner = Size::new(
            (bounds.size.width - i.leading - i.trailing).max(0.0),
            (bounds.size.height - i.top - i.bottom).max(0.0),
        );
        let s = cx.measure_child(c, Proposal::new(Some(inner.width), None));
        // Inside the margins where the row has room for them; a fixed-height row too short for
        // them (`RowHeight::Uniform`) centers its content in the whole row instead of clipping.
        let (y, h) = if s.height <= inner.height {
            (i.top + (inner.height - s.height) / 2.0, s.height)
        } else {
            let h = s.height.min(bounds.size.height);
            (((bounds.size.height - h) / 2.0).max(0.0), h)
        };
        cx.place_child(c, Rect::new(i.leading, y, inner.width, h));
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        PassThrough::forward(cx, children, size)
    }
}

impl PassThrough {
    /// Shared by every wrapper whose child measures at the wrapper's own size.
    fn forward(cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        let &c = children.first()?;
        let cs = cx.measure_child(c, Proposal::exact(size));
        wrapper_baseline(cx, children, cs, 0.0)
    }
}

/// Paint/clip wrapper (`.background`, `.corner_radius`, the animatable layers): like
/// [`PassThrough`] for measurement, but the granted rect flows to the child verbatim at place
/// time. These containers exist to paint or clip the area the parent granted, so a grow
/// stretch above them must reach the painted surface (the grid-cell card case) instead of
/// being re-hugged at every wrapper. Under a parent that places at measured size (every
/// stack, grid rigid cell, and overlay) bounds equal the measure and nothing changes.
pub struct FillThrough;

impl Layout for FillThrough {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        match children.first() {
            Some(&c) => cx.measure_child(c, p),
            None => Size::ZERO,
        }
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            cx.place_child(c, Rect::from_size(bounds.size));
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        wrapper_baseline(cx, children, size, 0.0)
    }
}

/// Native leaf: measurement delegates to the toolkit.
pub struct LeafLayout;

impl Layout for LeafLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, _children: &[RNode], p: Proposal) -> Size {
        cx.measure_leaf(p)
    }
    fn place(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], _bounds: Rect) {}
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Axis {
    Vertical,
    Horizontal,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CrossAlign {
    Leading,
    #[default]
    Center,
    Trailing,
    /// Align children on their first text baseline rather than on their boxes
    /// (docs/baseline.md). Horizontal rows only: a column's children are stacked along the
    /// axis a baseline lives on, so there is nothing to align. A child whose toolkit reports no
    /// baseline is centered, so a row of text and an image still looks right.
    FirstBaseline,
}

impl CrossAlign {
    /// Placement fraction of the free space: 0 = leading, 0.5 = center, 1 = trailing.
    fn fraction(self) -> f64 {
        match self {
            CrossAlign::Leading => 0.0,
            // No baseline to align to at this level (a column, or every child reported None):
            // centering is the fallback the variant promises.
            CrossAlign::Center | CrossAlign::FirstBaseline => 0.5,
            CrossAlign::Trailing => 1.0,
        }
    }
}

/// Column/row negotiation (§7.2): rigid children first, remaining main-axis space divided
/// among flexible children; `spacer()` is maximally flexible; group anchors expand inline.
pub struct StackLayout {
    pub axis: Axis,
    pub spacing: f64,
    pub align: CrossAlign,
}

impl StackLayout {
    fn main(&self, s: Size) -> f64 {
        match self.axis {
            Axis::Vertical => s.height,
            Axis::Horizontal => s.width,
        }
    }
    fn cross(&self, s: Size) -> f64 {
        match self.axis {
            Axis::Vertical => s.width,
            Axis::Horizontal => s.height,
        }
    }
    fn mk(&self, main: f64, cross: f64) -> Size {
        match self.axis {
            Axis::Vertical => Size::new(cross, main),
            Axis::Horizontal => Size::new(main, cross),
        }
    }
    fn split(&self, p: Proposal) -> (Option<f64>, Option<f64>) {
        match self.axis {
            Axis::Vertical => (p.height, p.width),
            Axis::Horizontal => (p.width, p.height),
        }
    }
    fn proposal(&self, main: Option<f64>, cross: Option<f64>) -> Proposal {
        match self.axis {
            Axis::Vertical => Proposal::new(cross, main),
            Axis::Horizontal => Proposal::new(main, cross),
        }
    }
    fn grows_main(&self, f: Flex) -> bool {
        f.is_spacer
            || match self.axis {
                Axis::Vertical => f.grow_h,
                Axis::Horizontal => f.grow_w,
            }
    }
    /// Sized after the rigid children, against what they leave, without growing the stack
    /// (`Flex::yields_h`, a column only).
    fn yields_main(&self, f: Flex) -> bool {
        self.axis == Axis::Vertical && f.yields_h && !self.grows_main(f)
    }
    fn grows_cross(&self, f: Flex) -> bool {
        match self.axis {
            Axis::Vertical => f.grow_w,
            Axis::Horizontal => f.grow_h,
        }
    }

    fn flatten(cx: &mut dyn LayoutOps, children: &[RNode], out: &mut Vec<RNode>) {
        for &c in children {
            if cx.flex_of(c).is_group {
                let inner = cx.children_of(c);
                Self::flatten(cx, &inner, out);
            } else {
                out.push(c);
            }
        }
    }

    fn negotiate(&self, cx: &mut dyn LayoutOps, kids: &[RNode], p: Proposal) -> Vec<Size> {
        let (main_p, cross_p) = self.split(p);
        let mut sizes = vec![Size::ZERO; kids.len()];
        let mut flex_idx = Vec::new();
        let mut yield_idx = Vec::new();
        let mut rigid_main = 0.0;
        for (i, &k) in kids.iter().enumerate() {
            let f = cx.flex_of(k);
            if self.grows_main(f) {
                flex_idx.push(i);
            } else if self.yields_main(f) {
                yield_idx.push(i);
            } else {
                let s = cx.measure_child(k, self.proposal(None, cross_p));
                rigid_main += self.main(s);
                sizes[i] = s;
            }
        }
        // Shrink pass: when the rigid children's natural sizes overflow a bounded main axis,
        // re-measure the overflowing ones against the space that is left (in order, so
        // earlier children keep their natural size first). Content that fits keeps its natural
        // measure, so proposal-expanding kinds (text fields, lists) don't balloon; wrapping
        // content (a capped message bubble, a long label) folds instead of spilling out of the
        // stack.
        if let Some(mp) = main_p {
            let spacing_total = self.spacing * (kids.len().saturating_sub(1)) as f64;
            let available = (mp - spacing_total).max(0.0);
            if rigid_main > available {
                let mut budget = available;
                rigid_main = 0.0;
                for (i, &k) in kids.iter().enumerate() {
                    let f = cx.flex_of(k);
                    if self.grows_main(f) || self.yields_main(f) {
                        continue;
                    }
                    if self.main(sizes[i]) > budget {
                        sizes[i] = cx.measure_child(k, self.proposal(Some(budget), cross_p));
                    }
                    let m = self.main(sizes[i]);
                    rigid_main += m;
                    budget = (budget - m).max(0.0);
                }
            }
        }
        // Yielding children, in order, against what the rigid ones (and any yielding one before
        // them) left: a capped scroll shrinks before the button under it is pushed out.
        for &i in &yield_idx {
            let spacing_total = self.spacing * (kids.len().saturating_sub(1)) as f64;
            let left = main_p.map(|mp| (mp - rigid_main - spacing_total).max(0.0));
            sizes[i] = cx.measure_child(kids[i], self.proposal(left, cross_p));
            rigid_main += self.main(sizes[i]);
        }
        if !flex_idx.is_empty() {
            let spacing_total = self.spacing * (kids.len().saturating_sub(1)) as f64;
            match main_p {
                Some(mp) => {
                    let remaining = (mp - rigid_main - spacing_total).max(0.0);
                    let share = remaining / flex_idx.len() as f64;
                    for &i in &flex_idx {
                        let f = cx.flex_of(kids[i]);
                        sizes[i] = if f.is_spacer {
                            self.mk(share, 0.0)
                        } else {
                            cx.measure_child(kids[i], self.proposal(Some(share), cross_p))
                        };
                    }
                }
                None => {
                    for &i in &flex_idx {
                        let f = cx.flex_of(kids[i]);
                        sizes[i] = if f.is_spacer {
                            Size::ZERO
                        } else {
                            cx.measure_child(kids[i], self.proposal(None, cross_p))
                        };
                    }
                }
            }
        }
        sizes
    }

    /// Each child's first baseline at the size it settled on, or `None` where it has none
    /// (docs/baseline.md). Only a horizontal row aligns on baselines; down a column the
    /// baselines sit on the stacking axis, where aligning them would pile the children up.
    fn baselines(
        &self,
        cx: &mut dyn LayoutOps,
        kids: &[RNode],
        sizes: &[Size],
    ) -> Vec<Option<f64>> {
        if self.align != CrossAlign::FirstBaseline || self.axis != Axis::Horizontal {
            return vec![None; kids.len()];
        }
        kids.iter()
            .zip(sizes)
            .map(|(&k, &s)| cx.baseline_of(k, s))
            .collect()
    }

    /// The row's own cross-axis extent once its children are baseline-shifted: the deepest
    /// baseline plus the deepest descent below one. A row measured at the tallest child's
    /// height would clip whichever child got shifted furthest down.
    fn baseline_cross_extent(&self, baselines: &[Option<f64>], sizes: &[Size]) -> Option<f64> {
        let deepest = baselines.iter().flatten().copied().fold(f64::MIN, f64::max);
        if deepest == f64::MIN {
            return None;
        }
        let below = baselines
            .iter()
            .zip(sizes)
            .filter_map(|(b, s)| b.map(|b| self.cross(*s) - b))
            .fold(0.0, f64::max);
        Some(deepest + below)
    }
}

impl Layout for StackLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let mut kids = Vec::new();
        Self::flatten(cx, children, &mut kids);
        if kids.is_empty() {
            return Size::ZERO;
        }
        let (main_p, cross_p) = self.split(p);
        let sizes = self.negotiate(cx, &kids, p);
        let spacing_total = self.spacing * (kids.len() - 1) as f64;
        let has_flex = kids.iter().any(|&k| self.grows_main(cx.flex_of(k)));
        // A stack with a stretching child fills what it is offered, but never reports less
        // than its children need together (a `.min_width`, fixed-size pairs), so an overflow
        // is visible to the parent (a `labeled` row stacks) instead of being clipped.
        let needed = sizes.iter().map(|&s| self.main(s)).sum::<f64>() + spacing_total;
        let main_total = match main_p {
            Some(mp) if has_flex => mp.max(needed),
            _ => needed,
        };
        let grows_cross = kids.iter().any(|&k| self.grows_cross(cx.flex_of(k)));
        let tallest = sizes.iter().map(|&s| self.cross(s)).fold(0.0, f64::max);
        // Baseline shifting can push a child below the tallest one's bottom edge, so the row
        // takes the extent the shifted children actually occupy (docs/baseline.md).
        let stacked = self
            .baseline_cross_extent(&self.baselines(cx, &kids, &sizes), &sizes)
            .unwrap_or(0.0)
            .max(tallest);
        let cross_total = match cross_p {
            Some(cp) if grows_cross => cp,
            _ => stacked,
        };
        self.mk(main_total, cross_total)
    }

    /// A stack's own baseline is its first baseline-bearing child's, moved by where that child
    /// sits: down a column that is the child's offset along the axis, across a row every
    /// aligned child already shares the deepest baseline (docs/baseline.md).
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        let mut kids = Vec::new();
        Self::flatten(cx, children, &mut kids);
        if kids.is_empty() {
            return None;
        }
        let sizes = self.negotiate(cx, &kids, Proposal::exact(size));
        match self.axis {
            Axis::Horizontal => {
                let baselines: Vec<Option<f64>> = kids
                    .iter()
                    .zip(&sizes)
                    .map(|(&k, &s)| cx.baseline_of(k, s))
                    .collect();
                let deepest = baselines.iter().flatten().copied().fold(f64::MIN, f64::max);
                (deepest != f64::MIN).then_some(deepest)
            }
            Axis::Vertical => {
                let mut pos = 0.0;
                for (&k, &s) in kids.iter().zip(&sizes) {
                    if let Some(b) = cx.baseline_of(k, s) {
                        return Some(pos + b);
                    }
                    pos += self.main(s) + self.spacing;
                }
                None
            }
        }
    }

    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        let mut kids = Vec::new();
        Self::flatten(cx, children, &mut kids);
        if kids.is_empty() {
            return;
        }
        let sizes = self.negotiate(cx, &kids, Proposal::exact(bounds.size));
        // The children's settled extent against what this stack was actually given: rigid
        // children are free to answer a shrink proposal with their natural size (native
        // buttons and labels routinely do), and then the tail of the stack lands offscreen
        // with every assertion still green. Say so, once, where a developer will see it.
        let needed = sizes.iter().map(|&s| self.main(s)).sum::<f64>()
            + self.spacing * (kids.len() - 1) as f64;
        let available = self.main(bounds.size);
        // A container with no extent is not overflowing, it is not laid out yet: the state a
        // native-owned surface is in until the backend reports its frame (a `cover` lays its
        // content out once before `Event::FrameChanged` arrives, §7.6/docs/cover.md). Reporting
        // it would name a real developer's ids for a transient the developer cannot act on:
        // no fit policy makes a stack fit in zero points.
        if available > 0.5 && needed > available + 0.5 {
            cx.report_overflow(needed, available);
        }
        let bounds_cross = self.cross(bounds.size);
        // Baseline row (docs/baseline.md): every child that has a baseline is shifted down so
        // they all land on the deepest one, which is the offset that keeps every child inside
        // the row. Children without one keep the centered fallback below.
        let baselines = self.baselines(cx, &kids, &sizes);
        let deepest = baselines.iter().flatten().copied().fold(f64::MIN, f64::max);
        let mut pos = 0.0;
        for (i, &k) in kids.iter().enumerate() {
            let s = sizes[i];
            let cross_off = match (self.align, baselines[i]) {
                (CrossAlign::FirstBaseline, Some(b)) => deepest - b,
                (CrossAlign::Leading, _) => 0.0,
                (CrossAlign::Center | CrossAlign::FirstBaseline, _) => {
                    ((bounds_cross - self.cross(s)) / 2.0).max(0.0)
                }
                (CrossAlign::Trailing, _) => (bounds_cross - self.cross(s)).max(0.0),
            };
            let rect = match self.axis {
                Axis::Vertical => Rect::new(cross_off, pos, s.width, s.height),
                Axis::Horizontal => Rect::new(pos, cross_off, s.width, s.height),
            };
            cx.place_child(k, rect);
            pos += self.main(s) + self.spacing;
        }
    }
}

/// A wrapping row (docs/size-classes.md "Adaptive pieces"): children keep their natural
/// measure and lay out leading-to-trailing, breaking onto a new line where the next child
/// would overflow the proposed width. Wrapping replaces main-axis negotiation, so flex is
/// inert here: nothing grows, and a `spacer()` contributes nothing. RTL mirroring comes from
/// `place_child`, the same as every layout.
pub struct FlowLayout {
    pub spacing: f64,
    /// Vertical gap between lines.
    pub run_spacing: f64,
    /// How children align within their own line (a line is as tall as its tallest child).
    pub align: CrossAlign,
    /// Uniform columns ([`RowFit::WrapColumns`](/api/day_pieces/enum.RowFit.html#variant.WrapColumns)): every cell takes the widest child's width and
    /// each line holds as many as the available width fits, so the wrapped lines align into
    /// columns; when any child grows (`grow_w`), the columns stretch to share the whole width.
    /// `false` packs each line at natural widths, which comes out ragged.
    pub uniform: bool,
}

/// One packed flow line: the child range it holds and its settled extent.
struct FlowLine {
    start: usize,
    end: usize,
    height: f64,
}

/// A solved flow: the size each child is placed at, and the lines they pack into. Measure and
/// place both go through this, so the two never disagree about where a line breaks.
struct FlowPlan {
    sizes: Vec<Size>,
    lines: Vec<FlowLine>,
}

impl FlowLayout {
    /// Pack natural sizes into lines under `max_w`. Unbounded → one line, the degenerate case
    /// that keeps a flow inside a horizontal scroll behaving like a plain row.
    fn pack(&self, sizes: &[Size], max_w: Option<f64>) -> Vec<FlowLine> {
        let mut lines = Vec::new();
        let mut start = 0;
        let mut w = 0.0;
        let mut h: f64 = 0.0;
        for (i, s) in sizes.iter().enumerate() {
            let grown = if i == start {
                s.width
            } else {
                w + self.spacing + s.width
            };
            if i > start && max_w.is_some_and(|m| grown > m + 0.5) {
                lines.push(FlowLine {
                    start,
                    end: i,
                    height: h,
                });
                start = i;
                w = s.width;
                h = s.height;
            } else {
                w = grown;
                h = h.max(s.height);
            }
        }
        if start < sizes.len() {
            lines.push(FlowLine {
                start,
                end: sizes.len(),
                height: h,
            });
        }
        lines
    }

    fn natural_sizes(cx: &mut dyn LayoutOps, kids: &[RNode]) -> Vec<Size> {
        kids.iter()
            .map(|&k| cx.measure_child(k, Proposal::UNCONSTRAINED))
            .collect()
    }

    /// Solve the flow for an available width: ragged lines at natural widths, or, under
    /// `uniform`, one column width for every cell with as many columns per line as fit.
    fn plan(&self, cx: &mut dyn LayoutOps, kids: &[RNode], max_w: Option<f64>) -> FlowPlan {
        let natural = Self::natural_sizes(cx, kids);
        if !self.uniform {
            let lines = self.pack(&natural, max_w);
            return FlowPlan {
                sizes: natural,
                lines,
            };
        }
        // The widest child sets the column, so no cell is ever narrower than its content.
        let cell_w = natural.iter().map(|s| s.width).fold(0.0, f64::max);
        let cols = match max_w {
            // How many whole columns fit, counting the gutter between them; never zero, or a
            // window narrower than one cell would produce no lines at all.
            Some(m) if cell_w > 0.0 => {
                (((m + self.spacing) / (cell_w + self.spacing)).floor() as usize).max(1)
            }
            // Unbounded (inside a horizontal scroll): one line, like a plain row.
            _ => kids.len().max(1),
        };
        // A growing child (`.grow_w()`) stretches the columns to share the whole line, the
        // adaptive grid: `cell_w` is then the narrowest a column gets, and a line with fewer
        // cells than columns leaves the rest empty. Without one, the leftover width trails the
        // last column.
        let col_w = match max_w {
            Some(m) if kids.iter().any(|&k| cx.flex_of(k).grow_w) => {
                ((m - self.spacing * (cols - 1) as f64) / cols as f64).max(cell_w)
            }
            _ => cell_w,
        };
        // Never wider than the line: a child whose natural width exceeds it (a card whose text
        // runs long on a phone) takes the line's width and wraps its text, rather than running
        // past the edge.
        let col_w = max_w.map_or(col_w, |m| col_w.min(m));
        // Re-measure at the column width: a child handed more width than it asked for can
        // settle shorter (a label unwrapping), and the line's height follows what it settles
        // on rather than what it wanted.
        let sizes: Vec<Size> = kids
            .iter()
            .map(|&k| {
                let s = cx.measure_child(k, Proposal::new(Some(col_w), None));
                Size::new(col_w, s.height)
            })
            .collect();
        let mut lines = Vec::new();
        let mut start = 0;
        while start < sizes.len() {
            let end = (start + cols).min(sizes.len());
            let height = sizes[start..end]
                .iter()
                .map(|s| s.height)
                .fold(0.0, f64::max);
            lines.push(FlowLine { start, end, height });
            start = end;
        }
        FlowPlan { sizes, lines }
    }
}

impl Layout for FlowLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let mut kids = Vec::new();
        StackLayout::flatten(cx, children, &mut kids);
        if kids.is_empty() {
            return Size::ZERO;
        }
        let plan = self.plan(cx, &kids, p.width);
        let height = plan.lines.iter().map(|l| l.height).sum::<f64>()
            + self.run_spacing * plan.lines.len().saturating_sub(1) as f64;
        // Bounded → fill the proposal (lines pack leading inside it, like wrapped text);
        // unbounded → the single packed line's own extent.
        let width = p.width.unwrap_or_else(|| {
            plan.sizes.iter().map(|s| s.width).sum::<f64>()
                + self.spacing * plan.sizes.len().saturating_sub(1) as f64
        });
        Size::new(width, height)
    }

    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        let mut kids = Vec::new();
        StackLayout::flatten(cx, children, &mut kids);
        if kids.is_empty() {
            return;
        }
        let plan = self.plan(cx, &kids, Some(bounds.size.width));
        let mut y = 0.0;
        for line in &plan.lines {
            let mut x = 0.0;
            let range = line.start..line.end;
            for (&k, &s) in kids[range.clone()].iter().zip(&plan.sizes[range]) {
                let dy = ((line.height - s.height) * self.align.fraction()).max(0.0);
                cx.place_child(k, Rect::new(x, y + dy, s.width, s.height));
                x += s.width + self.spacing;
            }
            y += line.height + self.run_spacing;
        }
    }
}

/// A grid cell, resolved from the realized tree: the node, its starting column, its span, and
/// its layout facts (docs/grid.md).
struct GridCell {
    node: RNode,
    col: usize,
    span: usize,
    flex: Flex,
}

/// One grid row: either a `grid_row`'s cells, or a single full-width cell (a non-row child).
struct GridRowRef {
    cells: Vec<GridCell>,
    full_width: bool,
    valign: Option<CrossAlign>,
}

/// The resolved geometry both [`GridLayout::measure`] and [`GridLayout::place`] work from.
struct GridGeom {
    rows: Vec<GridRowRef>,
    col_x: Vec<f64>,
    col_w: Vec<f64>,
    row_y: Vec<f64>,
    row_h: Vec<f64>,
    /// Pass-B size per cell, indexed `[row][cell]`; placement uses these, never re-measuring.
    cell_sizes: Vec<Vec<Size>>,
    size: Size,
}

/// SwiftUI-style grid negotiation (docs/grid.md): columns are inferred from `grid_row` children,
/// a column's width is the max ideal width of its span-1 cells, a `grow_w` cell makes its column
/// flexible (leftover width split evenly, the [`StackLayout`] share rule), and a non-row child
/// is a full-width cell spanning every column. `spacer()` is an inert empty cell. The contract:
/// exactly two measure proposals per cell per layout, unconstrained (pass A) and at the final
/// column width (pass B), and `place` re-runs the same proposals, so it measures from cache.
pub struct GridLayout {
    pub row_spacing: f64,
    pub column_spacing: f64,
    pub align: Alignment,
}

impl GridLayout {
    /// Expand group anchors and classify children into rows / full-width cells.
    fn collect(&self, cx: &mut dyn LayoutOps, children: &[RNode]) -> (Vec<GridRowRef>, usize) {
        let mut tops = Vec::new();
        StackLayout::flatten(cx, children, &mut tops);
        let mut rows = Vec::new();
        let mut ncols = 0usize;
        for &t in &tops {
            let f = cx.flex_of(t);
            if f.grid.is_row {
                let inner = cx.children_of(t);
                let mut cell_nodes = Vec::new();
                StackLayout::flatten(cx, &inner, &mut cell_nodes);
                let mut cells = Vec::new();
                let mut col = 0usize;
                for &c in &cell_nodes {
                    let cf = cx.flex_of(c);
                    let span = cf.grid.col_span.max(1) as usize;
                    cells.push(GridCell {
                        node: c,
                        col,
                        span,
                        flex: cf,
                    });
                    col += span;
                }
                ncols = ncols.max(col);
                rows.push(GridRowRef {
                    cells,
                    full_width: false,
                    valign: f.grid.row_valign,
                });
            } else {
                // A non-row child occupies a full-width row spanning every column (span is
                // patched to the final column count once it is known).
                rows.push(GridRowRef {
                    cells: vec![GridCell {
                        node: t,
                        col: 0,
                        span: 1,
                        flex: f,
                    }],
                    full_width: true,
                    valign: None,
                });
            }
        }
        (rows, ncols)
    }

    fn span_width(&self, col_w: &[f64], col: usize, span: usize) -> f64 {
        let end = (col + span).min(col_w.len());
        let cols: f64 = col_w[col..end].iter().sum();
        cols + self.column_spacing * (end.saturating_sub(col + 1)) as f64
    }

    /// The single geometry pass (docs/grid.md): pass A (unconstrained ideals → column widths),
    /// pass B (heights at final widths), then prefix sums. All decisions are closed-form (no
    /// iterative negotiation) and only `p.width` affects cell proposals, so a `measure` at
    /// `(Some(w), None)` and a `place` at `exact(w × h)` generate identical per-cell proposals.
    fn geometry(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> GridGeom {
        let (mut rows, ncols) = self.collect(cx, children);
        if rows.is_empty() {
            return GridGeom {
                rows,
                col_x: Vec::new(),
                col_w: Vec::new(),
                row_y: Vec::new(),
                row_h: Vec::new(),
                cell_sizes: Vec::new(),
                size: Size::ZERO,
            };
        }
        let ncols = ncols.max(1);
        for r in &mut rows {
            if r.full_width {
                r.cells[0].span = ncols;
            }
        }
        let unconstrained = Proposal::new(None, None);

        // Pass A1, span-1 cells: rigid ideals set their column's width; grow_w flags it
        // flexible (its unconstrained ideal only matters when the grid itself is unconstrained).
        let mut col_ideal = vec![0.0f64; ncols];
        let mut col_flex = vec![false; ncols];
        let mut flex_ideal = vec![0.0f64; ncols];
        for r in rows.iter().filter(|r| !r.full_width) {
            for c in r.cells.iter().filter(|c| c.span == 1 && !c.flex.is_spacer) {
                if c.flex.grow_w {
                    col_flex[c.col] = true;
                    if p.width.is_none() {
                        let s = cx.measure_child(c.node, unconstrained);
                        flex_ideal[c.col] = flex_ideal[c.col].max(s.width);
                    }
                } else {
                    let s = cx.measure_child(c.node, unconstrained);
                    col_ideal[c.col] = col_ideal[c.col].max(s.width);
                }
            }
        }
        // Pass A2: flexible spanning cells only flag their columns…
        for r in rows.iter().filter(|r| !r.full_width) {
            for c in r.cells.iter().filter(|c| c.span > 1 && !c.flex.is_spacer) {
                if c.flex.grow_w {
                    let end = (c.col + c.span).min(ncols);
                    col_flex[c.col..end].fill(true);
                }
            }
        }
        // …pass A3: then rigid spanning cells distribute any width deficit in one shot: onto
        // the spanned flexible columns if any (they absorb width anyway), else evenly.
        for r in rows.iter().filter(|r| !r.full_width) {
            for c in r.cells.iter().filter(|c| c.span > 1 && !c.flex.is_spacer) {
                if c.flex.grow_w {
                    continue;
                }
                let s = cx.measure_child(c.node, unconstrained);
                let end = (c.col + c.span).min(ncols);
                let avail = self.span_width(&col_ideal, c.col, c.span);
                let deficit = s.width - avail;
                if deficit > 0.0 {
                    let flexed: Vec<usize> = (c.col..end).filter(|&k| col_flex[k]).collect();
                    let targets = if flexed.is_empty() {
                        (c.col..end).collect()
                    } else {
                        flexed
                    };
                    let add = deficit / targets.len() as f64;
                    for k in targets {
                        col_ideal[k] += add;
                    }
                }
            }
        }
        // Full-width cells: their ideal widens the grid when nothing constrains it; a grow_w
        // full-width cell makes the grid width flexible like a flexible column does.
        let mut fw_ideal = 0.0f64;
        let mut fw_flex = false;
        for r in rows.iter().filter(|r| r.full_width) {
            let c = &r.cells[0];
            if c.flex.is_spacer {
                continue;
            }
            if c.flex.grow_w {
                fw_flex = true;
            } else {
                let s = cx.measure_child(c.node, unconstrained);
                fw_ideal = fw_ideal.max(s.width);
            }
        }

        // Resolve column widths (the StackLayout::negotiate share rule for flexible columns).
        let gutters = self.column_spacing * (ncols - 1) as f64;
        let has_flex_col = col_flex.iter().any(|&f| f);
        let mut col_w = vec![0.0f64; ncols];
        match p.width {
            Some(pw) if has_flex_col => {
                let rigid: f64 = (0..ncols)
                    .filter(|&k| !col_flex[k])
                    .map(|k| col_ideal[k])
                    .sum();
                let share = (pw - rigid - gutters).max(0.0)
                    / col_flex.iter().filter(|&&f| f).count() as f64;
                for k in 0..ncols {
                    col_w[k] = if col_flex[k] { share } else { col_ideal[k] };
                }
            }
            _ => {
                for k in 0..ncols {
                    col_w[k] = if col_flex[k] {
                        flex_ideal[k].max(col_ideal[k])
                    } else {
                        col_ideal[k]
                    };
                }
            }
        }
        let cols_total: f64 = col_w.iter().sum::<f64>() + gutters;
        let grid_w = match p.width {
            Some(pw) if has_flex_col || fw_flex => pw,
            _ => cols_total.max(fw_ideal),
        };

        // Pass B: heights at final widths (text height-for-width happens here).
        let nrows = rows.len();
        let mut cell_sizes: Vec<Vec<Size>> = Vec::with_capacity(nrows);
        let mut row_h = vec![0.0f64; nrows];
        let mut row_flex = vec![false; nrows];
        for (ri, r) in rows.iter().enumerate() {
            let mut sizes = Vec::with_capacity(r.cells.len());
            for c in &r.cells {
                if c.flex.is_spacer {
                    sizes.push(Size::ZERO);
                    continue;
                }
                let w = if r.full_width {
                    grid_w
                } else {
                    self.span_width(&col_w, c.col, c.span)
                };
                let s = cx.measure_child(c.node, Proposal::new(Some(w), None));
                row_h[ri] = row_h[ri].max(s.height);
                if c.flex.grow_h {
                    row_flex[ri] = true;
                }
                sizes.push(s);
            }
            cell_sizes.push(sizes);
        }
        // Flexible rows stretch to a height proposal (additive: this never re-measures cells,
        // which keeps measure/place proposal identity).
        let vgutters = self.row_spacing * (nrows - 1) as f64;
        if let Some(ph) = p.height
            && row_flex.iter().any(|&f| f)
        {
            let total: f64 = row_h.iter().sum::<f64>() + vgutters;
            let extra = (ph - total).max(0.0) / row_flex.iter().filter(|&&f| f).count() as f64;
            for (ri, flexed) in row_flex.iter().enumerate() {
                if *flexed {
                    row_h[ri] += extra;
                }
            }
        }
        let grid_h = row_h.iter().sum::<f64>() + vgutters;

        let mut col_x = vec![0.0f64; ncols];
        let mut x = 0.0;
        for k in 0..ncols {
            col_x[k] = x;
            x += col_w[k] + self.column_spacing;
        }
        let mut row_y = vec![0.0f64; nrows];
        let mut y = 0.0;
        for ri in 0..nrows {
            row_y[ri] = y;
            y += row_h[ri] + self.row_spacing;
        }
        GridGeom {
            rows,
            col_x,
            col_w,
            row_y,
            row_h,
            cell_sizes,
            size: Size::new(grid_w, grid_h),
        }
    }
}

impl Layout for GridLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        self.geometry(cx, children, p).size
    }

    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        let g = self.geometry(cx, children, Proposal::exact(bounds.size));
        for (ri, r) in g.rows.iter().enumerate() {
            for (ci, c) in r.cells.iter().enumerate() {
                if c.flex.is_spacer {
                    continue;
                }
                let cell = if r.full_width {
                    Rect::new(0.0, g.row_y[ri], g.size.width, g.row_h[ri])
                } else {
                    Rect::new(
                        g.col_x[c.col],
                        g.row_y[ri],
                        self.span_width(&g.col_w, c.col, c.span),
                        g.row_h[ri],
                    )
                };
                let s = g.cell_sizes[ri][ci];
                let w = if c.flex.grow_w {
                    cell.size.width
                } else {
                    s.width.min(cell.size.width)
                };
                let h = if c.flex.grow_h {
                    cell.size.height
                } else {
                    s.height.min(cell.size.height)
                };
                // Alignment precedence per axis: cell `.grid_align` > row `.align` (vertical
                // only) > the grid's own alignment.
                let hf = match c.flex.grid.align {
                    Some(a) => a.h_fraction(),
                    None => self.align.h_fraction(),
                };
                let vf = match (c.flex.grid.align, r.valign) {
                    (Some(a), _) => a.v_fraction(),
                    (None, Some(v)) => v.fraction(),
                    (None, None) => self.align.v_fraction(),
                };
                let x = cell.origin.x + (cell.size.width - w) * hf;
                let y = cell.origin.y + (cell.size.height - h) * vf;
                cx.place_child(c.node, Rect::new(x, y, w, h));
            }
        }
        // Row nodes are transparent carriers and are never placed.
    }
}

/// Two-axis placement of a child within a container's bounds (SwiftUI's `Alignment`). Used by
/// the z-layering primitives ([`OverlayLayout`]): `zstack`, `overlay`/`overlay_aligned`.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Alignment {
    TopLeading,
    Top,
    TopTrailing,
    Leading,
    #[default]
    Center,
    Trailing,
    BottomLeading,
    Bottom,
    BottomTrailing,
}

impl Alignment {
    /// Horizontal placement fraction of the free space: 0 = leading, 0.5 = center, 1 = trailing.
    fn h_fraction(self) -> f64 {
        match self {
            Alignment::TopLeading | Alignment::Leading | Alignment::BottomLeading => 0.0,
            Alignment::Top | Alignment::Center | Alignment::Bottom => 0.5,
            Alignment::TopTrailing | Alignment::Trailing | Alignment::BottomTrailing => 1.0,
        }
    }
    /// Vertical placement fraction of the free space: 0 = top, 0.5 = center, 1 = bottom.
    fn v_fraction(self) -> f64 {
        match self {
            Alignment::TopLeading | Alignment::Top | Alignment::TopTrailing => 0.0,
            Alignment::Leading | Alignment::Center | Alignment::Trailing => 0.5,
            Alignment::BottomLeading | Alignment::Bottom | Alignment::BottomTrailing => 1.0,
        }
    }
}

/// Z-layering (§overlay): children share the container bounds, stacked back-to-front in child
/// order (first child = bottom of the z-order), each positioned by a single [`Alignment`].
/// `size_to_first` reports only the first child's natural size, the badge/annotation sizing of
/// [`overlay`](crate) (the annotation does not grow the frame); otherwise the layout reports the
/// union (max) of all children's natural sizes, the ZStack sizing of `zstack`. No native work:
/// the container is the same panel as `column`/`row`, so backends stack children by attach order.
pub struct OverlayLayout {
    pub align: Alignment,
    pub size_to_first: bool,
}

impl OverlayLayout {
    /// Expand group anchors (`when`/`each`) inline, exactly like [`StackLayout`].
    fn flatten(cx: &mut dyn LayoutOps, children: &[RNode], out: &mut Vec<RNode>) {
        for &c in children {
            if cx.flex_of(c).is_group {
                let inner = cx.children_of(c);
                Self::flatten(cx, &inner, out);
            } else {
                out.push(c);
            }
        }
    }
}

impl Layout for OverlayLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let mut kids = Vec::new();
        Self::flatten(cx, children, &mut kids);
        if self.size_to_first {
            return match kids.first() {
                Some(&c) => cx.measure_child(c, p),
                None => Size::ZERO,
            };
        }
        let mut size = Size::ZERO;
        for &c in &kids {
            let s = cx.measure_child(c, p);
            size.width = size.width.max(s.width);
            size.height = size.height.max(s.height);
        }
        size
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        let mut kids = Vec::new();
        Self::flatten(cx, children, &mut kids);
        for &c in &kids {
            let s = cx.measure_child(c, Proposal::exact(bounds.size));
            let x = (bounds.size.width - s.width) * self.align.h_fraction();
            let y = (bounds.size.height - s.height) * self.align.v_fraction();
            cx.place_child(c, Rect::new(x, y, s.width, s.height));
        }
    }
}

pub struct PaddingLayout {
    pub insets: Insets,
}

impl Layout for PaddingLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let inner = Proposal::new(
            p.width
                .map(|w| (w - self.insets.horizontal_total()).max(0.0)),
            p.height
                .map(|h| (h - self.insets.vertical_total()).max(0.0)),
        );
        let s = match children.first() {
            Some(&c) => cx.measure_child(c, inner),
            None => Size::ZERO,
        };
        Size::new(
            s.width + self.insets.horizontal_total(),
            s.height + self.insets.vertical_total(),
        )
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            let inner = bounds.inset_by(self.insets);
            let s = cx.measure_child(c, Proposal::exact(inner.size));
            cx.place_child(
                c,
                Rect {
                    origin: inner.origin,
                    size: s,
                },
            );
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        // The one wrapper with a real offset: its child starts below the top inset.
        let inner = Size::new(
            (size.width - self.insets.horizontal_total()).max(0.0),
            (size.height - self.insets.vertical_total()).max(0.0),
        );
        wrapper_baseline(cx, children, inner, self.insets.top)
    }
}

/// The `aspect_ratio` decorator (§5.2): the largest `width / height == ratio` box that fits what
/// the parent proposes, with the child given that whole box.
///
/// With one axis proposed it derives the other, which is what makes `.grow_w().aspect_ratio(r)`
/// take the width a container offers and compute the height from it: a canvas that keeps its
/// proportions as the window resizes.
pub struct AspectRatioLayout {
    pub ratio: f64,
}

impl Layout for AspectRatioLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, _children: &[RNode], p: Proposal) -> Size {
        match (p.width, p.height) {
            (Some(w), Some(h)) => {
                if w / h > self.ratio {
                    Size::new(h * self.ratio, h)
                } else {
                    Size::new(w, w / self.ratio)
                }
            }
            (Some(w), None) => Size::new(w, w / self.ratio),
            (None, Some(h)) => Size::new(h * self.ratio, h),
            (None, None) => cx.measure_leaf(p),
        }
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        // The ratio has already decided the box; hand all of it to the child.
        for &c in children {
            cx.place_child(c, Rect::from_size(bounds.size));
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        PassThrough::forward(cx, children, size)
    }
}

/// The `grow`/`grow_w`/`grow_h` decorators (§5.2): a single-child wrapper carrying grow [`Flex`]
/// so the parent stack OFFERS it the space, and a greedy measure/place so the child actually
/// FILLS it. Non-grown axes hug the child (like `frame(maxWidth: .infinity)` on one axis).
pub struct GrowLayout {
    pub w: bool,
    pub h: bool,
}

impl Layout for GrowLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let cs = match children.first() {
            Some(&c) => cx.measure_child(c, p),
            None => Size::ZERO,
        };
        // A grown axis fills what is offered, but never reports less than the child needs, so
        // a child that cannot shrink to the offer (a `.min_width`, a fixed-size pair) overflows
        // visibly and its row can respond (a `labeled` row stacks) instead of clipping it.
        Size::new(
            if self.w {
                p.width.map_or(cs.width, |w| w.max(cs.width))
            } else {
                cs.width
            },
            if self.h {
                p.height.map_or(cs.height, |h| h.max(cs.height))
            } else {
                cs.height
            },
        )
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            let cs = cx.measure_child(c, Proposal::exact(bounds.size));
            // Fill the grown axes; hug the child on the rest.
            let w = if self.w { bounds.size.width } else { cs.width };
            let h = if self.h {
                bounds.size.height
            } else {
                cs.height
            };
            cx.place_child(c, Rect::from_size(Size::new(w, h)));
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        PassThrough::forward(cx, children, size)
    }
}

/// The `.max_width(w)` decorator (docs/layout.md): proposes at most `w` to the child, so
/// text wraps instead of overflowing, while narrower content still hugs. The vertical axis
/// passes through untouched.
pub struct MaxWidthLayout {
    pub max: f64,
}

impl Layout for MaxWidthLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let capped = Proposal::new(
            Some(p.width.map(|w| w.min(self.max)).unwrap_or(self.max)),
            p.height,
        );
        match children.first() {
            Some(&c) => {
                let s = cx.measure_child(c, capped);
                Size::new(s.width.min(self.max), s.height)
            }
            None => Size::ZERO,
        }
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            let w = bounds.size.width.min(self.max);
            let s = cx.measure_child(c, Proposal::exact(Size::new(w, bounds.size.height)));
            cx.place_child(c, Rect::from_size(Size::new(s.width.min(w), s.height)));
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        PassThrough::forward(cx, children, size)
    }
}

/// The `.max_height(h)` decorator, [`MaxWidthLayout`]'s vertical twin: proposes at most `h` to
/// the child, so a scroll or anything else that takes the height it is offered stops at `h`,
/// while shorter content still hugs. Where the parent offers less than `h` (a phone held
/// sideways), the child gets that, so a capped box still fits a short screen. The horizontal
/// axis passes through untouched.
pub struct MaxHeightLayout {
    pub max: f64,
}

impl Layout for MaxHeightLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let capped = Proposal::new(
            p.width,
            Some(p.height.map(|h| h.min(self.max)).unwrap_or(self.max)),
        );
        match children.first() {
            Some(&c) => {
                let s = cx.measure_child(c, capped);
                Size::new(s.width, s.height.min(self.max))
            }
            None => Size::ZERO,
        }
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            let h = bounds.size.height.min(self.max);
            let s = cx.measure_child(c, Proposal::exact(Size::new(bounds.size.width, h)));
            cx.place_child(c, Rect::from_size(Size::new(s.width, s.height.min(h))));
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        PassThrough::forward(cx, children, size)
    }
}

/// The `.min_width(w)` decorator: the child measures as it would, but never reports narrower
/// than `w`, and is placed across whatever width the wrapper is given. A stretching control
/// (a slider) in a row that is being squeezed thereby overflows its row instead of shrinking
/// to nothing, which is what lets a `labeled` row notice and stack (docs/forms.md). The
/// vertical axis passes through untouched.
pub struct MinWidthLayout {
    pub min: f64,
}

impl Layout for MinWidthLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let floored = Proposal::new(p.width.map(|w| w.max(self.min)), p.height);
        match children.first() {
            Some(&c) => {
                let s = cx.measure_child(c, floored);
                Size::new(s.width.max(self.min), s.height)
            }
            None => Size::new(self.min, 0.0),
        }
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            let w = bounds.size.width.max(self.min);
            let s = cx.measure_child(c, Proposal::exact(Size::new(w, bounds.size.height)));
            cx.place_child(c, Rect::from_size(Size::new(w, s.height)));
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        PassThrough::forward(cx, children, size)
    }
}

/// Reserve at least the size of a sample child, then lay the content out inside it.
///
/// The problem it solves: a numeric readout beside a slider changes width as the value changes
/// (`1` is narrower than `8` in a proportional font, and `9` → `10` adds a whole glyph), so the
/// row reflows on every drag and the control you are aiming at moves. A hardcoded width "fixes"
/// that until the reader raises their accessibility text size, at which point the number is
/// clipped.
///
/// Reserving a sample measures a real piece in the real font at the real size, so the reservation
/// scales with the text exactly as the content does. `children[0]` is the sample (built
/// transparent by `Decorate::reserving`, so it never paints and is placed at zero size);
/// `children[1]` is the content, which receives the full reserved bounds and aligns itself.
pub struct ReserveLayout;

impl Layout for ReserveLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let (Some(&sample), Some(&content)) = (children.first(), children.get(1)) else {
            return children
                .first()
                .map(|&c| cx.measure_child(c, p))
                .unwrap_or(Size::ZERO);
        };
        let s = cx.measure_child(sample, p);
        let c = cx.measure_child(content, p);
        Size::new(s.width.max(c.width), s.height.max(c.height))
    }

    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        // The sample has done its work in `measure`; give it no area so it costs nothing to draw.
        if let Some(&sample) = children.first() {
            cx.place_child(sample, Rect::from_size(Size::ZERO));
        }
        if let Some(&content) = children.get(1) {
            let s = cx.measure_child(content, Proposal::exact(bounds.size));
            let _ = s;
            cx.place_child(content, Rect::from_size(bounds.size));
        }
    }
}

pub struct FrameLayout {
    pub width: Option<f64>,
    pub height: Option<f64>,
}

impl Layout for FrameLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let child_p = Proposal::new(self.width.or(p.width), self.height.or(p.height));
        let s = match children.first() {
            Some(&c) => cx.measure_child(c, child_p),
            None => Size::ZERO,
        };
        Size::new(
            self.width.unwrap_or(s.width),
            self.height.unwrap_or(s.height),
        )
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            cx.place_child(c, bounds);
        }
    }
    fn baseline(&self, cx: &mut dyn LayoutOps, children: &[RNode], size: Size) -> Option<f64> {
        PassThrough::forward(cx, children, size)
    }
}

/// Scroll viewport (§7.6): greedy on the proposal; content measured unconstrained on the
/// scroll axis and reported via `set_scroll_content`. Children are placed in the scroll's
/// content coordinate space (the scroll node is their native ancestor).
pub struct ScrollLayout {
    pub axis: Axis,
}

impl Layout for ScrollLayout {
    fn measure(&self, cx: &mut dyn LayoutOps, children: &[RNode], p: Proposal) -> Size {
        let content_p = match self.axis {
            Axis::Vertical => Proposal::new(p.width, None),
            Axis::Horizontal => Proposal::new(None, p.height),
        };
        let cs = match children.first() {
            Some(&c) => cx.measure_child(c, content_p),
            None => Size::ZERO,
        };
        Size::new(p.width.unwrap_or(cs.width), p.height.unwrap_or(cs.height))
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        if let Some(&c) = children.first() {
            let vertical = self.axis == Axis::Vertical;
            let proposal = |cross: f64| match self.axis {
                Axis::Vertical => Proposal::new(Some(cross), None),
                Axis::Horizontal => Proposal::new(None, Some(cross)),
            };
            let (cross, main) = match self.axis {
                Axis::Vertical => (bounds.size.width, bounds.size.height),
                Axis::Horizontal => (bounds.size.height, bounds.size.width),
            };
            let mut cs = cx.measure_child(c, proposal(cross));
            let overflows = |s: Size| if vertical { s.height } else { s.width } > main;
            // Content that overflows brings a scroll bar up. Where that bar is a classic one
            // taking room from the viewport (Qt's Fusion style), lay the content out beside it,
            // or the content is wider than what is left and scrolls sideways by the bar's width.
            let mut cross = cross;
            if overflows(cs) {
                let inset = cx.scroll_bar_inset(vertical);
                if inset > 0.0 {
                    cross = (cross - inset).max(0.0);
                    cs = cx.measure_child(c, proposal(cross));
                }
            }
            let content = match self.axis {
                Axis::Vertical => Size::new(cross, cs.height.max(bounds.size.height)),
                Axis::Horizontal => Size::new(cs.width.max(bounds.size.width), cross),
            };
            cx.place_child(c, Rect::from_size(content));
            cx.set_scroll_content(content);
        }
    }
}

/// Navigation host (docs/navigation.md): page frames are native-owned (splitter panes,
/// nav-controller views), so `set_frame` on pages is a toolkit no-op; Day lays each page's
/// content within the size the toolkit last reported via `Event::FrameChanged`, falling
/// back to a sidebar/detail split (or the full host) of the host bounds.
pub struct NavLayout {
    pub sizes: std::rc::Rc<std::cell::RefCell<std::collections::HashMap<RNode, Size>>>,
    /// The presentation as last resolved. Shared with the piece rather than copied, so a
    /// re-present (`NavPatch::Presentation`) changes the fallback split without rebuilding the
    /// layout object the host was realized with.
    pub presentation: std::rc::Rc<std::cell::Cell<day_spec::props::NavPresentation>>,
    /// The host's sidebar page, once it has one. Identity, not position: after a re-present the
    /// pages keep their roles but a backend may have re-homed them in a different order.
    pub sidebar: std::rc::Rc<std::cell::Cell<Option<RNode>>>,
    /// The host's content-list page (`Pane::List`), once it has one; shared like `sidebar`.
    pub list: std::rc::Rc<std::cell::Cell<Option<RNode>>>,
    /// The content-list pane's preferred width (`NavProps::list_width`), for the fallback split.
    pub list_width: f64,
    /// Whether the pane is showing (`NavPatch::ListVisible`), shared with the piece so a
    /// collapsed pane stops narrowing the detail's fallback immediately, rather than one
    /// FrameChanged report later.
    pub list_visible: std::rc::Rc<std::cell::Cell<bool>>,
}

pub use day_spec::{NAV_LIST_WIDTH, NAV_SIDEBAR_WIDTH};

impl NavLayout {
    /// A host with no sidebar pane and no re-presenting to do: a tab strip's page area, or a
    /// `nav_stack` piece, both of which stay stacked at every size.
    pub fn stack(
        sizes: std::rc::Rc<std::cell::RefCell<std::collections::HashMap<RNode, Size>>>,
    ) -> Self {
        NavLayout {
            sizes,
            presentation: std::rc::Rc::new(std::cell::Cell::new(
                day_spec::props::NavPresentation::Stack,
            )),
            sidebar: std::rc::Rc::new(std::cell::Cell::new(None)),
            list: std::rc::Rc::new(std::cell::Cell::new(None)),
            list_width: NAV_LIST_WIDTH,
            list_visible: std::rc::Rc::new(std::cell::Cell::new(true)),
        }
    }
}

impl Layout for NavLayout {
    fn measure(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], p: Proposal) -> Size {
        // Greedy: the host owns the window. A nested stack merges into the enclosing host's page
        // list rather than nesting a second host (docs/navigation.md), so one NAV host still
        // spans the window.
        Size::new(p.width.unwrap_or(480.0), p.height.unwrap_or(640.0))
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        let pres = self.presentation.get();
        let split = pres.is_split();
        let sidebar = self.sidebar.get();
        let list = self.list.get();
        // The list pane narrows the detail's fallback only while it has its own pane and it
        // is showing; a collapsed pane gives the detail its width back at once.
        let list_w = if split && list.is_some() && self.list_visible.get() {
            self.list_width
        } else {
            0.0
        };
        for &page in children {
            let reported = self.sizes.borrow().get(&page).copied();
            // Only a fallback: every backend reports each page's real frame through
            // `Event::FrameChanged`, and that wins. This is what the page gets for the frame
            // before the first report arrives.
            let sz = reported.unwrap_or_else(|| {
                let is_sidebar = Some(page) == sidebar;
                let is_list = Some(page) == list;
                if pres.rows_are_chrome() {
                    // The rows are the chrome (a tab bar, a rail): the backend draws them itself
                    // and sizes its own bar, so the sidebar page is measured but not shown. Keep
                    // it at the pane width rather than zero, since a zero-width menu measured here
                    // would have to re-measure from scratch the moment the window widens back
                    // into a split.
                    if is_sidebar {
                        Size::new(NAV_SIDEBAR_WIDTH, bounds.size.height)
                    } else if is_list {
                        Size::new(self.list_width, bounds.size.height)
                    } else {
                        bounds.size
                    }
                } else if !split {
                    // Includes a stacked host's list page: full width where it joins the stack
                    // (`Cap::NavContentList` Emulated); where its pane persists instead
                    // (Native, a narrow Mac window) the first FrameChanged report corrects it.
                    bounds.size
                } else if is_sidebar {
                    Size::new(NAV_SIDEBAR_WIDTH, bounds.size.height)
                } else if is_list {
                    Size::new(self.list_width, bounds.size.height)
                } else {
                    Size::new(
                        (bounds.size.width - NAV_SIDEBAR_WIDTH - list_w - 1.0).max(0.0),
                        bounds.size.height,
                    )
                }
            });
            cx.place_child_native(page, Rect::from_size(sz));
        }
    }
}

/// Inspector split (docs/inspector.md): the two pane frames are native-owned (an
/// `NSSplitView`'s panes, a dock widget), so Day lays each pane's content within the size the
/// toolkit last reported via `Event::FrameChanged`, the same native-owned-frame contract as
/// [`NavLayout`] pages. The fallback before the first report splits the host bounds at the
/// pane width when visible, and gives the content everything when hidden.
pub struct InspectorLayout {
    pub sizes: std::rc::Rc<std::cell::RefCell<std::collections::HashMap<RNode, Size>>>,
    /// Visibility as last patched, shared with the piece so a toggle changes the fallback
    /// split without rebuilding the layout object the host was realized with.
    pub visible: std::rc::Rc<std::cell::Cell<bool>>,
    /// The pane's preferred width (`InspectorProps::width`), for the fallback split.
    pub width: f64,
}

impl Layout for InspectorLayout {
    fn measure(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], p: Proposal) -> Size {
        // Greedy, like a nav host: the split owns whatever its parent proposes.
        Size::new(p.width.unwrap_or(480.0), p.height.unwrap_or(640.0))
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        // Insertion order is the contract (day-spec `Builtin::Inspector`): 0 content, 1 panel.
        for (i, &pane) in children.iter().enumerate() {
            let reported = self.sizes.borrow().get(&pane).copied();
            let sz = reported.unwrap_or_else(|| {
                let panel_w = if self.visible.get() { self.width } else { 0.0 };
                if i == 0 {
                    Size::new((bounds.size.width - panel_w).max(0.0), bounds.size.height)
                } else {
                    Size::new(panel_w, bounds.size.height)
                }
            });
            cx.place_child_native(pane, Rect::from_size(sz));
        }
    }
}

/// Native split (docs/split.md): the two pane frames are native-owned (an `NSSplitView`'s, a
/// `GtkPaned`'s, a `QSplitter`'s), so Day lays each pane's content within the size the toolkit
/// last reported via `Event::FrameChanged`, the [`InspectorLayout`] contract. The fallback
/// before the first report divides the host bounds by the bound fraction along the axis.
pub struct SplitLayout {
    pub sizes: std::rc::Rc<std::cell::RefCell<std::collections::HashMap<RNode, Size>>>,
    /// Stacked (`true`) or side by side, as last patched; shared with the piece.
    pub stacked: std::rc::Rc<std::cell::Cell<bool>>,
    /// The first pane's share, as last patched; shared with the piece.
    pub fraction: std::rc::Rc<std::cell::Cell<f64>>,
}

impl Layout for SplitLayout {
    fn measure(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], p: Proposal) -> Size {
        // Greedy, like a nav host: the split owns whatever its parent proposes.
        Size::new(p.width.unwrap_or(480.0), p.height.unwrap_or(640.0))
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
        // Insertion order is the contract (day-spec `Builtin::Split`): 0 first, 1 second.
        let f = self.fraction.get().clamp(0.0, 1.0);
        let (w, h) = (bounds.size.width, bounds.size.height);
        for (i, &pane) in children.iter().enumerate() {
            let reported = self.sizes.borrow().get(&pane).copied();
            let sz = reported.unwrap_or_else(|| {
                let share = if i == 0 { f } else { 1.0 - f };
                if self.stacked.get() {
                    Size::new(w, (h * share).floor())
                } else {
                    Size::new((w * share).floor(), h)
                }
            });
            cx.place_child_native(pane, Rect::from_size(sz));
        }
    }
}

/// Fullscreen cover (docs/cover.md): the cover node occupies no space where it sits in the
/// tree (its native surface is presented over the window, outside the parent's bounds), and
/// its content is laid out at the size the backend reported via `Event::FrameChanged`, the
/// same native-owned-frame contract as [`NavLayout`] pages.
pub struct CoverLayout {
    pub size: std::rc::Rc<std::cell::RefCell<Option<Size>>>,
}

impl Layout for CoverLayout {
    fn measure(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], _p: Proposal) -> Size {
        Size::new(0.0, 0.0)
    }
    fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], _bounds: Rect) {
        let Some(sz) = *self.size.borrow() else {
            return; // not presented yet; content lays out on the first FrameChanged
        };
        for &child in children {
            cx.place_child_native(child, Rect::from_size(sz));
        }
    }
}

/// Helper for constructing shared layout Rcs.
pub fn rc_layout<L: Layout>(l: L) -> Rc<dyn Layout> {
    Rc::new(l)
}