cranpose-ui 0.0.59

UI primitives for Cranpose
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
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
// WIP: Layout system infrastructure - many helper types not yet fully wired up

pub mod coordinator;
pub mod core;
pub mod policies;

use cranpose_core::collections::map::HashMap;
use std::{
    cell::{Cell, RefCell},
    fmt,
    rc::Rc,
    sync::atomic::{AtomicU64, Ordering},
};

use cranpose_core::{
    Applier, ApplierHost, Composer, ConcreteApplierHost, MemoryApplier, Node, NodeError, NodeId,
    Phase, RuntimeHandle, SlotTable, SlotsHost, SnapshotStateObserver,
};

use self::coordinator::NodeCoordinator;
use self::core::Measurable;
use self::core::Placeable;
#[cfg(test)]
use self::core::{HorizontalAlignment, VerticalAlignment};
use crate::modifier::{
    collect_semantics_from_modifier, DimensionConstraint, EdgeInsets, Modifier, ModifierNodeSlices,
    Point, Rect as GeometryRect, ResolvedModifiers, Size,
};

use crate::subcompose_layout::SubcomposeLayoutNode;
use crate::widgets::nodes::{IntrinsicKind, LayoutNode, LayoutNodeCacheHandles};
use cranpose_foundation::{
    InvalidationKind, ModifierNodeContext, NodeCapabilities, SemanticsConfiguration,
};
use cranpose_ui_layout::{Constraints, MeasurePolicy, MeasureResult};

/// Runtime context for modifier nodes during measurement.
///
/// Unlike `BasicModifierNodeContext`, this context accumulates invalidations
/// that can be processed after measurement to set dirty flags on the LayoutNode.
#[derive(Default)]
pub(crate) struct LayoutNodeContext {
    invalidations: Vec<InvalidationKind>,
    update_requested: bool,
    active_capabilities: Vec<NodeCapabilities>,
}

impl LayoutNodeContext {
    pub(crate) fn new() -> Self {
        Self::default()
    }

    pub(crate) fn take_invalidations(&mut self) -> Vec<InvalidationKind> {
        std::mem::take(&mut self.invalidations)
    }
}

impl ModifierNodeContext for LayoutNodeContext {
    fn invalidate(&mut self, kind: InvalidationKind) {
        if !self.invalidations.contains(&kind) {
            self.invalidations.push(kind);
        }
    }

    fn request_update(&mut self) {
        self.update_requested = true;
    }

    fn push_active_capabilities(&mut self, capabilities: NodeCapabilities) {
        self.active_capabilities.push(capabilities);
    }

    fn pop_active_capabilities(&mut self) {
        self.active_capabilities.pop();
    }
}

static NEXT_CACHE_EPOCH: AtomicU64 = AtomicU64::new(1);

/// Forces all layout caches to be invalidated on the next measure by incrementing the epoch.
///
/// # ⚠️ Internal Use Only - NOT Public API
///
/// **This function is hidden from public documentation and MUST NOT be called by external code.**
///
/// Only `cranpose-app-shell` may call this for rare global events:
/// - Window/viewport resize
/// - Global font scale or density changes
/// - Debug toggles that affect all layout
///
/// **This is O(entire app size) - extremely expensive!**
///
/// # For Local Changes
///
/// **Do NOT use this for scroll, single-node mutations, or any local layout change.**
/// Instead, use the scoped repass mechanism:
/// ```text
/// cranpose_ui::schedule_layout_repass(node_id);
/// ```
///
/// The scoped path bubbles dirty flags without invalidating all caches, giving you O(subtree) instead of O(app).
#[doc(hidden)]
pub fn invalidate_all_layout_caches() {
    NEXT_CACHE_EPOCH.fetch_add(1, Ordering::Relaxed);
}

/// RAII guard that:
/// - moves the current MemoryApplier into a ConcreteApplierHost
/// - holds a shared handle to the `SlotTable` used by `LayoutBuilder`
/// - on Drop, always:
///   * restores slots into the host from the shared handle
///   * moves the original MemoryApplier back into the Composition
///
/// This makes `measure_layout` panic/Err-safe wrt both the applier and slots.
/// The key invariant: guard and builder share the same `Rc<RefCell<SlotTable>>`,
/// so the guard never loses access to the authoritative slots even on panic.
struct ApplierSlotGuard<'a> {
    /// The `MemoryApplier` inside the Composition::applier that we must restore into.
    target: &'a mut MemoryApplier,
    /// Host that owns the original MemoryApplier while layout is running.
    host: Rc<ConcreteApplierHost<MemoryApplier>>,
    /// Shared handle to the slot table. Both the guard and the builder hold a clone.
    /// On Drop, we write whatever is in this handle back into the applier.
    slots: Rc<RefCell<SlotTable>>,
}

impl<'a> ApplierSlotGuard<'a> {
    /// Creates a new guard:
    /// - moves the current MemoryApplier out of `target` into a host
    /// - takes the current slots out of the host and wraps them in a shared handle
    fn new(target: &'a mut MemoryApplier) -> Self {
        // Move the original applier into a host; leave `target` with a fresh one
        let original_applier = std::mem::replace(target, MemoryApplier::new());
        let host = Rc::new(ConcreteApplierHost::new(original_applier));

        // Take slots from the host into a shared handle
        let slots = {
            let mut applier_ref = host.borrow_typed();
            std::mem::take(applier_ref.slots())
        };
        let slots = Rc::new(RefCell::new(slots));

        Self {
            target,
            host,
            slots,
        }
    }

    /// Rc to pass into LayoutBuilder::new_with_epoch
    fn host(&self) -> Rc<ConcreteApplierHost<MemoryApplier>> {
        Rc::clone(&self.host)
    }

    /// Returns the shared handle to slots for the builder to use.
    /// The builder clones this Rc, so both guard and builder share the same slots.
    fn slots_handle(&self) -> Rc<RefCell<SlotTable>> {
        Rc::clone(&self.slots)
    }
}

impl Drop for ApplierSlotGuard<'_> {
    fn drop(&mut self) {
        // 1) Restore slots into the host's MemoryApplier from the shared handle.
        // This works correctly whether we're on the success path or panic/error path,
        // because we always have the shared handle.
        {
            let mut applier_ref = self.host.borrow_typed();
            *applier_ref.slots() = std::mem::take(&mut *self.slots.borrow_mut());
        }

        // 2) Move the original MemoryApplier (with restored/updated slots) back into `target`
        {
            let mut applier_ref = self.host.borrow_typed();
            let original_applier = std::mem::take(&mut *applier_ref);
            let _ = std::mem::replace(self.target, original_applier);
        }
        // No Rc::try_unwrap in Drop → no "panic during panic" risk.
    }
}

/// Result of measuring through the modifier node chain.
struct ModifierChainMeasurement {
    result: MeasureResult,
    /// Content offset for scroll/inner transforms - NOT padding semantics
    content_offset: Point,
    /// Node's own offset (from OffsetNode, affects position in parent)
    offset: Point,
}

type LayoutModifierNodeData = (
    usize,
    Rc<RefCell<Box<dyn cranpose_foundation::ModifierNode>>>,
);

struct ScratchVecPool<T> {
    available: Vec<Vec<T>>,
}

impl<T> ScratchVecPool<T> {
    fn acquire(&mut self) -> Vec<T> {
        self.available.pop().unwrap_or_default()
    }

    fn release(&mut self, mut values: Vec<T>) {
        values.clear();
        self.available.push(values);
    }

    #[cfg(test)]
    fn available_count(&self) -> usize {
        self.available.len()
    }
}

impl<T> Default for ScratchVecPool<T> {
    fn default() -> Self {
        Self {
            available: Vec::new(),
        }
    }
}

/// Discrete event callback reference produced during semantics extraction.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SemanticsCallback {
    node_id: NodeId,
}

impl SemanticsCallback {
    pub fn new(node_id: NodeId) -> Self {
        Self { node_id }
    }

    pub fn node_id(&self) -> NodeId {
        self.node_id
    }
}

/// Semantics action exposed to the input system.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SemanticsAction {
    Click { handler: SemanticsCallback },
}

/// Semantic role describing how a node should participate in accessibility and hit testing.
/// Roles are now derived from SemanticsConfiguration rather than widget types.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SemanticsRole {
    /// Generic container or layout node
    Layout,
    /// Subcomposition boundary
    Subcompose,
    /// Text content (derived from TextNode for backward compatibility)
    Text { value: String },
    /// Spacer (non-interactive)
    Spacer,
    /// Button (derived from is_button semantics flag)
    Button,
    /// Unknown or unspecified role
    Unknown,
}

/// A single node within the semantics tree.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SemanticsNode {
    pub node_id: NodeId,
    pub role: SemanticsRole,
    pub actions: Vec<SemanticsAction>,
    pub children: Vec<SemanticsNode>,
    pub description: Option<String>,
}

impl SemanticsNode {
    fn new(
        node_id: NodeId,
        role: SemanticsRole,
        actions: Vec<SemanticsAction>,
        children: Vec<SemanticsNode>,
        description: Option<String>,
    ) -> Self {
        Self {
            node_id,
            role,
            actions,
            children,
            description,
        }
    }
}

/// Rooted semantics tree extracted after layout.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SemanticsTree {
    root: SemanticsNode,
}

impl SemanticsTree {
    fn new(root: SemanticsNode) -> Self {
        Self { root }
    }

    pub fn root(&self) -> &SemanticsNode {
        &self.root
    }
}

/// Result of running layout for a Compose tree.
#[derive(Debug, Clone)]
pub struct LayoutTree {
    root: LayoutBox,
}

impl LayoutTree {
    pub fn new(root: LayoutBox) -> Self {
        Self { root }
    }

    pub fn root(&self) -> &LayoutBox {
        &self.root
    }

    pub fn root_mut(&mut self) -> &mut LayoutBox {
        &mut self.root
    }

    pub fn into_root(self) -> LayoutBox {
        self.root
    }
}

/// Layout information for a single node.
#[derive(Debug, Clone)]
pub struct LayoutBox {
    pub node_id: NodeId,
    pub rect: GeometryRect,
    /// Content offset for scroll/inner transforms (applies to children, NOT this node's position)
    pub content_offset: Point,
    pub node_data: LayoutNodeData,
    pub children: Vec<LayoutBox>,
}

impl LayoutBox {
    pub fn new(
        node_id: NodeId,
        rect: GeometryRect,
        content_offset: Point,
        node_data: LayoutNodeData,
        children: Vec<LayoutBox>,
    ) -> Self {
        Self {
            node_id,
            rect,
            content_offset,
            node_data,
            children,
        }
    }
}

/// Snapshot of the data required to render a layout node.
#[derive(Debug, Clone)]
pub struct LayoutNodeData {
    pub modifier: Modifier,
    pub resolved_modifiers: ResolvedModifiers,
    pub modifier_slices: Rc<ModifierNodeSlices>,
    pub kind: LayoutNodeKind,
}

impl LayoutNodeData {
    pub fn new(
        modifier: Modifier,
        resolved_modifiers: ResolvedModifiers,
        modifier_slices: Rc<ModifierNodeSlices>,
        kind: LayoutNodeKind,
    ) -> Self {
        Self {
            modifier,
            resolved_modifiers,
            modifier_slices,
            kind,
        }
    }

    pub fn resolved_modifiers(&self) -> ResolvedModifiers {
        self.resolved_modifiers
    }

    pub fn modifier_slices(&self) -> &ModifierNodeSlices {
        &self.modifier_slices
    }
}

/// Classification of the node captured inside a [`LayoutBox`].
///
/// Note: Text content is no longer represented as a distinct LayoutNodeKind.
/// Text nodes now use `LayoutNodeKind::Layout` with their content stored in
/// `modifier_slices.text_content()` via TextModifierNode, following Jetpack
/// Compose's pattern where text is a modifier node capability.
#[derive(Clone)]
pub enum LayoutNodeKind {
    Layout,
    Subcompose,
    Spacer,
    Button { on_click: Rc<RefCell<dyn FnMut()>> },
    Unknown,
}

impl fmt::Debug for LayoutNodeKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LayoutNodeKind::Layout => f.write_str("Layout"),
            LayoutNodeKind::Subcompose => f.write_str("Subcompose"),
            LayoutNodeKind::Spacer => f.write_str("Spacer"),
            LayoutNodeKind::Button { .. } => f.write_str("Button"),
            LayoutNodeKind::Unknown => f.write_str("Unknown"),
        }
    }
}

/// Extension trait that equips `MemoryApplier` with layout computation.
pub trait LayoutEngine {
    fn compute_layout(&mut self, root: NodeId, max_size: Size) -> Result<LayoutTree, NodeError>;
}

impl LayoutEngine for MemoryApplier {
    fn compute_layout(&mut self, root: NodeId, max_size: Size) -> Result<LayoutTree, NodeError> {
        let measurements = measure_layout(self, root, max_size)?;
        Ok(measurements.into_layout_tree())
    }
}

/// Result of running the measure pass for a Compose layout tree.
#[derive(Debug, Clone)]
pub struct LayoutMeasurements {
    root: Rc<MeasuredNode>,
    semantics: Option<SemanticsTree>,
    layout_tree: Option<LayoutTree>,
}

impl LayoutMeasurements {
    fn new(
        root: Rc<MeasuredNode>,
        semantics: Option<SemanticsTree>,
        layout_tree: Option<LayoutTree>,
    ) -> Self {
        Self {
            root,
            semantics,
            layout_tree,
        }
    }

    /// Returns the measured size of the root node.
    pub fn root_size(&self) -> Size {
        self.root.size
    }

    pub fn semantics_tree(&self) -> Option<&SemanticsTree> {
        self.semantics.as_ref()
    }

    /// Consumes the measurements and produces a [`LayoutTree`].
    pub fn into_layout_tree(self) -> LayoutTree {
        self.layout_tree
            .expect("layout tree was not built for these measurements")
    }

    /// Returns a borrowed [`LayoutTree`] for rendering.
    pub fn layout_tree(&self) -> LayoutTree {
        self.layout_tree
            .clone()
            .expect("layout tree was not built for these measurements")
    }
}

/// Builds a semantics tree from an existing [`LayoutTree`].
///
/// This is useful for consumers that need semantics on demand without forcing
/// every layout pass to eagerly allocate a full [`SemanticsTree`].
pub fn build_semantics_tree_from_layout_tree(layout_tree: &LayoutTree) -> SemanticsTree {
    SemanticsTree::new(build_semantics_node_from_layout_box(layout_tree.root()))
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MeasureLayoutOptions {
    pub collect_semantics: bool,
    pub build_layout_tree: bool,
}

impl Default for MeasureLayoutOptions {
    fn default() -> Self {
        Self {
            collect_semantics: true,
            build_layout_tree: true,
        }
    }
}

/// Check if a node or any of its descendants needs measure (selective measure optimization).
/// This can be used by the app shell to skip layout when the tree is clean.
///
/// O(1) check - just looks at root's dirty flag.
/// Works because all mutation paths bubble dirty flags to root via composer commands.
///
/// Returns Result to force caller to handle errors explicitly. No more unwrap_or(true) safety net.
pub fn tree_needs_layout(applier: &mut dyn Applier, root: NodeId) -> Result<bool, NodeError> {
    Ok(applier.get_mut(root)?.needs_layout())
}

/// Check if the root semantics snapshot is dirty.
///
/// Semantics invalidations bubble to the root the same way layout invalidations do,
/// so a root check is sufficient to determine whether the next layout pass needs to
/// rebuild semantic data even when geometry is otherwise unchanged.
pub fn tree_needs_semantics(applier: &mut dyn Applier, root: NodeId) -> Result<bool, NodeError> {
    Ok(applier.get_mut(root)?.needs_semantics())
}

/// Test helper: bubbles layout dirty flag to root.
#[cfg(test)]
pub(crate) fn bubble_layout_dirty(applier: &mut MemoryApplier, node_id: NodeId) {
    cranpose_core::bubble_layout_dirty(applier as &mut dyn Applier, node_id);
}

/// Runs the measure phase for the subtree rooted at `root`.
pub fn measure_layout(
    applier: &mut MemoryApplier,
    root: NodeId,
    max_size: Size,
) -> Result<LayoutMeasurements, NodeError> {
    measure_layout_with_options(applier, root, max_size, MeasureLayoutOptions::default())
}

pub fn measure_layout_with_options(
    applier: &mut MemoryApplier,
    root: NodeId,
    max_size: Size,
    options: MeasureLayoutOptions,
) -> Result<LayoutMeasurements, NodeError> {
    process_pending_layout_repasses(applier, root)?;

    let constraints = Constraints {
        min_width: 0.0,
        max_width: max_size.width,
        min_height: 0.0,
        max_height: max_size.height,
    };

    // Selective measure: only increment epoch if something needs MEASURING (not just layout)
    // O(1) check - just look at root's dirty flag (bubbling ensures correctness)
    //
    // CRITICAL: We check needs_MEASURE, not needs_LAYOUT!
    // - needs_measure: size may change, caches must be invalidated
    // - needs_layout: position may change but size is cached (e.g., scroll)
    //
    // Scroll operations bubble needs_layout to ancestors, but NOT needs_measure.
    // Using needs_layout here would wipe ALL caches on every scroll frame, causing
    // O(N) full remeasurement instead of O(changed nodes).
    let (needs_remeasure, _needs_semantics, cached_epoch) = match applier
        .with_node::<LayoutNode, _>(root, |node| {
            (
                node.needs_measure(), // CORRECT: check needs_measure, not needs_layout
                node.needs_semantics(),
                node.cache_handles().epoch(),
            )
        }) {
        Ok(tuple) => tuple,
        Err(NodeError::TypeMismatch { .. }) => {
            let node = applier.get_mut(root)?;
            // Non-LayoutNode roots still expose Node dirty flags.
            // Use needs_measure here so layout-only subtree repasses can reuse
            // the existing cache epoch instead of invalidating the whole tree.
            let measure_dirty = node.needs_measure();
            let semantics_dirty = node.needs_semantics();
            (measure_dirty, semantics_dirty, 0)
        }
        Err(err) => return Err(err),
    };

    let epoch = if needs_remeasure {
        NEXT_CACHE_EPOCH.fetch_add(1, Ordering::Relaxed)
    } else if cached_epoch != 0 {
        cached_epoch
    } else {
        // Fallback when caller root isn't a LayoutNode (e.g. tests using Spacer directly).
        NEXT_CACHE_EPOCH.load(Ordering::Relaxed)
    };

    // Move the current applier into a host and set up a guard that will
    // ALWAYS restore:
    // - the MemoryApplier back into `applier`
    // - the SlotTable back into that MemoryApplier
    //
    // IMPORTANT: Declare the guard *before* the builder so the builder
    // is dropped first (both on Ok and on unwind).
    let guard = ApplierSlotGuard::new(applier);
    let applier_host = guard.host();
    let slots_handle = guard.slots_handle();

    // Give the builder the shared slots handle - both guard and builder
    // now share access to the same SlotTable via Rc<RefCell<_>>.
    let mut builder =
        LayoutBuilder::new_with_epoch(Rc::clone(&applier_host), epoch, Rc::clone(&slots_handle));

    // ---- Measurement -------------------------------------------------------
    // If measurement fails, the guard will restore slots from the shared handle
    // on drop - this is safe because the handle always contains valid slots.

    let measured = builder.measure_node(root, normalize_constraints(constraints))?;

    // Root node has no parent to place it, so we must explicitly place it at (0,0).
    // This ensures is_placed=true, allowing the renderer to traverse the tree.
    // Handle both LayoutNode and SubcomposeLayoutNode as potential roots.
    if let Ok(mut applier) = applier_host.try_borrow_typed() {
        if applier
            .with_node::<LayoutNode, _>(root, |node| {
                node.set_position(Point::default());
            })
            .is_err()
        {
            let _ = applier.with_node::<SubcomposeLayoutNode, _>(root, |node| {
                node.set_position(Point::default());
            });
        }
    }

    let (layout_tree, semantics) = {
        let mut applier_ref = applier_host.borrow_typed();
        let layout_tree = if options.build_layout_tree {
            Some(build_layout_tree(&mut applier_ref, &measured)?)
        } else {
            None
        };
        let semantics = if options.collect_semantics {
            let semantics_tree = if let Some(layout_tree) = layout_tree.as_ref() {
                clear_semantics_dirty_flags(&mut applier_ref, &measured)?;
                build_semantics_tree_from_layout_tree(layout_tree)
            } else {
                build_semantics_tree_from_live_nodes(&mut applier_ref, &measured)?
            };
            Some(semantics_tree)
        } else {
            None
        };
        (layout_tree, semantics)
    };

    // Drop builder before guard - slots are already in the shared handle.
    // Guard's Drop will write them back to the applier.
    drop(builder);

    // DO NOT manually unwrap `applier_host` or replace `applier` here.
    // `ApplierSlotGuard::drop` will restore everything when this function returns.

    Ok(LayoutMeasurements::new(measured, semantics, layout_tree))
}

fn process_pending_layout_repasses(
    applier: &mut MemoryApplier,
    root: NodeId,
) -> Result<(), NodeError> {
    let repass_nodes = crate::take_layout_repass_nodes();
    if repass_nodes.is_empty() {
        return Ok(());
    }
    for node_id in repass_nodes {
        cranpose_core::bubble_layout_dirty(applier as &mut dyn Applier, node_id);
    }
    applier.get_mut(root)?.mark_needs_layout();
    Ok(())
}

struct LayoutBuilder {
    state: Rc<RefCell<LayoutBuilderState>>,
}

impl LayoutBuilder {
    fn new_with_epoch(
        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
        epoch: u64,
        slots: Rc<RefCell<SlotTable>>,
    ) -> Self {
        Self {
            state: Rc::new(RefCell::new(LayoutBuilderState::new_with_epoch(
                applier, epoch, slots,
            ))),
        }
    }

    fn measure_node(
        &mut self,
        node_id: NodeId,
        constraints: Constraints,
    ) -> Result<Rc<MeasuredNode>, NodeError> {
        LayoutBuilderState::measure_node(Rc::clone(&self.state), node_id, constraints)
    }

    fn set_runtime_handle(&mut self, handle: Option<RuntimeHandle>) {
        self.state.borrow_mut().runtime_handle = handle;
    }
}

struct LayoutBuilderState {
    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
    runtime_handle: Option<RuntimeHandle>,
    /// Shared handle to the slot table. This is shared with ApplierSlotGuard
    /// to ensure panic-safety: even if we panic, the guard can restore slots.
    slots: Rc<RefCell<SlotTable>>,
    cache_epoch: u64,
    tmp_measurables: ScratchVecPool<Box<dyn Measurable>>,
    tmp_records: ScratchVecPool<(NodeId, ChildRecord)>,
    tmp_child_ids: ScratchVecPool<NodeId>,
    tmp_layout_node_data: ScratchVecPool<LayoutModifierNodeData>,
}

impl LayoutBuilderState {
    fn new_with_epoch(
        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
        epoch: u64,
        slots: Rc<RefCell<SlotTable>>,
    ) -> Self {
        let runtime_handle = applier.borrow_typed().runtime_handle();

        Self {
            applier,
            runtime_handle,
            slots,
            cache_epoch: epoch,
            tmp_measurables: ScratchVecPool::default(),
            tmp_records: ScratchVecPool::default(),
            tmp_child_ids: ScratchVecPool::default(),
            tmp_layout_node_data: ScratchVecPool::default(),
        }
    }

    fn try_with_applier_result<R>(
        state_rc: &Rc<RefCell<Self>>,
        f: impl FnOnce(&mut MemoryApplier) -> Result<R, NodeError>,
    ) -> Option<Result<R, NodeError>> {
        let host = {
            let state = state_rc.borrow();
            Rc::clone(&state.applier)
        };

        // Try to borrow - if already borrowed (nested call), return None
        let Ok(mut applier) = host.try_borrow_typed() else {
            return None;
        };

        Some(f(&mut applier))
    }

    fn with_applier_result<R>(
        state_rc: &Rc<RefCell<Self>>,
        f: impl FnOnce(&mut MemoryApplier) -> Result<R, NodeError>,
    ) -> Result<R, NodeError> {
        Self::try_with_applier_result(state_rc, f).unwrap_or_else(|| {
            Err(NodeError::MissingContext {
                id: NodeId::default(),
                reason: "applier already borrowed",
            })
        })
    }

    /// Clears the is_placed flag for a node at the start of measurement.
    /// This ensures nodes that drop out of placement won't render with stale geometry.
    fn clear_node_placed(state_rc: &Rc<RefCell<Self>>, node_id: NodeId) {
        let host = {
            let state = state_rc.borrow();
            Rc::clone(&state.applier)
        };
        let Ok(mut applier) = host.try_borrow_typed() else {
            return;
        };
        // Try LayoutNode first, then SubcomposeLayoutNode
        if applier
            .with_node::<LayoutNode, _>(node_id, |node| {
                node.clear_placed();
            })
            .is_err()
        {
            let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
                node.clear_placed();
            });
        }
    }

    fn measure_node(
        state_rc: Rc<RefCell<Self>>,
        node_id: NodeId,
        constraints: Constraints,
    ) -> Result<Rc<MeasuredNode>, NodeError> {
        // Clear is_placed at the start of measurement.
        // Nodes that are placed will have is_placed set to true via Placeable::place().
        // Nodes that drop out of placement (not placed this pass) will remain is_placed=false.
        Self::clear_node_placed(&state_rc, node_id);

        // Try SubcomposeLayoutNode first
        if let Some(subcompose) =
            Self::try_measure_subcompose(Rc::clone(&state_rc), node_id, constraints)?
        {
            return Ok(subcompose);
        }

        // Try LayoutNode (the primary modern path)
        if let Some(result) = Self::try_with_applier_result(&state_rc, |applier| {
            match applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
                LayoutNodeSnapshot::from_layout_node(layout_node)
            }) {
                Ok(snapshot) => Ok(Some(snapshot)),
                Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => Ok(None),
                Err(err) => Err(err),
            }
        }) {
            // Applier was available, process the result
            if let Some(snapshot) = result? {
                return Self::measure_layout_node(
                    Rc::clone(&state_rc),
                    node_id,
                    snapshot,
                    constraints,
                );
            }
        }
        // If applier was busy (None) or snapshot was None, fall through to fallback

        // No alternate fallbacks - all widgets use LayoutNode or SubcomposeLayoutNode
        // If we reach here, it's an unknown node type (shouldn't happen in normal use)
        Ok(Rc::new(MeasuredNode::new(
            node_id,
            Size::default(),
            Point { x: 0.0, y: 0.0 },
            Point::default(), // No content offset for fallback nodes
            Vec::new(),
        )))
    }

    fn try_measure_subcompose(
        state_rc: Rc<RefCell<Self>>,
        node_id: NodeId,
        constraints: Constraints,
    ) -> Result<Option<Rc<MeasuredNode>>, NodeError> {
        let applier_host = {
            let state = state_rc.borrow();
            Rc::clone(&state.applier)
        };

        let (node_handle, resolved_modifiers) = {
            // Try to borrow - if already borrowed (nested measurement), return None
            let Ok(mut applier) = applier_host.try_borrow_typed() else {
                return Ok(None);
            };
            let node = match applier.get_mut(node_id) {
                Ok(node) => node,
                Err(NodeError::Missing { .. }) => return Ok(None),
                Err(err) => return Err(err),
            };
            let any = node.as_any_mut();
            if let Some(subcompose) =
                any.downcast_mut::<crate::subcompose_layout::SubcomposeLayoutNode>()
            {
                let handle = subcompose.handle();
                let resolved_modifiers = handle.resolved_modifiers();
                (handle, resolved_modifiers)
            } else {
                return Ok(None);
            }
        };

        let runtime_handle = {
            let mut state = state_rc.borrow_mut();
            if state.runtime_handle.is_none() {
                // Try to borrow - if already borrowed, we can't get runtime handle
                if let Ok(applier) = applier_host.try_borrow_typed() {
                    state.runtime_handle = applier.runtime_handle();
                }
            }
            state
                .runtime_handle
                .clone()
                .ok_or(NodeError::MissingContext {
                    id: node_id,
                    reason: "runtime handle required for subcomposition",
                })?
        };

        let props = resolved_modifiers.layout_properties();
        let padding = resolved_modifiers.padding();
        let offset = resolved_modifiers.offset();
        let mut inner_constraints = normalize_constraints(subtract_padding(constraints, padding));

        if let DimensionConstraint::Points(width) = props.width() {
            let constrained_width = width - padding.horizontal_sum();
            inner_constraints.max_width = inner_constraints.max_width.min(constrained_width);
            inner_constraints.min_width = inner_constraints.min_width.min(constrained_width);
        }
        if let DimensionConstraint::Points(height) = props.height() {
            let constrained_height = height - padding.vertical_sum();
            inner_constraints.max_height = inner_constraints.max_height.min(constrained_height);
            inner_constraints.min_height = inner_constraints.min_height.min(constrained_height);
        }

        let mut slots_guard = SlotsGuard::take(Rc::clone(&state_rc));
        let slots_host = slots_guard.host();
        let applier_host_dyn: Rc<dyn ApplierHost> = applier_host.clone();
        let observer = SnapshotStateObserver::new(|callback| callback());
        let composer = Composer::new(
            Rc::clone(&slots_host),
            applier_host_dyn,
            runtime_handle.clone(),
            observer,
            Some(node_id),
        );
        composer.enter_phase(Phase::Measure);

        let state_rc_clone = Rc::clone(&state_rc);
        let measure_error = RefCell::new(None);
        let state_rc_for_subcompose = Rc::clone(&state_rc_clone);
        let error_for_subcompose = &measure_error;
        let measured_children: Rc<RefCell<HashMap<NodeId, Rc<MeasuredNode>>>> =
            Rc::new(RefCell::new(HashMap::default()));
        let measured_children_for_subcompose = Rc::clone(&measured_children);

        let measure_result = node_handle.measure(
            &composer,
            node_id,
            inner_constraints,
            Box::new(
                move |child_id: NodeId, child_constraints: Constraints| -> Size {
                    match Self::measure_node(
                        Rc::clone(&state_rc_for_subcompose),
                        child_id,
                        child_constraints,
                    ) {
                        Ok(measured) => {
                            measured_children_for_subcompose
                                .borrow_mut()
                                .insert(child_id, Rc::clone(&measured));
                            measured.size
                        }
                        Err(err) => {
                            let mut slot = error_for_subcompose.borrow_mut();
                            if slot.is_none() {
                                *slot = Some(err);
                            }
                            Size::default()
                        }
                    }
                },
            ),
            &measure_error,
        )?;
        slots_guard.restore(slots_host.take());

        if let Some(err) = measure_error.borrow_mut().take() {
            return Err(err);
        }

        // NOTE: Children are now managed by the composer via insert_child commands
        // (from parent_stack initialization with root). set_active_children is no longer used.

        let mut width = measure_result.size.width + padding.horizontal_sum();
        let mut height = measure_result.size.height + padding.vertical_sum();

        width = resolve_dimension(
            width,
            props.width(),
            props.min_width(),
            props.max_width(),
            constraints.min_width,
            constraints.max_width,
        );
        height = resolve_dimension(
            height,
            props.height(),
            props.min_height(),
            props.max_height(),
            constraints.min_height,
            constraints.max_height,
        );

        let mut children = Vec::with_capacity(measure_result.placements.len());
        let mut measured_children_by_id = measured_children.borrow_mut();

        // Update the SubcomposeLayoutNode's size (position will be set by parent's placement)
        if let Ok(mut applier) = applier_host.try_borrow_typed() {
            let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |parent_node| {
                parent_node.set_measured_size(Size { width, height });
                parent_node.clear_needs_measure();
                parent_node.clear_needs_layout();
            });
        }

        for placement in measure_result.placements {
            let child = if let Some(measured) = measured_children_by_id.remove(&placement.node_id) {
                measured
            } else {
                // Policies may place subcomposed children without calling `measure()` first
                // (for example, when they only need a slot's rendered content). Keep the
                // existing fallback for that case, but preserve the policy-time measurement
                // whenever it exists so we don't silently remeasure lazy items with the
                // container's tighter constraints.
                Self::measure_node(Rc::clone(&state_rc), placement.node_id, inner_constraints)?
            };
            let position = Point {
                x: padding.left + placement.x,
                y: padding.top + placement.y,
            };

            // Critical: Update the child LayoutNode's retained state.
            // Standard layouts do this via Placeable::place(), but SubcomposeLayout logic
            // bypasses Placeables and returns raw Placements.
            if let Ok(mut applier) = applier_host.try_borrow_typed() {
                let _ = applier.with_node::<LayoutNode, _>(placement.node_id, |node| {
                    node.set_position(position);
                });
            }

            children.push(MeasuredChild {
                node: child,
                offset: position,
            });
        }

        // Update the SubcomposeLayoutNode's active children for rendering
        node_handle.set_active_children(children.iter().map(|c| c.node.node_id));

        Ok(Some(Rc::new(MeasuredNode::new(
            node_id,
            Size { width, height },
            offset,
            Point::default(), // Subcompose nodes: content_offset handled by child layout
            children,
        ))))
    }
    /// Measures through the layout modifier coordinator chain using reconciled modifier nodes.
    /// Iterates through LayoutModifierNode instances from the ModifierNodeChain and calls
    /// their measure() methods, mirroring Jetpack Compose's LayoutModifierNodeCoordinator pattern.
    ///
    /// Always succeeds, building a coordinator chain (possibly just InnerCoordinator) to measure.
    ///
    fn measure_through_modifier_chain(
        state_rc: &Rc<RefCell<Self>>,
        node_id: NodeId,
        measurables: &[Box<dyn Measurable>],
        measure_policy: &Rc<dyn MeasurePolicy>,
        constraints: Constraints,
        layout_node_data: &mut Vec<LayoutModifierNodeData>,
    ) -> ModifierChainMeasurement {
        use cranpose_foundation::NodeCapabilities;

        // Collect layout node information from the modifier chain
        layout_node_data.clear();
        let mut offset = Point::default();

        {
            let state = state_rc.borrow();
            let mut applier = state.applier.borrow_typed();

            let _ = applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
                let chain_handle = layout_node.modifier_chain();

                if !chain_handle.has_layout_nodes() {
                    return;
                }

                // Collect indices and node Rc clones for layout modifier nodes
                chain_handle.chain().for_each_forward_matching(
                    NodeCapabilities::LAYOUT,
                    |node_ref| {
                        if let Some(index) = node_ref.entry_index() {
                            // Get the Rc clone for this node
                            if let Some(node_rc) = chain_handle.chain().get_node_rc(index) {
                                layout_node_data.push((index, Rc::clone(&node_rc)));
                            }

                            // Extract offset from OffsetNode for the node's own position
                            // The coordinator chain handles placement_offset (for children),
                            // but the node's offset affects where IT is positioned in the parent
                            node_ref.with_node(|node| {
                                if let Some(offset_node) =
                                    node.as_any()
                                        .downcast_ref::<crate::modifier_nodes::OffsetNode>()
                                {
                                    let delta = offset_node.offset();
                                    offset.x += delta.x;
                                    offset.y += delta.y;
                                }
                            });
                        }
                    },
                );
            });
        }

        // Fast path: if there are no layout modifiers, measure directly without coordinator chain.
        // This saves 3 allocations (shared_context, policy_result, InnerCoordinator box).
        if layout_node_data.is_empty() {
            let result = measure_policy.measure(measurables, constraints);
            let final_size = result.size;
            let placements = result.placements;

            return ModifierChainMeasurement {
                result: MeasureResult {
                    size: final_size,
                    placements,
                },
                content_offset: Point::default(),
                offset,
            };
        }

        // Slow path: build coordinator chain for layout modifiers.
        // Popping from the end preserves the "rightmost modifier measures first" order
        // without allocating or cloning the collected node list.
        // Create a shared context for this measurement pass to track invalidations
        let shared_context = Rc::new(RefCell::new(LayoutNodeContext::new()));

        // Create the inner coordinator that wraps the measure policy
        let policy_result = Rc::new(RefCell::new(None));
        let inner_coordinator: Box<dyn NodeCoordinator + '_> =
            Box::new(coordinator::InnerCoordinator::new(
                Rc::clone(measure_policy),
                measurables,
                Rc::clone(&policy_result),
            ));

        // Wrap each layout modifier node in a coordinator, building the chain
        let mut current_coordinator = inner_coordinator;
        while let Some((_, node_rc)) = layout_node_data.pop() {
            current_coordinator = Box::new(coordinator::LayoutModifierCoordinator::new(
                node_rc,
                current_coordinator,
                Rc::clone(&shared_context),
            ));
        }

        // Measure through the complete coordinator chain
        let placeable = current_coordinator.measure(constraints);
        let final_size = Size {
            width: placeable.width(),
            height: placeable.height(),
        };

        // Get accumulated content offset from the placeable (computed during measure)
        let content_offset = placeable.content_offset();
        let all_placement_offset = Point {
            x: content_offset.0,
            y: content_offset.1,
        };

        // The content_offset for scroll/inner transforms is the accumulated placement offset
        // MINUS the node's own offset (which affects its position in the parent, not content position).
        // This properly separates: node position (offset) vs inner content position (content_offset).
        let content_offset = Point {
            x: all_placement_offset.x - offset.x,
            y: all_placement_offset.y - offset.y,
        };

        // offset was already extracted from OffsetNode above

        let placements = policy_result
            .borrow_mut()
            .take()
            .map(|result| result.placements)
            .unwrap_or_default();

        // Process any invalidations requested during measurement
        let invalidations = shared_context.borrow_mut().take_invalidations();
        if !invalidations.is_empty() {
            // Mark the LayoutNode as needing the appropriate passes
            Self::with_applier_result(state_rc, |applier| {
                applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
                    for kind in invalidations {
                        match kind {
                            InvalidationKind::Layout => layout_node.mark_needs_measure(),
                            InvalidationKind::Draw => layout_node.mark_needs_redraw(),
                            InvalidationKind::Semantics => layout_node.mark_needs_semantics(),
                            InvalidationKind::PointerInput => layout_node.mark_needs_pointer_pass(),
                            InvalidationKind::Focus => layout_node.mark_needs_focus_sync(),
                        }
                    }
                })
            })
            .ok();
        }

        ModifierChainMeasurement {
            result: MeasureResult {
                size: final_size,
                placements,
            },
            content_offset,
            offset,
        }
    }

    fn measure_layout_node(
        state_rc: Rc<RefCell<Self>>,
        node_id: NodeId,
        snapshot: LayoutNodeSnapshot,
        constraints: Constraints,
    ) -> Result<Rc<MeasuredNode>, NodeError> {
        let cache_epoch = {
            let state = state_rc.borrow();
            state.cache_epoch
        };
        let LayoutNodeSnapshot {
            measure_policy,
            cache,
            needs_layout,
            needs_measure,
        } = snapshot;
        cache.activate(cache_epoch);

        if needs_measure {
            // Node has needs_measure=true
        }

        // Only check cache when the node is fully clean.
        // needs_layout=true means either the node itself or one of its descendants
        // must be revisited even if the node's own measured size can stay cached.
        if !needs_measure && !needs_layout {
            // Check cache for current constraints
            if let Some(cached) = cache.get_measurement(constraints) {
                // Clear dirty flag after successful cache hit
                Self::with_applier_result(&state_rc, |applier| {
                    applier.with_node::<LayoutNode, _>(node_id, |node| {
                        node.clear_needs_measure();
                        node.clear_needs_layout();
                    })
                })
                .ok();
                return Ok(cached);
            }
        }

        let (runtime_handle, applier_host) = {
            let state = state_rc.borrow();
            (state.runtime_handle.clone(), Rc::clone(&state.applier))
        };

        let measure_handle = LayoutMeasureHandle::new(Rc::clone(&state_rc));
        let error = Rc::new(RefCell::new(None));
        let mut pools = VecPools::acquire(Rc::clone(&state_rc));
        let (measurables, records, child_ids, layout_node_data) = pools.parts();

        applier_host
            .borrow_typed()
            .with_node::<LayoutNode, _>(node_id, |node| {
                child_ids.extend_from_slice(&node.children);
            })?;

        for &child_id in child_ids.iter() {
            let measured = Rc::new(RefCell::new(None));
            let position = Rc::new(RefCell::new(None));

            let data = {
                let mut applier = applier_host.borrow_typed();
                match applier.with_node::<LayoutNode, _>(child_id, |n| {
                    (
                        n.cache_handles(),
                        n.layout_state_handle(),
                        n.needs_layout(),
                        n.needs_measure(),
                    )
                }) {
                    Ok((cache, state, needs_layout, needs_measure)) => {
                        Some((cache, Some(state), needs_layout, needs_measure))
                    }
                    Err(NodeError::TypeMismatch { .. }) => {
                        match applier.with_node::<SubcomposeLayoutNode, _>(child_id, |n| {
                            (n.needs_layout(), n.needs_measure())
                        }) {
                            Ok((needs_layout, needs_measure)) => Some((
                                LayoutNodeCacheHandles::default(),
                                None,
                                needs_layout,
                                needs_measure,
                            )),
                            Err(NodeError::TypeMismatch { .. }) => None,
                            Err(NodeError::Missing { .. }) => None,
                            Err(err) => return Err(err),
                        }
                    }
                    Err(NodeError::Missing { .. }) => None,
                    Err(err) => return Err(err),
                }
            };

            let Some((cache_handles, layout_state, needs_layout, needs_measure)) = data else {
                continue;
            };

            cache_handles.activate(cache_epoch);

            records.push((
                child_id,
                ChildRecord {
                    measured: Rc::clone(&measured),
                    last_position: Rc::clone(&position),
                },
            ));
            measurables.push(Box::new(LayoutChildMeasurable::new(
                Rc::clone(&applier_host),
                child_id,
                measured,
                position,
                Rc::clone(&error),
                runtime_handle.clone(),
                cache_handles,
                cache_epoch,
                needs_layout || needs_measure,
                Some(measure_handle.clone()),
                layout_state,
            )));
        }

        let chain_constraints = constraints;

        let modifier_chain_result = Self::measure_through_modifier_chain(
            &state_rc,
            node_id,
            measurables.as_slice(),
            &measure_policy,
            chain_constraints,
            layout_node_data,
        );

        // Modifier chain always succeeds - use the node-driven measurement.
        let (width, height, policy_result, content_offset, offset) = {
            let result = modifier_chain_result;
            // The size is already correct from the modifier chain (modifiers like SizeNode
            // have already enforced their constraints), so we use it directly.
            if let Some(err) = error.borrow_mut().take() {
                return Err(err);
            }

            (
                result.result.size.width,
                result.result.size.height,
                result.result,
                result.content_offset,
                result.offset,
            )
        };

        let mut measured_children = Vec::with_capacity(records.len());
        for (child_id, record) in records.iter() {
            if let Some(measured) = record.measured.borrow_mut().take() {
                let base_position = policy_result
                    .placements
                    .iter()
                    .find(|placement| placement.node_id == *child_id)
                    .map(|placement| Point {
                        x: placement.x,
                        y: placement.y,
                    })
                    .or_else(|| record.last_position.borrow().as_ref().copied())
                    .unwrap_or(Point { x: 0.0, y: 0.0 });
                // Apply content_offset (from scroll/transforms) to child positioning
                let position = Point {
                    x: content_offset.x + base_position.x,
                    y: content_offset.y + base_position.y,
                };
                measured_children.push(MeasuredChild {
                    node: measured,
                    offset: position,
                });
            }
        }

        let measured = Rc::new(MeasuredNode::new(
            node_id,
            Size { width, height },
            offset,
            content_offset,
            measured_children,
        ));

        cache.store_measurement(constraints, Rc::clone(&measured));

        // Clear dirty flags and update derived state
        Self::with_applier_result(&state_rc, |applier| {
            applier.with_node::<LayoutNode, _>(node_id, |node| {
                node.clear_needs_measure();
                node.clear_needs_layout();
                node.set_measured_size(Size { width, height });
                node.set_content_offset(content_offset);
            })
        })
        .ok();

        Ok(measured)
    }
}

/// Snapshot of a LayoutNode's data for measuring.
/// This is a temporary copy used during the measure phase, not a live node.
///
/// Note: We capture `needs_measure` here because it's checked during measure to enable
/// selective measure optimization at the individual node level. Even if the tree is partially
/// dirty (some nodes changed), clean nodes can skip measure and use cached results.
struct LayoutNodeSnapshot {
    measure_policy: Rc<dyn MeasurePolicy>,
    cache: LayoutNodeCacheHandles,
    needs_layout: bool,
    /// Whether this specific node needs to be measured (vs using cached measurement)
    needs_measure: bool,
}

impl LayoutNodeSnapshot {
    fn from_layout_node(node: &LayoutNode) -> Self {
        Self {
            measure_policy: Rc::clone(&node.measure_policy),
            cache: node.cache_handles(),
            needs_layout: node.needs_layout(),
            needs_measure: node.needs_measure(),
        }
    }
}

// Helper types for accessing subsets of LayoutBuilderState
struct VecPools {
    state: Rc<RefCell<LayoutBuilderState>>,
    measurables: Option<Vec<Box<dyn Measurable>>>,
    records: Option<Vec<(NodeId, ChildRecord)>>,
    child_ids: Option<Vec<NodeId>>,
    layout_node_data: Option<Vec<LayoutModifierNodeData>>,
}

impl VecPools {
    fn acquire(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
        let (measurables, records, child_ids, layout_node_data) = {
            let mut state_mut = state.borrow_mut();
            (
                state_mut.tmp_measurables.acquire(),
                state_mut.tmp_records.acquire(),
                state_mut.tmp_child_ids.acquire(),
                state_mut.tmp_layout_node_data.acquire(),
            )
        };
        Self {
            state,
            measurables: Some(measurables),
            records: Some(records),
            child_ids: Some(child_ids),
            layout_node_data: Some(layout_node_data),
        }
    }

    #[allow(clippy::type_complexity)] // Returns internal Vec references for layout operations
    fn parts(
        &mut self,
    ) -> (
        &mut Vec<Box<dyn Measurable>>,
        &mut Vec<(NodeId, ChildRecord)>,
        &mut Vec<NodeId>,
        &mut Vec<LayoutModifierNodeData>,
    ) {
        let measurables = self
            .measurables
            .as_mut()
            .expect("measurables already returned");
        let records = self.records.as_mut().expect("records already returned");
        let child_ids = self.child_ids.as_mut().expect("child_ids already returned");
        let layout_node_data = self
            .layout_node_data
            .as_mut()
            .expect("layout_node_data already returned");
        (measurables, records, child_ids, layout_node_data)
    }
}

impl Drop for VecPools {
    fn drop(&mut self) {
        let mut state = self.state.borrow_mut();
        if let Some(measurables) = self.measurables.take() {
            state.tmp_measurables.release(measurables);
        }
        if let Some(records) = self.records.take() {
            state.tmp_records.release(records);
        }
        if let Some(child_ids) = self.child_ids.take() {
            state.tmp_child_ids.release(child_ids);
        }
        if let Some(layout_node_data) = self.layout_node_data.take() {
            state.tmp_layout_node_data.release(layout_node_data);
        }
    }
}

struct SlotsGuard {
    state: Rc<RefCell<LayoutBuilderState>>,
    slots: Option<SlotTable>,
}

impl SlotsGuard {
    fn take(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
        let slots = {
            let state_ref = state.borrow();
            let mut slots_ref = state_ref.slots.borrow_mut();
            std::mem::take(&mut *slots_ref)
        };
        Self {
            state,
            slots: Some(slots),
        }
    }

    fn host(&mut self) -> Rc<SlotsHost> {
        let slots = self.slots.take().unwrap_or_default();
        Rc::new(SlotsHost::new(slots))
    }

    fn restore(&mut self, slots: SlotTable) {
        debug_assert!(self.slots.is_none());
        self.slots = Some(slots);
    }
}

impl Drop for SlotsGuard {
    fn drop(&mut self) {
        if let Some(slots) = self.slots.take() {
            let state_ref = self.state.borrow();
            *state_ref.slots.borrow_mut() = slots;
        }
    }
}

#[derive(Clone)]
struct LayoutMeasureHandle {
    state: Rc<RefCell<LayoutBuilderState>>,
}

impl LayoutMeasureHandle {
    fn new(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
        Self { state }
    }

    fn measure(
        &self,
        node_id: NodeId,
        constraints: Constraints,
    ) -> Result<Rc<MeasuredNode>, NodeError> {
        LayoutBuilderState::measure_node(Rc::clone(&self.state), node_id, constraints)
    }
}

#[derive(Debug, Clone)]
pub(crate) struct MeasuredNode {
    node_id: NodeId,
    size: Size,
    /// Node's position offset relative to parent (from OffsetNode etc.)
    offset: Point,
    /// Content offset for scroll/inner transforms (NOT node position)
    content_offset: Point,
    children: Vec<MeasuredChild>,
}

impl MeasuredNode {
    fn new(
        node_id: NodeId,
        size: Size,
        offset: Point,
        content_offset: Point,
        children: Vec<MeasuredChild>,
    ) -> Self {
        Self {
            node_id,
            size,
            offset,
            content_offset,
            children,
        }
    }
}

#[derive(Debug, Clone)]
struct MeasuredChild {
    node: Rc<MeasuredNode>,
    offset: Point,
}

struct ChildRecord {
    measured: Rc<RefCell<Option<Rc<MeasuredNode>>>>,
    last_position: Rc<RefCell<Option<Point>>>,
}

struct LayoutChildMeasurable {
    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
    node_id: NodeId,
    measured: Rc<RefCell<Option<Rc<MeasuredNode>>>>,
    last_position: Rc<RefCell<Option<Point>>>,
    error: Rc<RefCell<Option<NodeError>>>,
    runtime_handle: Option<RuntimeHandle>,
    cache: LayoutNodeCacheHandles,
    cache_epoch: u64,
    force_remeasure: Cell<bool>,
    measure_handle: Option<LayoutMeasureHandle>,
    layout_state: Option<Rc<RefCell<crate::widgets::nodes::layout_node::LayoutState>>>,
}

impl LayoutChildMeasurable {
    #[allow(clippy::too_many_arguments)] // Constructor needs all layout state for child measurement
    fn new(
        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
        node_id: NodeId,
        measured: Rc<RefCell<Option<Rc<MeasuredNode>>>>,
        last_position: Rc<RefCell<Option<Point>>>,
        error: Rc<RefCell<Option<NodeError>>>,
        runtime_handle: Option<RuntimeHandle>,
        cache: LayoutNodeCacheHandles,
        cache_epoch: u64,
        force_remeasure: bool,
        measure_handle: Option<LayoutMeasureHandle>,
        layout_state: Option<Rc<RefCell<crate::widgets::nodes::layout_node::LayoutState>>>,
    ) -> Self {
        cache.activate(cache_epoch);
        Self {
            applier,
            node_id,
            measured,
            last_position,
            error,
            runtime_handle,
            cache,
            cache_epoch,
            force_remeasure: Cell::new(force_remeasure),
            measure_handle,
            layout_state,
        }
    }

    fn record_error(&self, err: NodeError) {
        let mut slot = self.error.borrow_mut();
        if slot.is_none() {
            *slot = Some(err);
        }
    }

    fn perform_measure(&self, constraints: Constraints) -> Result<Rc<MeasuredNode>, NodeError> {
        if let Some(handle) = &self.measure_handle {
            handle.measure(self.node_id, constraints)
        } else {
            measure_node_with_host(
                Rc::clone(&self.applier),
                self.runtime_handle.clone(),
                self.node_id,
                constraints,
                self.cache_epoch,
            )
        }
    }

    fn intrinsic_measure(&self, constraints: Constraints) -> Option<Rc<MeasuredNode>> {
        self.cache.activate(self.cache_epoch);
        if !self.force_remeasure.get() {
            if let Some(cached) = self.cache.get_measurement(constraints) {
                return Some(cached);
            }
        }

        match self.perform_measure(constraints) {
            Ok(measured) => {
                self.force_remeasure.set(false);
                self.cache
                    .store_measurement(constraints, Rc::clone(&measured));
                Some(measured)
            }
            Err(err) => {
                self.record_error(err);
                None
            }
        }
    }
}

impl Measurable for LayoutChildMeasurable {
    fn measure(&self, constraints: Constraints) -> Placeable {
        self.cache.activate(self.cache_epoch);
        let measured_size;
        if !self.force_remeasure.get() {
            if let Some(cached) = self.cache.get_measurement(constraints) {
                measured_size = cached.size;
                *self.measured.borrow_mut() = Some(Rc::clone(&cached));
            } else {
                match self.perform_measure(constraints) {
                    Ok(measured) => {
                        self.force_remeasure.set(false);
                        measured_size = measured.size;
                        self.cache
                            .store_measurement(constraints, Rc::clone(&measured));
                        *self.measured.borrow_mut() = Some(measured);
                    }
                    Err(err) => {
                        self.record_error(err);
                        self.measured.borrow_mut().take();
                        measured_size = Size {
                            width: 0.0,
                            height: 0.0,
                        };
                    }
                }
            }
        } else {
            match self.perform_measure(constraints) {
                Ok(measured) => {
                    self.force_remeasure.set(false);
                    measured_size = measured.size;
                    self.cache
                        .store_measurement(constraints, Rc::clone(&measured));
                    *self.measured.borrow_mut() = Some(measured);
                }
                Err(err) => {
                    self.record_error(err);
                    self.measured.borrow_mut().take();
                    measured_size = Size {
                        width: 0.0,
                        height: 0.0,
                    };
                }
            }
        }

        // Update retained LayoutNode state with measured size (new architecture).
        // PRIORITIZE direct handle to avoid Applier borrow conflicts during layout!
        if let Some(state) = &self.layout_state {
            let mut state = state.borrow_mut();
            state.size = measured_size;
            state.measurement_constraints = constraints;
        } else if let Ok(mut applier) = self.applier.try_borrow_typed() {
            let _ = applier.with_node::<LayoutNode, _>(self.node_id, |node| {
                node.set_measured_size(measured_size);
                node.set_measurement_constraints(constraints);
            });
        }

        // Build the place closure that captures all state needed for placement
        let applier = Rc::clone(&self.applier);
        let node_id = self.node_id;
        let measured = Rc::clone(&self.measured);
        let last_position = Rc::clone(&self.last_position);
        let layout_state = self.layout_state.clone();

        let place_fn = Rc::new(move |x: f32, y: f32| {
            // Retrieve the node's own offset (from modifiers like offset(), padding(), etc.)
            let internal_offset = measured
                .borrow()
                .as_ref()
                .map(|m| m.offset)
                .unwrap_or_default();

            let position = Point {
                x: x + internal_offset.x,
                y: y + internal_offset.y,
            };
            *last_position.borrow_mut() = Some(position);

            // Update retained LayoutNode state
            if let Some(state) = &layout_state {
                let mut state = state.borrow_mut();
                state.position = position;
                state.is_placed = true;
            } else if let Ok(mut applier) = applier.try_borrow_typed() {
                if applier
                    .with_node::<LayoutNode, _>(node_id, |node| {
                        node.set_position(position);
                    })
                    .is_err()
                {
                    let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
                        node.set_position(position);
                    });
                }
            }
        });

        Placeable::with_place_fn(
            measured_size.width,
            measured_size.height,
            self.node_id,
            place_fn,
        )
    }

    fn min_intrinsic_width(&self, height: f32) -> f32 {
        let kind = IntrinsicKind::MinWidth(height);
        self.cache.activate(self.cache_epoch);
        if !self.force_remeasure.get() {
            if let Some(value) = self.cache.get_intrinsic(&kind) {
                return value;
            }
        }
        let constraints = Constraints {
            min_width: 0.0,
            max_width: f32::INFINITY,
            min_height: height,
            max_height: height,
        };
        if let Some(node) = self.intrinsic_measure(constraints) {
            let value = node.size.width;
            self.cache.store_intrinsic(kind, value);
            value
        } else {
            0.0
        }
    }

    fn max_intrinsic_width(&self, height: f32) -> f32 {
        let kind = IntrinsicKind::MaxWidth(height);
        self.cache.activate(self.cache_epoch);
        if !self.force_remeasure.get() {
            if let Some(value) = self.cache.get_intrinsic(&kind) {
                return value;
            }
        }
        let constraints = Constraints {
            min_width: 0.0,
            max_width: f32::INFINITY,
            min_height: 0.0,
            max_height: height,
        };
        if let Some(node) = self.intrinsic_measure(constraints) {
            let value = node.size.width;
            self.cache.store_intrinsic(kind, value);
            value
        } else {
            0.0
        }
    }

    fn min_intrinsic_height(&self, width: f32) -> f32 {
        let kind = IntrinsicKind::MinHeight(width);
        self.cache.activate(self.cache_epoch);
        if !self.force_remeasure.get() {
            if let Some(value) = self.cache.get_intrinsic(&kind) {
                return value;
            }
        }
        let constraints = Constraints {
            min_width: width,
            max_width: width,
            min_height: 0.0,
            max_height: f32::INFINITY,
        };
        if let Some(node) = self.intrinsic_measure(constraints) {
            let value = node.size.height;
            self.cache.store_intrinsic(kind, value);
            value
        } else {
            0.0
        }
    }

    fn max_intrinsic_height(&self, width: f32) -> f32 {
        let kind = IntrinsicKind::MaxHeight(width);
        self.cache.activate(self.cache_epoch);
        if !self.force_remeasure.get() {
            if let Some(value) = self.cache.get_intrinsic(&kind) {
                return value;
            }
        }
        let constraints = Constraints {
            min_width: 0.0,
            max_width: width,
            min_height: 0.0,
            max_height: f32::INFINITY,
        };
        if let Some(node) = self.intrinsic_measure(constraints) {
            let value = node.size.height;
            self.cache.store_intrinsic(kind, value);
            value
        } else {
            0.0
        }
    }

    fn flex_parent_data(&self) -> Option<cranpose_ui_layout::FlexParentData> {
        // Try to borrow the applier - if it's already borrowed (nested measurement), return None.
        // This is safe because parent data doesn't change during measurement.
        let Ok(mut applier) = self.applier.try_borrow_typed() else {
            return None;
        };

        applier
            .with_node::<LayoutNode, _>(self.node_id, |layout_node| {
                let props = layout_node.resolved_modifiers().layout_properties();
                props.weight().map(|weight_data| {
                    cranpose_ui_layout::FlexParentData::new(weight_data.weight, weight_data.fill)
                })
            })
            .ok()
            .flatten()
    }
}

fn measure_node_with_host(
    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
    runtime_handle: Option<RuntimeHandle>,
    node_id: NodeId,
    constraints: Constraints,
    epoch: u64,
) -> Result<Rc<MeasuredNode>, NodeError> {
    let runtime_handle = match runtime_handle {
        Some(handle) => Some(handle),
        None => applier.borrow_typed().runtime_handle(),
    };
    let mut builder =
        LayoutBuilder::new_with_epoch(applier, epoch, Rc::new(RefCell::new(SlotTable::default())));
    builder.set_runtime_handle(runtime_handle);
    builder.measure_node(node_id, constraints)
}

#[derive(Clone)]
struct RuntimeNodeMetadata {
    modifier: Modifier,
    resolved_modifiers: ResolvedModifiers,
    modifier_slices: Rc<ModifierNodeSlices>,
    role: SemanticsRole,
    button_handler: Option<Rc<RefCell<dyn FnMut()>>>,
}

impl Default for RuntimeNodeMetadata {
    fn default() -> Self {
        Self {
            modifier: Modifier::empty(),
            resolved_modifiers: ResolvedModifiers::default(),
            modifier_slices: Rc::default(),
            role: SemanticsRole::Unknown,
            button_handler: None,
        }
    }
}

fn role_from_modifier_slices(modifier_slices: &ModifierNodeSlices) -> SemanticsRole {
    modifier_slices
        .text_content()
        .map(|text| SemanticsRole::Text {
            value: text.to_string(),
        })
        .unwrap_or(SemanticsRole::Layout)
}

fn runtime_metadata_for(
    applier: &mut MemoryApplier,
    node_id: NodeId,
) -> Result<RuntimeNodeMetadata, NodeError> {
    // Try LayoutNode (the primary modern path)
    // IMPORTANT: We use with_node (reference) instead of try_clone because cloning
    // LayoutNode creates a NEW ModifierChainHandle with NEW nodes and NEW handlers,
    // which would lose gesture state like press_position.
    if let Ok(meta) = applier.with_node::<LayoutNode, _>(node_id, |layout| {
        let modifier = layout.modifier.clone();
        let resolved_modifiers = layout.resolved_modifiers();
        let modifier_slices = layout.modifier_slices_snapshot();
        let role = role_from_modifier_slices(&modifier_slices);

        RuntimeNodeMetadata {
            modifier,
            resolved_modifiers,
            modifier_slices,
            role,
            button_handler: None,
        }
    }) {
        return Ok(meta);
    }

    // Try SubcomposeLayoutNode
    if let Ok((modifier, resolved_modifiers, modifier_slices)) = applier
        .with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
            (
                node.modifier(),
                node.resolved_modifiers(),
                node.modifier_slices_snapshot(),
            )
        })
    {
        return Ok(RuntimeNodeMetadata {
            modifier,
            resolved_modifiers,
            modifier_slices,
            role: SemanticsRole::Subcompose,
            button_handler: None,
        });
    }
    Ok(RuntimeNodeMetadata::default())
}

fn clear_semantics_dirty_flags(
    applier: &mut MemoryApplier,
    node: &MeasuredNode,
) -> Result<(), NodeError> {
    if let Err(err) = applier.with_node::<LayoutNode, _>(node.node_id, |layout| {
        layout.clear_needs_semantics();
    }) {
        match err {
            NodeError::Missing { .. } | NodeError::TypeMismatch { .. } => {}
            _ => return Err(err),
        }
    }

    for child in &node.children {
        clear_semantics_dirty_flags(applier, &child.node)?;
    }

    Ok(())
}

fn build_semantics_tree_from_live_nodes(
    applier: &mut MemoryApplier,
    node: &MeasuredNode,
) -> Result<SemanticsTree, NodeError> {
    Ok(SemanticsTree::new(build_semantics_node_from_live_nodes(
        applier, node,
    )?))
}

fn semantics_node_from_parts(
    node_id: NodeId,
    mut role: SemanticsRole,
    config: Option<SemanticsConfiguration>,
    children: Vec<SemanticsNode>,
) -> SemanticsNode {
    let mut actions = Vec::new();
    let mut description = None;

    if let Some(config) = config {
        if config.is_button {
            role = SemanticsRole::Button;
        }
        if config.is_clickable {
            actions.push(SemanticsAction::Click {
                handler: SemanticsCallback::new(node_id),
            });
        }
        description = config.content_description;
    }

    SemanticsNode::new(node_id, role, actions, children, description)
}

fn build_semantics_node_from_live_nodes(
    applier: &mut MemoryApplier,
    node: &MeasuredNode,
) -> Result<SemanticsNode, NodeError> {
    let (role, config) = match applier.with_node::<LayoutNode, _>(node.node_id, |layout| {
        let role = role_from_modifier_slices(&layout.modifier_slices_snapshot());
        let config = layout.semantics_configuration();
        layout.clear_needs_semantics();
        (role, config)
    }) {
        Ok(data) => data,
        Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {
            match applier.with_node::<SubcomposeLayoutNode, _>(node.node_id, |subcompose| {
                (
                    SemanticsRole::Subcompose,
                    collect_semantics_from_modifier(&subcompose.modifier()),
                )
            }) {
                Ok(data) => data,
                Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {
                    (SemanticsRole::Unknown, None)
                }
                Err(err) => return Err(err),
            }
        }
        Err(err) => return Err(err),
    };

    let mut children = Vec::with_capacity(node.children.len());
    for child in &node.children {
        children.push(build_semantics_node_from_live_nodes(applier, &child.node)?);
    }

    Ok(semantics_node_from_parts(
        node.node_id,
        role,
        config,
        children,
    ))
}

fn build_layout_tree(
    applier: &mut MemoryApplier,
    node: &MeasuredNode,
) -> Result<LayoutTree, NodeError> {
    fn place(
        applier: &mut MemoryApplier,
        node: &MeasuredNode,
        origin: Point,
    ) -> Result<LayoutBox, NodeError> {
        // Include the node's own offset (from OffsetNode) in its position
        let top_left = Point {
            x: origin.x + node.offset.x,
            y: origin.y + node.offset.y,
        };
        let rect = GeometryRect {
            x: top_left.x,
            y: top_left.y,
            width: node.size.width,
            height: node.size.height,
        };
        let info = runtime_metadata_for(applier, node.node_id)?;
        let kind = layout_kind_from_metadata(node.node_id, &info);
        let RuntimeNodeMetadata {
            modifier,
            resolved_modifiers,
            modifier_slices,
            ..
        } = info;
        let data = LayoutNodeData::new(modifier, resolved_modifiers, modifier_slices, kind);
        let mut children = Vec::with_capacity(node.children.len());
        for child in &node.children {
            let child_origin = Point {
                x: top_left.x + child.offset.x,
                y: top_left.y + child.offset.y,
            };
            children.push(place(applier, &child.node, child_origin)?);
        }
        Ok(LayoutBox::new(
            node.node_id,
            rect,
            node.content_offset,
            data,
            children,
        ))
    }

    Ok(LayoutTree::new(place(
        applier,
        node,
        Point { x: 0.0, y: 0.0 },
    )?))
}

fn semantics_role_from_layout_box(layout_box: &LayoutBox) -> SemanticsRole {
    match &layout_box.node_data.kind {
        LayoutNodeKind::Subcompose => SemanticsRole::Subcompose,
        LayoutNodeKind::Spacer => SemanticsRole::Spacer,
        LayoutNodeKind::Unknown => SemanticsRole::Unknown,
        LayoutNodeKind::Button { .. } => SemanticsRole::Button,
        LayoutNodeKind::Layout => layout_box
            .node_data
            .modifier_slices()
            .text_content()
            .map(|text| SemanticsRole::Text {
                value: text.to_string(),
            })
            .unwrap_or(SemanticsRole::Layout),
    }
}

fn build_semantics_node_from_layout_box(layout_box: &LayoutBox) -> SemanticsNode {
    let children = layout_box
        .children
        .iter()
        .map(build_semantics_node_from_layout_box)
        .collect();

    semantics_node_from_parts(
        layout_box.node_id,
        semantics_role_from_layout_box(layout_box),
        collect_semantics_from_modifier(&layout_box.node_data.modifier),
        children,
    )
}

fn layout_kind_from_metadata(_node_id: NodeId, info: &RuntimeNodeMetadata) -> LayoutNodeKind {
    match &info.role {
        SemanticsRole::Layout => LayoutNodeKind::Layout,
        SemanticsRole::Subcompose => LayoutNodeKind::Subcompose,
        SemanticsRole::Text { .. } => {
            // Text content is now handled via TextModifierNode in the modifier chain
            // and collected in modifier_slices.text_content(). LayoutNodeKind should
            // reflect the layout policy (EmptyMeasurePolicy), not the content type.
            LayoutNodeKind::Layout
        }
        SemanticsRole::Spacer => LayoutNodeKind::Spacer,
        SemanticsRole::Button => {
            let handler = info
                .button_handler
                .as_ref()
                .cloned()
                .unwrap_or_else(|| Rc::new(RefCell::new(|| {})));
            LayoutNodeKind::Button { on_click: handler }
        }
        SemanticsRole::Unknown => LayoutNodeKind::Unknown,
    }
}

fn subtract_padding(constraints: Constraints, padding: EdgeInsets) -> Constraints {
    let horizontal = padding.horizontal_sum();
    let vertical = padding.vertical_sum();
    let min_width = (constraints.min_width - horizontal).max(0.0);
    let mut max_width = constraints.max_width;
    if max_width.is_finite() {
        max_width = (max_width - horizontal).max(0.0);
    }
    let min_height = (constraints.min_height - vertical).max(0.0);
    let mut max_height = constraints.max_height;
    if max_height.is_finite() {
        max_height = (max_height - vertical).max(0.0);
    }
    normalize_constraints(Constraints {
        min_width,
        max_width,
        min_height,
        max_height,
    })
}

#[cfg(test)]
pub(crate) fn align_horizontal(alignment: HorizontalAlignment, available: f32, child: f32) -> f32 {
    match alignment {
        HorizontalAlignment::Start => 0.0,
        HorizontalAlignment::CenterHorizontally => ((available - child) / 2.0).max(0.0),
        HorizontalAlignment::End => (available - child).max(0.0),
    }
}

#[cfg(test)]
pub(crate) fn align_vertical(alignment: VerticalAlignment, available: f32, child: f32) -> f32 {
    match alignment {
        VerticalAlignment::Top => 0.0,
        VerticalAlignment::CenterVertically => ((available - child) / 2.0).max(0.0),
        VerticalAlignment::Bottom => (available - child).max(0.0),
    }
}

fn resolve_dimension(
    base: f32,
    explicit: DimensionConstraint,
    min_override: Option<f32>,
    max_override: Option<f32>,
    min_limit: f32,
    max_limit: f32,
) -> f32 {
    let mut min_bound = min_limit;
    if let Some(min_value) = min_override {
        min_bound = min_bound.max(min_value);
    }

    let mut max_bound = if max_limit.is_finite() {
        max_limit
    } else {
        max_override.unwrap_or(max_limit)
    };
    if let Some(max_value) = max_override {
        if max_bound.is_finite() {
            max_bound = max_bound.min(max_value);
        } else {
            max_bound = max_value;
        }
    }
    if max_bound < min_bound {
        max_bound = min_bound;
    }

    let mut size = match explicit {
        DimensionConstraint::Points(points) => points,
        DimensionConstraint::Fraction(fraction) => {
            if max_limit.is_finite() {
                max_limit * fraction.clamp(0.0, 1.0)
            } else {
                base
            }
        }
        DimensionConstraint::Unspecified => base,
        // Intrinsic sizing is resolved at a higher level where we have access to children.
        // At this point we just use the base size as a fallback.
        DimensionConstraint::Intrinsic(_) => base,
    };

    size = clamp_dimension(size, min_bound, max_bound);
    size = clamp_dimension(size, min_limit, max_limit);
    size.max(0.0)
}

fn clamp_dimension(value: f32, min: f32, max: f32) -> f32 {
    let mut result = value.max(min);
    if max.is_finite() {
        result = result.min(max);
    }
    result
}

fn normalize_constraints(mut constraints: Constraints) -> Constraints {
    if constraints.max_width < constraints.min_width {
        constraints.max_width = constraints.min_width;
    }
    if constraints.max_height < constraints.min_height {
        constraints.max_height = constraints.min_height;
    }
    constraints
}

#[cfg(test)]
#[path = "tests/layout_tests.rs"]
mod tests;