incular-runtime 0.1.0

Application runtime and scheduling foundations for Incular
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
use crate::application_activations::{ActivationRouteBridge, ApplicationActivationService};
use crate::application_shell::{
    ApplicationShellService, NativeApplicationShellCompletion, NativeApplicationShellEvent,
    NativeApplicationShellRequest,
};
use crate::application_types::{
    ApplicationSchedulerCounters, LastWindowPolicy, RestorableWindowFactory,
    RestorableWindowMetadata, WindowError, WindowRestorationId,
};
use crate::context::BuildContext;
use crate::file_dialogs::{
    FileDialogBridge, FileDialogCompletionStatus, NativeFileDialogCompletion,
    NativeFileDialogRequest, QueuedFileDialogRequest, explicitly_unsupported_request,
    result_matches_request,
};
use crate::frame::{FrameStats, Runtime};
use crate::global_shortcuts::{
    GlobalShortcutBridge, GlobalShortcutCompletionStatus, GlobalShortcutService,
    NativeGlobalShortcutCompletion, NativeGlobalShortcutOperation, NativeGlobalShortcutRequest,
    PendingGlobalShortcutRequest, QueuedGlobalShortcutRequest, drain_queued,
};
use crate::profiling::{
    AccessibilitySnapshot, BudgetStatistics, FrameHistory, FrameRecord, FrameWork, GpuSample,
    PerformanceHub, PerformanceProfiler, PerformanceSnapshot, ProfilerMode, RenderFrameMetrics,
    TextCacheSnapshot, WidgetWorkSnapshot, WindowPerformance,
};
use crate::request_registry::{CompletionRejection, RequestPhase, RequestRegistry};
use crate::restoration::{self, RestorationConfig, RestorationDiagnostics};
use crate::scheduler_counters;
use crate::simulation::{self, Simulation};
use crate::tasks::{self, RuntimeWake, Task, TaskFailure, TaskHandle, TokioHandle};
use crate::transient_presentation::{
    ResolvedTransientPresentation, TransientPresentationResolution,
};
use crate::window_commands::{
    DisplayCatalog, NativeOperationCompletionStatus, NativeWindowCommand, QueuedNativeRequest,
    QueuedWindowCommand, WindowCommandBridge, WindowHandle,
};
use crate::window_state::{
    ApplicationDiagnostics, ContentSizeCoordinator, WindowDiagnostics, WindowManager, WindowRecord,
    WindowRegistry,
};
use incular_accessibility::{
    AccessKitProjection, AccessibilityDiagnostics, NativeAccessibilityUpdate, SemanticActionRequest,
};
use incular_config::{Constraints, RuntimeEnvironment};
use incular_core::RestorationKey;
use incular_platform::{
    ApplicationActivation, Clipboard, DisplayId, DisplaySnapshot, ExternalDragEvent,
    ExternalDragResponse, FileDialogError, FileDialogOutcome, FileDialogRequest,
    FileDialogRequestId, LaunchActivation, NativeOperationCompletion, NativeRequestId,
    PlatformCapabilities, PlatformEvent, PlatformLifecycle, PlatformOperationError,
    PlatformOperationErrorKind, PlatformOperationResult, SingleInstancePolicy, TextInputCommand,
    WindowCommand, WindowEvent, WindowEventKind, WindowId, WindowLifecycle, WindowOperation,
    WindowOptions,
};
use incular_rendering::DisplayList;
use incular_widgets::internal::{Key, MouseCursor, PlatformMenuBinding, TreeError};
use incular_widgets::{RouteInformation, RouteInformationProvider, Widget};
use std::{
    cell::RefCell,
    collections::{HashMap, HashSet, VecDeque},
    rc::Rc,
    sync::{Arc, RwLock, atomic::Ordering, mpsc},
    time::Instant,
};
use tokio::sync::oneshot;

struct PendingNativeRequest {
    window_id: WindowId,
    sender: oneshot::Sender<PlatformOperationResult>,
}

struct PendingFileDialogRequest {
    window_id: WindowId,
    request: FileDialogRequest,
    sender: Option<oneshot::Sender<Result<FileDialogOutcome, FileDialogError>>>,
    cancelled: Arc<std::sync::atomic::AtomicBool>,
}

/// Owns application services, one Tokio runtime, and multiple retained roots.
/// A root's environment, input/focus, semantics, compositor, frame work, and
/// cancellation scope are window-local; assets, Signals, and Tokio are shared.
pub struct Application {
    pub(crate) scheduler: Rc<RefCell<tasks::TaskScheduler>>,
    pub(crate) registry: Rc<RefCell<WindowRegistry>>,
    pub(crate) manager: WindowManager,
    pub(crate) command_receiver: mpsc::Receiver<QueuedWindowCommand>,
    pub(crate) request_cancellation_receiver: mpsc::Receiver<NativeRequestId>,
    pending_native_requests: RequestRegistry<NativeRequestId, PendingNativeRequest>,
    file_dialog_receiver: mpsc::Receiver<QueuedFileDialogRequest>,
    file_dialog_cancellation_receiver: mpsc::Receiver<FileDialogRequestId>,
    file_dialog_bridge: Arc<FileDialogBridge>,
    pending_file_dialog_requests: RequestRegistry<FileDialogRequestId, PendingFileDialogRequest>,
    active_file_dialog_requests: HashMap<WindowId, FileDialogRequestId>,
    queued_file_dialog_requests: HashMap<WindowId, VecDeque<FileDialogRequestId>>,
    native_file_dialog_requests: VecDeque<NativeFileDialogRequest>,
    global_shortcut_receiver: mpsc::Receiver<QueuedGlobalShortcutRequest>,
    global_shortcut_bridge: Arc<GlobalShortcutBridge>,
    pending_global_shortcut_requests: RequestRegistry<
        crate::global_shortcuts::GlobalShortcutRequestId,
        PendingGlobalShortcutRequest,
    >,
    native_global_shortcut_requests: VecDeque<NativeGlobalShortcutRequest>,
    platform_capabilities: Arc<RwLock<PlatformCapabilities>>,
    application_shell: ApplicationShellService,
    activations: ApplicationActivationService,
    launch_activation: LaunchActivation,
    single_instance_policy: Option<SingleInstancePolicy>,
    display_catalog: Arc<RwLock<DisplayCatalog>>,
    pub(crate) simulation_receiver: mpsc::Receiver<simulation::SimulationRequest>,
    pub(crate) simulation_bridge: Arc<simulation::SimulationBridge>,
    pub(crate) simulation_waiters: simulation::SimulationWaiters,
    pub(crate) native_commands: Rc<RefCell<VecDeque<NativeWindowCommand>>>,
    pub(crate) primary_window: WindowId,
    pub(crate) last_window_policy: LastWindowPolicy,
    pub(crate) should_exit: bool,
    pub(crate) close_request: Option<Box<dyn FnMut(WindowId) -> bool>>,
    pub(crate) restoration: Option<restoration::RestorationManager>,
    pub(crate) restoration_window_factories:
        HashMap<String, (WindowOptions, RestorableWindowFactory)>,
    pub(crate) profiler: PerformanceProfiler,
    pub(crate) hub: PerformanceHub,
    pub(crate) scheduler_counters: ApplicationSchedulerCounters,
}
impl Drop for Application {
    fn drop(&mut self) {
        self.application_shell.stop();
    }
}
impl Application {
    /// Creates an application from a retained root builder.
    ///
    /// The builder runs during construction and may run again whenever state
    /// or environment values read by the builder change. Keep the builder
    /// free of external side effects such as network requests, file writes,
    /// or analytics; put those operations in event callbacks or tasks.
    pub fn new(
        build: impl FnMut(&mut BuildContext) -> Widget + 'static,
    ) -> Result<Self, TreeError> {
        Self::new_with_options(WindowOptions::default(), build).map_err(|error| match error {
            WindowError::Tree(error) => error,
            WindowError::Options(_)
            | WindowError::Restoration(_)
            | WindowError::ApplicationStopped => {
                unreachable!("default application options are valid while constructing")
            }
        })
    }

    /// Builds the initial retained root with portable native-window options.
    pub fn new_with_options(
        options: WindowOptions,
        build: impl FnMut(&mut BuildContext) -> Widget + 'static,
    ) -> Result<Self, WindowError> {
        Self::new_inner(options, None, None, build)
    }

    /// Creates an application whose explicitly selected values can survive a
    /// restart. Restoration loads synchronously before the initial root build;
    /// a corrupt, incompatible, or unmigratable snapshot is reported through
    /// diagnostics and simply exposes application defaults instead.
    pub fn new_with_restoration(
        options: WindowOptions,
        restoration: RestorationConfig,
        build: impl FnMut(&mut BuildContext) -> Widget + 'static,
    ) -> Result<Self, WindowError> {
        Self::new_inner(options, Some(restoration), None, build)
    }

    /// Creates a restoration-enabled application whose primary native window
    /// also has a stable application identity. Use this instead of
    /// [`Self::new_with_restoration`] when its logical size/fullscreen state
    /// should be reconstructed across launches.
    pub fn new_restorable(
        restoration_id: WindowRestorationId,
        kind: impl Into<String>,
        options: WindowOptions,
        restoration: RestorationConfig,
        build: impl FnMut(&mut BuildContext) -> Widget + 'static,
    ) -> Result<Self, WindowError> {
        Self::new_inner(
            options,
            Some(restoration),
            Some(RestorableWindowMetadata::new(restoration_id, kind)?),
            build,
        )
    }

    fn new_inner(
        options: WindowOptions,
        restoration_config: Option<RestorationConfig>,
        primary_restoration: Option<RestorableWindowMetadata>,
        build: impl FnMut(&mut BuildContext) -> Widget + 'static,
    ) -> Result<Self, WindowError> {
        Self::with_primary(tasks::TaskScheduler::new(), restoration_config, |manager| {
            manager
                .open_window_with_inner(options, build, primary_restoration)
                .map(|handle| handle.id())
        })
    }

    /// Adopts an existing standalone runtime without remounting its tree or
    /// replacing its task scheduler. The action callback observes legacy action
    /// IDs on the UI thread; native event handling uses the application host.
    /// Existing reactive builders, focus and queued task completions are retained.
    pub fn from_runtime(
        mut runtime: Runtime,
        on_action: impl FnMut(incular_widgets::internal::ActionId) + 'static,
    ) -> Self {
        runtime.legacy_action_handler = Some(Box::new(on_action));
        Self::with_primary(runtime.scheduler.clone(), None, |manager| {
            Ok(manager.adopt_runtime(runtime))
        })
        .expect("adopting a mounted runtime with default window options is infallible")
    }

    fn with_primary(
        scheduler: Rc<RefCell<tasks::TaskScheduler>>,
        restoration_config: Option<RestorationConfig>,
        create: impl FnOnce(&WindowManager) -> Result<WindowId, WindowError>,
    ) -> Result<Self, WindowError> {
        let restoration = restoration_config.map(restoration::RestorationManager::load);
        if let Some(restoration) = &restoration {
            restoration.attach_scheduler(&scheduler);
        }
        let registry = Rc::new(RefCell::new(WindowRegistry::default()));
        let native_commands = Rc::new(RefCell::new(VecDeque::new()));
        let (sender, command_receiver) = mpsc::channel();
        let (cancellation_sender, request_cancellation_receiver) = mpsc::channel();
        let (file_dialog_sender, file_dialog_receiver) = mpsc::channel();
        let (file_dialog_cancellation_sender, file_dialog_cancellation_receiver) = mpsc::channel();
        let file_dialog_bridge = Arc::new(FileDialogBridge::new(
            file_dialog_sender,
            file_dialog_cancellation_sender,
        ));
        let (global_shortcut_sender, global_shortcut_receiver) = mpsc::channel();
        let global_shortcut_bridge = Arc::new(GlobalShortcutBridge::new(global_shortcut_sender));
        let (simulation_sender, simulation_receiver) = mpsc::channel();
        let simulation_bridge = Arc::new(simulation::SimulationBridge::new(simulation_sender));
        let platform_capabilities = Arc::new(RwLock::new(PlatformCapabilities::default()));
        let application_shell = ApplicationShellService::new(platform_capabilities.clone());
        let activations = ApplicationActivationService::default();
        let display_catalog = Arc::new(RwLock::new(DisplayCatalog::default()));
        let application_lifecycle = Rc::new(std::cell::Cell::new(
            crate::application_types::ApplicationLifecycle::Starting,
        ));
        let bridge = Arc::new(WindowCommandBridge::new(sender, cancellation_sender));
        let manager = WindowManager {
            registry: Rc::downgrade(&registry),
            scheduler: scheduler.clone(),
            bridge,
            file_dialog_bridge: file_dialog_bridge.clone(),
            global_shortcut_bridge: global_shortcut_bridge.clone(),
            application_shell: application_shell.clone(),
            native_commands: native_commands.clone(),
            restoration: restoration.clone(),
            application_capabilities: platform_capabilities.clone(),
            displays: display_catalog.clone(),
            application_lifecycle,
        };
        let primary_window = create(&manager)?;
        Ok(Self {
            scheduler,
            registry,
            manager,
            command_receiver,
            request_cancellation_receiver,
            pending_native_requests: RequestRegistry::new(),
            file_dialog_receiver,
            file_dialog_cancellation_receiver,
            file_dialog_bridge,
            pending_file_dialog_requests: RequestRegistry::new(),
            active_file_dialog_requests: HashMap::new(),
            queued_file_dialog_requests: HashMap::new(),
            native_file_dialog_requests: VecDeque::new(),
            global_shortcut_receiver,
            global_shortcut_bridge,
            pending_global_shortcut_requests: RequestRegistry::new(),
            native_global_shortcut_requests: VecDeque::new(),
            platform_capabilities,
            application_shell,
            activations,
            launch_activation: LaunchActivation::current_process(),
            single_instance_policy: None,
            display_catalog,
            simulation_receiver,
            simulation_bridge,
            simulation_waiters: simulation::SimulationWaiters::default(),
            native_commands,
            primary_window,
            last_window_policy: LastWindowPolicy::ExitOnLastWindow,
            should_exit: false,
            close_request: None,
            restoration,
            restoration_window_factories: HashMap::new(),
            profiler: PerformanceProfiler::new(ProfilerMode::Normal),
            hub: PerformanceHub::new(),
            scheduler_counters: ApplicationSchedulerCounters::default(),
        })
    }

    /// Adds an independent retained root. Native creation is deferred until a
    /// desktop adapter reaches an active Winit event-loop callback.
    pub fn open_window(
        &mut self,
        options: WindowOptions,
        root: Widget,
    ) -> Result<WindowHandle, WindowError> {
        self.manager.open_window(options, root)
    }

    /// Adds an independent declarative root with its own window environment
    /// and cancellation scope.
    pub fn open_window_with(
        &mut self,
        options: WindowOptions,
        build: impl FnMut(&mut BuildContext) -> Widget + 'static,
    ) -> Result<WindowHandle, WindowError> {
        self.manager.open_window_with(options, build)
    }

    /// Opens an auxiliary window with a stable restoration ID. A normal user
    /// close removes this descriptor and its window-scoped values from the
    /// next session; application shutdown preserves currently active
    /// restorable windows.
    pub fn open_restorable_window_with(
        &mut self,
        restoration_id: WindowRestorationId,
        kind: impl Into<String>,
        options: WindowOptions,
        build: impl FnMut(&mut BuildContext) -> Widget + 'static,
    ) -> Result<WindowHandle, WindowError> {
        self.manager
            .open_restorable_window_with(restoration_id, kind, options, build)
    }

    /// Registers an in-memory factory for one stable auxiliary window kind.
    /// Builders remain ordinary closures and are never stored in a snapshot.
    pub fn register_restorable_window_factory(
        &mut self,
        kind: impl Into<String>,
        options: WindowOptions,
        factory: impl Fn(&mut BuildContext) -> Widget + 'static,
    ) -> Result<(), WindowError> {
        let kind = kind.into();
        if kind.trim().is_empty() || kind.chars().any(char::is_control) {
            return Err(WindowError::Restoration(
                "window factory kind must be a non-empty printable stable identifier".to_owned(),
            ));
        }
        let factory: RestorableWindowFactory = Rc::new(move |context: &mut BuildContext| {
            incular_core::hot_reload::call_with(&mut &factory, context)
        });
        self.restoration_window_factories
            .insert(kind, (options, factory));
        Ok(())
    }

    /// Restores all persisted auxiliary windows whose stable factories have
    /// been registered. Call this after registration and before entering the
    /// native runner; it therefore cannot flash a default auxiliary UI before
    /// restore. Unknown kinds, unparseable ids, and duplicate descriptors are
    /// skipped safely and counted in diagnostics; only a genuine window-open
    /// failure aborts the remaining restores.
    pub fn restore_restorable_windows(&mut self) -> Result<usize, WindowError> {
        let Some(restoration) = self.restoration.clone() else {
            return Ok(0);
        };
        let mut active = self
            .registry
            .borrow()
            .slots
            .iter()
            .filter_map(|slot| slot.record.as_ref())
            .filter_map(|record| record.restoration.as_ref())
            .map(|metadata| metadata.id.as_key().as_str().to_owned())
            .collect::<HashSet<_>>();
        let descriptors = restoration.windows();
        let mut restored = 0;
        for descriptor in descriptors {
            if !active.insert(descriptor.restoration_id.clone()) {
                continue;
            }
            let restoration_id = match WindowRestorationId::new(descriptor.restoration_id) {
                Ok(id) => id,
                Err(_) => {
                    restoration.note_skipped_window();
                    continue;
                }
            };
            let Some((options, factory)) = self
                .restoration_window_factories
                .get(&descriptor.kind)
                .cloned()
            else {
                restoration.note_skipped_window();
                continue;
            };
            let kind = descriptor.kind;
            let factory = factory.clone();
            self.manager
                .open_restorable_window_with(restoration_id, kind, options, move |cx| {
                    factory(cx)
                })?;
            restored += 1;
        }
        Ok(restored)
    }

    #[must_use]
    pub const fn primary_window(&self) -> WindowId {
        self.primary_window
    }

    #[must_use]
    pub fn active_window_ids(&self) -> Vec<WindowId> {
        self.registry.borrow().ids()
    }

    /// Returns a thread-safe command handle for one live Incular window.
    #[must_use]
    pub fn window_handle(&self, window_id: WindowId) -> Option<WindowHandle> {
        self.manager.handle(window_id)
    }

    /// Effective retained cursor for the window's current hover position.
    /// Native adapters use this after input and frame updates; applications
    /// should normally express cursor intent through `MouseRegion` instead.
    #[doc(hidden)]
    #[must_use]
    pub fn window_mouse_cursor(&self, window_id: WindowId) -> Option<MouseCursor> {
        self.registry
            .borrow()
            .get(window_id)
            .map(|record| record.runtime.effective_mouse_cursor())
    }

    /// Returns the default application-menu bridges mounted in one retained
    /// window root. Desktop adapters bind them to their native menu delegate.
    #[doc(hidden)]
    #[must_use]
    pub fn platform_menu_bindings(&self, window_id: WindowId) -> Vec<PlatformMenuBinding> {
        self.registry
            .borrow()
            .get(window_id)
            .map_or_else(Vec::new, |record| record.runtime.platform_menu_bindings())
    }

    /// Returns the latest backend capability snapshot for application-level
    /// services. Before a native backend attaches, fields are `Unknown`.
    #[must_use]
    pub fn platform_capabilities(&self) -> PlatformCapabilities {
        *self
            .platform_capabilities
            .read()
            .expect("application capability snapshot lock")
    }

    /// Returns the application-scoped activation stream. Native open-file,
    /// deep-link, reopen, secondary-launch, and global-shortcut events all use
    /// this one ordered delivery path rather than a window input queue.
    #[must_use]
    pub fn activations(&self) -> ApplicationActivationService {
        self.activations.clone()
    }

    /// Returns the application-scoped native global shortcut service.
    #[must_use]
    pub fn global_shortcuts(&self) -> GlobalShortcutService {
        GlobalShortcutService::new(
            self.global_shortcut_bridge.clone(),
            self.platform_capabilities.clone(),
        )
    }

    /// Returns the application-owned native shell service. Tray/status items,
    /// notifications, and taskbar/Dock state are independent of retained widget
    /// lifetime and may therefore be created before or after windows.
    #[must_use]
    pub fn application_shell(&self) -> ApplicationShellService {
        self.application_shell.clone()
    }

    #[doc(hidden)]
    pub fn take_native_application_shell_requests(&self) -> Vec<NativeApplicationShellRequest> {
        self.application_shell.take_native_requests()
    }

    #[doc(hidden)]
    pub fn complete_application_shell_request(&self, completion: NativeApplicationShellCompletion) {
        self.application_shell.complete(completion);
    }

    #[doc(hidden)]
    pub fn handle_application_shell_event(&self, event: NativeApplicationShellEvent) -> bool {
        self.application_shell.handle_native_event(event)
    }

    /// Replaces the launch payload that the desktop runner publishes for this
    /// process. This is useful for custom launchers which already classified
    /// document or URL arguments and want to preserve those semantics.
    pub fn set_launch_activation(&mut self, activation: LaunchActivation) {
        self.launch_activation = activation;
    }

    #[doc(hidden)]
    #[must_use]
    pub fn launch_activation(&self) -> LaunchActivation {
        self.launch_activation.clone()
    }

    /// Enables or disables optional single-instance forwarding. Incular never
    /// enforces this policy unless the application explicitly supplies one.
    pub fn set_single_instance_policy(&mut self, policy: Option<SingleInstancePolicy>) {
        self.single_instance_policy = policy;
    }

    #[must_use]
    pub fn single_instance_policy(&self) -> Option<&SingleInstancePolicy> {
        self.single_instance_policy.as_ref()
    }

    /// Connects URL activations to an existing Router platform-route provider.
    ///
    /// The application supplies URL-to-route interpretation. This keeps deep
    /// link parsing in the route layer and prevents desktop adapters from
    /// acquiring application navigation policy.
    #[must_use]
    pub fn bridge_activation_routes(
        &self,
        provider: Rc<dyn RouteInformationProvider>,
        map: impl Fn(&str) -> Option<RouteInformation> + 'static,
    ) -> ActivationRouteBridge {
        ActivationRouteBridge::new(&self.activations, provider, map)
    }

    /// Publishes a normalized activation from a native/application host. This
    /// method is public only for platform embedders and deterministic tests;
    /// normal application code observes [`Self::activations`] instead.
    #[doc(hidden)]
    pub fn handle_application_activation(&mut self, activation: ApplicationActivation) {
        self.activations.publish(activation);
    }

    /// Replaces the backend's immutable connected-display snapshot.
    ///
    /// Display IDs are allocated/generationally managed by the native backend.
    /// Replacing the snapshot immediately invalidates removed IDs at this API
    /// boundary; a later display using the same slot must have a new generation.
    /// Replaces the native backend's immutable display snapshot catalog.
    ///
    /// This is public for sibling platform adapters and headless/test hosts; UI
    /// code should read displays through [`Application::displays`] or a
    /// [`WindowHandle`](crate::WindowHandle), not manufacture native topology.
    #[doc(hidden)]
    pub fn publish_displays(
        &mut self,
        displays: impl IntoIterator<Item = DisplaySnapshot>,
        primary: Option<DisplayId>,
    ) {
        self.display_catalog
            .write()
            .expect("display catalog lock")
            .replace(displays, primary);
    }

    #[must_use]
    pub fn displays(&self) -> Vec<DisplaySnapshot> {
        self.display_catalog
            .read()
            .expect("display catalog lock")
            .all()
    }

    #[must_use]
    pub fn display(&self, id: DisplayId) -> Option<DisplaySnapshot> {
        self.display_catalog
            .read()
            .expect("display catalog lock")
            .get(id)
    }

    #[must_use]
    pub fn primary_display(&self) -> Option<DisplaySnapshot> {
        self.display_catalog
            .read()
            .expect("display catalog lock")
            .primary()
    }

    #[must_use]
    pub fn current_display(&self, window_id: WindowId) -> Option<DisplaySnapshot> {
        let display_id = self
            .window_handle(window_id)?
            .observed_state()
            .current_display?;
        self.display(display_id)
    }

    /// Publishes one backend/session capability snapshot and applies it to all
    /// currently live windows. Custom embedders may call this before entering
    /// their native event loop.
    pub fn set_platform_capabilities(&mut self, capabilities: PlatformCapabilities) {
        *self
            .platform_capabilities
            .write()
            .expect("application capability snapshot lock") = capabilities;
        for slot in &self.registry.borrow().slots {
            if let Some(record) = slot.record.as_ref() {
                *record
                    .capabilities
                    .write()
                    .expect("window capability snapshot lock") = capabilities;
            }
        }
    }

    /// Returns the capability snapshot associated with one live window.
    #[must_use]
    pub fn window_capabilities(&self, window_id: WindowId) -> Option<PlatformCapabilities> {
        let registry = self.registry.borrow();
        let record = registry.get(window_id)?;
        Some(
            *record
                .capabilities
                .read()
                .expect("window capability snapshot lock"),
        )
    }

    /// Overrides capabilities for one live window when native support depends
    /// on that concrete surface/session rather than the application backend.
    pub fn set_window_capabilities(
        &mut self,
        window_id: WindowId,
        capabilities: PlatformCapabilities,
    ) -> bool {
        let registry = self.registry.borrow();
        let Some(record) = registry.get(window_id) else {
            return false;
        };
        *record
            .capabilities
            .write()
            .expect("window capability snapshot lock") = capabilities;
        true
    }

    /// Publishes the native adapter's resolved presentation for the visible
    /// semantic transients in one retained window. Custom embedders should
    /// replace the complete snapshot after each host-negotiation pass.
    pub fn set_transient_presentations(
        &mut self,
        window_id: WindowId,
        presentations: Vec<TransientPresentationResolution>,
    ) -> bool {
        self.with_window_mut(window_id, |record| {
            let native = presentations
                .iter()
                .filter_map(|presentation| {
                    (presentation.resolved == ResolvedTransientPresentation::Native)
                        .then_some(presentation.id)
                })
                .collect::<Vec<_>>();
            record.runtime.set_native_transient_presentations(native);
            *record
                .transient_presentations
                .write()
                .expect("transient presentation snapshot lock") = presentations;
        })
        .is_some()
    }

    /// Publishes the desktop bounds a confirmed native transient may use for
    /// collision placement. Bounds are parent-view-local logical geometry.
    pub fn set_native_transient_bounds(
        &mut self,
        window_id: WindowId,
        bounds: Option<incular_core::Rect>,
    ) -> bool {
        self.with_window_mut(window_id, |record| {
            record.runtime.set_native_transient_bounds(bounds)
        })
        .unwrap_or(false)
    }

    /// Development-only, read-only view of application windows.  Keeping this
    /// extraction here means platform runners never need to reach into the
    /// retained-window registry.
    #[cfg(feature = "devtools")]
    #[must_use]
    pub fn devtools_windows(&self) -> Vec<incular_devtools_protocol::WindowSummary> {
        self.active_window_ids()
            .into_iter()
            .filter_map(|id| {
                self.window_diagnostics(id)
                    .map(|window| incular_devtools_protocol::WindowSummary {
                        id: incular_devtools_protocol::DevWindowId::new(
                            u64::from(id.index()) + 1,
                            u64::from(id.generation()),
                        ),
                        title: window.title,
                        logical_size: [window.logical_size.width, window.logical_size.height],
                        scale_factor: window.scale_factor,
                    })
            })
            .collect()
    }

    /// Extracts one window's retained widget tree for DevTools.  The method
    /// performs no layout, paint, or allocation proportional to the entire
    /// application beyond the explicit bounded protocol snapshot.
    #[cfg(feature = "devtools")]
    pub fn devtools_widget_tree(
        &self,
        window: incular_devtools_protocol::DevWindowId,
    ) -> Result<Vec<incular_devtools_protocol::TreeDelta>, incular_devtools_protocol::ErrorCode>
    {
        let id = WindowId::from_parts(
            window.index().saturating_sub(1) as u32,
            window.generation() as u32,
        );
        let registry = self.registry.borrow();
        let record = registry
            .get(id)
            .ok_or(incular_devtools_protocol::ErrorCode::StaleId)?;
        let root = record
            .runtime
            .tree()
            .root()
            .ok_or(incular_devtools_protocol::ErrorCode::UnknownId)?;
        let (mut nodes, truncated) = record.runtime.tree().devtools_snapshot(root, false);
        let root = nodes
            .first()
            .cloned()
            .ok_or(incular_devtools_protocol::ErrorCode::UnknownId)?;
        nodes.remove(0);
        Ok(vec![incular_devtools_protocol::TreeDelta::Snapshot {
            window,
            root: Box::new(root),
            nodes,
            truncated,
        }])
    }

    /// Returns curated details for a live DevTools element id, rejecting stale
    /// ids before touching the retained tree.
    #[cfg(feature = "devtools")]
    pub fn devtools_node_details(
        &self,
        id: incular_devtools_protocol::DevWidgetId,
    ) -> Result<incular_devtools_protocol::NodeDetails, incular_devtools_protocol::ErrorCode> {
        for window in self.active_window_ids() {
            let registry = self.registry.borrow();
            let Some(record) = registry.get(window) else {
                continue;
            };
            let tree = record.runtime.tree();
            if let Some(element) = tree.devtools_resolve_id(id) {
                let mut details = tree
                    .devtools_node_details(
                        element,
                        id,
                        incular_devtools_protocol::DevWindowId::new(
                            u64::from(window.index()) + 1,
                            u64::from(window.generation()),
                        ),
                    )
                    .ok_or(incular_devtools_protocol::ErrorCode::StaleId)?;
                let root = record.runtime.devtools_reactive_root();
                details.consumed_signals = crate::devtools_registry::with_all(|signals| {
                    signals
                        .iter()
                        .filter(|(_, registration)| {
                            (registration.subscribers)().into_iter().any(
                                |(subscriber_root, subscriber)| {
                                    subscriber_root == root && subscriber == element
                                },
                            )
                        })
                        .map(|(signal_id, _)| {
                            incular_devtools_protocol::DevSignalId::new(*signal_id, 1)
                        })
                        .collect()
                });
                return Ok(details);
            }
        }
        Err(incular_devtools_protocol::ErrorCode::StaleId)
    }

    /// Applies a supported temporary property override to one live retained
    /// node. Generational IDs make stale edits fail closed.
    #[cfg(feature = "devtools")]
    pub fn devtools_edit_property(
        &mut self,
        id: incular_devtools_protocol::DevWidgetId,
        name: &str,
        value: &incular_devtools_protocol::DebugValue,
    ) -> bool {
        let mut registry = self.registry.borrow_mut();
        for slot in &mut registry.slots {
            let Some(record) = slot.record.as_mut() else {
                continue;
            };
            if record.runtime.devtools_edit_property(id, name, value) {
                return true;
            }
        }
        false
    }

    /// Reads the exact world-space bounds for the selected-widget overlay.
    #[cfg(feature = "devtools")]
    pub fn devtools_node_bounds(
        &self,
        id: incular_devtools_protocol::DevWidgetId,
    ) -> Option<[f32; 4]> {
        for window in self.active_window_ids() {
            let registry = self.registry.borrow();
            let Some(record) = registry.get(window) else {
                continue;
            };
            let tree = record.runtime.tree();
            if let Some(element) = tree.devtools_resolve_id(id) {
                return tree.element_bounds(element).map(|bounds| {
                    [
                        bounds.origin.x,
                        bounds.origin.y,
                        bounds.size.width,
                        bounds.size.height,
                    ]
                });
            }
        }
        None
    }

    /// Read-only retained geometry for selected-node debug adornments.
    #[cfg(feature = "devtools")]
    pub fn devtools_node_overlay_geometry(
        &self,
        id: incular_devtools_protocol::DevWidgetId,
    ) -> Option<incular_widgets::devtools::DevOverlayGeometry> {
        for window in self.active_window_ids() {
            let registry = self.registry.borrow();
            let Some(record) = registry.get(window) else {
                continue;
            };
            let tree = record.runtime.tree();
            if let Some(element) = tree.devtools_resolve_id(id) {
                return tree.devtools_overlay_geometry(element);
            }
        }
        None
    }

    /// Bounded exact retained layout rectangles for one target window. The
    /// platform adapter consumes these in its compositor-only overlay pass;
    /// no widget, layout or semantics state changes as a consequence.
    #[cfg(feature = "devtools")]
    pub fn devtools_window_layout_bounds(&self, window: WindowId, limit: usize) -> Vec<[f32; 4]> {
        self.registry
            .borrow()
            .get(window)
            .map(|record| record.runtime.tree().devtools_layout_bounds(limit))
            .unwrap_or_default()
    }

    #[cfg(feature = "devtools")]
    pub fn devtools_window_semantics_bounds(
        &self,
        window: WindowId,
        limit: usize,
    ) -> Vec<[f32; 4]> {
        self.registry
            .borrow()
            .get(window)
            .map(|record| record.runtime.tree().devtools_semantics_bounds(limit))
            .unwrap_or_default()
    }

    #[cfg(feature = "devtools")]
    pub fn devtools_window_hit_regions(&self, window: WindowId, limit: usize) -> Vec<[f32; 4]> {
        self.registry
            .borrow()
            .get(window)
            .map(|record| record.runtime.tree().devtools_hit_regions(limit))
            .unwrap_or_default()
    }

    #[cfg(feature = "devtools")]
    pub fn devtools_window_scroll_viewports(
        &self,
        window: WindowId,
        limit: usize,
    ) -> Vec<[f32; 4]> {
        self.registry
            .borrow()
            .get(window)
            .map(|record| record.runtime.tree().devtools_scroll_viewports(limit))
            .unwrap_or_default()
    }

    #[cfg(feature = "devtools")]
    pub fn devtools_window_layer_bounds(&self, window: WindowId, limit: usize) -> Vec<[f32; 4]> {
        self.registry
            .borrow()
            .get(window)
            .map(|record| record.runtime.tree().devtools_layer_bounds(limit))
            .unwrap_or_default()
    }

    /// Bounded existing per-node work counters for target-side phase flashes.
    #[cfg(feature = "devtools")]
    pub fn devtools_window_phase_nodes(
        &self,
        window: WindowId,
        limit: usize,
    ) -> Vec<incular_widgets::devtools::DevPhaseNode> {
        self.registry
            .borrow()
            .get(window)
            .map(|record| record.runtime.tree().devtools_phase_nodes(limit))
            .unwrap_or_default()
    }

    /// Enables one bounded per-node Deep trace for the next frame of this
    /// window. Normal/Performance profiler modes never call this, so the hot
    /// path has only compile-time-gated instrumentation checks.
    #[cfg(feature = "devtools")]
    pub fn devtools_begin_deep_trace(&mut self, window: WindowId, max_events: usize) -> bool {
        self.with_window_mut(window, |record| {
            record.runtime.tree_mut().begin_deep_trace(max_events);
        })
        .is_some()
    }

    /// Takes the completed per-node trace without cloning its event buffer.
    #[cfg(feature = "devtools")]
    pub fn devtools_take_deep_trace(
        &mut self,
        window: WindowId,
        frame: u64,
    ) -> Option<incular_devtools_protocol::DeepFrameTrace> {
        let (events, dropped_events) = self
            .with_window_mut(window, |record| record.runtime.tree_mut().take_deep_trace())
            .flatten()?;
        Some(incular_devtools_protocol::DeepFrameTrace {
            window: incular_devtools_protocol::DevWindowId::new(
                u64::from(window.index()) + 1,
                u64::from(window.generation()),
            ),
            frame,
            truncated: dropped_events > 0,
            dropped_events,
            events,
        })
    }

    /// Exact retained subtree bounds for the selected DevTools node, together
    /// with its owning native window. Stale IDs return no result.
    #[cfg(feature = "devtools")]
    pub fn devtools_subtree_layout_bounds(
        &self,
        id: incular_devtools_protocol::DevWidgetId,
        limit: usize,
    ) -> Option<(WindowId, Vec<[f32; 4]>)> {
        for window in self.active_window_ids() {
            let registry = self.registry.borrow();
            let Some(record) = registry.get(window) else {
                continue;
            };
            let tree = record.runtime.tree();
            if let Some(element) = tree.devtools_resolve_id(id) {
                return Some((window, tree.devtools_subtree_layout_bounds(element, limit)));
            }
        }
        None
    }

    /// Hit-tests a retained root for Select Widget mode without dispatching
    /// the pointer to application widgets.
    #[cfg(feature = "devtools")]
    pub fn devtools_hit_test(
        &self,
        window: WindowId,
        point: incular_core::Offset,
    ) -> Option<(incular_devtools_protocol::DevWidgetId, [f32; 4])> {
        let registry = self.registry.borrow();
        let record = registry.get(window)?;
        let tree = record.runtime.tree();
        let element = tree.devtools_deepest_at(point)?;
        let id = tree.devtools_id_for_element(element)?;
        let bounds = tree.element_bounds(element)?;
        Some((
            id,
            [
                bounds.origin.x,
                bounds.origin.y,
                bounds.size.width,
                bounds.size.height,
            ],
        ))
    }

    /// Framework-owned memory/resource inventory.  This is deliberately a
    /// count of retained Incular objects, not a misleading attempt to inspect
    /// Rust's allocator or another crate's heap.
    #[cfg(feature = "devtools")]
    #[must_use]
    pub fn devtools_resource_counts(&self) -> incular_devtools_protocol::ResourceCounts {
        let mut counts = incular_devtools_protocol::ResourceCounts::default();
        for window in self.active_window_ids() {
            let registry = self.registry.borrow();
            let Some(record) = registry.get(window) else {
                continue;
            };
            let tree = record.runtime.tree();
            counts.elements += tree.element_count();
            counts.render_objects += tree.render_object_count();
            counts.semantics_nodes += tree.semantics().len();
            counts.tasks_active += record.runtime.runtime_diagnostics().active_tracked_tasks;
        }
        crate::devtools_registry::with_all(|signals| counts.signals = signals.len());
        counts
    }

    /// Returns only signals which explicitly opted into DevTools visibility.
    /// Values are already reduced to bounded summaries at write time.
    #[cfg(feature = "devtools")]
    #[must_use]
    pub fn devtools_signals(&self) -> Vec<incular_devtools_protocol::SignalSummary> {
        crate::devtools_registry::with_all(|signals| {
            signals
                .iter()
                .map(|(id, registration)| {
                    let (_, last_write) = (registration.last_write)();
                    let writes = (registration.write_count)();
                    incular_devtools_protocol::SignalSummary {
                        id: incular_devtools_protocol::DevSignalId::new(*id, 1),
                        name: registration.name.clone(),
                        type_name: registration.type_name.to_owned(),
                        generation: writes,
                        write_count: writes,
                        subscriber_count: (registration.subscriber_count)(),
                        last_write_summary: last_write,
                        editable: registration.editable_kind.is_some(),
                    }
                })
                .collect()
        })
    }

    /// Resolves actual live reactive dependencies for a DevTools-visible
    /// signal.  The registry stores only weak signal state and Element ids;
    /// this method resolves those ids against the matching live window tree,
    /// so unmounted elements are never retained or reported.
    #[cfg(feature = "devtools")]
    #[must_use]
    pub fn devtools_signal_subscribers(
        &self,
        signal: incular_devtools_protocol::DevSignalId,
    ) -> Vec<incular_devtools_protocol::SignalSubscriber> {
        let entries = crate::devtools_registry::with_all(|signals| {
            signals
                .iter()
                .find(|(id, _)| *id == signal.index())
                .and_then(|(_, registration)| {
                    (signal.generation() == 1).then(|| (registration.subscribers)())
                })
                .unwrap_or_default()
        });
        if entries.is_empty() {
            return Vec::new();
        }

        let windows = self.active_window_ids();
        let registry = self.registry.borrow();
        windows
            .into_iter()
            .flat_map(|window| {
                let Some(record) = registry.get(window) else {
                    return Vec::new();
                };
                let root = record.runtime.devtools_reactive_root();
                let tree = record.runtime.tree();
                entries
                    .iter()
                    .filter(|(entry_root, _)| *entry_root == root)
                    .filter_map(|(_, element)| {
                        tree.devtools_id_for_element(*element).map(|dev_id| {
                            incular_devtools_protocol::SignalSubscriber {
                                signal,
                                element: dev_id,
                                path: tree.devtools_element_path(*element),
                            }
                        })
                    })
                    .collect::<Vec<_>>()
            })
            .collect()
    }

    /// Applies an explicitly opted-in typed DevTools edit on the UI thread.
    /// The conversion and the normal `Signal::set` invalidation both live in
    /// the signal registration; unsupported types are rejected.
    #[cfg(feature = "devtools")]
    pub fn devtools_edit_signal(
        &self,
        signal: incular_devtools_protocol::DevSignalId,
        value: &incular_devtools_protocol::EditableValue,
    ) -> bool {
        if signal.generation() != 1 {
            return false;
        }
        crate::devtools_registry::with_all(|signals| {
            signals
                .iter()
                .find(|(id, registration)| {
                    *id == signal.index() && registration.editable_kind.is_some()
                })
                .is_some_and(|(_, registration)| (registration.apply_edit)(value))
        })
    }

    #[must_use]
    pub fn contains_window(&self, window_id: WindowId) -> bool {
        self.registry.borrow().contains(window_id)
    }

    /// Visible transient surfaces belonging to `window_id` after its latest
    /// retained layout. Geometry is window-local logical space and therefore
    /// remains anchored when the native parent moves; the platform adapter is
    /// responsible only for mapping that geometry into desktop coordinates.
    #[must_use]
    pub fn transient_surfaces(
        &self,
        window_id: WindowId,
    ) -> Vec<incular_widgets::TransientSurfaceSnapshot> {
        self.registry
            .borrow()
            .get(window_id)
            .map_or_else(Vec::new, |record| record.runtime.transient_surfaces())
    }

    pub fn set_last_window_policy(&mut self, policy: LastWindowPolicy) {
        self.last_window_policy = policy;
    }

    #[must_use]
    pub const fn last_window_policy(&self) -> LastWindowPolicy {
        self.last_window_policy
    }

    /// Installs the application policy used for a native close request. The
    /// callback returns `true` to accept closing and `false` to keep the
    /// window alive (for example, an unsaved-work dialog flow).
    pub fn on_close_request(&mut self, callback: impl FnMut(WindowId) -> bool + 'static) {
        self.close_request = Some(Box::new(callback));
    }

    pub fn set_wake_handler(&mut self, wake: Arc<dyn RuntimeWake>) {
        self.scheduler.borrow_mut().set_wake(wake.clone());
        self.manager.bridge.set_wake(wake.clone());
        self.file_dialog_bridge.set_wake(wake.clone());
        self.global_shortcut_bridge.set_wake(wake.clone());
        self.application_shell.set_wake(wake.clone());
        self.simulation_bridge.set_wake(wake);
    }

    /// Returns a cloneable, in-process controller for the primary window.
    /// Simulation commands are serviced by the native event loop and never
    /// synthesize OS-level mouse or keyboard input.
    #[must_use]
    pub fn simulation(&self) -> Simulation {
        Simulation::new(self.simulation_bridge.clone(), self.primary_window)
    }

    pub(crate) fn with_window_mut<R>(
        &mut self,
        window_id: WindowId,
        callback: impl FnOnce(&mut WindowRecord) -> R,
    ) -> Option<R> {
        let mut record = self.registry.borrow_mut().take(window_id)?;
        let result = callback(&mut record);
        // `callback` may have opened a different window through BuildContext,
        // so borrow the registry only after it returns.
        // The root being serviced remains generationally reserved throughout.
        self.registry.borrow_mut().restore(window_id, record);
        Some(result)
    }
    /// Starts application-scoped Tokio work. It is not cancelled when any
    /// individual window closes.
    pub fn spawn<F, T>(&self, future: F) -> Task<T>
    where
        F: std::future::Future<Output = T> + Send + 'static,
        T: Send + 'static,
    {
        tasks::TaskScheduler::spawner(&self.scheduler).spawn(future)
    }

    pub fn spawn_blocking<T>(
        &self,
        work: impl FnOnce() -> T + Send + 'static,
        complete: impl FnOnce(Result<T, TaskFailure>, &mut Runtime) + 'static,
    ) -> TaskHandle
    where
        T: Send + 'static,
    {
        self.scheduler.borrow_mut().spawn_blocking(work, complete)
    }

    /// Queues a UI callback for the primary window. For work targeting another
    /// root, capture that root's `BuildContext` dispatcher instead.
    pub fn dispatch(&self, callback: impl FnOnce(&mut Runtime) + Send + 'static) {
        self.scheduler
            .borrow()
            .dispatcher_for(Some(self.primary_window))
            .dispatch(callback);
    }

    pub fn spawn_into<F, T>(
        &self,
        future: F,
        complete: impl FnOnce(Result<T, TaskFailure>, &mut Runtime) + 'static,
    ) -> TaskHandle
    where
        F: std::future::Future<Output = T> + Send + 'static,
        T: Send + 'static,
    {
        tasks::TaskScheduler::spawner(&self.scheduler).spawn_into(future, complete)
    }

    #[must_use]
    pub fn tokio_handle(&self) -> TokioHandle {
        self.scheduler.borrow().tokio_handle()
    }

    /// Drains at most one bounded Tokio UI turn, routing each completion to
    /// the window scope that created it. Closed/reused IDs discard stale work.
    pub fn process_runtime_work_at(&mut self, _now: Instant) {
        let work = self.scheduler.borrow_mut().take_turn();
        for item in work {
            let requested_window = match &item {
                tasks::UiWork::Dispatch { window_id, .. } => *window_id,
                tasks::UiWork::Task(pending) => pending.control.window_id(),
            };
            // Application-scoped work (including restoration write completion)
            // is not owned by a particular root. If the original primary has
            // closed while another window remains, deliver it to that live UI
            // turn rather than discarding persistence bookkeeping.
            let window_id = requested_window.or_else(|| {
                if self.contains_window(self.primary_window) {
                    Some(self.primary_window)
                } else {
                    self.active_window_ids().into_iter().next()
                }
            });
            let Some(window_id) = window_id else {
                self.discard_stale_ui_work(item);
                continue;
            };
            let record = self.registry.borrow_mut().take(window_id);
            if let Some(mut record) = record {
                record.runtime.process_ui_work(&self.scheduler, item);
                self.registry.borrow_mut().restore(window_id, record);
            } else {
                self.discard_stale_ui_work(item);
            }
        }
        self.drain_window_commands();
        self.drain_native_request_cancellations();
        self.drain_file_dialog_requests();
        self.drain_file_dialog_cancellations();
        self.drain_global_shortcut_requests();
        self.process_simulation_requests();
    }

    pub fn process_runtime_work(&mut self) {
        self.process_runtime_work_at(Instant::now());
    }

    fn discard_stale_ui_work(&mut self, work: tasks::UiWork) {
        if let tasks::UiWork::Task(pending) = work {
            self.scheduler.borrow().detach_scope(&pending.control);
            (pending.discard)();
            self.scheduler
                .borrow_mut()
                .record_discard(pending.blocking, true);
        }
    }

    /// Routes external platform transfer data to one retained window and
    /// returns the operation requested by the currently hit-tested target.
    /// Native adapters with richer drag protocols can feed this response back
    /// to the source; adapters such as Winit file-drop may simply ignore it.
    #[must_use]
    pub fn handle_external_drag_event(
        &mut self,
        window_id: WindowId,
        event: ExternalDragEvent,
    ) -> ExternalDragResponse {
        self.with_window_mut(window_id, |record| {
            record.input_events = record.input_events.wrapping_add(1);
            record.runtime.handle_external_drag(event)
        })
        .unwrap_or_default()
    }

    /// Routes a normalized event to exactly one retained root.
    pub fn handle_window_event(&mut self, event: WindowEvent) {
        let window_id = event.window_id;
        match event.kind {
            WindowEventKind::Platform(PlatformEvent::CloseRequested) => {
                self.request_close(window_id);
            }
            WindowEventKind::Platform(PlatformEvent::Metrics(metrics)) => {
                let _ = self.with_window_mut(window_id, |record| {
                    if record.metrics != metrics {
                        record.metrics = metrics;
                        record.surface_generation = record
                            .surface_generation
                            .checked_add(1)
                            .expect("runtime surface generation exhausted");
                        record.runtime.update_window_metrics(metrics);
                    }
                });
                self.manager.sync_restorable_windows(true);
            }
            WindowEventKind::Platform(PlatformEvent::Environment(environment)) => {
                let _ = self.with_window_mut(window_id, |record| {
                    record.runtime.set_environment(environment);
                });
            }
            WindowEventKind::Platform(PlatformEvent::Input(input)) => {
                let _ = self.with_window_mut(window_id, |record| {
                    record.input_events = record.input_events.wrapping_add(1);
                    let _ = record.runtime.handle_input(input);
                });
            }
            WindowEventKind::Platform(PlatformEvent::TextInputAction(action)) => {
                let _ = self.with_window_mut(window_id, |record| {
                    record.input_events = record.input_events.wrapping_add(1);
                    let _ = record.runtime.handle_text_input_action(action);
                });
            }
            WindowEventKind::Platform(PlatformEvent::Lifecycle(lifecycle)) => {
                self.handle_application_lifecycle(lifecycle);
            }
            WindowEventKind::Platform(PlatformEvent::ExternalDrag(event)) => {
                let _ = self.handle_external_drag_event(window_id, event);
            }
            WindowEventKind::Lifecycle(lifecycle) => {
                let _ = self.with_window_mut(window_id, |record| {
                    record.lifecycle = lifecycle;
                    match lifecycle {
                        WindowLifecycle::Focused => {
                            record.native_focused = true;
                            let mut environment = record.runtime.environment();
                            environment.window_focused = true;
                            record.runtime.set_environment(environment);
                        }
                        WindowLifecycle::Unfocused => {
                            record.native_focused = false;
                            record.runtime.dismiss_transients(
                                incular_widgets::TransientDismissReason::ParentDeactivated,
                            );
                            let mut environment = record.runtime.environment();
                            environment.window_focused = false;
                            record.runtime.set_environment(environment);
                        }
                        WindowLifecycle::Visible
                        | WindowLifecycle::Hidden
                        | WindowLifecycle::Creating
                        | WindowLifecycle::Closing
                        | WindowLifecycle::Closed => {}
                    }
                });
            }
            WindowEventKind::RedrawRequested => {}
            WindowEventKind::StateChanged(state) => {
                let _ = self.with_window_mut(window_id, |record| {
                    *record
                        .observed_state
                        .write()
                        .expect("window observed-state snapshot lock") = state;
                });
            }
        }
    }

    /// Applies one genuine application-level lifecycle transition to every
    /// retained window root. Window focus remains a separate per-window
    /// lifecycle channel and never synthesizes these application states.
    pub fn handle_application_lifecycle(&mut self, lifecycle: PlatformLifecycle) {
        self.manager.application_lifecycle.set(match lifecycle {
            PlatformLifecycle::Active | PlatformLifecycle::Resumed => {
                crate::application_types::ApplicationLifecycle::Active
            }
            PlatformLifecycle::Inactive => crate::application_types::ApplicationLifecycle::Inactive,
            PlatformLifecycle::Suspended => {
                crate::application_types::ApplicationLifecycle::Suspended
            }
            PlatformLifecycle::Stopping => crate::application_types::ApplicationLifecycle::Stopping,
        });
        if matches!(lifecycle, PlatformLifecycle::Stopping) {
            self.shutdown();
            return;
        }
        let ids = self.active_window_ids();
        for id in ids {
            let _ = self.with_window_mut(id, |record| {
                let _ = record
                    .runtime
                    .handle_platform_event(PlatformEvent::Lifecycle(lifecycle));
            });
        }
        if matches!(lifecycle, PlatformLifecycle::Suspended) {
            self.flush_restoration_and_wait();
        }
    }

    /// Records a backend failure for a user-driven operation that does not
    /// have an explicit result consumer (for example a `WindowDragRegion`).
    pub fn record_platform_operation_error(
        &mut self,
        window_id: WindowId,
        error: PlatformOperationError,
    ) -> bool {
        self.with_window_mut(window_id, |record| {
            record.last_platform_error = Some(error);
        })
        .is_some()
    }

    pub fn set_window_environment(
        &mut self,
        window_id: WindowId,
        environment: RuntimeEnvironment,
    ) -> bool {
        self.with_window_mut(window_id, |record| {
            record.runtime.set_environment(environment)
        })
        .unwrap_or(false)
    }

    /// Installs a platform clipboard for one window's input/editing bridge.
    /// The clipboard service itself may be application-wide; the runtime keeps
    /// the mutable adapter at the window input boundary.
    pub fn set_window_clipboard(&mut self, window_id: WindowId, clipboard: Box<dyn Clipboard>) {
        let _ = self.with_window_mut(window_id, |record| {
            record.runtime.set_clipboard(clipboard);
        });
    }

    /// Drains text-input commands for one native window. The caller applies
    /// them on that window's platform/UI thread.
    pub fn take_window_text_input_commands(
        &mut self,
        window_id: WindowId,
    ) -> Vec<TextInputCommand> {
        self.with_window_mut(window_id, |record| {
            record.runtime.take_text_input_commands()
        })
        .unwrap_or_default()
    }

    #[must_use]
    pub fn frame_requested(&self, window_id: WindowId) -> bool {
        self.registry
            .borrow()
            .slots
            .get(window_id.index() as usize)
            .filter(|slot| slot.generation == window_id.generation())
            .and_then(|slot| slot.record.as_ref())
            .is_some_and(|record| record.runtime.frame_requested())
    }

    pub fn note_frame_requested(&mut self, window_id: WindowId) {
        scheduler_counters::REDRAW_REQUESTS.fetch_add(1, Ordering::Relaxed);
        self.scheduler_counters.redraw_requests =
            self.scheduler_counters.redraw_requests.wrapping_add(1);
        let _ = self.with_window_mut(window_id, |record| {
            record.requested_frames = record.requested_frames.wrapping_add(1);
        });
    }

    /// Records one UI-relevant runtime wake (async completion or message).
    /// Wakes that mutate no visible state must not produce redraws; the idle
    /// contract test relies on these counters staying independent.
    pub fn note_runtime_wake(&mut self) {
        scheduler_counters::RUNTIME_WAKES.fetch_add(1, Ordering::Relaxed);
        self.scheduler_counters.runtime_wakes =
            self.scheduler_counters.runtime_wakes.wrapping_add(1);
    }

    pub fn run_window_frame_at(
        &mut self,
        window_id: WindowId,
        constraints: Constraints,
        now: Instant,
    ) -> Result<Option<(DisplayList, FrameStats)>, TreeError> {
        let mut sensitivity_update = None;
        let mut content_size_request = None;
        let outcome = self
            .with_window_mut(window_id, |record| {
                if record.metrics.physical_size.is_zero() {
                    record.skipped_frames = record.skipped_frames.wrapping_add(1);
                    scheduler_counters::FRAMES_SKIPPED.fetch_add(1, Ordering::Relaxed);
                    Ok(None)
                } else {
                    let frame_constraints =
                        ContentSizeCoordinator::layout_constraints(&record.options, constraints);
                    record
                        .runtime
                        .run_frame_at(frame_constraints, now)
                        .map(|(list, stats)| {
                            let sensitivity = record.runtime.content_sensitivity();
                            if record.last_content_sensitivity != Some(sensitivity) {
                                record.last_content_sensitivity = Some(sensitivity);
                                sensitivity_update = Some(sensitivity);
                            }
                            content_size_request = record.content_sizing.reconcile(
                                &record.options,
                                record.metrics,
                                record.runtime.tree().root_layout_size(),
                            );
                            record.last_frame = FrameRecord {
                                frame: 0,
                                timings: stats.timings,
                                work: FrameWork {
                                    updated_elements: stats.updated_elements as u64,
                                    rebuilt_elements: stats.rebuilt_elements,
                                    laid_out_render_objects: stats.laid_out_render_objects,
                                    repainted_render_objects: stats.repainted_render_objects,
                                    composited_layers: stats.composited,
                                    active_animations: stats.active_animations,
                                    display_list_commands: stats.display_list_commands,
                                    requested_another_frame: stats.requested_another_frame,
                                },
                                over_budget: false,
                            };
                            Some((list, stats))
                        })
                }
            })
            .unwrap_or(Ok(None));
        if let Some(size) = content_size_request {
            self.native_commands
                .borrow_mut()
                .push_back(NativeWindowCommand::Operate(WindowCommand::new(
                    window_id,
                    WindowOperation::SetLogicalSize(size),
                )));
        }
        if let Some(sensitivity) = sensitivity_update {
            self.native_commands
                .borrow_mut()
                .push_back(NativeWindowCommand::Operate(WindowCommand::new(
                    window_id,
                    WindowOperation::SetContentSensitivity(sensitivity),
                )));
        }
        if matches!(outcome, Ok(None)) && self.contains_window(window_id) {
            self.scheduler_counters.frames_skipped =
                self.scheduler_counters.frames_skipped.wrapping_add(1);
        }
        if matches!(outcome, Ok(Some(_))) {
            scheduler_counters::FRAMES_STARTED.fetch_add(1, Ordering::Relaxed);
            self.scheduler_counters.frames_started =
                self.scheduler_counters.frames_started.wrapping_add(1);
        }
        outcome
    }

    /// Projects this window's retained semantics through its window-local
    /// AccessKit bridge. Native adapters call this on the UI/event-loop thread
    /// only after AccessKit activation; a changed semantic revision produces a
    /// full or incremental tree update while ordinary paint/compositor frames
    /// produce no work.
    pub fn sync_accessibility(
        &mut self,
        window_id: WindowId,
        projection: &mut AccessKitProjection,
    ) -> Option<NativeAccessibilityUpdate> {
        let update = self
            .with_window_mut(window_id, |record| {
                projection.sync(
                    record.runtime.tree().semantics(),
                    record.metrics.scale_factor,
                )
            })
            .flatten();
        let diagnostics = projection.diagnostics();
        let _ = self.with_window_mut(window_id, |record| {
            record.accessibility = diagnostics;
        });
        update
    }

    /// Receives an already validated native semantic request for exactly one
    /// window. It uses the ordinary retained runtime action dispatcher, so
    /// native callbacks never mutate widget/controller state directly.
    pub fn dispatch_accessibility_action(
        &mut self,
        window_id: WindowId,
        request: SemanticActionRequest,
    ) -> bool {
        self.with_window_mut(window_id, |record| {
            record
                .runtime
                .dispatch_semantic_action(request.node, request.action)
        })
        .unwrap_or(false)
    }

    /// Turns one window's per-frame semantics pass on or off. Native hosts
    /// enable it when an assistive technology requests the tree; enabling
    /// schedules a frame so the first native update is complete.
    pub fn set_semantics_enabled(&mut self, window_id: WindowId, enabled: bool) {
        let _ = self.with_window_mut(window_id, |record| {
            let runtime = &mut record.runtime;
            if runtime.tree().semantics_enabled() != enabled {
                runtime.tree_mut().set_semantics_enabled(enabled);
                if enabled {
                    runtime.request_frame();
                }
            }
        });
    }

    /// Updates one window's value-free bridge diagnostics after a lifecycle or
    /// action event that did not emit a tree update.
    pub fn set_accessibility_diagnostics(
        &mut self,
        window_id: WindowId,
        diagnostics: AccessibilityDiagnostics,
    ) {
        let _ = self.with_window_mut(window_id, |record| {
            record.accessibility = diagnostics;
        });
    }

    /// Selects how much profiling state is retained. Normal keeps only
    /// counters and the latest frame; higher modes retain bounded history.
    pub fn set_profiler_mode(&mut self, mode: ProfilerMode) {
        self.profiler.set_mode(mode);
    }

    /// Applies the DevTools animation speed to every live window. This is a
    /// retained animation-clock setting, not a scheduler or Tokio setting.
    pub fn set_animation_time_scale(&mut self, scale: f32) {
        for window in self.active_window_ids() {
            let _ = self.with_window_mut(window, |record| {
                record.runtime.set_animation_time_scale(scale);
            });
        }
    }

    /// Rebuilds every retained builder in every live window on the next
    /// frame. Element identity, focus, scroll offsets and signal values are
    /// kept; only widget descriptions and the callbacks they carry are
    /// replaced. Use this after application code was swapped underneath a
    /// running tree.
    #[cfg(feature = "hot-reload")]
    pub fn reassemble(&mut self) {
        for window in self.active_window_ids() {
            let _ = self.with_window_mut(window, |record| record.runtime.reassemble());
        }
    }

    /// Loads a hot patch and [reassembles](Self::reassemble) every window.
    /// A patch that fails to load leaves the running code untouched.
    ///
    /// # Safety
    ///
    /// The patch must have been built against the executable image of this
    /// exact process; see [`HotPatch::apply`](crate::HotPatch::apply).
    #[cfg(feature = "hot-reload")]
    pub unsafe fn apply_hot_patch(
        &mut self,
        patch: crate::HotPatch,
    ) -> Result<(), crate::HotPatchError> {
        // SAFETY: forwarded caller contract.
        unsafe { patch.apply() }?;
        self.reassemble();
        Ok(())
    }

    #[must_use]
    pub const fn profiler_mode(&self) -> ProfilerMode {
        self.profiler.mode()
    }

    /// Derives frame budgets from an actual refresh rate (Hz). `None` clears.
    pub fn set_refresh_rate_hz(&mut self, hz: Option<f32>) {
        self.profiler.set_refresh_rate_hz(hz);
    }

    /// Merges renderer-reported metrics into one window's latest record and
    /// feeds the application profiler history with a complete frame sample.
    pub fn note_render_metrics(
        &mut self,
        window_id: WindowId,
        mut render: RenderFrameMetrics,
        gpu: Option<GpuSample>,
    ) {
        let frame_id = self.profiler.next_frame_id();
        render.frame = frame_id;
        let mut completed = None;
        {
            let mut registry = self.registry.borrow_mut();
            if let Some(slot) = registry.slots.get_mut(window_id.index() as usize)
                && slot.generation == window_id.generation()
                && let Some(record) = slot.record.as_mut()
            {
                record.render_metrics = render;
                record.gpu_sample = gpu;
                record.last_frame.frame = frame_id;
                // Renderer prepare/encode/submit completes the CPU picture.
                let mut timings = record.last_frame.timings;
                timings.cpu_total = timings
                    .cpu_total
                    .saturating_add(render.prepare_us)
                    .saturating_add(render.encode_us)
                    .saturating_add(render.submit_us);
                record.last_frame.timings = timings;
                completed = Some(record.last_frame);
            }
        }
        if let Some(record) = completed {
            self.profiler.record(record);
        }
        // Throttled publish for observers (debug overlays). Production frames
        // with no observer and Normal mode skip the snapshot build entirely.
        if self.hub.observed()
            && self.profiler.mode() >= ProfilerMode::Diagnostic
            && self.hub.publish_due(std::time::Duration::from_millis(200))
        {
            self.hub.publish(self.performance_snapshot());
        }
    }

    /// Shared handle to the observable performance snapshot. Overlay builders
    /// read [`PerformanceHub::version`] so only they rebuild on publish.
    #[must_use]
    pub const fn performance_hub(&self) -> &PerformanceHub {
        &self.hub
    }

    /// Installs the debug performance overlay into `window_id`.
    ///
    /// The application tree must contain a widget keyed with
    /// [`PERFORMANCE_OVERLAY_KEY`]; that placeholder element is replaced by a
    /// repaint-contained overlay whose builder re-runs **only** when the hub
    /// publishes, so measured widget work is unaffected. Requires a profiler
    /// mode of [`ProfilerMode::Diagnostic`] or higher to observe data.
    pub fn install_performance_overlay(&mut self, window_id: WindowId) -> Result<(), TreeError> {
        let hub = self.hub.clone();
        let installed = self.with_window_mut(window_id, |record| {
            let tree = record.runtime.tree();
            let Some(target) = tree.element_with_key(&Key::from(PERFORMANCE_OVERLAY_KEY)) else {
                return Err(TreeError::MissingElement(
                    tree.root().expect("mounted window root"),
                ));
            };
            record.runtime.register_builder(target, move || {
                hub.set_observed(true);
                // Reading the version subscribes this element alone.
                let _version = hub.version();
                overlay_widget(&hub.snapshot())
            })?;
            Ok(())
        });
        match installed {
            Some(result) => result,
            None => Err(TreeError::WindowUnknown),
        }
    }

    #[must_use]
    pub fn profiler_history(&self) -> &FrameHistory {
        self.profiler.history()
    }

    /// Builds a complete read-only performance view. This allocates and is
    /// intended for diagnostics tooling, the debug overlay, and JSON export —
    /// never for the per-frame hot path.
    #[must_use]
    pub fn performance_snapshot(&self) -> PerformanceSnapshot {
        let mut windows = Vec::new();
        let registry = self.registry.borrow();
        for slot in &registry.slots {
            let Some(record) = slot.record.as_ref() else {
                continue;
            };
            windows.push(WindowPerformance {
                requested_frames: record.requested_frames,
                presented_frames: record.presented_frames,
                skipped_frames: record.skipped_frames,
                latest: Some(record.last_frame),
                render: record.render_metrics,
                gpu: record.gpu_sample,
            });
        }
        // Widget/text totals come from the primary window's retained tree;
        // accessibility counters merge across every live window adapter.
        let mut widgets = WidgetWorkSnapshot::default();
        let mut text = TextCacheSnapshot::default();
        let mut accessibility = AccessibilitySnapshot::default();
        for slot in &registry.slots {
            let Some(record) = slot.record.as_ref() else {
                continue;
            };
            let tree = record.runtime.tree();
            if widgets.elements_total == 0 {
                let diagnostics = tree.diagnostics();
                widgets = WidgetWorkSnapshot {
                    mounts: diagnostics.mounts,
                    unmounts: diagnostics.unmounts,
                    rebuilds: diagnostics.rebuilds,
                    layouts: diagnostics.layouts,
                    paints: diagnostics.paints,
                    composites: diagnostics.composites,
                    animation_ticks: diagnostics.animation_ticks,
                    scroll_offset_updates: diagnostics.scroll_offset_updates,
                    reconciliation_fast_paths: diagnostics.reconciliation_fast_paths,
                    layout_cache_hits: diagnostics.layout_cache_hits,
                    display_lists_reused: diagnostics.display_lists_reused,
                    compositor_only_updates: diagnostics.compositor_only_updates,
                    lazy_layouts: diagnostics.lazy_layouts,
                    items_built: diagnostics.items_built,
                    items_reused: diagnostics.items_reused,
                    child_list_scans: diagnostics.child_list_scans,
                    identical_child_bailouts: diagnostics.identical_child_bailouts,
                    elements_created: diagnostics.elements_created,
                    elements_removed: diagnostics.elements_removed,
                    elements_moved: diagnostics.elements_moved,
                    dirty_requests: diagnostics.dirty_requests,
                    dirty_queue_deduplicated: diagnostics.dirty_queue_deduplicated,
                    elements_total: tree.element_count(),
                    render_objects_total: tree.render_object_count(),
                    layers_total: usize::try_from(tree.compositor_diagnostics().layers)
                        .unwrap_or(0),
                };
                let text_diagnostics = tree.text_diagnostics();
                text = TextCacheSnapshot {
                    layouts_requested: text_diagnostics.layouts_requested,
                    cache_hits: text_diagnostics.cache_hits,
                    cache_misses: text_diagnostics.cache_misses,
                    paragraphs_reshaped: text_diagnostics.paragraphs_reshaped,
                    parley_layouts_reused: text_diagnostics.parley_layouts_reused,
                    documents_composed: text_diagnostics.documents_composed,
                };
            }
            accessibility.nodes_published += record.accessibility.nodes_published;
            accessibility.updates_skipped_unchanged +=
                record.accessibility.semantic_updates_skipped_unchanged;
        }
        let mut scheduler = scheduler_counters();
        self.scheduler_counters.apply_to(&mut scheduler);
        PerformanceSnapshot {
            scheduler,
            budget: *self.profiler.budget(),
            fps: self.profiler.frames_per_second(),
            windows,
            frame_statistics: self.profiler.history().statistics(),
            widgets,
            text,
            accessibility,
        }
    }

    #[must_use]
    pub const fn profiler_budget(&self) -> &BudgetStatistics {
        self.profiler.budget()
    }

    pub fn note_presented(&mut self, window_id: WindowId, presented: bool) {
        if presented {
            scheduler_counters::FRAMES_PRESENTED.fetch_add(1, Ordering::Relaxed);
            self.scheduler_counters.frames_presented =
                self.scheduler_counters.frames_presented.wrapping_add(1);
        } else {
            scheduler_counters::FRAMES_SKIPPED.fetch_add(1, Ordering::Relaxed);
            self.scheduler_counters.frames_skipped =
                self.scheduler_counters.frames_skipped.wrapping_add(1);
        }
        self.profiler.note_present(presented);
        let _ = self.with_window_mut(window_id, |record| {
            if presented {
                record.presented_frames = record.presented_frames.wrapping_add(1);
            } else {
                record.skipped_frames = record.skipped_frames.wrapping_add(1);
            }
        });
    }

    pub fn request_close(&mut self, window_id: WindowId) -> bool {
        if !self.contains_window(window_id) {
            return false;
        }
        if self
            .close_request
            .as_mut()
            .is_some_and(|callback| !callback(window_id))
        {
            return false;
        }
        self.close_window(window_id)
    }

    pub fn close_window(&mut self, window_id: WindowId) -> bool {
        self.fail_simulation_window(window_id);
        self.fail_native_requests_for_window(window_id, PlatformOperationError::stale_resource());
        self.fail_file_dialog_requests_for_window(window_id, FileDialogError::ParentClosed);
        let record = self.registry.borrow_mut().close(window_id);
        let Some(mut record) = record else {
            self.registry.borrow_mut().stale_window_commands += 1;
            return false;
        };
        record
            .transient_presentations
            .write()
            .expect("transient presentation snapshot lock")
            .clear();
        record.scope.cancel();
        record.runtime.dispose_window();
        self.native_commands
            .borrow_mut()
            .push_back(NativeWindowCommand::Operate(WindowCommand::new(
                window_id,
                WindowOperation::Close,
            )));
        // Explicit user close removes the auxiliary descriptor and its
        // window-scoped values; shutdown bypasses this method and therefore
        // preserves active descriptors. Not-yet-restored descriptors for other
        // windows are retained.
        let closed_restoration = record.restoration.clone();
        if let (Some(metadata), Some(restoration)) = (&closed_restoration, &self.restoration) {
            restoration.remove_window_descriptor(metadata.id.as_key().as_str());
            restoration.remove_scope(&[
                RestorationKey::new("window").expect("static key"),
                metadata.id.as_key().clone(),
            ]);
        }
        self.manager.sync_restorable_windows(true);
        if self.registry.borrow().visible_count() == 0
            && self.last_window_policy == LastWindowPolicy::ExitOnLastWindow
        {
            self.should_exit = true;
        }
        true
    }

    fn drain_window_commands(&mut self) {
        while let Ok(queued) = self.command_receiver.try_recv() {
            let QueuedWindowCommand { command, request } = queued;
            let window_id = command.window_id;
            match command.operation.clone() {
                WindowOperation::Close => {
                    if let Some(request) = request {
                        Self::resolve_queued_request(
                            request,
                            Err(PlatformOperationError::with_context(
                                PlatformOperationErrorKind::InvalidState,
                                "window close does not expose a native completion request",
                            )),
                        );
                    }
                    let _ = self.close_window(window_id);
                }
                operation => {
                    let applied = self.with_window_mut(window_id, |record| {
                        match &operation {
                            WindowOperation::SetTitle(title) => {
                                record.options.title = title.clone()
                            }
                            WindowOperation::SetVisible(visible) => {
                                record.options.visible = *visible;
                                record.requested_state.visible = *visible;
                                record.lifecycle = if *visible {
                                    WindowLifecycle::Visible
                                } else {
                                    WindowLifecycle::Hidden
                                };
                            }
                            WindowOperation::SetLogicalSize(size) => {
                                if size.width.is_finite()
                                    && size.height.is_finite()
                                    && size.width > 0.0
                                    && size.height > 0.0
                                {
                                    record.content_sizing.reset();
                                    record.options.initial_logical_size = *size;
                                } else {
                                    return false;
                                }
                            }
                            WindowOperation::SetOuterPosition(_) => {}
                            WindowOperation::BeginMoveDrag
                            | WindowOperation::BeginResizeDrag(_) => {}
                            WindowOperation::SetMinimized(minimized) => {
                                record.requested_state.minimized = *minimized;
                            }
                            WindowOperation::SetMaximized(maximized) => {
                                record.options.maximized = *maximized;
                                record.requested_state.maximized = *maximized;
                            }
                            WindowOperation::SetFullscreen(fullscreen) => {
                                record.options.fullscreen = *fullscreen;
                                record.requested_state.fullscreen = *fullscreen;
                            }
                            WindowOperation::SetResizable(resizable) => {
                                record.options.resizable = *resizable;
                                record.requested_state.resizable = *resizable;
                            }
                            WindowOperation::SetDecorations(decorations) => {
                                record.options.decorations = *decorations;
                                record.requested_state.decorations = *decorations;
                            }
                            WindowOperation::SetLogicalSizeLimits(limits) => {
                                record.options.minimum_logical_size = limits.minimum();
                                record.options.maximum_logical_size = limits.maximum();
                                record.requested_state.size_limits = *limits;
                                record.content_sizing.reset();
                            }
                            WindowOperation::SetWindowLevel(level) => {
                                record.options.window_level = *level;
                                record.requested_state.level = *level;
                            }
                            WindowOperation::SetWindowIcon(icon) => {
                                record.options.window_icon = icon.clone();
                            }
                            WindowOperation::RequestUserAttention(_) => {}
                            WindowOperation::SetCursorGrab(_)
                            | WindowOperation::SetCursorVisible(_)
                            | WindowOperation::SetCursorPosition(_) => {}
                            WindowOperation::SetContentSensitivity(sensitivity) => {
                                record.last_content_sensitivity = Some(*sensitivity);
                            }
                            WindowOperation::RequestFocus => {}
                            WindowOperation::RequestRedraw => record.runtime.request_frame(),
                            WindowOperation::Close => unreachable!(),
                        }
                        true
                    });
                    if applied == Some(true) {
                        if let Some(request) = request {
                            let request_id = command
                                .request_id
                                .expect("result-bearing command has request id");
                            if !request.cancelled.load(Ordering::Acquire) {
                                self.pending_native_requests.insert(
                                    request_id,
                                    PendingNativeRequest {
                                        window_id,
                                        sender: request.sender,
                                    },
                                );
                            }
                        } else {
                            debug_assert!(command.request_id.is_none());
                        }
                        self.native_commands
                            .borrow_mut()
                            .push_back(NativeWindowCommand::Operate(command));
                        if matches!(operation, WindowOperation::SetVisible(false))
                            && self.registry.borrow().visible_count() == 0
                            && self.last_window_policy == LastWindowPolicy::ExitOnLastWindow
                        {
                            self.should_exit = true;
                        }
                        self.manager.sync_restorable_windows(true);
                    } else if applied == Some(false) {
                        if let Some(request) = request {
                            Self::resolve_queued_request(
                                request,
                                Err(PlatformOperationError::with_context(
                                    PlatformOperationErrorKind::InvalidState,
                                    "window operation arguments were rejected by the runtime",
                                )),
                            );
                        }
                    } else if applied.is_none() {
                        if let Some(request) = request {
                            Self::resolve_queued_request(
                                request,
                                Err(PlatformOperationError::stale_resource()),
                            );
                        }
                        self.registry.borrow_mut().stale_window_commands += 1;
                    }
                }
            }
        }
    }

    fn resolve_queued_request(request: QueuedNativeRequest, result: PlatformOperationResult) {
        if !request.cancelled.load(Ordering::Acquire) {
            let _ = request.sender.send(result);
        }
    }

    fn drain_native_request_cancellations(&mut self) {
        while let Ok(request_id) = self.request_cancellation_receiver.try_recv() {
            self.pending_native_requests.remove(&request_id);
        }
    }

    fn fail_native_requests_for_window(
        &mut self,
        window_id: WindowId,
        error: PlatformOperationError,
    ) {
        let request_ids = self
            .pending_native_requests
            .iter()
            .filter_map(|(request_id, request)| {
                (request.window_id == window_id).then_some(*request_id)
            })
            .collect::<Vec<_>>();
        for request_id in request_ids {
            if let Some(request) = self.pending_native_requests.remove(&request_id) {
                let _ = request.sender.send(Err(error.clone()));
            }
        }
    }

    fn fail_all_native_requests(&mut self, error: PlatformOperationError) {
        while self.request_cancellation_receiver.try_recv().is_ok() {}
        for (_, request) in self.pending_native_requests.drain() {
            let _ = request.sender.send(Err(error.clone()));
        }
        while let Ok(queued) = self.command_receiver.try_recv() {
            if let Some(request) = queued.request {
                Self::resolve_queued_request(request, Err(error.clone()));
            }
        }
    }

    fn drain_file_dialog_requests(&mut self) {
        while let Ok(queued) = self.file_dialog_receiver.try_recv() {
            if queued.cancelled.load(std::sync::atomic::Ordering::Acquire) {
                continue;
            }
            if !self.contains_window(queued.window_id) {
                let _ = queued.sender.send(Err(FileDialogError::ParentClosed));
                continue;
            }
            let capabilities = self
                .window_capabilities(queued.window_id)
                .expect("live file-dialog parent has capabilities")
                .application_services
                .file_dialogs;
            if let Some(error) = explicitly_unsupported_request(capabilities, &queued.request) {
                let _ = queued.sender.send(Err(error));
                continue;
            }
            let request_id = queued.request_id;
            let window_id = queued.window_id;
            self.pending_file_dialog_requests.insert(
                request_id,
                PendingFileDialogRequest {
                    window_id,
                    request: queued.request,
                    sender: Some(queued.sender),
                    cancelled: queued.cancelled,
                },
            );
            if self.active_file_dialog_requests.contains_key(&window_id) {
                self.queued_file_dialog_requests
                    .entry(window_id)
                    .or_default()
                    .push_back(request_id);
            } else {
                self.activate_file_dialog(request_id);
            }
        }
    }

    fn activate_file_dialog(&mut self, request_id: FileDialogRequestId) {
        let Some(pending) = self.pending_file_dialog_requests.get(&request_id) else {
            return;
        };
        if pending.cancelled.load(std::sync::atomic::Ordering::Acquire) {
            let window_id = pending.window_id;
            self.pending_file_dialog_requests.remove(&request_id);
            self.promote_next_file_dialog(window_id);
            return;
        }
        let window_id = pending.window_id;
        debug_assert!(!self.active_file_dialog_requests.contains_key(&window_id));
        self.active_file_dialog_requests
            .insert(window_id, request_id);
        self.native_file_dialog_requests
            .push_back(NativeFileDialogRequest {
                request_id,
                window_id,
                request: pending.request.clone(),
            });
    }

    fn promote_next_file_dialog(&mut self, window_id: WindowId) {
        if self.active_file_dialog_requests.contains_key(&window_id)
            || !self.contains_window(window_id)
        {
            return;
        }
        loop {
            let next = self
                .queued_file_dialog_requests
                .get_mut(&window_id)
                .and_then(VecDeque::pop_front);
            let Some(request_id) = next else {
                self.queued_file_dialog_requests.remove(&window_id);
                return;
            };
            let cancelled = self
                .pending_file_dialog_requests
                .get(&request_id)
                .is_none_or(|pending| pending.cancelled.load(std::sync::atomic::Ordering::Acquire));
            if cancelled {
                self.pending_file_dialog_requests.remove(&request_id);
                continue;
            }
            self.activate_file_dialog(request_id);
            return;
        }
    }

    fn drain_file_dialog_cancellations(&mut self) {
        while let Ok(request_id) = self.file_dialog_cancellation_receiver.try_recv() {
            let Some(window_id) = self
                .pending_file_dialog_requests
                .get(&request_id)
                .map(|pending| pending.window_id)
            else {
                continue;
            };
            let is_active = self.active_file_dialog_requests.get(&window_id) == Some(&request_id);
            if is_active {
                // If the desktop host has not taken this request yet, cancellation
                // can prevent native presentation and the FIFO may advance.
                let not_started = self.pending_file_dialog_requests.phase(&request_id)
                    == Some(RequestPhase::Queued);
                self.native_file_dialog_requests
                    .retain(|request| request.request_id != request_id);
                if not_started {
                    self.active_file_dialog_requests.remove(&window_id);
                    self.pending_file_dialog_requests.remove(&request_id);
                    self.promote_next_file_dialog(window_id);
                } else if let Some(pending) = self.pending_file_dialog_requests.get_mut(&request_id)
                {
                    // The native modal operation already owns the slot. Detach
                    // result delivery, but keep serialization until completion.
                    pending.sender = None;
                    self.pending_file_dialog_requests.abandon(&request_id);
                }
            } else {
                if let Some(queue) = self.queued_file_dialog_requests.get_mut(&window_id) {
                    queue.retain(|queued| *queued != request_id);
                    if queue.is_empty() {
                        self.queued_file_dialog_requests.remove(&window_id);
                    }
                }
                self.pending_file_dialog_requests.remove(&request_id);
            }
        }
    }

    fn fail_file_dialog_requests_for_window(
        &mut self,
        window_id: WindowId,
        error: FileDialogError,
    ) {
        self.native_file_dialog_requests
            .retain(|request| request.window_id != window_id);
        self.active_file_dialog_requests.remove(&window_id);
        self.queued_file_dialog_requests.remove(&window_id);
        let request_ids = self
            .pending_file_dialog_requests
            .iter()
            .filter_map(|(request_id, pending)| {
                (pending.window_id == window_id).then_some(*request_id)
            })
            .collect::<Vec<_>>();
        for request_id in request_ids {
            if let Some(mut pending) = self.pending_file_dialog_requests.remove(&request_id)
                && !pending.cancelled.load(std::sync::atomic::Ordering::Acquire)
                && let Some(sender) = pending.sender.take()
            {
                let _ = sender.send(Err(error.clone()));
            }
        }
    }

    fn fail_all_file_dialog_requests(&mut self, error: FileDialogError) {
        self.file_dialog_bridge.stop();
        while self.file_dialog_cancellation_receiver.try_recv().is_ok() {}
        while let Ok(queued) = self.file_dialog_receiver.try_recv() {
            if !queued.cancelled.load(std::sync::atomic::Ordering::Acquire) {
                let _ = queued.sender.send(Err(error.clone()));
            }
        }
        for (_, mut pending) in self.pending_file_dialog_requests.drain() {
            if !pending.cancelled.load(std::sync::atomic::Ordering::Acquire)
                && let Some(sender) = pending.sender.take()
            {
                let _ = sender.send(Err(error.clone()));
            }
        }
        self.active_file_dialog_requests.clear();
        self.queued_file_dialog_requests.clear();
        self.native_file_dialog_requests.clear();
    }

    /// Takes native file-dialog work ready to start. At most one request per
    /// parent window is active; later requests for that window remain FIFO in
    /// the runtime until completion or cancellation.
    pub fn take_native_file_dialog_requests(&mut self) -> Vec<NativeFileDialogRequest> {
        self.drain_file_dialog_requests();
        self.drain_file_dialog_cancellations();
        let requests: Vec<_> = self.native_file_dialog_requests.drain(..).collect();
        for request in &requests {
            self.pending_file_dialog_requests
                .dispatch(&request.request_id);
        }
        requests
    }

    /// Completes one active file dialog and promotes the next request for the
    /// same parent. Backends must invoke this only on the application UI/event
    /// loop thread; native dialog objects never cross this boundary.
    pub fn complete_file_dialog(
        &mut self,
        completion: NativeFileDialogCompletion,
    ) -> FileDialogCompletionStatus {
        let Some(pending) = self
            .pending_file_dialog_requests
            .get(&completion.request_id)
        else {
            return FileDialogCompletionStatus::UnknownRequest;
        };
        if pending.window_id != completion.window_id {
            return FileDialogCompletionStatus::TargetMismatch;
        }
        if self.active_file_dialog_requests.get(&completion.window_id)
            != Some(&completion.request_id)
        {
            return FileDialogCompletionStatus::UnknownRequest;
        }

        let Ok(mut pending) = self
            .pending_file_dialog_requests
            .complete(&completion.request_id, |_| true)
        else {
            return FileDialogCompletionStatus::UnknownRequest;
        };
        self.active_file_dialog_requests
            .remove(&completion.window_id);

        let (result, status) = match completion.result {
            Ok(outcome) if result_matches_request(&pending.request, &outcome) => {
                (Ok(outcome), FileDialogCompletionStatus::Completed)
            }
            Ok(_) => (
                Err(FileDialogError::InvalidResult),
                FileDialogCompletionStatus::InvalidResult,
            ),
            Err(error) => (Err(error), FileDialogCompletionStatus::Completed),
        };
        if !pending.cancelled.load(std::sync::atomic::Ordering::Acquire)
            && let Some(sender) = pending.sender.take()
        {
            let _ = sender.send(result);
        }
        self.promote_next_file_dialog(completion.window_id);
        status
    }

    fn drain_global_shortcut_requests(&mut self) {
        drain_queued(
            &self.global_shortcut_receiver,
            &mut self.pending_global_shortcut_requests,
            &mut self.native_global_shortcut_requests,
        );
        let abandoned: Vec<_> = self
            .pending_global_shortcut_requests
            .iter()
            .filter(|(_, pending)| pending.reply.is_abandoned())
            .map(|(id, pending)| (*id, pending.operation))
            .collect();
        for (id, operation) in abandoned {
            if self.pending_global_shortcut_requests.phase(&id) == Some(RequestPhase::Queued) {
                if matches!(operation, NativeGlobalShortcutOperation::Register { .. }) {
                    self.pending_global_shortcut_requests.remove(&id);
                    self.native_global_shortcut_requests
                        .retain(|request| request.request_id != id);
                }
            } else {
                self.pending_global_shortcut_requests.abandon(&id);
            }
        }
    }

    fn fail_all_global_shortcut_requests(&mut self) {
        self.global_shortcut_bridge.stop();
        while let Ok(queued) = self.global_shortcut_receiver.try_recv() {
            queued.reply.finish(
                queued.operation,
                Err(incular_platform::GlobalShortcutError::ApplicationStopped),
            );
        }
        for (_, pending) in self.pending_global_shortcut_requests.drain() {
            pending.reply.finish(
                pending.operation,
                Err(incular_platform::GlobalShortcutError::ApplicationStopped),
            );
        }
        self.native_global_shortcut_requests.clear();
    }

    /// Takes application-scoped global-shortcut work ready for the native UI
    /// thread. Custom desktop embedders may implement this boundary without
    /// exposing native hotkey handles to runtime/application code.
    #[doc(hidden)]
    pub fn take_native_global_shortcut_requests(&mut self) -> Vec<NativeGlobalShortcutRequest> {
        self.drain_global_shortcut_requests();
        let requests: Vec<_> = self.native_global_shortcut_requests.drain(..).collect();
        for request in &requests {
            self.pending_global_shortcut_requests
                .dispatch(&request.request_id);
        }
        requests
    }

    /// Completes one native global-shortcut request. If the requesting future
    /// was abandoned after a registration succeeded, the caller is instructed
    /// to roll that native registration back immediately.
    #[doc(hidden)]
    pub fn complete_global_shortcut_request(
        &mut self,
        completion: NativeGlobalShortcutCompletion,
    ) -> GlobalShortcutCompletionStatus {
        let Ok(pending) = self
            .pending_global_shortcut_requests
            .complete(&completion.request_id, |_| true)
        else {
            return GlobalShortcutCompletionStatus::UnknownRequest;
        };
        pending.reply.finish(pending.operation, completion.result)
    }

    /// Completes one result-bearing native operation. Backends call this only
    /// after applying the operation on the owning native/UI thread.
    pub fn complete_native_operation(
        &mut self,
        completion: NativeOperationCompletion,
    ) -> NativeOperationCompletionStatus {
        let pending = match self
            .pending_native_requests
            .complete(&completion.request_id, |pending| {
                pending.window_id == completion.window_id
            }) {
            Ok(pending) => pending,
            Err(CompletionRejection::TargetMismatch) => {
                return NativeOperationCompletionStatus::TargetMismatch;
            }
            Err(_) => return NativeOperationCompletionStatus::UnknownRequest,
        };
        if !self.contains_window(completion.window_id) {
            let _ = pending
                .sender
                .send(Err(PlatformOperationError::stale_resource()));
            return NativeOperationCompletionStatus::StaleTarget;
        }
        if let Err(error) = &completion.result {
            let error = error.clone();
            let _ = self.with_window_mut(completion.window_id, |record| {
                record.last_platform_error = Some(error);
            });
        }
        let _ = pending.sender.send(completion.result);
        NativeOperationCompletionStatus::Completed
    }

    /// Takes all UI-thread native operations. Adapters must call this only
    /// from their event-loop callback; creation is consequently valid for
    /// Winit 0.30's active-loop restriction.
    pub fn take_native_window_commands(&mut self) -> Vec<NativeWindowCommand> {
        self.drain_window_commands();
        self.drain_native_request_cancellations();
        let commands: Vec<_> = self.native_commands.borrow_mut().drain(..).collect();
        for command in &commands {
            if let NativeWindowCommand::Operate(command) = command
                && let Some(id) = command.request_id
            {
                self.pending_native_requests.dispatch(&id);
            }
        }
        commands
    }

    #[must_use]
    pub const fn should_exit(&self) -> bool {
        self.should_exit
    }

    #[must_use]
    pub fn window_diagnostics(&self, window_id: WindowId) -> Option<WindowDiagnostics> {
        let registry = self.registry.borrow();
        let slot = registry.slots.get(window_id.index() as usize)?;
        if slot.generation != window_id.generation() {
            return None;
        }
        let record = slot.record.as_ref()?;
        Some(WindowDiagnostics {
            id: window_id,
            title: record.options.title.clone(),
            logical_size: record.metrics.logical_size(),
            physical_size: record.metrics.physical_size,
            scale_factor: record.metrics.scale_factor,
            visible: record.options.visible,
            native_focused: record.native_focused,
            lifecycle: record.lifecycle,
            application_lifecycle: record.runtime.lifecycle(),
            frame_requested: record.runtime.frame_requested(),
            requested_frames: record.requested_frames,
            presented_frames: record.presented_frames,
            skipped_frames: record.skipped_frames,
            input_events: record.input_events,
            surface_generation: record.surface_generation,
            content_sensitivity: record.runtime.content_sensitivity(),
            elements: record.runtime.tree().element_count(),
            render_objects: record.runtime.tree().render_object_count(),
            semantics: record.runtime.tree().semantics().len(),
            accessibility: record.accessibility,
            capabilities: *record
                .capabilities
                .read()
                .expect("window capability snapshot lock"),
            last_platform_error: record.last_platform_error.clone(),
            requested_state: record.requested_state,
            observed_state: *record
                .observed_state
                .read()
                .expect("window observed-state snapshot lock"),
            transient_presentations: record
                .transient_presentations
                .read()
                .expect("transient presentation snapshot lock")
                .clone(),
        })
    }

    #[must_use]
    pub fn diagnostics(&self) -> ApplicationDiagnostics {
        let registry = self.registry.borrow();
        ApplicationDiagnostics {
            windows_created: registry.windows_created,
            windows_closed: registry.windows_closed,
            active_windows: registry.ids().len(),
            stale_window_commands: registry.stale_window_commands,
            pending_native_requests: self.pending_native_requests.len(),
            pending_file_dialog_requests: self.pending_file_dialog_requests.len(),
        }
    }

    /// Returns value-free health and persistence-coalescing data when the
    /// application opted into restoration.
    #[must_use]
    pub fn restoration_diagnostics(&self) -> Option<RestorationDiagnostics> {
        self.restoration
            .as_ref()
            .map(restoration::RestorationManager::diagnostics)
    }

    /// Returns the configured snapshot path for developer tooling. In-memory
    /// and custom stores intentionally have no filesystem location.
    #[must_use]
    pub fn restoration_path(&self) -> Option<std::path::PathBuf> {
        self.restoration
            .as_ref()
            .and_then(restoration::RestorationManager::store_location)
    }

    /// Removes every opt-in persisted value and every restorable-window
    /// descriptor. The deletion is coalesced through the normal Tokio-backed
    /// persistence pipeline; call [`Self::flush_restoration`] before an
    /// immediate process handoff if required.
    pub fn reset_restoration(&mut self) -> bool {
        let Some(restoration) = &self.restoration else {
            return false;
        };
        restoration.reset();
        true
    }

    /// Bypasses the normal 250ms debounce but still writes on Tokio's blocking
    /// pool. This is useful for an explicit user action or lifecycle flush.
    pub fn flush_restoration(&mut self) -> bool {
        let Some(restoration) = &self.restoration else {
            return false;
        };
        restoration.flush();
        true
    }

    /// Flushes restoration and bounded-waits for the blocking save to settle,
    /// mirroring the shutdown handoff. Used when the host may freeze or kill
    /// the process after the lifecycle event (for example mobile suspend).
    fn flush_restoration_and_wait(&mut self) {
        let Some(restoration) = self.restoration.clone() else {
            return;
        };
        restoration.flush();
        let deadline = Instant::now() + std::time::Duration::from_millis(100);
        while !restoration.is_clean() && Instant::now() < deadline {
            self.process_runtime_work();
            std::thread::sleep(std::time::Duration::from_millis(1));
        }
    }

    #[must_use]
    pub fn debug_dump(&self) -> String {
        let mut lines = vec!["Application".to_owned()];
        if let Some(restoration) = &self.restoration {
            lines.push(restoration.debug_dump());
        }
        for id in self.active_window_ids() {
            if let Some(window) = self.window_diagnostics(id) {
                lines.push(format!(
                    "  {id} title={:?} {:.0}x{:.0} @{} focused={}\\n    root elements={} render={} semantics={} dirty={}",
                    window.title,
                    window.logical_size.width,
                    window.logical_size.height,
                    window.scale_factor,
                    window.native_focused,
                    window.elements,
                    window.render_objects,
                    window.semantics,
                    window.frame_requested,
                ));
            }
        }
        lines.join("\\n")
    }

    pub fn shutdown(&mut self) {
        self.flush_restoration_before_shutdown();
        self.application_shell.stop();
        self.manager.bridge.stop();
        self.fail_all_file_dialog_requests(FileDialogError::ApplicationStopped);
        self.fail_all_global_shortcut_requests();
        self.fail_all_native_requests(PlatformOperationError::with_context(
            PlatformOperationErrorKind::Unavailable,
            "application runtime stopped before native completion",
        ));
        let ids = self.active_window_ids();
        for id in ids {
            self.fail_simulation_window(id);
            if let Some(mut record) = self.registry.borrow_mut().close(id) {
                record.scope.cancel();
                record.runtime.dispose_window();
            }
        }
        self.native_commands.borrow_mut().clear();
        self.scheduler.borrow_mut().shutdown();
        self.stop_simulation();
        self.should_exit = true;
    }

    fn flush_restoration_before_shutdown(&mut self) {
        self.flush_restoration_and_wait();
    }

    /// Compatibility escape hatch for existing single-window embedders and
    /// tests. Extra windows are cancelled; desktop `run` retains all windows.
    /// Pending restoration state is flushed with the same bounded wait as
    /// shutdown so the extracted runtime does not silently drop it.
    #[must_use]
    pub fn into_runtime(mut self) -> Runtime {
        self.flush_restoration_and_wait();
        self.manager.bridge.stop();
        self.fail_all_file_dialog_requests(FileDialogError::ApplicationStopped);
        self.fail_all_global_shortcut_requests();
        let primary = self.primary_window;
        let mut primary_record = self
            .registry
            .borrow_mut()
            .close(primary)
            .expect("application primary window exists");
        for id in self.active_window_ids() {
            if let Some(record) = self.registry.borrow_mut().close(id) {
                record.scope.cancel();
            }
        }
        primary_record.runtime.window_manager = None;
        primary_record.runtime.window_id = None;
        primary_record.runtime
    }
}
/// Widget key that marks the performance-overlay mount point.
pub const PERFORMANCE_OVERLAY_KEY: &str = incular_widgets::internal::PERFORMANCE_OVERLAY_KEY;

/// Builds the repaint-contained overlay visual from one snapshot.
fn overlay_widget(snapshot: &PerformanceSnapshot) -> incular_widgets::Widget {
    use incular_widgets::{DecoratedBox, Padding, Text, Widget};
    let lines = snapshot.overlay_lines();
    let monospace = || {
        incular_widgets::TextStyle::default()
            .family(incular_text::FontFamily::Monospace)
            .font_size(11.)
            .color(incular_core::Color::rgba(190, 220, 255, 255))
    };
    let rows: Vec<Widget> = lines
        .into_iter()
        .map(|line| Widget::from(Text::new(line).style(monospace())))
        .collect();
    // The overlay is diagnostic chrome, not an application control. Keep the
    // visual mounted in the tree while allowing the widgets underneath it to
    // receive pointer events, just like Flutter's non-interactive performance
    // overlay.
    Widget::from(
        incular_widgets::IgnorePointer::new(Widget::from(incular_widgets::RepaintBoundary::new(
            Widget::from(
                DecoratedBox::new(Padding::all(
                    6.,
                    Widget::from(incular_widgets::Column::new(rows)),
                ))
                .background(incular_core::Color::rgba(12, 14, 18, 216))
                .radius(4.)
                .border(incular_rendering::Border {
                    color: incular_core::Color::rgba(120, 170, 245, 90),
                    width: 1.,
                }),
            ),
        )))
        .ignoring(true),
    )
}