pax-runtime 0.39.0

Core shared runtime and rendering engine for Pax
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
use crate::api::math::Point2;
use crate::api::Window;
use crate::constants::{ACCEL_HANDLERS, GYRO_HANDLERS, PRE_RENDER_HANDLERS, TICK_HANDLERS};
use pax_language::interpreter::property_resolution::IdentifierResolver;
use pax_manifest::cartridge_generation::{TRANSITION_PHASE_ENTER, TRANSITION_PHASE_EXIT};
use pax_manifest::UniqueTemplateNodeIdentifier;
use pax_message::{NativeMessage, ScreenshotData};
use pax_runtime_api::properties::{
    drain_effects, drain_effects_with_report, property_has_direct_outbound,
    property_outbound_debug_names, register_effect_property, register_effect_property_with_name,
    UntypedProperty,
};
use pax_runtime_api::{
    borrow, borrow_mut, use_RefCell, Event, Interpolatable, LightShape, MouseOut, MouseOver,
    Property, RenderContext, SceneLight, SceneLighting, Store, Variable,
};
use_RefCell!();
use kurbo::{Affine, Point};
use std::any::{Any, TypeId};
use std::cell::Cell;
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::rc::{Rc, Weak};
use std::sync::OnceLock;
use std::time::Instant;

use crate::{ExpandedNode, Globals};

#[cfg(feature = "designtime")]
use crate::{ComponentInstance, InstanceNode};

fn effect_drain_instrumentation_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var("PAX_EFFECT_DRAIN_INSTRUMENTATION")
            .map(|value| {
                let value = value.to_ascii_lowercase();
                matches!(value.as_str(), "1" | "true" | "yes" | "on")
            })
            .unwrap_or(false)
    })
}

fn format_effect_top(top_effects: &[(String, usize)]) -> String {
    top_effects
        .iter()
        .map(|(name, count)| format!("{count}x {name}"))
        .collect::<Vec<_>>()
        .join("; ")
}

fn summarize_debug_names(names: Vec<String>) -> Vec<(String, usize)> {
    let mut counts = HashMap::new();
    for name in names {
        let category = name
            .split_once(" (")
            .map(|(category, _)| category)
            .unwrap_or(&name)
            .to_owned();
        *counts.entry(category).or_insert(0) += 1;
    }
    let mut counts = counts.into_iter().collect::<Vec<_>>();
    counts.sort_by(|(left_name, left_count), (right_name, right_count)| {
        right_count
            .cmp(left_count)
            .then_with(|| left_name.cmp(right_name))
    });
    counts
}

impl Interpolatable for ExpandedNodeIdentifier {}

fn transform_scene_light_for_layer(
    mut light: SceneLight,
    source_to_root: Affine,
    root_to_target: Affine,
) -> SceneLight {
    if matches!(light.shape, LightShape::Point) {
        let point = Point::new(light.position.x, light.position.y);
        let point = root_to_target * (source_to_root * point);
        light.position.x = point.x;
        light.position.y = point.y;
    }
    light
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// Stable runtime identifier assigned to an expanded node.
pub struct ExpandedNodeIdentifier(pub u32);

impl ExpandedNodeIdentifier {
    /// Convert to the integer id passed across chassis message boundaries.
    pub fn to_u32(&self) -> u32 {
        self.0
    }
}

/// Shared context for properties pass recursion
pub struct RuntimeContext {
    next_uid: Cell<ExpandedNodeIdentifier>,
    messages: RefCell<Vec<NativeMessage>>,
    globals: RefCell<Globals>,
    root_expanded_node: RefCell<Weak<ExpandedNode>>,
    #[cfg(feature = "designtime")]
    pub userland_frame_instance_node: RefCell<Option<Rc<dyn InstanceNode>>>,
    #[cfg(feature = "designtime")]
    pub userland_root_expanded_node: RefCell<Option<Rc<ExpandedNode>>>,
    node_cache: RefCell<NodeCache>,
    last_topmost_element: RefCell<Weak<ExpandedNode>>,
    active_touch_targets: RefCell<HashMap<i64, ExpandedNodeIdentifier>>,
    queued_custom_events: RefCell<Vec<(Rc<ExpandedNode>, &'static str)>>,
    queued_renders: RefCell<Vec<Rc<ExpandedNode>>>,
    pub layer_count: Cell<usize>,
    pub dirty_canvases: Rc<RefCell<Vec<bool>>>,
    dirty_canvas_nodes: RefCell<HashSet<ExpandedNodeIdentifier>>,
    canvas_node_light_masks: RefCell<HashMap<ExpandedNodeIdentifier, u32>>,
    lighting_overflow_counts: RefCell<HashMap<usize, usize>>,
    targeted_canvas_replay_node_ids: RefCell<HashMap<usize, HashSet<u32>>>,
    removed_canvas_nodes: RefCell<Vec<(usize, u32)>>,
    occlusion_dirty: Cell<bool>,
    layer_canvas_plan_generation: Cell<u32>,
    canvas_drawable_layers: RefCell<HashSet<usize>>,
    import_settings_node_count: Cell<usize>,
    tick_handler_nodes: RefCell<Vec<ExpandedNodeIdentifier>>,
    pre_render_handler_nodes: RefCell<Vec<ExpandedNodeIdentifier>>,
    gyro_handler_nodes: RefCell<Vec<ExpandedNodeIdentifier>>,
    accel_handler_nodes: RefCell<Vec<ExpandedNodeIdentifier>>,
    screenshot_map: Rc<RefCell<HashMap<u32, ScreenshotData>>>,
    scroller_surface_states: RefCell<HashMap<u32, ScrollerSurfaceState>>,
    layer_scroller_owners: RefCell<HashMap<usize, ExpandedNodeIdentifier>>,
    root_scroller_id: Cell<Option<u32>>,
    visual_viewport_state: Cell<Option<VisualViewportState>>,
}

#[derive(Clone, Copy, Debug, Default)]
/// Last-known scroll state for a native or browser-owned scroller surface.
pub struct ScrollerSurfaceState {
    pub viewport_width: f64,
    pub viewport_height: f64,
    pub content_width: f64,
    pub content_height: f64,
    pub scroll_x: f64,
    pub scroll_y: f64,
    pub presentation_scroll_x: f64,
    pub presentation_scroll_y: f64,
    pub clip_content: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Coarse classification of changes to a native or browser-owned scroller surface.
pub enum ScrollerSurfaceStateChange {
    Unchanged,
    ScrollOnly,
    Structural,
}

impl ScrollerSurfaceState {
    fn close_enough(lhs: f64, rhs: f64) -> bool {
        (lhs - rhs).abs() <= 1e-4
    }

    fn structural_eq(&self, other: &Self) -> bool {
        Self::close_enough(self.viewport_width, other.viewport_width)
            && Self::close_enough(self.viewport_height, other.viewport_height)
            && Self::close_enough(self.content_width, other.content_width)
            && Self::close_enough(self.content_height, other.content_height)
            && self.clip_content == other.clip_content
    }

    fn scroll_eq(&self, other: &Self) -> bool {
        Self::close_enough(self.scroll_x, other.scroll_x)
            && Self::close_enough(self.scroll_y, other.scroll_y)
            && Self::close_enough(self.presentation_scroll_x, other.presentation_scroll_x)
            && Self::close_enough(self.presentation_scroll_y, other.presentation_scroll_y)
    }
}

#[derive(Clone, Copy, Debug, Default)]
/// Browser visual viewport state used when page scrolling participates in root scroller behavior.
pub struct VisualViewportState {
    pub width: f64,
    pub height: f64,
    pub offset_x: f64,
    pub offset_y: f64,
    pub page_scroll_x: f64,
    pub page_scroll_y: f64,
}

struct NodeCache {
    eid_to_node: HashMap<ExpandedNodeIdentifier, Rc<ExpandedNode>>,
    uni_to_eid: HashMap<UniqueTemplateNodeIdentifier, Vec<ExpandedNodeIdentifier>>,
}

impl NodeCache {
    fn new() -> Self {
        Self {
            eid_to_node: Default::default(),
            uni_to_eid: Default::default(),
        }
    }

    // Add this node to all relevant constant lookup cache structures
    fn add_to_cache(&mut self, node: &Rc<ExpandedNode>) {
        self.eid_to_node.insert(node.id, Rc::clone(&node));
        let uni = borrow!(node.instance_node)
            .base()
            .template_node_identifier
            .clone();
        if let Some(uni) = uni {
            self.uni_to_eid.entry(uni).or_default().push(node.id);
        }
    }

    // Remove this node from all relevant constant lookup cache structures
    fn remove_from_cache(&mut self, node: &Rc<ExpandedNode>) {
        self.eid_to_node.remove(&node.id);
        if let Some(uni) = &borrow!(node.instance_node).base().template_node_identifier {
            self.uni_to_eid
                .entry(uni.clone())
                .or_default()
                .retain(|&n| n != node.id);
        }
    }
}

impl RuntimeContext {
    #[cfg(not(feature = "designtime"))]
    /// Create a runtime context for normal app execution.
    pub fn new(globals: Globals) -> Self {
        Self {
            next_uid: Cell::new(ExpandedNodeIdentifier(0)),
            messages: RefCell::new(Vec::new()),
            globals: RefCell::new(globals),
            root_expanded_node: RefCell::new(Weak::new()),
            node_cache: RefCell::new(NodeCache::new()),
            active_touch_targets: Default::default(),
            queued_custom_events: Default::default(),
            queued_renders: Default::default(),
            layer_count: Cell::default(),
            last_topmost_element: Default::default(),
            dirty_canvases: Default::default(),
            dirty_canvas_nodes: Default::default(),
            canvas_node_light_masks: Default::default(),
            lighting_overflow_counts: Default::default(),
            targeted_canvas_replay_node_ids: Default::default(),
            removed_canvas_nodes: Default::default(),
            occlusion_dirty: Cell::new(true),
            layer_canvas_plan_generation: Cell::new(1),
            canvas_drawable_layers: Default::default(),
            import_settings_node_count: Cell::new(0),
            tick_handler_nodes: Default::default(),
            pre_render_handler_nodes: Default::default(),
            gyro_handler_nodes: Default::default(),
            accel_handler_nodes: Default::default(),
            screenshot_map: Default::default(),
            scroller_surface_states: Default::default(),
            layer_scroller_owners: Default::default(),
            root_scroller_id: Cell::new(None),
            visual_viewport_state: Cell::new(None),
        }
    }

    #[cfg(feature = "designtime")]
    /// Create a runtime context with the userland component tracked for designtime tools.
    pub fn new(globals: Globals, userland: Rc<ComponentInstance>) -> Self {
        Self {
            next_uid: Cell::new(ExpandedNodeIdentifier(0)),
            messages: RefCell::new(Vec::new()),
            globals: RefCell::new(globals),
            root_expanded_node: RefCell::new(Weak::new()),
            userland_frame_instance_node: RefCell::new(Some(userland)),
            userland_root_expanded_node: Default::default(),
            node_cache: RefCell::new(NodeCache::new()),
            active_touch_targets: Default::default(),
            queued_custom_events: Default::default(),
            queued_renders: Default::default(),
            layer_count: Cell::default(),
            last_topmost_element: Default::default(),
            dirty_canvases: Default::default(),
            dirty_canvas_nodes: Default::default(),
            canvas_node_light_masks: Default::default(),
            lighting_overflow_counts: Default::default(),
            targeted_canvas_replay_node_ids: Default::default(),
            removed_canvas_nodes: Default::default(),
            occlusion_dirty: Cell::new(true),
            layer_canvas_plan_generation: Cell::new(1),
            canvas_drawable_layers: Default::default(),
            import_settings_node_count: Cell::new(0),
            tick_handler_nodes: Default::default(),
            pre_render_handler_nodes: Default::default(),
            gyro_handler_nodes: Default::default(),
            accel_handler_nodes: Default::default(),
            screenshot_map: Default::default(),
            scroller_surface_states: Default::default(),
            layer_scroller_owners: Default::default(),
            root_scroller_id: Cell::new(None),
            visual_viewport_state: Cell::new(None),
        }
    }

    #[cfg(feature = "designtime")]
    /// Create a runtime context before any userland tree has been mounted.
    pub fn new_empty(globals: Globals) -> Self {
        Self {
            next_uid: Cell::new(ExpandedNodeIdentifier(0)),
            messages: RefCell::new(Vec::new()),
            globals: RefCell::new(globals),
            root_expanded_node: RefCell::new(Weak::new()),
            userland_frame_instance_node: RefCell::new(None),
            userland_root_expanded_node: Default::default(),
            node_cache: RefCell::new(NodeCache::new()),
            active_touch_targets: Default::default(),
            queued_custom_events: Default::default(),
            queued_renders: Default::default(),
            layer_count: Cell::default(),
            last_topmost_element: Default::default(),
            dirty_canvases: Default::default(),
            dirty_canvas_nodes: Default::default(),
            canvas_node_light_masks: Default::default(),
            lighting_overflow_counts: Default::default(),
            targeted_canvas_replay_node_ids: Default::default(),
            removed_canvas_nodes: Default::default(),
            occlusion_dirty: Cell::new(true),
            layer_canvas_plan_generation: Cell::new(1),
            canvas_drawable_layers: Default::default(),
            import_settings_node_count: Cell::new(0),
            tick_handler_nodes: Default::default(),
            pre_render_handler_nodes: Default::default(),
            gyro_handler_nodes: Default::default(),
            accel_handler_nodes: Default::default(),
            screenshot_map: Default::default(),
            scroller_surface_states: Default::default(),
            layer_scroller_owners: Default::default(),
            root_scroller_id: Cell::new(None),
            visual_viewport_state: Cell::new(None),
        }
    }

    /// Store the root expanded node after it has been initialized.
    pub fn register_root_expanded_node(&self, root: &Rc<ExpandedNode>) {
        *borrow_mut!(self.root_expanded_node) = Rc::downgrade(root);
    }

    /// Clear the registered root expanded node.
    pub fn clear_root_expanded_node(&self) {
        *borrow_mut!(self.root_expanded_node) = Weak::new();
    }

    /// Add a node to runtime lookup caches.
    pub fn add_to_cache(&self, node: &Rc<ExpandedNode>) {
        borrow_mut!(self.node_cache).add_to_cache(node);
        self.register_node_lifecycle_handlers(node);
        if node.is_import_settings_node() {
            self.import_settings_node_count
                .set(self.import_settings_node_count.get() + 1);
        }
        self.mark_occlusion_dirty();
    }

    /// Remove a node from runtime lookup caches.
    pub fn remove_from_cache(&self, node: &Rc<ExpandedNode>) {
        borrow_mut!(self.node_cache).remove_from_cache(node);
        borrow_mut!(self.canvas_node_light_masks).remove(&node.id);
        borrow_mut!(self.active_touch_targets).retain(|_, target| *target != node.id);
        self.unregister_node_lifecycle_handlers(node.id);
        if node.is_import_settings_node() {
            self.import_settings_node_count
                .set(self.import_settings_node_count.get().saturating_sub(1));
        }
        self.mark_occlusion_dirty();
    }

    pub fn has_import_settings_nodes(&self) -> bool {
        self.import_settings_node_count.get() > 0
    }

    pub fn register_node_effect_property(&self, node: ExpandedNodeIdentifier, prop: &Property<()>) {
        self.register_node_effect_property_named(node, prop, "node effect");
    }

    pub fn register_node_effect_property_named(
        &self,
        node: ExpandedNodeIdentifier,
        prop: &Property<()>,
        effect_name: &str,
    ) {
        if effect_drain_instrumentation_enabled() {
            let debug_name = self.node_effect_debug_name(node, effect_name);
            register_effect_property_with_name(prop, &debug_name);
        } else {
            register_effect_property(prop);
        }
    }

    pub fn register_expanded_node_effect_property_named(
        &self,
        node: &Rc<ExpandedNode>,
        prop: &Property<()>,
        effect_name: &str,
    ) {
        if effect_drain_instrumentation_enabled() {
            let debug_name = Self::expanded_node_effect_debug_name(node, effect_name);
            register_effect_property_with_name(prop, &debug_name);
        } else {
            register_effect_property(prop);
        }
    }

    pub fn register_node_effect(
        &self,
        node: ExpandedNodeIdentifier,
        dependencies: &[UntypedProperty],
        effect: impl Fn() + 'static,
    ) -> Property<()> {
        let prop = Property::computed(effect, dependencies);
        self.register_node_effect_property_named(node, &prop, "registered node effect");
        prop
    }

    pub fn drain_node_effects(&self) {
        const MAX_NODE_EFFECTS_PER_TICK: usize = 100_000;
        if !effect_drain_instrumentation_enabled() {
            let drained = drain_effects(MAX_NODE_EFFECTS_PER_TICK);
            if drained == MAX_NODE_EFFECTS_PER_TICK {
                log::warn!(
                    "reactive drain hit {} evaluations in one tick; deferring remaining work",
                    MAX_NODE_EFFECTS_PER_TICK
                );
            }
            return;
        }

        let start = Instant::now();
        let report = drain_effects_with_report(MAX_NODE_EFFECTS_PER_TICK);
        let elapsed_ms = start.elapsed().as_secs_f64() * 1000.0;
        if report.budget_exhausted {
            log::warn!(
                "reactive drain hit {} evaluations in one tick; deferring remaining work",
                MAX_NODE_EFFECTS_PER_TICK
            );
        }
        if report.popped > 0 || report.cutoffs_evaluated > 0 {
            let globals = self.globals();
            let frame_dependents = summarize_debug_names(property_outbound_debug_names(
                &globals.elapsed_frames.untyped(),
            ));
            let millis_dependents = summarize_debug_names(property_outbound_debug_names(
                &globals.elapsed_millis.untyped(),
            ));
            let frame_clock = globals.elapsed_frames.untyped();
            let millis_clock = globals.elapsed_millis.untyped();
            let (entering, exiting, enter_cleanup, exit_cleanup, stale_clock_nodes) = {
                let nodes = borrow!(self.node_cache);
                nodes.eid_to_node.values().fold(
                    (0, 0, 0, 0, Vec::new()),
                    |(entering, exiting, enter_cleanup, exit_cleanup, mut stale_clock_nodes),
                     node| {
                        let phase = node.transition_phase.get();
                        if phase != TRANSITION_PHASE_ENTER
                            && phase != TRANSITION_PHASE_EXIT
                            && (property_has_direct_outbound(
                                &frame_clock,
                                &node.transition_playhead.untyped(),
                            ) || property_has_direct_outbound(
                                &millis_clock,
                                &node.transition_playhead_millis.untyped(),
                            ))
                        {
                            stale_clock_nodes
                                .push(Self::expanded_node_effect_debug_name(node, "idle clock"));
                        }
                        (
                            entering + usize::from(phase == TRANSITION_PHASE_ENTER),
                            exiting + usize::from(phase == TRANSITION_PHASE_EXIT),
                            enter_cleanup + usize::from(node.enter_cleanup_active.get()),
                            exit_cleanup + usize::from(node.exit_cleanup_active.get()),
                            stale_clock_nodes,
                        )
                    },
                )
            };
            println!(
                "[PaxEffectDrain] ran={} popped={} clean={} unregistered={} missing={} remaining={} cutoffs={} suppressed={} propagated={} remaining_cutoffs={} elapsed_ms={:.3} transitions=enter:{}/exit:{}/enter_cleanup:{}/exit_cleanup:{} stale_clocks=[{}] frame_deps=[{}] millis_deps=[{}] effects=[{}] cutoff_nodes=[{}]",
                report.ran,
                report.popped,
                report.skipped_clean,
                report.skipped_unregistered,
                report.skipped_missing,
                report.remaining,
                report.cutoffs_evaluated,
                report.cutoffs_suppressed,
                report.cutoffs_propagated,
                report.remaining_cutoffs,
                elapsed_ms,
                entering,
                exiting,
                enter_cleanup,
                exit_cleanup,
                stale_clock_nodes.join("; "),
                format_effect_top(&frame_dependents),
                format_effect_top(&millis_dependents),
                format_effect_top(&report.top_effects),
                format_effect_top(&report.top_cutoffs),
            );
        }
    }

    fn node_effect_debug_name(&self, node: ExpandedNodeIdentifier, effect_name: &str) -> String {
        let node = borrow!(self.node_cache).eid_to_node.get(&node).cloned();
        if let Some(node) = node {
            Self::expanded_node_effect_debug_name(&node, effect_name)
        } else {
            format!("{effect_name} node={node:?}")
        }
    }

    fn expanded_node_effect_debug_name(node: &Rc<ExpandedNode>, effect_name: &str) -> String {
        let instance_node = borrow!(node.instance_node);
        let base = instance_node.base();
        let type_name = base
            .template_node_type_id
            .as_ref()
            .map(|type_id| {
                type_id
                    .get_pascal_identifier()
                    .unwrap_or_else(|| type_id.to_string())
            })
            .unwrap_or_else(|| "<unknown>".to_owned());

        format!("{effect_name} type={type_name}")
    }

    pub fn tick_handler_nodes(&self) -> Vec<ExpandedNodeIdentifier> {
        borrow!(self.tick_handler_nodes).clone()
    }

    pub fn pre_render_handler_nodes(&self) -> Vec<ExpandedNodeIdentifier> {
        borrow!(self.pre_render_handler_nodes).clone()
    }

    pub fn gyro_handler_nodes(&self) -> Vec<ExpandedNodeIdentifier> {
        borrow!(self.gyro_handler_nodes).clone()
    }

    pub fn accel_handler_nodes(&self) -> Vec<ExpandedNodeIdentifier> {
        borrow!(self.accel_handler_nodes).clone()
    }

    fn register_node_lifecycle_handlers(&self, node: &Rc<ExpandedNode>) {
        let registry = borrow!(node.instance_node)
            .base()
            .handler_registry
            .as_ref()
            .map(Rc::clone);
        let Some(registry) = registry else {
            return;
        };
        let handlers = &borrow!(registry).handlers;
        if handlers.get(TICK_HANDLERS).is_some_and(|h| !h.is_empty()) {
            self.register_lifecycle_handler(&self.tick_handler_nodes, node.id);
        }
        if handlers
            .get(PRE_RENDER_HANDLERS)
            .is_some_and(|h| !h.is_empty())
        {
            self.register_lifecycle_handler(&self.pre_render_handler_nodes, node.id);
        }
        if handlers.get(GYRO_HANDLERS).is_some_and(|h| !h.is_empty()) {
            self.register_lifecycle_handler(&self.gyro_handler_nodes, node.id);
        }
        if handlers.get(ACCEL_HANDLERS).is_some_and(|h| !h.is_empty()) {
            self.register_lifecycle_handler(&self.accel_handler_nodes, node.id);
        }
    }

    fn unregister_node_lifecycle_handlers(&self, id: ExpandedNodeIdentifier) {
        borrow_mut!(self.tick_handler_nodes).retain(|node_id| *node_id != id);
        borrow_mut!(self.pre_render_handler_nodes).retain(|node_id| *node_id != id);
        borrow_mut!(self.gyro_handler_nodes).retain(|node_id| *node_id != id);
        borrow_mut!(self.accel_handler_nodes).retain(|node_id| *node_id != id);
    }

    fn register_lifecycle_handler(
        &self,
        handler_nodes: &RefCell<Vec<ExpandedNodeIdentifier>>,
        id: ExpandedNodeIdentifier,
    ) {
        let mut handler_nodes = borrow_mut!(handler_nodes);
        if !handler_nodes.contains(&id) {
            handler_nodes.push(id);
        }
    }

    /// Look up an expanded node by runtime id.
    pub fn get_expanded_node_by_eid(&self, id: ExpandedNodeIdentifier) -> Option<Rc<ExpandedNode>> {
        borrow!(self.node_cache).eid_to_node.get(&id).cloned()
    }

    /// Route a touch sequence to the node hit at touch-down, even after the finger moves away.
    pub fn capture_touch_target(&self, identifier: i64, target: ExpandedNodeIdentifier) {
        borrow_mut!(self.active_touch_targets).insert(identifier, target);
    }

    /// Resolve the node captured for an active touch sequence.
    pub fn captured_touch_target(&self, identifier: i64) -> Option<Rc<ExpandedNode>> {
        let target = borrow!(self.active_touch_targets)
            .get(&identifier)
            .copied()?;
        self.get_expanded_node_by_eid(target)
    }

    /// Release and resolve the node captured for a completed touch sequence.
    pub fn release_touch_target(&self, identifier: i64) -> Option<Rc<ExpandedNode>> {
        let target = borrow_mut!(self.active_touch_targets).remove(&identifier)?;
        self.get_expanded_node_by_eid(target)
    }

    /// Store a screenshot payload delivered by the chassis.
    pub fn load_screenshot(&self, id: u32, data: ScreenshotData) -> bool {
        borrow_mut!(self.screenshot_map).insert(id, data);
        true
    }

    /// Shared screenshot capture map keyed by request id.
    pub fn get_screenshot_map(&self) -> Rc<RefCell<HashMap<u32, ScreenshotData>>> {
        Rc::clone(&self.screenshot_map)
    }

    /// Remember browser-owned scroller state for native compositing and scroll transforms.
    pub fn set_scroller_surface_state(
        &self,
        id: u32,
        state: ScrollerSurfaceState,
    ) -> ScrollerSurfaceStateChange {
        let change = {
            let mut states = borrow_mut!(self.scroller_surface_states);
            let change = match states.get(&id) {
                Some(previous) if previous.structural_eq(&state) && previous.scroll_eq(&state) => {
                    ScrollerSurfaceStateChange::Unchanged
                }
                Some(previous) if previous.structural_eq(&state) => {
                    ScrollerSurfaceStateChange::ScrollOnly
                }
                _ => ScrollerSurfaceStateChange::Structural,
            };
            if change != ScrollerSurfaceStateChange::Unchanged {
                states.insert(id, state);
            }
            change
        };

        match change {
            ScrollerSurfaceStateChange::Unchanged => {}
            ScrollerSurfaceStateChange::ScrollOnly => {
                self.mark_layer_canvas_plans_dirty();
                self.mark_scroller_content_layers_dirty(id);
            }
            ScrollerSurfaceStateChange::Structural => {
                self.mark_layer_canvas_plans_dirty();
                self.mark_occlusion_dirty();
            }
        }

        change
    }

    /// Update hot scroll offsets for an existing native scroller surface without touching
    /// structural state.
    pub fn update_scroller_surface_scroll(
        &self,
        id: u32,
        scroll_x: f64,
        scroll_y: f64,
        presentation_scroll_x: f64,
        presentation_scroll_y: f64,
    ) -> ScrollerSurfaceStateChange {
        let change = {
            let mut states = borrow_mut!(self.scroller_surface_states);
            let Some(state) = states.get_mut(&id) else {
                return ScrollerSurfaceStateChange::Unchanged;
            };
            let next = ScrollerSurfaceState {
                scroll_x,
                scroll_y,
                presentation_scroll_x,
                presentation_scroll_y,
                ..*state
            };
            let change = if state.scroll_eq(&next) {
                ScrollerSurfaceStateChange::Unchanged
            } else {
                ScrollerSurfaceStateChange::ScrollOnly
            };
            if change != ScrollerSurfaceStateChange::Unchanged {
                *state = next;
            }
            change
        };

        if change == ScrollerSurfaceStateChange::ScrollOnly {
            self.mark_layer_canvas_plans_dirty();
        }

        change
    }

    /// Remove cached scroller surface state.
    pub fn remove_scroller_surface_state(&self, id: u32) {
        borrow_mut!(self.scroller_surface_states).remove(&id);
        self.mark_layer_canvas_plans_dirty();
        self.mark_occlusion_dirty();
    }

    /// Fetch cached scroller surface state by node id.
    pub fn get_scroller_surface_state(&self, id: u32) -> Option<ScrollerSurfaceState> {
        borrow!(self.scroller_surface_states).get(&id).cloned()
    }

    /// Fetch the presentation scroll offset for a native scroller surface, falling back to the
    /// authoritative scroll position when presentation scroll is unavailable.
    pub fn get_scroller_surface_scroll(&self, id: u32) -> Option<(f64, f64)> {
        borrow!(self.scroller_surface_states).get(&id).map(|state| {
            let scroll_x = if state.presentation_scroll_x.is_finite() {
                state.presentation_scroll_x
            } else {
                state.scroll_x
            };
            let scroll_y = if state.presentation_scroll_y.is_finite() {
                state.presentation_scroll_y
            } else {
                state.scroll_y
            };
            (scroll_x, scroll_y)
        })
    }

    fn scroller_content_presentation_transform(&self, node: &ExpandedNode) -> Affine {
        fn clamp_offset(value: f64, content: f64, viewport: f64) -> f64 {
            if content <= viewport || !value.is_finite() {
                return 0.0;
            }
            value.max(0.0).min((content - viewport).max(0.0))
        }

        let root_delegates_to_page_scroll = self.get_root_scroller_id() == Some(node.id.to_u32())
            && self.get_visual_viewport_state().is_some();
        if root_delegates_to_page_scroll {
            return Affine::IDENTITY;
        }

        let instance_node = borrow!(node.instance_node);
        let (scroll_x, scroll_y) = if self.get_root_scroller_id() == Some(node.id.to_u32()) {
            if let Some(visual) = self.get_visual_viewport_state() {
                let visual_x = visual.page_scroll_x + visual.offset_x;
                let visual_y = visual.page_scroll_y + visual.offset_y;
                if visual_x.is_finite() && visual_y.is_finite() {
                    if let Some(state) = self.get_scroller_surface_state(node.id.to_u32()) {
                        let viewport_width = if visual.width.is_finite() {
                            visual.width
                        } else {
                            state.viewport_width
                        };
                        let viewport_height = if visual.height.is_finite() {
                            visual.height
                        } else {
                            state.viewport_height
                        };
                        (
                            clamp_offset(visual_x, state.content_width, viewport_width),
                            clamp_offset(visual_y, state.content_height, viewport_height),
                        )
                    } else {
                        (visual_x, visual_y)
                    }
                } else {
                    self.get_scroller_surface_scroll(node.id.to_u32())
                        .or_else(|| instance_node.resolve_scroll_offset(node))
                        .unwrap_or((0.0, 0.0))
                }
            } else {
                self.get_scroller_surface_scroll(node.id.to_u32())
                    .or_else(|| instance_node.resolve_scroll_offset(node))
                    .unwrap_or((0.0, 0.0))
            }
        } else {
            self.get_scroller_surface_scroll(node.id.to_u32())
                .or_else(|| instance_node.resolve_scroll_offset(node))
                .unwrap_or((0.0, 0.0))
        };

        if scroll_x.abs() <= f64::EPSILON && scroll_y.abs() <= f64::EPSILON {
            return Affine::IDENTITY;
        }

        let world_transform = Affine::from(node.transform_and_bounds.get().transform);
        let inverse_world = Affine::from(node.transform_and_bounds.get().transform.inverse());
        world_transform * Affine::translate((-scroll_x, -scroll_y)) * inverse_world
    }

    /// Resolve the browser/native presentation transform inherited by a node from ancestor
    /// scrollers. Layout transforms remain in content coordinates, while events arrive in the
    /// scrolled window coordinates that the user sees.
    pub(crate) fn presentation_scroll_transform_for_node(&self, node: &ExpandedNode) -> Affine {
        let mut ancestors = Vec::new();
        let mut current = node.render_parent_node();
        while let Some(parent) = current {
            current = parent.render_parent_node();
            ancestors.push(parent);
        }
        ancestors.reverse();

        ancestors
            .into_iter()
            .filter(|ancestor| borrow!(ancestor.instance_node).scrolls_content(ancestor))
            .fold(Affine::IDENTITY, |transform, scroller| {
                transform * self.scroller_content_presentation_transform(&scroller)
            })
    }

    /// Clear render-layer-to-scroller ownership before recomputing occlusion.
    pub fn clear_layer_scroller_owners(&self) {
        borrow_mut!(self.layer_scroller_owners).clear();
        self.mark_layer_canvas_plans_dirty();
    }

    /// Record that a render layer is owned by a particular scroller.
    pub fn register_layer_scroller_owner(
        &self,
        layer_id: usize,
        scroller_id: ExpandedNodeIdentifier,
    ) {
        borrow_mut!(self.layer_scroller_owners).insert(layer_id, scroller_id);
        self.mark_layer_canvas_plans_dirty();
    }

    /// Find the scroller that owns a render layer, when one exists.
    pub fn get_layer_scroller_owner(&self, layer_id: usize) -> Option<ExpandedNodeIdentifier> {
        borrow!(self.layer_scroller_owners).get(&layer_id).copied()
    }

    fn mark_scroller_content_layers_dirty(&self, scroller_id: u32) {
        let layers = borrow!(self.layer_scroller_owners)
            .iter()
            .filter_map(|(layer, owner)| (owner.to_u32() == scroller_id).then_some(*layer))
            .collect::<Vec<_>>();
        for layer in layers {
            self.set_canvas_dirty(layer);
        }
    }

    /// Mark which node currently delegates root scrolling behavior to the page.
    pub fn set_root_scroller_id(&self, id: Option<u32>) {
        self.root_scroller_id.set(id);
        self.mark_layer_canvas_plans_dirty();
        self.mark_occlusion_dirty();
    }

    /// Current page-scroll-backed root scroller id.
    pub fn get_root_scroller_id(&self) -> Option<u32> {
        self.root_scroller_id.get()
    }

    /// Cache the browser visual viewport state for root scroller math.
    pub fn set_visual_viewport_state(&self, state: VisualViewportState) {
        self.visual_viewport_state.set(Some(state));
        self.mark_layer_canvas_plans_dirty();
        self.mark_occlusion_dirty();
    }

    /// Clear cached visual viewport state.
    pub fn clear_visual_viewport_state(&self) {
        self.visual_viewport_state.set(None);
        self.mark_layer_canvas_plans_dirty();
        self.mark_occlusion_dirty();
    }

    /// Return cached browser visual viewport state, if available.
    pub fn get_visual_viewport_state(&self) -> Option<VisualViewportState> {
        self.visual_viewport_state.get()
    }

    /// Ensure the dirty-canvas table has entries up to the requested layer count.
    pub fn resize_canvas_layers_to(&self, id: usize) {
        let mut dirty_canvases = borrow_mut!(self.dirty_canvases);
        let old_len = dirty_canvases.len();
        dirty_canvases.resize(id, false);
        for dirty in dirty_canvases.iter_mut().skip(old_len) {
            *dirty = true;
        }
        if old_len != id {
            self.mark_layer_canvas_plans_dirty();
        }
    }

    pub fn mark_layer_canvas_plans_dirty(&self) {
        let next = self.layer_canvas_plan_generation.get().wrapping_add(1);
        self.layer_canvas_plan_generation.set(next.max(1));
    }

    pub fn layer_canvas_plan_generation(&self) -> u32 {
        self.layer_canvas_plan_generation.get()
    }

    /// Replace the set of render layers that currently contain canvas drawables.
    pub fn set_canvas_drawable_layers(&self, layers: HashSet<usize>) {
        let mut current = borrow_mut!(self.canvas_drawable_layers);
        if *current == layers {
            return;
        }
        *current = layers;
        self.mark_layer_canvas_plans_dirty();
    }

    /// Return whether a render layer currently has canvas work to paint.
    pub fn layer_has_canvas_drawables(&self, layer: usize) -> bool {
        borrow!(self.canvas_drawable_layers).contains(&layer)
    }

    /// Mark every canvas layer clean.
    pub fn clear_all_dirty_canvases(&self) {
        let mut dirty_canvases = borrow_mut!(self.dirty_canvases);
        for v in dirty_canvases.iter_mut() {
            *v = false;
        }
    }

    /// Mark a canvas layer dirty.
    pub fn set_canvas_dirty(&self, id: usize) {
        let mut dirty_canvases = borrow_mut!(self.dirty_canvases);
        if let Some(v) = dirty_canvases.get_mut(id) {
            *v = true;
        }
    }

    /// Check whether a canvas layer needs redraw.
    pub fn is_canvas_dirty(&self, id: &usize) -> bool {
        *borrow!(self.dirty_canvases).get(*id).unwrap_or(&true)
    }

    pub fn dirty_canvas_layers(&self) -> Vec<usize> {
        borrow!(self.dirty_canvases)
            .iter()
            .enumerate()
            .filter_map(|(index, dirty)| dirty.then_some(index))
            .collect()
    }

    pub fn has_dirty_canvas_layers(&self) -> bool {
        borrow!(self.dirty_canvases).iter().any(|dirty| *dirty)
    }

    pub fn has_dirty_canvas_nodes(&self) -> bool {
        !borrow!(self.dirty_canvas_nodes).is_empty()
    }

    pub fn has_canvas_node_removals(&self) -> bool {
        !borrow!(self.removed_canvas_nodes).is_empty()
    }

    pub fn has_canvas_render_work(&self) -> bool {
        self.has_dirty_canvas_layers() || self.has_canvas_node_removals()
    }

    pub fn mark_occlusion_dirty(&self) {
        self.occlusion_dirty.set(true);
    }

    pub fn take_occlusion_dirty(&self) -> bool {
        let dirty = self.occlusion_dirty.get();
        self.occlusion_dirty.set(false);
        dirty
    }

    pub fn set_all_canvases_dirty(&self) {
        let mut dirty_canvases = borrow_mut!(self.dirty_canvases);
        for v in dirty_canvases.iter_mut() {
            *v = true;
        }
    }

    pub fn mark_canvas_node_dirty(&self, id: ExpandedNodeIdentifier) {
        borrow_mut!(self.dirty_canvas_nodes).insert(id);
    }

    pub fn clear_canvas_node_dirty(&self, id: &ExpandedNodeIdentifier) {
        borrow_mut!(self.dirty_canvas_nodes).remove(id);
    }

    pub fn is_canvas_node_dirty(&self, id: &ExpandedNodeIdentifier) -> bool {
        borrow!(self.dirty_canvas_nodes).contains(id)
    }

    pub fn dirty_canvas_node_ids(&self) -> Vec<ExpandedNodeIdentifier> {
        borrow!(self.dirty_canvas_nodes).iter().copied().collect()
    }

    pub fn mark_canvas_nodes_on_layer_dirty(&self, layer: usize) {
        let node_cache = borrow!(self.node_cache);
        let dirty_nodes = &mut *borrow_mut!(self.dirty_canvas_nodes);
        for node in node_cache.eid_to_node.values() {
            if node.occlusion.get().render_layer_id == layer
                && borrow!(node.instance_node).base().flags().layer == crate::api::Layer::Canvas
            {
                dirty_nodes.insert(node.id);
            }
        }
    }

    /// Return the direct-light membership mask resolved for a retained canvas node.
    pub fn canvas_node_light_mask(&self, id: ExpandedNodeIdentifier) -> u32 {
        borrow!(self.canvas_node_light_masks)
            .get(&id)
            .copied()
            .unwrap_or(0)
    }

    pub fn collect_scene_lighting_for_layer(&self, layer: usize) -> SceneLighting {
        let layer_to_root_transforms = self.layer_to_root_transforms(layer);
        let root_to_target = layer_to_root_transforms[&layer].inverse();
        let node_cache = borrow!(self.node_cache);
        let mut scoped_lights = Vec::new();
        let mut topmost_ambient = None;

        for node in node_cache.eid_to_node.values() {
            let occlusion = node.occlusion.get();
            let source_layer = occlusion.render_layer_id;
            let Some(source_to_root) = layer_to_root_transforms.get(&source_layer) else {
                continue;
            };

            let (light, ambient) = {
                let instance_node = borrow!(node.instance_node);
                (
                    instance_node.resolve_scene_light(node, self),
                    instance_node.resolve_scene_ambient_light(node, self),
                )
            };
            if let Some(light) = light {
                if light.enabled {
                    scoped_lights.push((
                        occlusion.z_index,
                        node.id,
                        Self::nearest_light_frame(node),
                        transform_scene_light_for_layer(light, *source_to_root, root_to_target),
                    ));
                }
            }
            if let Some(ambient) = ambient {
                let should_replace = topmost_ambient
                    .as_ref()
                    .map(|(z_index, id, _)| (occlusion.z_index, node.id) >= (*z_index, *id))
                    .unwrap_or(true);
                if should_replace {
                    topmost_ambient = Some((occlusion.z_index, node.id, ambient));
                }
            }
        }

        scoped_lights
            .sort_by(|left, right| right.0.cmp(&left.0).then_with(|| left.1.cmp(&right.1)));
        let overflow = scoped_lights
            .len()
            .saturating_sub(SceneLighting::MAX_LIGHTS);
        scoped_lights.truncate(SceneLighting::MAX_LIGHTS);

        {
            let mut overflow_counts = borrow_mut!(self.lighting_overflow_counts);
            if overflow == 0 {
                overflow_counts.remove(&layer);
            } else if overflow_counts.insert(layer, overflow) != Some(overflow) {
                log::warn!(
                    "canvas layer {layer} has more than {} enabled lights; omitting {overflow}",
                    SceneLighting::MAX_LIGHTS
                );
            }
        }

        let mut resolved_masks = Vec::new();
        for node in node_cache.eid_to_node.values() {
            if node.occlusion.get().render_layer_id != layer
                || borrow!(node.instance_node).base().flags().layer != crate::api::Layer::Canvas
            {
                continue;
            }

            let frame_ancestry = Self::light_frame_ancestry(node);
            let mask = scoped_lights.iter().enumerate().fold(
                0u32,
                |mask, (slot, (_, _, owner_frame, _))| {
                    if light_reaches_frame_ancestry(owner_frame.as_ref(), &frame_ancestry) {
                        mask | (1u32 << slot)
                    } else {
                        mask
                    }
                },
            );
            resolved_masks.push((node.id, mask));
        }
        drop(node_cache);

        let mut changed_nodes = Vec::new();
        {
            let mut masks = borrow_mut!(self.canvas_node_light_masks);
            for (node_id, mask) in resolved_masks {
                if masks.insert(node_id, mask) != Some(mask) {
                    changed_nodes.push(node_id);
                }
            }
        }
        borrow_mut!(self.dirty_canvas_nodes).extend(changed_nodes);

        let lights = scoped_lights
            .into_iter()
            .map(|(_, _, _, light)| light)
            .collect::<Vec<_>>();

        if let Some((_, _, ambient)) = topmost_ambient {
            SceneLighting {
                active: true,
                ambient_is_authored: true,
                ambient,
                lights,
            }
        } else {
            SceneLighting::with_default_ambient(lights)
        }
    }

    fn nearest_light_frame(node: &ExpandedNode) -> Option<ExpandedNodeIdentifier> {
        let mut current = node.render_parent_node();
        while let Some(parent) = current {
            if borrow!(parent.instance_node).establishes_light_frame() {
                return Some(parent.id);
            }
            current = parent.render_parent_node();
        }
        None
    }

    fn light_frame_ancestry(node: &ExpandedNode) -> HashSet<ExpandedNodeIdentifier> {
        let mut frames = HashSet::new();
        let mut current = node.render_parent_node();
        while let Some(parent) = current {
            if borrow!(parent.instance_node).establishes_light_frame() {
                frames.insert(parent.id);
            }
            current = parent.render_parent_node();
        }
        frames
    }

    fn layer_to_root_transforms(&self, layer: usize) -> HashMap<usize, Affine> {
        let mut transforms = HashMap::new();
        transforms.insert(layer, self.layer_to_root_transform(layer));

        let mut current_layer = layer;
        let mut visited = HashSet::new();
        while let Some(owner_id) = self.get_layer_scroller_owner(current_layer) {
            if !visited.insert(current_layer) {
                break;
            }
            let Some(owner_node) = self.get_expanded_node_by_eid(owner_id) else {
                break;
            };
            let owner_layer = owner_node.occlusion.get().render_layer_id;
            transforms
                .entry(owner_layer)
                .or_insert_with(|| self.layer_to_root_transform(owner_layer));
            if owner_layer == current_layer {
                break;
            }
            current_layer = owner_layer;
        }

        transforms
    }

    fn layer_to_root_transform(&self, layer: usize) -> Affine {
        let mut transform = Affine::IDENTITY;
        let mut current_layer = layer;
        let mut visited = HashSet::new();

        while let Some(owner_id) = self.get_layer_scroller_owner(current_layer) {
            if !visited.insert(current_layer) {
                break;
            }
            let Some(owner_node) = self.get_expanded_node_by_eid(owner_id) else {
                break;
            };
            let owner_layer = owner_node.occlusion.get().render_layer_id;
            let owner_transform = self.canvas_surface_transform_for_node(&owner_node);
            let (scroll_x, scroll_y) = self
                .get_scroller_surface_scroll(owner_node.id.to_u32())
                .or_else(|| borrow!(owner_node.instance_node).resolve_scroll_offset(&owner_node))
                .unwrap_or((0.0, 0.0));
            transform = owner_transform * Affine::translate((-scroll_x, -scroll_y)) * transform;

            if owner_layer == current_layer {
                break;
            }
            current_layer = owner_layer;
        }

        transform
    }

    fn canvas_surface_transform_for_node(&self, node: &ExpandedNode) -> Affine {
        let transform = Affine::from(node.transform_and_bounds.get().transform);
        let own_layer = node.occlusion.get().render_layer_id;
        let mut parent_frame_id = node.parent_frame.get();
        while let Some(current_parent_frame_id) = parent_frame_id {
            let Some(parent_frame) = self.get_expanded_node_by_eid(current_parent_frame_id) else {
                break;
            };
            if parent_frame.occlusion.get().render_layer_id != own_layer {
                return Affine::from(parent_frame.transform_and_bounds.get().transform.inverse())
                    * transform;
            }
            parent_frame_id = parent_frame.parent_frame.get();
        }
        transform
    }

    pub fn mark_canvas_nodes_on_layer_dirty_by_id(&self, layer: usize, node_ids: &[u32]) {
        let node_cache = borrow!(self.node_cache);
        let dirty_nodes = &mut *borrow_mut!(self.dirty_canvas_nodes);
        for node_id in node_ids {
            let id = ExpandedNodeIdentifier(*node_id);
            let Some(node) = node_cache.eid_to_node.get(&id) else {
                continue;
            };
            if node.occlusion.get().render_layer_id == layer
                && borrow!(node.instance_node).base().flags().layer == crate::api::Layer::Canvas
            {
                dirty_nodes.insert(id);
            }
        }
    }

    pub fn mark_targeted_canvas_replay_nodes(&self, layer: usize, node_ids: &[u32]) {
        let mut targeted = borrow_mut!(self.targeted_canvas_replay_node_ids);
        let layer_nodes = targeted.entry(layer).or_default();
        layer_nodes.extend(node_ids.iter().copied());
    }

    pub fn take_targeted_canvas_replay_node_ids(&self) -> HashMap<usize, HashSet<u32>> {
        let targeted = &mut *borrow_mut!(self.targeted_canvas_replay_node_ids);
        std::mem::take(targeted)
    }

    pub fn mark_all_canvas_nodes_dirty(&self) {
        let node_cache = borrow!(self.node_cache);
        let dirty_nodes = &mut *borrow_mut!(self.dirty_canvas_nodes);
        for node in node_cache.eid_to_node.values() {
            if borrow!(node.instance_node).base().flags().layer == crate::api::Layer::Canvas {
                dirty_nodes.insert(node.id);
            }
        }
    }

    pub fn enqueue_canvas_node_removal(&self, layer: usize, node_id: u32) {
        borrow_mut!(self.removed_canvas_nodes).push((layer, node_id));
    }

    pub fn take_canvas_node_removals(&self) -> Vec<(usize, u32)> {
        let mut removals = borrow_mut!(self.removed_canvas_nodes);
        std::mem::take(&mut *removals)
    }

    /// Finds all ExpandedNodes with the CommonProperty#id matching the provided string
    pub fn get_expanded_nodes_by_id(&self, id: &str) -> Vec<Rc<ExpandedNode>> {
        //v0 limitation: currently an O(n) lookup cost (could be made O(1) with an id->expandednode cache)
        borrow!(self.node_cache)
            .eid_to_node
            .values()
            .filter(|val| {
                let common_props = val.get_common_properties();
                let common_props = borrow!(common_props);
                common_props.id.get().is_some_and(|i| i == id)
            })
            .cloned()
            .collect()
    }

    /// Finds all ExpandedNodes with corresponding UniqueTemplateNodeIdentifier
    pub fn get_expanded_nodes_by_global_ids(
        &self,
        uni: &UniqueTemplateNodeIdentifier,
    ) -> Vec<Rc<ExpandedNode>> {
        let node_cache = borrow!(self.node_cache);
        node_cache
            .uni_to_eid
            .get(uni)
            .map(|eids| {
                let mut nodes = vec![];
                for e in eids {
                    if let Some(node) = node_cache.eid_to_node.get(e) {
                        nodes.push(Rc::clone(node));
                    } else {
                        log::warn!(
                            "failed to find node in engine for expanded node identifier: {:?}",
                            e
                        );
                    }
                }
                nodes
            })
            .unwrap_or_default()
    }

    /// Simple 2D raycasting: the coordinates of the ray represent a
    /// ray running orthogonally to the view plane, intersecting at
    /// the specified point `ray`.  Areas outside of clipping bounds will
    /// not register a `hit`, nor will elements that suppress input events.
    pub fn get_elements_beneath_ray(
        &self,
        root: Option<Rc<ExpandedNode>>,
        ray: Point2<Window>,
        limit_one: bool,
        mut accum: Vec<Rc<ExpandedNode>>,
        hit_invisible: bool,
    ) -> Vec<Rc<ExpandedNode>> {
        //Traverse all elements in render tree sorted by z-index (highest-to-lowest)
        //First: check whether events are suppressed
        //Next: check whether ancestral clipping bounds (hit_test) are satisfied
        //Finally: check whether element itself satisfies hit_test(ray)

        let Some(root_node) = root.or_else(|| borrow!(self.root_expanded_node).upgrade()) else {
            return accum;
        };
        let mut to_process = vec![(root_node, false, Affine::IDENTITY, false)];
        while let Some((node, clipped, active_scroll_transform, retained_for_exit)) =
            to_process.pop()
        {
            // make sure slot sources are updated for this node
            node.compute_flattened_projected_children();
            if !hit_invisible
                && (retained_for_exit || node.transition_phase.get() == TRANSITION_PHASE_EXIT)
            {
                continue;
            }
            // Browser-composited scrollers move descendants outside the engine transform tree.
            // Fold active scroll offsets into hit-testing so event rays line up with presented content.
            let (scroll_transform, clips_content) = {
                let instance_node = borrow!(node.instance_node);
                let scrolls_content = instance_node.scrolls_content(&node);
                let clips_content = instance_node.clips_content(&node);
                drop(instance_node);
                (
                    if scrolls_content {
                        self.scroller_content_presentation_transform(&node)
                    } else {
                        Affine::IDENTITY
                    },
                    clips_content,
                )
            };
            let descendant_scroll_transform = active_scroll_transform * scroll_transform;
            let hit = node.ray_cast_test(active_scroll_transform.inverse() * ray);
            if hit && !clipped {
                if hit_invisible
                    || !borrow!(node.instance_node)
                        .base()
                        .flags()
                        .invisible_to_raycasting
                {
                    //We only care about the topmost node getting hit, and the element
                    //pool is ordered by z-index so we can just resolve the whole
                    //calculation when we find the first matching node
                    if limit_one {
                        return vec![node];
                    }
                    accum.push(Rc::clone(&node));
                }
            }
            let clipped = clipped || (!hit && clips_content);
            let retained_children = borrow!(node.exiting_children).clone();
            to_process.extend(
                node.children
                    .get()
                    .iter()
                    .cloned()
                    .map(|v| {
                        let retained_for_exit = retained_children
                            .iter()
                            .any(|retained| Rc::ptr_eq(retained, &v));
                        let cp = v.get_common_properties();
                        let unclippable = borrow!(cp).unclippable.get().unwrap_or(false);
                        (
                            v,
                            clipped && !unclippable,
                            descendant_scroll_transform,
                            retained_for_exit,
                        )
                    })
                    .rev(),
            )
        }
        accum
    }
    /// Alias for `get_elements_beneath_ray` with `limit_one = true`
    pub fn get_topmost_element_beneath_ray(
        self: &Rc<Self>,
        ray: Point2<Window>,
    ) -> Option<Rc<ExpandedNode>> {
        let res = self.get_elements_beneath_ray(None, ray, true, vec![], false);
        let Some(new_topmost) = res
            .into_iter()
            .next()
            .or_else(|| borrow!(self.root_expanded_node).upgrade())
        else {
            *borrow_mut!(self.last_topmost_element) = Weak::new();
            return None;
        };

        // Send mouse over/out events if the hit element is different than last.
        // Use template ancestry rather than containing-component ancestry so
        // wrappers around slotted content can own hover affordances.
        let last_topmost = borrow!(self.last_topmost_element).upgrade();
        if Some(new_topmost.id) != last_topmost.as_ref().map(|n| n.id) {
            let (leaving, entering) = find_template_paths_to_common_ancestor(
                &last_topmost,
                &Some(Rc::clone(&new_topmost)),
            );
            for leave in leaving {
                leave.dispatch_mouse_out(Event::new(MouseOut {}), &self.globals(), self);
            }
            for enter in entering {
                enter.dispatch_mouse_over(Event::new(MouseOver {}), &self.globals(), self);
            }
            *borrow_mut!(self.last_topmost_element) = Rc::downgrade(&new_topmost);
        }
        Some(new_topmost)
    }

    pub fn gen_uid(&self) -> ExpandedNodeIdentifier {
        let val = self.next_uid.get();
        let next_val = ExpandedNodeIdentifier(val.0 + 1);
        self.next_uid.set(next_val);
        val
    }

    pub fn enqueue_native_message(&self, message: NativeMessage) {
        borrow_mut!(self.messages).push(message)
    }

    pub fn take_native_messages(&self) -> Vec<NativeMessage> {
        let mut messages = borrow_mut!(self.messages);
        std::mem::take(&mut *messages)
    }

    pub fn globals(&self) -> Globals {
        borrow!(self.globals).clone()
    }

    pub fn edit_globals(&self, f: impl Fn(&mut Globals)) {
        let mut globals = borrow_mut!(self.globals);
        f(&mut globals);
    }

    pub fn queue_custom_event(&self, source_expanded_node: Rc<ExpandedNode>, name: &'static str) {
        let mut queued_custom_events = borrow_mut!(self.queued_custom_events);
        queued_custom_events.push((source_expanded_node, name));
    }

    pub fn flush_custom_events(self: &Rc<Self>) -> Result<(), String> {
        let mut queued_custom_event = borrow_mut!(self.queued_custom_events);
        let to_flush: Vec<_> = std::mem::take(queued_custom_event.as_mut());
        for (target, ident) in to_flush {
            target.dispatch_custom_event(ident, self)?;
        }
        Ok(())
    }

    #[cfg(feature = "designtime")]
    pub fn get_userland_root_expanded_node(&self) -> Option<Rc<ExpandedNode>> {
        borrow!(self.userland_root_expanded_node).clone()
    }

    #[cfg(feature = "designtime")]
    pub fn get_userland_root_instance_node(&self) -> Option<Rc<dyn InstanceNode>> {
        borrow!(self.userland_frame_instance_node).clone()
    }

    #[cfg(feature = "designtime")]
    pub fn set_userland_root_expanded_node(&self, root: Option<Rc<ExpandedNode>>) {
        *borrow_mut!(self.userland_root_expanded_node) = root;
    }

    #[cfg(feature = "designtime")]
    pub fn set_userland_root_instance_node(&self, node: Option<Rc<dyn InstanceNode>>) {
        *borrow_mut!(self.userland_frame_instance_node) = node;
    }

    pub fn get_root_expanded_node(&self) -> Option<Rc<ExpandedNode>> {
        borrow!(self.root_expanded_node).upgrade()
    }

    pub fn queue_render(&self, expanded_node: Rc<ExpandedNode>) {
        borrow_mut!(self.queued_renders).push(expanded_node);
    }

    pub fn recurse_flush_queued_renders(self: &Rc<RuntimeContext>, rcs: &mut dyn RenderContext) {
        while !borrow!(self.queued_renders).is_empty() {
            for n in std::mem::take(&mut *borrow_mut!(self.queued_renders)) {
                n.recurse_render(self, rcs);
            }
        }
    }
}

fn find_template_paths_to_common_ancestor(
    last_topmost: &Option<Rc<ExpandedNode>>,
    new_topmost: &Option<Rc<ExpandedNode>>,
) -> (Vec<Rc<ExpandedNode>>, Vec<Rc<ExpandedNode>>) {
    let mut last_path = template_path_from_root(last_topmost);
    let mut new_path = template_path_from_root(new_topmost);
    let common_prefix_len = last_path
        .iter()
        .zip(new_path.iter())
        .take_while(|(last, new)| last.id == new.id)
        .count();

    // Remove common ancestors from both paths.
    last_path.drain(0..common_prefix_len);
    new_path.drain(0..common_prefix_len);

    (last_path, new_path)
}

fn template_path_from_root(node: &Option<Rc<ExpandedNode>>) -> Vec<Rc<ExpandedNode>> {
    let mut path = Vec::new();
    let mut current = node.clone();
    while let Some(node) = current {
        path.push(Rc::clone(&node));
        current = node.template_parent.upgrade();
    }
    path.reverse();
    path
}

/// Data structure for a single frame of our runtime stack, including
/// a reference to its parent frame and `properties` for
/// runtime evaluation, e.g. of Expressions.  `RuntimePropertiesStackFrame`s also track
/// timeline playhead position.
///
/// `Component`s push `RuntimePropertiesStackFrame`s before computing properties and pop them after computing, thus providing a
/// hierarchical store of node-relevant data that can be bound to symbols in expressions.

pub struct RuntimePropertiesStackFrame {
    symbols_within_frame: HashMap<String, Variable>,
    local_stores: Rc<RefCell<HashMap<TypeId, Box<dyn Any>>>>,
    parent: Option<Rc<RuntimePropertiesStackFrame>>,
}

impl RuntimePropertiesStackFrame {
    pub fn new(symbols_within_frame: HashMap<String, Variable>) -> Rc<Self> {
        Rc::new(Self {
            symbols_within_frame,
            local_stores: Default::default(),
            parent: None,
        })
    }

    pub fn push(self: &Rc<Self>, symbols_within_frame: HashMap<String, Variable>) -> Rc<Self> {
        Rc::new(RuntimePropertiesStackFrame {
            symbols_within_frame,
            local_stores: Default::default(),
            parent: Some(Rc::clone(self)),
        })
    }

    pub fn pop(self: &Rc<Self>) -> Option<Rc<Self>> {
        self.parent.clone()
    }

    pub fn insert_stack_local_store<T: Store>(&self, store: T) {
        let type_id = TypeId::of::<T>();
        borrow_mut!(self.local_stores).insert(type_id, Box::new(store));
    }

    pub fn peek_stack_local_store<T: Store, V>(
        self: &Rc<Self>,
        f: impl FnOnce(&mut T) -> V,
    ) -> Result<V, String> {
        let mut current = Rc::clone(self);
        let type_id = TypeId::of::<T>();

        while !borrow!(current.local_stores).contains_key(&type_id) {
            current = current
                .parent
                .clone()
                .ok_or_else(|| format!("couldn't find store in local stack"))?;
        }
        let v = {
            let mut stores = borrow_mut!(current.local_stores);
            let store = stores.get_mut(&type_id).unwrap().downcast_mut().unwrap();
            f(store)
        };
        Ok(v)
    }

    pub fn resolve_symbol_as_variable(&self, symbol: &str) -> Option<Variable> {
        if let Some(e) = self.symbols_within_frame.get(&clean_symbol(symbol)) {
            Some(e.clone())
        } else {
            self.parent.as_ref()?.resolve_symbol_as_variable(symbol)
        }
    }

    pub fn resolve_symbol_as_erased_property(&self, symbol: &str) -> Option<UntypedProperty> {
        if let Some(e) = self.symbols_within_frame.get(&clean_symbol(symbol)) {
            Some(e.clone().get_untyped_property().clone())
        } else {
            self.parent
                .as_ref()?
                .resolve_symbol_as_erased_property(symbol)
        }
    }

    pub fn resolve_symbol(&self, symbol: &str) -> Option<Variable> {
        self.symbols_within_frame
            .get(&clean_symbol(symbol))
            .cloned()
            .or_else(|| self.parent.as_ref()?.resolve_symbol(symbol))
    }
}

fn clean_symbol(symbol: &str) -> String {
    symbol.replace("self.", "").replace("this.", "")
}

impl IdentifierResolver for RuntimePropertiesStackFrame {
    fn resolve(&self, name: String) -> Result<Variable, String> {
        self.resolve_symbol(&name)
            .ok_or_else(|| format!("Could not resolve symbol {}", name))
    }
}

fn light_reaches_frame_ancestry(
    owner_frame: Option<&ExpandedNodeIdentifier>,
    frame_ancestry: &HashSet<ExpandedNodeIdentifier>,
) -> bool {
    owner_frame.is_none_or(|frame| frame_ancestry.contains(frame))
}

/// Data structure used for dynamic injection of values
/// into Expressions, maintaining a pointer e.g. to the current
/// stack frame to enable evaluation of properties & dependencies
pub struct ExpressionContext {
    pub stack_frame: Rc<RuntimePropertiesStackFrame>,
}

#[cfg(test)]
mod light_scope_tests {
    use super::*;
    use crate::api::math::Transform2;
    use crate::{
        BaseInstance, CommonPropertiesInit, ComponentInstance, InstanceFlags, InstanceNode,
        InstantiationArgs, PropertiesInit, PropertiesScopeInit, RouteLocation, TransformAndBounds,
    };
    use pax_runtime_api::pax_value::{PaxAny, PaxValue};
    use pax_runtime_api::{Layer, Platform, SceneAmbientLight, TargetInfo, OS};
    use std::fmt;

    #[derive(Clone, Copy)]
    enum TestLightingRole {
        Frame,
        Light(f64),
        Ambient(f64),
        Surface,
    }

    struct TestLightingNode {
        base: BaseInstance,
        role: TestLightingRole,
        enabled: Cell<bool>,
    }

    impl TestLightingNode {
        fn new(
            role: TestLightingRole,
            children: Vec<Rc<dyn InstanceNode>>,
        ) -> Rc<TestLightingNode> {
            Rc::new(Self {
                base: BaseInstance::new(
                    node_args(children),
                    InstanceFlags {
                        invisible_to_slot: false,
                        invisible_to_raycasting: true,
                        layer: match role {
                            TestLightingRole::Frame => Layer::DontCare,
                            TestLightingRole::Light(_)
                            | TestLightingRole::Ambient(_)
                            | TestLightingRole::Surface => Layer::Canvas,
                        },
                        is_component: false,
                        is_slot: false,
                    },
                ),
                role,
                enabled: Cell::new(true),
            })
        }

        fn as_instance(self: &Rc<Self>) -> Rc<dyn InstanceNode> {
            self.clone()
        }

        fn set_enabled(&self, enabled: bool) {
            self.enabled.set(enabled);
        }
    }

    impl InstanceNode for TestLightingNode {
        fn instantiate(args: InstantiationArgs) -> Rc<Self>
        where
            Self: Sized,
        {
            Rc::new(Self {
                base: BaseInstance::new(
                    args,
                    InstanceFlags {
                        invisible_to_slot: false,
                        invisible_to_raycasting: true,
                        layer: Layer::Canvas,
                        is_component: false,
                        is_slot: false,
                    },
                ),
                role: TestLightingRole::Surface,
                enabled: Cell::new(true),
            })
        }

        fn resolve_debug(
            &self,
            f: &mut fmt::Formatter,
            _expanded_node: Option<&ExpandedNode>,
        ) -> fmt::Result {
            f.debug_struct("TestLightingNode").finish()
        }

        fn resolve_scene_light(
            &self,
            _expanded_node: &ExpandedNode,
            _context: &RuntimeContext,
        ) -> Option<SceneLight> {
            match self.role {
                TestLightingRole::Light(intensity) if self.enabled.get() => Some(SceneLight {
                    intensity,
                    ..Default::default()
                }),
                _ => None,
            }
        }

        fn resolve_scene_ambient_light(
            &self,
            _expanded_node: &ExpandedNode,
            _context: &RuntimeContext,
        ) -> Option<SceneAmbientLight> {
            match self.role {
                TestLightingRole::Ambient(intensity) if self.enabled.get() => {
                    Some(SceneAmbientLight {
                        intensity,
                        ..Default::default()
                    })
                }
                _ => None,
            }
        }

        fn establishes_light_frame(&self) -> bool {
            matches!(self.role, TestLightingRole::Frame)
        }

        fn base(&self) -> &BaseInstance {
            &self.base
        }
    }

    struct ScopedLightingFixture {
        context: Rc<RuntimeContext>,
        _root: Rc<ExpandedNode>,
        root_light: Rc<TestLightingNode>,
        outer_light: Rc<TestLightingNode>,
        ambient: Rc<TestLightingNode>,
        root_surface: Rc<ExpandedNode>,
        outer_surface: Rc<ExpandedNode>,
        nested_surface: Rc<ExpandedNode>,
        sibling_surface: Rc<ExpandedNode>,
        sibling_frame: Rc<ExpandedNode>,
    }

    fn test_globals() -> Globals {
        Globals {
            elapsed_frames: Property::new(0),
            elapsed_millis: Property::new(0),
            viewport: Property::new(TransformAndBounds {
                transform: Transform2::identity(),
                bounds: (100.0, 100.0),
            }),
            gyro: Property::new(Default::default()),
            accel: Property::new(Default::default()),
            route_location: Property::new(RouteLocation::root()),
            browser_allows_scroller_vector_layers: Property::new(true),
            browser_allows_nested_scroller_vector_layers: Property::new(true),
            platform: Platform::Unknown,
            os: OS::Unknown,
            target: TargetInfo::new(Platform::Unknown, OS::Unknown),
            get_elapsed_millis: Rc::new(|| 0),
        }
    }

    fn properties_factory() -> crate::PropertiesFactory {
        Box::new(|_, expanded_node| {
            expanded_node
                .is_none()
                .then(|| Rc::new(RefCell::new(PaxAny::Builtin(PaxValue::default()))))
        })
    }

    fn node_args(children: Vec<Rc<dyn InstanceNode>>) -> InstantiationArgs {
        InstantiationArgs {
            prototypical_common_properties: CommonPropertiesInit::Default,
            prototypical_properties: PropertiesInit::Factory(properties_factory()),
            handler_registry: None,
            children: Some(RefCell::new(children)),
            component_template: None,
            component_settings: None,
            template_node_identifier: None,
            template_node_type_id: None,
            template_node_selector_info: None,
            transition_config: Default::default(),
            properties_scope: PropertiesScopeInit::None,
        }
    }

    fn component_args(template: Vec<Rc<dyn InstanceNode>>) -> InstantiationArgs {
        InstantiationArgs {
            prototypical_common_properties: CommonPropertiesInit::Default,
            prototypical_properties: PropertiesInit::Factory(properties_factory()),
            handler_registry: None,
            children: None,
            component_template: Some(RefCell::new(template)),
            component_settings: None,
            template_node_identifier: None,
            template_node_type_id: None,
            template_node_selector_info: None,
            transition_config: Default::default(),
            properties_scope: PropertiesScopeInit::None,
        }
    }

    fn mount_test_tree(
        children: Vec<Rc<dyn InstanceNode>>,
    ) -> (Rc<RuntimeContext>, Rc<ExpandedNode>) {
        let root_component = ComponentInstance::instantiate(component_args(children));
        let context = Rc::new(RuntimeContext::new(test_globals()));
        let root = ExpandedNode::initialize_root(root_component, &context);
        root.recurse_update(&context);
        crate::engine::occlusion::update_node_occlusion(&root, &context);
        (context, root)
    }

    fn clear_dirty_canvas_nodes(context: &RuntimeContext) {
        for id in context.dirty_canvas_node_ids() {
            context.clear_canvas_node_dirty(&id);
        }
    }

    fn scoped_lighting_fixture() -> ScopedLightingFixture {
        let nested_light = TestLightingNode::new(TestLightingRole::Light(3.0), Vec::new());
        let nested_surface = TestLightingNode::new(TestLightingRole::Surface, Vec::new());
        let nested_frame = TestLightingNode::new(
            TestLightingRole::Frame,
            vec![nested_light.as_instance(), nested_surface.as_instance()],
        );

        let sibling_surface = TestLightingNode::new(TestLightingRole::Surface, Vec::new());
        let sibling_frame =
            TestLightingNode::new(TestLightingRole::Frame, vec![sibling_surface.as_instance()]);

        let outer_light = TestLightingNode::new(TestLightingRole::Light(2.0), Vec::new());
        let outer_surface = TestLightingNode::new(TestLightingRole::Surface, Vec::new());
        let outer_frame = TestLightingNode::new(
            TestLightingRole::Frame,
            vec![
                outer_light.as_instance(),
                outer_surface.as_instance(),
                nested_frame.as_instance(),
                sibling_frame.as_instance(),
            ],
        );

        let root_light = TestLightingNode::new(TestLightingRole::Light(1.0), Vec::new());
        let root_surface = TestLightingNode::new(TestLightingRole::Surface, Vec::new());
        let ambient = TestLightingNode::new(TestLightingRole::Ambient(0.72), Vec::new());
        ambient.set_enabled(false);

        let (context, root) = mount_test_tree(vec![
            root_light.as_instance(),
            root_surface.as_instance(),
            outer_frame.as_instance(),
            ambient.as_instance(),
        ]);

        let root_children = root.children.get();
        let root_surface_node = root_children[1].clone();
        let outer_frame_node = root_children[2].clone();
        let outer_children = outer_frame_node.children.get();
        let outer_surface_node = outer_children[1].clone();
        let nested_frame_node = outer_children[2].clone();
        let sibling_frame_node = outer_children[3].clone();
        let nested_children = nested_frame_node.children.get();
        let nested_surface_node = nested_children[1].clone();
        let sibling_surface_node = sibling_frame_node.children.get()[0].clone();

        ScopedLightingFixture {
            context,
            _root: root,
            root_light,
            outer_light,
            ambient,
            root_surface: root_surface_node,
            outer_surface: outer_surface_node,
            nested_surface: nested_surface_node,
            sibling_surface: sibling_surface_node,
            sibling_frame: sibling_frame_node,
        }
    }

    #[test]
    fn root_lights_reach_every_frame_ancestry() {
        let ancestry = HashSet::from([ExpandedNodeIdentifier(10), ExpandedNodeIdentifier(20)]);
        assert!(light_reaches_frame_ancestry(None, &ancestry));
    }

    #[test]
    fn framed_lights_reach_only_descendants_of_their_owner_frame() {
        let outer_frame = ExpandedNodeIdentifier(10);
        let nested_frame = ExpandedNodeIdentifier(20);
        let sibling_frame = ExpandedNodeIdentifier(30);
        let nested_ancestry = HashSet::from([outer_frame, nested_frame]);

        assert!(light_reaches_frame_ancestry(
            Some(&outer_frame),
            &nested_ancestry
        ));
        assert!(light_reaches_frame_ancestry(
            Some(&nested_frame),
            &nested_ancestry
        ));
        assert!(!light_reaches_frame_ancestry(
            Some(&sibling_frame),
            &nested_ancestry
        ));
    }

    #[test]
    fn mounted_tree_resolves_root_nested_and_sibling_light_masks() {
        let fixture = scoped_lighting_fixture();

        let lighting = fixture.context.collect_scene_lighting_for_layer(0);

        assert_eq!(
            lighting
                .lights
                .iter()
                .map(|light| light.intensity)
                .collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0]
        );
        assert!(!lighting.ambient_is_authored);
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.root_surface.id),
            0b001
        );
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.outer_surface.id),
            0b011
        );
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.nested_surface.id),
            0b111
        );
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.sibling_surface.id),
            0b011
        );
    }

    #[test]
    fn slot_reallocation_and_render_reparenting_refresh_retained_masks() {
        let fixture = scoped_lighting_fixture();
        fixture.context.collect_scene_lighting_for_layer(0);
        clear_dirty_canvas_nodes(&fixture.context);

        fixture.outer_light.set_enabled(false);
        let lighting = fixture.context.collect_scene_lighting_for_layer(0);

        assert_eq!(
            lighting
                .lights
                .iter()
                .map(|light| light.intensity)
                .collect::<Vec<_>>(),
            vec![1.0, 3.0]
        );
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.root_surface.id),
            0b01
        );
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.outer_surface.id),
            0b01
        );
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.nested_surface.id),
            0b11
        );
        assert!(fixture
            .context
            .is_canvas_node_dirty(&fixture.outer_surface.id));
        assert!(fixture
            .context
            .is_canvas_node_dirty(&fixture.nested_surface.id));
        assert!(!fixture
            .context
            .is_canvas_node_dirty(&fixture.root_surface.id));

        clear_dirty_canvas_nodes(&fixture.context);
        *borrow_mut!(fixture.nested_surface.render_parent) = Rc::downgrade(&fixture.sibling_frame);
        fixture.context.collect_scene_lighting_for_layer(0);

        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.nested_surface.id),
            0b01
        );
        assert!(fixture
            .context
            .is_canvas_node_dirty(&fixture.nested_surface.id));
    }

    #[test]
    fn default_and_authored_ambient_keep_empty_direct_masks_distinct() {
        let fixture = scoped_lighting_fixture();
        fixture.root_light.set_enabled(false);

        let default_ambient = fixture.context.collect_scene_lighting_for_layer(0);
        assert!(default_ambient.active);
        assert!(!default_ambient.ambient_is_authored);
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.root_surface.id),
            0
        );
        assert_eq!(
            default_ambient.ambient.intensity,
            SceneLighting::DEFAULT_AMBIENT_INTENSITY
        );

        fixture.ambient.set_enabled(true);
        let authored_ambient = fixture.context.collect_scene_lighting_for_layer(0);
        assert!(authored_ambient.active);
        assert!(authored_ambient.ambient_is_authored);
        assert_eq!(authored_ambient.ambient.intensity, 0.72);
        assert_eq!(
            fixture
                .context
                .canvas_node_light_mask(fixture.root_surface.id),
            0
        );
    }

    #[test]
    fn light_overflow_selection_is_deterministic_and_recovers() {
        let lights = (0..10)
            .map(|index| {
                TestLightingNode::new(TestLightingRole::Light((index + 1) as f64), Vec::new())
            })
            .collect::<Vec<_>>();
        let surface = TestLightingNode::new(TestLightingRole::Surface, Vec::new());
        let mut templates = lights
            .iter()
            .rev()
            .map(TestLightingNode::as_instance)
            .collect::<Vec<_>>();
        templates.push(surface.as_instance());
        let (context, root) = mount_test_tree(templates);
        let expanded = root.children.get();
        let surface_node = expanded[10].clone();

        let first = context.collect_scene_lighting_for_layer(0);
        let second = context.collect_scene_lighting_for_layer(0);
        let expected = vec![10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0];
        assert_eq!(
            first
                .lights
                .iter()
                .map(|light| light.intensity)
                .collect::<Vec<_>>(),
            expected
        );
        assert_eq!(first.lights, second.lights);
        assert_eq!(context.canvas_node_light_mask(surface_node.id), 0xff);
        assert_eq!(borrow!(context.lighting_overflow_counts).get(&0), Some(&2));

        lights[0].set_enabled(false);
        lights[1].set_enabled(false);
        let recovered = context.collect_scene_lighting_for_layer(0);
        assert_eq!(
            recovered
                .lights
                .iter()
                .map(|light| light.intensity)
                .collect::<Vec<_>>(),
            expected
        );
        assert!(!borrow!(context.lighting_overflow_counts).contains_key(&0));
    }

    #[test]
    fn touch_capture_releases_and_clears_removed_targets() {
        let surface = TestLightingNode::new(TestLightingRole::Surface, Vec::new());
        let (context, root) = mount_test_tree(vec![surface.as_instance()]);
        let surface_node = root.children.get()[0].clone();

        context.capture_touch_target(41, surface_node.id);
        assert!(Rc::ptr_eq(
            &context.captured_touch_target(41).unwrap(),
            &surface_node
        ));
        assert!(Rc::ptr_eq(
            &context.release_touch_target(41).unwrap(),
            &surface_node
        ));
        assert!(context.captured_touch_target(41).is_none());

        context.capture_touch_target(42, surface_node.id);
        context.remove_from_cache(&surface_node);
        assert!(context.captured_touch_target(42).is_none());
        assert!(context.release_touch_target(42).is_none());
    }
}