crcbl-ui 0.3.0

Crucible immediate-mode GUI
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
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
//! Menus: a centred window frame, skinned buttons inside it, and the keyboard
//! that drives them.
//!
//! ```text
//!                     ┌───────────────────────────┐
//!                     │         PAUSED            │   ← title
//!                     │  ┌─────────────────────┐  │
//!                     │  │ RESUME          ESC │  │   ← selected → Hovered
//!                     │  ├─────────────────────┤  │
//!                     │  │ FULLSCREEN      F11 │  │
//!                     │  └─────────────────────┘  │
//!                     └───────────────────────────┘
//!                       centred in the framebuffer
//! ```
//!
//! # What is here and what is not
//!
//! **Layout, selection and activation.** Every rectangle this module produces is
//! in screen pixels, Y **down**, the space the whole crate measures in — so a
//! menu is positioned, hit-tested and navigated with no device and no art in the
//! room, which is what lets the interesting properties (it is centred, its
//! corners do not move, the paused state does not also draw the start menu) be
//! asserted as arithmetic.
//!
//! **The menu is built on the element tree.** [`Menu::layout`] and
//! [`Menu::render`] build the same [`crate::tree`] of blocks and spans — a
//! screen, the scrim, the panel, the title and a row per item — lay it out on
//! Taffy and read the rectangles back or emit it, so the arithmetic that used to
//! place each label is flexbox now. The tree is a view: which row is selected,
//! held or hovered stays in this module's model, and each row is built in the
//! pseudo-class [`Menu::state`] derives, so the keyboard-first API below is
//! unchanged.
//!
//! **The skin is `default.css`'s.** The window frame and the button frames are
//! `border-image`s over the panel and the rows, which frame a row draws is its
//! `:hover` or `:active` rule, and every colour is a rule too. The pictures are
//! a [`MenuSkin`]'s registered images, which [`Menu::render`] binds under the
//! names the sheet's `url()`s use; the art itself is `crcbl-render`'s
//! (`crcbl_render::menu::menu_skin` registers it). The lengths stay
//! [`MenuStyle`]'s, set on the nodes inline, because they are a pixel-art scale
//! times a base metric and a stylesheet has no arithmetic to scale with; the
//! frame's insets reach the layout through them, and `crcbl-render`'s tests
//! read the insets back off the art so the layout and the picture cannot drift.
//!
//! # Why the whole menu is one type and not a `Vec<Button>`
//!
//! A menu has one property a pile of buttons does not: **it is centred as a
//! unit**. Its panel has to be measured before any button can be placed, because
//! the panel's width is the widest button's width and the buttons are centred in
//! the panel. Handing a caller three `Button`s and letting it do the arithmetic
//! is handing every sample the same arithmetic to get subtly differently wrong.
//!
//! # The keyboard is the primary input, and the pointer is optional
//!
//! The samples are played with the keyboard, so a menu that could only be
//! clicked would be a regression. The model here is keyboard-first:
//! [`Menu::select_next`]/[`Menu::select_previous`] move a **selection**, and the
//! selected item draws as [`ButtonState::Hovered`] — the same art the pointer
//! lights up, because "the thing that will happen if you commit" is the same
//! idea for both devices. [`Menu::press`] holds it at [`ButtonState::Pressed`]
//! while the key is down and [`Menu::activate`] fires it.
//!
//! The pointer is [`Menu::point`], is entirely optional, and when a cursor is
//! over an item it takes over the highlight for as long as it is there. A game
//! that never calls it behaves exactly as if there were no mouse.
//!
//! # Three kinds of row
//!
//! A row fires, or it carries a value — see [`MenuItemKind`]. A **button**
//! reports its id from the commit key or a click. A **slider** carries a
//! position along a groove and reports nothing; the arrows and a drag move it.
//! A **cycler** carries one choice of a fixed list and reports nothing either:
//! the arrows walk the list and stop at its ends, the commit key and a click
//! walk it forward and round. The two value rows are read back by id —
//! [`Menu::slider`] and [`Menu::cycler`] — rather than fired, so no value ever
//! reaches a game's action table looking like a button press.
//!
//! # Captions
//!
//! [`Menu::subtitle`] is lines of text under the title — the controls, a
//! warning — each a [`Caption`] in a [`CaptionTone`]: the hint's colour, or
//! the warning's. They are not rows: nothing selects, hovers or fires them,
//! so the selection, the pointer and every index into [`Menu::items`] are
//! exactly what they were without them, whatever their tone.
//!
//! # Fitting a small window, then scrolling
//!
//! [`Menu::layout_with_font_fitted`] lays a menu out in a registered font at
//! the largest size that fits the window, in two stages:
//!
//! 1. **Shrink.** Every font size and gap shrinks by one factor, in steps of
//!    [`FIT_FONT_STEP`], down to [`MenuStyle::MIN_FONT_SIZE`]; the art stays at
//!    a whole-number scale of at least one, so the frames' corners stay on
//!    whole pixels.
//! 2. **Scroll.** If even the smallest size is too tall, the item list is
//!    capped at the height left and scrolls; the title and the captions stay
//!    put. Only a menu too wide at the smallest size — the list scrolls up and
//!    down only — or too short to show one row is refused, with a
//!    [`MenuFitError`] saying which.
//!
//! A scrolled list follows the keyboard: a selection moved by
//! [`Menu::select_next`] or [`Menu::select_previous`] is scrolled into view by
//! the next layout. [`Menu::scroll_wheel`] moves it by the pointer's wheel
//! instead, until the keyboard next moves the selection. A row scrolled wholly
//! out of view is neither drawn nor hit.

use std::ops::Range;

use glam::Vec2;

use crate::draw_list::{DrawCommand, DrawList};
use crate::font::Font;
use crate::image::{AtlasImage, NineSliceImage};
use crate::style::{Declaration, PseudoClasses, Sides};
use crate::text::FontAtlas;
#[cfg(test)]
use crate::text::LINE_HEIGHT;
use crate::tree::{
    AvailableSpace, FamilyName, Length, LengthAuto, LineHeight, NodeKey, Overflow, Ui,
};
use crate::widget::{
    ButtonSkin, ButtonState, NATURAL_FONT_SIZE, PointerInput, SkinInsets, UiState, WidgetId,
};

// ---------------------------------------------------------------------------
// Skin
// ---------------------------------------------------------------------------

/// The pictures a menu is drawn with: a window frame, a button frame per state
/// and the scrim behind them, all registered in one [`ImageAtlas`](crate::ImageAtlas).
///
/// Every sample's menus use the one `crcbl-render` ships; see
/// `crcbl_render::menu` for why the art lives there.
///
/// [`Menu::render`] binds the panel and the three button images under the
/// names `default.css`'s menu rules give them, and the sheet cuts each at its
/// own `border-image-slice`: the insets a [`NineSliceImage`] carries are the
/// art's, which [`PANEL_INSETS`] and [`BUTTON_INSETS`] restate for the layout
/// and the sheet's slices are held to.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MenuSkin {
    /// The window frame, nine-sliced round the panel.
    pub panel: NineSliceImage,
    /// The button frames.
    pub buttons: ButtonSkin,
    /// Stretched over the whole framebuffer and tinted by the sheet's
    /// `.menu-scrim` colour, so the art decides its texture and the sheet how
    /// dark it gets.
    pub scrim: AtlasImage,
}

// ---------------------------------------------------------------------------
// Style
// ---------------------------------------------------------------------------

/// Every length a menu is laid out with, in **pixels**.
///
/// Built from a whole-number `scale` by [`MenuStyle::pixel_art`]: the art is
/// pixel art and the panel is drawn at one texel per `scale` device pixels, so
/// every gap, pad and font size is a base metric times the same integer. A
/// fractional scale would put a nine-slice's fixed corner on a half pixel, which
/// is the one thing `SampleMode::Pixel` cannot hide.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MenuStyle {
    /// Device pixels per texel of the menu art.
    pub scale: f32,
    /// The window frame's fixed corners, in pixels.
    pub panel: SkinInsets,
    /// A button skin's fixed corners, in pixels.
    pub button: SkinInsets,
    /// Space between the panel's frame and its contents.
    pub panel_padding: Vec2,
    /// Space between a button's frame and its label.
    pub button_padding: Vec2,
    /// Vertical space between two buttons.
    pub item_gap: f32,
    /// Vertical space between the title and the first button.
    pub title_gap: f32,
    /// The title's font size.
    pub title_size: f32,
    /// An item label's font size.
    pub item_size: f32,
    /// The least space between an item's label and its key hint.
    pub hint_gap: f32,
    /// The least width a [`Slider`] row's groove is given.
    ///
    /// A least, not a width: [`Menu::layout_with`] stretches the groove into
    /// whatever the panel's widest row left over, so a slider beside a long
    /// label is a long slider. This is what stops it collapsing when the slider
    /// row *is* the widest one.
    pub track_width: f32,
    /// How tall a slider's groove is drawn.
    pub track_height: f32,
    /// The handle's size. Its width is also the travel the groove loses at each
    /// end, so the handle stays inside the groove at both extremes.
    pub handle_size: Vec2,
    /// The title's colour.
    ///
    /// **This and the other colours restate `default.css`'s menu rules, which
    /// are what draw them**: a style with different colours lays out the same
    /// menu and draws it in the sheet's. They are here for a caller measuring
    /// what a menu drew — the scrim's alpha over a golden image, say — and
    /// `the_sheet_draws_the_colours_and_the_slices_the_style_restates` holds
    /// [`MenuStyle::pixel_art`]'s to the sheet's.
    pub title_color: [f32; 4],
    /// An item label's colour. See [`MenuStyle::title_color`].
    pub label_color: [f32; 4],
    /// A key hint's colour — dimmer than the label it belongs to — and a
    /// [`CaptionTone::Hint`] caption's. See [`MenuStyle::title_color`].
    pub hint_color: [f32; 4],
    /// A [`CaptionTone::Warning`] caption's colour: the console's amber for a
    /// warning, so a line that warns reads as one. See
    /// [`MenuStyle::title_color`].
    pub warning_color: [f32; 4],
    /// A slider groove's colour, behind the part not filled in. See
    /// [`MenuStyle::title_color`].
    pub track_color: [f32; 4],
    /// The colour of the groove up to the handle. See
    /// [`MenuStyle::title_color`].
    pub fill_color: [f32; 4],
    /// The handle's colour. See [`MenuStyle::title_color`].
    pub handle_color: [f32; 4],
    /// What the frame behind the menu is dimmed with. See
    /// [`MenuStyle::title_color`].
    pub scrim_color: [f32; 4],
}

/// The insets of the window frame `crcbl-render` ships, in **texels**.
///
/// The source figure the pixel corners come from: [`MenuStyle::pixel_art`] is
/// the only place texels become pixels, and it multiplies this by the scale
/// into [`MenuStyle::panel`]. Kept in texels so a menu can be laid out — and
/// the layout tested — with no renderer and no device, and checked against the
/// art by `crcbl_render::menu`'s
/// `the_shipped_art_has_the_insets_the_layout_assumes`, which reads the same
/// figure back off the registered skin and compares.
pub const PANEL_INSETS: SkinInsets = SkinInsets::new(4.0, 4.0, 4.0, 4.0);

/// The insets of the button skin `crcbl-render` ships, in **texels**.
///
/// The source figure [`MenuStyle::button`] is pre-multiplied from —
/// [`MenuStyle::pixel_art`] is the only place texels become pixels.
///
/// **Currently the same four numbers as [`PANEL_INSETS`], and a separate
/// constant anyway.** A `.crpix` carries one set of insets for the whole sheet
/// and the shipped art is one sheet, so the panel and the buttons have to agree
/// today; they describe different things and a redrawn sheet could split them
/// without touching a line of layout. `crcbl_render::menu`'s
/// `the_shipped_art_has_the_insets_the_layout_assumes` reads both back off the
/// art, so a split shows up as a failure rather than as a silently wrong label
/// position.
pub const BUTTON_INSETS: SkinInsets = SkinInsets::new(4.0, 4.0, 4.0, 4.0);

impl MenuStyle {
    /// The largest scale [`Menu::layout`] will choose.
    ///
    /// Four rather than unbounded: past it the title's glyphs are taller than a
    /// brick and the menu stops reading as part of the game.
    pub const MAX_SCALE: u32 = 4;

    /// The smallest item font size [`Menu::layout_with_font_fitted`] shrinks
    /// to, in pixels per em.
    ///
    /// Eight: a typical face's x-height is about half an em, so this leaves
    /// lowercase four pixels tall — the fewest the hinter can snap `e`, `c`
    /// and `o` apart in. A window that needs smaller text gets a scrolled
    /// list, or a [`MenuFitError`], rather than a menu nobody can read.
    pub const MIN_FONT_SIZE: f32 = 8.0;

    /// The shipped look, at `scale` device pixels per texel.
    ///
    /// `scale` is clamped to at least one — a menu drawn at zero is not a
    /// smaller menu, it is an invisible one.
    #[must_use]
    pub fn pixel_art(scale: u32) -> Self {
        let scale = scale.max(1) as f32;
        Self {
            scale,
            panel: scaled(PANEL_INSETS, scale),
            button: scaled(BUTTON_INSETS, scale),
            panel_padding: Vec2::new(8.0, 7.0) * scale,
            button_padding: Vec2::new(6.0, 3.0) * scale,
            item_gap: 4.0 * scale,
            title_gap: 8.0 * scale,
            // Twice the atlas's natural height, so the title is a heading rather
            // than a bolder line of the same text.
            title_size: NATURAL_FONT_SIZE * 2.0 * scale,
            item_size: NATURAL_FONT_SIZE * scale,
            hint_gap: 10.0 * scale,
            track_width: 48.0 * scale,
            track_height: 4.0 * scale,
            handle_size: Vec2::new(6.0, 12.0) * scale,
            title_color: [1.0, 0.94, 0.55, 1.0],
            label_color: [0.94, 0.95, 1.0, 1.0],
            hint_color: [0.62, 0.64, 0.82, 1.0],
            warning_color: [1.0, 0.78, 0.35, 1.0],
            // The groove is the darkest of the three and the handle the
            // brightest, so the three read as recess, level and grip without
            // any art behind them.
            track_color: [0.10, 0.11, 0.18, 1.0],
            fill_color: [0.42, 0.46, 0.72, 1.0],
            handle_color: [0.94, 0.95, 1.0, 1.0],
            // Two thirds, in straight alpha: enough that the panel's own dark
            // fill separates from the game behind it, not so much that the
            // player loses track of where the ball was.
            scrim_color: [0.0, 0.0, 0.0, 0.66],
        }
    }

    /// This style with its item font at `item_size`, and every other length
    /// shrunk by the same factor — what [`Menu::layout_with_font_fitted`]
    /// tries.
    ///
    /// The frames' corners are the art's texels at the whole-number scale the
    /// factor leaves, never below one, so the nine-slice stays on whole
    /// pixels. Every other length but the font sizes is rounded to a whole
    /// pixel, which with [`row_line`]'s whole line pitch makes every row a
    /// whole number of pixels tall — what lets a scrolled list build only the
    /// rows in view and still put them where the layout said.
    fn shrunk(&self, item_size: f32) -> Self {
        let factor = item_size / self.item_size;
        let whole = |length: f32| (length * factor).round();
        let art = (self.scale * factor).floor().max(1.0);
        let corners = |insets: SkinInsets| {
            let texels = |length: f32| (length / self.scale * art).round();
            SkinInsets::new(
                texels(insets.left),
                texels(insets.right),
                texels(insets.top),
                texels(insets.bottom),
            )
        };
        Self {
            scale: art,
            panel: corners(self.panel),
            button: corners(self.button),
            panel_padding: Vec2::new(whole(self.panel_padding.x), whole(self.panel_padding.y)),
            button_padding: Vec2::new(whole(self.button_padding.x), whole(self.button_padding.y)),
            item_gap: whole(self.item_gap),
            title_gap: whole(self.title_gap),
            title_size: self.title_size * factor,
            item_size,
            hint_gap: whole(self.hint_gap),
            track_width: whole(self.track_width),
            track_height: whole(self.track_height),
            handle_size: Vec2::new(whole(self.handle_size.x), whole(self.handle_size.y)),
            ..*self
        }
    }

    /// The panel's fixed corners, in device pixels.
    ///
    /// [`MenuStyle::panel`] itself: `pixel_art` pre-multiplies the texel
    /// source figure [`PANEL_INSETS`] by the scale, so the layout reads the
    /// pixel corners through this accessor the same way it always has.
    #[must_use]
    pub fn panel_corners(&self) -> SkinInsets {
        self.panel
    }

    /// A button's fixed corners, in device pixels.
    ///
    /// [`MenuStyle::button`] itself — see [`panel_corners`](Self::panel_corners).
    #[must_use]
    pub fn button_corners(&self) -> SkinInsets {
        self.button
    }
}

fn scaled(insets: SkinInsets, scale: f32) -> SkinInsets {
    SkinInsets::new(
        insets.left * scale,
        insets.right * scale,
        insets.top * scale,
        insets.bottom * scale,
    )
}

impl Default for MenuStyle {
    fn default() -> Self {
        Self::pixel_art(1)
    }
}

// ---------------------------------------------------------------------------
// The model
// ---------------------------------------------------------------------------

/// A value between zero and one that the player drags along a groove.
///
/// **Unitless on purpose.** A slider that knew it was showing an exposure, a
/// volume or a mouse sensitivity would need to know that exposure is a ratio
/// and volume is decibels, and it would be wrong for the next one. The caller
/// owns the mapping in both directions: it writes a position in with
/// [`Menu::set_slider`] and reads one back with [`Menu::slider`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Slider {
    position: f32,
}

impl Slider {
    /// How far one press of a menu key moves the handle, as a share of the
    /// groove.
    ///
    /// Twenty presses end to end, which is the granularity a player expects of
    /// a volume: fine enough to land where they meant and coarse enough to
    /// cross the range without holding a key down. A pointer drag is not
    /// quantised by this at all — it lands wherever the cursor is.
    pub const KEY_STEP: f32 = 0.05;

    /// A slider whose handle sits `position` of the way along, clamped into
    /// `0.0..=1.0`.
    #[must_use]
    pub fn new(position: f32) -> Self {
        Self {
            position: clamp_unit(position),
        }
    }

    /// Where the handle sits: `0.0` at the left end of the groove, `1.0` at the
    /// right. Always in that range.
    #[must_use]
    pub const fn position(&self) -> f32 {
        self.position
    }

    /// Moves the handle, clamped into `0.0..=1.0`.
    pub fn set_position(&mut self, position: f32) {
        self.position = clamp_unit(position);
    }
}

/// Into `0.0..=1.0`, with `NaN` landing at zero.
///
/// `f32::clamp` propagates a `NaN` rather than replacing it, and a `NaN`
/// position is a handle drawn nowhere and a caller handed back a number it
/// cannot map. Zero is the end of the groove, which is a place.
fn clamp_unit(position: f32) -> f32 {
    if position.is_nan() {
        0.0
    } else {
        position.clamp(0.0, 1.0)
    }
}

/// A row that holds one choice of a fixed, ordered list — a display mode, a
/// frame cap, an on/off switch.
///
/// It knows how many choices there are and which is chosen, and **not what
/// they are**: the caller owns the list and its spellings, and writes the
/// chosen one's caption in as the row's hint, exactly as a [`Slider`] leaves
/// the mapping from a position to a value with the caller. What the widget
/// owns is the stepping, and it has two rules because a player's hands have
/// two: the arrows walk the list and **stop at its ends** — [`Menu::nudge_cycler`],
/// which draws a chevron only on the side a step would go — and the commit
/// key or a click walks it **forward and round** — [`Menu::activate`] and
/// [`Menu::point`] — so one key reaches every choice, which is what a row that
/// was a button did before this existed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cycler {
    chosen: usize,
    count: usize,
}

impl Cycler {
    /// `count` choices with `chosen` picked, clamped to the last of them.
    ///
    /// No choices at all is a cycler with nothing chosen: [`Cycler::chosen`]
    /// reads zero and every step is refused, rather than a wrap that would
    /// divide by the count.
    #[must_use]
    pub fn new(count: usize, chosen: usize) -> Self {
        let mut cycler = Self { chosen: 0, count };
        cycler.choose(chosen);
        cycler
    }

    /// How many choices there are.
    #[must_use]
    pub const fn count(&self) -> usize {
        self.count
    }

    /// Which choice is picked: an index into the caller's list, always below
    /// [`Cycler::count`] unless that is zero.
    #[must_use]
    pub const fn chosen(&self) -> usize {
        self.chosen
    }

    /// Picks a choice, clamped to the last one.
    pub fn choose(&mut self, index: usize) {
        self.chosen = index.min(self.count.saturating_sub(1));
    }

    /// Steps one choice along, and reports whether the choice changed.
    ///
    /// Without `wrap` a step past either end stays at that end; with it the
    /// step goes round. `false` means the choice is where it was, which at an
    /// end without `wrap` is the ordinary way a held key arrives.
    pub fn step(&mut self, forward: bool, wrap: bool) -> bool {
        if self.count == 0 {
            return false;
        }
        let last = self.count - 1;
        let next = match (forward, wrap) {
            (true, _) if self.chosen < last => self.chosen + 1,
            (true, true) => 0,
            (false, _) if self.chosen > 0 => self.chosen - 1,
            (false, true) => last,
            _ => return false,
        };
        let moved = next != self.chosen;
        self.chosen = next;
        moved
    }

    /// Whether a step in that direction, without a wrap, would move.
    #[must_use]
    pub const fn can_step(&self, forward: bool) -> bool {
        if forward {
            self.chosen + 1 < self.count
        } else {
            self.chosen > 0
        }
    }
}

/// What a cycler's caption is drawn between: a chevron on each side a step
/// would go, and the same width of nothing on a side it would not — so the
/// caption stays put as the chevrons come and go, which needs every glyph to
/// advance alike and the built-in atlas's do.
const BACK_CHEVRON: &str = "< ";
const FORWARD_CHEVRON: &str = " >";
const NO_CHEVRON: &str = "  ";

/// What a menu row *is*: something that fires, or something that carries a
/// value.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MenuItemKind {
    /// A button. [`Menu::activate`] and a click both report its id.
    Action,
    /// A slider. It never fires — see [`Menu::activate`] — and the caller reads
    /// its value with [`Menu::slider`] instead.
    Slider(Slider),
    /// A cycler. The commit key and a click step it rather than fire it, and
    /// the caller reads which choice is picked with [`Menu::cycler`].
    Cycler(Cycler),
}

/// One line of a menu: what it says, what key does it, and what it is called.
#[derive(Debug, Clone, PartialEq)]
pub struct MenuItem {
    /// Names this item to the caller — what [`Menu::activate`] hands back.
    ///
    /// A [`WidgetId`] rather than an index, so a menu that grows an entry does
    /// not silently re-number what the loop is matching on.
    pub id: WidgetId,
    /// The label, drawn left-aligned in the button.
    pub label: String,
    /// The key that also does this, drawn right-aligned and dimmer. Empty for an
    /// item with no keyboard shortcut of its own.
    ///
    /// **A hint, not a binding.** This type does not read the keyboard; the game
    /// loop already owns its key handling and this is what tells the player what
    /// it is.
    ///
    /// A [`MenuItemKind::Slider`] row has no key of its own to advertise, so
    /// this is where its **value** goes — the formatted number the caller
    /// refreshes as the handle moves. Right-aligned either way. A
    /// [`MenuItemKind::Cycler`] row's is the caption of the choice it holds,
    /// which the caller refreshes as the choice changes; it is drawn between
    /// chevrons — see [`MenuItem::caption`].
    pub hint: String,
    /// Whether this row fires or carries a value.
    pub kind: MenuItemKind,
}

impl MenuItem {
    /// An item with a key hint beside it.
    #[must_use]
    pub fn new(id: WidgetId, label: impl Into<String>, hint: impl Into<String>) -> Self {
        Self {
            id,
            label: label.into(),
            hint: hint.into(),
            kind: MenuItemKind::Action,
        }
    }

    /// A slider row: a label, a groove with its handle `position` of the way
    /// along, and `value` drawn where a key hint would be.
    #[must_use]
    pub fn slider(
        id: WidgetId,
        label: impl Into<String>,
        value: impl Into<String>,
        position: f32,
    ) -> Self {
        Self {
            id,
            label: label.into(),
            hint: value.into(),
            kind: MenuItemKind::Slider(Slider::new(position)),
        }
    }

    /// A cycler row: a label, one of `count` choices with `chosen` picked, and
    /// that choice's `caption` drawn where a key hint would be.
    #[must_use]
    pub fn cycler(
        id: WidgetId,
        label: impl Into<String>,
        caption: impl Into<String>,
        count: usize,
        chosen: usize,
    ) -> Self {
        Self {
            id,
            label: label.into(),
            hint: caption.into(),
            kind: MenuItemKind::Cycler(Cycler::new(count, chosen)),
        }
    }

    /// What this row draws where a key hint goes.
    ///
    /// The hint itself for a button or a slider. For a cycler, the hint between
    /// a `<` and a `>` — each present only on the side a step without a wrap
    /// would go, and replaced by blank of the same width on the side it would
    /// not, so the caption does not shift as they come and go.
    #[must_use]
    pub fn caption(&self) -> std::borrow::Cow<'_, str> {
        match self.kind {
            MenuItemKind::Cycler(cycler) => {
                let chevron = |forward: bool, chevron: &'static str| {
                    if cycler.can_step(forward) {
                        chevron
                    } else {
                        NO_CHEVRON
                    }
                };
                std::borrow::Cow::Owned(format!(
                    "{}{}{}",
                    chevron(false, BACK_CHEVRON),
                    self.hint,
                    chevron(true, FORWARD_CHEVRON)
                ))
            }
            MenuItemKind::Action | MenuItemKind::Slider(_) => {
                std::borrow::Cow::Borrowed(self.hint.as_str())
            }
        }
    }
}

/// How a [`Caption`] is drawn: which of [`MenuStyle`]'s colours it takes.
///
/// A tone per line rather than a separate warning field on [`Menu`], so a
/// menu can warn on any of its lines and another tone is another variant.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CaptionTone {
    /// Explains — the controls, say — in [`MenuStyle::hint_color`].
    #[default]
    Hint,
    /// Warns — of what a choice discards, say — in
    /// [`MenuStyle::warning_color`].
    Warning,
}

/// One line of a menu's [`Menu::subtitle`]: its text, and the tone it is
/// drawn in.
///
/// A `&str` or a `String` converts into a [`CaptionTone::Hint`] caption, so a
/// line that only explains is `"ARROWS MOVE".into()`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Caption {
    /// The line, drawn centred under the title.
    pub text: String,
    /// Which colour it is drawn in.
    pub tone: CaptionTone,
}

impl Caption {
    /// A line in [`CaptionTone::Hint`].
    #[must_use]
    pub fn hint(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            tone: CaptionTone::Hint,
        }
    }

    /// A line in [`CaptionTone::Warning`].
    #[must_use]
    pub fn warning(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            tone: CaptionTone::Warning,
        }
    }
}

impl From<&str> for Caption {
    fn from(text: &str) -> Self {
        Self::hint(text)
    }
}

impl From<String> for Caption {
    fn from(text: String) -> Self {
        Self::hint(text)
    }
}

/// A modal menu: a title, some items, and which one is selected.
///
/// Retained across frames — unlike the rest of this crate — because a selection
/// is state by definition, and an immediate-mode menu whose highlight was
/// recomputed from nothing every frame would have no keyboard at all.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Menu {
    /// The heading, drawn above the items.
    pub title: String,
    /// Lines drawn under the title, centred and each in its
    /// [`CaptionTone`]'s colour — see the module's *Captions*. Empty by
    /// default, which draws exactly the menu there was before captions
    /// existed.
    pub subtitle: Vec<Caption>,
    items: Vec<MenuItem>,
    selected: usize,
    /// Whether the selected item is being held down.
    pressed: bool,
    /// The item the current press belongs to — the pointer's captured item or
    /// the keyboard's selection. What decides who draws [`ButtonState::Pressed`]:
    /// `pressed` alone cannot, because a drag onto a neighbour leaves the
    /// capture (and the drawn press) with the item it started on.
    pressed_index: Option<usize>,
    /// Which item the pointer is over, if any. Takes over the highlight from
    /// [`Menu::selected`] while it is `Some`.
    hovered: Option<usize>,
    /// The slider row the pointer is dragging, if it is dragging one.
    ///
    /// What makes [`Menu::set_slider`] leave a handle alone while the player
    /// has hold of it: a caller pushing the value it last read back in every
    /// frame would otherwise fight the drag it is reading from.
    dragging: Option<usize>,
    /// How far a scrolled item list is scrolled, in pixels: what the next
    /// [`Menu::layout_with_font_fitted`] starts from before it clamps it to
    /// the list's reach and, while `follow_selection` holds, scrolls the
    /// selection into view. Zero for a list that is not scrolled.
    scroll: f32,
    /// Whether the next fitted layout scrolls the selection into view. Set by
    /// every keyboard move and cleared by [`Menu::scroll_wheel`]: a wheel
    /// snapped back to the selection every frame could never move the list.
    follow_selection: bool,
}

impl Menu {
    /// A menu with a title and its items, the first selected.
    #[must_use]
    pub fn new(title: impl Into<String>, items: Vec<MenuItem>) -> Self {
        Self {
            title: title.into(),
            subtitle: Vec::new(),
            items,
            selected: 0,
            pressed: false,
            pressed_index: None,
            hovered: None,
            dragging: None,
            scroll: 0.0,
            follow_selection: true,
        }
    }

    /// The items, in the order they are drawn.
    #[must_use]
    pub fn items(&self) -> &[MenuItem] {
        &self.items
    }

    /// Which item is selected. Zero for an empty menu, which has no items to
    /// index and never activates one.
    #[must_use]
    pub const fn selected(&self) -> usize {
        self.selected
    }

    /// The selected item, or `None` when there are none.
    #[must_use]
    pub fn selected_item(&self) -> Option<&MenuItem> {
        self.items.get(self.selected)
    }

    /// Moves the selection down one, wrapping at the end.
    ///
    /// A scrolled list — see [`Menu::layout_with_font_fitted`] — is scrolled
    /// to the new selection by the next layout, by as little as brings it
    /// wholly into view. "As little" is measured from the scroll the menu
    /// holds, which [`Menu::point`] and [`Menu::scroll_wheel`] keep up to
    /// date: a caller that calls neither still always sees the selection, but
    /// held at the edge it scrolled past rather than moving inside a still
    /// list.
    pub fn select_next(&mut self) {
        self.move_selection(1);
    }

    /// Moves the selection up one, wrapping at the start. A scrolled list
    /// follows it, as [`Menu::select_next`] says.
    pub fn select_previous(&mut self) {
        self.move_selection(-1);
    }

    fn move_selection(&mut self, delta: isize) {
        if self.items.is_empty() {
            return;
        }
        let len = self.items.len() as isize;
        let next = (self.selected as isize + delta).rem_euclid(len);
        self.selected = next as usize;
        self.follow_selection = true;
        // A keyboard move takes the highlight back off the pointer: otherwise a
        // cursor left resting over an item makes the arrow keys look dead.
        self.hovered = None;
        self.pressed = false;
        self.pressed_index = None;
        self.dragging = None;
    }

    /// Selects the item with this id, if it is in the menu.
    ///
    /// Returns whether it was found — a caller restoring a selection across a
    /// rebuild wants to know when the item it remembered is gone.
    pub fn select_id(&mut self, id: WidgetId) -> bool {
        match self.items.iter().position(|item| item.id == id) {
            Some(index) => {
                self.selected = index;
                self.hovered = None;
                self.follow_selection = true;
                true
            }
            None => false,
        }
    }

    /// Holds the highlighted item down, or lets it up.
    ///
    /// Called from the key-down and key-up of whatever key commits — the visible
    /// half of "the button is being pressed". Activation is [`Menu::activate`];
    /// this only changes what is drawn.
    pub const fn press(&mut self, down: bool) {
        self.pressed = down;
        // A keyboard press belongs to the highlighted item, which is the
        // selected one until the pointer takes over.
        self.pressed_index = if down { Some(self.selected) } else { None };
    }

    /// Fires the highlighted item and returns its id.
    ///
    /// The **highlighted** one, which is the hovered item when a pointer is over
    /// one and the selected item otherwise — so Enter commits what the player can
    /// see is about to happen, whichever device put the highlight there.
    /// A [`MenuItemKind::Slider`] row reports **nothing**: there is no press of
    /// a commit key that means anything to a value, and an id fired from one
    /// would reach the game's action table as if a button had been clicked. A
    /// [`MenuItemKind::Cycler`] row reports nothing for the same reason, and
    /// the press **steps it forward, round the end** — the one-key walk of the
    /// whole list the caller reads back with [`Menu::cycler`].
    pub fn activate(&mut self) -> Option<WidgetId> {
        self.pressed = false;
        self.pressed_index = None;
        let index = self.hovered.unwrap_or(self.selected);
        match self.items.get_mut(index) {
            Some(item) => match &mut item.kind {
                MenuItemKind::Action => Some(item.id),
                MenuItemKind::Cycler(cycler) => {
                    cycler.step(true, true);
                    None
                }
                MenuItemKind::Slider(_) => None,
            },
            None => None,
        }
    }

    /// Drops any hover and any held key.
    ///
    /// For a menu being taken off the screen: a menu re-shown with a stale
    /// `Pressed` on it draws a button nobody is touching.
    pub const fn clear_input(&mut self) {
        self.hovered = None;
        self.pressed = false;
        self.pressed_index = None;
        self.dragging = None;
    }

    /// Where the handle of the slider row named `id` sits, in `0.0..=1.0`.
    ///
    /// `None` for an id this menu has no row for, and for a row that is a
    /// button rather than a slider.
    #[must_use]
    pub fn slider(&self, id: WidgetId) -> Option<f32> {
        self.items.iter().find_map(|item| match item.kind {
            MenuItemKind::Slider(slider) if item.id == id => Some(slider.position()),
            _ => None,
        })
    }

    /// Moves the handle of the slider row named `id`, and reports whether it
    /// moved.
    ///
    /// **Refused while the player is dragging that row**, which is what lets a
    /// caller push the value back in unconditionally every frame: the state the
    /// caller is mirroring came from this handle in the first place, so the
    /// drag is the newer of the two and writing over it would pin the handle
    /// under the cursor.
    ///
    /// So `false` means one of three things — no such row, not a slider, or the
    /// player has hold of it — and a caller that needs to tell them apart asks
    /// [`Menu::slider`] first.
    pub fn set_slider(&mut self, id: WidgetId, position: f32) -> bool {
        let dragging = self.dragging;
        for (index, item) in self.items.iter_mut().enumerate() {
            if item.id != id {
                continue;
            }
            let MenuItemKind::Slider(slider) = &mut item.kind else {
                return false;
            };
            if dragging == Some(index) {
                return false;
            }
            slider.set_position(position);
            return true;
        }
        false
    }

    /// Moves the highlighted slider one [`Slider::KEY_STEP`], and reports
    /// whether a handle moved.
    ///
    /// **The keyboard's half of a slider**, and until it existed there was no
    /// such half: [`Menu::activate`] reports nothing for a slider row by
    /// design, so a player with no pointer could select a volume and then had
    /// no way to change it. This module's own opening says the keyboard is the
    /// primary input; a row that only a mouse could work was that claim being
    /// false in the one place it is most visible.
    ///
    /// The **highlighted** row, on [`Menu::activate`]'s terms — the hovered one
    /// when a pointer is over one, the selected one otherwise — so the key moves
    /// what the player can see is about to move.
    ///
    /// `false` means nothing moved: the row is a button, there is no row, the
    /// handle is already at that end of the groove, or the pointer has hold of
    /// it. The last is [`Menu::set_slider`]'s rule and it is here for the same
    /// reason: a drag is the newer of the two inputs.
    pub fn nudge_slider(&mut self, forward: bool) -> bool {
        let index = self.hovered.unwrap_or(self.selected);
        if self.dragging == Some(index) {
            return false;
        }
        let Some(item) = self.items.get_mut(index) else {
            return false;
        };
        let MenuItemKind::Slider(slider) = &mut item.kind else {
            return false;
        };
        let before = slider.position();
        slider.set_position(if forward {
            before + Slider::KEY_STEP
        } else {
            before - Slider::KEY_STEP
        });
        slider.position() != before
    }

    /// Whether the highlighted row is a slider.
    ///
    /// What a caller deciding whether a key belongs to the menu has to ask
    /// **before** offering it: [`nudge_slider`](Self::nudge_slider) reports
    /// whether a handle moved, which is also `false` at the end of the groove,
    /// and a key claimed on that would be handed back to the game halfway
    /// through a player holding it down.
    #[must_use]
    pub fn slider_highlighted(&self) -> bool {
        matches!(
            self.items.get(self.hovered.unwrap_or(self.selected)),
            Some(item) if matches!(item.kind, MenuItemKind::Slider(_))
        )
    }

    /// Which choice the cycler row named `id` holds: an index into the
    /// caller's list.
    ///
    /// `None` for an id this menu has no row for, and for a row that is not a
    /// cycler.
    #[must_use]
    pub fn cycler(&self, id: WidgetId) -> Option<usize> {
        self.items.iter().find_map(|item| match item.kind {
            MenuItemKind::Cycler(cycler) if item.id == id => Some(cycler.chosen()),
            _ => None,
        })
    }

    /// Picks the choice of the cycler row named `id`, clamped to its last, and
    /// reports whether there was such a row.
    ///
    /// Nothing refuses it: a cycler is never dragged, so the caller's write is
    /// always the newer of the two inputs and mirroring a value back in every
    /// frame is safe — the row it mirrors into is the row it read.
    pub fn set_cycler(&mut self, id: WidgetId, chosen: usize) -> bool {
        self.items
            .iter_mut()
            .find(|item| item.id == id)
            .is_some_and(|item| match &mut item.kind {
                MenuItemKind::Cycler(cycler) => {
                    cycler.choose(chosen);
                    true
                }
                _ => false,
            })
    }

    /// Steps the highlighted cycler one choice, **stopping at the ends**, and
    /// reports whether the choice changed.
    ///
    /// The arrows' half of a cycler, beside [`Menu::nudge_slider`]: the same
    /// keys, the same highlighted row, and the same `false` at the end of the
    /// list that a slider gives at the end of its groove — a key held against
    /// the last choice does not come round to the first, because a player
    /// holding Right to reach the top of a list would overshoot it. The commit
    /// key is the one that goes round; see [`Menu::activate`].
    pub fn nudge_cycler(&mut self, forward: bool) -> bool {
        let index = self.hovered.unwrap_or(self.selected);
        match self.items.get_mut(index).map(|item| &mut item.kind) {
            Some(MenuItemKind::Cycler(cycler)) => cycler.step(forward, false),
            _ => false,
        }
    }

    /// Whether the highlighted row is a cycler — [`slider_highlighted`]'s
    /// question for the other row the arrows drive, and asked for the same
    /// reason.
    ///
    /// [`slider_highlighted`]: Self::slider_highlighted
    #[must_use]
    pub fn cycler_highlighted(&self) -> bool {
        matches!(
            self.items.get(self.hovered.unwrap_or(self.selected)),
            Some(item) if matches!(item.kind, MenuItemKind::Cycler(_))
        )
    }

    /// The value text a slider row draws, replaced.
    ///
    /// Separate from [`Menu::set_slider`] because the two move for different
    /// reasons: the handle is refused mid-drag and the caption never is — a
    /// player dragging the exposure is precisely who needs to read the number
    /// it has reached.
    pub fn set_item_hint(&mut self, id: WidgetId, hint: impl Into<String>) -> bool {
        match self.items.iter_mut().find(|item| item.id == id) {
            Some(item) => {
                item.hint = hint.into();
                true
            }
            None => false,
        }
    }

    /// How item `index` is drawn.
    ///
    /// [`ButtonState::Pressed`] for the item the current press belongs to —
    /// the pointer's captured item, or the selected one under the keyboard —
    /// [`ButtonState::Hovered`] for the highlighted item otherwise, and
    /// [`ButtonState::Idle`] for everything else — which is what makes a
    /// keyboard-only player see the same three frames of art a mouse would light
    /// up.
    #[must_use]
    pub fn state(&self, index: usize) -> ButtonState {
        if index >= self.items.len() || index != self.hovered.unwrap_or(self.selected) {
            return ButtonState::Idle;
        }
        if self.pressed_index == Some(index) {
            ButtonState::Pressed
        } else if self.pressed {
            // The press belongs to another item (a drag-off): this one must
            // stay Idle, exactly as `interact` reports it.
            ButtonState::Idle
        } else {
            ButtonState::Hovered
        }
    }

    /// Runs one frame of pointer input against `layout`, and reports a click.
    ///
    /// Entirely optional: a game that never calls this is driven by the keyboard
    /// alone and every item stays at the state [`Menu::state`] derives from the
    /// selection. A game that does call it gets hover, press capture through
    /// `ui` — so a press that starts on one item and is released over another
    /// fires neither — and a click that also **moves the selection**, so the
    /// keyboard picks up where the mouse left off.
    ///
    /// In a scrolled list only the part of a row inside the list's
    /// [`viewport`](MenuLayout::viewport) is hit, and a row scrolled wholly
    /// out of it is not hit at all. While the list follows the keyboard, the
    /// scroll `layout` settled on is kept as the one the next keyboard move
    /// scrolls from — see [`Menu::select_next`].
    pub fn point(
        &mut self,
        layout: &MenuLayout,
        ui: &mut UiState,
        pointer: PointerInput,
    ) -> Option<WidgetId> {
        if self.follow_selection {
            self.scroll = layout.scroll();
        }
        let in_view = layout
            .viewport()
            .is_none_or(|viewport| contains(viewport, pointer.pos));
        let mut clicked = None;
        let mut hovered = None;
        for (index, item) in layout.items.iter().enumerate() {
            let inside =
                in_view && layout.shows(index) && contains((item.min, item.max), pointer.pos);
            let (state, fired) = ui.interact(item.id, inside, pointer.down, pointer.released);
            if inside {
                hovered = Some(index);
            }
            let is_slider = matches!(
                self.items.get(index).map(|item| item.kind),
                Some(MenuItemKind::Slider(_))
            );
            if state == ButtonState::Pressed && is_slider {
                // Tracked whether or not the cursor is still **inside** the
                // row, unlike a button's press: a slider dragged off the end of
                // its groove — or above it, which is what a hand does while
                // watching the frame behind the panel — keeps the handle at the
                // end rather than letting go of it.
                if let Some(track) = item.track
                    && let Some(MenuItemKind::Slider(slider)) =
                        self.items.get_mut(index).map(|item| &mut item.kind)
                {
                    slider.set_position(position_along(
                        track,
                        layout.style.handle_size.x,
                        pointer.pos.x,
                    ));
                }
                self.dragging = Some(index);
                self.pressed = true;
                self.pressed_index = Some(index);
                self.selected = index;
            } else if state == ButtonState::Pressed && inside {
                self.pressed = true;
                self.pressed_index = Some(index);
            }
            // A release over a slider ends a drag; it is not a click, and an id
            // reported from one would reach the game's action table as though a
            // button had been pressed. A release over a cycler is its step
            // forward and round, `activate`'s rule, and reports nothing either.
            if fired {
                match self.items.get_mut(index).map(|item| &mut item.kind) {
                    Some(MenuItemKind::Action) => {
                        clicked = Some(item.id);
                        self.selected = index;
                    }
                    Some(MenuItemKind::Cycler(cycler)) => {
                        cycler.step(true, true);
                        self.selected = index;
                    }
                    _ => {}
                }
            }
        }
        self.hovered = hovered;
        if !pointer.down {
            self.pressed = false;
            self.pressed_index = None;
            self.dragging = None;
        }
        clicked
    }

    /// Scrolls a scrolled item list by a wheel's `delta`, in pixels, and says
    /// whether it moved: positive `y` moves toward the end of the list, as
    /// [`Ui::scroll_wheel`] reads a wheel. `x` is ignored — the list scrolls
    /// up and down only.
    ///
    /// Only while `pointer` is over the panel, since the list is the one thing
    /// in it that moves. The list stops following the keyboard's selection
    /// until the keyboard next moves it, so the selection may be scrolled out
    /// of view — and the scroll is kept to the fraction of a pixel, so a
    /// trackpad's small steps add up, while each layout draws it at the whole
    /// pixel nearest. `false` for a list that does not scroll, so the caller
    /// can give the wheel to something else.
    ///
    /// A separate call rather than a [`PointerInput`] field, as
    /// [`Ui::scroll_wheel`] is, because every caller builds that by literal.
    pub fn scroll_wheel(&mut self, layout: &MenuLayout, pointer: Vec2, delta: Vec2) -> bool {
        let Some(list) = &layout.setup.list else {
            return false;
        };
        if !contains(layout.panel, pointer) {
            return false;
        }
        let from = if self.follow_selection {
            list.scroll
        } else {
            self.scroll.clamp(0.0, list.reach)
        };
        let to = (from + delta.y).clamp(0.0, list.reach);
        self.follow_selection = false;
        self.scroll = to;
        to != from
    }

    /// Lays this menu out centred in an `extent`-sized framebuffer, at the
    /// largest scale that fits.
    ///
    /// The scale is a pure function of the extent and the menu's own contents:
    /// the largest whole number up to [`MenuStyle::MAX_SCALE`] whose panel fits
    /// inside [`FIT_FRACTION`] of the framebuffer on **both** axes, and 1 when
    /// none of them do. Whole numbers because the art is pixel art; the fit
    /// because a menu that overflowed a 1440×400 canvas would have its buttons
    /// off the bottom of the screen, which is the failure this replaces.
    ///
    /// **The panel is measured once, at scale 1**, and each candidate is that
    /// size times the scale: every length [`MenuStyle::pixel_art`] makes and
    /// every glyph advance is a whole number times the scale, so the tree lays
    /// the panel out exactly proportionally —
    /// `the_panel_grows_exactly_with_the_scale` holds that — and one layout
    /// answers every candidate.
    #[must_use]
    pub fn layout(&self, extent: (u32, u32), atlas: &FontAtlas) -> MenuLayout {
        let base = self.panel_size(atlas, &MenuStyle::pixel_art(1));
        let mut chosen = 1;
        for scale in 2..=MenuStyle::MAX_SCALE {
            let size = base * scale as f32;
            if size.x <= extent.0 as f32 * FIT_FRACTION && size.y <= extent.1 as f32 * FIT_FRACTION
            {
                chosen = scale;
            } else {
                break;
            }
        }
        self.layout_with(extent, atlas, &MenuStyle::pixel_art(chosen))
    }

    /// Lays this menu out centred, at a style the caller chose.
    #[must_use]
    pub fn layout_with(
        &self,
        extent: (u32, u32),
        atlas: &FontAtlas,
        style: &MenuStyle,
    ) -> MenuLayout {
        let screen = Vec2::new(extent.0 as f32, extent.1 as f32);
        let setup = Setup::default();
        self.laid_out(Some(screen), style, None, &setup, atlas, |ui, built| {
            Self::read_layout(ui, built, &self.items, screen, style, &setup)
        })
    }

    /// Lays this menu out centred at `style`, with every line of its text
    /// measured in `font` rather than the built-in [`FontAtlas`] — and drawn in
    /// it too, since [`Menu::render`] draws in the font the layout it is given
    /// was measured in.
    ///
    /// The text is a registered font's in the menu's own tree, so it is
    /// measured by the tree's one text path, as any span in a registered font
    /// is: `font-size` is [`MenuStyle`]'s, and the line pitch is the font's
    /// `line-height: normal`. [`Menu::layout`]'s scale fit has no counterpart
    /// here — it counts on the bitmap font's advances growing exactly with the
    /// scale, which a parsed font's do not — so `style` is the caller's; see
    /// [`Menu::layout_with_font_fitted`] for a fit that measures instead. A
    /// cycler's chevrons keep its caption still only in a font whose `<`, `>`
    /// and space advance alike.
    #[must_use]
    pub fn layout_with_font(
        &self,
        extent: (u32, u32),
        style: &MenuStyle,
        font: &Font,
    ) -> MenuLayout {
        let setup = Setup {
            font: Some(font.clone()),
            ..Setup::default()
        };
        self.placed(extent_size(extent), style, &setup)
    }

    /// Lays this menu out centred in `font`, as [`Menu::layout_with_font`]
    /// does, at the largest size up to `style` that fits the framebuffer —
    /// shrinking it first and scrolling its item list after, as the module's
    /// *Fitting a small window, then scrolling* describes.
    ///
    /// **Fits** is [`Menu::layout`]'s word: inside [`FIT_FRACTION`] of the
    /// framebuffer on both axes. `style` is the largest the menu is drawn at,
    /// and is what it is drawn at when that fits. Otherwise the item font
    /// steps down by [`FIT_FONT_STEP`] to [`MenuStyle::MIN_FONT_SIZE`] — or
    /// stays at `style`'s own, if that is smaller — and every other length
    /// shrinks with it by the same factor, the art to the whole-number scale
    /// the factor leaves and never below one. The step is found by bisection
    /// over the steps, which is the largest that fits so long as a smaller
    /// step never lays out a bigger panel — every length shrinks with the
    /// font, so only a rounded pixel could. If even the smallest step is
    /// too tall, the item list at that step is capped at the height the rest
    /// of the panel leaves, and scrolls: see [`MenuLayout::viewport`].
    ///
    /// Every length but the font sizes is rounded to a whole pixel, and each
    /// row's line pitch is the font's `line-height: normal` rounded up to one,
    /// so every row is a whole number of pixels tall — which is what lets a
    /// scrolled list draw only the rows in view where this layout put them. A
    /// `style` that fits is therefore drawn at exactly its own lengths only
    /// when they are whole, as [`MenuStyle::pixel_art`]'s are.
    ///
    /// A pure function of the menu — its contents, its selection and its
    /// scroll — and its arguments: the same inputs lay out the same menu.
    ///
    /// # Errors
    ///
    /// [`MenuFitError::TooWide`] when the panel is wider than the room even at
    /// the smallest step, which scrolling up and down cannot help, and
    /// [`MenuFitError::TooShort`] when the frame, the title and the captions
    /// leave less height than the list's tallest row — or no list at all.
    pub fn layout_with_font_fitted(
        &self,
        extent: (u32, u32),
        style: &MenuStyle,
        font: &Font,
    ) -> Result<MenuLayout, MenuFitError> {
        let screen = extent_size(extent);
        let room = screen * FIT_FRACTION;
        let sizes = fit_sizes(style.item_size);
        let measure = |item_size: f32| {
            let style = style.shrunk(item_size);
            let setup = Setup {
                font: Some(font.clone()),
                line: Some(row_line(font, &style)),
                list: None,
            };
            let measured = self.measure(&style, &setup);
            Candidate {
                style,
                setup,
                measured,
            }
        };
        let fits = |candidate: &Candidate| {
            let panel = candidate.measured.panel;
            panel.x <= room.x && panel.y <= room.y
        };

        let mut fitting = measure(sizes[0]);
        if !fits(&fitting) {
            let last = sizes.len() - 1;
            let smallest = if last == 0 {
                fitting
            } else {
                measure(sizes[last])
            };
            if !fits(&smallest) {
                return self.scrolled(screen, room, smallest);
            }
            // `sizes[lo]` is too big and `sizes[hi]`, which `fitting` holds,
            // fits.
            fitting = smallest;
            let (mut lo, mut hi) = (0, last);
            while hi - lo > 1 {
                let mid = (lo + hi) / 2;
                let candidate = measure(sizes[mid]);
                if fits(&candidate) {
                    hi = mid;
                    fitting = candidate;
                } else {
                    lo = mid;
                }
            }
        }
        Ok(self.placed(screen, &fitting.style, &fitting.setup))
    }

    /// The fit's second stage: `candidate`, too tall for `room` at the
    /// smallest size the fit may use, with its item list capped at the height
    /// the rest of the panel leaves, and scrolled.
    fn scrolled(
        &self,
        screen: Vec2,
        room: Vec2,
        candidate: Candidate,
    ) -> Result<MenuLayout, MenuFitError> {
        let Candidate {
            style,
            setup,
            measured,
        } = candidate;
        let font_size = style.item_size;
        if measured.panel.x > room.x {
            return Err(MenuFitError::TooWide {
                font_size,
                width: measured.panel.x,
                room: room.x,
            });
        }
        let chrome = measured.panel.y - measured.list.y;
        let mut height = (room.y - chrome).floor();
        loop {
            if self.items.is_empty() || height < measured.tallest {
                return Err(MenuFitError::TooShort {
                    font_size,
                    height: chrome + measured.tallest,
                    room: room.y,
                });
            }
            // Every row, unscrolled: the geometry `scroll_list` scrolls.
            let list = ListView {
                size: Vec2::new(measured.list.x, height),
                viewport: (Vec2::ZERO, Vec2::ZERO),
                scroll: 0.0,
                reach: 0.0,
                shown: 0..self.items.len(),
                shown_top: 0.0,
            };
            let setup = Setup {
                list: Some(list),
                ..setup.clone()
            };
            let layout = self.placed(screen, &style, &setup);
            // The panel is its measured chrome plus the list's height, less
            // whatever Taffy's rounding moved; a pixel over is a pixel off
            // the list.
            let over = layout.panel_size().y - room.y;
            if over <= 0.0 {
                return Ok(self.scroll_list(layout));
            }
            height -= over.ceil();
        }
    }

    /// Scrolls `layout` — placed with every row of its capped list built and
    /// unscrolled — to this menu's scroll, clamped to the list's reach and,
    /// while the list follows the keyboard, moved by as little as brings the
    /// selected row wholly into view. Every row's rectangles move with it, and
    /// the rows left overlapping the viewport are the ones drawn and hit.
    fn scroll_list(&self, mut layout: MenuLayout) -> MenuLayout {
        let list = layout
            .setup
            .list
            .as_mut()
            .expect("a scrolled layout has a list");
        let (top, bottom) = (list.viewport.0.y, list.viewport.1.y);
        let height = bottom - top;
        let end = layout.items.last().map_or(top, |row| row.max.y);
        list.reach = (end - top - height).max(0.0);

        let mut scroll = self.scroll.round().clamp(0.0, list.reach);
        if self.follow_selection
            && let Some(row) = layout.items.get(self.selected)
        {
            let (row_top, row_bottom) = (row.min.y - top, row.max.y - top);
            if row_top < scroll {
                scroll = row_top;
            } else if row_bottom > scroll + height {
                scroll = row_bottom - height;
            }
        }
        let scroll = scroll.clamp(0.0, list.reach);
        list.scroll = scroll;

        let up = Vec2::new(0.0, scroll);
        for row in &mut layout.items {
            row.min -= up;
            row.max -= up;
            row.label_pos -= up;
            row.hint_pos -= up;
            if let Some(track) = &mut row.track {
                track.0 -= up;
                track.1 -= up;
            }
        }
        let rows = &layout.items;
        let first = rows
            .iter()
            .position(|row| row.max.y > top)
            .unwrap_or(rows.len());
        let past = rows
            .iter()
            .rposition(|row| row.min.y < bottom)
            .map_or(first, |last| (last + 1).max(first));
        list.shown = first..past;
        list.shown_top = rows.get(first).map_or(0.0, |row| row.min.y + scroll - top);
        layout
    }

    /// Lays this menu out centred in `screen` at `style`, built as `setup`
    /// says, and reads the layout back.
    fn placed(&self, screen: Vec2, style: &MenuStyle, setup: &Setup) -> MenuLayout {
        self.laid_out(
            Some(screen),
            style,
            None,
            setup,
            &FontAtlas::built_in(),
            |ui, built| Self::read_layout(ui, built, &self.items, screen, style, setup),
        )
    }

    /// The panel, the item list and the list's tallest row at `style`, built
    /// as `setup` says, before any of them is placed.
    fn measure(&self, style: &MenuStyle, setup: &Setup) -> Measured {
        self.laid_out(
            None,
            style,
            None,
            setup,
            &FontAtlas::built_in(),
            |ui, built| {
                let size = |key| {
                    let (min, max) = ui.rect(key).expect("laid out this frame");
                    max - min
                };
                Measured {
                    panel: size(built.panel),
                    list: size(built.items),
                    tallest: built
                        .rows
                        .iter()
                        .map(|row| size(row.row).y)
                        .fold(0.0, f32::max),
                }
            },
        )
    }

    /// Reads a [`MenuLayout`] back out of `ui`, which [`Menu::laid_out`] built
    /// for `items` at `style` in `screen`, as `setup` says.
    fn read_layout(
        ui: &Ui,
        built: &BuiltMenu,
        items: &[MenuItem],
        screen: Vec2,
        style: &MenuStyle,
        setup: &Setup,
    ) -> MenuLayout {
        let rect = |key| ui.rect(key).expect("laid out this frame");
        let panel = rect(built.panel);
        let corners = style.panel_corners();
        let content_min = panel.0 + corners.min_corner() + style.panel_padding;
        let content_max = panel.1 - Vec2::new(corners.right, corners.bottom) - style.panel_padding;
        // An empty title builds no span, and stands where a zero-width one
        // would: centred on the content's top edge.
        let title_pos = built.title.map_or(
            Vec2::new((content_min.x + content_max.x) * 0.5, content_min.y),
            |key| rect(key).0,
        );
        let items = items
            .iter()
            .zip(&built.rows)
            .map(|(item, row)| {
                let (min, max) = rect(row.row);
                let label_pos = rect(row.label).0;
                let inner = style.button_corners();
                let hint_pos = row.hint.map_or(
                    Vec2::new(max.x - inner.right - style.button_padding.x, label_pos.y),
                    |key| rect(key).0,
                );
                MenuItemLayout {
                    id: item.id,
                    min,
                    max,
                    label_pos,
                    hint_pos,
                    track: row.groove.map(rect),
                }
            })
            .collect();

        let mut setup = setup.clone();
        if let Some(list) = &mut setup.list {
            list.viewport = rect(built.items);
        }
        MenuLayout {
            style: *style,
            setup,
            screen,
            panel,
            title_pos,
            items,
        }
    }

    /// The panel's size at `style`, before it is placed anywhere.
    ///
    /// Public because the *fit* half of [`Menu::layout`] is a question a caller
    /// may reasonably ask on its own ("would this menu fit at scale 3?"), and
    /// because a test that could not measure the panel without placing it could
    /// not tell a menu that grew from one that moved.
    #[must_use]
    pub fn panel_size(&self, atlas: &FontAtlas, style: &MenuStyle) -> Vec2 {
        self.laid_out(None, style, None, &Setup::default(), atlas, |ui, built| {
            let (min, max) = ui.rect(built.panel).expect("laid out this frame");
            max - min
        })
    }

    /// Builds this menu with [`Menu::build`] and lays it out, in the calling
    /// thread's one menu tree, then hands the tree to `read`. Its text is in
    /// `setup`'s font, registered in that tree, or in the bitmap font for
    /// none.
    ///
    /// **One tree per thread, rebuilt by every call** — two, one to measure
    /// in and one to place in — rather than a fresh
    /// [`Ui`] each time: a menu is laid out several times a frame — for its
    /// fit, its hit test and its drawing — with the same selectors and the same
    /// inline lengths, so a kept tree resolves each node's style from its own
    /// last resolve and lays out from Taffy's cache instead of from nothing.
    /// It holds no state a call can see: the pointer it begins each frame with
    /// is off every rectangle, so no rule's `:hover` applies, every build sets
    /// the item list's scroll, and whatever the last call built is replaced
    /// and pruned. `read` must not lay out a menu itself, which would borrow
    /// the tree twice.
    fn laid_out<R>(
        &self,
        screen: Option<Vec2>,
        style: &MenuStyle,
        skin: Option<&MenuSkin>,
        setup: &Setup,
        atlas: &FontAtlas,
        read: impl FnOnce(&Ui, &BuiltMenu) -> R,
    ) -> R {
        let tree = if screen.is_some() {
            &PLACED_TREE
        } else {
            &MEASURED_TREE
        };
        tree.with(|tree| {
            let mut ui = tree.borrow_mut();
            ui.begin_frame(PointerInput::hovering(OFF_SCREEN));
            if let Some(font) = &setup.font {
                ui.register_font(MENU_FONT, font)
                    .expect("the menu's family name is not reserved");
            }
            let built = self.build(&mut ui, screen, style, skin, setup);
            let available = screen.map_or(AvailableSpace::MAX_CONTENT, AvailableSpace::definite);
            ui.layout(Vec2::ZERO, available, atlas);
            read(&ui, &built)
        })
    }

    /// Builds this menu into `ui` at `style`: the tree [`Menu::layout_with`]
    /// measures and [`Menu::render`] draws, one for both.
    ///
    /// `screen` is the framebuffer the panel is centred in, or `None` for a
    /// root that is only as big as the panel. `skin` is `Some` only when the
    /// tree is to be drawn: it binds the frames `default.css` names and adds
    /// the scrim, which is positioned out of the flow and moves nothing.
    /// A `setup` with a font names [`MENU_FONT`] on the root, which every span
    /// inherits; otherwise the text is `default.css`'s bitmap font. Its line
    /// pitch, if any, is set on the item list for every row to inherit, and
    /// its list, if any, caps the item list at its size, clips it and builds
    /// only the rows it shows, scrolled to where the layout put them.
    ///
    /// **What comes from where.** Every length is `style`'s, set inline, because
    /// it is the pixel-art scale times a base metric and a stylesheet has no
    /// arithmetic to scale with. The frames, their slices, which frame each
    /// state draws and every colour are `default.css`'s menu rules. Which state
    /// a row is in is [`Menu::state`]'s: the tree is this menu's view, and the
    /// selection, the press and the pointer's hover stay in the model.
    fn build(
        &self,
        ui: &mut Ui,
        screen: Option<Vec2>,
        style: &MenuStyle,
        skin: Option<&MenuSkin>,
        setup: &Setup,
    ) -> BuiltMenu {
        use Declaration as D;
        if let Some(skin) = skin {
            ui.set_image(PANEL_IMAGE, skin.panel.image);
            ui.set_image(IDLE_IMAGE, skin.buttons.idle.image);
            ui.set_image(HOVERED_IMAGE, skin.buttons.hovered.image);
            ui.set_image(PRESSED_IMAGE, skin.buttons.pressed.image);
        }
        let px = LengthAuto::Px;
        let mut root = screen.map_or_else(Vec::new, |screen| {
            vec![D::Width(px(screen.x)), D::Height(px(screen.y))]
        });
        if setup.font.is_some() {
            root.push(D::FamilyName(Some(FamilyName::new(MENU_FONT))));
        }
        let panel_corners = style.panel_corners();
        let panel = [
            D::BorderWidth(Sides::Top, panel_corners.top),
            D::BorderWidth(Sides::Right, panel_corners.right),
            D::BorderWidth(Sides::Bottom, panel_corners.bottom),
            D::BorderWidth(Sides::Left, panel_corners.left),
            D::Padding(Sides::Top, Length::Px(style.panel_padding.y)),
            D::Padding(Sides::Bottom, Length::Px(style.panel_padding.y)),
            D::Padding(Sides::Left, Length::Px(style.panel_padding.x)),
            D::Padding(Sides::Right, Length::Px(style.panel_padding.x)),
            D::RowGap(Length::Px(style.title_gap)),
        ];
        let button_corners = style.button_corners();
        let row = [
            D::BorderWidth(Sides::Top, button_corners.top),
            D::BorderWidth(Sides::Right, button_corners.right),
            D::BorderWidth(Sides::Bottom, button_corners.bottom),
            D::BorderWidth(Sides::Left, button_corners.left),
            D::Padding(Sides::Top, Length::Px(style.button_padding.y)),
            D::Padding(Sides::Bottom, Length::Px(style.button_padding.y)),
            D::Padding(Sides::Left, Length::Px(style.button_padding.x)),
            D::Padding(Sides::Right, Length::Px(style.button_padding.x)),
        ];
        let rows_style = RowStyle {
            row,
            label: [D::FontSize(style.item_size)],
            hint: [
                D::FontSize(style.item_size),
                D::Margin(Sides::Left, px(style.hint_gap)),
            ],
            groove: [
                D::Margin(Sides::Left, px(style.hint_gap)),
                D::MinWidth(px(style.track_width)),
                D::Height(px(style.track_height)),
            ],
            thumb: [
                D::Width(px(style.handle_size.x)),
                D::Height(px(style.handle_size.y)),
            ],
        };
        let caption = [D::FontSize(style.item_size)];
        let mut list = vec![D::RowGap(Length::Px(style.item_gap))];
        if let Some(line) = setup.line {
            list.push(D::LineHeight(LineHeight::Px(line)));
        }
        if let Some(view) = &setup.list {
            list.extend([
                D::Overflow(Overflow::Hidden),
                D::FlexShrink(0.0),
                D::Width(px(view.size.x)),
                D::Height(px(view.size.y)),
            ]);
        }
        let (shown, offset) = setup.list.as_ref().map_or(
            (0..self.items.len(), 0.0),
            // The first row built is laid out at the list's top, so the
            // offset is the scroll less how far down that row starts.
            |view| (view.shown.clone(), view.scroll - view.shown_top),
        );

        let mut title = None;
        let mut rows = Vec::with_capacity(shown.len());
        let mut panel_key = None;
        let mut items_key = None;
        ui.block("menu-screen", &root, |ui| {
            if let Some(skin) = skin {
                ui.span(".menu-scrim", skin.scrim, &[]);
            }
            panel_key = Some(
                ui.block("menu", &panel, |ui| {
                    if !self.title.is_empty() {
                        title = Some(
                            ui.span(
                                ".menu-title",
                                self.title.as_str(),
                                &[D::FontSize(style.title_size)],
                            )
                            .key,
                        );
                    }
                    if !self.subtitle.is_empty() {
                        ui.block(".menu-captions", &[], |ui| {
                            for line in &self.subtitle {
                                let selector = match line.tone {
                                    CaptionTone::Hint => ".menu-caption",
                                    CaptionTone::Warning => ".menu-caption.menu-caption-warning",
                                };
                                ui.span(selector, line.text.as_str(), &caption);
                            }
                        });
                    }
                    items_key = Some(
                        ui.block(".menu-items", &list, |ui| {
                            // Set every build, not only a scrolled one's: the
                            // tree is shared, and an offset a scrolled menu
                            // left on this node would move the next menu's
                            // rows.
                            ui.set_scroll_offset(Vec2::new(0.0, offset));
                            for (index, item) in self.items.iter().enumerate() {
                                if shown.contains(&index) {
                                    rows.push(self.build_row(ui, index, item, &rows_style));
                                }
                            }
                        })
                        .key,
                    );
                })
                .key,
            );
        });
        BuiltMenu {
            panel: panel_key.expect("the screen builds its panel"),
            title,
            items: items_key.expect("the panel builds its item list"),
            rows,
        }
    }

    /// Builds item `index`: a row in the pseudo-class [`Menu::state`] gives it,
    /// holding its label, then what the row carries — a groove and its handle
    /// for a slider, a spacer for anything with a caption to push right — and
    /// then the caption itself.
    fn build_row(&self, ui: &mut Ui, index: usize, item: &MenuItem, style: &RowStyle) -> BuiltRow {
        let state = match self.state(index) {
            ButtonState::Idle => PseudoClasses::NONE,
            ButtonState::Hovered => PseudoClasses::HOVER,
            ButtonState::Pressed => PseudoClasses::ACTIVE,
        };
        let mut label = None;
        let mut groove = None;
        let mut hint = None;
        let row = ui
            .block_keyed_in_state(index, "menu-item", &style.row, state, |ui| {
                label = Some(
                    ui.span(".menu-label", item.label.as_str(), &style.label)
                        .key,
                );
                let caption = item.caption();
                match item.kind {
                    MenuItemKind::Slider(slider) => {
                        groove = Some(build_groove(ui, slider, style));
                    }
                    MenuItemKind::Action | MenuItemKind::Cycler(_) if !caption.is_empty() => {
                        ui.block(".menu-spacer", &[], |_| {});
                    }
                    MenuItemKind::Action | MenuItemKind::Cycler(_) => {}
                }
                if !caption.is_empty() {
                    hint = Some(ui.span(".menu-hint", caption.as_ref(), &style.hint).key);
                }
            })
            .key;
        BuiltRow {
            row,
            label: label.expect("every row builds its label"),
            hint,
            groove,
        }
    }

    /// Emits the menu's **pictures** alone: the scrim over the framebuffer, the
    /// window frame, and each button's frame for its state.
    ///
    /// The half of [`render`](Self::render) that is art: the same tree, with
    /// every command that is not a picture left out — the text, and a
    /// slider's groove and handle. The frames are nine-sliced at
    /// [`MenuStyle::scale`] pixels per texel, which is what keeps the art's
    /// corners the layout's corners.
    pub fn render_art(&self, dl: &mut DrawList, layout: &MenuLayout, skin: &MenuSkin) {
        let mut whole = DrawList::new();
        self.render(&mut whole, layout, skin);
        // Each under the clip it was drawn under, which a scrolled list's
        // frames need: a row half out of view is cut at the list's edge.
        for (command, clip) in whole.commands().iter().zip(whole.clips()) {
            if matches!(command, DrawCommand::Image { .. }) {
                dl.push_clip(clip.min, clip.max);
                dl.push_command(command.clone());
                dl.pop_clip()
                    .expect("the clip pushed above is still on the stack");
            }
        }
    }

    /// Emits the whole menu into `dl`: the tree [`Menu::layout_with`] measured
    /// at `layout`'s style and framebuffer, drawn with `skin`. Its text is
    /// measured in the font `layout` was — the built-in [`FontAtlas`], or the
    /// font [`Menu::layout_with_font`] was given — so it lands where `layout`
    /// says.
    ///
    /// **The order is the tree's paint order**: the scrim first, then the
    /// window frame and the title, then each row — its frame, then what sits
    /// on it. The draw list is drawn in the order commands went in, so the
    /// scrim dims what was pushed before this call, the frame covers the
    /// scrim, and every label is drawn over its own row's frame; rows do not
    /// overlap, so no frame covers another row's text.
    pub fn render(&self, dl: &mut DrawList, layout: &MenuLayout, skin: &MenuSkin) {
        self.laid_out(
            Some(layout.screen),
            &layout.style,
            Some(skin),
            &layout.setup,
            &FontAtlas::built_in(),
            |ui, _| ui.emit(dl),
        );
    }
}

thread_local! {
    /// The tree [`Menu::laid_out`] places and draws menus in; see there.
    static PLACED_TREE: std::cell::RefCell<Ui> = std::cell::RefCell::new(Ui::new());
    /// The tree [`Menu::panel_size`] measures in: apart from
    /// [`PLACED_TREE`], because [`Menu::layout`] measures at scale 1 and
    /// places at the scale it chose, and one tree alternating between the two
    /// would resolve and lay out every node from nothing on every call.
    static MEASURED_TREE: std::cell::RefCell<Ui> = std::cell::RefCell::new(Ui::new());
}

/// Where [`Menu::laid_out`]'s pointer is: above and left of every rectangle a
/// menu lays out, which all start at the origin.
const OFF_SCREEN: Vec2 = Vec2::splat(-1.0);

/// The family a font [`Menu::layout_with_font`] is given is registered under
/// in the menu trees, which are this module's own, so no app's name collides.
const MENU_FONT: &str = "crcbl-menu";

/// The names `default.css`'s menu rules give the [`MenuSkin`]'s frames, which
/// [`Menu::render`] binds with [`Ui::set_image`].
const PANEL_IMAGE: &str = "menu-panel";
/// See [`PANEL_IMAGE`].
const IDLE_IMAGE: &str = "menu-button-idle";
/// See [`PANEL_IMAGE`].
const HOVERED_IMAGE: &str = "menu-button-hovered";
/// See [`PANEL_IMAGE`].
const PRESSED_IMAGE: &str = "menu-button-pressed";

/// The nodes [`Menu::build`] made, for reading a layout back.
struct BuiltMenu {
    panel: NodeKey,
    title: Option<NodeKey>,
    /// The block the rows are in.
    items: NodeKey,
    /// The rows built, which a scrolled list's render limits to those in view.
    rows: Vec<BuiltRow>,
}

/// A slider row's groove: the tree's own slider drawing the well and the fill,
/// and a handle over it in a rail whose two spaces share out the free width by
/// the value — so the handle travels from flush left to flush right and hangs
/// over neither end. Returns the groove's key, which is the rectangle a
/// pointer's drag is measured against.
fn build_groove(ui: &mut Ui, slider: Slider, style: &RowStyle) -> NodeKey {
    use Declaration as D;
    let share = slider.position();
    ui.block(".menu-groove", &style.groove, |ui| {
        // The tree's slider reads its value and draws it; the menu's model is
        // what moves it, so the copy it is handed goes nowhere.
        let mut position = share;
        ui.slider(".menu-slider", &mut position, 0.0..=1.0, Slider::KEY_STEP);
        ui.block(".menu-thumb-rail", &[], |ui| {
            ui.block(".menu-thumb-space", &[D::FlexGrow(share)], |_| {});
            ui.block(".menu-thumb", &style.thumb, |_| {});
            ui.block(".menu-thumb-space", &[D::FlexGrow(1.0 - share)], |_| {});
        });
    })
    .key
}

/// The inline declarations every row of one menu is built with, at its style.
struct RowStyle {
    row: [Declaration; 8],
    label: [Declaration; 1],
    hint: [Declaration; 2],
    groove: [Declaration; 3],
    thumb: [Declaration; 2],
}

/// One row's nodes.
struct BuiltRow {
    row: NodeKey,
    label: NodeKey,
    hint: Option<NodeKey>,
    groove: Option<NodeKey>,
}

/// The share of the framebuffer a menu is allowed to fill before
/// [`Menu::layout`] drops to a smaller scale.
///
/// Nine tenths rather than all of it: a modal that reached the edge of the
/// window would have no frame visible on the short axis, and the scrim behind it
/// would have nothing to dim.
pub const FIT_FRACTION: f32 = 0.9;

/// How far apart the item font sizes [`Menu::layout_with_font_fitted`] tries
/// are, in pixels.
///
/// A quarter pixel rather than any size at all: a window dragged through
/// every size in between then draws at a bounded set of sizes, so the glyph
/// atlas is not handed a fresh rasterisation of every glyph for each pixel of
/// the drag, and the fit is a bisection over a known list.
pub const FIT_FONT_STEP: f32 = 0.25;

/// The item font sizes [`Menu::layout_with_font_fitted`] tries, largest first:
/// `largest` itself, then every [`FIT_FONT_STEP`] below it down to
/// [`MenuStyle::MIN_FONT_SIZE`]. Only `largest` when it is no bigger than
/// that.
fn fit_sizes(largest: f32) -> Vec<f32> {
    let mut sizes = vec![largest];
    let spare = largest - MenuStyle::MIN_FONT_SIZE;
    if spare > 0.0 {
        let steps = (spare / FIT_FONT_STEP).floor() as u32;
        sizes.extend(
            (0..=steps)
                .rev()
                .map(|step| MenuStyle::MIN_FONT_SIZE + step as f32 * FIT_FONT_STEP)
                .filter(|&size| size < largest),
        );
    }
    sizes
}

/// A fitted row's line pitch in `font` at `style`: its `line-height: normal`
/// rounded up to a whole pixel — see [`MenuStyle::shrunk`].
fn row_line(font: &Font, style: &MenuStyle) -> f32 {
    font.metrics().normal_line_height(style.item_size).ceil()
}

/// `extent` as a size in pixels.
fn extent_size(extent: (u32, u32)) -> Vec2 {
    Vec2::new(extent.0 as f32, extent.1 as f32)
}

/// Whether `point` is inside `rect`, edges included.
fn contains(rect: (Vec2, Vec2), point: Vec2) -> bool {
    point.x >= rect.0.x && point.x <= rect.1.x && point.y >= rect.0.y && point.y <= rect.1.y
}

/// How a menu's tree is built beyond its style: the font its text is in, and
/// what [`Menu::layout_with_font_fitted`] decided. The default is the bitmap
/// font and every row in the flow — what [`Menu::layout`] builds.
#[derive(Debug, Clone, PartialEq, Default)]
struct Setup {
    /// The font the text is measured and drawn in; `None` for the bitmap font.
    font: Option<Font>,
    /// Every row's line pitch, in whole pixels; `None` for the font's own.
    line: Option<f32>,
    /// The item list capped and scrolled; `None` for a list as tall as its
    /// rows.
    list: Option<ListView>,
}

/// A scrolled item list: see [`Menu::layout_with_font_fitted`].
#[derive(Debug, Clone, PartialEq)]
struct ListView {
    /// The list's width and its capped height, in whole pixels.
    size: Vec2,
    /// Where the list shows its rows: `(min, max)` in screen pixels.
    viewport: (Vec2, Vec2),
    /// How far the rows are scrolled, in whole pixels.
    scroll: f32,
    /// The furthest they can be.
    reach: f32,
    /// The rows at least partly in view: the ones built, drawn and hit.
    shown: Range<usize>,
    /// How far below the list's top the first shown row starts, unscrolled.
    shown_top: f32,
}

/// What [`Menu::measure`] reads off a menu.
struct Measured {
    panel: Vec2,
    list: Vec2,
    tallest: f32,
}

/// One size [`Menu::layout_with_font_fitted`] tries: the style and setup it
/// builds with, and what that measured.
struct Candidate {
    style: MenuStyle,
    setup: Setup,
    measured: Measured,
}

/// Why [`Menu::layout_with_font_fitted`] could not fit a menu, even at the
/// smallest size it may shrink to with its item list scrolled. Every length is
/// in pixels, and `room` is [`FIT_FRACTION`] of the framebuffer.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MenuFitError {
    /// The panel is wider than the room, and a list that scrolls up and down
    /// cannot make it narrower.
    TooWide {
        /// The item font size it was measured at.
        font_size: f32,
        /// The panel's width there.
        width: f32,
        /// The width it had to fit.
        room: f32,
    },
    /// The frame, the title and the captions leave less height than the
    /// list's tallest row — or the menu has no rows to scroll.
    TooShort {
        /// The item font size it was measured at.
        font_size: f32,
        /// The panel's height showing one row, its tallest.
        height: f32,
        /// The height it had to fit.
        room: f32,
    },
}

impl std::fmt::Display for MenuFitError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match *self {
            Self::TooWide {
                font_size,
                width,
                room,
            } => write!(
                f,
                "the menu is {width}px wide at a {font_size}px font, the smallest it may \
                 shrink to, and the window has room for {room}px"
            ),
            Self::TooShort {
                font_size,
                height,
                room,
            } => write!(
                f,
                "the menu needs {height}px to show one row at a {font_size}px font, the \
                 smallest it may shrink to, and the window has room for {room}px"
            ),
        }
    }
}

impl std::error::Error for MenuFitError {}

#[cfg(test)]
fn text_width(atlas: &FontAtlas, text: &str, size: f32) -> f32 {
    if text.is_empty() {
        return 0.0;
    }
    atlas.text_width(text, size / NATURAL_FONT_SIZE)
}

#[cfg(test)]
fn line_height(size: f32) -> f32 {
    LINE_HEIGHT * (size / NATURAL_FONT_SIZE)
}

/// Where the **centre** of a handle `handle_width` wide may travel inside
/// `track`, as `(leftmost, rightmost)`.
///
/// Half a handle in from each end, so position `0.0` and position `1.0` both
/// draw a handle wholly inside the groove — a handle centred on the end would
/// hang half of itself out over the button's face and read as a different
/// value at each extreme. A groove narrower than one handle collapses the
/// travel to its middle rather than inverting it.
fn handle_travel(track: (Vec2, Vec2), handle_width: f32) -> (f32, f32) {
    let half = (handle_width * 0.5)
        .min((track.1.x - track.0.x) * 0.5)
        .max(0.0);
    (track.0.x + half, track.1.x - half)
}

/// The position a pointer at `x` names on `track`, before clamping.
fn position_along(track: (Vec2, Vec2), handle_width: f32, x: f32) -> f32 {
    let (start, end) = handle_travel(track, handle_width);
    if end <= start {
        return 0.0;
    }
    (x - start) / (end - start)
}

// ---------------------------------------------------------------------------
// The layout
// ---------------------------------------------------------------------------

/// Where one item landed.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MenuItemLayout {
    /// The item this rectangle belongs to.
    pub id: WidgetId,
    /// The button's upper-left corner, in screen pixels.
    pub min: Vec2,
    /// The button's lower-right corner, in screen pixels.
    pub max: Vec2,
    /// Where the label's em box starts.
    pub label_pos: Vec2,
    /// Where the key hint's em box starts — right-aligned inside the button.
    pub hint_pos: Vec2,
    /// A [`MenuItemKind::Slider`] row's groove: `(min, max)` in screen pixels,
    /// between the label and the value. `None` for a button row.
    pub track: Option<(Vec2, Vec2)>,
}

impl MenuItemLayout {
    /// The button's size in pixels.
    #[must_use]
    pub fn size(&self) -> Vec2 {
        self.max - self.min
    }
}

/// One frame's placement of a menu: the panel, the title and every button, in
/// screen pixels.
///
/// Produced by [`Menu::layout`] and consumed by [`Menu::render`], for the art
/// and the text alike. Carrying the [`MenuStyle`] it was built with is what
/// stops the two being measured with different metrics — the failure that puts
/// a label a scale out of step with the frame around it.
#[derive(Debug, Clone, PartialEq)]
pub struct MenuLayout {
    style: MenuStyle,
    /// How the tree was built: the font the text was measured in and, for a
    /// fitted layout, the rows' line pitch and a scrolled list.
    setup: Setup,
    screen: Vec2,
    panel: (Vec2, Vec2),
    title_pos: Vec2,
    items: Vec<MenuItemLayout>,
}

impl MenuLayout {
    /// The style this layout was measured with.
    #[must_use]
    pub const fn style(&self) -> &MenuStyle {
        &self.style
    }

    /// The framebuffer this was laid out in, in pixels.
    #[must_use]
    pub const fn screen(&self) -> Vec2 {
        self.screen
    }

    /// The window frame's rectangle: `(min, max)` in screen pixels.
    #[must_use]
    pub const fn panel(&self) -> (Vec2, Vec2) {
        self.panel
    }

    /// The panel's size in pixels.
    #[must_use]
    pub fn panel_size(&self) -> Vec2 {
        self.panel.1 - self.panel.0
    }

    /// The middle of the panel, in screen pixels.
    #[must_use]
    pub fn panel_centre(&self) -> Vec2 {
        (self.panel.0 + self.panel.1) * 0.5
    }

    /// Where the title's em box starts.
    #[must_use]
    pub const fn title_pos(&self) -> Vec2 {
        self.title_pos
    }

    /// Every button, in the order they are drawn.
    ///
    /// In a scrolled list every row is here, where the scroll put it: a row
    /// scrolled out of view lies outside the [`viewport`](Self::viewport), and
    /// outside the panel too, and is neither drawn nor hit — see
    /// [`shows`](Self::shows).
    #[must_use]
    pub fn items(&self) -> &[MenuItemLayout] {
        &self.items
    }

    /// Where a scrolled item list shows its rows: `(min, max)` in screen
    /// pixels, inside the panel. `None` when the list is not scrolled and
    /// every row is in view.
    ///
    /// A row is drawn cut at this rectangle's edges and hit only inside it.
    #[must_use]
    pub fn viewport(&self) -> Option<(Vec2, Vec2)> {
        self.setup.list.as_ref().map(|list| list.viewport)
    }

    /// How far the item list is scrolled, in pixels: zero unless it scrolls.
    #[must_use]
    pub fn scroll(&self) -> f32 {
        self.setup.list.as_ref().map_or(0.0, |list| list.scroll)
    }

    /// Whether row `index` is at least partly in view, and so drawn and hit.
    /// Every row is, unless the list scrolls; no row past the last is.
    #[must_use]
    pub fn shows(&self, index: usize) -> bool {
        index < self.items.len()
            && self
                .setup
                .list
                .as_ref()
                .is_none_or(|list| list.shown.contains(&index))
    }

    /// The whole framebuffer, as the rectangle the scrim covers.
    #[must_use]
    pub fn scrim(&self) -> (Vec2, Vec2) {
        (Vec2::ZERO, self.screen)
    }
}

// ---------------------------------------------------------------------------
// Menu set
// ---------------------------------------------------------------------------

/// Every menu one game owns, which of them is on screen, and the pointer's
/// capture that outlives any single frame.
///
/// A [`Menu`] is one panel. A game has several — a start screen, a pause panel,
/// whatever it shows when a run ends — and needs exactly three things a single
/// menu cannot give it: a way to say which one this frame draws, a guarantee
/// that switching between two does not carry a half-finished click across, and
/// one [`UiState`] shared by all of them so a press captured on a button is the
/// same capture the release is tested against.
///
/// `K` names the panels. It is the game's own enum, not one this crate
/// dictates, because which states a game has is the one part of a menu system
/// that is genuinely per-game. **A `K` with no menu in the set draws nothing**,
/// which is how a "no menu this frame" state is spelled: give the set a `shown`
/// it holds no entry for — a `None` variant, or `false` for a game whose only
/// panel is a pause screen — and every method below becomes a no-op.
///
/// # What it does with the capture
///
/// [`UiState`] latches the widget a press started on so a release somewhere else
/// fires nothing. That latch has to survive between frames, which means it also
/// survives a menu being swapped out from under it, and a capture pointing at a
/// button that is no longer on screen is how a click on the pause menu ends up
/// firing whatever the start menu happens to draw in the same place. So both
/// [`show`](Self::show) and [`replace`](Self::replace) drop it.
///
/// # Example
///
/// ```
/// use crcbl_ui::menu::{Menu, MenuItem, MenuSet};
///
/// #[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// enum Kind {
///     None,
///     Paused,
/// }
///
/// let mut menus = MenuSet::new(
///     Kind::None,
///     vec![(
///         Kind::Paused,
///         Menu::new("PAUSED", vec![MenuItem::new(1, "RESUME", "ESC")]),
///     )],
/// );
///
/// // `Kind::None` has no menu, so the frame draws nothing and the keys do
/// // nothing.
/// assert!(!menus.is_showing());
/// assert_eq!(menus.activate(), None);
///
/// menus.show(Kind::Paused);
/// assert_eq!(menus.activate(), Some(1));
/// ```
#[derive(Debug)]
pub struct MenuSet<K> {
    /// The panels, keyed by the state each belongs to. A `Vec` rather than a
    /// map because a game has a handful and they are walked, never hashed.
    menus: Vec<(K, Menu)>,
    /// Which of them this frame draws.
    shown: K,
    /// The pointer's capture, shared by every menu in the set — see the type's
    /// docs for why it cannot be per-menu.
    ui: UiState,
}

impl<K: Copy + Eq> MenuSet<K> {
    /// A set of menus, with `shown` on screen.
    ///
    /// Pass the state that draws no menu as `shown` to open with none.
    ///
    /// # Panics
    ///
    /// If two entries name the same `K`. The second would be unreachable —
    /// every lookup here takes the first match — so a game that built one by
    /// mistake would have a panel it could never show and no way to notice.
    #[must_use]
    pub fn new(shown: K, menus: Vec<(K, Menu)>) -> Self {
        for (index, (kind, _)) in menus.iter().enumerate() {
            assert!(
                !menus[..index].iter().any(|(seen, _)| seen == kind),
                "two menus claim the same state",
            );
        }
        Self {
            menus,
            shown,
            ui: UiState::new(),
        }
    }

    /// Which menu is being shown.
    #[must_use]
    pub const fn kind(&self) -> K {
        self.shown
    }

    /// Whether this frame has a menu on it.
    ///
    /// False for a `shown` the set holds no menu for, which is what the games
    /// use to decide whether the menu keys are the menu's this frame or the
    /// simulation's.
    #[must_use]
    pub fn is_showing(&self) -> bool {
        self.current().is_some()
    }

    /// Switches to the menu this frame shows.
    ///
    /// A change drops the outgoing menu's hover and held key and the pointer's
    /// capture with it: a menu re-shown with a stale press on it draws a button
    /// nobody is touching, and a capture left in [`UiState`] would credit the
    /// next click to a widget that is no longer on screen.
    ///
    /// Showing what is already shown does nothing, so this is safe to call
    /// every frame — which is how the games call it.
    pub fn show(&mut self, kind: K) {
        if kind == self.shown {
            return;
        }
        if let Some(menu) = self.current_mut() {
            menu.clear_input();
        }
        self.ui.clear();
        self.shown = kind;
    }

    /// Swaps the menu a state draws, or adds it if the state had none.
    ///
    /// For a panel whose buttons are not known until the game is running — a
    /// level-up screen offering three upgrades drawn from a seed. The capture
    /// goes, for the same reason [`show`](Self::show) drops it: the button the
    /// press landed on no longer says what it said.
    ///
    /// The caller decides *when* the menu is stale. This rebuilds
    /// unconditionally, so calling it every frame would throw away the
    /// selection every frame.
    pub fn replace(&mut self, kind: K, menu: Menu) {
        match self.menus.iter_mut().find(|(seen, _)| *seen == kind) {
            Some(slot) => slot.1 = menu,
            None => self.menus.push((kind, menu)),
        }
        self.ui.clear();
    }

    /// The menu being shown, or `None` on a frame with no menu on it.
    #[must_use]
    pub fn current(&self) -> Option<&Menu> {
        let shown = self.shown;
        self.menus
            .iter()
            .find_map(|(kind, menu)| (*kind == shown).then_some(menu))
    }

    /// The menu being shown, mutably.
    pub fn current_mut(&mut self) -> Option<&mut Menu> {
        self.get_mut(self.shown)
    }

    /// One menu of the set by name, mutably, whether or not it is on screen.
    ///
    /// What a game refreshing a live value on a panel needs and
    /// [`current_mut`](Self::current_mut) cannot give it: `crcbl`'s loop asks
    /// the game which menu to show *after* the game has had its say, so during
    /// that call `current_mut` is still last frame's panel. Naming the menu is
    /// exact where "the current one" is a frame out of step.
    ///
    /// Unlike [`replace`](Self::replace) this keeps the panel's selection and
    /// the pointer's capture, which is what makes it safe to call every frame.
    pub fn get_mut(&mut self, kind: K) -> Option<&mut Menu> {
        self.menus
            .iter_mut()
            .find_map(|(seen, menu)| (*seen == kind).then_some(menu))
    }

    /// Moves the selection down, if there is a menu.
    pub fn select_next(&mut self) {
        if let Some(menu) = self.current_mut() {
            menu.select_next();
        }
    }

    /// Moves the selection up, if there is a menu.
    pub fn select_previous(&mut self) {
        if let Some(menu) = self.current_mut() {
            menu.select_previous();
        }
    }

    /// Holds the highlighted button down, or lets it up.
    pub fn press(&mut self, down: bool) {
        if let Some(menu) = self.current_mut() {
            menu.press(down);
        }
    }

    /// Fires the highlighted button, reporting the [`WidgetId`] it carries.
    pub fn activate(&mut self) -> Option<WidgetId> {
        self.current_mut().and_then(Menu::activate)
    }

    /// Moves the highlighted slider one step, if there is a menu.
    pub fn nudge_slider(&mut self, forward: bool) -> bool {
        self.current_mut()
            .is_some_and(|menu| menu.nudge_slider(forward))
    }

    /// Whether the showing menu's highlighted row is a slider.
    ///
    /// `false` when nothing is showing, so a caller asking "is this key the
    /// menu's?" gets the answer for the panel the player can see.
    #[must_use]
    pub fn slider_highlighted(&self) -> bool {
        self.current().is_some_and(Menu::slider_highlighted)
    }

    /// Steps the highlighted cycler one choice, stopping at the ends, if there
    /// is a menu.
    pub fn nudge_cycler(&mut self, forward: bool) -> bool {
        self.current_mut()
            .is_some_and(|menu| menu.nudge_cycler(forward))
    }

    /// Whether the showing menu's highlighted row is a cycler. `false` when
    /// nothing is showing, as [`slider_highlighted`](Self::slider_highlighted)
    /// is.
    #[must_use]
    pub fn cycler_highlighted(&self) -> bool {
        self.current().is_some_and(Menu::cycler_highlighted)
    }

    /// Whether a press is latched onto one of this set's buttons.
    ///
    /// The question a caller driving the menu from **something other than the
    /// pointer** has to ask: a contact is offered to [`point`](Self::point) once
    /// and then has to be remembered or forgotten, and this is what says which —
    /// a press that latched a button is one whose later events matter, and a
    /// press that landed on the panel's background is not the menu's at all.
    #[must_use]
    pub fn press_captured(&self) -> bool {
        self.ui.active().is_some()
    }

    /// Drops a press in progress **without firing it**.
    ///
    /// The [`Cancelled`](crcbl_core::input::TouchPhase::Cancelled) path, and the
    /// distinction is the same one [`crate::touch::TouchButton`] draws: a lift
    /// commits, a gesture the system took away does not. Without it a cancelled
    /// contact would leave [`UiState`]'s capture latched onto a button nobody is
    /// touching, and the next press could latch nothing at all.
    ///
    /// A press the pointer made is dropped by this too — there is one capture,
    /// and a caller that cancels one cancels the press that is live.
    pub fn cancel_press(&mut self) {
        self.ui.clear();
        if let Some(menu) = self.current_mut() {
            menu.clear_input();
        }
    }

    /// Runs one frame of pointer input against the menu on screen.
    ///
    /// The layout is recomputed here rather than kept, because it depends on
    /// the framebuffer's size and on the menu's own contents and both can
    /// change between frames — and a hit test against last frame's rectangles
    /// is how a resized window gets buttons that are not where they are drawn.
    pub fn point(
        &mut self,
        extent: (u32, u32),
        atlas: &FontAtlas,
        pointer: PointerInput,
    ) -> Option<WidgetId> {
        // The menus and the capture are borrowed as **separate fields**: a
        // `self.current_mut()` here would borrow the whole struct and `self.ui`
        // with it.
        let shown = self.shown;
        let menu = self
            .menus
            .iter_mut()
            .find_map(|(kind, menu)| (*kind == shown).then_some(menu))?;
        let layout = menu.layout(extent, atlas);
        menu.point(&layout, &mut self.ui, pointer)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::draw_list::DrawCommand;

    /// The five extents every "it is on screen" test in this repository uses:
    /// what a window opens at, a smaller 4:3, 16:9, a canvas clamped by
    /// `max-height: 68vh`, and one taller than it is wide.
    const EXTENTS: [(u32, u32); 5] = [
        (960, 720),
        (800, 600),
        (1920, 1080),
        (1440, 400),
        (600, 900),
    ];

    fn atlas() -> FontAtlas {
        FontAtlas::built_in()
    }

    /// A skin of five flat 16x16 images, each a different shade, with the
    /// shipped art's insets — enough for `render` to draw, and different
    /// enough that a frame drawn from the wrong image samples a different UV.
    fn skin() -> MenuSkin {
        let mut images = crate::image::ImageAtlas::new();
        let mut register = |shade: u8| {
            images
                .register(16, 16, &[shade; 16 * 16 * 4])
                .expect("five 16x16 images fit an empty page")
        };
        let mut sliced = |shade, insets| NineSliceImage {
            image: register(shade),
            insets,
        };
        let panel = sliced(10, PANEL_INSETS);
        let buttons = ButtonSkin {
            idle: sliced(20, BUTTON_INSETS),
            hovered: sliced(30, BUTTON_INSETS),
            pressed: sliced(40, BUTTON_INSETS),
        };
        MenuSkin {
            panel,
            buttons,
            scrim: register(255),
        }
    }

    fn pause_menu() -> Menu {
        Menu::new(
            "PAUSED",
            vec![
                MenuItem::new(1, "RESUME", "ESC"),
                MenuItem::new(2, "FULLSCREEN", "F11"),
                MenuItem::new(3, "DEBUG OVERLAY", "F3"),
            ],
        )
    }

    // -----------------------------------------------------------------------
    // In a font
    // -----------------------------------------------------------------------

    /// **A menu laid out in a font measures its text in it and draws it in
    /// it**: a row is its label's advances in the font plus the button's frame
    /// and padding, and one line of the font tall — where the same style in
    /// the atlas measures the atlas's width — and [`Menu::render`] draws that
    /// layout's label as a glyph run of the font. [`Menu::layout`] is what it
    /// was before, the font registered in the shared trees notwithstanding.
    #[cfg(feature = "parsed-font")]
    #[test]
    fn a_menu_laid_out_in_a_font_measures_and_draws_its_text_in_it() {
        use crate::font::FontMetrics;

        let font = Font::fixed_pitch(
            FontMetrics {
                units_per_em: 1000,
                ascent: 800.0,
                descent: -200.0,
                line_gap: 0.0,
            },
            500.0,
        );
        let menu = Menu::new("", vec![MenuItem::new(1, "RESUME", "")]);
        let style = MenuStyle::pixel_art(1);
        let extent = (960, 720);
        let before = menu.layout(extent, &atlas());

        let in_font = menu.layout_with_font(extent, &style, font);
        let in_atlas = menu.layout_with(extent, &atlas(), &style);
        let corners = style.button_corners();
        let chrome = Vec2::new(
            corners.left + corners.right + 2.0 * style.button_padding.x,
            corners.top + corners.bottom + 2.0 * style.button_padding.y,
        );
        let label = |layout: &MenuLayout| layout.items()[0].size() - chrome;
        assert_eq!(
            label(&in_font),
            Vec2::new(6.0 * 0.5 * style.item_size, style.item_size),
            "the row was not measured in the font"
        );
        assert_eq!(
            label(&in_atlas),
            Vec2::new(
                text_width(&atlas(), "RESUME", style.item_size),
                line_height(style.item_size)
            )
        );
        assert_ne!(label(&in_font), label(&in_atlas));

        let drawn = |layout: &MenuLayout| {
            let mut list = DrawList::new();
            menu.render(&mut list, layout, &skin());
            let runs: Vec<_> = list
                .commands()
                .iter()
                .filter_map(|command| match command {
                    DrawCommand::Glyphs { font, .. } => Some(font.id()),
                    _ => None,
                })
                .collect();
            let texts = list
                .commands()
                .iter()
                .filter(|command| matches!(command, DrawCommand::Text { .. }))
                .count();
            (runs, texts)
        };
        assert_eq!(drawn(&in_font), (vec![font.id()], 0));
        assert_eq!(drawn(&in_atlas), (Vec::new(), 1));

        assert_eq!(menu.layout(extent, &atlas()), before);
    }

    // -----------------------------------------------------------------------
    // Captions
    // -----------------------------------------------------------------------

    /// [`pause_menu`] with two lines under its title.
    fn captioned_menu() -> Menu {
        let mut menu = pause_menu();
        menu.subtitle = vec!["ARROWS MOVE".into(), "ENTER PICKS".into()];
        menu
    }

    /// The `(text, pos, colour)` of every bitmap string `dl` draws.
    fn texts(dl: &DrawList) -> Vec<(String, Vec2, [f32; 4])> {
        dl.commands()
            .iter()
            .filter_map(|command| match command {
                DrawCommand::Text {
                    text, pos, color, ..
                } => Some((text.clone(), *pos, *color)),
                _ => None,
            })
            .collect()
    }

    /// **The lines under the title are drawn there, in the hint's colour, and
    /// inside the panel** — between the title and the first row, in the
    /// order given — and the rows are laid out below them rather than over
    /// them.
    #[test]
    fn captions_are_drawn_under_the_title_in_the_hint_colour() {
        let atlas = atlas();
        let menu = captioned_menu();
        let layout = menu.layout((960, 720), &atlas);
        let mut dl = DrawList::new();
        menu.render(&mut dl, &layout, &skin());
        let drawn = texts(&dl);
        let line = |wanted: &str| {
            drawn
                .iter()
                .find(|(text, ..)| text == wanted)
                .unwrap_or_else(|| panic!("{wanted} is not drawn: {drawn:?}"))
                .clone()
        };
        let (_, first, first_colour) = line("ARROWS MOVE");
        let (_, second, second_colour) = line("ENTER PICKS");
        assert_eq!(first_colour, layout.style().hint_color);
        assert_eq!(second_colour, layout.style().hint_color);

        let title_bottom = layout.title_pos().y + line_height(layout.style().title_size);
        let caption_bottom = second.y + line_height(layout.style().item_size);
        let (panel_min, panel_max) = layout.panel();
        assert!(
            title_bottom <= first.y && first.y < second.y,
            "the captions are not under the title, in order: {first:?} {second:?}"
        );
        assert!(
            caption_bottom <= layout.items()[0].min.y,
            "the first row starts at {} over a caption ending at {caption_bottom}",
            layout.items()[0].min.y
        );
        for pos in [first, second] {
            assert!(pos.x >= panel_min.x && pos.x <= panel_max.x, "{pos:?}");
        }
    }

    /// **A caption takes no input from either device.** The keyboard walks
    /// the three rows and wraps as it did without captions, the commit key
    /// fires the rows' ids, and a pointer over a caption hovers and clicks
    /// nothing — the highlight stays with the keyboard.
    #[test]
    fn captions_are_skipped_by_the_keyboard_and_the_pointer() {
        let atlas = atlas();
        let mut menu = captioned_menu();
        let mut fired = Vec::new();
        for _ in 0..=menu.items().len() {
            fired.push(menu.activate());
            menu.select_next();
        }
        assert_eq!(fired, [Some(1), Some(2), Some(3), Some(1)]);
        assert!(menu.select_id(1));
        menu.select_previous();
        assert_eq!(menu.activate(), Some(3), "the wrap upward went astray");
        assert!(menu.select_id(1));

        let layout = menu.layout((960, 720), &atlas);
        let mut dl = DrawList::new();
        menu.render(&mut dl, &layout, &skin());
        let (_, caption, _) = texts(&dl)
            .into_iter()
            .find(|(text, ..)| text == "ENTER PICKS")
            .expect("the caption is drawn");
        let on_caption = caption + Vec2::new(2.0, 2.0);
        let mut ui = UiState::new();
        assert_eq!(menu.point(&layout, &mut ui, press_at(on_caption)), None);
        assert_eq!(menu.point(&layout, &mut ui, release_at(on_caption)), None);
        assert_eq!(
            menu.state(0),
            ButtonState::Hovered,
            "the caption took the hover"
        );
        assert_eq!(menu.selected(), 0);
    }

    /// [`captioned_menu`] with a warning between its two hint lines.
    fn warned_menu() -> Menu {
        let mut menu = captioned_menu();
        menu.subtitle
            .insert(1, Caption::warning("PROGRESS WILL BE LOST"));
        menu
    }

    /// **Each caption is drawn in its tone's colour**: a warning in the
    /// style's warning colour and the hint lines round it in the hint's,
    /// in the order given.
    #[test]
    fn a_warning_caption_is_drawn_in_the_warning_colour() {
        let menu = warned_menu();
        let layout = menu.layout((960, 720), &atlas());
        let style = layout.style();
        assert_ne!(style.warning_color, style.hint_color);
        let mut dl = DrawList::new();
        menu.render(&mut dl, &layout, &skin());
        let captions: Vec<(String, [f32; 4])> = texts(&dl)
            .into_iter()
            .filter(|(text, ..)| menu.subtitle.iter().any(|line| &line.text == text))
            .map(|(text, _, colour)| (text, colour))
            .collect();
        assert_eq!(
            captions,
            [
                ("ARROWS MOVE".to_owned(), style.hint_color),
                ("PROGRESS WILL BE LOST".to_owned(), style.warning_color),
                ("ENTER PICKS".to_owned(), style.hint_color),
            ]
        );
    }

    /// **A caption's tone moves nothing but its colour.** The same lines
    /// as hints and as warnings lay out alike, fitted in a font too; the
    /// keyboard walks the same rows; and a pointer on a warning hits
    /// nothing.
    #[test]
    fn a_captions_tone_changes_no_row_or_hit() {
        let atlas = atlas();
        let hinted = captioned_menu();
        let mut warned = captioned_menu();
        for line in &mut warned.subtitle {
            line.tone = CaptionTone::Warning;
        }
        assert_eq!(
            warned.layout((960, 720), &atlas),
            hinted.layout((960, 720), &atlas)
        );
        #[cfg(feature = "parsed-font")]
        assert_eq!(
            warned.layout_with_font_fitted(
                (480, 360),
                &MenuStyle::pixel_art(2),
                font_like_roboto()
            ),
            hinted.layout_with_font_fitted(
                (480, 360),
                &MenuStyle::pixel_art(2),
                font_like_roboto()
            ),
        );

        let mut fired = Vec::new();
        for _ in 0..=warned.items().len() {
            fired.push(warned.activate());
            warned.select_next();
        }
        assert_eq!(fired, [Some(1), Some(2), Some(3), Some(1)]);
        assert!(warned.select_id(1));

        let layout = warned.layout((960, 720), &atlas);
        let mut dl = DrawList::new();
        warned.render(&mut dl, &layout, &skin());
        let (_, caption, colour) = texts(&dl)
            .into_iter()
            .find(|(text, ..)| text == "ENTER PICKS")
            .expect("the caption is drawn");
        assert_eq!(colour, layout.style().warning_color);
        let on_caption = caption + Vec2::new(2.0, 2.0);
        let mut ui = UiState::new();
        assert_eq!(warned.point(&layout, &mut ui, press_at(on_caption)), None);
        assert_eq!(warned.point(&layout, &mut ui, release_at(on_caption)), None);
        assert_eq!(warned.state(0), ButtonState::Hovered);
        assert_eq!(warned.selected(), 0);
    }

    /// **A stylesheet's `.menu-caption-warning` rule sets a warning's
    /// colour**, over `default.css`'s, and leaves the hint lines alone.
    #[test]
    fn a_stylesheet_rule_sets_the_warning_colour() {
        let menu = warned_menu();
        let style = MenuStyle::pixel_art(1);
        let red = [1.0, 0.0, 0.0, 1.0];
        let mut ui = Ui::new();
        ui.add_stylesheet(
            "game",
            ".menu-caption-warning { color: color(srgb-linear 1 0 0); }",
        );
        ui.begin_frame(PointerInput::hovering(OFF_SCREEN));
        menu.build(
            &mut ui,
            Some(Vec2::new(960.0, 720.0)),
            &style,
            None,
            &Setup::default(),
        );
        ui.layout(
            Vec2::ZERO,
            AvailableSpace::definite(Vec2::new(960.0, 720.0)),
            &atlas(),
        );
        let mut dl = DrawList::new();
        ui.emit(&mut dl);
        let colour = |wanted: &str| {
            texts(&dl)
                .into_iter()
                .find_map(|(text, _, colour)| (text == wanted).then_some(colour))
                .unwrap_or_else(|| panic!("{wanted} is not drawn"))
        };
        assert_eq!(colour("PROGRESS WILL BE LOST"), red);
        assert_eq!(colour("ARROWS MOVE"), style.hint_color);
    }

    /// A menu of captions and no rows has nothing to select or fire, and lays
    /// out, fitted or not, without an index to panic on.
    #[cfg(feature = "parsed-font")]
    #[test]
    fn a_menu_of_only_captions_selects_nothing() {
        let mut menu = Menu::new("NOTICE", Vec::new());
        menu.subtitle = vec![Caption::warning("NOTHING TO CHOOSE")];
        menu.select_next();
        menu.select_previous();
        assert_eq!(menu.activate(), None);
        assert_eq!(menu.selected_item(), None);
        let layout = menu.layout((960, 720), &atlas());
        assert!(layout.items().is_empty());
        let fitted = menu
            .layout_with_font_fitted((480, 360), &MenuStyle::pixel_art(1), font_like_roboto())
            .expect("a notice fits");
        assert!(fitted.items().is_empty() && fitted.viewport().is_none());
    }

    // -----------------------------------------------------------------------
    // Fitted in a font, and scrolled
    // -----------------------------------------------------------------------

    /// Every glyph's advance in [`font_like_roboto`], in ems.
    #[cfg(feature = "parsed-font")]
    const ADVANCE_EM: f32 = 0.55;

    /// A stand-in for a parsed UI font such as Roboto: every glyph
    /// [`ADVANCE_EM`] wide, on a line 1.172 em tall, as Roboto's own metrics
    /// give it. One parse per process, as an app's registered font is, so two
    /// layouts in it compare equal.
    #[cfg(feature = "parsed-font")]
    fn font_like_roboto() -> &'static Font {
        use crate::font::FontMetrics;
        static FONT: std::sync::OnceLock<&'static Font> = std::sync::OnceLock::new();
        FONT.get_or_init(|| {
            Font::fixed_pitch(
                FontMetrics {
                    units_per_em: 1000,
                    ascent: 928.0,
                    descent: -244.0,
                    line_gap: 0.0,
                },
                ADVANCE_EM * 1000.0,
            )
        })
    }

    /// The row count of [`scene_menu`].
    #[cfg(feature = "parsed-font")]
    const SCENES: usize = 17;

    /// A stand-in for EW's scene menu: [`SCENES`] rows, each a label and a
    /// one-line description in the hint slot — the longest seventy
    /// characters — under a title and two captions, the second a warning.
    #[cfg(feature = "parsed-font")]
    fn scene_menu() -> Menu {
        let items = (0..SCENES)
            .map(|index| {
                let description = format!("{:02} {}", index, "a".repeat(40 + index * 2))
                    .chars()
                    .take(70)
                    .collect::<String>();
                MenuItem::new(
                    100 + index as WidgetId,
                    format!("SCENE {index:02} NAME"),
                    description,
                )
            })
            .collect();
        let mut menu = Menu::new("SCENES", items);
        menu.subtitle = vec![
            "UP/DOWN MOVES - ENTER LOADS - ESC CLOSES".into(),
            Caption::warning("LOADING A SCENE DISCARDS UNSAVED WORK"),
        ];
        menu
    }

    /// The smallest window EW supports.
    #[cfg(feature = "parsed-font")]
    const SMALL: (u32, u32) = (480, 360);

    /// The fitted layout of `menu` at `extent`, from EW's style.
    #[cfg(feature = "parsed-font")]
    fn fitted(menu: &Menu, extent: (u32, u32)) -> MenuLayout {
        menu.layout_with_font_fitted(extent, &MenuStyle::pixel_art(2), font_like_roboto())
            .unwrap_or_else(|error| panic!("{extent:?}: {error}"))
    }

    /// Whether `inner` lies wholly inside `outer`.
    #[cfg(feature = "parsed-font")]
    fn within(inner: (Vec2, Vec2), outer: (Vec2, Vec2)) -> bool {
        inner.0.x >= outer.0.x
            && inner.0.y >= outer.0.y
            && inner.1.x <= outer.1.x
            && inner.1.y <= outer.1.y
    }

    /// **Seventeen rows with descriptions and two captions fit a 480x360
    /// window.** The panel is inside the fit's share of the framebuffer; the
    /// font is no smaller than the minimum; the art is at a whole-number
    /// scale with the art's corners; seventeen rows do not fit that height
    /// at any legible size, so the list scrolls, and its viewport is inside
    /// the panel, every row drawn is inside the panel's width and reaches
    /// into the viewport, and the widest description ends inside its row.
    /// The same inputs lay out the same menu.
    #[cfg(feature = "parsed-font")]
    #[test]
    fn a_seventeen_row_menu_fits_a_480_by_360_window() {
        let menu = scene_menu();
        let layout = fitted(&menu, SMALL);
        let style = layout.style();
        let screen = extent_size(SMALL);
        let panel = layout.panel();
        assert!(
            within(panel, (Vec2::ZERO, screen)),
            "the panel {panel:?} leaves the window"
        );
        assert!(layout.panel_size().cmple(screen * FIT_FRACTION).all());
        assert!(
            style.item_size >= MenuStyle::MIN_FONT_SIZE,
            "{}",
            style.item_size
        );
        assert!(
            style.scale >= 1.0 && style.scale.fract() == 0.0,
            "{}",
            style.scale
        );
        assert_eq!(style.panel_corners().left, PANEL_INSETS.left * style.scale);

        let viewport = layout
            .viewport()
            .expect("seventeen rows scroll at 360 tall");
        assert!(within(viewport, panel), "{viewport:?} leaves {panel:?}");
        let shown: Vec<usize> = (0..SCENES).filter(|&index| layout.shows(index)).collect();
        assert!(shown.len() > 1 && shown.len() < SCENES, "{shown:?}");
        for &index in &shown {
            let row = layout.items()[index];
            assert!(
                row.min.x >= panel.0.x && row.max.x <= panel.1.x,
                "row {index} {row:?} is wider than the panel"
            );
            assert!(
                row.max.y > viewport.0.y && row.min.y < viewport.1.y,
                "row {index} is drawn but nowhere in view"
            );
            let description = &menu.items()[index].hint;
            let end =
                row.hint_pos.x + description.chars().count() as f32 * ADVANCE_EM * style.item_size;
            assert!(
                end <= row.max.x + 0.5,
                "row {index}'s description ends at {end}"
            );
        }
        assert!(within(
            (layout.items()[0].min, layout.items()[0].max),
            viewport
        ));
        assert_eq!(fitted(&menu, SMALL), layout, "the fit is not deterministic");
    }

    /// **The fit is the largest step that fits.** At a window big enough to
    /// fit without scrolling but too small for the ceiling, the chosen size
    /// fits and the next step up does not; and a window big enough for the
    /// ceiling gets the ceiling, unscrolled.
    #[cfg(feature = "parsed-font")]
    #[test]
    fn the_fit_takes_the_largest_step_that_fits() {
        let menu = scene_menu();
        let ceiling = MenuStyle::pixel_art(2);
        let font = font_like_roboto();
        let extent = (1100, 900);
        let layout = fitted(&menu, extent);
        let chosen = layout.style().item_size;
        assert!(layout.viewport().is_none(), "{extent:?} scrolled");
        assert!(
            chosen < ceiling.item_size && chosen > MenuStyle::MIN_FONT_SIZE,
            "{extent:?} does not exercise the shrink: {chosen}"
        );
        let panel_at = |size: f32| {
            let style = ceiling.shrunk(size);
            let setup = Setup {
                font: Some(font.clone()),
                line: Some(row_line(font, &style)),
                list: None,
            };
            menu.measure(&style, &setup).panel
        };
        let room = extent_size(extent) * FIT_FRACTION;
        assert!(panel_at(chosen).cmple(room).all());
        assert!(
            !panel_at(chosen + FIT_FONT_STEP).cmple(room).all(),
            "a step larger than {chosen} fits too"
        );

        let roomy = fitted(&menu, (3840, 2160));
        assert_eq!(roomy.style().item_size, ceiling.item_size);
        assert_eq!(roomy.style().scale, ceiling.scale);
        assert!(roomy.viewport().is_none());
    }

    /// **Below the minimum the fit refuses, and says why.** A window too
    /// narrow for the descriptions at [`MenuStyle::MIN_FONT_SIZE`] is too
    /// wide a menu, which scrolling cannot help; a window too short for the
    /// title, the captions and one row is too short. Neither is laid out at
    /// a smaller size than the minimum.
    #[cfg(feature = "parsed-font")]
    #[test]
    fn the_fit_refuses_honestly_below_the_minimum() {
        let menu = scene_menu();
        let style = MenuStyle::pixel_art(2);
        let font = font_like_roboto();
        match menu.layout_with_font_fitted((300, 360), &style, font) {
            Err(MenuFitError::TooWide {
                font_size,
                width,
                room,
            }) => {
                assert_eq!(font_size, MenuStyle::MIN_FONT_SIZE);
                assert!(
                    width > room && room == 300.0 * FIT_FRACTION,
                    "{width} {room}"
                );
            }
            other => panic!("a 300px window gave {other:?}"),
        }
        match menu.layout_with_font_fitted((480, 60), &style, font) {
            Err(MenuFitError::TooShort {
                font_size,
                height,
                room,
            }) => {
                assert_eq!(font_size, MenuStyle::MIN_FONT_SIZE);
                assert!(height > room, "{height} {room}");
            }
            other => panic!("a 60px window gave {other:?}"),
        }
    }

    /// **The keyboard keeps its selection in view, all the way down and all
    /// the way back.** Every step of a walk to the last row and back to the
    /// first leaves the selected row wholly inside the viewport, both for a
    /// caller that only lays out and draws and for one that also runs the
    /// pointer — which keeps the list still while the selection moves inside
    /// it, as a list does.
    #[cfg(feature = "parsed-font")]
    #[test]
    fn scrolling_keeps_the_selected_row_in_view_down_and_up() {
        for with_pointer in [false, true] {
            let mut menu = scene_menu();
            let mut ui = UiState::new();
            let mut scrolls = Vec::new();
            let mut step = |menu: &mut Menu, moved: &str| {
                let layout = fitted(menu, SMALL);
                let viewport = layout.viewport().expect("the list scrolls");
                let selected = layout.items()[menu.selected()];
                assert!(
                    layout.shows(menu.selected()) && within((selected.min, selected.max), viewport),
                    "after {moved} to row {}, {selected:?} is not wholly in {viewport:?}",
                    menu.selected(),
                );
                if with_pointer {
                    menu.point(&layout, &mut ui, PointerInput::hovering(OFF_SCREEN));
                }
                scrolls.push(layout.scroll());
            };
            step(&mut menu, "opening");
            for _ in 1..SCENES {
                menu.select_next();
                step(&mut menu, "down");
            }
            assert_eq!(menu.selected(), SCENES - 1);
            for _ in 1..SCENES {
                menu.select_previous();
                step(&mut menu, "up");
            }
            assert_eq!(menu.selected(), 0);
            assert!(scrolls[SCENES - 1] > 0.0, "the walk down never scrolled");
            assert_eq!(*scrolls.last().expect("walked"), 0.0);
            if with_pointer {
                // One step up from the bottom stays inside the view, so the
                // list does not move.
                assert_eq!(scrolls[SCENES], scrolls[SCENES - 1], "{scrolls:?}");
            }
        }
    }

    /// **A row scrolled out of view is neither drawn nor hit.** With the last
    /// row selected the first is scrolled off the top: a press where it was
    /// placed hovers and fires nothing, a row in view still answers, and the
    /// draw list holds the title, the captions and the shown rows' text and
    /// frames and nothing of the others.
    #[cfg(feature = "parsed-font")]
    #[test]
    fn rows_scrolled_out_of_view_are_neither_drawn_nor_hit() {
        let mut menu = scene_menu();
        menu.select_previous();
        let last = SCENES - 1;
        assert_eq!(menu.selected(), last);
        let layout = fitted(&menu, SMALL);
        let viewport = layout.viewport().expect("the list scrolls");
        assert!(!layout.shows(0), "the first row is still in view");
        assert!(layout.shows(last));

        let mut ui = UiState::new();
        let hidden = layout.items()[0];
        let where_it_was = (hidden.min + hidden.max) * 0.5;
        assert_eq!(menu.point(&layout, &mut ui, press_at(where_it_was)), None);
        assert_eq!(menu.point(&layout, &mut ui, release_at(where_it_was)), None);
        assert_eq!(
            menu.state(0),
            ButtonState::Idle,
            "a hidden row took the hover"
        );
        let visible = layout.items()[last];
        let on_it = (visible.min + visible.max) * 0.5;
        menu.point(&layout, &mut ui, press_at(on_it));
        assert_eq!(
            menu.point(&layout, &mut ui, release_at(on_it)),
            Some(100 + last as WidgetId)
        );

        let shown = (0..SCENES).filter(|&index| layout.shows(index)).count();
        let mut dl = DrawList::new();
        menu.render(&mut dl, &layout, &skin());
        let runs: Vec<Vec2> = dl
            .commands()
            .iter()
            .filter_map(|command| match command {
                DrawCommand::Glyphs { origin, .. } => Some(*origin),
                _ => None,
            })
            .collect();
        assert_eq!(runs.len(), 1 + 2 + 2 * shown, "title, captions, shown rows");
        let frames = pictures(&dl).len();
        assert_eq!(
            frames,
            1 + 9 + 9 * shown,
            "scrim, panel, shown rows' frames"
        );
        // Every row's text starts inside the viewport's reach: none is drawn
        // at a hidden row's place.
        for origin in &runs[3..] {
            assert!(
                origin.y + layout.style().item_size > viewport.0.y && origin.y < viewport.1.y,
                "a row's text at {origin:?} is out of {viewport:?}"
            );
        }
        // And the art alone keeps the list's clip.
        let mut art = DrawList::new();
        menu.render_art(&mut art, &layout, &skin());
        assert_eq!(art.len(), frames);
        assert!(
            art.clips()[10..]
                .iter()
                .all(|clip| clip.min == viewport.0 && clip.max == viewport.1),
            "a row's frame lost the list's clip"
        );
    }

    /// **The wheel scrolls the list, and the keyboard takes it back.** A
    /// wheel over the panel moves the list and reports it — even off the
    /// selection, which stays where it was — a wheel off the panel, or past
    /// the end, reports nothing, and the next keyboard move scrolls its row
    /// back into view.
    #[cfg(feature = "parsed-font")]
    #[test]
    fn the_wheel_scrolls_the_list_and_the_keyboard_takes_it_back() {
        let mut menu = scene_menu();
        let layout = fitted(&menu, SMALL);
        let (panel_min, panel_max) = layout.panel();
        let over = (panel_min + panel_max) * 0.5;
        assert_eq!(layout.scroll(), 0.0);
        assert!(!menu.scroll_wheel(&layout, Vec2::new(1.0, 1.0), Vec2::new(0.0, 60.0)));
        assert!(menu.scroll_wheel(&layout, over, Vec2::new(0.0, 60.0)));
        let wheeled = fitted(&menu, SMALL);
        assert_eq!(wheeled.scroll(), 60.0, "the wheel did not move the list");
        assert_eq!(menu.selected(), 0);
        assert!(!wheeled.shows(0), "60px should scroll the first row away");

        assert!(menu.scroll_wheel(&wheeled, over, Vec2::new(0.0, 10_000.0)));
        let bottom = fitted(&menu, SMALL);
        assert!(!menu.scroll_wheel(&bottom, over, Vec2::new(0.0, 10.0)));
        assert!(bottom.shows(SCENES - 1));

        menu.select_next();
        let back = fitted(&menu, SMALL);
        let row = back.items()[1];
        assert!(within(
            (row.min, row.max),
            back.viewport().expect("scrolls")
        ));

        // A menu that fits unscrolled has no list for the wheel to move.
        let small = pause_menu();
        let unscrolled = small.layout((960, 720), &atlas());
        assert!(!menu.clone().scroll_wheel(
            &unscrolled,
            unscrolled.panel_centre(),
            Vec2::new(0.0, 10.0)
        ));
    }

    // -----------------------------------------------------------------------
    // Centred
    // -----------------------------------------------------------------------

    /// **The ask, as arithmetic.** The panel's centre is the framebuffer's
    /// centre, at every aspect ratio and every size — not "on screen", which a
    /// menu jammed into the top-left corner also is.
    ///
    /// Half a pixel of tolerance and no more: [`Menu::layout_with`] rounds the
    /// panel's origin to a whole pixel so the nine-slice's corners land on texel
    /// boundaries, and a size of odd parity then puts the centre on a half pixel.
    /// Anything looser would admit a menu that was merely near the middle.
    #[test]
    fn the_panel_is_centred_at_every_aspect_ratio() {
        let menu = pause_menu();
        let atlas = atlas();
        for extent in EXTENTS {
            let layout = menu.layout(extent, &atlas);
            let screen = Vec2::new(extent.0 as f32, extent.1 as f32);
            let offset = layout.panel_centre() - screen * 0.5;
            assert!(
                offset.x.abs() <= 0.5 && offset.y.abs() <= 0.5,
                "{extent:?}: the panel's centre is {offset:?} off the screen's",
            );
        }
    }

    /// And it is centred at every scale, not only the one the fit happened to
    /// choose — so a game that pins a style still gets a centred menu.
    #[test]
    fn the_panel_is_centred_at_every_scale() {
        let menu = pause_menu();
        let atlas = atlas();
        for scale in 1..=MenuStyle::MAX_SCALE {
            let style = MenuStyle::pixel_art(scale);
            for extent in EXTENTS {
                let layout = menu.layout_with(extent, &atlas, &style);
                let screen = Vec2::new(extent.0 as f32, extent.1 as f32);
                let offset = layout.panel_centre() - screen * 0.5;
                assert!(
                    offset.x.abs() <= 0.5 && offset.y.abs() <= 0.5,
                    "scale {scale} at {extent:?}: centre off by {offset:?}",
                );
            }
        }
    }

    /// Everything the menu draws is inside the panel, and the panel is inside
    /// the framebuffer. Without this the centring test above would pass on a
    /// panel so large it hung off both edges symmetrically.
    #[test]
    fn the_whole_menu_is_inside_the_screen_at_every_aspect_ratio() {
        let menu = pause_menu();
        let atlas = atlas();
        for extent in EXTENTS {
            let layout = menu.layout(extent, &atlas);
            let (min, max) = layout.panel();
            assert!(
                min.x >= 0.0
                    && min.y >= 0.0
                    && max.x <= extent.0 as f32
                    && max.y <= extent.1 as f32,
                "{extent:?}: the panel {min:?}..{max:?} leaves the framebuffer",
            );
            for item in layout.items() {
                assert!(
                    item.min.x >= min.x
                        && item.min.y >= min.y
                        && item.max.x <= max.x
                        && item.max.y <= max.y,
                    "{extent:?}: a button at {:?}..{:?} escapes the panel {min:?}..{max:?}",
                    item.min,
                    item.max,
                );
            }
        }
    }

    /// The chosen scale is the largest that fits, which is what makes the menu
    /// grow with the window rather than sit at a fixed size in the middle of a
    /// 4K screen.
    #[test]
    fn a_bigger_window_gets_a_bigger_menu() {
        let menu = pause_menu();
        let atlas = atlas();
        let small = menu.layout((640, 480), &atlas);
        let large = menu.layout((1920, 1440), &atlas);
        assert!(
            large.style().scale > small.style().scale,
            "640x480 chose {} and 1920x1440 chose {}",
            small.style().scale,
            large.style().scale,
        );
        // And the small one is not merely a scale-1 fallback: the fit must have
        // been exercised at more than one candidate.
        assert!(small.style().scale >= 1.0);
        assert!(large.style().scale <= MenuStyle::MAX_SCALE as f32);
    }

    // -----------------------------------------------------------------------
    // The nine-slice property, at the menu's level
    // -----------------------------------------------------------------------

    /// **The window frame's corners keep their size as the menu grows.**
    ///
    /// A menu with one short item and one with five long ones produce very
    /// different panels at the same scale, and the fixed bands the art refuses to
    /// stretch are the same number of pixels in both. This is the layout half of
    /// the property [`DrawList::nine_slice`] implements — a caller that laid out
    /// against a corner that scaled with the panel would put its content over the
    /// frame at one size and inside it at another.
    #[test]
    fn the_frames_corners_do_not_grow_with_the_menu() {
        let atlas = atlas();
        let style = MenuStyle::pixel_art(3);
        let small = Menu::new("GO", vec![MenuItem::new(1, "OK", "")]);
        let large = Menu::new(
            "A MUCH LONGER TITLE",
            (0..5)
                .map(|i| MenuItem::new(i, "A VERY LONG MENU ITEM LABEL", "SHIFT+F11"))
                .collect(),
        );

        let small_size = small.panel_size(&atlas, &style);
        let large_size = large.panel_size(&atlas, &style);
        assert!(
            large_size.x > small_size.x * 1.5 && large_size.y > small_size.y * 1.5,
            "the two menus are not different enough to prove anything: \
             {small_size:?} vs {large_size:?}",
        );

        // The corners are the same pixels in both, and they are the art's insets
        // times the scale rather than a fraction of the panel.
        let corners = style.panel_corners();
        assert_eq!(corners.left, PANEL_INSETS.left * style.scale);
        assert_eq!(corners.bottom, PANEL_INSETS.bottom * style.scale);

        // And the content box of each sits exactly that far inside its panel, so
        // the claim is about the layout and not only about the constant.
        for (menu, size) in [(&small, small_size), (&large, large_size)] {
            let layout = menu.layout_with((1600, 1200), &atlas, &style);
            let (min, max) = layout.panel();
            assert_eq!(max - min, size);
            let item = layout.items()[0];
            assert_eq!(
                item.min.x - min.x,
                corners.left + style.panel_padding.x,
                "the first button is not one fixed corner plus the padding in",
            );
            assert_eq!(max.x - item.max.x, corners.right + style.panel_padding.x);
        }
    }

    /// Every button in one menu is the same size, whatever its label — a menu
    /// whose buttons were shrink-to-fit reads as a ragged list rather than as a
    /// panel of choices.
    #[test]
    fn every_button_is_the_same_size() {
        let atlas = atlas();
        let menu = Menu::new(
            "T",
            vec![
                MenuItem::new(1, "X", ""),
                MenuItem::new(2, "A MUCH LONGER LABEL", "F11"),
                MenuItem::new(3, "MID", "Q"),
            ],
        );
        let layout = menu.layout((1280, 960), &atlas);
        let first = layout.items()[0].size();
        for item in layout.items() {
            assert_eq!(item.size(), first, "buttons disagree about their size");
        }
        assert!(first.x > 0.0 && first.y > 0.0);
    }

    // -----------------------------------------------------------------------
    // States
    // -----------------------------------------------------------------------

    /// **Exactly one item is highlighted, and it is the selected one.**
    #[test]
    fn only_the_selected_item_is_highlighted() {
        let mut menu = pause_menu();
        for expected in 0..menu.items().len() {
            let states: Vec<ButtonState> = (0..menu.items().len()).map(|i| menu.state(i)).collect();
            assert_eq!(states[expected], ButtonState::Hovered, "{states:?}");
            assert_eq!(
                states.iter().filter(|s| **s != ButtonState::Idle).count(),
                1,
                "{states:?} lights up more than one item",
            );
            menu.select_next();
        }
        // Wrapped back to the start.
        assert_eq!(menu.selected(), 0);
    }

    /// The selection wraps both ways, so a player at the top can reach the
    /// bottom without walking the whole list.
    #[test]
    fn the_selection_wraps_both_ways() {
        let mut menu = pause_menu();
        menu.select_previous();
        assert_eq!(menu.selected(), 2);
        menu.select_next();
        assert_eq!(menu.selected(), 0);
    }

    /// **Holding the commit key changes the state, and letting it go changes it
    /// back.** Asserted as the three-way state rather than as a bool, because the
    /// state is what selects the frame of art the button draws.
    #[test]
    fn holding_the_commit_key_presses_the_highlighted_button() {
        let mut menu = pause_menu();
        assert_eq!(menu.state(0), ButtonState::Hovered);
        menu.press(true);
        assert_eq!(menu.state(0), ButtonState::Pressed);
        assert_eq!(menu.state(1), ButtonState::Idle, "the press leaked");
        assert_eq!(menu.activate(), Some(1));
        assert_eq!(
            menu.state(0),
            ButtonState::Hovered,
            "activating left the button held down",
        );
    }

    /// Activation reports the item's **id**, not its index, and follows the
    /// selection.
    #[test]
    fn activation_reports_the_selected_items_id() {
        let mut menu = pause_menu();
        assert_eq!(menu.activate(), Some(1));
        menu.select_next();
        assert_eq!(menu.activate(), Some(2));
        assert!(menu.select_id(3));
        assert_eq!(menu.activate(), Some(3));
        assert!(!menu.select_id(99), "an id that is not in the menu");
    }

    /// An empty menu has nothing to select, nothing to press and nothing to
    /// fire — and says so rather than panicking on an index.
    #[test]
    fn an_empty_menu_activates_nothing() {
        let mut menu = Menu::new("EMPTY", Vec::new());
        menu.select_next();
        menu.select_previous();
        menu.press(true);
        assert_eq!(menu.activate(), None);
        assert_eq!(menu.state(0), ButtonState::Idle);
    }

    // -----------------------------------------------------------------------
    // The pointer
    // -----------------------------------------------------------------------

    /// The pointer hovers, presses and clicks — and a press released off the
    /// button it started on fires nothing, which is [`UiState`]'s capture doing
    /// its job through the menu.
    #[test]
    fn the_pointer_hovers_presses_and_clicks() {
        let atlas = atlas();
        let mut menu = pause_menu();
        let layout = menu.layout((960, 720), &atlas);
        let mut ui = UiState::new();
        let second = layout.items()[1];
        let over_second = (second.min + second.max) * 0.5;
        let outside = Vec2::new(2.0, 2.0);

        // Hover moves the highlight off the keyboard's selection.
        assert_eq!(
            menu.point(&layout, &mut ui, PointerInput::hovering(over_second)),
            None
        );
        assert_eq!(menu.state(1), ButtonState::Hovered);
        assert_eq!(
            menu.state(0),
            ButtonState::Idle,
            "the selection still shows"
        );

        // Press.
        let down = PointerInput {
            pos: over_second,
            down: true,
            released: false,
            secondary_pressed: false,
        };
        assert_eq!(menu.point(&layout, &mut ui, down), None);
        assert_eq!(menu.state(1), ButtonState::Pressed);

        // Released somewhere else: nothing fires, and the capture is dropped.
        let elsewhere = PointerInput {
            pos: outside,
            down: false,
            released: true,
            secondary_pressed: false,
        };
        assert_eq!(menu.point(&layout, &mut ui, elsewhere), None);
        assert_eq!(ui.active(), None);

        // A press and release over the same button fires it, and leaves the
        // keyboard's selection on it.
        assert_eq!(menu.point(&layout, &mut ui, down), None);
        let up = PointerInput {
            pos: over_second,
            down: false,
            released: true,
            secondary_pressed: false,
        };
        assert_eq!(menu.point(&layout, &mut ui, up), Some(2));
        assert_eq!(menu.selected(), 1);
    }

    /// A pointer that is over nothing leaves the keyboard in charge.
    #[test]
    fn a_pointer_over_nothing_gives_the_highlight_back_to_the_keyboard() {
        let atlas = atlas();
        let mut menu = pause_menu();
        let layout = menu.layout((960, 720), &atlas);
        let mut ui = UiState::new();
        let third = layout.items()[2];
        menu.point(
            &layout,
            &mut ui,
            PointerInput::hovering((third.min + third.max) * 0.5),
        );
        assert_eq!(menu.state(2), ButtonState::Hovered);
        menu.point(
            &layout,
            &mut ui,
            PointerInput::hovering(Vec2::new(1.0, 1.0)),
        );
        assert_eq!(menu.state(0), ButtonState::Hovered, "back to the selection");
        assert_eq!(menu.state(2), ButtonState::Idle);
    }

    /// **A drag from one item onto a neighbour draws neither of them
    /// pressed.** [`UiState`]'s capture belongs to the item the press started
    /// on, so the neighbour reports `Idle` even while the cursor is over it —
    /// and the drawn state must say the same, not the `Pressed` a menu-global
    /// "something is down" bool painted onto whatever was hovered.
    #[test]
    fn a_drag_onto_a_neighbour_does_not_press_it() {
        let atlas = atlas();
        let mut menu = pause_menu();
        let layout = menu.layout((960, 720), &atlas);
        let mut ui = UiState::new();
        let first = layout.items()[0];
        let second = layout.items()[1];

        // Press on the first item: it draws Pressed.
        let down = PointerInput {
            pos: (first.min + first.max) * 0.5,
            down: true,
            released: false,
            secondary_pressed: false,
        };
        assert_eq!(menu.point(&layout, &mut ui, down), None);
        assert_eq!(menu.state(0), ButtonState::Pressed);

        // Drag onto the second: the capture still belongs to the first, so the
        // second must draw Idle — and once the cursor leaves it, so must the
        // first, exactly as `interact` reports both.
        let dragged = PointerInput {
            pos: (second.min + second.max) * 0.5,
            down: true,
            released: false,
            secondary_pressed: false,
        };
        assert_eq!(menu.point(&layout, &mut ui, dragged), None);
        assert_eq!(
            menu.state(1),
            ButtonState::Idle,
            "the neighbour drew Pressed while the capture belongs to the first item",
        );
        assert_eq!(
            menu.state(0),
            ButtonState::Idle,
            "the cursor left the captured item"
        );
    }

    // -----------------------------------------------------------------------
    // Text
    // -----------------------------------------------------------------------

    /// **The menu draws back to front: the scrim, the frame and the title on
    /// it, then each button's frame followed by the strings on that button** —
    /// every picture before the text that sits on it, so no frame paints over
    /// a label — and the text is the title, every label and every hint.
    #[test]
    fn render_draws_each_frame_before_the_text_on_it() {
        let atlas = atlas();
        let menu = pause_menu();
        let layout = menu.layout((960, 720), &atlas);
        let mut dl = DrawList::new();
        menu.render(&mut dl, &layout, &skin());

        // `I` for a picture, the string itself for text: one scrim, nine panel
        // quads (every band non-empty at this size), the title, then nine
        // quads and two strings per button.
        let shape: Vec<String> = dl
            .commands()
            .iter()
            .map(|command| match command {
                DrawCommand::Image { .. } => "I".to_owned(),
                DrawCommand::Text { text, .. } => text.clone(),
                other => panic!("a menu of buttons drew {other:?}"),
            })
            .collect();
        let quads = |count: usize| std::iter::repeat_n("I".to_owned(), count);
        let mut expected: Vec<String> = quads(1 + 9).collect();
        expected.push("PAUSED".to_owned());
        for (label, hint) in [
            ("RESUME", "ESC"),
            ("FULLSCREEN", "F11"),
            ("DEBUG OVERLAY", "F3"),
        ] {
            expected.extend(quads(9));
            expected.extend([label.to_owned(), hint.to_owned()]);
        }
        assert_eq!(shape, expected);
    }

    /// An item with no hint draws one string, not an empty second one.
    #[test]
    fn an_item_with_no_hint_draws_no_hint() {
        let atlas = atlas();
        let menu = Menu::new("T", vec![MenuItem::new(1, "OK", "")]);
        let layout = menu.layout((960, 720), &atlas);
        let mut dl = DrawList::new();
        menu.render(&mut dl, &layout, &skin());
        assert_eq!(
            dl.len(),
            1 + 9 + 9 + 2,
            "the scrim, the frame, one button, the title and the label, and nothing else"
        );
    }

    /// The `Image` rectangles of `commands`, with their UV origin.
    fn pictures(dl: &DrawList) -> Vec<(Vec2, Vec2, Vec2)> {
        dl.commands()
            .iter()
            .filter_map(|command| match command {
                DrawCommand::Image {
                    min, max, uv_min, ..
                } => Some((*min, *max, *uv_min)),
                _ => None,
            })
            .collect()
    }

    /// **The scrim covers the framebuffer, tinted by the style, and the frame's
    /// nine quads tile exactly the panel the layout placed** — so the art is
    /// centred wherever the layout is, and the layout's centring tests speak for
    /// the picture.
    #[test]
    fn the_scrim_covers_the_screen_and_the_frame_tiles_the_panel() {
        let atlas = atlas();
        let menu = pause_menu();
        let skin = skin();
        for extent in EXTENTS {
            let layout = menu.layout(extent, &atlas);
            let mut dl = DrawList::new();
            menu.render(&mut dl, &layout, &skin);

            let DrawCommand::Image { min, max, tint, .. } = &dl.commands()[0] else {
                panic!("{extent:?}: the first command is not the scrim");
            };
            assert_eq!((*min, *max), layout.scrim(), "{extent:?}");
            assert_eq!(*tint, layout.style().scrim_color);

            let frame = &pictures(&dl)[1..10];
            let area: f32 = frame
                .iter()
                .map(|(min, max, _)| (max.x - min.x) * (max.y - min.y))
                .sum();
            let (panel_min, panel_max) = layout.panel();
            let low = frame
                .iter()
                .fold(Vec2::splat(f32::MAX), |acc, q| acc.min(q.0));
            let high = frame
                .iter()
                .fold(Vec2::splat(f32::MIN), |acc, q| acc.max(q.1));
            assert_eq!((low, high), (panel_min, panel_max), "{extent:?}");
            assert_eq!(area, layout.panel_size().x * layout.panel_size().y);
        }
    }

    /// **A button's state changes the picture it samples, not the rectangle it
    /// covers.** A `ButtonState` that never reached the art is a menu whose
    /// buttons never light up and whose every layout test still passes.
    #[test]
    fn a_state_change_moves_the_uvs_and_leaves_the_rectangle_alone() {
        let atlas = atlas();
        let skin = skin();
        let mut menu = pause_menu();
        let layout = menu.layout((960, 720), &atlas);
        let button = |menu: &Menu, index: usize| {
            let mut dl = DrawList::new();
            menu.render(&mut dl, &layout, &skin);
            pictures(&dl)[10 + index * 9..10 + (index + 1) * 9].to_vec()
        };

        // Item 0 is selected, so it draws `Hovered` and the others `Idle`.
        let idle = button(&menu, 1);
        let hovered = button(&menu, 0);
        menu.press(true);
        let pressed = button(&menu, 0);
        assert_eq!(menu.state(0), ButtonState::Pressed);

        for quad in 0..9 {
            assert_eq!(
                (hovered[quad].0, hovered[quad].1),
                (pressed[quad].0, pressed[quad].1)
            );
            assert_ne!(hovered[quad].2, pressed[quad].2, "quad {quad}");
            assert_ne!(idle[quad].2, hovered[quad].2, "quad {quad}");
            assert_ne!(idle[quad].2, pressed[quad].2, "quad {quad}");
        }
    }

    /// **The frame's corners are the art's insets at the menu's scale** — four
    /// texels at scale three is twelve pixels — whatever the panel's size.
    #[test]
    fn the_frames_corner_quads_are_the_insets_at_the_menus_scale() {
        let atlas = atlas();
        let skin = skin();
        let style = MenuStyle::pixel_art(3);
        let small = Menu::new("GO", vec![MenuItem::new(1, "OK", "")]);
        let large = Menu::new(
            "A MUCH LONGER TITLE",
            (0..5)
                .map(|i| MenuItem::new(i, "A VERY LONG MENU ITEM LABEL", "SHIFT+F11"))
                .collect(),
        );
        let corners = |menu: &Menu| {
            let layout = menu.layout_with((1600, 1200), &atlas, &style);
            let mut dl = DrawList::new();
            menu.render(&mut dl, &layout, &skin);
            let frame = pictures(&dl)[1..10].to_vec();
            [0usize, 2, 6, 8].map(|index| (frame[index].1 - frame[index].0, frame[index].2))
        };
        let (a, b) = (corners(&small), corners(&large));
        assert_eq!(a, b, "a corner changed size or picture with the menu");
        for (size, _) in a {
            assert_eq!(size, Vec2::splat(PANEL_INSETS.left * style.scale));
        }
    }

    /// Every glyph a menu draws is inside the button or panel it belongs to.
    /// Without this the layout could put a label a hundred pixels to the right
    /// and every other test here would still pass.
    #[test]
    fn every_label_is_inside_the_button_it_belongs_to() {
        let atlas = atlas();
        let menu = pause_menu();
        for extent in EXTENTS {
            let layout = menu.layout(extent, &atlas);
            let style = layout.style();
            for (item, placed) in menu.items().iter().zip(layout.items()) {
                let label_end =
                    placed.label_pos.x + text_width(&atlas, &item.label, style.item_size);
                assert!(
                    placed.label_pos.x >= placed.min.x && label_end <= placed.max.x,
                    "{extent:?}: {} runs from {} to {label_end} in a button \
                     {}..{}",
                    item.label,
                    placed.label_pos.x,
                    placed.min.x,
                    placed.max.x,
                );
                let hint_end = placed.hint_pos.x + text_width(&atlas, &item.hint, style.item_size);
                assert!(
                    placed.hint_pos.x >= label_end && hint_end <= placed.max.x,
                    "{extent:?}: the hint {} overlaps its label or leaves the button",
                    item.hint,
                );
                let bottom = placed.label_pos.y + line_height(style.item_size);
                assert!(
                    placed.label_pos.y >= placed.min.y && bottom <= placed.max.y,
                    "{extent:?}: {} is not vertically inside its button",
                    item.label,
                );
            }
            let title_end =
                layout.title_pos().x + text_width(&atlas, &menu.title, style.title_size);
            let (min, max) = layout.panel();
            assert!(layout.title_pos().x >= min.x && title_end <= max.x);
        }
    }

    // -----------------------------------------------------------------------
    // MenuSet
    // -----------------------------------------------------------------------

    /// A stand-in for a game's own menu states — the shape every sample has:
    /// one variant that draws nothing and the rest one panel each.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum Kind {
        None,
        Start,
        Paused,
    }

    fn start_menu() -> Menu {
        Menu::new(
            "START",
            vec![
                MenuItem::new(10, "PLAY", "SPACE"),
                MenuItem::new(2, "FULLSCREEN", "F11"),
            ],
        )
    }

    fn menus() -> MenuSet<Kind> {
        MenuSet::new(
            Kind::None,
            vec![(Kind::Start, start_menu()), (Kind::Paused, pause_menu())],
        )
    }

    /// The centre of the `index`-th button, in framebuffer pixels.
    fn over(menus: &MenuSet<Kind>, extent: (u32, u32), index: usize) -> Vec2 {
        let layout = menus.current().expect("a menu").layout(extent, &atlas());
        let item = layout.items()[index];
        (item.min + item.max) * 0.5
    }

    fn press_at(pos: Vec2) -> PointerInput {
        PointerInput {
            pos,
            down: true,
            released: false,
            secondary_pressed: false,
        }
    }

    fn release_at(pos: Vec2) -> PointerInput {
        PointerInput {
            pos,
            down: false,
            released: true,
            secondary_pressed: false,
        }
    }

    /// **A state the set holds no menu for draws nothing and takes no input.**
    /// This is how every sample spells "no menu this frame" — there is no
    /// separate `Option`, the state simply has no entry — so if any of these
    /// were to act on the last menu shown, a playing frame would be steering a
    /// panel nobody can see.
    #[test]
    fn a_state_with_no_menu_draws_nothing_and_takes_no_input() {
        let extent = (960, 720);
        let mut menus = menus();
        assert_eq!(menus.kind(), Kind::None);
        assert!(!menus.is_showing());
        assert!(menus.current().is_none());
        assert!(menus.current_mut().is_none());
        assert_eq!(menus.activate(), None, "there is no menu to activate");
        assert_eq!(menus.point(extent, &atlas(), press_at(Vec2::ZERO)), None);

        // And the keys are not merely ignored on the way in: they must not have
        // moved the menu that is *about* to be shown.
        menus.select_next();
        menus.select_previous();
        menus.press(true);
        menus.show(Kind::Paused);
        assert!(menus.is_showing());
        assert_eq!(menus.current().expect("a menu").selected(), 0);
        assert_eq!(
            menus.current().expect("a menu").state(0),
            ButtonState::Hovered
        );
    }

    /// **Keyboard activation works**, on whichever menu is shown, and reports
    /// the id the selected button carries.
    #[test]
    fn the_keyboard_selects_and_activates() {
        let mut menus = menus();
        menus.show(Kind::Paused);
        assert_eq!(menus.activate(), Some(1));
        menus.select_next();
        assert_eq!(menus.activate(), Some(2));
        menus.select_next();
        assert_eq!(menus.activate(), Some(3));
        menus.select_next();
        assert_eq!(menus.activate(), Some(1), "it wraps");
        menus.select_previous();
        assert_eq!(menus.activate(), Some(3));

        // A different menu has its own selection and its own ids.
        menus.show(Kind::Start);
        assert_eq!(menus.activate(), Some(10));
    }

    /// Holding the commit key presses the highlighted button and nothing else,
    /// which is what selects the pressed frame of the skin.
    #[test]
    fn holding_the_commit_key_presses_the_selected_button() {
        let mut menus = menus();
        menus.show(Kind::Paused);
        menus.select_next();
        menus.press(true);
        let menu = menus.current().expect("a menu");
        assert_eq!(menu.state(1), ButtonState::Pressed);
        assert_eq!(menu.state(0), ButtonState::Idle);
        menus.press(false);
        assert_eq!(
            menus.current().expect("a menu").state(1),
            ButtonState::Hovered,
        );
    }

    /// **The pointer activates too**, reporting the same ids, and a click over
    /// nothing fires nothing.
    #[test]
    fn the_pointer_clicks_a_button() {
        let extent = (960, 720);
        let mut menus = menus();
        menus.show(Kind::Paused);
        let target = over(&menus, extent, 1);

        assert_eq!(menus.point(extent, &atlas(), press_at(target)), None);
        assert_eq!(
            menus.current().expect("a menu").state(1),
            ButtonState::Pressed,
            "the pointer's press did not reach the art",
        );
        assert_eq!(menus.point(extent, &atlas(), release_at(target)), Some(2));

        let corner = Vec2::new(3.0, 3.0);
        assert_eq!(menus.point(extent, &atlas(), press_at(corner)), None);
        assert_eq!(menus.point(extent, &atlas(), release_at(corner)), None);
    }

    /// **A cancelled press fires nothing and leaves nothing latched**, which is
    /// what lets the next press latch at all.
    ///
    /// The second half is the one that matters: a capture left behind by a
    /// gesture the system took away is not visible — the panel looks idle — and
    /// the symptom is every later press doing nothing, on touch only.
    #[test]
    fn a_cancelled_press_fires_nothing_and_frees_the_capture() {
        let extent = (960, 720);
        let mut menus = menus();
        menus.show(Kind::Paused);
        let target = over(&menus, extent, 1);

        assert!(!menus.press_captured(), "nothing is pressed yet");
        menus.point(extent, &atlas(), press_at(target));
        assert!(menus.press_captured(), "the press latched nothing");

        menus.cancel_press();
        assert!(
            !menus.press_captured(),
            "the cancel left the capture behind"
        );
        assert_eq!(
            menus.current().expect("a menu").state(1),
            ButtonState::Idle,
            "the button stayed pressed under a finger that is gone",
        );
        assert_eq!(
            menus.point(extent, &atlas(), release_at(target)),
            None,
            "the cancelled press fired on the lift anyway",
        );

        // And the button still works afterwards, which is what a capture left
        // latched would have made impossible.
        menus.point(extent, &atlas(), press_at(target));
        assert_eq!(menus.point(extent, &atlas(), release_at(target)), Some(2));
    }

    /// **A press on the panel's background captures nothing**, so a caller
    /// driving the menu with contacts can tell the press it must remember from
    /// the one it must let go of.
    #[test]
    fn a_press_that_lands_on_no_button_captures_nothing() {
        let extent = (960, 720);
        let mut menus = menus();
        menus.show(Kind::Paused);

        menus.point(extent, &atlas(), press_at(Vec2::new(3.0, 3.0)));
        assert!(
            !menus.press_captured(),
            "a press in the corner of the screen latched a button",
        );
        menus.point(extent, &atlas(), press_at(over(&menus, extent, 0)));
        assert!(menus.press_captured());
    }

    /// **Switching menus drops the press capture**, so a click that started on
    /// one menu cannot land on another menu's button.
    ///
    /// The reason [`UiState`] lives in the set rather than in each [`Menu`]:
    /// the capture spans frames, and the menu on screen can change between
    /// them.
    ///
    /// **The press is put on the `FULLSCREEN` button deliberately, because both
    /// menus carry it under the same [`WidgetId`].** [`UiState::interact`]
    /// fires on release only when the capture names the *same* id the cursor is
    /// over, so a press parked on an id the new menu does not have could not
    /// fire whatever the set did with it — and a test built that way would pass
    /// with the capture never cleared at all.
    #[test]
    fn switching_menus_drops_the_press() {
        let extent = (960, 720);
        let mut menus = menus();
        menus.show(Kind::Paused);
        assert_eq!(
            menus.current().expect("a menu").items()[1].id,
            start_menu().items()[1].id,
            "the two menus stopped sharing a button, so this test guards nothing",
        );

        menus.point(extent, &atlas(), press_at(over(&menus, extent, 1)));
        assert_eq!(
            menus.current().expect("a menu").state(1),
            ButtonState::Pressed,
        );

        menus.show(Kind::Start);
        assert_eq!(
            menus.current().expect("a menu").state(0),
            ButtonState::Hovered,
            "the new menu inherited a press nobody is making",
        );
        let shared = over(&menus, extent, 1);
        assert_eq!(
            menus.point(extent, &atlas(), release_at(shared)),
            None,
            "a release fired a button whose press was on another menu",
        );

        // And the menu that was left behind is clean when it comes back. The
        // capture is only half of it: the outgoing menu holds its own hover and
        // pressed flags, and a panel re-shown still lit up draws a button
        // nobody is touching. `Idle` rather than `Hovered` because the pointer
        // pressed the second button while the selection stayed on the first —
        // an uncleared menu reports this one `Pressed`.
        menus.show(Kind::Paused);
        assert_eq!(
            menus.current().expect("a menu").state(1),
            ButtonState::Idle,
            "the menu came back still holding the press it was left with",
        );
    }

    /// Showing what is already shown changes nothing — the samples call `show`
    /// every frame, so an unconditional clear here would throw the selection
    /// and the press away sixty times a second.
    #[test]
    fn showing_the_same_menu_again_keeps_the_selection_and_the_press() {
        let mut menus = menus();
        menus.show(Kind::Paused);
        menus.select_next();
        menus.press(true);
        menus.show(Kind::Paused);
        assert_eq!(menus.current().expect("a menu").selected(), 1);
        assert_eq!(
            menus.current().expect("a menu").state(1),
            ButtonState::Pressed,
        );
    }

    /// **A replaced menu drops the capture too** — the press was on a button
    /// that no longer says what it said. For a panel whose buttons are built
    /// while the game runs.
    ///
    /// The replacement keeps the **same ids** on purpose, which is what a
    /// rebuilt panel really looks like: the same three slots, three different
    /// things in them. Ids that changed would make the stale capture
    /// unmatchable and the test green whatever `replace` did — see
    /// [`switching_menus_drops_the_press`].
    #[test]
    fn a_replaced_menu_drops_the_press() {
        let extent = (960, 720);
        let mut menus = menus();
        menus.show(Kind::Start);
        menus.point(extent, &atlas(), press_at(over(&menus, extent, 0)));
        assert_eq!(
            menus.current().expect("a menu").state(0),
            ButtonState::Pressed,
        );

        menus.replace(
            Kind::Start,
            Menu::new(
                "START",
                vec![
                    MenuItem::new(10, "OTHER", "SPACE"),
                    MenuItem::new(2, "FULLSCREEN", "F11"),
                ],
            ),
        );
        assert_eq!(
            menus.current().expect("a menu").items()[0].label,
            "OTHER",
            "the swap did not take",
        );
        let same_slot = over(&menus, extent, 0);
        assert_eq!(
            menus.point(extent, &atlas(), release_at(same_slot)),
            None,
            "a release fired a button the player never pressed",
        );
    }

    /// `replace` on a state that had no menu adds one, so a set can be filled
    /// in after it is built.
    #[test]
    fn replacing_a_state_with_no_menu_adds_it() {
        let mut menus = MenuSet::new(Kind::None, vec![(Kind::Paused, pause_menu())]);
        menus.show(Kind::Start);
        assert!(!menus.is_showing(), "Start has no menu yet");
        menus.replace(Kind::Start, start_menu());
        assert!(menus.is_showing());
        assert_eq!(menus.current().expect("a menu").title, "START");
        assert_eq!(menus.activate(), Some(10));
    }

    /// Two menus for one state is rejected at construction: the second would be
    /// unreachable, because every lookup takes the first match.
    #[test]
    #[should_panic(expected = "two menus claim the same state")]
    fn two_menus_for_one_state_is_rejected() {
        let _ = MenuSet::new(
            Kind::None,
            vec![(Kind::Paused, pause_menu()), (Kind::Paused, start_menu())],
        );
    }
    // -----------------------------------------------------------------------
    // Sliders
    // -----------------------------------------------------------------------

    /// The id of the slider row in [`slider_menu`].
    const DIAL: WidgetId = 20;

    /// A panel whose second row is a slider, so every test below has a button
    /// above it to prove the two kinds do not behave alike.
    fn slider_menu() -> Menu {
        Menu::new(
            "OPTIONS",
            vec![
                MenuItem::new(10, "BACK", "ESC"),
                MenuItem::slider(DIAL, "EXPOSURE", "1.00", 0.5),
            ],
        )
    }

    /// The slider row's groove and the travel its handle's centre has inside
    /// it, at `extent`.
    ///
    /// Both read off the layout the menu actually chose. A test that measured
    /// the travel at [`MenuStyle::pixel_art`]`(1)` would be a quarter of a
    /// handle out at the scale a 960x720 window picks, and would report the
    /// clamp as a rounding error.
    fn groove(menu: &Menu, extent: (u32, u32)) -> ((Vec2, Vec2), (f32, f32)) {
        let layout = menu.layout(extent, &atlas());
        let track = layout.items()[1]
            .track
            .expect("the slider row has a groove");
        (track, handle_travel(track, layout.style().handle_size.x))
    }

    /// **A slider is dragged, not clicked.** The handle follows the pointer's
    /// `x` while the press is held, which is the whole of the control.
    #[test]
    fn dragging_a_slider_moves_the_handle_to_the_pointer() {
        let extent = (960, 720);
        let mut menu = slider_menu();
        let mut ui = UiState::new();
        let (track, (start, end)) = groove(&menu, extent);

        for (fraction, expected) in [(0.0, 0.0), (0.25, 0.25), (1.0, 1.0)] {
            let layout = menu.layout(extent, &atlas());
            let x = start + (end - start) * fraction;
            let y = (track.0.y + track.1.y) * 0.5;
            menu.point(&layout, &mut ui, press_at(Vec2::new(x, y)));
            let at = menu.slider(DIAL).expect("the row is a slider");
            assert!(
                (at - expected).abs() <= 1e-3,
                "a press {fraction} of the way along put the handle at {at}",
            );
        }
    }

    /// **Past either end is the end, not a wrap and not a `NaN`.** A pointer
    /// dragged off the panel entirely is the ordinary way a player reaches the
    /// minimum, so the clamp is the feature.
    #[test]
    fn a_drag_past_either_end_of_the_groove_stops_there() {
        let extent = (960, 720);
        let mut menu = slider_menu();
        let mut ui = UiState::new();
        let (track, _) = groove(&menu, extent);
        let y = (track.0.y + track.1.y) * 0.5;
        let layout = menu.layout(extent, &atlas());

        menu.point(&layout, &mut ui, press_at(Vec2::new(track.0.x, y)));
        menu.point(
            &layout,
            &mut ui,
            press_at(Vec2::new(track.0.x - 10_000.0, y)),
        );
        assert_eq!(menu.slider(DIAL), Some(0.0));

        menu.point(
            &layout,
            &mut ui,
            press_at(Vec2::new(track.1.x + 10_000.0, y)),
        );
        assert_eq!(menu.slider(DIAL), Some(1.0));
    }

    /// **A drag that leaves the row keeps the handle.** Unlike a button, whose
    /// press is abandoned when the cursor wanders off it: a player watching the
    /// frame behind the panel drags upward out of the row as a matter of course,
    /// and a slider that let go there would be unusable.
    #[test]
    fn a_drag_that_leaves_the_row_still_moves_the_handle() {
        let extent = (960, 720);
        let mut menu = slider_menu();
        let mut ui = UiState::new();
        let (track, (start, end)) = groove(&menu, extent);
        let layout = menu.layout(extent, &atlas());

        let inside = Vec2::new(start, (track.0.y + track.1.y) * 0.5);
        menu.point(&layout, &mut ui, press_at(inside));
        assert_eq!(menu.slider(DIAL), Some(0.0));

        // Three quarters along, and a long way above the panel.
        let outside = Vec2::new(start + (end - start) * 0.75, 0.0);
        menu.point(&layout, &mut ui, press_at(outside));
        let at = menu.slider(DIAL).expect("the row is a slider");
        assert!(
            (at - 0.75).abs() <= 1e-3,
            "the drag left the handle at {at}"
        );
    }

    /// **A slider never reports an id**, from the commit key or from a release
    /// over it. Either would arrive at the game's action table looking exactly
    /// like a button press, and the game would run whatever that id means.
    #[test]
    fn a_slider_fires_nothing_from_either_device() {
        let extent = (960, 720);
        let mut menu = slider_menu();
        let mut ui = UiState::new();

        menu.select_next();
        assert_eq!(menu.selected(), 1, "the slider is the second row");
        menu.press(true);
        assert_eq!(menu.activate(), None, "the commit key fired the slider");

        let (track, _) = groove(&menu, extent);
        let at = Vec2::new((track.0.x + track.1.x) * 0.5, (track.0.y + track.1.y) * 0.5);
        let layout = menu.layout(extent, &atlas());
        menu.point(&layout, &mut ui, press_at(at));
        assert_eq!(
            menu.point(&layout, &mut ui, release_at(at)),
            None,
            "letting go of a slider was reported as a click",
        );

        // And the button above it still does, so the suppression is the
        // slider's and not a menu that stopped firing altogether.
        menu.select_previous();
        assert_eq!(menu.activate(), Some(10));
    }

    /// **The caller's write loses to the player's drag.** A game mirroring the
    /// value it read back into the handle every frame is the normal shape; if
    /// that write won, the handle would be pinned wherever the game last
    /// committed and the drag would go nowhere.
    #[test]
    fn a_write_is_refused_while_the_handle_is_held_and_taken_after() {
        let extent = (960, 720);
        let mut menu = slider_menu();
        let mut ui = UiState::new();
        let (track, _) = groove(&menu, extent);
        let y = (track.0.y + track.1.y) * 0.5;
        let layout = menu.layout(extent, &atlas());

        assert!(menu.set_slider(DIAL, 0.25), "an untouched handle takes it");
        assert_eq!(menu.slider(DIAL), Some(0.25));

        menu.point(&layout, &mut ui, press_at(Vec2::new(track.1.x, y)));
        assert_eq!(menu.slider(DIAL), Some(1.0));
        assert!(
            !menu.set_slider(DIAL, 0.25),
            "the write went through mid-drag",
        );
        assert_eq!(menu.slider(DIAL), Some(1.0));

        menu.point(&layout, &mut ui, release_at(Vec2::new(track.1.x, y)));
        assert!(menu.set_slider(DIAL, 0.25), "the drag was never let go of");
        assert_eq!(menu.slider(DIAL), Some(0.25));
    }

    /// A value neither the caller nor the pointer can name lands at the start of
    /// the groove, because `f32::clamp` would hand a `NaN` straight back.
    #[test]
    fn a_nan_position_lands_at_the_start() {
        let mut menu = slider_menu();
        assert!(menu.set_slider(DIAL, f32::NAN));
        assert_eq!(menu.slider(DIAL), Some(0.0));
        assert_eq!(Slider::new(f32::NAN).position(), 0.0);
        assert_eq!(Slider::new(f32::INFINITY).position(), 1.0);
    }

    /// Asking a button row for a value, or a row that is not there, is `None`
    /// rather than a number the caller would act on.
    #[test]
    fn only_a_slider_row_has_a_value() {
        let mut menu = slider_menu();
        assert_eq!(menu.slider(10), None, "the button row reported a value");
        assert_eq!(menu.slider(999), None, "an absent row reported a value");
        assert!(!menu.set_slider(10, 0.5), "a button row took a value");
    }

    /// **The keyboard moves the row the player can see is highlighted**, in
    /// both directions and by the step the constant names.
    #[test]
    fn a_key_moves_the_highlighted_slider_and_nothing_else() {
        let mut menu = slider_menu();
        // The button row is selected first, so a nudge has no slider to move.
        assert!(!menu.nudge_slider(true), "a button row took a nudge");
        assert_eq!(menu.slider(DIAL), Some(0.5), "the far row moved anyway");

        menu.select_next();
        assert!(menu.nudge_slider(true), "the highlighted slider refused");
        assert_eq!(menu.slider(DIAL), Some(0.5 + Slider::KEY_STEP));
        assert!(menu.nudge_slider(false), "the slider refused to come back");
        assert_eq!(menu.slider(DIAL), Some(0.5));
    }

    /// **The end of the groove is a place, and a key held against it stays
    /// there.** The return value is what tells the two apart: nothing moved.
    #[test]
    fn a_nudge_at_the_end_of_the_groove_moves_nothing() {
        let mut menu = slider_menu();
        menu.select_next();
        // Twenty steps is the whole groove from the middle twice over, so this
        // arrives at the end whatever `KEY_STEP` is set to.
        for _ in 0..(1.0 / Slider::KEY_STEP) as u32 + 1 {
            menu.nudge_slider(true);
        }
        assert_eq!(menu.slider(DIAL), Some(1.0), "the handle left the groove");
        assert!(
            !menu.nudge_slider(true),
            "a handle at the end reported that it moved"
        );
    }

    /// **A drag is the newer input**, which is [`Menu::set_slider`]'s rule and
    /// applies to a key for the same reason: a keyboard repeat arriving while
    /// the player has the handle would fight the cursor for it.
    #[test]
    fn a_key_does_not_move_a_handle_the_pointer_is_holding() {
        let extent = (960, 720);
        let mut menu = slider_menu();
        let mut ui = UiState::new();
        let (track, _) = groove(&menu, extent);
        let middle = Vec2::new((track.0.x + track.1.x) * 0.5, (track.0.y + track.1.y) * 0.5);
        let layout = menu.layout(extent, &atlas());

        menu.point(&layout, &mut ui, press_at(middle));
        let held = menu.slider(DIAL).expect("the row is a slider");
        assert!(!menu.nudge_slider(true), "a held handle took a key");
        assert_eq!(menu.slider(DIAL), Some(held), "the held handle moved");

        // And it takes one again once the pointer lets go, from the position
        // the drag left it — not from the one the key would have written.
        menu.point(&layout, &mut ui, release_at(middle));
        assert!(
            menu.nudge_slider(false),
            "the released handle refused a key"
        );
        assert!(
            menu.slider(DIAL).expect("still a slider") < held,
            "the key moved it the wrong way, or not at all, from {held}"
        );
    }

    /// **The groove is reserved, not squeezed in.** The panel grows by at least
    /// a groove and its gap over the same menu with a button in that row — a
    /// slider row that fitted in a button's width would have laid its groove
    /// over the label.
    #[test]
    fn a_slider_row_widens_the_panel_by_its_groove() {
        let atlas = atlas();
        let style = MenuStyle::pixel_art(1);
        let with_button = Menu::new(
            "OPTIONS",
            vec![
                MenuItem::new(10, "BACK", "ESC"),
                MenuItem::new(DIAL, "EXPOSURE", "1.00"),
            ],
        );
        let grew =
            slider_menu().panel_size(&atlas, &style).x - with_button.panel_size(&atlas, &style).x;
        assert!(
            grew >= style.track_width + style.hint_gap - 1e-3,
            "a slider row only widened the panel by {grew}",
        );
    }

    /// **The groove sits between the label and the value, and the handle inside
    /// the groove — at both ends.** A handle centred on the groove's end would
    /// hang half of itself over the button's face, so nought and one would look
    /// like different amounts of overhang rather than like the two extremes.
    #[test]
    fn the_handle_stays_inside_the_groove_at_both_ends() {
        let extent = (960, 720);
        let atlas = atlas();
        for position in [0.0, 0.5, 1.0] {
            let mut menu = slider_menu();
            assert!(menu.set_slider(DIAL, position));
            let layout = menu.layout(extent, &atlas);
            let placed = layout.items()[1];
            let track = placed.track.expect("a groove");

            let label_end = placed.label_pos.x + atlas.text_width("EXPOSURE", 1.0);
            assert!(
                track.0.x >= label_end && track.1.x <= placed.hint_pos.x,
                "at {position} the groove {track:?} overlaps the row's text",
            );

            let mut dl = DrawList::new();
            menu.render(&mut dl, &layout, &skin());
            let handle = dl
                .commands()
                .iter()
                .filter_map(|command| match command {
                    DrawCommand::Rect { min, max, color }
                        if *color == layout.style().handle_color =>
                    {
                        Some((*min, *max))
                    }
                    _ => None,
                })
                .next()
                .expect("the handle is drawn");
            assert!(
                handle.0.x >= track.0.x - 1e-3 && handle.1.x <= track.1.x + 1e-3,
                "at {position} the handle {handle:?} left the groove {track:?}",
            );
        }
    }

    /// The filled part of the groove is what says how far along the handle is
    /// without reading the number, so it has to actually track the value.
    #[test]
    fn the_fill_grows_with_the_value() {
        let extent = (960, 720);
        let atlas = atlas();
        let mut widths = Vec::new();
        for position in [0.0, 0.5, 1.0] {
            let mut menu = slider_menu();
            assert!(menu.set_slider(DIAL, position));
            let layout = menu.layout(extent, &atlas);
            let mut dl = DrawList::new();
            menu.render(&mut dl, &layout, &skin());
            let fill = dl
                .commands()
                .iter()
                .find_map(|command| match command {
                    DrawCommand::Rect { min, max, color }
                        if *color == layout.style().fill_color =>
                    {
                        Some(max.x - min.x)
                    }
                    _ => None,
                })
                .expect("the fill is drawn");
            widths.push(fill);
        }
        assert!(
            widths[0] < widths[1] && widths[1] < widths[2],
            "the fill widths were {widths:?}",
        );
    }
    // -----------------------------------------------------------------------
    // Cyclers
    // -----------------------------------------------------------------------

    /// The id of the cycler row in [`cycler_menu`].
    const MODE: WidgetId = 30;

    /// The captions the cycler row in [`cycler_menu`] walks, in order — the
    /// caller's list, which the widget never sees.
    const MODES: [&str; 3] = ["windowed", "borderless", "exclusive"];

    /// A panel whose second row is a cycler holding the middle of three
    /// choices, with a button above it for the same reason [`slider_menu`]
    /// has one.
    fn cycler_menu() -> Menu {
        Menu::new(
            "OPTIONS",
            vec![
                MenuItem::new(10, "BACK", "ESC"),
                MenuItem::cycler(MODE, "DISPLAY", MODES[1], MODES.len(), 1),
            ],
        )
    }

    /// **The arrows walk the list and stop at its ends.** The return value is
    /// what tells a step from a key held against the end, exactly as
    /// [`Menu::nudge_slider`] reports the end of a groove — and a button row
    /// under the highlight takes no step at all.
    #[test]
    fn a_key_steps_the_highlighted_cycler_and_stops_at_its_ends() {
        let mut menu = cycler_menu();
        assert!(
            !menu.cycler_highlighted(),
            "the button row read as a cycler"
        );
        assert!(!menu.nudge_cycler(true), "a button row took a step");
        assert_eq!(menu.cycler(MODE), Some(1), "the far row stepped anyway");

        menu.select_next();
        assert!(menu.cycler_highlighted());
        assert!(menu.nudge_cycler(true), "the highlighted cycler refused");
        assert_eq!(menu.cycler(MODE), Some(2));
        assert!(
            !menu.nudge_cycler(true),
            "the last choice reported a step forward"
        );
        assert_eq!(menu.cycler(MODE), Some(2), "a key at the end went round");

        assert!(menu.nudge_cycler(false));
        assert!(menu.nudge_cycler(false));
        assert_eq!(menu.cycler(MODE), Some(0));
        assert!(
            !menu.nudge_cycler(false),
            "the first choice reported a step back"
        );
        assert_eq!(menu.cycler(MODE), Some(0), "a key at the start went round");
    }

    /// **The commit key and a click step a cycler forward and round, and
    /// neither reports an id.** One key reaches every choice, which is what
    /// the row did when it was a button; and nothing from it arrives at the
    /// game's action table, which is the slider's rule for the same reason.
    #[test]
    fn the_commit_key_and_a_click_step_a_cycler_round_and_report_nothing() {
        let extent = (960, 720);
        let mut menu = cycler_menu();
        let mut ui = UiState::new();

        menu.select_next();
        menu.press(true);
        assert_eq!(menu.activate(), None, "the commit key fired the cycler");
        assert_eq!(menu.cycler(MODE), Some(2));
        assert_eq!(menu.activate(), None);
        assert_eq!(
            menu.cycler(MODE),
            Some(0),
            "the commit key stopped at the end"
        );

        let layout = menu.layout(extent, &atlas());
        let row = layout.items()[1];
        let at = (row.min + row.max) * 0.5;
        menu.point(&layout, &mut ui, press_at(at));
        assert_eq!(
            menu.point(&layout, &mut ui, release_at(at)),
            None,
            "a click on a cycler was reported as a click",
        );
        assert_eq!(menu.cycler(MODE), Some(1), "the click did not step it");

        // And the button above it still fires, so the suppression is the
        // cycler's and not a menu that stopped firing altogether.
        menu.select_previous();
        assert_eq!(menu.activate(), Some(10));
    }

    /// Asking a button row for a choice, or a row that is not there, is `None`
    /// rather than an index the caller would look up; a write past the end
    /// lands on the last choice, and a write to a button row is refused.
    #[test]
    fn only_a_cycler_row_has_a_choice() {
        let mut menu = cycler_menu();
        assert_eq!(menu.cycler(10), None, "the button row reported a choice");
        assert_eq!(menu.cycler(999), None, "an absent row reported a choice");
        assert!(!menu.set_cycler(10, 1), "a button row took a choice");
        assert!(menu.set_cycler(MODE, 99));
        assert_eq!(menu.cycler(MODE), Some(MODES.len() - 1));
        assert!(menu.set_cycler(MODE, 0));
        assert_eq!(menu.cycler(MODE), Some(0));
    }

    /// **A chevron is drawn only on the side a step would go, and the caption
    /// does not move as they come and go.** The blank that stands in for a
    /// missing chevron is the same width, which the built-in atlas's uniform
    /// advance guarantees and this checks rather than assumes — with one
    /// caption for every choice, so the only thing that changes between the
    /// three layouts is the chevrons.
    #[test]
    fn a_cycler_draws_a_chevron_only_where_a_step_would_go() {
        let atlas = atlas();
        let extent = (960, 720);
        let mut menu = cycler_menu();
        assert!(menu.set_item_hint(MODE, "same"));
        let mut seen = Vec::new();
        for (chosen, expected) in [(1, "< same >"), (0, "  same >"), (2, "< same  ")] {
            assert!(menu.set_cycler(MODE, chosen));
            let layout = menu.layout(extent, &atlas);
            let row = layout.items()[1];
            assert!(row.track.is_none(), "a cycler row was given a groove");
            let mut dl = DrawList::new();
            menu.render(&mut dl, &layout, &skin());
            let texts: Vec<&str> = dl
                .commands()
                .iter()
                .filter_map(|command| match command {
                    DrawCommand::Text { text, .. } => Some(text.as_str()),
                    _ => None,
                })
                .collect();
            assert!(
                texts.contains(&expected),
                "choice {chosen} drew {texts:?}, not {expected:?}"
            );
            seen.push((row.hint_pos, layout.panel_size()));
        }
        let (anchor, panel) = seen[0];
        for (pos, size) in &seen[1..] {
            assert_eq!(*pos, anchor, "the caption moved as the chevrons changed");
            assert_eq!(*size, panel, "the panel resized as the chevrons changed");
        }
    }

    /// A cycler with no choices has nothing to step to and says so from both
    /// keys, rather than wrapping through a division by nothing; and a choice
    /// past the end lands on the last one.
    #[test]
    fn a_cycler_with_no_choices_refuses_every_step() {
        let mut empty = Cycler::new(0, 3);
        assert_eq!(empty.chosen(), 0);
        for (forward, wrap) in [(true, true), (true, false), (false, true), (false, false)] {
            assert!(!empty.step(forward, wrap), "an empty cycler stepped");
        }
        assert_eq!(Cycler::new(3, 9).chosen(), 2);

        let mut round = Cycler::new(3, 2);
        assert!(round.step(true, true), "a wrap from the end did not move");
        assert_eq!(round.chosen(), 0);
        assert!(round.step(false, true));
        assert_eq!(round.chosen(), 2);
        assert!(!round.step(true, false));
        assert_eq!(round.chosen(), 2);
    }

    // -----------------------------------------------------------------------
    // The tree and the sheet
    // -----------------------------------------------------------------------

    /// **The panel's size is the scale-1 panel times the scale, exactly**, for
    /// every kind of row — the property [`Menu::layout`]'s one-measurement fit
    /// rests on. A length that stopped being a whole number times the scale,
    /// or a rounding that stopped being exact, fails here rather than as a
    /// menu that silently picks the wrong scale.
    #[test]
    fn the_panel_grows_exactly_with_the_scale() {
        let atlas = atlas();
        let mut mixed = slider_menu();
        mixed
            .items
            .push(MenuItem::cycler(MODE, "DISPLAY", "windowed", 3, 1));
        mixed.items.push(MenuItem::new(40, "NO HINT", ""));
        for menu in [pause_menu(), mixed, Menu::new("", Vec::new())] {
            let base = menu.panel_size(&atlas, &MenuStyle::pixel_art(1));
            assert!(base.x > 0.0 && base.y > 0.0, "{base:?}");
            for scale in 2..=MenuStyle::MAX_SCALE {
                assert_eq!(
                    menu.panel_size(&atlas, &MenuStyle::pixel_art(scale)),
                    base * scale as f32,
                    "{:?} at scale {scale}",
                    menu.title,
                );
            }
        }
    }

    /// **`default.css` draws the colours [`MenuStyle::pixel_art`] restates,
    /// and cuts the frames at the insets the layout assumes** — the two
    /// places a sheet and the style could drift apart without a layout test
    /// noticing.
    #[test]
    fn the_sheet_draws_the_colours_and_the_slices_the_style_restates() {
        let atlas = atlas();
        let skin = skin();
        let mut menu = slider_menu();
        menu.items.push(MenuItem::new(11, "QUIT", "Q"));
        menu.subtitle = vec![Caption::warning("UNSAVED")];
        let layout = menu.layout((960, 720), &atlas);
        let style = layout.style();
        let mut dl = DrawList::new();
        menu.render(&mut dl, &layout, &skin);

        let text_colour = |wanted: &str| {
            dl.commands()
                .iter()
                .find_map(|command| match command {
                    DrawCommand::Text { text, color, .. } if text == wanted => Some(*color),
                    _ => None,
                })
                .unwrap_or_else(|| panic!("{wanted} is not drawn"))
        };
        assert_eq!(text_colour("OPTIONS"), style.title_color);
        assert_eq!(text_colour("BACK"), style.label_color);
        assert_eq!(text_colour("ESC"), style.hint_color);
        assert_eq!(text_colour("UNSAVED"), style.warning_color);
        let rect_colours: Vec<[f32; 4]> = dl
            .commands()
            .iter()
            .filter_map(|command| match command {
                DrawCommand::Rect { color, .. } => Some(*color),
                _ => None,
            })
            .collect();
        assert_eq!(
            rect_colours,
            [style.track_color, style.fill_color, style.handle_color],
            "the groove, its fill and the handle, in that order"
        );
        let DrawCommand::Image { tint, .. } = dl.commands()[0] else {
            panic!("the scrim is not first");
        };
        assert_eq!(tint, style.scrim_color);

        let corner_uvs = |min: usize| {
            let DrawCommand::Image { uv_min, uv_max, .. } = pictures_with_uv_max(&dl)[min] else {
                unreachable!("filtered to images");
            };
            (uv_min, uv_max)
        };
        assert_eq!(
            corner_uvs(1),
            (
                skin.panel.image.uv(Vec2::ZERO),
                skin.panel
                    .image
                    .uv(Vec2::new(PANEL_INSETS.left, PANEL_INSETS.top))
            ),
            "the panel is not cut at PANEL_INSETS"
        );
        // Row 0 is selected, so it draws the hovered frame; row 1 is idle.
        assert_eq!(
            corner_uvs(10),
            (
                skin.buttons.hovered.image.uv(Vec2::ZERO),
                skin.buttons
                    .hovered
                    .image
                    .uv(Vec2::new(BUTTON_INSETS.left, BUTTON_INSETS.top))
            ),
            "the buttons are not cut at BUTTON_INSETS"
        );
        assert_eq!(
            corner_uvs(19).0,
            skin.buttons.idle.image.uv(Vec2::ZERO),
            "an idle row does not draw the idle frame"
        );
    }

    /// Every `Image` command of `dl`, whole.
    fn pictures_with_uv_max(dl: &DrawList) -> Vec<DrawCommand> {
        dl.commands()
            .iter()
            .filter(|command| matches!(command, DrawCommand::Image { .. }))
            .cloned()
            .collect()
    }
}