waft-widgets 0.6.0

Widget framework and widgets for Waft
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
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
//! Flutter counterpart: `widgets/focus_manager.dart`.
//!
//! The focus tree: [`FocusNode`] and [`FocusScopeNode`] (one arena struct per Dart leaf,
//! joined by the type-erased handle [`AnyFocusNode`]), the [`FocusAttachment`] a host widget uses to
//! keep a node in the tree, and the [`FocusManager`] the `BuildOwner` holds.

use std::fmt;
use std::rc::Rc;

use waft_embedder::{AppLifecycleState, Offset, PointerDeviceKind, Rect, Size, TargetPlatform};
use waft_foundation::{
    App, ChangeNotifier, ChangeNotifierData, Handle, HandleId, K_IS_WEB, ListenableHandle,
    Listener, OptionRetained, Ref, Retained, ValueChanged,
};
use waft_gestures::{GestureBinding, PointerEvent, PointerRoute};
use waft_rendering::RendererBinding;
use waft_scheduler::{SchedulerBinding, SchedulerPhase};
use waft_services::{HardwareKeyboard, KeyEvent, KeyEventCallback};

use crate::binding::{WidgetsBinding, WidgetsBindingObserver, WidgetsBindingObserverRef};
use crate::framework::BuildContext;
use crate::widgets::focus_traversal::{FocusTraversalGroup, TraversalDirection};

/// An enum that describes how to handle a key event handled by a
/// [`FocusOnKeyEventCallback`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyEventResult {
    /// The key event has been handled, and the event should not be propagated to other key
    /// event handlers.
    Handled,

    /// The key event has not been handled, and the event should continue to be propagated to
    /// other key event handlers, even non-Flutter ones.
    Ignored,

    /// The key event has not been handled, but the key event should not be propagated to
    /// other key event handlers.
    ///
    /// It will be returned to the platform embedding to be propagated to text fields and
    /// non-Flutter key event handlers on the platform.
    SkipRemainingHandlers,
}

/// Combine the results returned by multiple [`FocusOnKeyEventCallback`]s.
///
/// If any callback returns [`KeyEventResult::Handled`], the node considers the message
/// handled; otherwise, if any callback returns [`KeyEventResult::SkipRemainingHandlers`], the
/// node skips the remaining handlers without preventing the platform to handle; otherwise the
/// node is ignored.
pub fn combine_key_event_results(
    results: impl IntoIterator<Item = KeyEventResult>,
) -> KeyEventResult {
    let mut has_skip_remaining_handlers = false;
    for result in results {
        match result {
            KeyEventResult::Handled => return KeyEventResult::Handled,
            KeyEventResult::SkipRemainingHandlers => has_skip_remaining_handlers = true,
            KeyEventResult::Ignored => {}
        }
    }
    if has_skip_remaining_handlers {
        KeyEventResult::SkipRemainingHandlers
    } else {
        KeyEventResult::Ignored
    }
}

/// Signature of a callback used by `Focus::on_key_event` and `FocusScope::on_key_event` to
/// receive key events.
///
/// The `node` is the node that received the event.
///
/// Returns a [`KeyEventResult`] that describes how, and whether, the key event was handled.
pub type FocusOnKeyEventCallback = Rc<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult>;

/// Signature of a callback used by [`FocusManager::add_early_key_event_handler`] and
/// [`FocusManager::add_late_key_event_handler`].
///
/// The `event` parameter is a [`KeyEvent`] that is being sent to the callback to be handled.
///
/// The [`KeyEventResult`] return value indicates whether or not the event will continue to be
/// propagated. If the value returned is [`KeyEventResult::Handled`] or
/// [`KeyEventResult::SkipRemainingHandlers`], then the event will not continue to be
/// propagated.
pub type OnKeyEventCallback = Rc<dyn Fn(&mut App, &KeyEvent) -> KeyEventResult>;

/// Signature of a closure registered with [`FocusManager::add_highlight_mode_listener`].
pub type HighlightModeListener = Rc<dyn Fn(&mut App, FocusHighlightMode)>;

/// Represents a pending autofocus request.
struct Autofocus {
    scope: Ref<FocusScopeNode>,
    autofocus_node: FocusNodeRef,
}

impl Autofocus {
    /// Applies the autofocus request, if the node is still attached to the original scope and
    /// the scope has no focused child.
    ///
    /// The widget tree is responsible for calling reparent/detach on attached nodes to keep
    /// their parent/manager information up-to-date, so here we can safely check if the
    /// scope/node involved in each autofocus request is still attached, and discard the ones
    /// which are no longer attached to the original manager.
    fn apply_if_valid(self, app: &mut App, manager: Handle<FocusManager>) {
        let scope = self.scope.as_node();
        let should_apply = (scope.parent(app).is_some() || self.scope == *manager.root_scope(app))
            && scope.manager(app) == Some(manager)
            && self.scope.focused_child(app).is_none()
            && self.autofocus_node.ancestors(app).contains(&scope);
        if should_apply {
            self.autofocus_node.do_request_focus(app, true);
        }
    }
}

/// An attachment point for a [`FocusNode`].
///
/// Using a [`FocusAttachment`] is rarely needed, unless building something akin to the
/// `Focus` or `FocusScope` widgets from scratch.
///
/// Once created, a [`FocusNode`] must be attached to the widget tree by its _host_
/// `StatefulWidget` via a [`FocusAttachment`] object. [`FocusAttachment`]s are owned by the
/// `StatefulWidget` that hosts a [`FocusNode`] or [`FocusScopeNode`]. There can be multiple
/// [`FocusAttachment`]s for each [`FocusNode`], but the node will only ever be attached to one
/// of them at a time.
///
/// This attachment is created by calling [`AnyFocusNode::attach`], usually from the host
/// widget's `State::init_state` method. If the widget is updated to have a different focus
/// node, then the new node needs to be attached in `State::did_update_widget`, after calling
/// [`detach`](Self::detach) on the previous [`FocusAttachment`]. Once detached, the attachment
/// is defunct and will no longer make changes to the [`FocusNode`] through
/// [`reparent`](Self::reparent).
///
/// Without these attachment points, it would be possible for a focus node to simultaneously be
/// attached to more than one part of the widget tree during the build stage.
pub struct FocusAttachment {
    /// The focus node that this attachment manages an attachment for. The node may not yet
    /// have a parent, or may have been detached from this attachment, so don't count on this
    /// node being in a usable state.
    node: AnyFocusNode,
}

impl FocusAttachment {
    /// A private constructor, because [`FocusAttachment`]s are only to be created by
    /// [`AnyFocusNode::attach`].
    fn new(app: &mut App, node: AnyFocusNode) -> Ref<FocusAttachment> {
        app.create(FocusAttachment { node })
    }

    /// Returns true if the associated node is attached to this attachment.
    ///
    /// It is possible to be attached to the widget tree, but not be placed in the focus tree
    /// (i.e. to not have a parent yet in the focus tree).
    pub fn is_attached(self: Handle<Self>, app: &App) -> bool {
        let node = app.get(self).node;
        node.data(app).attachment.handle() == Some(self)
    }

    /// Detaches the [`FocusNode`] this attachment point is associated with from the focus
    /// tree, and disconnects it from this attachment point.
    ///
    /// Calling [`AnyFocusNode::dispose`] will also automatically detach the node.
    pub fn detach(self: Handle<Self>, app: &mut App) {
        let node = app.get(self).node;
        if self.is_attached(app) {
            let marked_for_focus = node
                .manager(app)
                .map(|manager| app.get(manager).marked_for_focus.handle());
            if node.has_primary_focus(app) || marked_for_focus == Some(Some(node)) {
                node.unfocus(app, UnfocusDisposition::PreviouslyFocusedChild);
            }
            // This node is no longer in the tree, so shouldn't send notifications anymore.
            if let Some(manager) = node.manager(app) {
                manager.mark_detached(app, node);
            }
            if let Some(parent) = node.data(app).parent {
                parent.remove_child(app, node, true);
            }
            node.data_mut(app).attachment = None;
            debug_assert!(
                !node.has_primary_focus(app),
                "Node {node:?} still has primary focus while being detached."
            );
            debug_assert!(
                node.manager(app)
                    .is_none_or(|manager| app.get(manager).marked_for_focus.handle() != Some(node)),
                "Node {node:?} still marked for focus while being detached."
            );
        }
        debug_assert!(!self.is_attached(app));
    }

    /// Ensures that the [`FocusNode`] attached at this attachment point has the proper parent
    /// node, changing it if necessary.
    ///
    /// If given, ensures that the given `parent` node is the parent of the node that is
    /// attached at this attachment point, changing it if necessary. However, it is usually not
    /// necessary to supply an explicit parent, since [`reparent`](Self::reparent) will use
    /// `Focus::maybe_of` to determine the correct parent node for the context given in
    /// [`AnyFocusNode::attach`].
    ///
    /// If [`is_attached`](Self::is_attached) is false, then calling this method does nothing.
    ///
    /// Should be called whenever the associated widget is rebuilt in order to maintain the
    /// focus hierarchy.
    ///
    /// A `StatefulWidget` that hosts a [`FocusNode`] should call this method on the node it
    /// hosts during its `State::build` or `State::did_change_dependencies` methods in case the
    /// widget is moved from one location in the tree to another location that has a different
    /// `FocusScope` or context.
    ///
    /// The optional `parent` argument must be supplied when not using `Focus` and `FocusScope`
    /// widgets to build the focus tree, or if there is a need to supply the parent explicitly
    /// (which are both uncommon).
    pub fn reparent(self: Handle<Self>, app: &mut App, parent: Option<AnyFocusNode>) {
        if self.is_attached(app) {
            let node = app.get(self).node;
            let context = node.data(app).context;
            debug_assert!(context.is_some());
            let context = context.expect("an attached node has a context");
            let parent = parent
                .or_else(|| crate::widgets::focus_scope::Focus::maybe_of(app, context, true, true))
                .or_else(|| {
                    context
                        .owner(app)
                        .map(|owner| owner.focus_manager(app))
                        .map(|manager| manager.root_scope(app).as_node())
                })
                .expect("the element's build owner has a focus manager");
            parent.reparent_child(app, node);
        }
    }
}

/// Describe what should happen after [`AnyFocusNode::unfocus`] is called.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum UnfocusDisposition {
    /// Focus the nearest focusable enclosing scope of this node, but do not descend to locate
    /// the leaf [`FocusScopeNode::focused_child`] the way
    /// [`PreviouslyFocusedChild`](Self::PreviouslyFocusedChild) does.
    ///
    /// Focusing the scope in this way clears the [`FocusScopeNode::focused_child`] history for
    /// the enclosing scope when it receives focus. Because of this, calling a traversal method
    /// like `FocusNode.nextFocus` after unfocusing will cause the `FocusTraversalPolicy` to
    /// pick the node it thinks should be first in the scope.
    ///
    /// This is the default disposition for [`AnyFocusNode::unfocus`].
    #[default]
    Scope,

    /// Focus the previously focused child of the nearest focusable enclosing scope of this
    /// node.
    ///
    /// If there is no previously focused child, then this is equivalent to using the
    /// [`Scope`](Self::Scope) disposition.
    ///
    /// Unfocusing with this disposition will cause [`AnyFocusNode::unfocus`] to walk up the
    /// tree to the nearest focusable enclosing scope, then start to walk down the tree, looking
    /// for a focused child at its [`FocusScopeNode::focused_child`].
    ///
    /// If the [`FocusScopeNode::focused_child`] is a scope, then look for its
    /// [`FocusScopeNode::focused_child`], and so on, finding the leaf
    /// [`FocusScopeNode::focused_child`] that is not a scope, or, failing that, a leaf scope
    /// that has no focused child.
    PreviouslyFocusedChild,
}

// ---------------------------------------------------------------------------------------------
// FocusNode

/// The fields Dart's `FocusNode` declares; every focus node leaf carries this bag under the
/// field `focus_node`.
pub struct FocusNodeData {
    skip_traversal: bool,
    can_request_focus: bool,
    descendants_are_focusable: bool,
    descendants_are_traversable: bool,
    context: Option<BuildContext>,
    on_key_event: Option<FocusOnKeyEventCallback>,
    manager: Option<Handle<FocusManager>>,
    ancestors: Option<Vec<AnyFocusNode>>,
    descendants: Option<Vec<FocusNodeRef>>,
    has_keyboard_token: bool,
    parent: Option<AnyFocusNode>,
    children: Vec<FocusNodeRef>,
    debug_label: Option<String>,
    attachment: Option<Ref<FocusAttachment>>,
    enclosing_scope: Option<Handle<FocusScopeNode>>,
    request_focus_when_reparented: bool,
}

impl FocusNodeData {
    /// The bag of a freshly created node: Dart's constructor defaults.
    pub fn new() -> FocusNodeData {
        FocusNodeData {
            skip_traversal: false,
            can_request_focus: true,
            descendants_are_focusable: true,
            descendants_are_traversable: true,
            context: None,
            on_key_event: None,
            manager: None,
            ancestors: None,
            descendants: None,
            has_keyboard_token: false,
            parent: None,
            children: Vec::new(),
            debug_label: None,
            attachment: None,
            enclosing_scope: None,
            request_focus_when_reparented: false,
        }
    }
}

impl Default for FocusNodeData {
    fn default() -> FocusNodeData {
        FocusNodeData::new()
    }
}

/// The accessors [`FocusNodeLeaf`] asks for, for a struct whose bag is the field `focus_node`.
#[macro_export]
macro_rules! focus_node_accessors {
    () => {
        fn focus_node_data(
            self: ::waft_foundation::Handle<Self>,
            app: &::waft_foundation::App,
        ) -> &$crate::FocusNodeData {
            &app.get(self).focus_node
        }

        fn focus_node_data_mut(
            self: ::waft_foundation::Handle<Self>,
            app: &mut ::waft_foundation::App,
        ) -> &mut $crate::FocusNodeData {
            &mut app.get_mut(self).focus_node
        }
    };
}

/// A concrete focus node: Dart's `FocusNode` and its one subclass [`FocusScopeNode`].
///
/// The trait holds the members a superclass body dispatches to the leaf ([`nearest_scope`],
/// [`descendants_are_focusable`], [`traversal_children`], [`traversal_descendants`],
/// [`do_request_focus`], and Dart's `node is FocusScopeNode` as [`as_scope`]), plus the
/// [`as_node`] type-erased handle and one-line forwarders for the shared members, which live
/// on [`AnyFocusNode`] because a node reaches its parent and children through that handle.
///
/// The defaults are `FocusNode`'s own bodies; [`FocusScopeNode`] overrides them and calls
/// `FocusNode::…` where Dart writes `super.…`.
///
/// [`nearest_scope`]: Self::nearest_scope
/// [`descendants_are_focusable`]: Self::descendants_are_focusable
/// [`traversal_children`]: Self::traversal_children
/// [`traversal_descendants`]: Self::traversal_descendants
/// [`do_request_focus`]: Self::do_request_focus
/// [`as_scope`]: Self::as_scope
/// [`as_node`]: Self::as_node
pub trait FocusNodeLeaf: ChangeNotifier + Sized + 'static {
    /// Dart's `FocusNode` fields, held under the field `focus_node`
    /// ([`focus_node_accessors!`](crate::focus_node_accessors)).
    fn focus_node_data(self: Handle<Self>, app: &App) -> &FocusNodeData;

    /// See [`focus_node_data`](Self::focus_node_data).
    fn focus_node_data_mut(self: Handle<Self>, app: &mut App) -> &mut FocusNodeData;

    /// This node as the erased [`AnyFocusNode`] — what to pass where a Dart API takes a
    /// `FocusNode`.
    ///
    /// The object is untouched; this mints an erased second handle to it, so concrete members
    /// stay reachable through the typed one.
    fn as_node(self: Handle<Self>) -> AnyFocusNode {
        AnyFocusNode {
            id: self.id(),
            vtable: const { &FocusNodeVTable::of::<Self>() },
        }
    }

    /// Dart's `node is FocusScopeNode`.
    fn as_scope(self: Handle<Self>) -> Option<Handle<FocusScopeNode>> {
        let _ = self;
        None
    }

    /// Returns the nearest enclosing scope node above this node, including this node, if it's
    /// a scope.
    fn nearest_scope(self: Handle<Self>, app: &mut App) -> Option<Handle<FocusScopeNode>> {
        FocusNode::nearest_scope(self.as_node(), app)
    }

    /// If false, will disable focus for all of this node's descendants.
    fn descendants_are_focusable(self: Handle<Self>, app: &App) -> bool {
        FocusNode::descendants_are_focusable(self.as_node(), app)
    }

    /// An iterator over the children that are allowed to be traversed by the
    /// `FocusTraversalPolicy`.
    fn traversal_children(self: Handle<Self>, app: &mut App) -> Vec<AnyFocusNode> {
        FocusNode::traversal_children(self.as_node(), app)
    }

    /// Returns all descendants which do not have the `skip_traversal` and do have the
    /// `can_request_focus` flag set.
    fn traversal_descendants(self: Handle<Self>, app: &mut App) -> Vec<AnyFocusNode> {
        FocusNode::traversal_descendants(self.as_node(), app)
    }

    /// Requests the primary focus for this node; overridden by [`FocusScopeNode`].
    fn do_request_focus(self: Handle<Self>, app: &mut App, find_first_focus: bool) {
        FocusNode::do_request_focus(self.as_node(), app, find_first_focus);
    }

    // ---- one-line forwarders to the type-erased handle ----

    /// See [`AnyFocusNode::skip_traversal`].
    fn skip_traversal(self: Handle<Self>, app: &mut App) -> bool {
        self.as_node().skip_traversal(app)
    }

    /// See [`AnyFocusNode::set_skip_traversal`].
    fn set_skip_traversal(self: Handle<Self>, app: &mut App, value: bool) {
        self.as_node().set_skip_traversal(app, value);
    }

    /// See [`AnyFocusNode::can_request_focus`].
    fn can_request_focus(self: Handle<Self>, app: &mut App) -> bool {
        self.as_node().can_request_focus(app)
    }

    /// See [`AnyFocusNode::set_can_request_focus`].
    fn set_can_request_focus(self: Handle<Self>, app: &mut App, value: bool) {
        self.as_node().set_can_request_focus(app, value);
    }

    /// See [`AnyFocusNode::set_descendants_are_focusable`].
    fn set_descendants_are_focusable(self: Handle<Self>, app: &mut App, value: bool) {
        self.as_node().set_descendants_are_focusable(app, value);
    }

    /// See [`AnyFocusNode::descendants_are_traversable`].
    fn descendants_are_traversable(self: Handle<Self>, app: &App) -> bool {
        self.as_node().descendants_are_traversable(app)
    }

    /// See [`AnyFocusNode::set_descendants_are_traversable`].
    fn set_descendants_are_traversable(self: Handle<Self>, app: &mut App, value: bool) {
        self.as_node().set_descendants_are_traversable(app, value);
    }

    /// See [`AnyFocusNode::context`].
    fn context(self: Handle<Self>, app: &App) -> Option<BuildContext> {
        self.as_node().context(app)
    }

    /// See [`AnyFocusNode::on_key_event`].
    fn on_key_event(self: Handle<Self>, app: &App) -> Option<FocusOnKeyEventCallback> {
        self.as_node().on_key_event(app)
    }

    /// See [`AnyFocusNode::set_on_key_event`].
    fn set_on_key_event(self: Handle<Self>, app: &mut App, value: Option<FocusOnKeyEventCallback>) {
        self.as_node().set_on_key_event(app, value);
    }

    /// See [`AnyFocusNode::debug_label`].
    fn debug_label(self: Handle<Self>, app: &App) -> Option<String> {
        self.as_node().debug_label(app)
    }

    /// See [`AnyFocusNode::set_debug_label`].
    fn set_debug_label(self: Handle<Self>, app: &mut App, value: Option<String>) {
        self.as_node().set_debug_label(app, value);
    }

    /// See [`AnyFocusNode::parent`].
    fn parent(self: Handle<Self>, app: &App) -> Option<AnyFocusNode> {
        self.as_node().parent(app)
    }

    /// See [`AnyFocusNode::children`].
    fn children(self: Handle<Self>, app: &App) -> Vec<AnyFocusNode> {
        self.as_node().children(app)
    }

    /// See [`AnyFocusNode::descendants`].
    fn descendants(self: Handle<Self>, app: &mut App) -> Vec<AnyFocusNode> {
        self.as_node().descendants(app)
    }

    /// See [`AnyFocusNode::ancestors`].
    fn ancestors(self: Handle<Self>, app: &mut App) -> Vec<AnyFocusNode> {
        self.as_node().ancestors(app)
    }

    /// See [`AnyFocusNode::has_focus`].
    fn has_focus(self: Handle<Self>, app: &mut App) -> bool {
        self.as_node().has_focus(app)
    }

    /// See [`AnyFocusNode::has_primary_focus`].
    fn has_primary_focus(self: Handle<Self>, app: &App) -> bool {
        self.as_node().has_primary_focus(app)
    }

    /// See [`AnyFocusNode::highlight_mode`].
    fn highlight_mode(self: Handle<Self>, app: &mut App) -> FocusHighlightMode {
        self.as_node().highlight_mode(app)
    }

    /// See [`AnyFocusNode::enclosing_scope`].
    fn enclosing_scope(self: Handle<Self>, app: &mut App) -> Option<Handle<FocusScopeNode>> {
        self.as_node().enclosing_scope(app)
    }

    /// See [`AnyFocusNode::size`].
    fn size(self: Handle<Self>, app: &App) -> Size {
        self.as_node().size(app)
    }

    /// See [`AnyFocusNode::offset`].
    fn offset(self: Handle<Self>, app: &App) -> Offset {
        self.as_node().offset(app)
    }

    /// See [`AnyFocusNode::rect`].
    fn rect(self: Handle<Self>, app: &App) -> Rect {
        self.as_node().rect(app)
    }

    /// See [`AnyFocusNode::unfocus`].
    fn unfocus(self: Handle<Self>, app: &mut App, disposition: UnfocusDisposition) {
        self.as_node().unfocus(app, disposition);
    }

    /// See [`AnyFocusNode::consume_keyboard_token`].
    fn consume_keyboard_token(self: Handle<Self>, app: &mut App) -> bool {
        self.as_node().consume_keyboard_token(app)
    }

    /// See [`AnyFocusNode::attach`].
    fn attach(
        self: Handle<Self>,
        app: &mut App,
        context: Option<BuildContext>,
        on_key_event: Option<FocusOnKeyEventCallback>,
    ) -> Ref<FocusAttachment> {
        self.as_node().attach(app, context, on_key_event)
    }

    /// See [`AnyFocusNode::dispose`].
    fn dispose(self: Handle<Self>, app: &mut App) {
        self.as_node().dispose(app);
    }

    /// See [`AnyFocusNode::request_focus`].
    fn request_focus(self: Handle<Self>, app: &mut App, node: Option<AnyFocusNode>) {
        self.as_node().request_focus(app, node);
    }
}

/// The vtable of an erased [`AnyFocusNode`]: one `&'static` table per leaf type, built by
/// [`FocusNodeVTable::of`].
///
/// It lives outside the arena — `&'static` — because a virtual hands `&mut App` onward.
struct FocusNodeVTable {
    type_name: fn() -> &'static str,
    data: fn(&App, HandleId) -> &FocusNodeData,
    data_mut: fn(&mut App, HandleId) -> &mut FocusNodeData,
    as_scope: fn(HandleId) -> Option<Handle<FocusScopeNode>>,
    nearest_scope: fn(&mut App, HandleId) -> Option<Handle<FocusScopeNode>>,
    descendants_are_focusable: fn(&App, HandleId) -> bool,
    traversal_children: fn(&mut App, HandleId) -> Vec<AnyFocusNode>,
    traversal_descendants: fn(&mut App, HandleId) -> Vec<AnyFocusNode>,
    do_request_focus: fn(&mut App, HandleId, bool),
    add_listener: fn(&mut App, HandleId, Listener),
    remove_listener: fn(&mut App, HandleId, &Listener),
    notify_listeners: fn(&mut App, HandleId),
    dispose_notifier: fn(&mut App, HandleId),
}

/// The typed handle for an erased id. Free: nothing is looked up; `get` checks the slot.
fn resolve<L: 'static>(id: HandleId) -> Handle<L> {
    Handle::from_id(id)
}

impl FocusNodeVTable {
    /// The table for one leaf type.
    const fn of<L: FocusNodeLeaf>() -> FocusNodeVTable {
        FocusNodeVTable {
            type_name: std::any::type_name::<L>,
            data: |app, id| L::focus_node_data(resolve(id), app),
            data_mut: |app, id| L::focus_node_data_mut(resolve(id), app),
            as_scope: |id| L::as_scope(resolve(id)),
            nearest_scope: |app, id| L::nearest_scope(resolve(id), app),
            descendants_are_focusable: |app, id| L::descendants_are_focusable(resolve(id), app),
            traversal_children: |app, id| L::traversal_children(resolve(id), app),
            traversal_descendants: |app, id| L::traversal_descendants(resolve(id), app),
            do_request_focus: |app, id, find_first_focus| {
                L::do_request_focus(resolve(id), app, find_first_focus)
            },
            add_listener: |app, id, listener| {
                app.get_mut(resolve::<L>(id))
                    .change_notifier_data_mut()
                    .add_listener(listener)
            },
            remove_listener: |app, id, listener| {
                app.get_mut(resolve::<L>(id))
                    .change_notifier_data_mut()
                    .remove_listener(listener)
            },
            notify_listeners: |app, id| resolve::<L>(id).notify_listeners(app),
            dispose_notifier: |app, id| ChangeNotifierData::dispose(app, resolve::<L>(id)),
        }
    }
}

/// Erased `FocusNode`: one identity and a static vtable, the fat pointer rustc cannot build
/// for an arena id. No lease.
///
/// This is what a field or parameter Dart types as `FocusNode` becomes; the leaf stays in the
/// [`App`] under its own type, and [`downcast`](Self::downcast) gets the typed handle back.
/// The shared `FocusNode` members live here because a node reaches its parent, children, and
/// the primary focus through this handle.
///
/// Equality is Dart's `==` on an object reference: two handles are equal exactly when they
/// address the same node.
#[derive(Clone, Copy)]
pub struct AnyFocusNode {
    id: HandleId,
    vtable: &'static FocusNodeVTable,
}

/// A field Dart types as the erased object, kept: AnyFocusNode with its count.
pub type FocusNodeRef = Retained<AnyFocusNode>;

waft_foundation::erased_handle!(AnyFocusNode: FocusNodeLeaf => FocusNodeLeaf::as_node);

impl fmt::Debug for AnyFocusNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}({:?})", (self.vtable.type_name)(), self.id)
    }
}

impl AnyFocusNode {
    /// The arena id behind this handle.
    pub fn id(self) -> HandleId {
        self.id
    }

    /// Dart's `node as T`: the typed handle when this node is a `T`, else `None`.
    pub fn downcast<T: 'static>(self, app: &App) -> Option<Handle<T>> {
        app.handle::<T>(self.id)
    }

    /// Dart's `node is FocusScopeNode`.
    pub fn as_scope(self) -> Option<Handle<FocusScopeNode>> {
        (self.vtable.as_scope)(self.id)
    }

    /// Dart's `FocusNode` fields.
    fn data(self, app: &App) -> &FocusNodeData {
        (self.vtable.data)(app, self.id)
    }

    fn data_mut(self, app: &mut App) -> &mut FocusNodeData {
        (self.vtable.data_mut)(app, self.id)
    }

    pub(crate) fn manager(self, app: &App) -> Option<Handle<FocusManager>> {
        self.data(app).manager
    }

    /// If true, tells the focus traversal policy to skip over this node for purposes of the
    /// traversal algorithm.
    ///
    /// This may be used to place nodes in the focus tree that may be focused, but not
    /// traversed, allowing them to receive key events as part of the focus chain, but not be
    /// traversed to via focus traversal.
    ///
    /// This is different from [`can_request_focus`](Self::can_request_focus) because it only
    /// implies that the node can't be reached via traversal, not that it can't be focused. It
    /// may still be focused explicitly.
    pub fn skip_traversal(self, app: &mut App) -> bool {
        if self.data(app).skip_traversal {
            return true;
        }
        for ancestor in self.ancestors(app) {
            if !ancestor.descendants_are_traversable(app) {
                return true;
            }
        }
        false
    }

    /// See [`skip_traversal`](Self::skip_traversal).
    pub fn set_skip_traversal(self, app: &mut App, value: bool) {
        if value != self.data(app).skip_traversal {
            self.data_mut(app).skip_traversal = value;
            if let Some(manager) = self.manager(app) {
                manager.mark_properties_changed(app, self);
            }
        }
    }

    /// If true, this focus node may request the primary focus.
    ///
    /// Defaults to true. Set to false if you want this node to do nothing when
    /// [`request_focus`](Self::request_focus) is called on it.
    ///
    /// If set to false on a [`FocusScopeNode`], will cause all of the children of the scope
    /// node to not be focusable.
    ///
    /// If set to false on a [`FocusNode`], it will not affect the focusability of children of
    /// the node.
    ///
    /// The [`has_focus`](Self::has_focus) member can still return true if this node is the
    /// ancestor of a node with primary focus.
    ///
    /// This is different than [`skip_traversal`](Self::skip_traversal) because
    /// [`skip_traversal`](Self::skip_traversal) still allows the node to be focused, just not
    /// traversed to via the `FocusTraversalPolicy`.
    ///
    /// Setting [`can_request_focus`](Self::can_request_focus) to false implies that the node
    /// will also be skipped for traversal purposes.
    pub fn can_request_focus(self, app: &mut App) -> bool {
        if !self.data(app).can_request_focus {
            return false;
        }
        let ancestors = self.ancestors(app);
        ancestors
            .into_iter()
            .all(|ancestor| AnyFocusNode::allow_descendants_to_be_focused(ancestor, app))
    }

    fn allow_descendants_to_be_focused(ancestor: AnyFocusNode, app: &App) -> bool {
        ancestor.descendants_are_focusable(app)
    }

    /// See [`can_request_focus`](Self::can_request_focus).
    pub fn set_can_request_focus(self, app: &mut App, value: bool) {
        if value != self.data(app).can_request_focus {
            // Have to set this first before unfocusing, since it checks this to cull
            // unfocusable, previously-focused children.
            self.data_mut(app).can_request_focus = value;
            if self.has_focus(app) && !value {
                self.unfocus(app, UnfocusDisposition::PreviouslyFocusedChild);
            }
            if let Some(manager) = self.manager(app) {
                manager.mark_properties_changed(app, self);
            }
        }
    }

    /// If false, will disable focus for all of this node's descendants.
    ///
    /// Defaults to true. Does not affect focusability of this node: for that, use
    /// [`can_request_focus`](Self::can_request_focus).
    ///
    /// If any descendants are focused when this is set to false, they will be unfocused. When
    /// [`descendants_are_focusable`](Self::descendants_are_focusable) is set to true again,
    /// they will not be refocused, although they will be able to accept focus again.
    ///
    /// Does not affect the value of [`can_request_focus`](Self::can_request_focus) on the
    /// descendants.
    ///
    /// If a descendant node loses focus when this value is changed, the focus will move to the
    /// scope enclosing this node.
    pub fn descendants_are_focusable(self, app: &App) -> bool {
        (self.vtable.descendants_are_focusable)(app, self.id)
    }

    /// See [`descendants_are_focusable`](Self::descendants_are_focusable).
    pub fn set_descendants_are_focusable(self, app: &mut App, value: bool) {
        if value == self.data(app).descendants_are_focusable {
            return;
        }
        // Set descendants_are_focusable before unfocusing, so the scope won't try and focus
        // any of the children here again if it is false.
        self.data_mut(app).descendants_are_focusable = value;
        if !value && self.has_focus(app) {
            self.unfocus(app, UnfocusDisposition::PreviouslyFocusedChild);
        }
        if let Some(manager) = self.manager(app) {
            manager.mark_properties_changed(app, self);
        }
    }

    /// If false, tells the focus traversal policy to skip over for all of this node's
    /// descendants for purposes of the traversal algorithm.
    ///
    /// Defaults to true. Does not affect the focus traversal of this node: for that, use
    /// [`skip_traversal`](Self::skip_traversal).
    ///
    /// Does not affect the value of [`skip_traversal`](Self::skip_traversal) on the
    /// descendants. Does not affect focusability of the descendants.
    pub fn descendants_are_traversable(self, app: &App) -> bool {
        self.data(app).descendants_are_traversable
    }

    /// See [`descendants_are_traversable`](Self::descendants_are_traversable).
    pub fn set_descendants_are_traversable(self, app: &mut App, value: bool) {
        if value != self.data(app).descendants_are_traversable {
            self.data_mut(app).descendants_are_traversable = value;
            if let Some(manager) = self.manager(app) {
                manager.mark_properties_changed(app, self);
            }
        }
    }

    /// The context that was supplied to [`attach`](Self::attach).
    ///
    /// This is typically the context for the widget that is being focused, as it is used to
    /// determine the bounds of the widget.
    pub fn context(self, app: &App) -> Option<BuildContext> {
        self.data(app).context
    }

    /// Called if this focus node receives a key event while focused (i.e. when
    /// [`has_focus`](Self::has_focus) returns true).
    ///
    /// The [`FocusManager`] receives key events from `HardwareKeyboard` and will pass them to
    /// the focused nodes. It starts with the node with the primary focus, and will call the
    /// `on_key_event` callback for that node. If the callback returns
    /// [`KeyEventResult::Ignored`], indicating that it did not handle the event, the
    /// [`FocusManager`] will move to the parent of that node and call its `on_key_event`. If
    /// that `on_key_event` returns [`KeyEventResult::Handled`], then it will stop propagating
    /// the event. If it reaches the root [`FocusScopeNode`], [`FocusManager::root_scope`], the
    /// event is discarded.
    pub fn on_key_event(self, app: &App) -> Option<FocusOnKeyEventCallback> {
        self.data(app).on_key_event.clone()
    }

    /// See [`on_key_event`](Self::on_key_event).
    pub fn set_on_key_event(self, app: &mut App, value: Option<FocusOnKeyEventCallback>) {
        self.data_mut(app).on_key_event = value;
    }

    /// Returns the parent node for this object.
    ///
    /// All nodes except for the root [`FocusScopeNode`] ([`FocusManager::root_scope`]) will be
    /// given a parent when they are added to the focus tree, which is done using
    /// [`FocusAttachment::reparent`].
    pub fn parent(self, app: &App) -> Option<AnyFocusNode> {
        self.data(app).parent
    }

    /// An iterator over the children of this node.
    pub fn children(self, app: &App) -> Vec<AnyFocusNode> {
        self.data(app)
            .children
            .iter()
            .map(Retained::handle)
            .collect()
    }

    /// An iterator over the children that are allowed to be traversed by the
    /// `FocusTraversalPolicy`.
    ///
    /// Returns the list of focusable, traversable children of this node, regardless of those
    /// settings on this focus node. Will return an empty iterable if
    /// [`descendants_are_focusable`](Self::descendants_are_focusable) is false.
    pub fn traversal_children(self, app: &mut App) -> Vec<AnyFocusNode> {
        (self.vtable.traversal_children)(app, self.id)
    }

    /// A debug label that is used for diagnostic output.
    pub fn debug_label(self, app: &App) -> Option<String> {
        self.data(app).debug_label.clone()
    }

    /// See [`debug_label`](Self::debug_label).
    pub fn set_debug_label(self, app: &mut App, value: Option<String>) {
        if cfg!(debug_assertions) {
            // Only set the value in debug builds.
            self.data_mut(app).debug_label = value;
        }
    }

    /// The hierarchy of children below this one, in depth-first order.
    pub fn descendants(self, app: &mut App) -> Vec<AnyFocusNode> {
        self.fill_descendants(app);
        self.data(app)
            .descendants
            .iter()
            .flatten()
            .map(Retained::handle)
            .collect()
    }

    /// Fills the cache [`descendants`](Self::descendants) reads, depth first.
    fn fill_descendants(self, app: &mut App) {
        if self.data(app).descendants.is_some() {
            return;
        }
        let mut result = Vec::new();
        for child in self.data(app).children.clone() {
            child.fill_descendants(app);
            result.extend(child.data(app).descendants.iter().flatten().cloned());
            result.push(child);
        }
        self.data_mut(app).descendants = Some(result);
    }

    /// Returns all descendants which do not have the
    /// [`skip_traversal`](Self::skip_traversal) and do have the
    /// [`can_request_focus`](Self::can_request_focus) flag set.
    pub fn traversal_descendants(self, app: &mut App) -> Vec<AnyFocusNode> {
        (self.vtable.traversal_descendants)(app, self.id)
    }

    /// The ancestors of this node.
    ///
    /// Iterates the ancestors of this node starting at the parent and iterating over
    /// successively more remote ancestors of this node, ending at the root [`FocusScopeNode`]
    /// ([`FocusManager::root_scope`]).
    pub fn ancestors(self, app: &mut App) -> Vec<AnyFocusNode> {
        if self.data(app).ancestors.is_none() {
            let mut result = Vec::new();
            let mut parent = self.data(app).parent;
            while let Some(node) = parent {
                result.push(node);
                parent = node.data(app).parent;
            }
            self.data_mut(app).ancestors = Some(result);
        }
        self.data(app)
            .ancestors
            .clone()
            .expect("just filled the cache")
    }

    /// Whether this node has input focus.
    ///
    /// A [`FocusNode`] has focus when it is an ancestor of a node that returns true from
    /// [`has_primary_focus`](Self::has_primary_focus), or it has the primary focus itself.
    ///
    /// The [`has_focus`](Self::has_focus) accessor is different from
    /// [`has_primary_focus`](Self::has_primary_focus) in that
    /// [`has_focus`](Self::has_focus) is true if the node is anywhere in the focus chain, but
    /// for [`has_primary_focus`](Self::has_primary_focus) the node must be at the end of the
    /// chain to return true.
    pub fn has_focus(self, app: &mut App) -> bool {
        if self.has_primary_focus(app) {
            return true;
        }
        let primary_focus = self
            .manager(app)
            .and_then(|manager| app.get(manager).primary_focus.handle());
        primary_focus.is_some_and(|primary| primary.ancestors(app).contains(&self))
    }

    /// Returns true if this node currently has the application-wide input focus.
    ///
    /// A [`FocusNode`] has the primary focus when the node is focused in its nearest ancestor
    /// [`FocusScopeNode`] and [`has_focus`](Self::has_focus) is true for all its ancestor
    /// nodes, but none of its descendants.
    pub fn has_primary_focus(self, app: &App) -> bool {
        self.manager(app)
            .is_some_and(|manager| app.get(manager).primary_focus.handle() == Some(self))
    }

    /// Returns the [`FocusHighlightMode`] that is currently in effect for this node.
    pub fn highlight_mode(self, app: &mut App) -> FocusHighlightMode {
        FocusManager::instance(app).highlight_mode(app)
    }

    /// Returns the nearest enclosing scope node above this node, including this node, if it's
    /// a scope.
    ///
    /// Returns `None` if no scope is found.
    ///
    /// Use [`enclosing_scope`](Self::enclosing_scope) to look for scopes above this node.
    pub fn nearest_scope(self, app: &mut App) -> Option<Handle<FocusScopeNode>> {
        (self.vtable.nearest_scope)(app, self.id)
    }

    fn clear_enclosing_scope_cache(self, app: &mut App) {
        let Some(cached_scope) = self.data(app).enclosing_scope else {
            return;
        };
        self.data_mut(app).enclosing_scope = None;
        for child in self.data(app).children.clone() {
            if child.data(app).enclosing_scope == Some(cached_scope) {
                child.clear_enclosing_scope_cache(app);
            }
        }
    }

    /// Returns the nearest enclosing scope node above this node, or `None` if the node has not
    /// yet been added to the focus tree.
    ///
    /// If this node is itself a scope, this will only return ancestors of this scope.
    ///
    /// Use [`nearest_scope`](Self::nearest_scope) to start at this node instead of above it.
    pub fn enclosing_scope(self, app: &mut App) -> Option<Handle<FocusScopeNode>> {
        if let Some(cached) = self.data(app).enclosing_scope {
            return Some(cached);
        }
        let parent = self.data(app).parent;
        // Point access: the parent is another slot, so resolve it before writing ours.
        let computed = parent.and_then(|parent| parent.nearest_scope(app));
        self.data_mut(app).enclosing_scope = computed;
        computed
    }

    /// Returns the size of the attached widget's `RenderObject`, in logical units.
    ///
    /// Size is the size of the transformed widget in global coordinates.
    pub fn size(self, app: &App) -> Size {
        self.rect(app).size()
    }

    /// Returns the global offset to the upper left corner of the attached widget's
    /// `RenderObject`, in logical units.
    ///
    /// Offset is the offset of the transformed widget in global coordinates.
    pub fn offset(self, app: &App) -> Offset {
        let object = self.render_box(app);
        object.local_to_global(app, Offset::ZERO, None)
    }

    /// Returns the global rectangle of the attached widget's `RenderObject`, in logical units.
    ///
    /// Rect is the rectangle of the transformed widget in global coordinates.
    pub fn rect(self, app: &App) -> Rect {
        let object = self.render_box(app);
        let top_left = object.local_to_global(app, Offset::ZERO, None);
        let bottom_right =
            object.local_to_global(app, object.size(app).bottom_right(Offset::ZERO), None);
        Rect::from_ltrb(
            top_left.dx(),
            top_left.dy(),
            bottom_right.dx(),
            bottom_right.dy(),
        )
    }

    /// Dart reads `context!.findRenderObject()!` and its `semanticBounds`; a focus node hosts a
    /// box, whose semantic bounds are `Offset.zero & size`.
    fn render_box(self, app: &App) -> waft_rendering::AnyRenderBox {
        let context = self.data(app).context.expect(
            "Tried to get the bounds of a focus node that didn't have its context set yet.\n\
             The context needs to be set before trying to evaluate traversal policies. \
             Setting the context is typically done with the attach method.",
        );
        context
            .find_render_object(app)
            .expect("the focus node's context has a render object")
            .as_box()
            .expect("the focus node's render object is a RenderBox")
    }

    /// Removes the focus on this node by moving the primary focus to another node.
    ///
    /// This method removes focus from a node that has the primary focus, cancels any
    /// outstanding requests to focus it, while setting the primary focus to another node
    /// according to the `disposition`.
    ///
    /// It is safe to call regardless of whether this node has ever requested focus or not. If
    /// this node doesn't have focus or primary focus, nothing happens.
    ///
    /// If `disposition` is [`UnfocusDisposition::Scope`], then the previously focused node
    /// history of the enclosing scope will be cleared, and the primary focus will be moved to
    /// the nearest enclosing scope ancestor that is enabled for focus, ignoring the
    /// [`FocusScopeNode::focused_child`] for that scope.
    ///
    /// If `disposition` is [`UnfocusDisposition::PreviouslyFocusedChild`], then this node will
    /// be removed from the previously focused list in the
    /// [`enclosing_scope`](Self::enclosing_scope), and the focus will be moved to the
    /// previously focused node of the [`enclosing_scope`](Self::enclosing_scope), which (if it
    /// is a scope itself), will find its focused child, etc., until a leaf focus node is found.
    /// If there is no previously focused child, then the scope itself will receive focus, as if
    /// [`UnfocusDisposition::Scope`] were specified.
    pub fn unfocus(self, app: &mut App, disposition: UnfocusDisposition) {
        let marked_for_focus = self
            .manager(app)
            .map(|manager| app.get(manager).marked_for_focus.handle());
        if !self.has_focus(app) && marked_for_focus != Some(Some(self)) {
            return;
        }
        // If the scope is null, then this is either the root node, or a node that is not yet
        // in the tree, neither of which do anything when unfocused.
        let Some(mut scope) = self.enclosing_scope(app) else {
            return;
        };
        match disposition {
            UnfocusDisposition::Scope => {
                // If it can't request focus, then don't modify its focused children.
                if scope.can_request_focus(app) {
                    // Clearing the focused children here prevents re-focusing the node that we
                    // just unfocused if we immediately hit "next" after unfocusing, and also
                    // prevents choosing to refocus the next-to-last focused child if unfocus is
                    // called more than once.
                    app.get_mut(scope).scope.focused_children.clear();
                }
                while !scope.can_request_focus(app) {
                    scope = scope
                        .enclosing_scope(app)
                        .or_else(|| {
                            self.manager(app)
                                .map(|manager| manager.root_scope(app).handle())
                        })
                        .expect("a node with a scope has a manager");
                }
                scope.do_request_focus(app, false);
            }
            UnfocusDisposition::PreviouslyFocusedChild => {
                // Select the most recent focused child from the nearest focusable scope and
                // focus that. If there isn't one, focus the scope itself.
                if scope.can_request_focus(app) {
                    remove_focused_child(app, scope, self);
                }
                while !scope.can_request_focus(app) {
                    if let Some(enclosing) = scope.enclosing_scope(app) {
                        remove_focused_child(app, enclosing, scope.as_node());
                    }
                    scope = scope
                        .enclosing_scope(app)
                        .or_else(|| {
                            self.manager(app)
                                .map(|manager| manager.root_scope(app).handle())
                        })
                        .expect("a node with a scope has a manager");
                }
                scope.do_request_focus(app, true);
            }
        }
    }

    /// Removes the keyboard token from this focus node if it has one.
    ///
    /// This mechanism helps distinguish between an input control gaining focus by default and
    /// gaining focus as a result of an explicit user action.
    ///
    /// When a focus node requests the focus (either via
    /// [`request_focus`](Self::request_focus) or [`FocusScopeNode::autofocus`]), the focus node
    /// receives a keyboard token if it does not already have one. Later, when the focus node
    /// becomes focused, the widget that manages the text input connection should show the
    /// keyboard only if it successfully consumes the keyboard token from the focus node.
    ///
    /// Returns true if this method successfully consumes the keyboard token.
    pub fn consume_keyboard_token(self, app: &mut App) -> bool {
        if !self.data(app).has_keyboard_token {
            return false;
        }
        self.data_mut(app).has_keyboard_token = false;
        true
    }

    /// Marks the node as being the next to be focused, meaning that it will become the primary
    /// focus and notify listeners of a focus change the next time focus is resolved by the
    /// manager. If something else calls `mark_next_focus` before then, then that node will
    /// become the next focus instead of the previous one.
    fn mark_next_focus(self, app: &mut App, new_focus: AnyFocusNode) {
        if let Some(manager) = self.manager(app) {
            // If we have a manager, then let it handle the focus change.
            manager.mark_next_focus(app, self);
            return;
        }
        // If we don't have a manager, then change the focus locally.
        new_focus.set_as_focused_child_for_scope(app);
        new_focus.notify(app);
        if new_focus != self {
            self.notify(app);
        }
    }

    /// Removes the given [`AnyFocusNode`] and its children as a child of this node.
    fn remove_child(self, app: &mut App, node: AnyFocusNode, remove_scope_focus: bool) {
        debug_assert!(
            self.data(app).children.iter().any(|kept| kept == node),
            "Tried to remove a node that wasn't a child."
        );
        debug_assert!(node.data(app).parent == Some(self));
        debug_assert!(node.manager(app) == self.manager(app));

        if remove_scope_focus && let Some(node_scope) = node.enclosing_scope(app) {
            remove_focused_child(app, node_scope, node);
            for descendant in node.descendants(app) {
                if descendant.enclosing_scope(app) == Some(node_scope) {
                    remove_focused_child(app, node_scope, descendant);
                }
            }
        }

        node.data_mut(app).parent = None;
        node.clear_enclosing_scope_cache(app);
        node.clear_ancestors_caches(app);
        self.data_mut(app).children.retain(|child| child != node);
        for ancestor in self.ancestors(app) {
            ancestor.data_mut(app).descendants = None;
        }
        self.data_mut(app).descendants = None;
        debug_assert!(match self.manager(app) {
            None => true,
            Some(manager) => {
                let root = manager.root_scope(app).as_node();
                !root.descendants(app).contains(&node)
            }
        });
    }

    /// Forgets the ancestors this node and its descendants cached, for a node that has left its
    /// parent: Dart keeps reading them, but the arena frees them.
    fn clear_ancestors_caches(self, app: &mut App) {
        self.data_mut(app).ancestors = None;
        for descendant in self.descendants(app) {
            descendant.data_mut(app).ancestors = None;
        }
    }

    fn update_manager(self, app: &mut App, manager: Option<Handle<FocusManager>>) {
        self.data_mut(app).manager = manager;
        for descendant in self.descendants(app) {
            let data = descendant.data_mut(app);
            data.manager = manager;
            data.ancestors = None;
        }
    }

    /// Used by [`FocusAttachment::reparent`] to perform the actual parenting operation.
    fn reparent_child(self, app: &mut App, child: AnyFocusNode) {
        debug_assert!(
            child != self,
            "Tried to make a child into a parent of itself."
        );
        if child.data(app).parent == Some(self) {
            debug_assert!(
                self.data(app).children.iter().any(|kept| kept == child),
                "Found a node that says it's a child, but doesn't appear in the child list."
            );
            // The child is already a child of this parent.
            return;
        }
        debug_assert!(match self.manager(app) {
            None => true,
            Some(manager) => child != manager.root_scope(app).as_node(),
        });
        debug_assert!(
            !self.ancestors(app).contains(&child),
            "The supplied child is already an ancestor of this node. Loops are not allowed."
        );
        let old_scope = child.enclosing_scope(app);
        let had_focus = child.has_focus(app);
        if let Some(parent) = child.data(app).parent {
            let nearest_scope = self.nearest_scope(app);
            parent.remove_child(app, child, old_scope != nearest_scope);
        }
        let kept_child = child.keep(app);
        self.data_mut(app).children.push(kept_child);
        let child_data = child.data_mut(app);
        child_data.parent = Some(self);
        child_data.ancestors = None;
        let manager = self.manager(app);
        child.update_manager(app, manager);
        for ancestor in child.ancestors(app) {
            ancestor.data_mut(app).descendants = None;
        }
        if had_focus {
            // Update the focus chain for the current focus without changing it.
            let primary_focus = self
                .manager(app)
                .and_then(|manager| app.get(manager).primary_focus.handle());
            if let Some(primary_focus) = primary_focus {
                primary_focus.set_as_focused_child_for_scope(app);
            }
        }
        if let Some(old_scope) = old_scope
            && let Some(context) = child.data(app).context
            && child.enclosing_scope(app) != Some(old_scope)
            && let Some(policy) = FocusTraversalGroup::maybe_of(app, context)
        {
            policy.changed_scope(app, Some(child), Some(old_scope));
        }
        if child.data(app).request_focus_when_reparented {
            child.do_request_focus(app, true);
            child.data_mut(app).request_focus_when_reparented = false;
        }
    }

    /// Called by the _host_ `StatefulWidget` to attach a [`FocusNode`] to the widget tree.
    ///
    /// In order to attach a [`FocusNode`] to the widget tree, call
    /// [`attach`](Self::attach), typically from the `StatefulWidget`'s `State::init_state`
    /// method.
    ///
    /// If the focus node in the host widget is swapped out, the new node will need to be
    /// attached. [`FocusAttachment::detach`] should be called on the old node, and then
    /// [`attach`](Self::attach) called on the new node. This typically happens in the
    /// `State::did_update_widget` method.
    pub fn attach(
        self,
        app: &mut App,
        context: Option<BuildContext>,
        on_key_event: Option<FocusOnKeyEventCallback>,
    ) -> Ref<FocusAttachment> {
        self.data_mut(app).context = context;
        if on_key_event.is_some() {
            self.data_mut(app).on_key_event = on_key_event;
        }
        let attachment = FocusAttachment::new(app, self);
        self.data_mut(app).attachment = Some(attachment.clone());
        attachment
    }

    /// Discards any resources used by this node.
    ///
    /// Detaching also unfocuses and cleans up the manager's data structures.
    pub fn dispose(self, app: &mut App) {
        if let Some(attachment) = self.data(app).attachment.handle() {
            attachment.detach(app);
        }
        (self.vtable.dispose_notifier)(app, self.id);
    }

    fn notify(self, app: &mut App) {
        if self.data(app).parent.is_none() {
            // no longer part of the tree, so don't notify.
            return;
        }
        if self.has_primary_focus(app) {
            self.set_as_focused_child_for_scope(app);
        }
        (self.vtable.notify_listeners)(app, self.id);
    }

    /// Requests the primary focus for this node, or for a supplied `node`, which will also give
    /// focus to its [`ancestors`](Self::ancestors).
    ///
    /// If called without a node, request focus for this node. If the node hasn't been added to
    /// the focus tree yet, then defer the focus request until it is, allowing newly created
    /// widgets to request focus as soon as they are added.
    ///
    /// If the given `node` is not yet a part of the focus tree, then this method will add the
    /// `node` as a child of this node before requesting focus.
    ///
    /// If the given `node` is a [`FocusScopeNode`] and that focus scope node has a non-null
    /// [`FocusScopeNode::focused_child`], then request the focus for the focused child. This
    /// process is recursive and continues until it encounters either a focus scope node with a
    /// null focused child or an ordinary (non-scope) [`FocusNode`] is found.
    ///
    /// The node is notified that it has received the primary focus in a microtask, so
    /// notification may lag the request by up to one frame.
    pub fn request_focus(self, app: &mut App, node: Option<AnyFocusNode>) {
        if let Some(node) = node {
            if node.data(app).parent.is_none() {
                self.reparent_child(app, node);
            }
            debug_assert!(
                node.ancestors(app).contains(&self),
                "Focus was requested for a node that is not a descendant of the scope from \
                 which it was requested."
            );
            node.do_request_focus(app, true);
            return;
        }
        self.do_request_focus(app, true);
    }

    /// Dispatches to the leaf; [`FocusScopeNode`] overrides this.
    fn do_request_focus(self, app: &mut App, find_first_focus: bool) {
        (self.vtable.do_request_focus)(app, self.id, find_first_focus);
    }

    /// Sets this node as the [`FocusScopeNode::focused_child`] of the enclosing scope.
    ///
    /// Sets this node as the focused child for the enclosing scope, and that scope as the
    /// focused child for the scope above it, etc., until it reaches the root node. It doesn't
    /// change the primary focus, it just changes what node would be focused if the enclosing
    /// scope receives focus, and keeps track of previously focused children in that scope, so
    /// that if the focused child in that scope is removed, the previous focus returns.
    fn set_as_focused_child_for_scope(self, app: &mut App) {
        let mut scope_focus = self;
        for ancestor in self.ancestors(app) {
            let Some(ancestor) = ancestor.as_scope() else {
                continue;
            };
            debug_assert!(
                scope_focus != ancestor.as_node(),
                "Somehow made a loop by setting focusedChild to its scope."
            );
            // Remove it anywhere in the focused child history.
            remove_focused_child(app, ancestor, scope_focus);
            // Add it to the end of the list, which is also the top of the queue: the end of the
            // list represents the currently focused child.
            let kept = scope_focus.keep(app);
            app.get_mut(ancestor).scope.focused_children.push(kept);
            scope_focus = ancestor.as_node();
        }
    }

    /// Request to move the focus to the next focus node, by calling the
    /// [`FocusTraversalPolicy::next`] method.
    ///
    /// Returns true if it successfully found a node and requested focus.
    ///
    /// [`FocusTraversalPolicy::next`]: crate::FocusTraversalPolicy::next
    pub fn next_focus(self, app: &mut App) -> bool {
        let context = self
            .data(app)
            .context
            .expect("a node in the tree has a context");
        FocusTraversalGroup::of(app, context).next(app, self)
    }

    /// Request to move the focus to the previous focus node, by calling the
    /// [`FocusTraversalPolicy::previous`] method.
    ///
    /// Returns true if it successfully found a node and requested focus.
    ///
    /// [`FocusTraversalPolicy::previous`]: crate::FocusTraversalPolicy::previous
    pub fn previous_focus(self, app: &mut App) -> bool {
        let context = self
            .data(app)
            .context
            .expect("a node in the tree has a context");
        FocusTraversalGroup::of(app, context).previous(app, self)
    }

    /// Request to move the focus to the nearest focus node in the given direction, by calling
    /// the [`FocusTraversalPolicy::in_direction`] method.
    ///
    /// Returns true if it successfully found a node and requested focus.
    ///
    /// [`FocusTraversalPolicy::in_direction`]: crate::FocusTraversalPolicy::in_direction
    pub fn focus_in_direction(self, app: &mut App, direction: TraversalDirection) -> bool {
        let context = self
            .data(app)
            .context
            .expect("a node in the tree has a context");
        FocusTraversalGroup::of(app, context).in_direction(app, self, direction)
    }

    /// Register a closure to be called when this node notifies its listeners.
    pub fn add_listener(self, app: &mut App, listener: Listener) {
        (self.vtable.add_listener)(app, self.id, listener);
    }

    /// Remove a previously registered closure from the list of closures this node notifies.
    pub fn remove_listener(self, app: &mut App, listener: &Listener) {
        (self.vtable.remove_listener)(app, self.id, listener);
    }
}

impl ListenableHandle for AnyFocusNode {
    fn add_listener_to(self, app: &mut App, listener: Listener) {
        AnyFocusNode::add_listener(self, app, listener);
    }

    fn remove_listener_from(self, app: &mut App, listener: &Listener) {
        AnyFocusNode::remove_listener(self, app, listener);
    }
}

/// Dart's `scope._focusedChildren.remove(node)`.
fn remove_focused_child(app: &mut App, scope: Handle<FocusScopeNode>, node: AnyFocusNode) {
    app.get_mut(scope)
        .scope
        .focused_children
        .retain(|child| child != node);
}

/// An object that can be used by a stateful widget to obtain the keyboard focus and to handle
/// keyboard events.
///
/// _Please see the `Focus` and `FocusScope` widgets, which are utility widgets that manage
/// their own [`FocusNode`]s and [`FocusScopeNode`]s, respectively. If they aren't appropriate,
/// [`FocusNode`]s can be managed directly, but doing this is rare._
///
/// [`FocusNode`]s are persistent objects that form a _focus tree_ that is a representation of
/// the widgets in the hierarchy that are interested in focus. A focus node might need to be
/// created if it is passed in from an ancestor of a `Focus` widget to control the focus of the
/// children from the ancestor, or a widget might need to host one if the widget subsystem is
/// not being used, or if the `Focus` and `FocusScope` widgets provide insufficient control.
///
/// [`FocusNode`]s are organized into _scopes_ (see [`FocusScopeNode`]), which form sub-trees of
/// nodes that restrict traversal to a group of nodes. Within a scope, the most recent nodes to
/// have focus are remembered, and if a node is focused and then unfocused, the previous node
/// receives focus again.
///
/// The focus node hierarchy can be traversed using the [`AnyFocusNode::parent`],
/// [`AnyFocusNode::children`], [`AnyFocusNode::ancestors`] and [`AnyFocusNode::descendants`]
/// accessors.
///
/// [`FocusNode`]s are `ChangeNotifier`s, so a listener can be registered to receive a
/// notification when the focus changes. Listeners will also be notified when
/// [`AnyFocusNode::skip_traversal`], [`AnyFocusNode::can_request_focus`],
/// [`AnyFocusNode::descendants_are_focusable`], and
/// [`AnyFocusNode::descendants_are_traversable`] properties are updated.
///
/// ## Lifecycle
///
/// There are several actors involved in the lifecycle of a [`FocusNode`] / [`FocusScopeNode`].
/// They are created and disposed by their _owner_, attached, detached, and re-parented using a
/// [`FocusAttachment`] by their _host_ (which must be owned by the `State` of a
/// `StatefulWidget`), and they are managed by the [`FocusManager`].
///
/// Dart's optional constructor arguments (`debugLabel`, `onKeyEvent`, `skipTraversal`,
/// `canRequestFocus`, `descendantsAreFocusable`, `descendantsAreTraversable`) are the node's
/// setters; each of them is a public setter in Dart too, and none of them notifies before the
/// node joins a tree.
pub struct FocusNode {
    change_notifier: ChangeNotifierData,
    focus_node: FocusNodeData,
}

impl FocusNode {
    /// Creates a focus node.
    pub fn new(app: &mut App) -> Ref<FocusNode> {
        app.create(FocusNode {
            change_notifier: ChangeNotifierData::new(),
            focus_node: FocusNodeData::new(),
        })
    }

    // ---- the bodies a subclass reaches with `super` ----

    /// `FocusNode.nearestScope`.
    fn nearest_scope(this: AnyFocusNode, app: &mut App) -> Option<Handle<FocusScopeNode>> {
        this.enclosing_scope(app)
    }

    /// `FocusNode.descendantsAreFocusable`.
    fn descendants_are_focusable(this: AnyFocusNode, app: &App) -> bool {
        this.data(app).descendants_are_focusable
    }

    /// `FocusNode.traversalChildren`.
    fn traversal_children(this: AnyFocusNode, app: &mut App) -> Vec<AnyFocusNode> {
        if !this.descendants_are_focusable(app) {
            return Vec::new();
        }
        this.children(app)
            .into_iter()
            .filter(|node| !node.skip_traversal(app) && node.can_request_focus(app))
            .collect()
    }

    /// `FocusNode.traversalDescendants`.
    fn traversal_descendants(this: AnyFocusNode, app: &mut App) -> Vec<AnyFocusNode> {
        if !this.descendants_are_focusable(app) {
            return Vec::new();
        }
        this.descendants(app)
            .into_iter()
            .filter(|node| !node.skip_traversal(app) && node.can_request_focus(app))
            .collect()
    }

    /// `FocusNode._doRequestFocus`.
    fn do_request_focus(this: AnyFocusNode, app: &mut App, _find_first_focus: bool) {
        if !this.can_request_focus(app) {
            return;
        }
        // If the node isn't part of the tree, then we just defer the focus request until the
        // next time it is reparented, so that it's possible to focus newly added widgets.
        if this.data(app).parent.is_none() {
            this.data_mut(app).request_focus_when_reparented = true;
            return;
        }
        this.set_as_focused_child_for_scope(app);
        let marked_for_focus = this
            .manager(app)
            .and_then(|manager| app.get(manager).marked_for_focus.handle());
        if this.has_primary_focus(app)
            && (marked_for_focus.is_none() || marked_for_focus == Some(this))
        {
            return;
        }
        this.data_mut(app).has_keyboard_token = true;
        this.mark_next_focus(app, this);
    }
}

impl ChangeNotifier for FocusNode {
    fn change_notifier_data(&self) -> &ChangeNotifierData {
        &self.change_notifier
    }

    fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
        &mut self.change_notifier
    }
}

impl FocusNodeLeaf for FocusNode {
    crate::focus_node_accessors!();
}

// ---------------------------------------------------------------------------------------------
// FocusScopeNode

/// The fields Dart's `FocusScopeNode` adds to [`FocusNodeData`].
pub struct FocusScopeNodeData {
    /// Controls the transfer of focus beyond the first and the last items of a
    /// [`FocusScopeNode`].
    ///
    /// Changing this field value has no immediate effect on the UI. Instead, next time focus
    /// traversal takes place `FocusTraversalPolicy` will read this value and apply the new
    /// behavior.
    pub traversal_edge_behavior: TraversalEdgeBehavior,

    /// Controls the directional transfer of focus when the focus is on the first or last item.
    ///
    /// Changing this field value has no immediate effect on the UI. Instead, next time focus
    /// traversal takes place `FocusTraversalPolicy` will read this value and apply the new
    /// behavior.
    pub directional_traversal_edge_behavior: TraversalEdgeBehavior,

    /// A stack of the children that have been set as the `focused_child`, most recent last
    /// (which is the top of the stack).
    focused_children: Vec<FocusNodeRef>,
}

/// A subclass of [`FocusNode`] that acts as a scope for its descendants, maintaining
/// information about which descendant is currently or was last focused.
///
/// _Please see the `FocusScope` and `Focus` widgets, which are utility widgets that manage
/// their own [`FocusScopeNode`]s and [`FocusNode`]s, respectively. If they aren't appropriate,
/// [`FocusScopeNode`]s can be managed directly._
///
/// [`FocusScopeNode`] organizes [`FocusNode`]s into _scopes_. Scopes form sub-trees of nodes
/// that can be traversed as a group. Within a scope, the most recent nodes to have focus are
/// remembered, and if a node is focused and then removed, the original node receives focus
/// again.
///
/// From a [`FocusScopeNode`], calling [`set_first_focus`](Self::set_first_focus) sets the given
/// focus scope as the [`focused_child`](Self::focused_child) of this node, adopting if it isn't
/// already part of the focus tree.
pub struct FocusScopeNode {
    change_notifier: ChangeNotifierData,
    focus_node: FocusNodeData,
    scope: FocusScopeNodeData,
}

impl FocusScopeNode {
    /// Creates a [`FocusScopeNode`].
    ///
    /// Dart's optional constructor arguments are the setters; `descendantsAreFocusable` is
    /// always true on a scope, which is [`FocusNodeData`]'s default.
    pub fn new(app: &mut App) -> Ref<FocusScopeNode> {
        app.create(FocusScopeNode {
            change_notifier: ChangeNotifierData::new(),
            focus_node: FocusNodeData::new(),
            scope: FocusScopeNodeData {
                traversal_edge_behavior: TraversalEdgeBehavior::ClosedLoop,
                directional_traversal_edge_behavior: TraversalEdgeBehavior::Stop,
                focused_children: Vec::new(),
            },
        })
    }

    /// See [`FocusScopeNodeData::traversal_edge_behavior`].
    pub fn traversal_edge_behavior(self: Handle<Self>, app: &App) -> TraversalEdgeBehavior {
        app.get(self).scope.traversal_edge_behavior
    }

    /// See [`FocusScopeNodeData::traversal_edge_behavior`].
    pub fn set_traversal_edge_behavior(
        self: Handle<Self>,
        app: &mut App,
        value: TraversalEdgeBehavior,
    ) {
        app.get_mut(self).scope.traversal_edge_behavior = value;
    }

    /// See [`FocusScopeNodeData::directional_traversal_edge_behavior`].
    pub fn directional_traversal_edge_behavior(
        self: Handle<Self>,
        app: &App,
    ) -> TraversalEdgeBehavior {
        app.get(self).scope.directional_traversal_edge_behavior
    }

    /// See [`FocusScopeNodeData::directional_traversal_edge_behavior`].
    pub fn set_directional_traversal_edge_behavior(
        self: Handle<Self>,
        app: &mut App,
        value: TraversalEdgeBehavior,
    ) {
        app.get_mut(self).scope.directional_traversal_edge_behavior = value;
    }

    /// Returns true if this scope is the focused child of its parent scope.
    pub fn is_first_focus(self: Handle<Self>, app: &mut App) -> bool {
        let enclosing = self
            .enclosing_scope(app)
            .expect("a scope that is asked for isFirstFocus has an enclosing scope");
        enclosing.focused_child(app) == Some(self.as_node())
    }

    /// Returns the child of this node that should receive focus if this scope node receives
    /// focus.
    ///
    /// If [`has_focus`](FocusNodeLeaf::has_focus) is true, then this points to the child of
    /// this node that is currently focused.
    ///
    /// Returns `None` if there is no currently focused child.
    pub fn focused_child(self: Handle<Self>, app: &mut App) -> Option<AnyFocusNode> {
        let last = app
            .get(self)
            .scope
            .focused_children
            .last()
            .map(Retained::handle);
        debug_assert!(match last {
            None => true,
            Some(last) => last.enclosing_scope(app) == Some(self),
        });
        last
    }

    /// Make the given `scope` the active child scope for this scope.
    ///
    /// If the given `scope` is not yet a part of the focus tree, then add it to the tree as a
    /// child of this scope. If it is already part of the focus tree, the given scope must be a
    /// descendant of this scope.
    pub fn set_first_focus(self: Handle<Self>, app: &mut App, scope: Handle<FocusScopeNode>) {
        debug_assert!(scope != self, "Unexpected self-reference in setFirstFocus.");
        if scope.parent(app).is_none() {
            self.as_node().reparent_child(app, scope.as_node());
        }
        debug_assert!(
            scope.ancestors(app).contains(&self.as_node()),
            "A FocusScopeNode must be a child of this scope to set it as first focus."
        );
        if self.has_focus(app) {
            scope.do_request_focus(app, true);
        } else {
            scope.as_node().set_as_focused_child_for_scope(app);
        }
    }

    /// If this scope lacks a focus, request that the given node become the focus.
    ///
    /// If the given node is not yet part of the focus tree, then add it as a child of this
    /// node.
    ///
    /// Useful for widgets that wish to grab the focus if no other widget already has the focus.
    ///
    /// The node is notified that it has received the primary focus in a microtask, so
    /// notification may lag the request by up to one frame.
    pub fn autofocus(self: Handle<Self>, app: &mut App, node: AnyFocusNode) {
        // Attach the node to the tree first, so in `apply_focus_change` if the node is detached
        // we don't add it back to the tree.
        if node.parent(app).is_none() {
            self.as_node().reparent_child(app, node);
        }
        debug_assert!(self.as_node().manager(app).is_some());
        if let Some(manager) = self.as_node().manager(app) {
            let autofocus = Autofocus {
                scope: self.keep(app),
                autofocus_node: node.keep(app),
            };
            app.get_mut(manager).pending_autofocuses.push(autofocus);
            manager.mark_needs_update(app);
        }
    }

    /// Requests that the scope itself receive focus, without trying to find a descendant that
    /// should receive focus.
    ///
    /// This is used only if you want to park the focus on a scope itself.
    pub fn request_scope_focus(self: Handle<Self>, app: &mut App) {
        self.do_request_focus(app, false);
    }
}

impl ChangeNotifier for FocusScopeNode {
    fn change_notifier_data(&self) -> &ChangeNotifierData {
        &self.change_notifier
    }

    fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
        &mut self.change_notifier
    }
}

impl FocusNodeLeaf for FocusScopeNode {
    crate::focus_node_accessors!();

    fn as_scope(self: Handle<Self>) -> Option<Handle<FocusScopeNode>> {
        Some(self)
    }

    fn nearest_scope(self: Handle<Self>, _app: &mut App) -> Option<Handle<FocusScopeNode>> {
        Some(self)
    }

    fn descendants_are_focusable(self: Handle<Self>, app: &App) -> bool {
        app.get(self).focus_node.can_request_focus
            && FocusNode::descendants_are_focusable(self.as_node(), app)
    }

    /// Will return an empty iterable if this scope node is not focusable, or if
    /// `descendants_are_focusable` is false.
    fn traversal_children(self: Handle<Self>, app: &mut App) -> Vec<AnyFocusNode> {
        if !self.can_request_focus(app) {
            return Vec::new();
        }
        FocusNode::traversal_children(self.as_node(), app)
    }

    /// Will return an empty iterable if this scope node is not focusable, or if
    /// `descendants_are_focusable` is false.
    fn traversal_descendants(self: Handle<Self>, app: &mut App) -> Vec<AnyFocusNode> {
        if !self.can_request_focus(app) {
            return Vec::new();
        }
        FocusNode::traversal_descendants(self.as_node(), app)
    }

    fn do_request_focus(self: Handle<Self>, app: &mut App, find_first_focus: bool) {
        // It is possible that a previously focused child is no longer focusable, so clean out
        // the list if so.
        loop {
            let last = app
                .get(self)
                .scope
                .focused_children
                .last()
                .map(Retained::handle);
            let Some(last) = last else { break };
            if last.can_request_focus(app) && last.enclosing_scope(app).is_some() {
                break;
            }
            app.get_mut(self).scope.focused_children.pop();
        }

        let focused_child = self.focused_child(app);
        // If find_first_focus is false, then the request is to make this scope the focus
        // instead of looking for the ultimate first focus for this scope and its descendants.
        let Some(focused_child) = focused_child.filter(|_| find_first_focus) else {
            if self.can_request_focus(app) {
                self.as_node().set_as_focused_child_for_scope(app);
                self.as_node().mark_next_focus(app, self.as_node());
            }
            return;
        };

        focused_child.do_request_focus(app, true);
    }
}

// ---------------------------------------------------------------------------------------------
// The highlight mode

/// An enum to describe which kind of focus highlight behavior to use when displaying focus
/// information.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FocusHighlightMode {
    /// Touch interfaces will not show the focus highlight except for controls which bring up
    /// the soft keyboard.
    ///
    /// If a device that uses a traditional mouse and keyboard has a touch screen attached, it
    /// can also enter `touch` mode if the user is using the touch screen.
    Touch,

    /// Traditional interfaces (keyboard and mouse) will show the currently focused control via
    /// a focus highlight of some sort.
    ///
    /// If a touch device (like a mobile phone) has a keyboard and/or mouse attached, it also
    /// can enter `traditional` mode if the user is using these input devices.
    Traditional,
}

/// An enum to describe how the current value of [`FocusManager::highlight_mode`] is determined.
/// The strategy is set on [`FocusManager::set_highlight_strategy`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FocusHighlightStrategy {
    /// Automatic switches between the various highlight modes based on the last kind of input
    /// that was received. This is the default.
    #[default]
    Automatic,

    /// [`FocusManager::highlight_mode`] always returns [`FocusHighlightMode::Touch`].
    AlwaysTouch,

    /// [`FocusManager::highlight_mode`] always returns [`FocusHighlightMode::Traditional`].
    AlwaysTraditional,
}

// ---------------------------------------------------------------------------------------------
// FocusManager

/// Dart's `_AppLifecycleListener`: the observer through which a [`FocusManager`] hears the
/// app's lifecycle, so that the manager itself is no binding observer.
struct AppLifecycleListener {
    on_lifecycle_state_changed: ValueChanged<AppLifecycleState>,
}

impl WidgetsBindingObserver for AppLifecycleListener {
    fn did_change_app_lifecycle_state(&self, app: &mut App, state: AppLifecycleState) {
        (self.on_lifecycle_state_changed)(app, state);
    }
}

/// Manages the focus tree.
///
/// The focus tree is a separate, sparser, tree from the widget tree that maintains the
/// hierarchical relationship between focusable widgets in the widget tree.
///
/// The focus manager is responsible for tracking which [`FocusNode`] has the primary input
/// focus (the [`primary_focus`](Self::primary_focus)), holding the [`FocusScopeNode`] that is
/// the root of the focus tree (the [`root_scope`](Self::root_scope)), and what the current
/// [`highlight_mode`](Self::highlight_mode) is. It also distributes [`KeyEvent`]s to the nodes
/// in the focus tree.
///
/// The singleton [`FocusManager`] instance is held by the `WidgetsBinding` as
/// `WidgetsBinding::focus_manager`, and can be conveniently accessed using the
/// [`FocusManager::instance`] accessor.
///
/// To find the [`FocusNode`] for a given `BuildContext`, use `Focus::of`. To find the
/// [`FocusScopeNode`] for a given `BuildContext`, use `FocusScope::of`.
///
/// If you would like notification whenever the [`primary_focus`](Self::primary_focus) changes,
/// register a listener with `add_listener`.
///
/// The [`highlight_mode`](Self::highlight_mode) describes how focus highlights should be
/// displayed on components in the UI. The highlight mode changes are notified separately via
/// [`add_highlight_mode_listener`](Self::add_highlight_mode_listener) and removed with
/// [`remove_highlight_mode_listener`](Self::remove_highlight_mode_listener).
pub struct FocusManager {
    change_notifier: ChangeNotifierData,
    highlight_manager: Ref<HighlightModeManager>,
    /// The root [`FocusScopeNode`] in the focus tree.
    root_scope: Ref<FocusScopeNode>,
    primary_focus: Option<FocusNodeRef>,
    /// The set of nodes that need to notify their listeners of changes at the next update.
    dirty_nodes: Vec<FocusNodeRef>,
    /// The node that has requested to have the primary focus, but hasn't been given it yet.
    marked_for_focus: Option<FocusNodeRef>,
    /// The list of autofocus requests made since the last `apply_focus_change` call.
    pending_autofocuses: Vec<Autofocus>,
    /// True indicates that there is an update pending.
    have_scheduled_update: bool,
    /// Allows the manager to respond to app lifecycle state changes, temporarily suspending
    /// the primary focus when the app is inactive.
    app_lifecycle_listener: Option<WidgetsBindingObserverRef>,
    /// The node that was focused before the app lifecycle changed. Will be restored as the
    /// primary focus once the app is resumed.
    suspended_node: Option<FocusNodeRef>,
}

impl FocusManager {
    /// Creates an object that manages the focus tree.
    ///
    /// This constructor is rarely called directly. To access the [`FocusManager`], consider
    /// using the [`instance`](Self::instance) accessor instead (which gets it from the
    /// `WidgetsBinding` singleton).
    ///
    /// This newly constructed focus manager does not have the necessary event handlers
    /// registered to allow it to manage focus. To register those event handlers, callers must
    /// call [`register_global_handlers`](Self::register_global_handlers).
    pub fn new(app: &mut App) -> Ref<FocusManager> {
        let root_scope = FocusScopeNode::new(app);
        root_scope.set_debug_label(app, Some("Root Focus Scope".to_string()));
        let highlight_manager = HighlightModeManager::new(app);
        let this = app.create(FocusManager {
            change_notifier: ChangeNotifierData::new(),
            highlight_manager,
            root_scope: root_scope.clone(),
            primary_focus: None,
            dirty_nodes: Vec::new(),
            marked_for_focus: None,
            pending_autofocuses: Vec::new(),
            have_scheduled_update: false,
            app_lifecycle_listener: None,
            suspended_node: None,
        });
        this.listen_to_application_lifecycle_changes_if_supported(app);
        root_scope.focus_node_data_mut(app).manager = Some(this.handle());
        this
    }

    /// It appears that some Android keyboard implementations can cause app lifecycle state
    /// changes: adding the app lifecycle listener would cause the text field to unfocus as the
    /// user is trying to type.
    ///
    /// Additionally, on iOS, input fields aren't automatically populated with relevant data
    /// when using autofill.
    ///
    /// Until these are resolved, we won't be adding the listener to mobile platforms.
    /// <https://github.com/flutter/flutter/issues/148475#issuecomment-2118407411>
    /// <https://github.com/flutter/flutter/pull/142930#issuecomment-1981750069>
    fn respond_to_lifecycle_change(app: &App) -> bool {
        K_IS_WEB
            || match app.platform().target_platform() {
                TargetPlatform::Android | TargetPlatform::IOS => false,
                TargetPlatform::Fuchsia | TargetPlatform::Linux => true,
                TargetPlatform::Windows | TargetPlatform::MacOS => true,
            }
    }

    /// Enables this [`FocusManager`] to listen to changes of the application lifecycle if it
    /// does not already have an application lifecycle listener active, and the app isn't
    /// running on a native mobile platform.
    ///
    /// Typically, the application lifecycle listener for this [`FocusManager`] is set up at
    /// construction, but sometimes it is necessary to manually initialize it when the platform
    /// did not name the relevant target at the time of construction. This can happen in a test
    /// that changes the platform's target after the binding has built its [`FocusManager`]; the
    /// test then calls this method, so the [`FocusManager`] can accurately listen to
    /// application lifecycle changes, if supported.
    pub fn listen_to_application_lifecycle_changes_if_supported(self: Handle<Self>, app: &mut App) {
        if app.get(self).app_lifecycle_listener.is_none()
            && FocusManager::respond_to_lifecycle_change(app)
        {
            let listener: WidgetsBindingObserverRef = Rc::new(AppLifecycleListener {
                on_lifecycle_state_changed: Rc::new(move |app, state| {
                    self.app_lifecycle_change(app, state)
                }),
            });
            app.get_mut(self).app_lifecycle_listener = Some(Rc::clone(&listener));
            WidgetsBinding::instance(app).add_observer(app, listener);
        }
    }

    /// Registers global input event handlers that are needed to manage focus.
    ///
    /// This calls `HardwareKeyboard::add_handler` on the shared instance of `HardwareKeyboard`
    /// and adds a route to the global entry in the gesture routing table. As such, only one
    /// [`FocusManager`] instance should register its global handlers.
    ///
    /// When this focus manager is no longer needed, calling [`dispose`](Self::dispose) on it
    /// will unregister these handlers.
    pub fn register_global_handlers(self: Handle<Self>, app: &mut App) {
        app.get(self)
            .highlight_manager
            .register_global_handlers(app);
    }

    /// Discards any resources used by this object.
    pub fn dispose(self: Handle<Self>, app: &mut App) {
        if let Some(listener) = app.get(self).app_lifecycle_listener.clone() {
            WidgetsBinding::instance(app).remove_observer(app, &listener);
        }
        app.get(self).highlight_manager.dispose(app);
        self.root_scope(app).handle().dispose(app);
        ChangeNotifierData::dispose(app, self);
    }

    /// Provides convenient access to the current [`FocusManager`] singleton from the
    /// `WidgetsBinding` instance.
    pub fn instance(app: &mut App) -> Handle<FocusManager> {
        WidgetsBinding::instance(app).focus_manager(app)
    }

    /// The strategy by which [`highlight_mode`](Self::highlight_mode) is determined.
    ///
    /// If set to [`FocusHighlightStrategy::Automatic`], then the highlight mode will change
    /// depending upon the interaction mode used last.
    ///
    /// Defaults to [`FocusHighlightStrategy::Automatic`].
    pub fn highlight_strategy(self: Handle<Self>, app: &App) -> FocusHighlightStrategy {
        app.get(&app.get(self).highlight_manager).strategy
    }

    /// See [`highlight_strategy`](Self::highlight_strategy).
    pub fn set_highlight_strategy(
        self: Handle<Self>,
        app: &mut App,
        value: FocusHighlightStrategy,
    ) {
        let highlight_manager = app.get(self).highlight_manager.handle();
        if app.get(highlight_manager).strategy == value {
            return;
        }
        highlight_manager.set_strategy(app, value);
    }

    /// Indicates the current interaction mode for focus highlights.
    ///
    /// The value returned depends upon the
    /// [`highlight_strategy`](Self::highlight_strategy) used, and possibly the most recent
    /// interaction mode that the user used.
    ///
    /// If [`highlight_mode`](Self::highlight_mode) returns [`FocusHighlightMode::Touch`], then
    /// widgets should not draw their focus highlight unless they perform text entry.
    ///
    /// If it returns [`FocusHighlightMode::Traditional`], then widgets should draw their focus
    /// highlight whenever they are focused.
    pub fn highlight_mode(self: Handle<Self>, app: &mut App) -> FocusHighlightMode {
        app.get(self).highlight_manager.highlight_mode(app)
    }

    /// Register a closure to be called when the [`FocusManager`] notifies its listeners that
    /// the value of [`highlight_mode`](Self::highlight_mode) has changed.
    pub fn add_highlight_mode_listener(
        self: Handle<Self>,
        app: &mut App,
        listener: HighlightModeListener,
    ) {
        app.get(self).highlight_manager.add_listener(app, listener);
    }

    /// Remove a previously registered closure from the list of closures that the
    /// [`FocusManager`] notifies.
    pub fn remove_highlight_mode_listener(
        self: Handle<Self>,
        app: &mut App,
        listener: &HighlightModeListener,
    ) {
        app.get(self)
            .highlight_manager
            .remove_listener(app, listener);
    }

    /// Adds a key event handler to a set of handlers that are called before any key event
    /// handlers in the focus tree are called.
    ///
    /// All of the handlers in the set will be called for every key event the [`FocusManager`]
    /// receives. If any one of the handlers returns [`KeyEventResult::Handled`] or
    /// [`KeyEventResult::SkipRemainingHandlers`], then none of the handlers in the focus tree
    /// will be called.
    pub fn add_early_key_event_handler(
        self: Handle<Self>,
        app: &mut App,
        handler: OnKeyEventCallback,
    ) {
        app.get(self)
            .highlight_manager
            .add_early_key_event_handler(app, handler);
    }

    /// Removes a key handler added by calling
    /// [`add_early_key_event_handler`](Self::add_early_key_event_handler).
    pub fn remove_early_key_event_handler(
        self: Handle<Self>,
        app: &mut App,
        handler: &OnKeyEventCallback,
    ) {
        app.get(self)
            .highlight_manager
            .remove_early_key_event_handler(app, handler);
    }

    /// Adds a key event handler to a set of handlers that are called if none of the key event
    /// handlers in the focus tree handle the event.
    ///
    /// If the event reaches the root of the focus tree without being handled, then all of the
    /// handlers in the set will be called. If any of them returns [`KeyEventResult::Handled`]
    /// or [`KeyEventResult::SkipRemainingHandlers`], then event propagation to the platform
    /// will be stopped.
    pub fn add_late_key_event_handler(
        self: Handle<Self>,
        app: &mut App,
        handler: OnKeyEventCallback,
    ) {
        app.get(self)
            .highlight_manager
            .add_late_key_event_handler(app, handler);
    }

    /// Removes a key handler added by calling
    /// [`add_late_key_event_handler`](Self::add_late_key_event_handler).
    pub fn remove_late_key_event_handler(
        self: Handle<Self>,
        app: &mut App,
        handler: &OnKeyEventCallback,
    ) {
        app.get(self)
            .highlight_manager
            .remove_late_key_event_handler(app, handler);
    }

    /// The root [`FocusScopeNode`] in the focus tree.
    ///
    /// This field is rarely used directly. To find the nearest [`FocusScopeNode`] for a given
    /// [`FocusNode`], call [`AnyFocusNode::nearest_scope`].
    pub fn root_scope(self: Handle<Self>, app: &App) -> &Ref<FocusScopeNode> {
        &app.get(self).root_scope
    }

    /// The node that currently has the primary focus.
    pub fn primary_focus(self: Handle<Self>, app: &App) -> Option<AnyFocusNode> {
        app.get(self).primary_focus.handle()
    }

    fn app_lifecycle_change(self: Handle<Self>, app: &mut App, state: AppLifecycleState) {
        let root_scope: FocusNodeRef = self.root_scope(app).erase();
        let root_has_primary_focus = self.primary_focus(app) == Some(root_scope.handle());
        if state == AppLifecycleState::Resumed {
            if !root_has_primary_focus {
                // The focus changed while the app was paused: the suspended node is ignored.
                app.get_mut(self).suspended_node = None;
            } else if let Some(suspended_node) = app.get(self).suspended_node.handle() {
                // Only restore the focus that was suspended when the app went inactive if
                // nothing else has requested focus in the meantime. For example, activating a
                // different window will cause its view to request focus.
                if app.get(self).marked_for_focus.is_none() {
                    suspended_node.request_focus(app, None);
                    app.get_mut(self).suspended_node = None;
                } else {
                    debug_assert!(app.get(self).have_scheduled_update);
                    app.get_mut(self).suspended_node = None;
                }
            }
        } else if !root_has_primary_focus {
            let manager = app.get_mut(self);
            manager.marked_for_focus = Some(root_scope);
            manager.suspended_node = manager.primary_focus.clone();
            self.apply_focus_changes_if_needed(app);
        }
    }

    fn mark_detached(self: Handle<Self>, app: &mut App, node: AnyFocusNode) {
        // The node has been removed from the tree, so it no longer needs to be notified of
        // changes.
        let manager = app.get_mut(self);
        if manager.primary_focus.handle() == Some(node) {
            manager.primary_focus = None;
        }
        if manager.suspended_node.handle() == Some(node) {
            manager.suspended_node = None;
        }
        manager.dirty_nodes.retain(|dirty| dirty != node);
    }

    fn mark_properties_changed(self: Handle<Self>, app: &mut App, node: AnyFocusNode) {
        self.mark_needs_update(app);
        add_dirty_node(app, self, node);
    }

    fn mark_next_focus(self: Handle<Self>, app: &mut App, node: AnyFocusNode) {
        if self.primary_focus(app) == Some(node) {
            // The caller asked for the current focus to be the next focus, so just pretend that
            // didn't happen.
            app.get_mut(self).marked_for_focus = None;
        } else {
            let node = node.keep(app);
            app.get_mut(self).marked_for_focus = Some(node);
            self.mark_needs_update(app);
        }
    }

    /// Request that an update be scheduled, optionally requesting focus for the given newFocus
    /// node.
    fn mark_needs_update(self: Handle<Self>, app: &mut App) {
        if app.get(self).have_scheduled_update {
            return;
        }
        app.get_mut(self).have_scheduled_update = true;
        app.schedule_microtask(Listener::handle_method(
            self,
            FocusManager::apply_focus_changes_if_needed,
        ));
    }

    /// Applies any pending focus changes and notifies listeners that the focus has changed.
    ///
    /// Must not be called during the build phase. This method is meant to be called in a
    /// post-frame callback or microtask when the pending focus changes need to be resolved
    /// before something else occurs.
    ///
    /// It can't be called during the build phase because not all listeners are safe to be
    /// called with an update during a build.
    ///
    /// Typically, this is called automatically by the [`FocusManager`], but sometimes it is
    /// necessary to ensure that no focus changes are pending before executing an action.
    ///
    /// It is safe to call this if no focus changes are pending.
    pub fn apply_focus_changes_if_needed(self: Handle<Self>, app: &mut App) {
        debug_assert!(
            SchedulerBinding::scheduler_phase(app) != SchedulerPhase::PersistentCallbacks,
            "applyFocusChangesIfNeeded() should not be called during the build phase."
        );

        app.get_mut(self).have_scheduled_update = false;
        let previous_focus = self.primary_focus(app);

        for autofocus in std::mem::take(&mut app.get_mut(self).pending_autofocuses) {
            autofocus.apply_if_valid(app, self);
        }

        if app.get(self).primary_focus.is_none() && app.get(self).marked_for_focus.is_none() {
            // If we don't have any current focus, and nobody has asked to focus yet, then
            // revert to the root scope.
            let root_scope: FocusNodeRef = self.root_scope(app).erase();
            app.get_mut(self).marked_for_focus = Some(root_scope);
        }
        // A node has requested to be the next focus, and isn't already the primary focus.
        let marked_for_focus = app.get(self).marked_for_focus.clone();
        if let Some(marked_for_focus) = marked_for_focus
            && Some(marked_for_focus.handle()) != self.primary_focus(app)
        {
            let previous_path = match previous_focus {
                Some(previous) => previous.ancestors(app),
                None => Vec::new(),
            };
            let next_path = marked_for_focus.ancestors(app);
            // Notify nodes that are newly focused.
            for node in next_path
                .iter()
                .filter(|node| !previous_path.contains(node))
            {
                add_dirty_node(app, self, *node);
            }
            // Notify nodes that are no longer focused.
            for node in previous_path
                .iter()
                .filter(|node| !next_path.contains(node))
            {
                add_dirty_node(app, self, *node);
            }

            let manager = app.get_mut(self);
            manager.primary_focus = Some(marked_for_focus);
            manager.marked_for_focus = None;
        }
        debug_assert!(app.get(self).marked_for_focus.is_none());
        let primary_focus = self.primary_focus(app);
        if previous_focus != primary_focus {
            if let Some(previous_focus) = previous_focus {
                add_dirty_node(app, self, previous_focus);
            }
            if let Some(primary_focus) = primary_focus {
                add_dirty_node(app, self, primary_focus);
            }
        }
        for node in std::mem::take(&mut app.get_mut(self).dirty_nodes) {
            node.notify(app);
        }
        if previous_focus != primary_focus {
            self.notify_listeners(app);
        }
    }
}

/// Dart's `_dirtyNodes.add(node)`: a set that keeps insertion order.
fn add_dirty_node(app: &mut App, manager: Handle<FocusManager>, node: AnyFocusNode) {
    if app
        .get(manager)
        .dirty_nodes
        .iter()
        .all(|dirty| dirty != node)
    {
        let node = node.keep(app);
        app.get_mut(manager).dirty_nodes.push(node);
    }
}

impl ChangeNotifier for FocusManager {
    fn change_notifier_data(&self) -> &ChangeNotifierData {
        &self.change_notifier
    }

    fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
        &mut self.change_notifier
    }
}

// ---------------------------------------------------------------------------------------------
// The highlight mode manager

/// A class to detect and manage the highlight mode transitions. An instance of this is owned by
/// the [`FocusManager`].
///
/// This doesn't hold a `ChangeNotifierData` because the callback passes the updated value, and
/// `ChangeNotifier` requires using a plain [`Listener`].
struct HighlightModeManager {
    /// If `None`, no interactions have occurred yet and the default highlight mode for the
    /// current platform applies.
    last_interaction_requires_traditional_highlights: Option<bool>,
    highlight_mode: Option<FocusHighlightMode>,
    strategy: FocusHighlightStrategy,
    /// The list of callbacks for early key handling.
    early_key_event_handlers: Vec<OnKeyEventCallback>,
    /// The list of callbacks for late key handling.
    late_key_event_handlers: Vec<OnKeyEventCallback>,
    /// The list of listeners for `highlight_mode` state changes.
    listeners: Vec<HighlightModeListener>,
    /// The handler `register_global_handlers` gave `HardwareKeyboard`, kept so that
    /// `dispose` can hand the same `Rc` back.
    key_event_handler: Option<KeyEventCallback>,
}

impl HighlightModeManager {
    fn new(app: &mut App) -> Ref<HighlightModeManager> {
        app.create(HighlightModeManager {
            last_interaction_requires_traditional_highlights: None,
            highlight_mode: None,
            strategy: FocusHighlightStrategy::default(),
            early_key_event_handlers: Vec::new(),
            late_key_event_handlers: Vec::new(),
            listeners: Vec::new(),
            key_event_handler: None,
        })
    }

    fn highlight_mode(self: Handle<Self>, app: &mut App) -> FocusHighlightMode {
        match app.get(self).highlight_mode {
            Some(mode) => mode,
            None => HighlightModeManager::default_mode_for_platform(app),
        }
    }

    fn set_strategy(self: Handle<Self>, app: &mut App, value: FocusHighlightStrategy) {
        if app.get(self).strategy == value {
            return;
        }
        app.get_mut(self).strategy = value;
        self.update_mode(app);
    }

    fn add_early_key_event_handler(
        self: Handle<Self>,
        app: &mut App,
        callback: OnKeyEventCallback,
    ) {
        app.get_mut(self).early_key_event_handlers.push(callback);
    }

    fn remove_early_key_event_handler(
        self: Handle<Self>,
        app: &mut App,
        callback: &OnKeyEventCallback,
    ) {
        app.get_mut(self)
            .early_key_event_handlers
            .retain(|handler| !Rc::ptr_eq(handler, callback));
    }

    fn add_late_key_event_handler(self: Handle<Self>, app: &mut App, callback: OnKeyEventCallback) {
        app.get_mut(self).late_key_event_handlers.push(callback);
    }

    fn remove_late_key_event_handler(
        self: Handle<Self>,
        app: &mut App,
        callback: &OnKeyEventCallback,
    ) {
        app.get_mut(self)
            .late_key_event_handlers
            .retain(|handler| !Rc::ptr_eq(handler, callback));
    }

    fn add_listener(self: Handle<Self>, app: &mut App, listener: HighlightModeListener) {
        app.get_mut(self).listeners.push(listener);
    }

    fn remove_listener(self: Handle<Self>, app: &mut App, listener: &HighlightModeListener) {
        app.get_mut(self)
            .listeners
            .retain(|registered| !Rc::ptr_eq(registered, listener));
    }

    fn register_global_handlers(self: Handle<Self>, app: &mut App) {
        let handler: KeyEventCallback =
            Rc::new(move |app: &mut App, event: &KeyEvent| self.handle_key_event(app, event));
        app.get_mut(self).key_event_handler = Some(Rc::clone(&handler));
        HardwareKeyboard::instance(app).add_handler(app, handler);
        let router = GestureBinding::instance(app).pointer_router(app);
        router.add_global_route(
            app,
            PointerRoute::handle_method(self, HighlightModeManager::handle_pointer_event),
            None,
        );
        // Dart also listens for semantics actions here; accessibility waits.
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        if let Some(handler) = app.get_mut(self).key_event_handler.take() {
            let router = GestureBinding::instance(app).pointer_router(app);
            router.remove_global_route(
                app,
                &PointerRoute::handle_method(self, HighlightModeManager::handle_pointer_event),
            );
            HardwareKeyboard::instance(app).remove_handler(app, &handler);
        }
        app.get_mut(self).listeners = Vec::new();
    }

    fn notify_listeners(self: Handle<Self>, app: &mut App) {
        if app.get(self).listeners.is_empty() {
            return;
        }
        let highlight_mode = self.highlight_mode(app);
        // Make a copy to prevent problems if the list is modified during iteration.
        let local_listeners = app.get(self).listeners.clone();
        for listener in local_listeners {
            let still_registered = app
                .get(self)
                .listeners
                .iter()
                .any(|registered| Rc::ptr_eq(registered, &listener));
            if still_registered {
                listener(app, highlight_mode);
            }
        }
    }

    fn handle_pointer_event(self: Handle<Self>, app: &mut App, event: PointerEvent) {
        match event.kind() {
            PointerDeviceKind::Touch
            | PointerDeviceKind::Stylus
            | PointerDeviceKind::InvertedStylus => {
                if app
                    .get(self)
                    .last_interaction_requires_traditional_highlights
                    != Some(true)
                {
                    app.get_mut(self)
                        .last_interaction_requires_traditional_highlights = Some(true);
                    self.update_mode(app);
                }
            }
            PointerDeviceKind::Mouse | PointerDeviceKind::Trackpad | PointerDeviceKind::Unknown => {
            }
        }
    }

    /// Dart's `handleKeyMessage`: the handler `HardwareKeyboard` calls for every key event.
    fn handle_key_event(self: Handle<Self>, app: &mut App, event: &KeyEvent) -> bool {
        // Update highlight_mode first, since things responding to the keys might look at the
        // highlight mode, and it should be accurate.
        if app
            .get(self)
            .last_interaction_requires_traditional_highlights
            != Some(false)
        {
            app.get_mut(self)
                .last_interaction_requires_traditional_highlights = Some(false);
            self.update_mode(app);
        }

        if FocusManager::instance(app).primary_focus(app).is_none() {
            return false;
        }

        let mut handled = false;
        // Check to see if any of the early handlers handle the key. If so, then return early.
        if !app.get(self).early_key_event_handlers.is_empty() {
            // Make a copy to prevent problems if the list is modified during iteration.
            let callbacks = app.get(self).early_key_event_handlers.clone();
            let results: Vec<KeyEventResult> = callbacks
                .into_iter()
                .map(|callback| callback(app, event))
                .collect();
            match combine_key_event_results(results) {
                KeyEventResult::Ignored => {}
                KeyEventResult::Handled => handled = true,
                KeyEventResult::SkipRemainingHandlers => handled = false,
            }
        }
        if handled {
            return true;
        }

        // Walk the current focus from the leaf to the root, calling each node's on_key_event on
        // the way up, and if one responds that they handled it or want to stop propagation,
        // stop.
        let primary_focus = FocusManager::instance(app)
            .primary_focus(app)
            .expect("checked above");
        let mut nodes = vec![primary_focus];
        nodes.extend(primary_focus.ancestors(app));
        for node in nodes {
            let results: Vec<KeyEventResult> = match node.on_key_event(app) {
                Some(on_key_event) => vec![on_key_event(app, node, event)],
                None => Vec::new(),
            };
            let result = combine_key_event_results(results);
            match result {
                KeyEventResult::Ignored => continue,
                KeyEventResult::Handled => handled = true,
                KeyEventResult::SkipRemainingHandlers => handled = false,
            }
            // Only KeyEventResult::Ignored will continue the loop. All other options will stop
            // the event propagation.
            debug_assert!(result != KeyEventResult::Ignored);
            break;
        }

        // Check to see if any late key event handlers want to handle the event.
        if !handled && !app.get(self).late_key_event_handlers.is_empty() {
            // Make a copy to prevent problems if the list is modified during iteration.
            let callbacks = app.get(self).late_key_event_handlers.clone();
            let results: Vec<KeyEventResult> = callbacks
                .into_iter()
                .map(|callback| callback(app, event))
                .collect();
            match combine_key_event_results(results) {
                KeyEventResult::Ignored => {}
                KeyEventResult::Handled => handled = true,
                KeyEventResult::SkipRemainingHandlers => handled = false,
            }
        }
        handled
    }

    /// Update function to be called whenever the state relating to `highlight_mode` changes.
    fn update_mode(self: Handle<Self>, app: &mut App) {
        let new_mode = match app.get(self).strategy {
            FocusHighlightStrategy::Automatic => {
                match app
                    .get(self)
                    .last_interaction_requires_traditional_highlights
                {
                    // If we don't have any information about the last interaction yet, then just
                    // rely on the default value for the platform, which will be determined based
                    // on the target platform if `highlight_mode` is not set.
                    None => return,
                    Some(true) => FocusHighlightMode::Touch,
                    Some(false) => FocusHighlightMode::Traditional,
                }
            }
            FocusHighlightStrategy::AlwaysTouch => FocusHighlightMode::Touch,
            FocusHighlightStrategy::AlwaysTraditional => FocusHighlightMode::Traditional,
        };
        // We can't just compare new_mode with `highlight_mode` here, since `highlight_mode`
        // could be unset, so we want to compare with the return value for the getter, since
        // that's what clients will be looking at.
        let old_mode = self.highlight_mode(app);
        app.get_mut(self).highlight_mode = Some(new_mode);
        if self.highlight_mode(app) != old_mode {
            self.notify_listeners(app);
        }
    }

    fn default_mode_for_platform(app: &mut App) -> FocusHighlightMode {
        // Assume that if we're on one of the mobile platforms, and there's no mouse connected,
        // that the initial interaction will be touch-based, and that it's traditional mouse and
        // keyboard on all other platforms.
        //
        // This only affects the initial value: the ongoing value is updated to a known correct
        // value as soon as any pointer/keyboard events are received.
        match app.platform().target_platform() {
            TargetPlatform::Android | TargetPlatform::Fuchsia | TargetPlatform::IOS => {
                let mouse_tracker = RendererBinding::instance(app).mouse_tracker(app);
                if mouse_tracker.mouse_is_connected(app) {
                    FocusHighlightMode::Traditional
                } else {
                    FocusHighlightMode::Touch
                }
            }
            TargetPlatform::Linux | TargetPlatform::MacOS | TargetPlatform::Windows => {
                FocusHighlightMode::Traditional
            }
        }
    }
}

/// Provides convenient access to the current [`FocusManager::primary_focus`] from the
/// `WidgetsBinding` instance.
pub fn primary_focus(app: &mut App) -> Option<AnyFocusNode> {
    let manager = WidgetsBinding::instance(app).focus_manager(app);
    manager.primary_focus(app)
}

// ---------------------------------------------------------------------------------------------
// TraversalEdgeBehavior

/// Controls the transfer of focus beyond the first and the last items of a
/// [`FocusScopeNode`].
///
/// This enumeration only controls the traversal behavior performed by
/// `FocusTraversalPolicy`. Other methods of focus transfer, such as direct calls to
/// [`AnyFocusNode::request_focus`] and [`AnyFocusNode::unfocus`], are not affected by this
/// enumeration.
///
/// Dart declares this in `focus_traversal.dart`; it lives here, where [`FocusScopeNode`] reads
/// it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TraversalEdgeBehavior {
    /// Keeps the focus among the items of the focus scope.
    ///
    /// Transfer focus to the edge node in the opposite direction of [`FocusScopeNode`] as the
    /// edge node continues to move, thus forming a closed loop of focusable items.
    #[default]
    ClosedLoop,

    /// Allows the focus to leave the `FlutterView`.
    ///
    /// Requesting next focus after the last focusable item or previous to the first item will
    /// unfocus any focused nodes.
    LeaveFlutterView,

    /// Allows focus to traverse up to parent scope.
    ///
    /// When reaching the edge of the current scope, requesting the next focus will look up to
    /// the parent scope of the current scope and focus the focus node next to the current
    /// scope.
    ///
    /// If there is no parent scope above the current scope, fallback to
    /// [`ClosedLoop`](Self::ClosedLoop) behavior.
    ParentScope,

    /// Stops the focus traversal at the edge of the focus scope.
    ///
    /// Keeps the focus in its current position when it reaches the edge of a focus scope.
    Stop,
}

#[cfg(test)]
pub(crate) mod tests {
    use std::cell::{Cell, RefCell};
    use std::time::Duration;

    use waft_embedder::{
        Matrix4, Rect, Size, SystemMouseCursorKind, TextEditingValue, TextInputConfiguration,
        TextInputHost, ViewFocusDirection, ViewFocusState, ViewHost,
    };
    use waft_services::{KeyDownEvent, LogicalKeyboardKey, PhysicalKeyboardKey, ServicesBinding};
    use waft_test::TestPlatform;

    use super::*;
    use crate::framework::IntoWidget;
    use crate::testing::{TargetPlatformVariant, WidgetTester, test_widgets, test_widgets_variant};
    use crate::widgets::basic::{Builder, SizedBox};

    /// A view's host whose text input answers whether it edits keys itself, and whether it
    /// provides Edit's commands.
    struct EditingHost {
        handles_editing_keys: bool,
        provides_edit_commands: bool,
    }

    impl TextInputHost for EditingHost {
        fn set_client(&self, _configuration: &TextInputConfiguration) {}

        fn show(&self) {}

        fn update_config(&self, _configuration: &TextInputConfiguration) {}

        fn clear_client(&self) {}

        fn hide(&self) {}

        fn set_editing_state(&self, _value: &TextEditingValue) {}

        fn set_composing_rect(&self, _rect: Rect) {}

        fn set_caret_rect(&self, _rect: Rect) {}

        fn set_client_geometry(&self, _size: Size, _transform: &Matrix4) {}

        fn handles_editing_keys(&self) -> bool {
            self.handles_editing_keys
        }

        fn provides_edit_commands(&self) -> bool {
            self.provides_edit_commands
        }
    }

    impl ViewHost for EditingHost {
        fn text_input(&self) -> Option<&dyn TextInputHost> {
            Some(self)
        }

        fn activate_system_cursor(&self, _device: i64, _kind: SystemMouseCursorKind) {}

        fn request_focus_change(&self, _state: ViewFocusState, _direction: ViewFocusDirection) {}
    }

    /// A tester on a macOS host whose view's text input `host` answers for.
    fn tester_on_mac_with(host: EditingHost) -> WidgetTester {
        WidgetTester::with_view(
            TestPlatform::new().on(TargetPlatform::MacOS),
            waft_test::TestView::with_pixel_ratio(
                WidgetTester::SIZE.width() * WidgetTester::DEVICE_PIXEL_RATIO,
                WidgetTester::SIZE.height() * WidgetTester::DEVICE_PIXEL_RATIO,
                WidgetTester::DEVICE_PIXEL_RATIO,
            )
            .with_host(Rc::new(host)),
        )
    }

    /// A tester over a macOS host that turns editing keys into edits itself, as Flutter's macOS
    /// and iOS embedders do.
    pub(crate) fn tester_whose_host_edits() -> WidgetTester {
        tester_on_mac_with(EditingHost {
            handles_editing_keys: true,
            provides_edit_commands: false,
        })
    }

    /// A tester over a macOS host that reports plain keys and provides Edit's commands, as Waft's
    /// macOS host does with its Edit menu.
    pub(crate) fn tester_whose_host_provides_edit_commands() -> WidgetTester {
        tester_on_mac_with(EditingHost {
            handles_editing_keys: false,
            provides_edit_commands: true,
        })
    }

    /// Pumps a tree that is one `Builder`, and answers that builder's context.
    fn pump_builder(tester: &mut WidgetTester) -> BuildContext {
        let captured: Rc<Cell<Option<BuildContext>>> = Rc::default();
        let sink = Rc::clone(&captured);
        tester.pump_widget(Builder::new(move |_app, context| {
            sink.set(Some(context));
            SizedBox::shrink().into_widget()
        }));
        captured.get().expect("the builder ran")
    }

    fn key_a_down() -> KeyEvent {
        KeyEvent::Down(KeyDownEvent::new(
            PhysicalKeyboardKey::KEY_A,
            LogicalKeyboardKey::KEY_A,
            Duration::ZERO,
        ))
    }

    /// Attaches `node` under `parent`, the way a host widget's `initState` would.
    fn attach_under(
        app: &mut App,
        node: AnyFocusNode,
        context: BuildContext,
        parent: AnyFocusNode,
    ) -> Ref<FocusAttachment> {
        let attachment = node.attach(app, Some(context), None);
        attachment.reparent(app, Some(parent));
        attachment
    }

    #[test]
    fn a_node_attached_under_a_scope_gains_primary_focus_on_request_focus() {
        test_widgets(|tester| {
            let context = pump_builder(tester);
            let mut app = tester.cell.borrow_mut();
            let manager = FocusManager::instance(&mut app);
            let root = manager.root_scope(&app).handle();
            let scope = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope.as_node(), context, root.as_node());
            let node = FocusNode::new(&mut app);
            attach_under(&mut app, node.as_node(), context, scope.as_node());

            node.request_focus(&mut app, None);
            // The node is notified in a microtask, so the manager still has the old focus.
            assert!(!node.has_primary_focus(&app));

            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(node.as_node()));
            assert!(node.has_primary_focus(&app));
            assert!(node.has_focus(&mut app));
            assert!(scope.has_focus(&mut app));
            assert!(!scope.has_primary_focus(&app));
            assert_eq!(scope.focused_child(&mut app), Some(node.as_node()));
        });
    }

    #[test]
    fn unfocus_moves_the_focus_according_to_its_disposition() {
        test_widgets(|tester| {
            let context = pump_builder(tester);
            let mut app = tester.cell.borrow_mut();
            let manager = FocusManager::instance(&mut app);
            let root = manager.root_scope(&app).handle();
            let scope = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope.as_node(), context, root.as_node());
            let child1 = FocusNode::new(&mut app);
            attach_under(&mut app, child1.as_node(), context, scope.as_node());
            let child2 = FocusNode::new(&mut app);
            attach_under(&mut app, child2.as_node(), context, scope.as_node());

            child1.request_focus(&mut app, None);
            app.drain_microtasks();
            child2.request_focus(&mut app, None);
            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(child2.as_node()));

            // `previouslyFocusedChild` walks back down to the child focused before this one.
            child2.unfocus(&mut app, UnfocusDisposition::PreviouslyFocusedChild);
            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(child1.as_node()));

            // `scope` clears the history and parks the focus on the enclosing scope.
            child1.unfocus(&mut app, UnfocusDisposition::Scope);
            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(scope.as_node()));
            assert_eq!(scope.focused_child(&mut app), None);
        });
    }

    #[test]
    fn set_first_focus_adopts_the_scope_and_focuses_it_with_its_parent() {
        test_widgets(|tester| {
            let context = pump_builder(tester);
            let mut app = tester.cell.borrow_mut();
            let manager = FocusManager::instance(&mut app);
            let root = manager.root_scope(&app).handle();
            let scope1 = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope1.as_node(), context, root.as_node());
            let scope2 = FocusScopeNode::new(&mut app);

            scope1.set_first_focus(&mut app, scope2.handle());
            assert_eq!(scope2.parent(&app), Some(scope1.as_node()));
            assert_eq!(scope1.focused_child(&mut app), Some(scope2.as_node()));
            assert!(!scope2.has_focus(&mut app));

            scope1.request_focus(&mut app, None);
            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(scope2.as_node()));
        });
    }

    #[test]
    fn a_node_that_cannot_request_focus_refuses_the_request() {
        test_widgets(|tester| {
            let context = pump_builder(tester);
            let mut app = tester.cell.borrow_mut();
            let manager = FocusManager::instance(&mut app);
            let root = manager.root_scope(&app).handle();
            let scope = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope.as_node(), context, root.as_node());
            let node = FocusNode::new(&mut app);
            attach_under(&mut app, node.as_node(), context, scope.as_node());
            node.set_can_request_focus(&mut app, false);

            node.request_focus(&mut app, None);
            app.drain_microtasks();
            assert!(!node.has_primary_focus(&app));
            // Nothing asked for the focus, so it reverted to the root scope.
            assert_eq!(manager.primary_focus(&app), Some(root.as_node()));
        });
    }

    #[test]
    fn descendants_are_focusable_false_unfocuses_and_blocks_the_descendants() {
        test_widgets(|tester| {
            let context = pump_builder(tester);
            let mut app = tester.cell.borrow_mut();
            let manager = FocusManager::instance(&mut app);
            let root = manager.root_scope(&app).handle();
            let scope = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope.as_node(), context, root.as_node());
            let node = FocusNode::new(&mut app);
            attach_under(&mut app, node.as_node(), context, scope.as_node());

            node.request_focus(&mut app, None);
            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(node.as_node()));

            // Dart unfocuses the node the flag was set on, so the focus leaves the scope for the
            // nearest focusable enclosing scope: the root.
            scope.set_descendants_are_focusable(&mut app, false);
            app.drain_microtasks();
            assert!(!scope.descendants_are_focusable(&app));
            assert!(!node.can_request_focus(&mut app));
            assert_eq!(manager.primary_focus(&app), Some(root.as_node()));

            node.request_focus(&mut app, None);
            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(root.as_node()));
        });
    }

    #[test]
    fn a_key_event_walks_from_the_primary_focus_to_the_root_until_one_handles_it() {
        test_widgets(|tester| {
            let context = pump_builder(tester);
            let mut app = tester.cell.borrow_mut();
            let manager = FocusManager::instance(&mut app);
            let root = manager.root_scope(&app).handle();
            let scope = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope.as_node(), context, root.as_node());
            let node = FocusNode::new(&mut app);
            attach_under(&mut app, node.as_node(), context, scope.as_node());
            node.request_focus(&mut app, None);
            app.drain_microtasks();

            let log: Rc<RefCell<Vec<&'static str>>> = Rc::default();
            let result: Rc<Cell<KeyEventResult>> = Rc::new(Cell::new(KeyEventResult::Ignored));
            {
                let log = Rc::clone(&log);
                let result = Rc::clone(&result);
                node.set_on_key_event(
                    &mut app,
                    Some(Rc::new(move |_app, _node, _event| {
                        log.borrow_mut().push("node");
                        result.get()
                    })),
                );
            }
            {
                let log = Rc::clone(&log);
                scope.set_on_key_event(
                    &mut app,
                    Some(Rc::new(move |_app, _node, _event| {
                        log.borrow_mut().push("scope");
                        KeyEventResult::Ignored
                    })),
                );
            }
            {
                let log = Rc::clone(&log);
                root.set_on_key_event(
                    &mut app,
                    Some(Rc::new(move |_app, _node, _event| {
                        log.borrow_mut().push("root");
                        KeyEventResult::Ignored
                    })),
                );
            }

            let keyboard = HardwareKeyboard::instance(&mut app);
            assert!(!keyboard.handle_key_event(&mut app, &key_a_down()));
            assert_eq!(*log.borrow(), vec!["node", "scope", "root"]);

            log.borrow_mut().clear();
            result.set(KeyEventResult::Handled);
            assert!(keyboard.handle_key_event(&mut app, &key_a_down()));
            assert_eq!(*log.borrow(), vec!["node"]);
        });
    }

    /// The host reports `state`, as Dart's test sends it on `flutter/lifecycle`.
    fn set_app_lifecycle_state(app: &mut App, state: AppLifecycleState) {
        ServicesBinding::instance(app).handle_lifecycle_message(app, state);
    }

    /// The set-up the lifecycle tests share: a node in a scope under the root scope, with the
    /// primary focus.
    fn focused_node(tester: &mut WidgetTester) -> (Ref<FocusNode>, Ref<FocusAttachment>) {
        let context = pump_builder(tester);
        let (node, attachment) = {
            let mut app = tester.cell.borrow_mut();
            let root = FocusManager::instance(&mut app).root_scope(&app).handle();
            let scope = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope.as_node(), context, root.as_node());
            let node = FocusNode::new(&mut app);
            let attachment = attach_under(&mut app, node.as_node(), context, scope.as_node());
            node.request_focus(&mut app, None);
            app.drain_microtasks();
            assert!(node.has_primary_focus(&app));
            (node, attachment)
        };
        (node, attachment)
    }

    #[test]
    fn focus_manager_ignores_app_lifecycle_changes_on_android_and_ios() {
        test_widgets_variant(
            TargetPlatformVariant::new([TargetPlatform::Android, TargetPlatform::IOS]),
            |tester| {
                let (node, _attachment) = focused_node(tester);
                let mut app = tester.cell.borrow_mut();

                set_app_lifecycle_state(&mut app, AppLifecycleState::Paused);
                app.drain_microtasks();
                assert!(node.has_primary_focus(&app));

                set_app_lifecycle_state(&mut app, AppLifecycleState::Resumed);
                app.drain_microtasks();
                assert!(node.has_primary_focus(&app));
            },
        );
    }

    #[test]
    fn focus_manager_responds_to_app_lifecycle_changes() {
        test_widgets_variant(TargetPlatformVariant::desktop(), |tester| {
            let (node, _attachment) = focused_node(tester);
            let mut app = tester.cell.borrow_mut();

            set_app_lifecycle_state(&mut app, AppLifecycleState::Paused);
            app.drain_microtasks();
            assert!(!node.has_primary_focus(&app));

            set_app_lifecycle_state(&mut app, AppLifecycleState::Resumed);
            app.drain_microtasks();
            assert!(node.has_primary_focus(&app));
        });
    }

    #[test]
    fn node_is_removed_completely_even_if_app_is_paused() {
        test_widgets(|tester| {
            let (node, attachment) = focused_node(tester);
            let mut app = tester.cell.borrow_mut();

            set_app_lifecycle_state(&mut app, AppLifecycleState::Paused);
            app.drain_microtasks();
            attachment.detach(&mut app);
            assert!(!node.has_primary_focus(&app));

            set_app_lifecycle_state(&mut app, AppLifecycleState::Resumed);
            app.drain_microtasks();
            assert!(!node.has_primary_focus(&app));
        });
    }

    #[test]
    fn suspended_focus_is_not_restored_on_resume_when_another_node_claimed_focus() {
        test_widgets_variant(TargetPlatformVariant::desktop(), |tester| {
            let context = pump_builder(tester);
            let mut app = tester.cell.borrow_mut();
            let root = FocusManager::instance(&mut app).root_scope(&app).handle();
            let scope = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope.as_node(), context, root.as_node());
            let node_a = FocusNode::new(&mut app);
            attach_under(&mut app, node_a.as_node(), context, scope.as_node());
            let node_b = FocusNode::new(&mut app);
            attach_under(&mut app, node_b.as_node(), context, scope.as_node());

            node_a.request_focus(&mut app, None);
            app.drain_microtasks();
            assert!(node_a.has_primary_focus(&app));

            // App goes inactive: node A's focus is suspended and parked.
            set_app_lifecycle_state(&mut app, AppLifecycleState::Inactive);
            app.drain_microtasks();
            assert!(!node_a.has_primary_focus(&app));

            // Another node requests focus before the app resumes (e.g. a different
            // window/view was activated).
            node_b.request_focus(&mut app, None);

            // On resume, the suspended node A must not steal focus back from node B.
            set_app_lifecycle_state(&mut app, AppLifecycleState::Resumed);
            app.drain_microtasks();
            assert!(node_b.has_primary_focus(&app));
            assert!(!node_a.has_primary_focus(&app));
        });
    }

    #[test]
    fn disposing_a_focused_node_moves_the_focus_to_its_scope() {
        test_widgets(|tester| {
            let context = pump_builder(tester);
            let mut app = tester.cell.borrow_mut();
            let manager = FocusManager::instance(&mut app);
            let root = manager.root_scope(&app).handle();
            let scope = FocusScopeNode::new(&mut app);
            attach_under(&mut app, scope.as_node(), context, root.as_node());
            let node = FocusNode::new(&mut app);
            attach_under(&mut app, node.as_node(), context, scope.as_node());
            node.request_focus(&mut app, None);
            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(node.as_node()));

            node.dispose(&mut app);
            assert_eq!(manager.primary_focus(&app), None);
            assert_eq!(node.parent(&app), None);

            app.drain_microtasks();
            assert_eq!(manager.primary_focus(&app), Some(scope.as_node()));
        });
    }

    /// Dart's `Events change focus highlight mode.`, its key and tap steps, on Android.
    #[test]
    fn the_highlight_mode_follows_the_last_interaction() {
        test_widgets(|tester| {
            tester.pump_widget(SizedBox::shrink());
            let seen: Rc<RefCell<Vec<FocusHighlightMode>>> = Rc::default();
            tester.update(|app| {
                let manager = FocusManager::instance(app);
                let seen = Rc::clone(&seen);
                manager.add_highlight_mode_listener(
                    app,
                    Rc::new(move |_app, mode| seen.borrow_mut().push(mode)),
                );
                manager.set_highlight_strategy(app, FocusHighlightStrategy::Automatic);
            });
            let highlight_mode = |tester: &WidgetTester| {
                tester.update(|app| FocusManager::instance(app).highlight_mode(app))
            };
            // Android with no interaction yet.
            assert_eq!(highlight_mode(tester), FocusHighlightMode::Touch);

            tester.send_key_event(
                PhysicalKeyboardKey::META_LEFT,
                LogicalKeyboardKey::META_LEFT,
            );
            assert_eq!(*seen.borrow(), vec![FocusHighlightMode::Traditional]);
            assert_eq!(highlight_mode(tester), FocusHighlightMode::Traditional);

            tester.tap_at(Offset::ZERO, 1);
            assert_eq!(
                *seen.borrow(),
                vec![FocusHighlightMode::Traditional, FocusHighlightMode::Touch]
            );
            assert_eq!(highlight_mode(tester), FocusHighlightMode::Touch);
        });
    }

    #[test]
    fn the_highlight_strategy_pins_the_mode() {
        test_widgets(|tester| {
            tester.pump_widget(SizedBox::shrink());
            let mut app = tester.cell.borrow_mut();
            let manager = FocusManager::instance(&mut app);
            manager.set_highlight_strategy(&mut app, FocusHighlightStrategy::AlwaysTouch);
            assert_eq!(
                manager.highlight_strategy(&app),
                FocusHighlightStrategy::AlwaysTouch
            );
            assert_eq!(manager.highlight_mode(&mut app), FocusHighlightMode::Touch);

            HardwareKeyboard::instance(&mut app).handle_key_event(&mut app, &key_a_down());
            assert_eq!(manager.highlight_mode(&mut app), FocusHighlightMode::Touch);
        });
    }

    #[test]
    fn combine_key_event_results_prefers_handled_then_skip() {
        assert_eq!(
            combine_key_event_results([KeyEventResult::Ignored, KeyEventResult::Ignored]),
            KeyEventResult::Ignored
        );
        assert_eq!(
            combine_key_event_results([
                KeyEventResult::Ignored,
                KeyEventResult::SkipRemainingHandlers
            ]),
            KeyEventResult::SkipRemainingHandlers
        );
        assert_eq!(
            combine_key_event_results([
                KeyEventResult::SkipRemainingHandlers,
                KeyEventResult::Handled
            ]),
            KeyEventResult::Handled
        );
    }
}