vampir 0.2.0

Lit widget toolkit for GPUI: buttons, fields, pop-ups, menus and a text input, over a small OKLCH palette.
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
//! The mutable bits the controls need between frames.
//!
//! Most of a control is a pure function of the data it is handed. What is
//! left over is small and always the same shape: which pop-up is open and
//! how far into its fade it is, what each animated control showed last
//! frame, where things painted, and whatever is being dragged. A host keeps
//! one [`ControlState`], implements [`ControlHost`] for its view, and every
//! control is generic over that host, so nothing here knows the host's type.
//!
//! Everything a control remembers about itself from one frame to the next
//! is one record in one table, filed under a [`Tag`] — a hash, so building
//! one allocates nothing — and retired as soon as the control stops
//! rendering. The table is bounded by what is on screen, not by what has
//! ever been.

use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, DefaultHasher, Hash, Hasher};
use std::rc::Rc;
use std::time::{Duration, Instant};

use gpui::{
    App, Bounds, Context, ElementId, Entity, FocusHandle, InteractiveElement, MouseButton,
    MouseMoveEvent, Pixels, Point, ScrollHandle, SharedString, Window,
};

use crate::palette::{Palette, normalized_saturation};
pub use crate::scroll::{ScrollAxis, ScrollDrag, apply_scroll_drag};
use crate::text_input::TextInput;
use crate::theme::{Theme, system_dark};

/// A pop-up list fades and slides into place over this long, and back out
/// over the same.
pub const COMBO_REVEAL: Duration = Duration::from_millis(140);
/// A switch's knob and track take this long to cross over. Disclosure
/// chevrons and other small state changes use it too, so everything in a
/// view turns at one speed.
pub const SWITCH_SLIDE: Duration = Duration::from_millis(140);
/// Something moving from one place to another — a selection pill sliding
/// along its segments, a row taking its new place after a sort — takes
/// this long. A little slower than a state flip, because the eye has to
/// follow it there.
pub const MOVE: Duration = Duration::from_millis(180);
/// A whole scheme crossing over — light to dark, one hue to another. The
/// slowest thing here: everything on screen is changing at once, and a
/// fast cut of that reads as a flash.
pub const SCHEME_FADE: Duration = Duration::from_millis(240);

/// Identifies one pop-up, menu, track or tab bar. An element id doubles as
/// its identity, so a host needs no enum of its own. Two of the same kind of
/// widget in one view must not share one.
pub type ComboId = &'static str;

// ---- Tags -------------------------------------------------------------------

/// What a control's record is filed under: its id, the name of the moving
/// part, and — for a row, a tab, a command — which one.
///
/// A tag is the hash of whatever it was built from, so a control can name
/// its parts every frame without allocating: `(id, "pill-x")` is a tuple on
/// the stack, where a formatted string would be two heap allocations per
/// part per frame, freed a moment later because the record already existed.
/// Anything that hashes will do — a `&str`, a `SharedString`, an
/// `ElementId`, a tuple of them — and text hashes as text whichever type
/// it comes in, so `"row"` and `SharedString::from("row")` are one tag.
///
/// Two different names hashing to one tag would make two parts share a
/// record; with sixty-four bits and a screen's worth of parts that is not
/// going to happen, and the worst it could do is a wrong slide.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Tag(u64);

impl Tag {
    /// The tag of anything that hashes. Note that a `Tag` hashes too, so
    /// `Tag::new(tag)` is a different tag — use [`Tag::with`] to extend one.
    pub fn new(of: impl Hash) -> Self {
        let mut hasher = DefaultHasher::new();
        of.hash(&mut hasher);
        Tag(hasher.finish())
    }

    /// A tag under this one: `Tag::new(id).with("shown")` names the same
    /// part as `Tag::new((id, "shown"))` does not — pick one form and keep
    /// to it.
    pub fn with(self, and: impl Hash) -> Self {
        Tag::new((self.0, and))
    }

    pub(crate) fn element_id(self) -> ElementId {
        ("vampir", self.0).into()
    }
}

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

impl From<String> for Tag {
    fn from(text: String) -> Self {
        Tag::new(text.as_str())
    }
}

impl From<SharedString> for Tag {
    fn from(text: SharedString) -> Self {
        Tag::new(text.as_ref())
    }
}

impl From<&SharedString> for Tag {
    fn from(text: &SharedString) -> Self {
        Tag::new(text.as_ref())
    }
}

impl From<ElementId> for Tag {
    fn from(id: ElementId) -> Self {
        Tag::new(id)
    }
}

impl From<&ElementId> for Tag {
    fn from(id: &ElementId) -> Self {
        Tag::new(id)
    }
}

impl<A: Hash, B: Hash> From<(A, B)> for Tag {
    fn from(parts: (A, B)) -> Self {
        Tag::new(parts)
    }
}

impl<A: Hash, B: Hash, C: Hash> From<(A, B, C)> for Tag {
    fn from(parts: (A, B, C)) -> Self {
        Tag::new(parts)
    }
}

/// Tags are hashes already, so the tables keyed by them pass the bits
/// straight through instead of hashing them a second time.
#[derive(Default)]
struct TagHasher(u64);

impl Hasher for TagHasher {
    fn finish(&self) -> u64 {
        self.0
    }

    fn write_u64(&mut self, tag: u64) {
        self.0 = tag;
    }

    fn write(&mut self, bytes: &[u8]) {
        // Never reached for a `Tag`, whose `Hash` is one `write_u64`; kept
        // sound for anything else by folding the bytes in.
        for byte in bytes {
            self.0 = (self.0 ^ u64::from(*byte)).wrapping_mul(0x0100_0000_01b3);
        }
    }
}

type TagMap<T> = HashMap<Tag, T, BuildHasherDefault<TagHasher>>;

// ---- Records ----------------------------------------------------------------

/// The kinds of record one tag can hold, one of each at once: a list is
/// `present` as a whole and has a keyboard row, a track has bounds and a
/// tween. The kind is folded into the tag, so the kinds never collide and
/// still share one table.
#[derive(Clone, Copy)]
enum Kind {
    Flip,
    Tween,
    Streak,
    List,
    Bounds,
    Reveal,
}

impl Kind {
    fn slot(self, tag: Tag) -> Tag {
        // XOR with a constant is a bijection, so a well-mixed tag stays
        // well-mixed for the table's sake.
        const SALT: [u64; 6] = [
            0x9E37_79B9_7F4A_7C15,
            0xD1B5_4A32_D192_ED03,
            0x8CB9_2BA7_2F3D_8DD7,
            0x5851_F42D_4C95_7F2D,
            0x2545_F491_4F6C_DD1D,
            0xA24B_1CD7_95E8_6F03,
        ];
        Tag(tag.0 ^ SALT[self as usize])
    }
}

/// What one part of one control remembers between frames. Times are
/// microseconds on the state's own clock — see [`ControlState::now`] — and
/// durations microseconds too, which is three words where two `Instant`s
/// and a `Duration` were six.
#[derive(Clone, Copy, Debug)]
enum Record {
    /// A two-state element: the value it showed, and the change under way
    /// since `since` if it has one.
    Flip {
        value: u64,
        since: u64,
        duration: u32,
    },
    /// A continuous value on its way from `from` to `to`.
    Tween {
        from: f32,
        to: f32,
        since: u64,
        duration: u32,
    },
    /// A group of rows: the frame its current run of frames began in.
    Streak { started: u32 },
    /// The keyboard's row in a filtering list, a hash of the query the row
    /// belongs to, the tag of the result it was on when the list was last
    /// drawn (zero before it has been drawn with ids), and whether anyone
    /// has moved it since the query last changed.
    List {
        highlight: usize,
        query: u64,
        item: u64,
        chosen: bool,
    },
    /// Where something painted last frame.
    Bounds(Bounds<Pixels>),
    /// A selected tab needs its bar painted, in the layout it is shown in,
    /// before it can be scrolled into view. Once acknowledged, manual
    /// scrolling must stay in the user's control until the selection or
    /// layout changes again.
    Reveal {
        selection: Tag,
        pending: bool,
        retries: u8,
    },
}

/// What [`ControlState::follow_list`] did to a list's keyboard row, which
/// tells a scrolling list what to do with its scroll.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ListChange {
    /// The row is where it was.
    Same,
    /// A new query, or the highlighted result went away: back to the
    /// first row, and the list back to its top.
    Reset,
    /// The highlighted result moved, and the row went with it.
    Moved,
}

#[derive(Clone, Copy, Debug)]
struct Entry {
    /// The frame it was last asked about; see [`ControlState::animating`].
    touched: u32,
    record: Record,
}

/// Progress 0..=1 of something that started at `since` and runs for
/// `duration`, both in microseconds. A zero-length change is finished, not
/// undefined: zero over zero would be a NaN, and a NaN offset makes an
/// element vanish for as long as the value is kept.
fn progress_of(now: u64, since: u64, duration: u32) -> f32 {
    if duration == 0 {
        return 1.0;
    }
    (now.saturating_sub(since) as f32 / duration as f32).clamp(0.0, 1.0)
}

fn tween_value(now: u64, from: f32, to: f32, since: u64, duration: u32) -> f32 {
    let t = progress_of(now, since, duration);
    if t >= 1.0 {
        return to;
    }
    from + (to - from) * crate::easing::ease_out_cubic(t)
}

fn micros(duration: Duration) -> u32 {
    u32::try_from(duration.as_micros()).unwrap_or(u32::MAX)
}

fn next_dialog_button(indices: &[usize], current: Option<usize>, backward: bool) -> Option<usize> {
    let count = indices.len();
    if count == 0 {
        return None;
    }
    let here = current.and_then(|current| indices.iter().position(|&index| index == current));
    let next = match (here, backward) {
        (Some(here), true) => (here + count - 1) % count,
        (Some(here), false) => (here + 1) % count,
        (None, true) => count - 1,
        (None, false) => 0,
    };
    Some(indices[next])
}

// ---- Overlays and gestures --------------------------------------------------

/// Which way a drag reads its position out of a track.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum TrackAxis {
    Horizontal,
    Vertical,
    /// A two-dimensional pad, such as a colour field.
    Both,
}

/// A drag along some widget's track: a slider handle, a split divider, a
/// colour pad. All of them are the same gesture over different geometry, so
/// they share one mechanism.
#[derive(Clone, Copy, Debug)]
pub struct TrackDrag {
    pub id: ComboId,
    pub axis: TrackAxis,
    /// Evenly spaced stops to snap to, if the track has any.
    pub stops: Option<u32>,
}

/// A tab being dragged along its bar.
#[derive(Clone, Copy, Debug)]
pub struct TabDrag {
    pub bar: ComboId,
    /// The bar's direction; horizontal tabs read x, vertical tabs read y.
    pub axis: TrackAxis,
    /// Where the tab started.
    pub from: usize,
    /// Where it would land if released now.
    pub to: usize,
    /// Whether the pointer has travelled far enough from the press to count
    /// as a drag. Until then the gesture is a click on the tab, not a
    /// reorder: a real click almost always wobbles a pixel between press and
    /// release, and that must still select the tab.
    pub moved: bool,
    /// How far into the tab the pointer took hold, along the bar, so the tab
    /// rides under the hand where it was grabbed rather than by its edge.
    pub grab: f32,
    /// Where the pointer was pressed, along the bar.
    pub pressed: f32,
    /// Where the pointer is now, along the bar.
    pub pointer: f32,
}

fn tab_start(bounds: Bounds<Pixels>, axis: TrackAxis) -> f32 {
    match axis {
        TrackAxis::Vertical => f32::from(bounds.top()),
        _ => f32::from(bounds.left()),
    }
}

fn tab_end(bounds: Bounds<Pixels>, axis: TrackAxis) -> f32 {
    match axis {
        TrackAxis::Vertical => f32::from(bounds.bottom()),
        _ => f32::from(bounds.right()),
    }
}

/// How far a pressed tab has to travel before the press becomes a drag.
pub const TAB_DRAG_THRESHOLD: f32 = 4.0;

/// The one gesture the controls can have in flight. One pointer, one
/// gesture: a thumb, a track and a tab cannot be held at once, so they
/// share one slot rather than three that have to be kept exclusive by hand.
enum Drag {
    Scroll(ScrollDrag),
    Track(TrackDrag),
    Tab(TabDrag),
}

/// A pop-up list that is open, or on its way out.
#[derive(Clone, Copy, Debug)]
pub struct OpenCombo {
    pub id: ComboId,
    pub opened_at: Instant,
    /// The option that was current when the list opened, which is the row
    /// the list lays over the button. Kept through the fade out: by then a
    /// pick has changed the value, and the list must not jump to the new
    /// row on its way out.
    pub opened_on: usize,
    /// Which option the keyboard is on. Not the selection: a list you are
    /// arrowing through has not chosen anything yet, and closing it with
    /// Escape has to leave the old value alone. It commits on Enter and is
    /// dropped when the list closes.
    pub highlight: Option<usize>,
    /// When it was told to close, if it has been. It keeps rendering,
    /// without taking clicks, until the fade finishes.
    pub closing: Option<Instant>,
}

/// A context menu that is open: which menu, where it was summoned, and what
/// it was summoned on.
#[derive(Clone, Debug)]
pub struct OpenMenu {
    pub id: ComboId,
    /// Where the pointer was, in window coordinates. The menu opens here.
    pub anchor: Point<Pixels>,
    /// Optional element the menu hangs beneath instead, for a menu button.
    pub under: Option<Bounds<Pixels>>,
    /// What was clicked, in whatever form the host wants it back: a row id,
    /// a file path, a row id. The menu never looks inside it.
    pub target: SharedString,
    pub opened_at: Instant,
    /// Which row the keyboard is on, counting only the rows that can be
    /// activated — headers and separators are passed over rather than landed
    /// on, because arrowing onto something inert reads as a stuck key.
    pub highlight: Option<usize>,
    /// The submenu rows taken from the root to the visible level. Each one
    /// retains its reachable-row position so Back restores the cursor to the
    /// row that opened that level.
    pub(crate) levels: Vec<MenuBranch>,
    /// When the visible level last changed. The rows of a new level slide in
    /// under a pointer that was aimed at the old one, so they take no clicks
    /// until they have arrived.
    pub(crate) level_changed_at: Option<Instant>,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct MenuBranch {
    pub index: usize,
    pub highlight: usize,
    /// Fingerprint of this branch's label and its own rows. An index alone
    /// could point into a different submenu after the host rebuilds rows.
    pub identity: u64,
    /// How far the parent level was scrolled, so coming back out lands
    /// where the reader left it rather than at the top.
    pub scroll: Point<Pixels>,
}

/// A modal dialog that is open, or on its way out.
#[derive(Clone, Copy, Debug)]
pub struct OpenDialog {
    pub id: ComboId,
    pub opened_at: Instant,
    /// When it was told to close, if it has been. It keeps rendering, faded
    /// and without the keyboard, until the exit finishes.
    pub closing: Option<Instant>,
}

/// A command palette that is open.
#[derive(Clone, Debug)]
pub struct OpenPalette {
    pub id: ComboId,
    pub opened_at: Instant,
    /// Where the keyboard was before the palette took it. Closing puts it
    /// back: focus left on a field that no longer exists reaches nothing,
    /// and the shortcut that opens the palette would have to be re-earned
    /// with a click.
    pub return_focus: Option<FocusHandle>,
}

/// The tag the root's focus handle lives under.
const ROOT_FOCUS: &str = "vampir-root";

/// Per-view control state. `Default` is the empty state, which is also the
/// right starting point: nothing open, nothing animating, nothing dragged.
pub struct ControlState {
    /// The hue, saturation and scheme the palette is derived from, for a host with no
    /// theme of its own. See [`crate::theme`].
    pub theme: Theme,

    /// When the state was made. Every time in a record is measured from
    /// here, in microseconds, so a record carries a word rather than an
    /// `Instant`.
    epoch: Instant,
    /// Frames counted by [`ControlState::animating`], which is called once
    /// per render. Wraps, and is only ever compared by difference.
    frame: Cell<u32>,
    /// Every duration is multiplied by this; see
    /// [`ControlState::set_time_scale`]. `None` is one.
    time_scale: Option<f32>,

    /// Everything the controls remember about themselves from frame to
    /// frame: what each two-state element showed, where each continuous
    /// value is on its way to its target, which groups of rows were up last
    /// frame, the keyboard's row in each filtering list, and where each
    /// track, group and slot painted. One table, and every record in it is
    /// retired by [`ControlState::animating`] a frame or two after the thing
    /// it belongs to stops rendering, so the table is the size of the
    /// screen rather than of the session.
    ///
    /// Behind a `RefCell` because the records are read and written during
    /// render, from `&ControlState`: a control notices its own change while
    /// it renders. See [`ControlState::transition`].
    records: RefCell<TagMap<Entry>>,
    color_pads: RefCell<crate::swatch::PadCache>,
    /// Sizes to remember after the thing measured has gone: a disclosure's
    /// body, so it can be shown growing to the height it will have rather
    /// than appearing at it. Written from paint, kept until overwritten,
    /// and bounded by the widgets that ask.
    measured: TagMap<Bounds<Pixels>>,

    /// One focus handle per composite control.
    ///
    /// A group — radio buttons, a segmented control, a tab bar, a tree — is
    /// one stop in the tab order, and the ring belongs on whichever option
    /// inside it is current rather than around the whole group. That means
    /// the handle has to move from option to option as the selection moves,
    /// which it can only do if it outlives all of them: a handle owned by an
    /// option dies the moment that option stops being the current one, and
    /// takes the keyboard with it after a single arrow press. Never retired,
    /// for the same reason: a handle the keyboard is on, or is coming back
    /// to, has to stay the same handle.
    ///
    /// A `RefCell` because this is a cache, not state. A control asks for
    /// its handle while it renders, holding only `&ControlState`, and the
    /// answer is the same handle every frame.
    focus_handles: RefCell<TagMap<FocusHandle>>,
    /// One scroll handle per pop-up list, so a list can read its own offset
    /// and fade its edges. Same reasoning as `focus_handles`.
    scroll_handles: RefCell<TagMap<ScrollHandle>>,

    /// The pop-up list that is open, or still fading out.
    combo: Option<OpenCombo>,
    /// A pop-up just closed by a press outside it, and when. The same
    /// press's click must not reopen it when it landed on that pop-up's own
    /// button. The release that should clear it can land on an occluding
    /// surface and never reach the host, so the marker expires on its own
    /// too.
    combo_dismissed: Option<(ComboId, Instant)>,

    /// The open context menu, if any.
    pub menu: Option<OpenMenu>,
    /// The handle the open context menu holds focus with.
    ///
    /// A menu has to hold focus to hear the arrows, and the handle has to be
    /// the same one every frame — a fresh handle per render would lose focus
    /// the instant it was given. Created once, on the first menu opened, and
    /// reused after that.
    pub menu_focus: Option<FocusHandle>,
    /// Where the keyboard was before the menu took it, so closing puts it
    /// back rather than dropping the reader at the top of the window.
    pub menu_return_focus: Option<FocusHandle>,

    /// The open dialog, if any, or one still fading out.
    pub dialog: Option<OpenDialog>,
    /// The same as `menu_return_focus`, for a modal dialog.
    pub dialog_return_focus: Option<FocusHandle>,
    /// The dialog whose buttons Tab stays among while it is up, and how many
    /// it has; the handles themselves are in `focus_handles` under
    /// [`ControlState::dialog_button_focus`]. Set every frame the dialog
    /// renders and cleared when it does not. See
    /// [`crate::keyboard::move_focus`].
    dialog_trap: RefCell<Option<(ComboId, Vec<usize>)>>,

    /// The open command palette, if any.
    pub palette_overlay: Option<OpenPalette>,
    /// The shortcut recorder waiting for a key chord, if any.
    pub recording: Option<ComboId>,
    pub(crate) recording_keys: Option<[gpui::Subscription; 2]>,
    pub(crate) recording_activation: Option<&'static str>,
    /// True while the mouse is in charge and the focus ring is hidden. Any
    /// mouse press sets it and Tab clears it, and the first Tab after the
    /// mouse only shows where the keyboard is: see `keyboard::move_focus`.
    pub ring_hidden: bool,

    /// The gesture in flight, if any.
    drag: Option<Drag>,
    /// A rendered tab bar changed axis while its drag was in flight.
    tab_drag_invalidated: Cell<bool>,
}

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

impl ControlState {
    pub fn new() -> Self {
        Self {
            theme: Theme::default(),
            epoch: Instant::now(),
            frame: Cell::new(0),
            time_scale: None,
            records: RefCell::default(),
            color_pads: RefCell::default(),
            measured: TagMap::default(),
            focus_handles: RefCell::default(),
            scroll_handles: RefCell::default(),
            combo: None,
            combo_dismissed: None,
            menu: None,
            menu_focus: None,
            menu_return_focus: None,
            dialog: None,
            dialog_return_focus: None,
            dialog_trap: RefCell::new(None),
            palette_overlay: None,
            recording: None,
            recording_keys: None,
            recording_activation: None,
            ring_hidden: false,
            drag: None,
            tab_drag_invalidated: Cell::new(false),
        }
    }

    /// Microseconds since the state was made: the clock every record keeps
    /// time by.
    fn now(&self) -> u64 {
        u64::try_from(self.epoch.elapsed().as_micros()).unwrap_or(u64::MAX)
    }

    // ---- Pop-ups ----

    /// Whether `combo` is open — not counting one that is fading out.
    pub fn is_combo_open(&self, combo: ComboId) -> bool {
        self.combo
            .as_ref()
            .is_some_and(|open| open.id == combo && open.closing.is_none())
    }

    /// Closes the open pop-up, letting its list fade back out the way it
    /// faded in. A list closed twice leaves the first fade running.
    pub fn close_combo(&mut self) {
        if let Some(open) = self.combo.as_mut()
            && open.closing.is_none()
        {
            open.closing = Some(Instant::now());
            open.highlight = None;
        }
    }

    /// Opens one pop-up on `chosen`, its current option, in place of
    /// whichever was open or fading. The list lays that row over the button,
    /// and the keyboard starts on it, so the first arrow press moves from
    /// there rather than from the top.
    pub fn open_combo(&mut self, combo: ComboId, chosen: usize) {
        self.combo = Some(OpenCombo {
            id: combo,
            opened_at: Instant::now(),
            opened_on: chosen,
            highlight: Some(chosen),
            closing: None,
        });
    }

    /// Puts the keyboard on one option of the open list.
    pub fn highlight_combo(&mut self, index: usize) {
        if let Some(open) = self.combo.as_mut()
            && open.closing.is_none()
        {
            open.highlight = Some(index);
        }
    }

    /// Which option the keyboard is on, falling back to `selected` for a
    /// list opened with the mouse and never arrowed through.
    pub fn combo_highlight_or(&self, selected: usize) -> usize {
        self.combo
            .as_ref()
            .filter(|open| open.closing.is_none())
            .and_then(|open| open.highlight)
            .unwrap_or(selected)
    }

    /// The option `combo`'s list opened on, while the list is on screen at
    /// all — open or fading out.
    pub fn combo_opened_on(&self, combo: ComboId) -> Option<usize> {
        self.combo_fade(combo)?;
        self.combo.as_ref().map(|open| open.opened_on)
    }

    /// `(reveal, still_animating)` for `combo`'s list if it is on screen at
    /// all, open or leaving; `None` once it has gone. Reveal runs 0 to 1,
    /// eased: opening eases out and closing eases in, so both ends of the
    /// motion sit against the button rather than drifting from it.
    pub fn combo_fade(&self, combo: ComboId) -> Option<(f32, bool)> {
        let open = self.combo.as_ref().filter(|open| open.id == combo)?;
        let reveal = self.scaled(COMBO_REVEAL);
        let fade = crate::easing::modal_opacity_at(
            Some(open.opened_at),
            open.closing,
            Instant::now(),
            reveal,
            reveal,
        );
        (open.closing.is_none() || fade.1).then_some(fade)
    }

    /// A press landed outside the open list of `combo`: closes it, and
    /// marks it so the same press's click, if it lands on the list's own
    /// button, does not reopen it.
    pub fn combo_pressed_outside(&mut self, combo: ComboId) {
        self.close_combo();
        self.combo_dismissed = Some((combo, Instant::now()));
    }

    /// The pop-up a press outside it closed a moment ago, if any, clearing
    /// the marker either way: one no toggle consumed, because the click
    /// landed elsewhere, must not eat some later toggle click.
    pub fn take_combo_dismissal(&mut self) -> Option<ComboId> {
        let (combo, at) = self.combo_dismissed.take()?;
        (at.elapsed() < COMBO_REVEAL).then_some(combo)
    }

    // ---- Animation ----

    /// Slows every animation down by `scale` — eight makes a 140ms slide
    /// take over a second. For looking at motion: a screenshot taken during
    /// a slide is the only way to see what the slide actually does, and at
    /// full speed the shutter is slower than the slide.
    /// [`ControlState::slow_motion_from_env`] reads it from the environment.
    pub fn set_time_scale(&mut self, scale: f32) {
        self.time_scale = (scale.is_finite() && scale > 0.0 && scale != 1.0).then_some(scale);
    }

    /// The time scale every animation is running at, when it is not one.
    pub fn time_scale(&self) -> Option<f32> {
        self.time_scale
    }

    /// Reads `VAMPIR_SLOW_MOTION` into the time scale, so a host can be
    /// looked at slowed down without a build: `VAMPIR_SLOW_MOTION=8 app`.
    /// Returns the scale if one was set, for a host that wants to say so on
    /// screen — a screenshot taken in slow motion should not pass for the
    /// real thing.
    pub fn slow_motion_from_env(&mut self) -> Option<f32> {
        let scale = std::env::var("VAMPIR_SLOW_MOTION")
            .ok()
            .and_then(|value| value.parse::<f32>().ok())?;
        self.set_time_scale(scale);
        self.time_scale
    }

    /// A duration at the current time scale.
    pub fn scaled(&self, duration: Duration) -> Duration {
        match self.time_scale {
            Some(scale) => Duration::try_from_secs_f64(duration.as_secs_f64() * f64::from(scale))
                .unwrap_or(Duration::MAX),
            None => duration,
        }
    }

    // ---- Theme ----

    /// Starts following the desktop's colour scheme: reads it now, and keeps
    /// [`Theme::system_is_dark`](crate::theme::Theme::system_is_dark) up to
    /// date as it changes. Call it once, in the view's constructor. A window
    /// that opens dark on a light desktop looks broken before it looks like
    /// a choice.
    pub fn observe_appearance<V: ControlHost>(&mut self, window: &Window, cx: &mut Context<V>) {
        self.theme.set_system_dark(system_dark(window));
        let this = cx.weak_entity();
        let subscription = window.observe_window_appearance(move |window, cx| {
            let dark = system_dark(window);
            this.update(cx, |this, cx| {
                if this.control_state_mut().theme.set_system_dark(dark) {
                    cx.notify();
                }
            })
            .ok();
        });
        self.theme.keep_appearance(subscription);
    }

    /// The palette to hand every control this frame.
    ///
    /// The theme's scheme, hue and saturation are the truth; this is what is shown, and
    /// it follows the truth rather than jumping to it. A scheme crosses over
    /// through [`Palette::mix`] over [`SCHEME_FADE`], so a change from a menu
    /// item, a shortcut, a command palette or the desktop switching to dark
    /// at sunset all arrive the same way: every colour on screen mixed
    /// between the palette it had and the one it is getting. A hue set from
    /// anywhere but its own slider glides the short way round. Saturation
    /// changes also glide; while either slider is held its value sits under
    /// the hand.
    pub fn palette(&self) -> Palette {
        let theme = &self.theme;
        let darkness = self.tween(
            "vampir-scheme-dark",
            if theme.is_dark() { 1.0 } else { 0.0 },
            SCHEME_FADE,
        );
        let held = theme
            .hue_track()
            .is_some_and(|track| self.is_dragging(track));
        let hue = f64::from(if held {
            self.snap("vampir-scheme-hue", theme.hue as f32)
        } else {
            self.tween_angle("vampir-scheme-hue", theme.hue as f32, MOVE)
        });
        let saturation = normalized_saturation(theme.saturation) as f32;
        let saturation_held = theme
            .saturation_track()
            .is_some_and(|track| self.is_dragging(track));
        let saturation = f64::from(if saturation_held {
            self.snap("vampir-scheme-saturation", saturation)
        } else {
            self.tween("vampir-scheme-saturation", saturation, MOVE)
        });
        if darkness <= 0.0 {
            theme.palette_at(hue, saturation, false)
        } else if darkness >= 1.0 {
            theme.palette_at(hue, saturation, true)
        } else {
            Palette::mix(
                theme.palette_at(hue, saturation, false),
                theme.palette_at(hue, saturation, true),
                darkness,
            )
        }
    }

    // ---- Dialogs ----

    /// Opens the modal dialog `id`. It fades in, takes the keyboard when it
    /// first paints, and keeps Tab among its own buttons until it closes.
    pub fn open_dialog(&mut self, id: ComboId) {
        self.dismiss_popups();
        *self.dialog_trap.get_mut() = None;
        self.dialog = Some(OpenDialog {
            id,
            opened_at: Instant::now(),
            closing: None,
        });
    }

    /// Starts the open dialog on its way out. It keeps rendering, faded,
    /// until the exit finishes; a dialog closed twice leaves the first exit
    /// running rather than restarting it.
    pub fn close_dialog(&mut self) {
        *self.dialog_trap.get_mut() = None;
        if let Some(dialog) = self.dialog.as_mut()
            && dialog.closing.is_none()
        {
            dialog.closing = Some(Instant::now());
        }
    }

    /// Whether `id` is open — not counting one that is fading out.
    pub fn is_dialog_open(&self, id: ComboId) -> bool {
        self.dialog
            .as_ref()
            .is_some_and(|dialog| dialog.id == id && dialog.closing.is_none())
    }

    /// `(opacity, still_animating)` for dialog `id` if it is on screen at
    /// all, open or leaving; `None` once it has gone. An exit that started
    /// mid-entrance fades from wherever the entrance had got to.
    pub fn dialog_fade(&self, id: ComboId) -> Option<(f32, bool)> {
        let dialog = self.dialog.as_ref().filter(|dialog| dialog.id == id)?;
        let fade = crate::easing::modal_opacity_at(
            Some(dialog.opened_at),
            dialog.closing,
            Instant::now(),
            self.scaled(crate::easing::MODAL_ENTER),
            self.scaled(crate::easing::MODAL_EXIT),
        );
        (dialog.closing.is_none() || fade.1).then_some(fade)
    }

    // ---- Command palette ----

    /// Opens the command palette `id`, remembering where the keyboard was
    /// and putting it in `query`, emptied. A palette that opens without
    /// focus is a box you have to click before you can type into, which is
    /// the one thing nobody reaching for its shortcut wants to do.
    pub fn open_palette(
        &mut self,
        id: ComboId,
        query: &Entity<TextInput>,
        window: &mut Window,
        cx: &mut App,
    ) {
        self.dismiss_popups();
        self.restore_menu_focus(window, cx);
        let return_focus = self
            .palette_overlay
            .take()
            .and_then(|open| open.return_focus)
            .or_else(|| window.focused(cx));
        self.palette_overlay = Some(OpenPalette {
            id,
            opened_at: Instant::now(),
            return_focus,
        });
        self.set_list_highlight(id.into(), 0, 0, false);
        let scroll = self.scroll((id, "list"));
        scroll.set_offset(Point::default());
        query.update(cx, |input, cx| input.set_text("", cx));
        let focus = query.read(cx).focus_handle.clone();
        window.focus(&focus, cx);
    }

    /// Closes the open palette and hands the keyboard back where it was, or
    /// to the root — somewhere real, because the query field is about to
    /// stop existing and focus left on it goes nowhere.
    pub fn close_palette(&mut self, window: &mut Window, cx: &mut App) {
        if let Some(open) = self.palette_overlay.take() {
            self.return_focus_to(open.return_focus, window, cx);
        }
    }

    /// Opens the palette if it is closed and closes it if it is open: what
    /// its shortcut does.
    pub fn toggle_palette(
        &mut self,
        id: ComboId,
        query: &Entity<TextInput>,
        window: &mut Window,
        cx: &mut App,
    ) {
        if self.is_palette_open(id) {
            self.close_palette(window, cx);
        } else {
            self.open_palette(id, query, window, cx);
        }
    }

    pub fn is_palette_open(&self, id: ComboId) -> bool {
        self.palette_overlay
            .as_ref()
            .is_some_and(|open| open.id == id)
    }

    // ---- Filtering lists ----

    /// Which row the keyboard is on in list `list`, given the query the
    /// list is currently filtered by. A changed query puts it back on the
    /// first row: the rows have been re-ranked, and the best match is the
    /// one the person most likely means.
    pub fn list_highlight(&self, list: impl Into<Tag>, query: &str) -> usize {
        let hash = Tag::new(query).0;
        let frame = self.frame.get();
        let mut records = self.records.borrow_mut();
        let entry = records
            .entry(Kind::List.slot(list.into()))
            .or_insert(Entry {
                touched: frame,
                record: Record::List {
                    highlight: 0,
                    query: hash,
                    item: 0,
                    chosen: false,
                },
            });
        entry.touched = frame;
        let Record::List {
            highlight,
            query,
            item,
            chosen,
        } = &mut entry.record
        else {
            return 0;
        };
        if *query != hash {
            *query = hash;
            *highlight = 0;
            *item = 0;
            *chosen = false;
        }
        *highlight
    }

    /// [`list_highlight`](Self::list_highlight) for a list that knows its
    /// rows' ids. A new query goes back to the first row. Until the
    /// highlight is moved it stays on the first row as results change under
    /// the same query, so the best match is still the one Enter picks when
    /// a better one arrives. Once it has been moved — by the keys, or by
    /// [`highlight_list`](Self::highlight_list) — it stays on the result it
    /// was moved to as results arrive or re-rank, so Enter never picks
    /// something the person did not choose; only if that result has gone
    /// does it fall back to the first row.
    pub(crate) fn follow_list(
        &self,
        list: impl Into<Tag>,
        query: &str,
        ids: &[SharedString],
    ) -> (usize, ListChange) {
        let hash = Tag::new(query).0;
        let tag = |id: &SharedString| Tag::from(id).0;
        let frame = self.frame.get();
        let mut records = self.records.borrow_mut();
        let slot = Kind::List.slot(list.into());
        // A list arriving afresh starts at the top, wherever a scroll
        // handle that outlived its last showing was left.
        let fresh = !records.contains_key(&slot);
        let entry = records.entry(slot).or_insert(Entry {
            touched: frame,
            record: Record::List {
                highlight: 0,
                query: hash,
                item: 0,
                chosen: false,
            },
        });
        entry.touched = frame;
        let Record::List {
            highlight,
            query,
            item,
            chosen,
        } = &mut entry.record
        else {
            return (0, ListChange::Same);
        };
        let change = if fresh || *query != hash {
            *query = hash;
            *highlight = 0;
            *chosen = false;
            ListChange::Reset
        } else if *chosen && *item != 0 && ids.get(*highlight).map(tag) != Some(*item) {
            match ids.iter().position(|id| tag(id) == *item) {
                Some(index) => {
                    *highlight = index;
                    ListChange::Moved
                }
                None => {
                    *highlight = 0;
                    *chosen = false;
                    ListChange::Reset
                }
            }
        } else {
            ListChange::Same
        };
        *highlight = (*highlight).min(ids.len().saturating_sub(1));
        *item = ids.get(*highlight).map_or(0, tag);
        (*highlight, change)
    }

    /// The keyboard's row in list `list` as last set, whatever the query.
    pub fn list_position(&self, list: impl Into<Tag>) -> usize {
        match self.records.borrow().get(&Kind::List.slot(list.into())) {
            Some(Entry {
                record: Record::List { highlight, .. },
                ..
            }) => *highlight,
            _ => 0,
        }
    }

    /// Puts the keyboard on row `index` of list `list`.
    pub fn highlight_list(&self, list: impl Into<Tag>, index: usize) {
        self.set_list_highlight(list.into(), index, 0, true);
    }

    /// Puts the keyboard on row `index` of list `list`, which shows result
    /// `id` there. Naming the result as well as the row lets
    /// [`follow_list`](Self::follow_list) keep the keyboard on it even if
    /// the results change before the list is next drawn.
    pub(crate) fn highlight_list_item(
        &self,
        list: impl Into<Tag>,
        index: usize,
        id: &SharedString,
    ) {
        self.set_list_highlight(list.into(), index, Tag::from(id).0, true);
    }

    fn set_list_highlight(&self, list: Tag, index: usize, to: u64, moved: bool) {
        let frame = self.frame.get();
        let mut records = self.records.borrow_mut();
        let entry = records.entry(Kind::List.slot(list)).or_insert(Entry {
            touched: frame,
            record: Record::List {
                highlight: index,
                query: Tag::new("").0,
                item: to,
                chosen: moved,
            },
        });
        entry.touched = frame;
        if let Record::List {
            highlight,
            item,
            chosen,
            ..
        } = &mut entry.record
        {
            *highlight = index;
            *item = to;
            *chosen = moved;
        }
    }

    // ---- Records ----

    /// Progress of `tag`'s transition to `value`, 0..=1, starting one when
    /// the value is not what it showed last frame.
    ///
    /// Called from render rather than from the click handler, so a switch
    /// flipped by a menu item, a shortcut, a command palette or the host's
    /// own code slides exactly as one flipped by the pointer does. The
    /// animation is about the state changing, not about who changed it. The
    /// first time an element is seen it paints its end state: nothing should
    /// animate in from nowhere on the first frame.
    ///
    /// Because the change is noticed *during* render, a host has to ask
    /// [`ControlState::animating`] after its controls have been built, not
    /// before — otherwise the frame that starts a slide never asks for the
    /// frame that would continue it, and the control sits at the start of
    /// its slide until something else repaints. GPUI ignores a `notify` from
    /// inside render, so the control cannot ask on the host's behalf.
    pub fn transition(&self, tag: impl Into<Tag>, value: u64, duration: Duration) -> f32 {
        let now = self.now();
        let frame = self.frame.get();
        let mut records = self.records.borrow_mut();
        let entry = records.entry(Kind::Flip.slot(tag.into())).or_insert(Entry {
            touched: frame,
            record: Record::Flip {
                value,
                since: now,
                duration: 0,
            },
        });
        entry.touched = frame;
        let Record::Flip {
            value: shown,
            since,
            duration: running,
        } = &mut entry.record
        else {
            return 1.0;
        };
        if *shown != value {
            *shown = value;
            *since = now;
            *running = micros(self.scaled(duration));
        }
        progress_of(now, *since, *running)
    }

    /// Where a two-state element is between its off (0) and on (1) looks,
    /// eased, given whether it is on now. The companion to
    /// [`ControlState::transition`] for the common case: a control mixes its
    /// two looks by this number and gets its slide in either direction, from
    /// wherever it was when the state flipped back.
    ///
    /// Notice it during render, so the host asks [`ControlState::animating`]
    /// after its controls have been built.
    pub fn blend(&self, tag: impl Into<Tag>, on: bool, duration: Duration) -> f32 {
        self.tween(tag, if on { 1.0 } else { 0.0 }, duration)
    }

    /// A continuous value on its way to `target`: the pill under a segmented
    /// control's selection, a row's place in a re-sorted table, the darkness
    /// of a scheme. Returns what to draw this frame.
    ///
    /// Asked for a new target it sets off from wherever it is now, so a
    /// change of mind mid-slide bends the motion rather than restarting it.
    /// The first time a tag is seen it is already at its target: nothing
    /// slides in from nowhere on the first frame; [`ControlState::tween_from`]
    /// is for the one that should. Like [`ControlState::blend`], this is
    /// noticed during render, so the host asks [`ControlState::animating`]
    /// after its controls have been built.
    pub fn tween(&self, tag: impl Into<Tag>, target: f32, duration: Duration) -> f32 {
        self.tween_inner(tag.into(), None, target, duration)
    }

    /// [`ControlState::tween`], but a tag seen for the first time starts at
    /// `initial` and slides to `target` — for a row fading in when it joins
    /// a list. Pair it with [`ControlState::present`] so the first frame of
    /// the whole list is not a hundred rows fading in at once.
    pub fn tween_from(
        &self,
        tag: impl Into<Tag>,
        initial: f32,
        target: f32,
        duration: Duration,
    ) -> f32 {
        self.tween_inner(tag.into(), Some(initial), target, duration)
    }

    /// A tween for an angle in degrees, which goes the short way round: from
    /// 350 to 10 is twenty degrees, not three hundred and forty.
    pub fn tween_angle(&self, tag: impl Into<Tag>, degrees: f32, duration: Duration) -> f32 {
        let tag = tag.into();
        let target = match self.records.borrow().get(&Kind::Tween.slot(tag)) {
            Some(Entry {
                record: Record::Tween { to, .. },
                ..
            }) => {
                let here = *to;
                let delta = (degrees - here).rem_euclid(360.0);
                here + if delta > 180.0 { delta - 360.0 } else { delta }
            }
            _ => degrees,
        };
        self.tween_inner(tag, None, target, duration)
    }

    /// Puts a tween at `value` at once, with no slide. For a value the hand
    /// is dragging: the pointer is the animation, and a control that trails
    /// it reads as lag.
    pub fn snap(&self, tag: impl Into<Tag>, value: f32) -> f32 {
        self.records.borrow_mut().insert(
            Kind::Tween.slot(tag.into()),
            Entry {
                touched: self.frame.get(),
                record: Record::Tween {
                    from: value,
                    to: value,
                    since: self.now(),
                    duration: 0,
                },
            },
        );
        value
    }

    fn tween_inner(&self, tag: Tag, initial: Option<f32>, target: f32, duration: Duration) -> f32 {
        let now = self.now();
        let frame = self.frame.get();
        let duration = micros(self.scaled(duration));
        let mut records = self.records.borrow_mut();
        let entry = records
            .entry(Kind::Tween.slot(tag))
            .or_insert_with(|| Entry {
                touched: frame,
                record: match initial {
                    Some(from) => Record::Tween {
                        from,
                        to: target,
                        since: now,
                        duration,
                    },
                    None => Record::Tween {
                        from: target,
                        to: target,
                        since: now,
                        duration: 0,
                    },
                },
            });
        entry.touched = frame;
        let Record::Tween {
            from,
            to,
            since,
            duration: running,
        } = &mut entry.record
        else {
            return target;
        };
        if *to != target {
            *from = tween_value(now, *from, *to, *since, *running);
            *to = target;
            *since = now;
            *running = duration;
        }
        tween_value(now, *from, *to, *since, *running)
    }

    /// Whether a group of rows — a list, a tree, a menu — was already on
    /// screen last frame. Call it once per row with the *group's* tag.
    ///
    /// A row joining a list that is already up should fade in; a list that
    /// has just appeared as a whole should not fade in row by row, it is
    /// arriving with whatever brought it. This tells the two apart, and it
    /// answers the same for every row in a frame however many ask.
    pub fn present(&self, group: impl Into<Tag>) -> bool {
        let frame = self.frame.get();
        let mut records = self.records.borrow_mut();
        let entry = records
            .entry(Kind::Streak.slot(group.into()))
            .or_insert(Entry {
                touched: frame,
                record: Record::Streak { started: frame },
            });
        let Record::Streak { started } = &mut entry.record else {
            return false;
        };
        // A gap of more than a frame ends the streak.
        if frame.wrapping_sub(entry.touched) > 1 {
            *started = frame;
        }
        entry.touched = frame;
        *started != frame
    }

    /// The scroll handle for a pop-up list, made the first time it is asked
    /// for and the same one thereafter, so the list can read its own offset
    /// and fade the edge that has something past it.
    pub fn scroll(&self, list: impl Into<Tag>) -> ScrollHandle {
        self.scroll_handles
            .borrow_mut()
            .entry(list.into())
            .or_default()
            .clone()
    }

    /// Whether a selected tab still needs to be brought into view. Its first
    /// render has nothing painted to measure, so the request survives until
    /// a render that has.
    /// A completed request stays completed through ordinary manual scrolls.
    pub(crate) fn tab_reveal_pending(&self, bar: ComboId, selection: impl Hash) -> bool {
        let selection = Tag::new(selection);
        let frame = self.frame.get();
        let mut records = self.records.borrow_mut();
        let entry = records
            .entry(Kind::Reveal.slot(Tag::new((bar, "selected-tab"))))
            .or_insert(Entry {
                touched: frame,
                record: Record::Reveal {
                    selection,
                    pending: true,
                    retries: 0,
                },
            });
        entry.touched = frame;
        let Record::Reveal {
            selection: shown,
            pending,
            retries,
        } = &mut entry.record
        else {
            return false;
        };
        if *shown != selection {
            *shown = selection;
            *pending = true;
            *retries = 0;
        }
        if *pending {
            *retries = retries.saturating_add(1);
        }
        *pending
    }

    /// A successful reveal is one shot: the selected tab can later be
    /// scrolled away by hand without being pulled back on the next frame.
    pub(crate) fn finish_tab_reveal(&self, bar: ComboId, selection: impl Hash) {
        let selection = Tag::new(selection);
        let mut records = self.records.borrow_mut();
        if let Some(Entry {
            record:
                Record::Reveal {
                    selection: shown,
                    pending,
                    ..
                },
            ..
        }) = records.get_mut(&Kind::Reveal.slot(Tag::new((bar, "selected-tab"))))
            && *shown == selection
        {
            *pending = false;
        }
    }

    /// True while a fade or slide still needs frames, or a selected tab is
    /// waiting for its first painted bounds. A host folds this into whatever
    /// decides to request the next frame.
    ///
    /// Also the end of a frame's bookkeeping: it is called once per render,
    /// after everything has been built, so it counts frames, and retires the
    /// records of anything that has not been rendered for two of them. That
    /// is what lets a row that leaves a list and comes back be new again,
    /// what keeps the table the size of the screen, and why a host that
    /// skips calling this leaks nothing worse than a few stale entries.
    pub fn animating(&self) -> bool {
        let frame = self.frame.get();
        self.frame.set(frame.wrapping_add(1));
        self.color_pads.borrow_mut().retain_recent(frame);
        let now = self.now();
        let mut running = false;
        let mut records = self.records.borrow_mut();
        records.retain(|_, entry| {
            if frame.wrapping_sub(entry.touched) > 1 {
                return false;
            }
            running |= match entry.record {
                Record::Flip {
                    since, duration, ..
                }
                | Record::Tween {
                    since, duration, ..
                } => now.saturating_sub(since) < u64::from(duration),
                Record::Reveal {
                    pending, retries, ..
                } => pending && retries <= 3,
                _ => false,
            };
            true
        });
        // Retain visits empty buckets too. A large list that disappeared
        // must not leave every later frame scanning its peak allocation.
        if records.capacity() > records.len().saturating_mul(4).max(64) {
            let target = records.len().saturating_mul(2).max(32);
            records.shrink_to(target);
        }
        drop(records);
        if running {
            return true;
        }
        if let Some(open) = &self.combo
            && self.combo_fade(open.id).is_some_and(|(_, running)| running)
        {
            return true;
        }
        let reveal = self.scaled(COMBO_REVEAL);
        if self
            .menu
            .as_ref()
            .is_some_and(|menu| menu.opened_at.elapsed() < reveal)
        {
            return true;
        }
        if self
            .palette_overlay
            .as_ref()
            .is_some_and(|open| open.opened_at.elapsed() < reveal)
        {
            return true;
        }
        self.dialog.as_ref().is_some_and(|dialog| {
            self.dialog_fade(dialog.id)
                .is_some_and(|(_, running)| running)
        })
    }

    // ---- Geometry ----

    pub(crate) fn color_pad_colors(&self, id: ComboId, hue: f64, rows: usize) -> Rc<[gpui::Rgba]> {
        self.color_pads
            .borrow_mut()
            .colors(id, hue, rows, self.frame.get())
    }

    /// Records where something painted this frame, under `tag`. Called from
    /// a paint probe; retired with everything else once the probe stops
    /// painting.
    pub fn record_bounds(&mut self, tag: impl Into<Tag>, bounds: Bounds<Pixels>) {
        self.records.borrow_mut().insert(
            Kind::Bounds.slot(tag.into()),
            Entry {
                touched: self.frame.get(),
                record: Record::Bounds(bounds),
            },
        );
    }

    /// Where `tag` painted last frame, if it did.
    pub fn bounds(&self, tag: impl Into<Tag>) -> Option<Bounds<Pixels>> {
        match self.records.borrow().get(&Kind::Bounds.slot(tag.into())) {
            Some(Entry {
                record: Record::Bounds(bounds),
                ..
            }) => Some(*bounds),
            _ => None,
        }
    }

    /// Records the bounds of a track — a slider's, a pop-up button's, a menu
    /// button's — as it paints. A drag needs them long after the pointer has
    /// left the track, and a list is laid over the button wherever the
    /// button is nested.
    pub fn record_track(&mut self, id: ComboId, bounds: Bounds<Pixels>) {
        self.record_bounds((id, "track"), bounds);
    }

    /// Where track `id` painted last frame.
    pub fn track(&self, id: ComboId) -> Option<Bounds<Pixels>> {
        self.bounds((id, "track"))
    }

    /// Records the bounds of the group around a set of options — a
    /// segmented control's well, a tab bar's — as it paints. The options'
    /// bounds only mean something relative to these.
    pub fn record_group(&mut self, id: ComboId, bounds: Bounds<Pixels>) {
        self.record_bounds((id, "group"), bounds);
    }

    /// Where group `id` painted last frame.
    pub fn group(&self, id: ComboId) -> Option<Bounds<Pixels>> {
        self.bounds((id, "group"))
    }

    /// Records the bounds of option `slot` of group `id` as it paints. The
    /// selection pill slides between these, and a tab drag compares the
    /// pointer with them.
    pub fn record_slot(&mut self, id: ComboId, slot: usize, bounds: Bounds<Pixels>) {
        self.record_bounds((id, "slot", slot), bounds);
    }

    /// Where option `slot` of group `id` painted last frame.
    pub fn slot(&self, id: ComboId, slot: usize) -> Option<Bounds<Pixels>> {
        self.bounds((id, "slot", slot))
    }

    /// Remembers a size after the thing measured has gone: a disclosure's
    /// body, so the next time it opens it can grow to the height it had
    /// rather than appear at it. Unlike [`ControlState::record_bounds`]
    /// this is kept, so tag it by the widget, not by its data.
    pub fn measure(&mut self, tag: impl Into<Tag>, bounds: Bounds<Pixels>) {
        self.measured.insert(tag.into(), bounds);
    }

    /// The last size remembered under `tag`.
    pub fn measured(&self, tag: impl Into<Tag>) -> Option<Bounds<Pixels>> {
        self.measured.get(&tag.into()).copied()
    }

    // ---- Drags ----

    pub fn scroll_dragging(&self) -> bool {
        matches!(self.drag, Some(Drag::Scroll(_)))
    }

    pub fn track_dragging(&self) -> bool {
        matches!(self.drag, Some(Drag::Track(_)))
    }

    /// Whether a particular track is the one being dragged.
    pub fn is_dragging(&self, id: ComboId) -> bool {
        matches!(&self.drag, Some(Drag::Track(drag)) if drag.id == id)
    }

    /// The tab reorder in flight, if any.
    pub fn tab_drag(&self) -> Option<&TabDrag> {
        match &self.drag {
            Some(Drag::Tab(drag)) => Some(drag),
            _ => None,
        }
    }

    fn scroll_drag(&self) -> Option<&ScrollDrag> {
        match &self.drag {
            Some(Drag::Scroll(drag)) => Some(drag),
            _ => None,
        }
    }

    /// Starts a scrollbar thumb drag, in place of any other gesture.
    pub fn begin_scroll_drag(&mut self, drag: ScrollDrag) {
        self.drag = Some(Drag::Scroll(drag));
    }

    /// Starts a drag on a track, recording how it should be read.
    pub fn begin_track_drag(&mut self, id: ComboId, axis: TrackAxis, stops: Option<u32>) {
        self.drag = Some(Drag::Track(TrackDrag { id, axis, stops }));
    }

    /// Starts a tab reorder.
    pub fn begin_tab_drag(&mut self, drag: TabDrag) {
        self.tab_drag_invalidated.set(false);
        self.drag = Some(Drag::Tab(drag));
    }

    /// A tab bar can change orientation while a press is held. Remember
    /// that its old positions cannot be used for a reorder on release.
    pub(crate) fn note_tab_axis(&self, bar: ComboId, axis: TrackAxis) -> bool {
        if matches!(&self.drag, Some(Drag::Tab(drag)) if drag.bar == bar && drag.axis != axis) {
            self.tab_drag_invalidated.set(true);
        }
        !self.tab_drag_invalidated.get()
    }

    /// Where along the dragged track a pointer position falls, each
    /// component 0..=1 and already snapped to the track's stops. `None`
    /// when nothing is being dragged, or when the track has not painted yet
    /// and so has no bounds.
    ///
    /// Both components are always filled in; a horizontal track's `y` is
    /// simply not interesting.
    pub fn track_ratio_at(&self, position: Point<Pixels>) -> Option<(ComboId, Point<f32>)> {
        let Some(Drag::Track(drag)) = &self.drag else {
            return None;
        };
        let bounds = self.track(drag.id)?;
        let width = f32::from(bounds.size.width);
        let height = f32::from(bounds.size.height);
        if width <= 0.0 || height <= 0.0 {
            return None;
        }
        let snap = |ratio: f32| match drag.stops {
            Some(stops) if stops >= 2 => {
                let last = (stops - 1) as f32;
                (ratio * last).round() / last
            }
            _ => ratio,
        };
        let x = ((f32::from(position.x) - f32::from(bounds.origin.x)) / width).clamp(0.0, 1.0);
        let y = ((f32::from(position.y) - f32::from(bounds.origin.y)) / height).clamp(0.0, 1.0);
        // A pad snaps on both axes; a one-dimensional track only on its own,
        // so the unused component stays exact for a host that reads it.
        let point = match drag.axis {
            TrackAxis::Horizontal => Point::new(snap(x), y),
            TrackAxis::Vertical => Point::new(x, snap(y)),
            TrackAxis::Both => Point::new(snap(x), snap(y)),
        };
        Some((drag.id, point))
    }

    /// Which tab in a bar sits under a pointer position, if any.
    pub fn tab_at(&self, bar: ComboId, position: Point<Pixels>) -> Option<usize> {
        (0..)
            .map_while(|slot| self.slot(bar, slot))
            .position(|bounds| bounds.contains(&position))
    }

    /// Updates a tab drag for a new pointer position, returning whether
    /// anything on screen should change: the held tab rides with the pointer,
    /// so every move during a drag is a redraw, and some moves also change
    /// the landing slot.
    ///
    /// Nothing happens until the pointer is [`TAB_DRAG_THRESHOLD`] from
    /// where it pressed: a click that wobbles a pixel stays a click. Past
    /// that, a tab moves only once the pointer is past the midpoint of its
    /// neighbour, so a tab never swaps back and forth under a still hand.
    /// It keeps going while the pointer is past the next neighbour too, so
    /// a fling that clears several tabs in one event lands where the hand
    /// stopped rather than one slot along.
    pub fn drag_tab_to(&mut self, position: Point<Pixels>) -> bool {
        if self.tab_drag_invalidated.get() {
            return false;
        }
        let Some(Drag::Tab(drag)) = &self.drag else {
            return false;
        };
        let axis = drag.axis;
        let pointer = match axis {
            TrackAxis::Vertical => f32::from(position.y),
            _ => f32::from(position.x),
        };
        if !drag.moved && (pointer - drag.pressed).abs() < TAB_DRAG_THRESHOLD {
            return false;
        }
        let (bar, mut to) = (drag.bar, drag.to);
        // Only walk one way: slots are last frame's geometry, and walking
        // back over ground just covered could otherwise loop for ever.
        let backwards = self
            .slot(bar, to)
            .is_some_and(|current| pointer < tab_start(current, axis));
        while let Some(current) = self.slot(bar, to) {
            let next = if backwards {
                if pointer >= tab_start(current, axis) || to == 0 {
                    break;
                }
                to - 1
            } else {
                if pointer <= tab_end(current, axis) {
                    break;
                }
                to + 1
            };
            let Some(neighbour) = self.slot(bar, next) else {
                break;
            };
            let centre = (tab_start(neighbour, axis) + tab_end(neighbour, axis)) / 2.0;
            let past = if backwards {
                pointer < centre
            } else {
                pointer > centre
            };
            if !past {
                break;
            }
            to = next;
        }
        if let Some(Drag::Tab(drag)) = &mut self.drag {
            drag.pointer = pointer;
            drag.moved = true;
            drag.to = to;
        }
        true
    }

    /// Whether any gesture owned by the controls is in flight. A host with
    /// drags of its own ORs this with them.
    pub fn dragging_anything(&self) -> bool {
        self.drag.is_some()
    }

    /// Ends the drag and clears the dismissal marker. Call it from the
    /// host's mouse-up, and from a mouse-move with no button held: a release
    /// outside the window never arrives, so without that a drag would still
    /// be running when the pointer comes back.
    ///
    /// Returns the finished tab reorder, if the gesture was one and it
    /// actually moved, so the host can apply it.
    pub fn end_drag(&mut self) -> Option<(ComboId, usize, usize)> {
        // A marker no toggle consumed (the click landed elsewhere) must not
        // eat some later toggle click.
        self.combo_dismissed = None;
        let invalidated = self.tab_drag_invalidated.replace(false);
        match self.drag.take()? {
            Drag::Tab(drag) if !invalidated && drag.moved && drag.from != drag.to => {
                Some((drag.bar, drag.from, drag.to))
            }
            _ => None,
        }
    }

    // ---- Context menus ----

    /// Opens a context menu at a pointer position. `target` is the host's
    /// own note of what was clicked; it comes back untouched.
    pub fn open_menu(
        &mut self,
        id: ComboId,
        anchor: Point<Pixels>,
        target: impl Into<SharedString>,
    ) {
        self.close_combo();
        self.menu = Some(OpenMenu {
            id,
            anchor,
            under: None,
            target: target.into(),
            opened_at: Instant::now(),
            highlight: None,
            levels: Vec::new(),
            level_changed_at: None,
        });
        self.menu_scroll(id).set_offset(Point::default());
    }

    /// Opens a menu hanging beneath an element rather than at the pointer,
    /// for a button that drops a menu.
    pub fn open_menu_under(
        &mut self,
        id: ComboId,
        under: Bounds<Pixels>,
        target: impl Into<SharedString>,
    ) {
        self.close_combo();
        self.menu = Some(OpenMenu {
            id,
            anchor: under.origin,
            under: Some(under),
            target: target.into(),
            opened_at: Instant::now(),
            highlight: None,
            levels: Vec::new(),
            level_changed_at: None,
        });
        self.menu_scroll(id).set_offset(Point::default());
    }

    // ---- Focus ----

    /// The focus handle for a composite control, made the first time it is
    /// asked for and the same one thereafter.
    ///
    /// Always a tab stop: a group is exactly one stop however many options
    /// it holds, which is why Tab passes a twenty-tab bar in one press.
    pub fn focus(&self, tag: impl Into<Tag>, cx: &App) -> FocusHandle {
        self.focus_handles
            .borrow_mut()
            .entry(tag.into())
            .or_insert_with(|| cx.focus_handle().tab_stop(true))
            .clone()
    }

    /// The root's focus handle: where the keyboard goes when it has nowhere
    /// else to be. [`crate::handle_keys`] puts it on the root element. Not a
    /// tab stop — it is one Tab from the first control, not a control.
    pub fn root_focus(&self, cx: &App) -> FocusHandle {
        self.focus_handles
            .borrow_mut()
            .entry(Tag::from(ROOT_FOCUS))
            .or_insert_with(|| cx.focus_handle())
            .clone()
    }

    /// Puts the keyboard on the root. For a host about to remove whatever
    /// has focus — switching pages, say: GPUI dispatches nothing from a
    /// handle that is no longer in the tree, not even the root's own
    /// shortcuts, so the keyboard has to be re-homed first.
    pub fn focus_root(&self, window: &mut Window, cx: &mut App) {
        window.focus(&self.root_focus(cx), cx);
    }

    /// Hands the keyboard back to `previous`, or to the root if there was
    /// no previous. Anywhere real: an overlay closing takes its focused
    /// element with it, and focus left on that goes nowhere.
    pub fn return_focus_to(
        &self,
        previous: Option<FocusHandle>,
        window: &mut Window,
        cx: &mut App,
    ) {
        match previous {
            Some(handle) => window.focus(&handle, cx),
            None => self.focus_root(window, cx),
        }
    }

    /// The focus handle of button `index` of dialog `id`. One handle per
    /// button, owned here rather than by the buttons, so the dialog can put
    /// the keyboard on its default action when it opens and Tab can find
    /// its way round them while it is up.
    pub fn dialog_button_focus(&self, id: ComboId, index: usize, cx: &App) -> FocusHandle {
        self.focus((id, "button", index), cx)
    }

    /// Records, or clears, the dialog a modal is keeping the keyboard inside
    /// of, and how many buttons it has. Called by
    /// [`crate::containers::dialog`] each frame.
    pub fn set_dialog_trap(&self, trap: Option<(ComboId, usize)>) {
        *self.dialog_trap.borrow_mut() = trap.map(|(id, count)| (id, (0..count).collect()));
    }

    pub(crate) fn set_dialog_buttons(&self, id: ComboId, indices: Vec<usize>) {
        *self.dialog_trap.borrow_mut() = Some((id, indices));
    }

    pub(crate) fn clear_dialog_trap(&self, id: ComboId) {
        let mut trap = self.dialog_trap.borrow_mut();
        if trap.as_ref().is_some_and(|(active, _)| *active == id) {
            *trap = None;
        }
    }

    /// Where Tab goes next while a modal dialog holds the keyboard, or `None`
    /// when none does.
    ///
    /// Skips disabled buttons and keeps focus on the panel when none are
    /// enabled. Focus elsewhere in an open modal returns to its first or
    /// last enabled button.
    pub fn trap_next(&self, window: &Window, backward: bool) -> Option<FocusHandle> {
        let trap = self.dialog_trap.borrow();
        let (id, indices) = trap.as_ref()?;
        if !self.is_dialog_open(id) {
            return None;
        }
        let handles = self.focus_handles.borrow();
        if indices.is_empty() {
            return handles.get(&Tag::new((*id, "panel"))).cloned();
        }
        let button = |index: usize| handles.get(&Tag::new((id, "button", index)));
        let here = indices
            .iter()
            .copied()
            .find(|&index| button(index).is_some_and(|handle| handle.is_focused(window)));
        button(next_dialog_button(indices, here, backward)?).cloned()
    }

    /// Hands the keyboard back to whatever had it before a dialog opened.
    ///
    /// The dialog's own buttons and scrim do this themselves. A host that
    /// closes the dialog some other way — Escape through its own action,
    /// say — calls it, or focus is left on a button that no longer exists
    /// and the next shortcut has nowhere to arrive.
    pub fn restore_dialog_focus(&mut self, window: &mut Window, cx: &mut App) {
        let previous = self.dialog_return_focus.take();
        self.return_focus_to(previous, window, cx);
    }

    /// The focus handle the open menu uses, creating it the first time a
    /// menu is opened in this view.
    pub fn menu_focus(&mut self, cx: &App) -> FocusHandle {
        self.menu_focus
            .get_or_insert_with(|| cx.focus_handle())
            .clone()
    }

    /// Returns the keyboard before removing a menu, unless its callback
    /// already moved focus somewhere else.
    pub fn restore_menu_focus(&mut self, window: &mut Window, cx: &mut App) {
        let previous = self.menu_return_focus.take();
        if self
            .menu_focus
            .as_ref()
            .is_some_and(|focus| focus.is_focused(window))
        {
            self.return_focus_to(previous, window, cx);
        }
    }

    /// Puts the keyboard on one row of the open menu.
    pub fn highlight_menu(&mut self, index: Option<usize>) {
        if let Some(menu) = self.menu.as_mut() {
            menu.highlight = index;
        }
    }

    /// Which row of the open menu the keyboard is on.
    pub fn menu_highlight(&self) -> Option<usize> {
        self.menu.as_ref().and_then(|menu| menu.highlight)
    }

    /// Enters an enabled submenu. The menu opening, target and focus handle
    /// stay put; only the visible level and its keyboard row change.
    ///
    /// A level entered from the keyboard starts on its first row, so the
    /// next Enter has something to act on. One entered with the pointer
    /// starts on nothing, as the root does: the hand is still over the row
    /// it clicked, and a wash on a row it never touched would be a guess.
    pub(crate) fn enter_menu(
        &mut self,
        index: usize,
        highlight: usize,
        identity: u64,
        by_keyboard: bool,
    ) {
        let Some(id) = self.menu.as_ref().map(|menu| menu.id) else {
            return;
        };
        let scroll = self.menu_scroll(id).offset();
        if let Some(menu) = self.menu.as_mut() {
            menu.levels.push(MenuBranch {
                index,
                highlight,
                identity,
                scroll,
            });
            menu.highlight = by_keyboard.then_some(0);
        }
        self.menu_level_changed(Point::default());
    }

    /// Returns to the parent level, restoring the row that opened it.
    /// Returns false when the menu is already at its root.
    pub(crate) fn back_menu(&mut self) -> bool {
        let Some(menu) = self.menu.as_mut() else {
            return false;
        };
        let Some(branch) = menu.levels.pop() else {
            return false;
        };
        menu.highlight = Some(branch.highlight);
        self.menu_level_changed(branch.scroll);
        true
    }

    /// The open menu's scroll handle. Keyed by the menu alone, not by the
    /// opening or the level, so a session of menus holds one handle rather
    /// than one per level ever shown; each change of level sets its offset
    /// instead.
    pub(crate) fn menu_scroll(&self, id: ComboId) -> ScrollHandle {
        self.scroll((id, "menu-scroll"))
    }

    /// Notes that the open menu now shows another level, scrolled to
    /// `offset`.
    pub(crate) fn menu_level_changed(&mut self, offset: Point<Pixels>) {
        let Some(menu) = self.menu.as_mut() else {
            return;
        };
        menu.level_changed_at = Some(Instant::now());
        let id = menu.id;
        self.menu_scroll(id).set_offset(offset);
    }

    /// Whether a row click belongs to the opening `opened_at` and to the
    /// level now on screen. The second click of a double-click on a submenu
    /// row lands on whatever row of the new level slid under the pointer;
    /// until the level has finished arriving, a click is the tail of one
    /// aimed at the level before.
    pub(crate) fn menu_takes_click(&self, opened_at: Instant) -> bool {
        self.menu.as_ref().is_some_and(|menu| {
            menu.opened_at == opened_at
                && menu
                    .level_changed_at
                    .is_none_or(|changed| changed.elapsed() >= self.scaled(MOVE))
        })
    }

    /// Closes whatever is showing over the view: a pop-up list, a context
    /// menu.
    ///
    /// Call it when the keyboard commits to something else. A dropdown is a
    /// question the view is asking, and moving to another control answers it
    /// by walking away — leaving the list on screen makes it look as though
    /// the next key pressed will land in it.
    pub fn dismiss_popups(&mut self) {
        self.close_combo();
        self.close_menu();
    }

    /// What Escape does when nothing closer to the keyboard has taken it:
    /// closes a pop-up list, a menu or the command palette. Returns whether
    /// there was anything to close, so a host can pass an idle Escape on to
    /// its own overlays.
    pub fn dismiss_overlays(&mut self, window: &mut Window, cx: &mut App) -> bool {
        let had_popup = self
            .combo
            .as_ref()
            .is_some_and(|open| open.closing.is_none())
            || self.menu.is_some();
        self.dismiss_popups();
        self.restore_menu_focus(window, cx);
        if self.palette_overlay.is_some() {
            self.close_palette(window, cx);
            return true;
        }
        had_popup
    }

    pub fn close_menu(&mut self) {
        self.menu = None;
    }

    pub fn is_menu_open(&self, id: ComboId) -> bool {
        self.menu.as_ref().is_some_and(|menu| menu.id == id)
    }

    /// What the open menu was summoned on, if any.
    pub fn menu_target(&self) -> Option<&str> {
        self.menu.as_ref().map(|menu| menu.target.as_ref())
    }

    /// When the open menu was summoned, if any.
    ///
    /// This identifies one *opening* rather than one menu, which is what
    /// lets a caller tell "still the menu I was in" apart from "the callback
    /// I just ran opened another one".
    pub fn menu_opened_at(&self) -> Option<Instant> {
        self.menu.as_ref().map(|menu| menu.opened_at)
    }
}

/// A view that hosts vampir controls.
///
/// The two required methods hand out the view's [`ControlState`]. The rest
/// have defaults and matter only for the gestures that outlive a single
/// element: a drag continues long after the pointer has left the control
/// that started it, so the host has to pump it from wherever it tracks the
/// pointer at the root.
pub trait ControlHost: Sized + 'static {
    fn control_state(&self) -> &ControlState;
    fn control_state_mut(&mut self) -> &mut ControlState;

    /// A slider, split divider or colour pad moved. Both components of `at`
    /// run 0..=1 across the track and are already snapped to its stops;
    /// read `x` for a horizontal control, `y` for a vertical one, both for
    /// a pad.
    ///
    /// Fires on the press as well as on every move, so a click anywhere on
    /// a track jumps there. Implement it once and route on `id`.
    fn track_dragged(&mut self, _id: ComboId, _at: Point<f32>, _cx: &mut Context<Self>) {}

    /// A tab was dropped in a new slot. `from` and `to` are indices into the
    /// bar's items.
    fn tabs_reordered(&mut self, _bar: ComboId, _from: usize, _to: usize, _cx: &mut Context<Self>) {
    }

    /// Forwarded from a surface that blocks the mouse, such as a scrollbar
    /// track. While the pointer is over one, the host sees neither moves nor
    /// releases, so the surface passes them through here. The default pumps
    /// the toolkit's own drags, which is all a host without drags of its
    /// own needs; a host tracking gestures of its own at the root routes
    /// these into the same place.
    fn forwarded_mouse_move(
        &mut self,
        event: &MouseMoveEvent,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if mouse_moved(self, event, cx) {
            cx.notify();
        }
    }

    /// Forwarded from a surface that blocks the mouse.
    fn forwarded_mouse_up(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
        end_drags(self, cx);
        cx.notify();
    }
}

/// What a pointer move at the root means to the controls' drags: with a
/// button held it continues them; with none it ends them, because a release
/// outside the window never arrives and a move with no button held is that
/// release. Returns whether anything on screen changed.
///
/// [`handle_mouse`] calls it from the root. A host with drags of its own at
/// the root calls it from its own mouse-move handler instead.
pub fn mouse_moved<V: ControlHost>(
    host: &mut V,
    event: &MouseMoveEvent,
    cx: &mut Context<V>,
) -> bool {
    if !event.dragging() {
        if host.control_state().dragging_anything() {
            end_drags(host, cx);
            return true;
        }
        return false;
    }
    continue_drags(host, event.position, cx)
}

/// Puts the mouse handlers the toolkit's drags need on the host's root: a
/// slider handle, a split divider, a tab or a scrollbar thumb is let go of
/// long after the pointer has left it, so the gesture has to be tracked
/// from the root. Put it on every `.occlude()`d surface too, because an
/// occluding panel swallows the move stream a drag underneath it depends
/// on. [`crate::root`] is this and [`crate::handle_keys`] together.
pub fn handle_mouse<E: InteractiveElement, V: ControlHost>(root: E, cx: &mut Context<V>) -> E {
    root.on_mouse_move(cx.listener(|this, event: &MouseMoveEvent, _window, cx| {
        if mouse_moved(this, event, cx) {
            cx.notify();
        }
    }))
    .on_mouse_up(
        MouseButton::Left,
        cx.listener(|this, _event, _window, cx| {
            end_drags(this, cx);
            cx.notify();
        }),
    )
}

/// Pumps the in-flight control drag from one pointer position. Call it
/// from the host's mouse-move tracking; it does nothing when nothing is
/// being dragged, so it is safe to call unconditionally.
///
/// Returns whether anything moved, so the host can skip a redraw.
pub fn continue_drags<V: ControlHost>(
    host: &mut V,
    position: Point<Pixels>,
    cx: &mut Context<V>,
) -> bool {
    if let Some(drag) = host.control_state().scroll_drag() {
        let along = match drag.axis {
            ScrollAxis::Vertical => f32::from(position.y),
            ScrollAxis::Horizontal => f32::from(position.x),
        };
        apply_scroll_drag(drag, along);
        return true;
    }
    if let Some((id, at)) = host.control_state().track_ratio_at(position) {
        update_track(host, id, at, cx);
        return true;
    }
    host.control_state_mut().drag_tab_to(position)
}

// Presses, keyboard nudges and continuing drags must all reach the same
// binding, including the toolkit-owned theme tracks.
pub(crate) fn update_track<V: ControlHost>(
    host: &mut V,
    id: ComboId,
    at: Point<f32>,
    cx: &mut Context<V>,
) {
    if !host.control_state_mut().theme.apply_track(id, at.x) {
        host.track_dragged(id, at, cx);
    }
}

/// Ends the in-flight control drag, applying a finished tab reorder. Call
/// it from the host's mouse-up, and from a mouse-move with no button held.
pub fn end_drags<V: ControlHost>(host: &mut V, cx: &mut Context<V>) {
    if let Some((bar, from, to)) = host.control_state_mut().end_drag() {
        host.tabs_reordered(bar, from, to, cx);
    }
}

#[cfg(test)]
mod tests {
    use super::{ControlState, Entry, ListChange, Palette, SCHEME_FADE, TabDrag, Tag, TrackAxis};
    use gpui::{Bounds, ElementId, Point, SharedString, px, size};

    fn at(x: f32, y: f32) -> Point<gpui::Pixels> {
        gpui::point(px(x), px(y))
    }

    fn rect(x: f32, y: f32, w: f32, h: f32) -> Bounds<gpui::Pixels> {
        Bounds::new(at(x, y), size(px(w), px(h)))
    }

    /// The whole point: a record is a few words, not a string and two
    /// timestamps. Sizes are what the refactor bought, so they are pinned.
    #[test]
    fn a_record_is_small() {
        assert_eq!(std::mem::size_of::<Tag>(), 8);
        assert!(
            std::mem::size_of::<Entry>() <= 40,
            "{}",
            std::mem::size_of::<Entry>()
        );
    }

    /// Tags are built from parts without allocating, and the parts matter
    /// in order and in kind.
    #[test]
    fn tags_are_hashes_of_their_parts() {
        assert_eq!(Tag::new(("tabs", "pill-x")), Tag::from(("tabs", "pill-x")));
        assert_ne!(Tag::new(("tabs", "pill-x")), Tag::new(("pill-x", "tabs")));
        assert_ne!(Tag::new(("ab", "c")), Tag::new(("a", "bc")));
        assert_ne!(
            Tag::new(("rows", "shown", 1usize)),
            Tag::new(("rows", "shown", 2usize))
        );
        // Text is text whichever type carries it: a row keyed by a
        // `SharedString` matches the same row keyed by a `&str`.
        let owned: SharedString = String::from("budget.csv").into();
        assert_eq!(
            Tag::new(("tree", "selected", &owned)),
            Tag::new(("tree", "selected", "budget.csv"))
        );
        assert_eq!(Tag::from(&owned), Tag::from("budget.csv"));
        assert_eq!(
            Tag::from(ElementId::Name("x".into())),
            Tag::from(&ElementId::Name("x".into()))
        );
        // Extending a tag is not the same as hashing it again.
        assert_ne!(Tag::new("a").with("b"), Tag::new(Tag::new("a")));
    }

    /// One tag can hold one record of every kind at once, because the kinds
    /// share a table but not a slot: a list is present, has a keyboard row
    /// and painted somewhere, all under its own id.
    #[test]
    fn kinds_do_not_clobber_each_other() {
        let mut state = ControlState::new();
        state.present("list");
        state.highlight_list("list", 3);
        state.record_bounds("list", rect(1.0, 2.0, 3.0, 4.0));
        state.tween("list", 5.0, super::MOVE);
        state.transition("list", 7, super::SWITCH_SLIDE);
        assert_eq!(state.list_position("list"), 3);
        assert_eq!(state.bounds("list"), Some(rect(1.0, 2.0, 3.0, 4.0)));
        assert_eq!(state.tween("list", 5.0, super::MOVE), 5.0);
        assert_eq!(state.transition("list", 7, super::SWITCH_SLIDE), 1.0);
    }

    /// The target is the whole reason a host opens a menu with one, and it
    /// lives and dies with the open menu. Anything that closes the menu
    /// before running the host's callback hands that callback nothing —
    /// which is exactly what `context_menu` used to do.
    #[test]
    fn a_menu_carries_its_target_until_it_closes() {
        let mut state = ControlState::new();
        assert_eq!(state.menu_target(), None);

        state.open_menu("row", at(10.0, 20.0), "budget.csv");
        assert_eq!(state.menu_target(), Some("budget.csv"));
        assert!(state.is_menu_open("row"));

        state.close_menu();
        assert_eq!(state.menu_target(), None);
        assert_eq!(state.menu_opened_at(), None);
    }

    #[test]
    fn submenu_navigation_keeps_the_opening_and_restores_its_parent_row() {
        let mut state = ControlState::new();
        state.open_menu("row", at(10.0, 20.0), "budget.csv");
        let opening = state.menu_opened_at();

        state.enter_menu(3, 1, 11, false);
        state.highlight_menu(Some(2));
        state.enter_menu(4, 2, 22, false);
        assert_eq!(state.menu_target(), Some("budget.csv"));
        assert_eq!(state.menu_opened_at(), opening);
        assert_eq!(state.menu_highlight(), None);

        assert!(state.back_menu());
        assert_eq!(state.menu_highlight(), Some(2));
        assert!(state.back_menu());
        assert_eq!(state.menu_highlight(), Some(1));
        assert!(!state.back_menu());
        assert_eq!(state.menu_target(), Some("budget.csv"));
    }

    #[test]
    fn a_level_entered_by_keyboard_starts_on_its_first_row() {
        let mut state = ControlState::new();
        state.open_menu("row", at(10.0, 20.0), "budget.csv");
        state.enter_menu(3, 1, 11, true);
        assert_eq!(state.menu_highlight(), Some(0));
        assert!(state.back_menu());
        state.enter_menu(3, 1, 11, false);
        assert_eq!(state.menu_highlight(), None);
    }

    /// Back is the way out of a level, so it returns to the parent as it
    /// was left: the same row lit and the same stretch scrolled into view,
    /// while the level that opened in its place starts at the top.
    #[test]
    fn back_restores_the_parent_scroll_and_a_new_level_starts_at_the_top() {
        let mut state = ControlState::new();
        state.open_menu("row", at(10.0, 20.0), "budget.csv");
        let scroll = state.menu_scroll("row");
        scroll.set_offset(at(0.0, -120.0));
        state.enter_menu(9, 4, 11, false);
        assert_eq!(scroll.offset(), Point::default());

        assert!(state.back_menu());
        assert_eq!(scroll.offset(), at(0.0, -120.0));
        assert_eq!(state.menu_highlight(), Some(4));
        // At the root there is nothing to go back to, and Back says so
        // rather than closing anything itself.
        assert!(!state.back_menu());
        assert!(state.is_menu_open("row"));

        state.open_menu("row", at(10.0, 20.0), "notes.md");
        assert_eq!(scroll.offset(), Point::default());
    }

    #[test]
    fn rows_take_no_clicks_until_their_level_has_arrived() {
        let mut state = ControlState::new();
        state.open_menu("row", at(10.0, 20.0), "budget.csv");
        let opening = state.menu_opened_at().expect("open");
        assert!(state.menu_takes_click(opening));

        state.enter_menu(3, 1, 11, false);
        assert!(!state.menu_takes_click(opening));
        state.set_time_scale(0.001);
        std::thread::sleep(std::time::Duration::from_millis(2));
        assert!(state.menu_takes_click(opening));

        state.open_menu("row", at(10.0, 20.0), "notes.md");
        assert!(!state.menu_takes_click(opening));
    }

    /// Reopening replaces the target rather than keeping the first one, so a
    /// second right-click on a different row aims at that row.
    #[test]
    fn reopening_a_menu_retargets_it() {
        let mut state = ControlState::new();
        state.open_menu("row", at(0.0, 0.0), "report.pdf");
        state.open_menu("row", at(40.0, 60.0), "budget.csv");
        assert_eq!(state.menu_target(), Some("budget.csv"));
    }

    /// A tween seen for the first time is already where it was asked to be:
    /// nothing slides in from nowhere on the first frame.
    #[test]
    fn a_new_tween_starts_at_its_target() {
        let state = ControlState::new();
        assert_eq!(state.tween("pill", 40.0, super::MOVE), 40.0);
        // And a new target sets off from where it is, not from the end.
        let moving = state.tween("pill", 100.0, super::MOVE);
        assert!((40.0..100.0).contains(&moving), "{moving}");
    }

    #[test]
    fn reversing_a_blend_starts_from_its_visible_value() {
        let mut state = ControlState::new();
        // Advance the clock instead of sleeping. A long duration keeps
        // scheduler delays negligible next to the motion being tested.
        let duration = std::time::Duration::from_secs(1000);
        assert_eq!(state.blend("switch", false, duration), 0.0);
        assert_eq!(state.blend("switch", true, duration), 0.0);
        state.epoch -= std::time::Duration::from_secs(300);
        let before = state.blend("switch", true, duration);
        assert!((0.6..0.7).contains(&before), "{before}");
        let reversing = state.blend("switch", false, duration);
        assert!(
            (reversing - before).abs() < 0.001,
            "{before} -> {reversing}"
        );
        state.epoch -= std::time::Duration::from_secs(300);
        let returning = state.blend("switch", false, duration);
        assert!(returning < reversing, "{returning} < {reversing}");
        let reversed_again = state.blend("switch", true, duration);
        assert!((reversed_again - returning).abs() < 0.001);
    }

    #[test]
    fn dialog_navigation_skips_disabled_buttons_and_wraps() {
        let enabled = [0, 3, 5];
        assert_eq!(super::next_dialog_button(&enabled, Some(0), false), Some(3));
        assert_eq!(super::next_dialog_button(&enabled, Some(5), false), Some(0));
        assert_eq!(super::next_dialog_button(&enabled, Some(0), true), Some(5));
        assert_eq!(super::next_dialog_button(&enabled, Some(3), true), Some(0));
        // A button can become disabled while it has focus; the next Tab
        // still has to land inside the dialog.
        assert_eq!(super::next_dialog_button(&enabled, Some(2), false), Some(0));
        assert_eq!(super::next_dialog_button(&enabled, None, true), Some(5));
        assert_eq!(super::next_dialog_button(&[], None, false), None);
    }

    #[test]
    fn an_inactive_dialog_cannot_clear_the_active_dialogs_trap() {
        let mut state = ControlState::new();
        state.open_dialog("active");
        state.set_dialog_buttons("active", vec![1, 3]);
        state.clear_dialog_trap("inactive");
        assert_eq!(*state.dialog_trap.borrow(), Some(("active", vec![1, 3])));
        state.close_dialog();
        assert_eq!(*state.dialog_trap.borrow(), None);

        state.set_dialog_trap(Some(("active", 4)));
        state.open_dialog("replacement");
        assert_eq!(*state.dialog_trap.borrow(), None);
    }

    #[test]
    fn invalid_or_extreme_time_scales_cannot_panic() {
        let mut state = ControlState::new();
        for scale in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 0.0, -1.0] {
            state.set_time_scale(scale);
            assert_eq!(state.time_scale(), None);
            assert_eq!(state.scaled(super::MOVE), super::MOVE);
        }
        state.set_time_scale(f32::MAX);
        assert_eq!(state.scaled(super::MOVE), std::time::Duration::MAX);
        assert_eq!(
            state.scaled(std::time::Duration::ZERO),
            std::time::Duration::ZERO
        );
    }

    /// `tween_from` is the exception, for rows joining a list: it starts at
    /// the given value and heads for the target.
    #[test]
    fn tween_from_starts_where_told() {
        let state = ControlState::new();
        let v = state.tween_from("row", 0.0, 1.0, super::MOVE);
        assert!(v < 0.5, "{v}");
        assert!(state.animating());
    }

    /// Angles go the short way round.
    #[test]
    fn angles_take_the_short_way() {
        let state = ControlState::new();
        state.tween_angle("hue", 350.0, super::MOVE);
        let v = state.tween_angle("hue", 10.0, super::MOVE);
        // Heading up through 360 rather than down through 180.
        assert!(v >= 350.0, "{v}");
    }

    /// A tween made and read in the same instant is at its target, not NaN:
    /// zero elapsed over a zero duration must not be a division.
    #[test]
    fn a_zero_length_tween_is_finished() {
        assert_eq!(super::progress_of(0, 0, 0), 1.0);
        let state = ControlState::new();
        for _ in 0..1000 {
            let v = state.tween("fresh", 3.0, super::MOVE);
            assert!(v.is_finite() && v == 3.0, "{v}");
            let s = state.snap("snapped", 4.0);
            assert_eq!(state.tween("snapped", 4.0, super::MOVE), s);
        }
    }

    /// A snapped tween has no motion left in it.
    #[test]
    fn snap_lands_at_once() {
        let state = ControlState::new();
        state.tween("thumb", 0.2, super::MOVE);
        assert_eq!(state.snap("thumb", 0.9), 0.9);
        assert_eq!(state.tween("thumb", 0.9, super::MOVE), 0.9);
        assert!(!state.animating());
    }

    /// A group of rows is "present" from its second frame on, and every row
    /// asking in the same frame gets the same answer.
    #[test]
    fn presence_is_per_frame_and_consistent() {
        let state = ControlState::new();
        assert!(!state.present("list"));
        assert!(!state.present("list"), "second row, same frame");
        state.animating(); // ends the frame
        assert!(state.present("list"));
        // Gone for a couple of frames, and it is new again.
        state.animating();
        state.animating();
        state.animating();
        assert!(!state.present("list"));
    }

    /// Records of things no longer rendered are retired, so a checkbox that
    /// leaves the screen and returns paints its state rather than sliding
    /// from whatever it last showed — and the table does not grow with
    /// everything that was ever on screen.
    #[test]
    fn stale_records_are_retired() {
        let state = ControlState::new();
        state.transition("check", 1, super::SWITCH_SLIDE);
        state.transition("check", 0, super::SWITCH_SLIDE);
        assert!(state.animating(), "a change started a slide");
        assert_eq!(state.records.borrow().len(), 1);
        state.animating();
        state.animating();
        assert_eq!(
            state.records.borrow().len(),
            0,
            "two frames unseen, and it is gone"
        );
        // First sight again: a different value starts no animation.
        let t = state.transition("check", 1, super::SWITCH_SLIDE);
        assert_eq!(t, 1.0);
    }

    #[test]
    fn retired_records_release_excess_capacity() {
        let state = ControlState::new();
        for slot in 0..1024 {
            state.snap(("rows", slot), 1.0);
        }
        assert!(state.records.borrow().capacity() >= 1024);
        for _ in 0..3 {
            state.snap(("rows", 0), 1.0);
            state.animating();
        }
        assert_eq!(state.records.borrow().len(), 1);
        assert!(state.records.borrow().capacity() <= 64);
    }

    /// Where things painted is a per-frame record like any other: a tab
    /// closed and gone from the bar takes its width with it, rather than
    /// leaving one behind for every tab the bar ever had.
    #[test]
    fn geometry_is_retired_but_measurements_are_kept() {
        let mut state = ControlState::new();
        state.record_slot("tabs", 0, rect(0.0, 0.0, 80.0, 26.0));
        state.record_bounds(("tabs", "tab", "notes.md"), rect(0.0, 0.0, 80.0, 26.0));
        state.measure(("section", "body"), rect(0.0, 0.0, 300.0, 120.0));
        assert_eq!(
            state.slot("tabs", 0).map(|b| f32::from(b.size.width)),
            Some(80.0)
        );
        assert_eq!(state.tab_at("tabs", at(10.0, 10.0)), Some(0));
        state.animating();
        state.animating();
        state.animating();
        assert_eq!(state.slot("tabs", 0), None);
        assert_eq!(state.bounds(("tabs", "tab", "notes.md")), None);
        assert_eq!(state.tab_at("tabs", at(10.0, 10.0)), None);
        assert_eq!(
            state
                .measured(("section", "body"))
                .map(|b| f32::from(b.size.height)),
            Some(120.0),
            "a disclosure grows to the height it had, however long it was shut"
        );
    }

    /// Slow motion stretches every duration, so a slide that would be over
    /// is still under way.
    ///
    /// The scale is huge on purpose: a millisecond becomes a quarter of an
    /// hour, so a test runner that stalls for a second between two lines,
    /// as a loaded CI machine does, still reads a slide that has barely
    /// begun.
    #[test]
    fn time_scale_stretches_every_duration() {
        let mut state = ControlState::new();
        state.set_time_scale(1_000_000.0);
        state.tween("x", 0.0, std::time::Duration::from_millis(1));
        state.tween("x", 1.0, std::time::Duration::from_millis(1));
        std::thread::sleep(std::time::Duration::from_millis(10));
        let v = state.tween("x", 1.0, std::time::Duration::from_millis(1));
        assert!(v < 0.2, "{v}");
        state.transition("flip", 0, std::time::Duration::from_millis(1));
        state.transition("flip", 1, std::time::Duration::from_millis(1));
        std::thread::sleep(std::time::Duration::from_millis(10));
        let t = state.transition("flip", 1, std::time::Duration::from_millis(1));
        assert!(t < 0.2, "{t}");
    }

    /// A dialog is open from the moment it is asked for, fades out rather
    /// than vanishing when closed, and is gone once the fade is over.
    #[test]
    fn a_dialog_fades_out_before_it_is_gone() {
        let mut state = ControlState::new();
        assert_eq!(state.dialog_fade("confirm"), None);
        state.open_dialog("confirm");
        assert!(state.is_dialog_open("confirm"));
        assert!(!state.is_dialog_open("other"));
        let (opacity, running) = state.dialog_fade("confirm").expect("on screen");
        assert!(running && opacity < 1.0, "{opacity}");
        state.close_dialog();
        assert!(!state.is_dialog_open("confirm"), "closing is not open");
        assert!(state.dialog_fade("confirm").is_some(), "still fading");
        assert!(state.animating());
        // A second close does not restart the exit.
        let first = state.dialog.as_ref().and_then(|d| d.closing);
        state.close_dialog();
        assert_eq!(state.dialog.as_ref().and_then(|d| d.closing), first);
        std::thread::sleep(crate::easing::MODAL_EXIT + std::time::Duration::from_millis(20));
        assert_eq!(state.dialog_fade("confirm"), None);
        assert!(!state.animating());
    }

    /// A pop-up list is the same shape as a dialog: open at once, fading
    /// out when closed, gone after the fade — and it keeps the row it opened
    /// on all the way out, because a pick has changed the value by then.
    #[test]
    fn a_combo_fades_out_on_the_row_it_opened_on() {
        let mut state = ControlState::new();
        assert_eq!(state.combo_fade("sort"), None);
        state.open_combo("sort", 2);
        assert!(state.is_combo_open("sort"));
        assert_eq!(
            state.combo_highlight_or(0),
            2,
            "the keyboard starts on the current option"
        );
        let (reveal, running) = state.combo_fade("sort").expect("on screen");
        assert!(running && reveal < 1.0, "{reveal}");
        assert!(state.animating());
        state.highlight_combo(4);
        assert_eq!(state.combo_highlight_or(0), 4);
        state.close_combo();
        assert!(!state.is_combo_open("sort"));
        assert_eq!(
            state.combo_opened_on("sort"),
            Some(2),
            "kept through the fade"
        );
        assert_eq!(
            state.combo_highlight_or(0),
            0,
            "the highlight went with the list"
        );
        assert!(state.combo_fade("sort").is_some(), "still fading");
        std::thread::sleep(super::COMBO_REVEAL + std::time::Duration::from_millis(20));
        assert_eq!(state.combo_fade("sort"), None);
        assert_eq!(state.combo_opened_on("sort"), None);
        assert!(!state.animating());
    }

    /// A press outside a list closes it and marks it, so the same press's
    /// click on the list's own button does not reopen it; the marker is
    /// used once and expires on its own.
    #[test]
    fn a_dismissal_is_taken_once() {
        let mut state = ControlState::new();
        state.open_combo("sort", 0);
        state.combo_pressed_outside("sort");
        assert!(!state.is_combo_open("sort"));
        assert_eq!(state.take_combo_dismissal(), Some("sort"));
        assert_eq!(state.take_combo_dismissal(), None, "taken");
        state.combo_pressed_outside("sort");
        state.end_drag();
        assert_eq!(state.take_combo_dismissal(), None, "a release clears it");
    }

    /// One pointer, one gesture: starting one drag ends another, and only a
    /// tab drag that actually moved comes back as a reorder.
    #[test]
    fn one_gesture_at_a_time() {
        let mut state = ControlState::new();
        state.begin_track_drag("volume", TrackAxis::Horizontal, None);
        assert!(state.is_dragging("volume") && state.track_dragging());
        state.begin_tab_drag(TabDrag {
            bar: "tabs",
            axis: TrackAxis::Horizontal,
            from: 0,
            to: 0,
            moved: false,
            grab: 0.0,
            pressed: 0.0,
            pointer: 0.0,
        });
        assert!(!state.track_dragging(), "the tab took over");
        assert!(state.dragging_anything());
        assert_eq!(
            state.end_drag(),
            None,
            "a press that never moved is a click"
        );
        assert!(!state.dragging_anything());

        state.record_slot("tabs", 0, rect(0.0, 0.0, 80.0, 26.0));
        state.record_slot("tabs", 1, rect(82.0, 0.0, 80.0, 26.0));
        state.begin_tab_drag(TabDrag {
            bar: "tabs",
            axis: TrackAxis::Horizontal,
            from: 0,
            to: 0,
            moved: false,
            grab: 10.0,
            pressed: 10.0,
            pointer: 10.0,
        });
        assert!(
            !state.drag_tab_to(at(11.0, 10.0)),
            "a one-pixel wobble is still a click"
        );
        assert_eq!(state.tab_drag().map(|drag| drag.moved), Some(false));
        assert!(
            state.drag_tab_to(at(40.0, 10.0)),
            "every move during a drag redraws"
        );
        assert_eq!(
            state.tab_drag().map(|drag| drag.to),
            Some(0),
            "not past the neighbour's middle"
        );
        state.drag_tab_to(at(130.0, 10.0));
        assert_eq!(state.tab_drag().map(|drag| drag.to), Some(1));
        assert_eq!(state.end_drag(), Some(("tabs", 0, 1)));
    }

    /// One move event that clears several tabs lands where the pointer is,
    /// not one slot along; and the walk back goes the same way.
    #[test]
    fn a_fling_lands_where_the_pointer_stops() {
        let mut state = ControlState::new();
        for slot in 0..4 {
            let left = slot as f32 * 82.0;
            state.record_slot("tabs", slot, rect(left, 0.0, 80.0, 26.0));
        }
        state.begin_tab_drag(TabDrag {
            bar: "tabs",
            axis: TrackAxis::Horizontal,
            from: 0,
            to: 0,
            moved: false,
            grab: 10.0,
            pressed: 10.0,
            pointer: 10.0,
        });
        // Past the middle of slot 3 (centre 286) in one go.
        state.drag_tab_to(at(300.0, 10.0));
        assert_eq!(state.tab_drag().map(|drag| drag.to), Some(3));
        // Back to just past the middle of slot 1 (centre 122).
        state.drag_tab_to(at(120.0, 10.0));
        assert_eq!(state.tab_drag().map(|drag| drag.to), Some(1));
        // Short of the middle of slot 0 (centre 40): stays.
        state.drag_tab_to(at(50.0, 10.0));
        assert_eq!(state.tab_drag().map(|drag| drag.to), Some(1));
        assert_eq!(state.end_drag(), Some(("tabs", 0, 1)));
    }

    #[test]
    fn a_vertical_tab_drag_uses_y_and_crosses_row_midpoints() {
        let mut state = ControlState::new();
        for slot in 0..3 {
            state.record_slot("sidebar", slot, rect(20.0, slot as f32 * 30.0, 160.0, 28.0));
        }
        state.begin_tab_drag(TabDrag {
            bar: "sidebar",
            axis: TrackAxis::Vertical,
            from: 0,
            to: 0,
            moved: false,
            grab: 10.0,
            pressed: 10.0,
            pointer: 10.0,
        });
        assert!(
            !state.drag_tab_to(at(140.0, 11.0)),
            "x motion does not drag a vertical tab"
        );
        state.drag_tab_to(at(140.0, 80.0));
        assert_eq!(state.tab_drag().map(|drag| drag.to), Some(2));
        state.drag_tab_to(at(140.0, 34.0));
        assert_eq!(state.tab_drag().map(|drag| drag.to), Some(1));
        assert_eq!(state.end_drag(), Some(("sidebar", 0, 1)));
    }

    #[test]
    fn changing_a_tab_bars_axis_cancels_its_pending_reorder() {
        let mut state = ControlState::new();
        state.begin_tab_drag(TabDrag {
            bar: "tabs",
            axis: TrackAxis::Horizontal,
            from: 0,
            to: 1,
            moved: true,
            grab: 10.0,
            pressed: 10.0,
            pointer: 90.0,
        });
        assert!(state.note_tab_axis("other", TrackAxis::Vertical));
        assert!(state.note_tab_axis("tabs", TrackAxis::Horizontal));
        assert!(state.tab_drag().is_some());
        assert!(!state.note_tab_axis("tabs", TrackAxis::Vertical));
        assert!(!state.note_tab_axis("tabs", TrackAxis::Horizontal));
        assert!(!state.drag_tab_to(at(200.0, 20.0)));
        assert_eq!(state.end_drag(), None);
        assert!(!state.dragging_anything());
    }

    #[test]
    fn selected_tab_reveal_waits_for_bounds_then_leaves_manual_scroll_alone() {
        let state = ControlState::new();
        assert!(state.tab_reveal_pending("tabs", ("first", false)));
        assert!(state.animating(), "pending bounds request another frame");
        assert!(state.tab_reveal_pending("tabs", ("first", false)));

        state.finish_tab_reveal("tabs", ("first", false));
        assert!(!state.tab_reveal_pending("tabs", ("first", false)));
        assert!(
            !state.animating(),
            "completed reveal stops asking for frames"
        );
        assert!(!state.tab_reveal_pending("tabs", ("first", false)));

        assert!(state.tab_reveal_pending("tabs", ("second", false)));
        state.finish_tab_reveal("tabs", ("first", false));
        assert!(state.tab_reveal_pending("tabs", ("second", false)));
        state.finish_tab_reveal("tabs", ("second", false));
        assert!(state.tab_reveal_pending("tabs", ("second", true)));
    }

    #[test]
    fn an_unpainted_tab_does_not_request_frames_forever() {
        let state = ControlState::new();
        for _ in 0..3 {
            assert!(state.tab_reveal_pending("tabs", ("hidden", false)));
            assert!(state.animating());
        }
        assert!(state.tab_reveal_pending("tabs", ("hidden", false)));
        assert!(!state.animating());
        // The request remains pending for a later render when the tab paints.
        assert!(state.tab_reveal_pending("tabs", ("hidden", false)));
    }

    /// A list's keyboard row survives frames while the query is the same,
    /// and goes back to the top when the query changes.
    #[test]
    fn a_new_query_puts_the_keyboard_back_on_the_best_match() {
        let state = ControlState::new();
        assert_eq!(state.list_highlight("results", "re"), 0);
        state.highlight_list("results", 2);
        assert_eq!(state.list_highlight("results", "re"), 2);
        assert_eq!(state.list_position("results"), 2);
        assert_eq!(state.list_highlight("results", "rep"), 0);
        // Another list is another keyboard.
        state.highlight_list("commands", 4);
        assert_eq!(state.list_highlight("results", "rep"), 0);
        assert_eq!(state.list_position("commands"), 4);
    }

    fn ids(names: &[&'static str]) -> Vec<SharedString> {
        names.iter().map(|name| SharedString::from(*name)).collect()
    }

    /// Arrowing to a result and then having a slow source land must leave
    /// Enter on the result that was arrowed to, not on whatever is now in
    /// its row.
    #[test]
    fn the_highlight_stays_on_its_result_as_sources_arrive() {
        let state = ControlState::new();
        let first = ids(&["tab", "bookmark", "file", "action"]);
        assert_eq!(
            state.follow_list("results", "des", &first).1,
            ListChange::Reset
        );
        state.highlight_list_item("results", 3, &first[3]);

        let appended = ids(&["tab", "bookmark", "file", "action", "remote"]);
        assert_eq!(
            state.follow_list("results", "des", &appended),
            (3, ListChange::Same)
        );

        let ranked_above = ids(&["tab", "history", "bookmark", "file", "action", "remote"]);
        assert_eq!(
            state.follow_list("results", "des", &ranked_above),
            (4, ListChange::Moved)
        );
        assert_eq!(
            state.follow_list("results", "des", &ranked_above),
            (4, ListChange::Same)
        );

        let gone = ids(&["tab", "history"]);
        assert_eq!(
            state.follow_list("results", "des", &gone),
            (0, ListChange::Reset)
        );
    }

    /// Until someone moves the highlight it belongs to the best match, so
    /// a better result arriving takes it, the way a launcher's top hit does.
    #[test]
    fn an_untouched_highlight_stays_on_the_first_row_as_results_arrive() {
        let state = ControlState::new();
        let first = ids(&["review", "system"]);
        state.follow_list("results", "design", &first);
        let better = ids(&["notes", "review", "system"]);
        assert_eq!(
            state.follow_list("results", "design", &better),
            (0, ListChange::Same)
        );
    }

    #[test]
    fn a_new_query_puts_a_followed_list_back_on_its_first_row() {
        let state = ControlState::new();
        let results = ids(&["tab", "bookmark", "file"]);
        state.follow_list("results", "de", &results);
        state.highlight_list_item("results", 2, &results[2]);
        assert_eq!(
            state.follow_list("results", "des", &results),
            (0, ListChange::Reset)
        );
        let better = ids(&["notes", "tab", "bookmark", "file"]);
        assert_eq!(
            state.follow_list("results", "des", &better),
            (0, ListChange::Same)
        );
    }

    /// A host moving the highlight by row alone has it followed from the
    /// next time the list is drawn.
    #[test]
    fn a_row_set_by_the_host_is_followed_once_drawn() {
        let state = ControlState::new();
        let results = ids(&["tab", "bookmark", "file"]);
        state.follow_list("results", "", &results);
        state.highlight_list("results", 1);
        assert_eq!(
            state.follow_list("results", "", &results),
            (1, ListChange::Same)
        );
        let reordered = ids(&["bookmark", "tab", "file"]);
        assert_eq!(
            state.follow_list("results", "", &reordered),
            (0, ListChange::Moved)
        );
    }

    /// The theme's palette follows the truth rather than jumping to it,
    /// and stands still once it has arrived.
    #[test]
    fn the_palette_crosses_over_rather_than_cutting() {
        let mut state = ControlState::new();
        let light = state.palette();
        assert!(!light.is_dark);
        state.theme.scheme = crate::theme::Scheme::Dark;
        // The frame that notices the change paints the old scheme: the
        // crossing has begun, but no time has passed yet.
        assert_eq!(state.palette(), light);
        assert!(state.animating(), "the scheme is crossing over");
        std::thread::sleep(std::time::Duration::from_millis(20));
        let crossing = state.palette();
        assert_ne!(crossing.backdrop, light.backdrop);
        assert_ne!(
            crossing.backdrop,
            Palette::from_hue(state.theme.hue, true).backdrop
        );
        std::thread::sleep(SCHEME_FADE + std::time::Duration::from_millis(20));
        assert_eq!(state.palette(), Palette::from_hue(state.theme.hue, true));
    }

    /// `menu_opened_at` identifies one opening, which is how an item can
    /// close the menu it was in without closing one its callback opened.
    #[test]
    fn an_opening_is_identifiable_while_it_lasts() {
        let mut state = ControlState::new();
        state.open_menu("row", at(0.0, 0.0), "report.pdf");
        let opening = state.menu_opened_at().expect("a menu is open");
        assert_eq!(state.menu_opened_at(), Some(opening));

        state.close_menu();
        state.open_menu("row", at(0.0, 0.0), "report.pdf");
        assert!(state.menu_opened_at().is_some());
    }
}