lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
//! A table: rows of the same things in columns, which line up whatever is in a cell.

use std::{
  cell::Cell,
  cmp::Ordering,
  collections::{HashMap, HashSet},
  hash::Hash,
  marker::PhantomData,
  sync::Arc,
};

use parking_lot::Mutex;

use super::{
  checkbox::Checkbox,
  empty_state::{EmptyState, EmptyStateSize},
  icons,
  input::{Input, InputSize},
  menu::{Menu, MenuItem},
  names,
  number_input::NumberInput,
  part_style::{FontFamily, Length},
  recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
  select::Select,
  shared::{fading, glyph, pressable},
  skeleton::Skeleton,
  theme::{Themed, current_theme},
};
mod columns;
mod export;
mod filtering;
mod filters;
mod groups;
mod heights;
mod keys;
mod range;
mod search;
mod source;
mod summary;
mod text;

pub use columns::{ColumnLayout, ColumnMenuLabels, ColumnPlace};
use columns::{ColumnMenuLink, DropSide, MenuChoice};
pub use export::{ExportFormat, ExportLabels, ExportScope, GridBarLabels, GridExport, to_csv, to_delimited, to_tsv};
pub use filtering::FilterLabels;
use filtering::{FilterLink, FilterShape, FilterState, MOST_VALUES};
pub use filters::{ColumnFilter, GridQuery, TableFilters, TextMatch};
use filters::{FilterBy, Test};
pub use groups::{GroupKey, TableGroup};
use groups::{Lines, Shown};
use heights::Heights;
use keys::{KeptIndex, RowKeys};
pub use range::{CellKey, CellRange};
use range::{RangeMark, Span, span_of};
pub use search::SearchMatches;
use search::{Found, SearchState};
pub use source::{GridPage, GridRequest, SourceRow};
use source::{RowSource, Slot};
use summary::{Said, Summary, SummaryKey, summarize, summary_content};
pub use summary::{SummaryKind, SummaryLabels, TableSummary};
pub use text::TableTextError;

use crate::{
  app::{
    component::Component,
    ctx::{Ctx, Placement},
    events::{
      CopyEvent, DragEvent, EditEvent, KeyboardEvent, MouseButton, MouseButtonMask, MouseEvent, PasteEvent, ScrollEvent,
    },
    runtime::{DOUBLE_CLICK_DISTANCE, DOUBLE_CLICK_INTERVAL},
    theme::{BorderSize, Density, IconSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
  },
  components::{self, Column, Row, ScrollBoth},
  core::{ElementRef, Signal, SignalValue},
  layout::{
    Alignment,
    column_tracks::{ColumnTracks, TrackSpec, TrackWidth},
    layout_kind::{Justify, ScrollState, Sticky},
    text_style::FontWeight,
  },
  node::{
    BackgroundColor, CursorIcon, Element, FocusGroup, FocusRows, border::Border as Line, dimension::Dimension,
    outline::Outline,
  },
};

/// The parts of a table a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TablePart {
  /// The box: its edge, its corners, its fill. It clips what scrolls in it.
  Root,
  /// What scrolls, both ways: the header and the rows.
  Scroll,
  /// The header row: its height, its fill, the line under it, its padding and the room between columns.
  Header,
  /// A column's header: its type and its color. A sortable one is pressed, and colored under the pointer.
  HeaderCell,
  /// The arrow of the column the rows are sorted by, and the mark of one that sorts.
  SortIcon,
  /// The place of a column in the sort after its arrow, where the rows are sorted by more than one.
  SortOrder,
  /// The mark of a column the rows are filtered by, after its title.
  FilterIcon,
  /// The row of filters under the header, where a grid has one.
  FilterRow,
  /// The word over each editor of a column's filter panel.
  FilterLabel,
  /// What is dragged at the end of a column's header to make it wider or narrower: its width, its cursor.
  Resizer,
  /// The line in the resizer, shown under the pointer and while it is dragged.
  ResizeLine,
  /// What opens a column's menu in a data grid's header: shown under the pointer, with the focus and while the
  /// menu is open.
  MenuTrigger,
  /// Where a column dragged by its header goes: a line at the start of a header, or the end of the last.
  DropLine,
  /// A group's row, where the rows are grouped by a column's value: what is pressed to open and close it.
  GroupRow,
  /// The chevron of a group's row, which points down while it is open.
  Chevron,
  /// The value of a group's row.
  GroupLabel,
  /// How many rows a group holds.
  GroupCount,
  /// A summary row: at the bottom of the grid, where it stays, and at the end of each group open.
  Summary,
  /// What a row shows under it while it is open, as wide as the view.
  Detail,
  /// What opens and closes a row of a tree, or its detail, at the start of its first cell: the chevron's room.
  TreeToggle,
  /// The cell of a row's number, in the column of numbers at the start that stays, and the header's: pressed to
  /// select the row, or all of them.
  RowNumber,
  /// What a summary cell says its value is: "Sum", "Average".
  SummaryLabel,
  /// A summary cell's value.
  SummaryValue,
  /// One row: its height, its padding, the room between columns, the line under it. What is pressed,
  /// selected and focused.
  Row,
  /// A cell of a row: its type and its color.
  Cell,
  /// The cell of the column of checkboxes, in the header and in each row.
  Select,
  /// What holds the control a cell of a data grid is edited with, while it is.
  Editor,
  /// What stands under the header where there are no rows.
  Empty,
  /// The footer under the rows: the count of them and the pagination.
  Footer,
  /// What the footer says of the rows: "24 rows", "1 of 24 rows selected".
  Count,
}

names!(TablePart {
  Root => "root",
  Scroll => "scroll",
  Header => "header",
  HeaderCell => "header_cell",
  SortIcon => "sort_icon",
  SortOrder => "sort_order",
  FilterIcon => "filter_icon",
  FilterRow => "filter_row",
  FilterLabel => "filter_label",
  Resizer => "resizer",
  ResizeLine => "resize_line",
  MenuTrigger => "menu_trigger",
  DropLine => "drop_line",
  GroupRow => "group_row",
  Chevron => "chevron",
  GroupLabel => "group_label",
  GroupCount => "group_count",
  Summary => "summary",
  Detail => "detail",
  TreeToggle => "tree_toggle",
  RowNumber => "row_number",
  SummaryLabel => "summary_label",
  SummaryValue => "summary_value",
  Row => "row",
  Cell => "cell",
  Select => "select",
  Editor => "editor",
  Empty => "empty",
  Footer => "footer",
  Count => "count",
});

/// The states of a table, its rows and its cells a recipe styles, besides what the pointer and the
/// keyboard do to them.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TableState {
  /// Its rows are selected: it has a column of checkboxes.
  Selectable,
  /// A row is selected.
  Selected,
  /// The last row, with nothing under it in the box: it has no line under it.
  Last,
  /// A header that sorts the rows when it is pressed.
  Sortable,
  /// The header of the column the rows are sorted by.
  Sorted,
  /// A cell of a column whose text is in the muted color: a date, a note.
  Muted,
  /// A cell of a column whose text is in the mono type: a path, an id.
  Mono,
  /// A cell of a column of numbers: at its end, in the mono type, smaller and muted.
  Numeric,
  /// A cell or a header of a column that stays while the rows scroll sideways ([`ColumnPin`]).
  Pinned,
  /// It has a footer under its rows.
  Footer,
  /// It takes no input.
  Disabled,
  /// A cell of a data grid in the range of cells selected, but the one the focus is on: the range's fill.
  InRange,
  /// A cell at the top of the range of cells selected: the range's edge along its top.
  RangeTop,
  /// A cell at the bottom of the range: the edge along its bottom.
  RangeBottom,
  /// A cell at the start of the range: the edge along its start.
  RangeStart,
  /// A cell at the end of the range: the edge along its end.
  RangeEnd,
  /// A header whose column's menu is open.
  Open,
  /// A header a column dragged by its header goes before.
  DropBefore,
  /// The last header, where a column dragged by its header goes after it.
  DropAfter,
}

names!(TableState {
  Selectable => "selectable",
  Selected => "selected",
  Last => "last",
  Sortable => "sortable",
  Sorted => "sorted",
  Muted => "muted",
  Mono => "mono",
  Numeric => "numeric",
  Pinned => "pinned",
  Footer => "footer",
  Disabled => "disabled",
  InRange => "in_range",
  RangeTop => "range_top",
  RangeBottom => "range_bottom",
  RangeStart => "range_start",
  RangeEnd => "range_end",
  Open => "open",
  DropBefore => "drop_before",
  DropAfter => "drop_after",
});

/// What builds a table's nodes: a theme's in place of the default one.
pub type TableRenderer = Arc<dyn Fn(TableView) -> Element + Send + Sync>;

/// Which way the rows are sorted by a column.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SortDirection {
  /// The least first: A to Z, 1 to 9, the oldest first.
  Ascending,
  /// The most first.
  Descending,
}

/// A column the rows of a table are sorted by, and which way. The rows are sorted by a list of them: by the first,
/// rows equal in it by the second, and so on.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TableSort {
  /// The id of the column.
  pub column: Arc<str>,
  pub direction: SortDirection,
}

impl crate::app::component::DevtoolsInspectable for TableSort {}

impl TableSort {
  pub fn ascending(column: &str) -> Self {
    Self {
      column: Arc::from(column),
      direction: SortDirection::Ascending,
    }
  }

  pub fn descending(column: &str) -> Self {
    Self {
      column: Arc::from(column),
      direction: SortDirection::Descending,
    }
  }

  /// What a press of the header of `column` makes of the sort `current`. A plain press sorts by that column alone:
  /// ascending, then descending, then not at all. With Shift (`adding`) the column is added to the sort after the
  /// others, ascending, or, where it is in it, turned to descending in its place, then taken out.
  pub fn after_press(current: &[TableSort], column: &str, adding: bool) -> Vec<TableSort> {
    let held = current.iter().position(|sort| &*sort.column == column);
    if !adding {
      return match held.map(|at| current[at].direction) {
        Some(SortDirection::Ascending) => vec![Self::descending(column)],
        Some(SortDirection::Descending) => Vec::new(),
        None => vec![Self::ascending(column)],
      };
    }
    let mut next = current.to_vec();
    match held {
      Some(at) if next[at].direction == SortDirection::Ascending => next[at].direction = SortDirection::Descending,
      Some(at) => {
        next.remove(at);
      }
      None => next.push(Self::ascending(column)),
    }
    next
  }

  /// Sorts `rows` by `sorts` of `columns`: by the first, the rows equal in it by the next, and so on, each by
  /// what its column compares rows with or its text (see [`sort`](Self::sort)). Rows equal in all of them keep
  /// their order.
  pub fn sort_rows<R: 'static>(sorts: &[TableSort], columns: &[TableColumn<R>], rows: &mut [R]) {
    let compares: Vec<(CompareFn<R>, SortDirection)> = sorts
      .iter()
      .filter_map(|sort| Some((sort.compare(columns)?, sort.direction)))
      .collect();
    if compares.is_empty() {
      return;
    }
    rows.sort_by(|a, b| {
      compares
        .iter()
        .map(|(compare, direction)| match direction {
          SortDirection::Ascending => compare(a, b),
          SortDirection::Descending => compare(b, a),
        })
        .find(|order| order.is_ne())
        .unwrap_or(Ordering::Equal)
    });
  }

  /// How its column compares two rows: by what it compares with, or its text.
  fn compare<R: 'static>(&self, columns: &[TableColumn<R>]) -> Option<CompareFn<R>> {
    let column = columns.iter().find(|column| column.id == self.column)?;
    match (&column.compare, &column.content) {
      (Some(compare), _) => Some(compare.clone()),
      (None, Content::Text(text)) => {
        let text = text.clone();
        Some(Arc::new(move |a: &R, b: &R| text(a).cmp(&text(b))))
      }
      (None, _) => None,
    }
  }

  /// Sorts `rows` by this column of `columns`, by what the column compares rows with
  /// ([`TableColumn::sort_by`], [`TableColumn::sort_key`]) or, for a column of text, by its text. Rows that
  /// compare equal keep their order. A column it does not find, or one with nothing to compare by, leaves
  /// them as they are.
  pub fn sort<R: 'static>(&self, columns: &[TableColumn<R>], rows: &mut [R]) {
    let Some(compare) = self.compare(columns) else {
      return;
    };
    match self.direction {
      SortDirection::Ascending => rows.sort_by(|a, b| compare(a, b)),
      SortDirection::Descending => rows.sort_by(|a, b| compare(b, a)),
    }
  }
}

/// Where a cell's content sits in its column.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ColumnAlign {
  #[default]
  Start,
  Center,
  /// At its end: a number, the actions of a row.
  End,
}

/// Where a column stays while the rows scroll sideways under the others.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ColumnPin {
  /// It scrolls with the rows.
  #[default]
  None,
  /// It stays at the start: a name, an id. The columns pinned there are the first ones, one after another.
  Start,
  /// It stays at the end: a row's actions, a total. The columns pinned there are the last ones.
  End,
}

type CellFn<R> = Arc<dyn Fn(&R) -> Element + Send + Sync>;
type TextFn<R> = Arc<dyn Fn(&R) -> String + Send + Sync>;
type CompareFn<R> = Arc<dyn Fn(&R, &R) -> Ordering + Send + Sync>;

/// What a column builds a row's cell from.
enum Content<R> {
  /// A text, in the type of the cell.
  Text(TextFn<R>),
  /// Whatever the app builds: a badge, an avatar and a name, buttons.
  Element(CellFn<R>),
  Nothing,
}

impl<R> Clone for Content<R> {
  fn clone(&self) -> Self {
    match self {
      Self::Text(text) => Self::Text(text.clone()),
      Self::Element(cell) => Self::Element(cell.clone()),
      Self::Nothing => Self::Nothing,
    }
  }
}

/// A column of a table: its header, its width, where its cells' content sits, and how a row's cell is
/// built from the row.
///
/// ```
/// use lurq::ui::{Badge, TableColumn};
///
/// struct Crate { name: String, status: &'static str, size_kb: u32 }
///
/// let name = TableColumn::new("name", "Name").text(|row: &Crate| row.name.clone()).mono(true).sortable(true);
/// let status = TableColumn::new("status", "Status").width(120.0).cell(|row: &Crate| Badge::new(row.status).into());
/// let size = TableColumn::new("size", "Size")
///   .width(80.0)
///   .numeric()
///   .text(|row: &Crate| format!("{} KB", row.size_kb))
///   .sort_key(|row: &Crate| row.size_kb);
/// ```
pub struct TableColumn<R> {
  id: Arc<str>,
  title: Arc<str>,
  title_hidden: bool,
  track: TrackSpec,
  align: ColumnAlign,
  sortable: bool,
  pin: ColumnPin,
  resizable: Option<bool>,
  controls: bool,
  kinds: Vec<Name>,
  content: Content<R>,
  compare: Option<CompareFn<R>>,
  editor: Option<CellEditor<R>>,
  summary: Option<Summary<R>>,
  wrap: bool,
  filter: Option<FilterBy<R>>,
}

impl<R> Clone for TableColumn<R> {
  fn clone(&self) -> Self {
    Self {
      id: self.id.clone(),
      title: self.title.clone(),
      title_hidden: self.title_hidden,
      track: self.track,
      align: self.align,
      sortable: self.sortable,
      pin: self.pin,
      resizable: self.resizable,
      controls: self.controls,
      kinds: self.kinds.clone(),
      content: self.content.clone(),
      compare: self.compare.clone(),
      editor: self.editor.clone(),
      summary: self.summary.clone(),
      wrap: self.wrap,
      filter: self.filter.clone(),
    }
  }
}

impl<R: 'static> TableColumn<R> {
  /// A column of `id`, under the header `title`, that takes a share of the room the others leave, and whose
  /// cells are empty until it is told what to build them from.
  pub fn new(id: &str, title: &str) -> Self {
    Self {
      id: Arc::from(id),
      title: Arc::from(title),
      title_hidden: false,
      track: TrackSpec::share(1.0),
      align: ColumnAlign::Start,
      sortable: false,
      pin: ColumnPin::None,
      resizable: None,
      controls: false,
      kinds: Vec::new(),
      content: Content::Nothing,
      compare: None,
      editor: None,
      summary: None,
      wrap: false,
      filter: None,
    }
  }

  /// A column of what a row's end holds that is pressed: buttons, a menu. Its header says nothing (it is
  /// called "Actions" to what reads the tree), it is as wide as what is in it, its cells sit at its end, and a
  /// press in them is theirs, not the row's.
  pub fn actions(id: &str, cell: impl Fn(&R) -> Element + Send + Sync + 'static) -> Self {
    let mut column = Self::new(id, &crate::ui::current_locale().data.actions_column)
      .fit()
      .align(ColumnAlign::End)
      .controls(true)
      .cell(cell);
    column.title_hidden = true;
    column
  }

  /// A row's cell is this text, in the cell's type: the common case.
  pub fn text(mut self, text: impl Fn(&R) -> String + Send + Sync + 'static) -> Self {
    self.content = Content::Text(Arc::new(text));
    self
  }

  /// A row's cell is what this builds: a badge, an avatar with a name, a progress.
  pub fn cell(mut self, cell: impl Fn(&R) -> Element + Send + Sync + 'static) -> Self {
    self.content = Content::Element(Arc::new(cell));
    self
  }

  /// The column is `px` wide.
  pub fn width(mut self, px: f32) -> Self {
    self.track.width = TrackWidth::Px(px.max(0.0));
    self
  }

  /// The column takes `share` of the room the others leave: two of 1 and 2 split it one to two. Where the
  /// table is as wide as its content, it is as wide as its widest cell.
  pub fn share(mut self, share: f32) -> Self {
    self.track.width = TrackWidth::Share(share.max(0.0));
    self
  }

  /// The column is as wide as the widest of its cells the table has shown, and its header.
  pub fn fit(mut self) -> Self {
    self.track.width = TrackWidth::Content;
    self
  }

  /// The least the column is: a share does not narrow past it, and the table scrolls sideways instead.
  pub fn min_width(mut self, px: f32) -> Self {
    self.track = self.track.min(px);
    self
  }

  /// The most the column is.
  pub fn max_width(mut self, px: f32) -> Self {
    self.track = self.track.max(px);
    self
  }

  /// Where the content of its cells and its header sits.
  pub fn align(mut self, align: ColumnAlign) -> Self {
    self.align = align;
    self
  }

  /// A column of numbers: at its end, in the mono type, smaller and muted, as the board draws a size.
  pub fn numeric(mut self) -> Self {
    self.align = ColumnAlign::End;
    self.kind(TableState::Numeric, true)
  }

  /// Whether its text wraps onto as many lines as it needs, its row growing to hold them: a note, a message. A row
  /// is never lower than the table's rows. A mounted table measures the rows it builds as they are laid out and
  /// keeps what is in view still as their heights become known; the rows it has not built are taken to be as high
  /// as a row.
  pub fn wrap(mut self, wrap: bool) -> Self {
    self.wrap = wrap;
    self
  }

  /// Whether the rows are filtered by its text: those whose text contains a text, starts with it or is it,
  /// whatever its case ([`ColumnFilter::Text`]), or those whose text is one of the values ticked among those the
  /// column has ([`ColumnFilter::Set`]).
  pub fn filter_text(mut self, filter: bool) -> Self {
    self.filter = filter.then_some(FilterBy::Text);
    self
  }

  /// Whether the rows are filtered by ticking the values of its text the column has, as a status is
  /// ([`ColumnFilter::Set`]); a filter of text matches them too.
  pub fn filter_set(mut self, filter: bool) -> Self {
    self.filter = filter.then_some(FilterBy::Set);
    self
  }

  /// The rows are filtered by ticking the values `value` gives them, for a column whose cell is an element (a
  /// badge): [`ColumnFilter::Set`].
  pub fn filter_values(mut self, value: impl Fn(&R) -> String + Send + Sync + 'static) -> Self {
    self.filter = Some(FilterBy::Values(Arc::new(value)));
    self
  }

  /// The rows are filtered by the number `value` gives them: a range, or a comparison ([`ColumnFilter::Number`]).
  pub fn filter_number(mut self, value: impl Fn(&R) -> f64 + Send + Sync + 'static) -> Self {
    self.filter = Some(FilterBy::Number(Arc::new(value)));
    self
  }

  /// The rows are filtered by the day `value` gives them, from one day to another ([`ColumnFilter::Date`]); a row
  /// with no day is let through by no filter of days.
  pub fn filter_date(mut self, value: impl Fn(&R) -> Option<crate::core::Date> + Send + Sync + 'static) -> Self {
    self.filter = Some(FilterBy::Date(Arc::new(value)));
    self
  }

  /// The rows are filtered by whether `value` holds for them ([`ColumnFilter::Bool`]).
  pub fn filter_bool(mut self, value: impl Fn(&R) -> bool + Send + Sync + 'static) -> Self {
    self.filter = Some(FilterBy::Bool(Arc::new(value)));
    self
  }

  /// Whether its text is in the mono type: a path, an id.
  pub fn mono(self, mono: bool) -> Self {
    self.kind(TableState::Mono, mono)
  }

  /// Whether its text is in the muted color: a date, a note.
  pub fn muted(self, muted: bool) -> Self {
    self.kind(TableState::Muted, muted)
  }

  /// Puts the column's cells in a state the theme's recipe names, or takes them out of it.
  pub fn kind(mut self, state: impl Into<Name>, on: bool) -> Self {
    let state = state.into();
    self.kinds.retain(|held| *held != state);
    if on {
      self.kinds.push(state);
    }
    self
  }

  /// Whether a press of its header sorts the rows by it: see [`Table::sort`].
  pub fn sortable(mut self, sortable: bool) -> Self {
    self.sortable = sortable;
    self
  }

  /// How two rows compare in this column, for [`TableSort::sort`]: the column is sortable.
  pub fn sort_by(mut self, compare: impl Fn(&R, &R) -> Ordering + Send + Sync + 'static) -> Self {
    self.compare = Some(Arc::new(compare));
    self.sortable = true;
    self
  }

  /// What a row is sorted by in this column, for [`TableSort::sort`]: the column is sortable.
  pub fn sort_key<O: Ord>(self, key: impl Fn(&R) -> O + Send + Sync + 'static) -> Self {
    self.sort_by(move |a, b| key(a).cmp(&key(b)))
  }

  /// Where the column stays while the rows scroll sideways under the others: at the start or at the end. The
  /// columns pinned at the start are the first ones, one after another, the column of checkboxes before them;
  /// those at the end are the last ones. A column pinned where others between it and its end are not scrolls
  /// with them. Pinned columns after the first are best given a width of their own: where each stays is reckoned
  /// from the widths of those before it.
  pub fn pin(mut self, pin: ColumnPin) -> Self {
    self.pin = pin;
    self
  }

  /// Whether a person makes the column wider or narrower, by dragging the end of its header or with Ctrl and
  /// Left or Right on its header: as the table says unless told ([`Table::resizable`]). Only a mounted table keeps
  /// the width it is made.
  pub fn resizable(mut self, resizable: bool) -> Self {
    self.resizable = Some(resizable);
    self
  }

  /// Whether its cells hold what is pressed, buttons, a menu, a switch: a press in them is theirs and does
  /// not select or press the row.
  pub fn controls(mut self, controls: bool) -> Self {
    self.controls = controls;
    self
  }

  /// How a [`DataGrid`](super::DataGrid) edits the column's cells in place: a text, a number, a choice or a box
  /// ticked. A table shows them and edits nothing. A column edited with a checkbox that builds no cell of its
  /// own shows the box.
  pub fn editor(mut self, editor: CellEditor<R>) -> Self {
    self.editor = Some(editor);
    self
  }

  /// What the column says in a [`DataGrid`](super::DataGrid)'s summary rows, of a column of numbers: the sum, the
  /// average, the count, the least or the most of the rows summed up, the grid's whole or a group's. Its numbers
  /// are the values its editor edits, or those its text starts with ("12 KB"). A count is of any column.
  pub fn summary(mut self, kind: SummaryKind) -> Self {
    self.summary = Some(Summary::Kind(kind));
    self
  }

  /// What the column says in a data grid's summary rows, as the app computes it from the rows summed up.
  pub fn summary_text(mut self, text: impl Fn(&[&R]) -> String + Send + Sync + 'static) -> Self {
    self.summary = Some(Summary::Text(Arc::new(text)));
    self
  }

  /// The id of the column, which a sort names.
  pub fn id(&self) -> &str {
    &self.id
  }

  /// What its header says.
  pub fn title(&self) -> &str {
    &self.title
  }
}

type Told<K> = Arc<dyn Fn(&K) + Send + Sync>;
type SortFn = Arc<dyn Fn(Vec<TableSort>) + Send + Sync>;
type SortHeard = Arc<dyn Fn(&[TableSort]) + Send + Sync>;
type SelectionHeard<K> = Arc<dyn Fn(&[K]) + Send + Sync>;
/// What asks for a range of cells to be the grid's, and what is told it.
type RangeWrite<K> = Arc<dyn Fn(Option<CellRange<K>>) + Send + Sync>;
type RangeHeard<K> = Arc<dyn Fn(Option<&CellRange<K>>) + Send + Sync>;
/// What asks for the columns to be arranged another way, and what is told it.
type LayoutWrite = Arc<dyn Fn(ColumnLayout) + Send + Sync>;
type LayoutHeard = Arc<dyn Fn(&ColumnLayout) + Send + Sync>;
/// What asks for other groups to be open, and what is told a group opened or closed.
type OpenWrite = Arc<dyn Fn(Vec<GroupKey>) + Send + Sync>;
type OpenHeard = Arc<dyn Fn(&GroupKey, bool) + Send + Sync>;
/// What says the count of the footer, given how many rows there are and how many are selected where they are.
type CountFn = Arc<dyn Fn(TableCount) -> String + Send + Sync>;
type SlotsLink = (
  Arc<dyn Fn(usize) -> Slot + Send + Sync>,
  Arc<dyn Fn(usize) + Send + Sync>,
);

/// What the footer's count of a table says it of: how many rows it shows, how many there are, and how many are
/// selected where it selects.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TableCount {
  /// The rows shown: those its filters let through, every row where it has none.
  pub shown: usize,
  /// The rows there are: the app's total where it gives one.
  pub total: usize,
  pub selected: Option<usize>,
}

type FiltersFn = Arc<dyn Fn(TableFilters) + Send + Sync>;
type FiltersHeard = Arc<dyn Fn(&TableFilters) + Send + Sync>;
/// What is told the width a column is made: its id, and the width.
type ResizeHeard = Arc<dyn Fn(&str, f32) + Send + Sync>;
type ResizeLabelFn = Arc<dyn Fn(&str) -> String + Send + Sync>;
/// The least a column is made by a person, where it says no least of its own.
const LEAST_COLUMN: f32 = 48.0;
/// How far Ctrl and Left or Right make a column narrower or wider, and with Shift too.
const RESIZE_STEP: f32 = 10.0;
const RESIZE_STEP_SHIFT: f32 = 50.0;

/// The words a table writes itself, each of which the app says otherwise.
#[derive(Clone)]
struct Words {
  /// The title of the empty state of a table with no rows.
  empty: Arc<str>,
  /// The name of the header's checkbox.
  select_all: Arc<str>,
  /// The name of a row's checkbox.
  select_row: Arc<str>,
  /// The footer's count, where the app says it.
  count: Option<CountFn>,
  /// The name of a column's resizer, given the column's title.
  resize: Option<ResizeLabelFn>,
  /// A grid's: the words of a column's menu, the name of its trigger given the column's title, the name of a
  /// group's row given its column's title, its value and its count, the words of the summary rows, and the mark of
  /// the numbers' header.
  menu: Arc<ColumnMenuLabels>,
  menu_trigger: Option<ResizeLabelFn>,
  group: Option<GroupLabelFn>,
  summary: Arc<SummaryLabels>,
  numbers: Arc<str>,
  filter: Arc<FilterLabels>,
  source: Arc<SourceLabels>,
  export: Arc<ExportLabels>,
  bar: Arc<GridBarLabels>,
}

/// The words of a grid over rows the app gives on demand: what a row whose range failed says, and its button.
#[derive(Clone, Debug, PartialEq)]
pub struct SourceLabels {
  pub failed: String,
  pub retry: String,
}

impl Default for SourceLabels {
  fn default() -> Self {
    let locale = crate::ui::current_locale();
    Self {
      failed: locale.data.rows_failed.to_string(),
      retry: locale.common.retry.to_string(),
    }
  }
}

type GroupLabelFn = Arc<dyn Fn(&str, &str, usize) -> String + Send + Sync>;

impl Default for Words {
  fn default() -> Self {
    let locale = crate::ui::current_locale();
    Self {
      empty: locale.data.no_rows.clone(),
      select_all: locale.common.select_all.clone(),
      select_row: locale.data.select_row.clone(),
      count: None,
      resize: None,
      menu: Arc::default(),
      menu_trigger: None,
      group: None,
      summary: Arc::default(),
      numbers: Arc::from("#"),
      filter: Arc::default(),
      source: Arc::default(),
      export: Arc::default(),
      bar: Arc::default(),
    }
  }
}
type KeyFn<R, K> = Arc<dyn Fn(usize, &R) -> K + Send + Sync>;
/// The key of the row a row is under in a tree, or `None` for a row at the top.
type ParentFn<R, K> = Arc<dyn Fn(&R) -> Option<K> + Send + Sync>;
type KeysWrite<K> = Arc<dyn Fn(Vec<K>) + Send + Sync>;
type KeyOpenHeard<K> = Arc<dyn Fn(&K, bool) + Send + Sync>;
type DetailFn<R> = Arc<dyn Fn(&R) -> Element + Send + Sync>;

/// Rows of the same things in columns: crates and their status, files and their sizes, people and their
/// roles. The columns of the header and of every row are the same widths whatever is in a cell.
///
/// ```
/// use lurq::{core::Signal, ui::{Table, TableColumn, TableSort}};
///
/// #[derive(Clone)]
/// struct Crate { name: String, status: String, updated: String }
///
/// # fn crates(rows: Vec<Crate>, sort: Signal<Vec<TableSort>>, selected: Signal<Vec<String>>) {
/// let columns = vec![
///   TableColumn::new("name", "Name").text(|row: &Crate| row.name.clone()).mono(true).sortable(true),
///   TableColumn::new("status", "Status").width(120.0).text(|row: &Crate| row.status.clone()),
///   TableColumn::new("updated", "Updated").width(120.0).muted(true).text(|row: &Crate| row.updated.clone()),
/// ];
/// // The app gives the rows in the order it wants: here, by the sort the header was pressed to.
/// let mut rows = rows;
/// TableSort::sort_rows(&sort.get(), &columns, &mut rows);
/// let table = Table::new(columns, rows)
///   .row_key(|row: &Crate| row.name.clone())
///   .label("Crates")
///   .sort(sort)
///   .selected(selected)
///   .height(320.0)
///   .on_row_activate(|name: &String| println!("open {name}"));
/// # }
/// ```
///
/// What the rows are, their order, what is selected and what they are sorted by are the app's. A press of a
/// sortable header asks for the rows sorted by it, ascending, then descending, then not at all, and with Shift
/// for its column added to the sort after the others ([`Table::sort`], [`Table::on_sort_change`]); the app gives
/// them in that order ([`TableSort::sort_rows`] sorts them by each column's text or what it compares by).
///
/// With a selection ([`Table::selected`]) the table has a column of checkboxes. A press of a row selects it
/// alone, with Ctrl (or Cmd) adds it or takes it out, and with Shift selects the rows from the last selected
/// one to it; a press of its checkbox adds it or takes it out, with Shift the range. The header's checkbox
/// selects every row the table shows, or none where all are, and is mixed where some are.
///
/// The header stays while the rows scroll in the table's own height ([`Table::height`]); a table wider than
/// its box scrolls sideways, its header with it. Mounted ([`Table::mount`]), a table builds the rows in view
/// and a few around them, so that thousands of rows cost about what a screen of them does; converted into an
/// element it builds them all.
///
/// The rows are one stop of Tab, at the first selected row or the first. Up and Down move through them,
/// Home and End to the first and the last, and in a mounted table PageUp and PageDown by what the table
/// shows. Space selects the focused row (adds it or takes it out), Enter activates it, as a double press
/// does ([`Table::on_row_activate`]), and Ctrl+A selects them all. Tab from a row goes on into what it holds
/// that takes the focus, its buttons, and then out of the table. The sortable headers are a stop of their
/// own before the rows, Left and Right between them.
pub struct Table<R, K = usize> {
  columns: Vec<TableColumn<R>>,
  rows: Arc<Vec<R>>,
  key_of: KeyFn<R, K>,
  sort: Vec<TableSort>,
  write_sort: Option<SortFn>,
  on_sort_change: Vec<SortHeard>,
  filters: TableFilters,
  write_filters: Option<FiltersFn>,
  on_filters_change: Vec<FiltersHeard>,
  filter_row: bool,
  search: Arc<str>,
  write_search: Option<Arc<dyn Fn(String) + Send + Sync>>,
  on_search_change: Vec<Arc<dyn Fn(&str) + Send + Sync>>,
  search_options: super::highlight::FindOptions,
  on_matches_change: Vec<Arc<dyn Fn(SearchMatches) + Send + Sync>>,
  find_bar: bool,
  find_bar_with: Option<Arc<dyn Fn(super::find_bar::FindBar) -> super::find_bar::FindBar + Send + Sync>>,
  /// Whether a grid's column menus write its rows out, and who is told the text.
  export: bool,
  on_export: Vec<Arc<dyn Fn(&GridExport) + Send + Sync>>,
  /// Whether a grid has the bar of its tools over it.
  toolbar: bool,
  source: Option<Arc<dyn RowSource<R>>>,
  on_query_change: Vec<Arc<dyn Fn(&GridQuery) + Send + Sync>>,
  /// Over a source, as a render finds them: where each row's range is, and what asks for a failed one again.
  slots: Option<SlotsLink>,
  selected: Vec<K>,
  select: Option<Arc<dyn Fn(Vec<K>) + Send + Sync>>,
  on_selection_change: Vec<SelectionHeard<K>>,
  words: Words,
  resizable: bool,
  on_column_resize: Vec<ResizeHeard>,
  on_column_resize_end: Vec<ResizeHeard>,
  /// The widths a person made the columns, by their ids: what the table's host keeps.
  resized: Arc<HashMap<Arc<str>, f32>>,
  /// Whether its cells take the focus and are edited: a data grid.
  grid: bool,
  read_only: bool,
  on_edit: Vec<EditHeard<K>>,
  /// The range of cells a grid has selected, what asks for another, and who is told.
  cell_range: Option<CellRange<K>>,
  write_cell_range: Option<RangeWrite<K>>,
  on_cell_selection_change: Vec<RangeHeard<K>>,
  /// How a grid's columns are arranged, what asks for another arrangement, and who is told.
  column_layout: Option<ColumnLayout>,
  write_column_layout: Option<LayoutWrite>,
  on_column_layout_change: Vec<LayoutHeard>,
  /// Whether a grid's headers have a menu.
  column_menu: bool,
  /// Every column of a mounted grid in its layout, shown or not, with its title: what its menus list, and what
  /// a change of the arrangement starts from.
  arranged: Option<Arc<(ColumnLayout, Vec<(Arc<str>, Arc<str>)>)>>,
  /// The columns the rows are grouped by, the groups open, what asks for others and who is told; the columns
  /// themselves, which may be hidden.
  group_by: Vec<Arc<str>>,
  open_groups: Vec<GroupKey>,
  write_open: Option<OpenWrite>,
  on_open_change: Vec<OpenHeard>,
  grouping: Vec<TableColumn<R>>,
  /// Where the rows are a tree: the row each is under, the rows open, what asks for others and who is told.
  tree_parent: Option<ParentFn<R, K>>,
  expanded: Vec<K>,
  write_expanded: Option<KeysWrite<K>>,
  on_expanded_change: Vec<KeyOpenHeard<K>>,
  /// What a row shows under it while it is open, the rows open, what asks for others and who is told.
  detail: Option<DetailFn<R>>,
  details_open: Vec<K>,
  write_details: Option<KeysWrite<K>>,
  on_detail_change: Vec<KeyOpenHeard<K>>,
  /// Whether a grid has a column of the rows' numbers at its start.
  row_numbers: bool,
  checkboxes: bool,
  on_row_click: Vec<Told<K>>,
  on_row_activate: Vec<Told<K>>,
  /// The row a mounted table is asked to bring into view, and which asking it is.
  reveal: Option<(K, u64)>,
  density: Option<Density>,
  height: Option<Length>,
  max_height: Option<Length>,
  empty: Option<Element>,
  loading: bool,
  loading_rows: usize,
  pagination: Option<Element>,
  count: bool,
  total: Option<usize>,
  label: Option<Arc<str>>,
  disabled: bool,
  states: Vec<Name>,
  rules: Recipe,
  forced: Option<Interaction>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl<R, K: Clone> Clone for Table<R, K> {
  fn clone(&self) -> Self {
    Self {
      columns: self.columns.clone(),
      rows: self.rows.clone(),
      key_of: self.key_of.clone(),
      sort: self.sort.clone(),
      write_sort: self.write_sort.clone(),
      on_sort_change: self.on_sort_change.clone(),
      filters: self.filters.clone(),
      write_filters: self.write_filters.clone(),
      on_filters_change: self.on_filters_change.clone(),
      filter_row: self.filter_row,
      search: self.search.clone(),
      write_search: self.write_search.clone(),
      on_search_change: self.on_search_change.clone(),
      search_options: self.search_options,
      on_matches_change: self.on_matches_change.clone(),
      find_bar: self.find_bar,
      find_bar_with: self.find_bar_with.clone(),
      export: self.export,
      on_export: self.on_export.clone(),
      toolbar: self.toolbar,
      source: self.source.clone(),
      on_query_change: self.on_query_change.clone(),
      slots: self.slots.clone(),
      selected: self.selected.clone(),
      select: self.select.clone(),
      on_selection_change: self.on_selection_change.clone(),
      words: self.words.clone(),
      resizable: self.resizable,
      on_column_resize: self.on_column_resize.clone(),
      on_column_resize_end: self.on_column_resize_end.clone(),
      resized: self.resized.clone(),
      grid: self.grid,
      read_only: self.read_only,
      on_edit: self.on_edit.clone(),
      cell_range: self.cell_range.clone(),
      write_cell_range: self.write_cell_range.clone(),
      on_cell_selection_change: self.on_cell_selection_change.clone(),
      column_layout: self.column_layout.clone(),
      write_column_layout: self.write_column_layout.clone(),
      on_column_layout_change: self.on_column_layout_change.clone(),
      column_menu: self.column_menu,
      arranged: self.arranged.clone(),
      group_by: self.group_by.clone(),
      open_groups: self.open_groups.clone(),
      write_open: self.write_open.clone(),
      on_open_change: self.on_open_change.clone(),
      tree_parent: self.tree_parent.clone(),
      expanded: self.expanded.clone(),
      write_expanded: self.write_expanded.clone(),
      on_expanded_change: self.on_expanded_change.clone(),
      detail: self.detail.clone(),
      details_open: self.details_open.clone(),
      write_details: self.write_details.clone(),
      on_detail_change: self.on_detail_change.clone(),
      grouping: self.grouping.clone(),
      row_numbers: self.row_numbers,
      checkboxes: self.checkboxes,
      on_row_click: self.on_row_click.clone(),
      on_row_activate: self.on_row_activate.clone(),
      reveal: self.reveal.clone(),
      density: self.density.clone(),
      height: self.height,
      max_height: self.max_height,
      empty: self.empty.clone(),
      loading: self.loading,
      loading_rows: self.loading_rows,
      pagination: self.pagination.clone(),
      count: self.count,
      total: self.total,
      label: self.label.clone(),
      disabled: self.disabled,
      states: self.states.clone(),
      rules: self.rules.clone(),
      forced: self.forced,
      id: self.id.clone(),
      key: self.key.clone(),
    }
  }
}

impl<R: Send + Sync + 'static> Table<R, usize> {
  /// A table of `rows` in `columns`, each row told apart by where it is until the app gives it a key
  /// ([`Table::row_key`]): none selected, and sorted by nothing.
  pub fn new(columns: impl IntoIterator<Item = TableColumn<R>>, rows: impl IntoIterator<Item = R>) -> Self {
    Self::new_shared(columns, Arc::new(rows.into_iter().collect()))
  }

  /// A table of rows the app keeps shared: a list of thousands given again at every render is neither copied
  /// nor built again.
  pub fn new_shared(columns: impl IntoIterator<Item = TableColumn<R>>, rows: Arc<Vec<R>>) -> Self {
    Self {
      columns: columns.into_iter().collect(),
      rows,
      key_of: Arc::new(|index, _| index),
      sort: Vec::new(),
      write_sort: None,
      filters: TableFilters::default(),
      write_filters: None,
      on_filters_change: Vec::new(),
      filter_row: false,
      on_sort_change: Vec::new(),
      search: Arc::from(""),
      write_search: None,
      on_search_change: Vec::new(),
      search_options: super::highlight::FindOptions::default(),
      on_matches_change: Vec::new(),
      find_bar: false,
      find_bar_with: None,
      export: true,
      on_export: Vec::new(),
      toolbar: false,
      source: None,
      on_query_change: Vec::new(),
      slots: None,
      selected: Vec::new(),
      select: None,
      on_selection_change: Vec::new(),
      words: Words::default(),
      resizable: false,
      on_column_resize: Vec::new(),
      on_column_resize_end: Vec::new(),
      resized: Arc::default(),
      grid: false,
      read_only: false,
      on_edit: Vec::new(),
      cell_range: None,
      write_cell_range: None,
      on_cell_selection_change: Vec::new(),
      column_layout: None,
      write_column_layout: None,
      on_column_layout_change: Vec::new(),
      column_menu: false,
      arranged: None,
      group_by: Vec::new(),
      open_groups: Vec::new(),
      write_open: None,
      on_open_change: Vec::new(),
      tree_parent: None,
      expanded: Vec::new(),
      write_expanded: None,
      on_expanded_change: Vec::new(),
      detail: None,
      details_open: Vec::new(),
      write_details: None,
      on_detail_change: Vec::new(),
      grouping: Vec::new(),
      row_numbers: false,
      checkboxes: true,
      on_row_click: Vec::new(),
      on_row_activate: Vec::new(),
      reveal: None,
      density: None,
      height: None,
      max_height: None,
      empty: None,
      loading: false,
      loading_rows: 4,
      pagination: None,
      count: false,
      total: None,
      label: None,
      disabled: false,
      states: Vec::new(),
      rules: Recipe::new(),
      forced: None,
      id: None,
      key: None,
    }
  }

  /// What tells each row apart: its id, which the selection and the handlers name it by, wherever it is
  /// sorted to. Given before the selection and the handlers, which name rows by it.
  pub fn row_key<K: Clone + Eq + Hash + Send + Sync + 'static>(
    self,
    key: impl Fn(&R) -> K + Send + Sync + 'static,
  ) -> Table<R, K> {
    Table {
      columns: self.columns,
      rows: self.rows,
      key_of: Arc::new(move |_, row| key(row)),
      sort: self.sort,
      write_sort: self.write_sort,
      on_sort_change: self.on_sort_change,
      filters: self.filters,
      write_filters: self.write_filters,
      on_filters_change: self.on_filters_change,
      filter_row: self.filter_row,
      search: self.search,
      write_search: self.write_search,
      on_search_change: self.on_search_change,
      search_options: self.search_options,
      on_matches_change: self.on_matches_change,
      find_bar: self.find_bar,
      find_bar_with: self.find_bar_with,
      export: self.export,
      on_export: self.on_export,
      toolbar: self.toolbar,
      source: self.source,
      on_query_change: self.on_query_change,
      slots: self.slots,
      selected: Vec::new(),
      select: None,
      on_selection_change: Vec::new(),
      words: self.words,
      resizable: self.resizable,
      on_column_resize: self.on_column_resize,
      on_column_resize_end: self.on_column_resize_end,
      resized: self.resized,
      grid: self.grid,
      read_only: self.read_only,
      on_edit: Vec::new(),
      cell_range: None,
      write_cell_range: None,
      on_cell_selection_change: Vec::new(),
      column_layout: self.column_layout,
      write_column_layout: self.write_column_layout,
      on_column_layout_change: self.on_column_layout_change,
      column_menu: self.column_menu,
      arranged: None,
      group_by: self.group_by,
      open_groups: self.open_groups,
      write_open: self.write_open,
      on_open_change: self.on_open_change,
      // What names rows by their keys starts again with the keys.
      tree_parent: None,
      expanded: Vec::new(),
      write_expanded: None,
      on_expanded_change: Vec::new(),
      detail: self.detail,
      details_open: Vec::new(),
      write_details: None,
      on_detail_change: Vec::new(),
      grouping: self.grouping,
      row_numbers: self.row_numbers,
      checkboxes: self.checkboxes,
      on_row_click: Vec::new(),
      on_row_activate: Vec::new(),
      reveal: None,
      density: self.density,
      height: self.height,
      max_height: self.max_height,
      empty: self.empty,
      loading: self.loading,
      loading_rows: self.loading_rows,
      pagination: self.pagination,
      count: self.count,
      total: self.total,
      label: self.label,
      disabled: self.disabled,
      states: self.states,
      rules: self.rules,
      forced: self.forced,
      id: self.id,
      key: self.key,
    }
  }
}

impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> Table<R, K> {
  /// What the rows are sorted by: the signal's list of columns, the first first, which a press of a sortable
  /// header sets to that column alone (ascending, descending, then none) and a press with Shift adds the column
  /// to or turns it in ([`TableSort::after_press`]). Where it holds more than one, each sorted header shows its
  /// place in it. The rows are given in that order by the app ([`TableSort::sort_rows`]).
  pub fn sort(mut self, sort: Signal<Vec<TableSort>>) -> Self
  where
    Vec<TableSort>: SignalValue,
  {
    self.sort = sort.get();
    self.write_sort = Some(Arc::new(move |next| sort.set(next)));
    self
  }

  /// What the rows are sorted by, as the app holds it: `on_change` is told the next at each press of a
  /// sortable header, and the table shows what the app gives it at the next render.
  pub fn sort_controlled(
    mut self,
    sort: Vec<TableSort>,
    on_change: impl Fn(Vec<TableSort>) + Send + Sync + 'static,
  ) -> Self {
    self.sort = sort;
    self.write_sort = Some(Arc::new(on_change));
    self
  }

  /// Called with what the rows are to be sorted by at each press of a sortable header, bound to a signal or
  /// not; a second handler is called after the first. Given alone, it makes the headers sortable, and the
  /// table shows the sort the app gives it with [`sort_controlled`](Self::sort_controlled).
  pub fn on_sort_change(mut self, handler: impl Fn(&[TableSort]) + Send + Sync + 'static) -> Self {
    self.on_sort_change.push(Arc::new(handler));
    self
  }

  /// What the rows are filtered by: the signal's filters, a filter for each column filtered, which the table
  /// applies to the rows it is given, showing those every filter lets through, in their order. A column says how
  /// it is filtered ([`TableColumn::filter_text`] and the others); a person sets one from the column's menu in a
  /// data grid. The count in the footer says how many of all are shown.
  pub fn filters(mut self, filters: Signal<TableFilters>) -> Self
  where
    TableFilters: SignalValue,
  {
    self.filters = filters.get();
    self.write_filters = Some(Arc::new(move |next| filters.set(next)));
    self
  }

  /// What the rows are filtered by, as the app holds it: `on_change` is told the next filters, and the table
  /// shows what the app gives it at the next render.
  pub fn filters_controlled(
    mut self,
    filters: TableFilters,
    on_change: impl Fn(TableFilters) + Send + Sync + 'static,
  ) -> Self {
    self.filters = filters;
    self.write_filters = Some(Arc::new(on_change));
    self
  }

  /// Called with the next filters each time a person changes them, bound to a signal or not; a second handler is
  /// called after the first.
  pub fn on_filters_change(mut self, handler: impl Fn(&TableFilters) + Send + Sync + 'static) -> Self {
    self.on_filters_change.push(Arc::new(handler));
    self
  }

  /// Whether a grid has a row of filters under its header: a filter in the cell of each column that filters (a
  /// box for a text, a number or a day, where `>10`, `<5` and `5..20` are ranges; a choice for values and for yes
  /// or no). It stays under the header while the rows scroll.
  pub fn filter_row(mut self, filter_row: bool) -> Self {
    self.filter_row = filter_row;
    self
  }

  /// What is searched for in the cells: the signal's query. Every match in the text of a cell of the rows shown
  /// is marked as a find marks it ([`Highlight`](super::Highlight)), the current one stronger; F3 and Shift+F3 in
  /// the grid, and the find bar's buttons, go to the next and the one before, across rows not built yet, each
  /// brought into view. A cell whose content is an element is not searched.
  pub fn search(mut self, query: Signal<String>) -> Self
  where
    String: SignalValue,
  {
    self.search = Arc::from(query.get().as_str());
    self.write_search = Some(Arc::new(move |next| query.set(next)));
    self
  }

  /// What is searched for, as the app holds it: `on_change` is told what the find bar has typed.
  pub fn search_controlled(mut self, query: &str, on_change: impl Fn(String) + Send + Sync + 'static) -> Self {
    self.search = Arc::from(query);
    self.write_search = Some(Arc::new(on_change));
    self
  }

  /// Called with what is searched for each time the find bar's query changes.
  pub fn on_search_change(mut self, handler: impl Fn(&str) + Send + Sync + 'static) -> Self {
    self.on_search_change.push(Arc::new(handler));
    self
  }

  /// How the query matches: the case of its letters, whole words, a regular expression. Ignoring the case,
  /// anywhere in a word, unless told; the find bar's toggles change it.
  pub fn search_options(mut self, options: super::highlight::FindOptions) -> Self {
    self.search_options = options;
    self
  }

  /// Called with how many matches the search has and which is the current one, each time that changes.
  pub fn on_matches_change(mut self, handler: impl Fn(SearchMatches) + Send + Sync + 'static) -> Self {
    self.on_matches_change.push(Arc::new(handler));
    self
  }

  /// Whether Ctrl+F (Cmd+F) in the grid opens a find bar over it, its query the grid's search, its count the
  /// matches, its arrows the next and the one before.
  pub fn find_bar(mut self, find_bar: bool) -> Self {
    self.find_bar = find_bar;
    self
  }

  /// What the grid's find bar is made of besides its query and its matches: its words, the toggles it shows.
  pub fn find_bar_with(
    mut self,
    with: impl Fn(super::find_bar::FindBar) -> super::find_bar::FindBar + Send + Sync + 'static,
  ) -> Self {
    self.find_bar_with = Some(Arc::new(with));
    self
  }

  /// Called with the sort, the filters and the search the rows are asked by, each time a person changes one, where
  /// the app gives the rows on demand ([`DataGrid::on_demand`](super::DataGrid::on_demand)).
  pub fn on_query_change(mut self, handler: impl Fn(&GridQuery) + Send + Sync + 'static) -> Self {
    self.on_query_change.push(Arc::new(handler));
    self
  }

  /// The words of rows the app gives on demand: what a range that failed says, and its button.
  pub fn source_labels(mut self, labels: SourceLabels) -> Self {
    self.words.source = Arc::new(labels);
    self
  }

  /// Whether a grid's column menus have Export, which writes the rows shown or selected out as comma-separated or
  /// tab-separated text: on unless told.
  pub fn export(mut self, export: bool) -> Self {
    self.export = export;
    self
  }

  /// Told the text of each export from a grid's menu ([`GridExport`]): the app saves it, or sends it. Without a
  /// handler the text goes on the clipboard.
  pub fn on_export(mut self, handler: impl Fn(&GridExport) + Send + Sync + 'static) -> Self {
    self.on_export.push(Arc::new(handler));
    self
  }

  /// The words of the export in a grid's menu.
  pub fn export_labels(mut self, labels: ExportLabels) -> Self {
    self.words.export = Arc::new(labels);
    self
  }

  pub(super) fn set_toolbar(mut self, toolbar: bool) -> Self {
    self.toolbar = toolbar;
    self
  }

  pub(super) fn set_toolbar_labels(mut self, labels: GridBarLabels) -> Self {
    self.words.bar = Arc::new(labels);
    self
  }

  /// Every word of filtering: the column menu's items, the filter panel's and the filter row's.
  pub fn filter_labels(mut self, labels: FilterLabels) -> Self {
    self.words.filter = Arc::new(labels);
    self
  }

  /// The values the column has, which its filter of values ticks: each once, in order, at most
  /// [`MOST_VALUES`]; kept in the host while the rows are the same.
  fn values_of(&self, host: &HostState, column: &TableColumn<R>) -> Arc<Vec<String>> {
    let seen = Arc::as_ptr(&self.rows) as *const () as usize;
    if let Some((rows, values)) = host.filters.values.lock().get(&column.id)
      && *rows == seen
    {
      return values.clone();
    }
    let value: Box<dyn Fn(&R) -> String> = match (&column.filter, &column.content) {
      (Some(FilterBy::Values(values)), _) => {
        let values = values.clone();
        Box::new(move |row| values(row))
      }
      (_, Content::Text(text)) => {
        let text = text.clone();
        Box::new(move |row| text(row))
      }
      _ => return Arc::default(),
    };
    let mut values: Vec<String> = self
      .rows
      .iter()
      .map(value)
      .collect::<std::collections::BTreeSet<_>>()
      .into_iter()
      .take(MOST_VALUES)
      .collect();
    values.shrink_to_fit();
    let values = Arc::new(values);
    host
      .filters
      .values
      .lock()
      .insert(column.id.clone(), (seen, values.clone()));
    values
  }

  /// Asks for `next` to be the filters and tells every handler: nothing where it is what they are.
  fn filter_changer(&self) -> Option<Arc<dyn Fn(TableFilters) + Send + Sync>> {
    if self.write_filters.is_none() && self.on_filters_change.is_empty() {
      return None;
    }
    let (now, write, heard) = (
      self.filters.clone(),
      self.write_filters.clone(),
      self.on_filters_change.clone(),
    );
    Some(Arc::new(move |next: TableFilters| {
      if next == now {
        return;
      }
      if let Some(write) = &write {
        write(next.clone());
      }
      for handler in &heard {
        handler(&next);
      }
    }))
  }

  /// The rows that are selected: those whose keys the signal has. The table has a column of checkboxes.
  /// The last key is where a range selected with Shift starts.
  pub fn selected(mut self, selected: Signal<Vec<K>>) -> Self
  where
    Vec<K>: SignalValue,
  {
    self.selected = selected.get();
    self.select = Some(Arc::new(move |next| selected.set(next)));
    self
  }

  /// The rows that are selected, as the app holds them: `on_change` is told what is to be selected after each
  /// press or key that changes it, and the table shows what the app gives it at the next render.
  pub fn selected_controlled(
    mut self,
    selected: impl IntoIterator<Item = K>,
    on_change: impl Fn(Vec<K>) + Send + Sync + 'static,
  ) -> Self {
    self.selected = selected.into_iter().collect();
    self.select = Some(Arc::new(on_change));
    self
  }

  /// Called with the whole new selection after each press or key that changes it, bound to a signal or not:
  /// not for a press that leaves it as it is, nor when the app sets the signal. A second handler is called
  /// after the first. It hears a table that is selected ([`selected`](Self::selected),
  /// [`selected_controlled`](Self::selected_controlled)); alone it selects nothing.
  pub fn on_selection_change(mut self, handler: impl Fn(&[K]) + Send + Sync + 'static) -> Self {
    self.on_selection_change.push(Arc::new(handler));
    self
  }

  /// Whether a table whose rows are selected has the column of checkboxes. It has unless told otherwise;
  /// without it a row is selected by a press of it, as the board's table shows one.
  pub fn checkboxes(mut self, checkboxes: bool) -> Self {
    self.checkboxes = checkboxes;
    self
  }

  /// Called with the row a press of it is on, whatever it selects.
  pub fn on_row_click(mut self, handler: impl Fn(&K) + Send + Sync + 'static) -> Self {
    self.on_row_click.push(Arc::new(handler));
    self
  }

  /// Called with the row a double press or Enter activates: open what it is.
  pub fn on_row_activate(mut self, handler: impl Fn(&K) + Send + Sync + 'static) -> Self {
    self.on_row_activate.push(Arc::new(handler));
    self
  }

  /// Brings the row of `key` into view, once for each new `request`: a mounted table scrolls the least that
  /// shows it, as it does for a row the keys go to, and makes it the row Tab stops on. For a row the app selects
  /// from outside the table (a thing picked elsewhere): the app counts its askings, and asking again with the
  /// same number does nothing, so a person who scrolls away is not brought back at the next render.
  pub fn reveal(mut self, key: K, request: u64) -> Self {
    self.reveal = Some((key, request));
    self
  }

  /// How much room a row takes, whatever the theme is at: tight for much to show, loose for a finger.
  pub fn density(mut self, density: impl Into<Density>) -> Self {
    self.density = Some(density.into());
    self
  }

  /// The table's height: the header stays and the rows scroll in what is left under it.
  pub fn height(mut self, height: impl Into<Length>) -> Self {
    self.height = Some(height.into());
    self
  }

  /// The most the table is high: as high as its rows up to it, and they scroll past it.
  pub fn max_height(mut self, height: impl Into<Length>) -> Self {
    self.max_height = Some(height.into());
    self
  }

  /// The title of the empty state that stands under the header where there are no rows: "No rows" unless
  /// told. [`empty`](Self::empty) puts an element of the app's in its place.
  pub fn empty_text(mut self, text: &str) -> Self {
    self.words.empty = Arc::from(text);
    self
  }

  /// The name of the header's checkbox, which selects every row: "Select all" unless told.
  pub fn select_all_label(mut self, label: &str) -> Self {
    self.words.select_all = Arc::from(label);
    self
  }

  /// The name of a row's checkbox: "Select row" unless told.
  pub fn select_row_label(mut self, label: &str) -> Self {
    self.words.select_row = Arc::from(label);
    self
  }

  /// What the footer's count says, given how many rows it shows, how many there are and, where they are
  /// selected, how many are ([`TableCount`]): "24 rows", "3 of 24 rows selected", and filtered "12 of 340 rows"
  /// unless told.
  pub fn count_text(mut self, text: impl Fn(TableCount) -> String + Send + Sync + 'static) -> Self {
    self.words.count = Some(Arc::new(text));
    self
  }

  /// Whether a person makes the columns wider or narrower, each that says nothing of its own
  /// ([`TableColumn::resizable`]): by dragging the end of a column's header, or with Ctrl and Left or Right on it
  /// (with Shift, by more). Off unless told. A mounted table keeps the widths it is made; one converted into an
  /// element has no resizers.
  pub fn resizable(mut self, resizable: bool) -> Self {
    self.resizable = resizable;
    self
  }

  /// Called with a column's id and its width as a person makes it wider or narrower, at each move; a second
  /// handler is called after the first.
  pub fn on_column_resize(mut self, handler: impl Fn(&str, f32) + Send + Sync + 'static) -> Self {
    self.on_column_resize.push(Arc::new(handler));
    self
  }

  /// Called with a column's id and its width when a person has made it wider or narrower: the end of a drag
  /// that changed it, or a key.
  pub fn on_column_resize_end(mut self, handler: impl Fn(&str, f32) + Send + Sync + 'static) -> Self {
    self.on_column_resize_end.push(Arc::new(handler));
    self
  }

  /// The name of a column's resizer to what reads the tree, given the column's title: "Resize Name" unless told.
  pub fn resize_label(mut self, label: impl Fn(&str) -> String + Send + Sync + 'static) -> Self {
    self.words.resize = Some(Arc::new(label));
    self
  }

  /// What stands under the header where there are no rows: by default an empty state that says so.
  pub fn empty(mut self, empty: impl Into<Element>) -> Self {
    self.empty = Some(empty.into());
    self
  }

  /// Whether the rows are on their way: the header stays, and rows of skeletons stand where they will be.
  pub fn loading(mut self, loading: bool) -> Self {
    self.loading = loading;
    self
  }

  /// How many rows of skeletons stand while the table is loading: 4 unless it is said.
  pub fn loading_rows(mut self, rows: usize) -> Self {
    self.loading_rows = rows.max(1);
    self
  }

  /// A footer under the rows with the count of them and this pagination at its end.
  pub fn pagination(mut self, pagination: impl Into<Element>) -> Self {
    self.pagination = Some(pagination.into());
    self.count = true;
    self
  }

  /// Whether the table has a footer that counts its rows: "24 rows", and how many are selected.
  pub fn count(mut self, count: bool) -> Self {
    self.count = count;
    self
  }

  /// How many rows there are in all, where the table shows some of them: a page.
  pub fn total(mut self, total: usize) -> Self {
    self.total = Some(total);
    self
  }

  /// What the table is called, to what reads the tree of the window. It draws nothing.
  pub fn label(mut self, label: &str) -> Self {
    self.label = Some(Arc::from(label));
    self
  }

  /// Whether no row and no header takes input.
  pub fn disabled(mut self, disabled: bool) -> Self {
    self.disabled = disabled;
    self
  }

  /// Puts the table in a state the theme's recipe names, or takes it out.
  pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
    let state = state.into();
    self.states.retain(|held| *held != state);
    if on {
      self.states.push(state);
    }
    self
  }

  /// Styles one part of this table over what the theme gives it.
  pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
    self.rules = self.rules.base(part, style);
    self
  }

  /// Lays rules over the theme's recipe for this table.
  pub fn rules(mut self, rules: &Recipe) -> Self {
    self.rules = self.rules.extend(rules);
    self
  }

  /// Draws every row as it is under the pointer, held down or focused from the keyboard, with nothing
  /// pointing at it: for a gallery or a test.
  pub fn force(mut self, interaction: Option<Interaction>) -> Self {
    self.forced = interaction;
    self
  }

  /// The id of the root. A row's element has this id with `-row-` and its place after it, from 0; a header's,
  /// `-header-` and its column's id; the header's checkbox `-select-all`.
  pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
    self.id = Some(id.into());
    self
  }

  pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
    self.key = Some(key.into());
    self
  }

  /// Builds the table in `theme` with every row. Converting a table into an element does this in the theme of
  /// what is being rendered.
  pub fn build(mut self, theme: &Theme) -> Element {
    self.find_grouping();
    let tracks = ColumnTracks::new(self.track_specs(theme));
    let view = self.view(theme, tracks, None);
    match theme.renderer::<Table<R, K>>() {
      Some(renderer) => renderer(view),
      None => view.render(),
    }
  }

  /// The table mounted where it is declared: it builds the rows in view and a few around them, keeps the
  /// widths its columns have measured from render to render, and takes PageUp and PageDown. For a table of
  /// many rows, or one whose columns are as wide as their content.
  pub fn mount(self, ctx: &mut Ctx) -> Element {
    let key = self.key.clone();
    let props = TableProps {
      table: Arc::new(Mutex::new(self)),
    };
    match key {
      Some(key) => ctx.mount_keyed::<TableHost<R, K>>(&key, props),
      None => ctx.mount::<TableHost<R, K>>(props),
    }
  }

  fn selectable(&self) -> bool {
    self.select.is_some()
  }

  /// The table as a data grid: its cells take the focus and are edited, where it is mounted.
  pub(super) fn as_grid(mut self) -> Self {
    self.grid = true;
    self.resizable = true;
    self.column_menu = true;
    self
  }

  pub(super) fn column_layout(mut self, layout: Option<ColumnLayout>, write: LayoutWrite) -> Self {
    self.column_layout = layout;
    self.write_column_layout = Some(write);
    self
  }

  pub(super) fn add_on_column_layout_change(mut self, handler: impl Fn(&ColumnLayout) + Send + Sync + 'static) -> Self {
    self.on_column_layout_change.push(Arc::new(handler));
    self
  }

  pub(super) fn set_group_by(mut self, columns: Vec<Arc<str>>) -> Self {
    self.group_by = columns;
    self
  }

  pub(super) fn set_open_groups(mut self, open: Vec<GroupKey>, write: OpenWrite) -> Self {
    self.open_groups = open;
    self.write_open = Some(write);
    self
  }

  pub(super) fn add_on_open_change(mut self, handler: impl Fn(&GroupKey, bool) + Send + Sync + 'static) -> Self {
    self.on_open_change.push(Arc::new(handler));
    self
  }

  pub(super) fn set_tree(mut self, parent: impl Fn(&R) -> Option<K> + Send + Sync + 'static) -> Self {
    self.tree_parent = Some(Arc::new(parent));
    self
  }

  pub(super) fn set_expanded(mut self, open: Vec<K>, write: KeysWrite<K>) -> Self {
    self.expanded = open;
    self.write_expanded = Some(write);
    self
  }

  pub(super) fn add_on_expanded_change(mut self, handler: impl Fn(&K, bool) + Send + Sync + 'static) -> Self {
    self.on_expanded_change.push(Arc::new(handler));
    self
  }

  pub(super) fn set_detail(mut self, detail: impl Fn(&R) -> Element + Send + Sync + 'static) -> Self {
    self.detail = Some(Arc::new(detail));
    self
  }

  pub(super) fn set_details_open(mut self, open: Vec<K>, write: KeysWrite<K>) -> Self {
    self.details_open = open;
    self.write_details = Some(write);
    self
  }

  pub(super) fn add_on_detail_change(mut self, handler: impl Fn(&K, bool) + Send + Sync + 'static) -> Self {
    self.on_detail_change.push(Arc::new(handler));
    self
  }

  /// Opens or closes the row `key`'s children, or its detail: asks for the keys open with it in or out, and tells
  /// every handler.
  fn key_toggle(
    now: &[K],
    write: &Option<KeysWrite<K>>,
    heard: &[KeyOpenHeard<K>],
  ) -> Arc<dyn Fn(&K, bool) + Send + Sync> {
    let (now, write, heard) = (now.to_vec(), write.clone(), heard.to_vec());
    Arc::new(move |key: &K, open: bool| {
      if now.contains(key) == open {
        return;
      }
      let mut next: Vec<K> = now.iter().filter(|held| *held != key).cloned().collect();
      if open {
        next.push(key.clone());
      }
      if let Some(write) = &write {
        write(next);
      }
      for handler in &heard {
        handler(key, open);
      }
    })
  }

  /// The rows at `filtered`, or every row, as a tree: each row after the row it is under, the rows under one in
  /// their order, those of a closed row out. A row a filter keeps keeps the rows it is under; a row whose parent is
  /// not among the rows is at the top.
  fn tree_lines(&self, parent_of: &ParentFn<R, K>, filtered: Option<Vec<usize>>) -> Lines {
    let count = self.rows.len();
    let keys: Vec<K> = self
      .rows
      .iter()
      .enumerate()
      .map(|(index, row)| (self.key_of)(index, row))
      .collect();
    let index_of: HashMap<&K, usize> = keys.iter().enumerate().map(|(index, key)| (key, index)).collect();
    let parent: Vec<Option<usize>> = self
      .rows
      .iter()
      .enumerate()
      .map(|(index, row)| {
        parent_of(row)
          .and_then(|key| index_of.get(&key).copied())
          .filter(|at| *at != index)
      })
      .collect();
    // The rows shown: those the filters keep, and every row above one of them.
    let mut shown = vec![filtered.is_none(); count];
    if let Some(kept) = &filtered {
      for &index in kept {
        let mut at = Some(index);
        while let Some(index) = at.filter(|index| !shown[*index]) {
          shown[index] = true;
          at = parent[index];
        }
      }
    }
    let mut children: Vec<Vec<usize>> = vec![Vec::new(); count];
    let mut tops = Vec::new();
    for index in (0..count).filter(|index| shown[*index]) {
      match parent[index] {
        Some(above) if shown[above] => children[above].push(index),
        _ => tops.push(index),
      }
    }
    let open: HashSet<&K> = self.expanded.iter().collect();
    let mut order = Vec::new();
    let mut tree = groups::Tree::default();
    let mut seen = vec![false; count];
    // Depth first, from the last of the tops: a row is followed by those under it while it is open.
    let mut stack: Vec<(usize, u32, Option<usize>)> = tops.iter().rev().map(|index| (*index, 0, None)).collect();
    while let Some((index, level, above)) = stack.pop() {
      if std::mem::replace(&mut seen[index], true) {
        continue;
      }
      let rank = order.len();
      order.push(index);
      let branch = !children[index].is_empty();
      let is_open = branch && open.contains(&keys[index]);
      tree.level.push(level);
      tree.parent.push(above);
      tree.branch.push(branch);
      tree.open.push(is_open);
      if is_open {
        stack.extend(
          children[index]
            .iter()
            .rev()
            .map(|child| (*child, level + 1, Some(rank))),
        );
      }
    }
    Lines::tree(order, tree)
  }

  /// The columns the rows are grouped by, found among all of them: before a grid's arrangement hides any.
  fn find_grouping(&mut self) {
    self.grouping = self
      .group_by
      .iter()
      .filter_map(|id| self.columns.iter().find(|column| &column.id == id).cloned())
      .collect();
  }

  /// The lines the table shows: its rows, or its rows grouped by the values of its grouping's columns, with the
  /// rows of the closed groups out.
  /// Whether its rows grow with what they hold: where a column wraps its text.
  fn tall(&self) -> bool {
    self.detail.is_some() || self.columns.iter().any(|column| column.wrap)
  }

  /// The places of the rows every filter lets through, in their order: `None` where no filter is on a column that
  /// filters.
  fn filtered(&self) -> Option<Vec<usize>> {
    // Over a source the app filters the rows it gives.
    if self.source.is_some() {
      return None;
    }
    let tests: Vec<Test<R>> = self
      .filters
      .iter()
      .filter_map(|(id, filter)| {
        let column = self.columns.iter().find(|column| &*column.id == id)?;
        let text = match &column.content {
          Content::Text(text) => Some(text.clone()),
          _ => None,
        };
        Test::new(column.filter.as_ref()?, text, filter)
      })
      .collect();
    if tests.is_empty() {
      return None;
    }
    Some(
      self
        .rows
        .iter()
        .enumerate()
        .filter(|(_, row)| tests.iter().all(|test| test.passes(row)))
        .map(|(index, _)| index)
        .collect(),
    )
  }

  fn lines(&self) -> Lines {
    let passed = self.filtered().map(Arc::new);
    let mut lines = self.laid_out(passed.as_deref().cloned());
    lines.passed = passed;
    // The detail of each row open, under it.
    if self.detail.is_some() && !self.details_open.is_empty() {
      let open: HashSet<&K> = self.details_open.iter().collect();
      let opened: Vec<bool> = (0..lines.ranks())
        .map(|rank| {
          lines
            .row_of_rank(rank)
            .is_some_and(|index| open.contains(&(self.key_of)(index, &self.rows[index])))
        })
        .collect();
      lines = lines.with_details(|rank| opened[rank]);
    }
    lines
  }

  /// The lines of the rows at `filtered`, or of every row.
  fn laid_out(&self, filtered: Option<Vec<usize>>) -> Lines {
    if let Some(parent) = self.tree_parent.as_ref().filter(|_| self.grouping.is_empty()) {
      return self.tree_lines(parent, filtered);
    }
    if self.grouping.is_empty() {
      return match filtered {
        Some(order) => Lines::rows(Arc::new(order)),
        None => Lines::flat(self.rows.len()),
      };
    }
    let ids: Vec<Arc<str>> = self.grouping.iter().map(|column| column.id.clone()).collect();
    let open: HashSet<&GroupKey> = self.open_groups.iter().collect();
    Lines::grouped(
      filtered.unwrap_or_else(|| (0..self.rows.len()).collect()),
      &ids,
      self.summarized(),
      &|row, level| column_text(&self.grouping[level], &self.rows[row]),
      &|key| open.contains(key),
    )
  }

  /// Whether a column says something in the summary rows: then the grid has one at its bottom, and one at the end
  /// of each group open.
  fn summarized(&self) -> bool {
    self.grid && self.columns.iter().any(|column| column.summary.is_some())
  }

  /// Opens or closes the group `key`: asks for the groups open with it in or out, and tells every handler.
  fn group_toggle(&self) -> Arc<dyn Fn(&GroupKey, bool) + Send + Sync> {
    let (now, write, heard) = (
      self.open_groups.clone(),
      self.write_open.clone(),
      self.on_open_change.clone(),
    );
    Arc::new(move |key: &GroupKey, open: bool| {
      if now.contains(key) == open {
        return;
      }
      let mut next: Vec<GroupKey> = now.iter().filter(|held| *held != key).cloned().collect();
      if open {
        next.push(key.clone());
      }
      if let Some(write) = &write {
        write(next);
      }
      for handler in &heard {
        handler(key, open);
      }
    })
  }

  pub(super) fn set_column_menu(mut self, menu: bool) -> Self {
    self.column_menu = menu;
    self
  }

  /// Asks for the columns of a grid arranged as `next`, and tells every handler: nothing where they are.
  fn layout_changer(&self) -> Option<LayoutWrite> {
    let (now, _) = &**self.arranged.as_ref()?;
    let now = now.clone();
    let (write, heard) = (self.write_column_layout.clone(), self.on_column_layout_change.clone());
    Some(Arc::new(move |next: ColumnLayout| {
      if next == now {
        return;
      }
      if let Some(write) = &write {
        write(next.clone());
      }
      for handler in &heard {
        handler(&next);
      }
    }))
  }

  pub(super) fn set_read_only(mut self, read_only: bool) -> Self {
    self.read_only = read_only;
    self
  }

  pub(super) fn add_on_edit(mut self, handler: impl Fn(&CellEdit<K>) -> bool + Send + Sync + 'static) -> Self {
    self.on_edit.push(Arc::new(handler));
    self
  }

  pub(super) fn cell_selection(mut self, range: Option<CellRange<K>>, write: RangeWrite<K>) -> Self {
    self.cell_range = range;
    self.write_cell_range = Some(write);
    self
  }

  pub(super) fn add_on_cell_selection_change(
    mut self,
    handler: impl Fn(Option<&CellRange<K>>) + Send + Sync + 'static,
  ) -> Self {
    self.on_cell_selection_change.push(Arc::new(handler));
    self
  }

  fn with_checkboxes(&self) -> bool {
    self.selectable() && self.checkboxes
  }

  pub(super) fn set_column_menu_labels(mut self, labels: ColumnMenuLabels) -> Self {
    self.words.menu = Arc::new(labels);
    self
  }

  pub(super) fn set_menu_trigger_label(mut self, label: impl Fn(&str) -> String + Send + Sync + 'static) -> Self {
    self.words.menu_trigger = Some(Arc::new(label));
    self
  }

  pub(super) fn set_group_label(mut self, label: impl Fn(&str, &str, usize) -> String + Send + Sync + 'static) -> Self {
    self.words.group = Some(Arc::new(label));
    self
  }

  pub(super) fn set_summary_labels(mut self, labels: SummaryLabels) -> Self {
    self.words.summary = Arc::new(labels);
    self
  }

  pub(super) fn set_row_numbers_label(mut self, label: &str) -> Self {
    self.words.numbers = Arc::from(label);
    self
  }

  pub(super) fn set_row_numbers(mut self, numbers: bool) -> Self {
    self.row_numbers = numbers;
    self
  }

  /// Whether a mounted grid shows its rows' numbers at its start.
  fn numbered(&self) -> bool {
    self.grid && self.row_numbers
  }

  /// The track of the column of numbers: the padding at the row's start it holds, the widest number, the room
  /// after it. Nothing where there is none.
  fn number_track(&self, geometry: Geometry) -> f32 {
    if !self.numbered() {
      return 0.0;
    }
    let digits = self.rows.len().max(1).to_string().len().max(2);
    geometry.padding_start + digits as f32 * 8.0 + geometry.gap
  }

  /// The tracks before the first column's: the numbers' and the checkboxes'.
  fn lead_tracks(&self) -> usize {
    usize::from(self.numbered()) + usize::from(self.with_checkboxes())
  }

  /// The case of the table as a whole.
  fn case(&self) -> Case {
    let mut case = Case::new()
      .breakpoint(super::current_breakpoint())
      .state(TableState::Selectable, self.with_checkboxes())
      .state(TableState::Footer, self.count || self.pagination.is_some())
      .state(TableState::Disabled, self.disabled);
    case.states.extend(self.states.iter().copied());
    if let Some(density) = &self.density {
      case = case.density(density.clone());
    }
    case
  }

  fn resolve(&self, theme: &Theme, case: &Case) -> ResolvedRecipe {
    theme.resolve::<Table<R, K>>(case, Some(&self.rules))
  }

  /// The tracks of the columns: the checkbox's first, then a track for each column. The room between two
  /// columns is the cell's (`Geometry`), so a track holds it.
  fn track_specs(&self, theme: &Theme) -> Vec<TrackSpec> {
    let geometry = Geometry::of(&self.resolve(theme, &self.case()));
    let count = self.columns.len();
    let mut specs = Vec::with_capacity(count + 2);
    let numbered = self.numbered();
    if numbered {
      specs.push(TrackSpec::px(self.number_track(geometry)));
    }
    if self.with_checkboxes() {
      let lead = if self.pins().0 > 0 && !numbered {
        geometry.padding_start
      } else {
        0.0
      };
      specs.push(TrackSpec::px(geometry.select + geometry.gap + lead));
    }
    for (index, column) in self.columns.iter().enumerate() {
      let mut extra = if index + 1 < count { geometry.gap } else { 0.0 };
      let (start, end) = self.pins();
      if index == 0 && start > 0 && !self.with_checkboxes() && !numbered {
        extra += geometry.padding_start;
      }
      // A row whose last columns stay holds its padding at the end in the last cell, which covers it.
      if index + 1 == count && end > 0 {
        extra += geometry.padding_end;
      }
      let mut spec = column.track;
      if let Some(width) = self.resized.get(&column.id) {
        spec.width = TrackWidth::Px(*width);
      }
      if let TrackWidth::Px(px) = spec.width {
        spec.width = TrackWidth::Px(px + extra);
      }
      if spec.min > 0.0 {
        spec.min += extra;
      }
      if spec.max.is_finite() {
        spec.max += extra;
      }
      specs.push(spec);
    }
    specs
  }

  /// How many columns stay at the start and how many at the end: the first ones pinned at the start, one
  /// after another, and the last ones pinned at the end.
  fn pins(&self) -> (usize, usize) {
    let start = self
      .columns
      .iter()
      .take_while(|column| column.pin == ColumnPin::Start)
      .count();
    let end = self.columns[start..]
      .iter()
      .rev()
      .take_while(|column| column.pin == ColumnPin::End)
      .count();
    (start, end)
  }

  /// The table as a renderer sees it, with the rows of `window` where it builds some of them.
  fn view(&self, theme: &Theme, tracks: ColumnTracks, window: Option<Window>) -> TableView {
    let case = self.case();
    let recipe = self.resolve(theme, &case).owned_by(self.id.as_ref());
    let geometry = Geometry::of(&recipe);
    let selectable = self.selectable();
    let checkboxes = self.with_checkboxes();
    let (start_pins, end_pins) = self.pins();
    // A grid's numbers stay at its start, and so does what is before its first column.
    let numbered = self.numbered();
    let sticky = start_pins > 0 || numbered;
    let number_lead = self.number_track(geometry);
    let footer = self.count || self.pagination.is_some();
    let count = self.rows.len();

    // Each case of a row and of a cell is resolved once, however many rows there are.
    let mut row_recipes: HashMap<(bool, bool), ResolvedRecipe> = HashMap::new();
    let mut row_recipe = |selected: bool, last: bool| {
      row_recipes
        .entry((selected, last))
        .or_insert_with(|| {
          let case = case
            .clone()
            .state(TableState::Selected, selected)
            .state(TableState::Last, last)
            .state(TableState::Pinned, sticky);
          self.resolve(theme, &case).owned_like(&recipe)
        })
        .clone()
    };
    let mut cell_recipes: HashMap<(usize, bool, Option<RangeMark>), ResolvedRecipe> = HashMap::new();
    let mut cell_recipe = |column: usize, selected: bool, mark: Option<RangeMark>| {
      cell_recipes
        .entry((column, selected, mark))
        .or_insert_with(|| {
          let pinned = column < start_pins || column + end_pins >= self.columns.len();
          let marked = |side: fn(&RangeMark) -> bool| mark.as_ref().is_some_and(side);
          let mut case = case
            .clone()
            .state(TableState::Selected, selected)
            .state(TableState::Pinned, pinned)
            .state(TableState::InRange, marked(|mark| mark.fill))
            .state(TableState::RangeTop, marked(|mark| mark.top))
            .state(TableState::RangeBottom, marked(|mark| mark.bottom))
            .state(TableState::RangeStart, marked(|mark| mark.start))
            .state(TableState::RangeEnd, marked(|mark| mark.end));
          case.states.extend(self.columns[column].kinds.iter().copied());
          self.resolve(theme, &case).owned_like(&recipe)
        })
        .clone()
    };

    // The lines shown: the rows, or the groups and the rows of the open ones. A row's rank is its place among the
    // rows shown, which the cells of a grid are reckoned by.
    let lines: Arc<Lines> = match window.as_ref() {
      Some(window) => window.lines.clone(),
      None => Arc::new(self.lines()),
    };
    // The keys of the rows are reckoned as they are asked for: those of the rows built, and of those a selection
    // or a range names, through an index a mounted table keeps while its rows are the same.
    let key_index = match window.as_ref().and_then(|window| window.host.as_ref()) {
      Some(host) => host.state.key_index.lock().for_rows::<R, K>(&self.rows),
      None => Arc::default(),
    };
    let keys = {
      let (rows, key_of) = (self.rows.clone(), self.key_of.clone());
      RowKeys::new(
        Arc::new(move |index: usize| key_of(index, &rows[index])),
        self.rows.len(),
        lines.clone(),
        key_index,
      )
    };
    // A row the app asks to see is scrolled to once for each asking, by the table that keeps where it is
    // scrolled, once it knows how high its rows are.
    if let Some((key, request)) = &self.reveal
      && let Some(host) = window.as_ref().and_then(|window| window.host.as_ref())
      && *host.state.revealed.lock() != Some(*request)
      && host.state.metrics.lock().row_height > 0.0
    {
      *host.state.revealed.lock() = Some(*request);
      if let Some(position) = keys.rank_of(key).and_then(|rank| lines.position_of(rank)) {
        host.state.reveal(position);
      }
    }
    let grouped = !lines.groups.is_empty() || !self.grouping.is_empty() || lines.tree.is_some();
    let ranked = keys.clone();
    let model = Arc::new(Model {
      keys: keys.clone(),
      selected: self.selected.clone(),
      select: self.select.clone(),
      on_selection_change: self.on_selection_change.clone(),
      on_row_click: self.on_row_click.clone(),
      on_row_activate: self.on_row_activate.clone(),
    });

    // Where the cells at the start stay: the checkbox's at the edge, the first column's after it.
    let lead_inset = number_lead
      + if checkboxes {
        geometry.select + geometry.gap + if numbered { 0.0 } else { geometry.padding_start }
      } else {
        0.0
      };
    // Where each pinned column stays: after the checkboxes and the pinned columns before it at the start, before
    // the pinned columns after it at the end. Each is as wide as its track is known to be.
    let track_widths = tracks.widths(None, 0.0);
    let lead_tracks = self.lead_tracks();
    let width_of = |column: usize| track_widths.get(column + lead_tracks).copied().unwrap_or(0.0);
    let columns_count = self.columns.len();
    let sticky_of = |column: usize| -> Option<Sticky> {
      if column < start_pins {
        Some(Sticky::left(lead_inset + (0..column).map(width_of).sum::<f32>()))
      } else if column + end_pins >= columns_count {
        Some(Sticky::right((column + 1..columns_count).map(width_of).sum::<f32>()))
      } else {
        None
      }
    };
    let trail_of = |column: usize| {
      if column + 1 == columns_count && end_pins > 0 {
        geometry.padding_end
      } else {
        0.0
      }
    };
    let table_fill = recipe.part(TablePart::Root).rest().background;
    let header_fill = recipe.part(TablePart::Header).rest().background;
    let cell_ends = |column: usize| column + 1 == self.columns.len();
    let columns_built = window.as_ref().and_then(|window| window.columns.clone());
    let built = |column: usize| {
      columns_built
        .as_ref()
        .is_none_or(|built| built.get(column).copied().unwrap_or(true))
    };

    // A mounted grid's cells take the focus and are edited.
    let grid_host = window
      .as_ref()
      .filter(|_| self.grid)
      .and_then(|window| window.host.as_ref())
      .map(|host| host.state.clone());
    let ids: Vec<Arc<str>> = self.columns.iter().map(|column| column.id.clone()).collect();
    let span = grid_host
      .as_ref()
      .and(self.cell_range.as_ref())
      .and_then(|range| span_of(range, &ranked, &ids));
    let grid_model: Option<Arc<dyn GridCells>> = grid_host.as_ref().map(|host| {
      Arc::new(GridModel {
        host: host.clone(),
        span,
        range: self.cell_range.clone(),
        write_range: self.write_cell_range.clone(),
        on_range: self.on_cell_selection_change.clone(),
        texts: Arc::new(
          self
            .columns
            .iter()
            .map(|column| match &column.content {
              Content::Text(text) => Some(text.clone()),
              _ => None,
            })
            .collect(),
        ),
        titles: Arc::new(self.columns.iter().map(|column| column.title.clone()).collect()),
        rows: self.rows.clone(),
        keys: ranked.clone(),
        lines: lines.clone(),
        editors: Arc::new(
          self
            .columns
            .iter()
            .map(|column| (column.id.clone(), column.editor.clone()))
            .collect(),
        ),
        on_edit: self.on_edit.clone(),
        model: model.clone(),
        read_only: self.read_only,
      }) as Arc<dyn GridCells>
    });
    let editing = grid_host.as_ref().and_then(|host| *host.editing.lock());
    let focused_column = grid_host
      .as_ref()
      .and_then(|host| *host.focused_column.lock())
      .unwrap_or(0);
    let held_now: HashSet<&K> = self.selected.iter().collect();
    // The lines built, by their positions.
    let shown: Vec<usize> = match &window {
      _ if self.loading => Vec::new(),
      Some(window) => window.indices.clone(),
      None => (0..lines.len()).collect(),
    };
    let entry = window
      .as_ref()
      .and_then(|window| window.focused.filter(|position| shown.contains(position)))
      .or_else(|| {
        shown.iter().copied().find(|position| {
          lines
            .rank_at(*position)
            .and_then(|rank| keys.get(rank))
            .is_some_and(|key| held_now.contains(&key))
        })
      })
      .or_else(|| shown.first().copied());
    let line_count = lines.len();
    let id_of = |what: &str| self.id.as_ref().map(|id| Arc::<str>::from(format!("{id}-{what}")));

    // The groups' rows built, each opened and closed through the app's groups.
    let toggle = self.group_toggle();
    if let Some(host) = window.as_ref().and_then(|window| window.host.as_ref()) {
      let (toggle, lines) = (toggle.clone(), lines.clone());
      *host.state.group_toggle.lock() = Some(Arc::new(move |group: usize, open: bool| {
        if let Some(line) = lines.groups.get(group) {
          toggle(&line.key, open);
        }
      }));
    }
    let group_recipe = row_recipe(false, false);
    let indent = geometry.padding_start;
    let groups: Vec<TableGroup> = shown
      .iter()
      .filter_map(|&position| {
        let Some(Shown::Group(group)) = lines.line(position) else {
          return None;
        };
        let line = &lines.groups[group];
        let title = self
          .grouping
          .iter()
          .find(|column| column.id == line.column)
          .map_or_else(|| line.column.clone(), |column| column.title.clone());
        let (toggle, key) = (toggle.clone(), line.key.clone());
        let id = id_of(&format!("group-{position}"));
        Some(TableGroup {
          position,
          key: line.key.clone(),
          title: title.clone(),
          count: line.count,
          open: line.open,
          entry: entry == Some(position),
          recipe: group_recipe.item(id.as_ref(), TablePart::GroupRow),
          toggle: Arc::new(move |open| toggle(&key, open)),
          element: window
            .as_ref()
            .and_then(|window| window.refs.get(&position).cloned())
            .unwrap_or_default(),
          grid: grid_model.is_some(),
          indent: indent + (line.key.level() - 1) as f32 * 20.0,
          name: {
            let value = line.key.values.last().map_or("", String::as_str);
            match &self.words.group {
              Some(label) => label(&title, value, line.count),
              None => {
                let locale = crate::ui::current_locale();
                format!("{title}: {value}, {}", locale.count(&locale.data.rows, line.count))
              }
            }
          },
          id,
        })
      })
      .collect();
    // The summary rows: one at the end of each group open, built where it is in view, and the grid's at its bottom.
    let summary_recipe = row_recipe(false, false);
    let summary_fill = summary_recipe.part(TablePart::Summary).rest().background;
    // What each summary says is kept by a mounted grid while what it is computed from is the same: a render that
    // only scrolls, hovers or moves the focus computes none.
    let summary_host = window
      .as_ref()
      .and_then(|window| window.host.as_ref())
      .map(|host| host.state.clone());
    let mut held: HashMap<SummaryKey, Arc<Vec<Said>>> = summary_host
      .as_ref()
      .map(|host| std::mem::take(&mut *host.summaries.lock()))
      .unwrap_or_default();
    let mut kept: HashMap<SummaryKey, Arc<Vec<Said>>> = HashMap::new();
    let summary_columns: Vec<(Arc<str>, Option<SummaryKind>)> = self
      .columns
      .iter()
      .filter_map(|column| {
        let kind = match column.summary.as_ref()? {
          Summary::Kind(kind) => Some(*kind),
          Summary::Text(_) => None,
        };
        Some((column.id.clone(), kind))
      })
      .collect();
    let grouping_ids: Vec<Arc<str>> = self.grouping.iter().map(|column| column.id.clone()).collect();
    let rows_seen = (Arc::as_ptr(&self.rows) as *const () as usize, self.rows.len());
    // What a summary is of: the rows shown, those the filters let through.
    let filters_seen = self.filters.to_text();
    // A group's rows by their places, or every row.
    let mut values_of = |group: Option<&GroupKey>, places: Option<&[usize]>| -> Arc<Vec<Said>> {
      let key = SummaryKey {
        rows: rows_seen,
        columns: summary_columns.clone(),
        grouping: grouping_ids.clone(),
        group: group.map(|group| group.values.clone()),
        filters: filters_seen.clone(),
      };
      let values = held
        .remove(&key)
        .or_else(|| kept.get(&key).cloned())
        .unwrap_or_else(|| {
          let rows: Vec<&R> = match places {
            Some(places) => places.iter().map(|index| &self.rows[*index]).collect(),
            None => match &lines.passed {
              Some(passed) => passed.iter().map(|index| &self.rows[*index]).collect(),
              None => self.rows.iter().collect(),
            },
          };
          Arc::new(self.columns.iter().map(|column| summarize(column, &rows)).collect())
        });
      kept.insert(key, values.clone());
      values
    };
    let summary_of = |values: &[Said], id: Option<Arc<str>>, position: Option<usize>| {
      // Its cells are named after the summary, each by its column: `x-summary-cell-size`.
      let summary_recipe = summary_recipe.item(id.as_ref(), TablePart::Summary);
      TableSummary {
        position,
        cells: self
          .columns
          .iter()
          .enumerate()
          .filter(|(column_index, _)| built(*column_index))
          .map(|(column_index, column)| TableCell {
            column: column_index,
            content: summary_content(&summary_recipe, &values[column_index], &self.words.summary),
            grid: None,
            part: summary_recipe.part(TablePart::Cell).keyed(&column.id),
            align: column.align,
            pinned: sticky_of(column_index),
            fill: sticky_of(column_index).and(summary_fill.clone()),
            controls: false,
            last: cell_ends(column_index),
            gap: geometry.gap,
            lead: if column_index == 0 && sticky && !checkboxes && !numbered {
              geometry.padding_start
            } else {
              0.0
            },
            trail: trail_of(column_index),
          })
          .collect(),
        recipe: summary_recipe.clone(),
        tracks: tracks.clone(),
        columns_built: columns_built.clone(),
        geometry,
        checkbox: checkboxes,
        number_lead,
        pinned_start: sticky,
        pinned_end: end_pins > 0,
        fill: summary_fill.clone(),
        name: self.words.summary.row.clone(),
        id,
      }
    };
    // Every open group's summary is computed with the others, built or not, so a scroll that brings one into view
    // finds it.
    if self.summarized() {
      for line in lines.groups.iter().filter(|line| line.open) {
        values_of(Some(&line.key), Some(&line.rows));
      }
    }
    let summaries: Vec<TableSummary> = shown
      .iter()
      .filter_map(|&position| {
        let Some(Shown::Summary(group)) = lines.line(position) else {
          return None;
        };
        let line = &lines.groups[group];
        let values = values_of(Some(&line.key), Some(&line.rows));
        Some(summary_of(
          &values,
          id_of(&format!("summary-{position}")),
          Some(position),
        ))
      })
      .collect();
    let summary = (self.summarized() && count > 0 && !self.loading).then(|| {
      let values = values_of(None, None);
      summary_of(&values, id_of("summary"), None)
    });
    drop(values_of);
    if let Some(host) = &summary_host {
      *host.summaries.lock() = kept;
    }
    let tall = self.tall();
    // A cell's text with the search's matches in it marked, the current one stronger.
    let searched = window.as_ref().and_then(|window| window.search.clone());
    let marked = |rank: usize, column: usize, text: components::Text, shown: &str| -> components::Text {
      let Some((found, current)) = &searched else {
        return text;
      };
      let (ranges, now) = SearchState::in_cell(found, *current, rank, column);
      if ranges.is_empty() {
        return text;
      }
      text.highlights(
        super::highlight::Highlight::new(shown)
          .marks(ranges)
          .current(now)
          .fills_in(theme),
      )
    };
    // A row of a tree opens and closes the rows under it, a row with a detail its detail: by its rank, through the
    // app's keys open.
    let by_rank = |toggle: Arc<dyn Fn(&K, bool) + Send + Sync>| {
      let keys = keys.clone();
      Arc::new(move |rank: usize, open: bool| {
        if let Some(key) = keys.get(rank) {
          toggle(&key, open);
        }
      }) as Arc<dyn Fn(usize, bool) + Send + Sync>
    };
    let tree_toggle = lines.tree.as_ref().map(|_| {
      by_rank(Self::key_toggle(
        &self.expanded,
        &self.write_expanded,
        &self.on_expanded_change,
      ))
    });
    let detail_toggle = self.detail.as_ref().map(|_| {
      by_rank(Self::key_toggle(
        &self.details_open,
        &self.write_details,
        &self.on_detail_change,
      ))
    });
    if let Some(host) = window.as_ref().and_then(|window| window.host.as_ref()) {
      *host.state.tree_toggle.lock() = tree_toggle.clone();
      *host.state.detail_toggle.lock() = detail_toggle.clone();
    }
    let details_now: HashSet<&K> = self.details_open.iter().collect();
    // What opens a row in its first cell: the tree's, or the detail's where the rows are no tree.
    let opener = |rank: usize, index: usize| -> Option<(u32, Option<bool>, Arc<dyn Fn(usize, bool) + Send + Sync>)> {
      match (&lines.tree, &tree_toggle, &detail_toggle) {
        (Some(tree), Some(toggle), _) => Some((
          tree.level.get(rank).copied().unwrap_or(0),
          tree.branch.get(rank).copied().unwrap_or(false).then(|| tree.open[rank]),
          toggle.clone(),
        )),
        (None, _, Some(toggle)) => Some((0, Some(details_now.contains(&keys.at(index))), toggle.clone())),
        _ => None,
      }
    };
    let toggle_part = recipe.part(TablePart::TreeToggle).forced(self.forced);
    let rows = shown
      .iter()
      .filter_map(|&position| {
        let rank = lines.rank_at(position)?;
        Some((position, rank, lines.row_of_rank(rank)?))
      })
      .map(|(position, rank, index)| {
        let row = &self.rows[index];
        // Over a source: a row whose range has not come is a skeleton, one whose range failed says so.
        let slot = self.slots.as_ref().map(|(slots, _)| slots(index));
        let first_built = (0..self.columns.len()).find(|column| built(*column));
        let selected = selectable && held_now.contains(&keys.at(index));
        let last = position + 1 == line_count && !footer;
        // The row's parts are named after its id, `x-row-<index>`.
        let row_id = id_of(&format!("row-{index}"));
        let recipe = row_recipe(selected, last).item(row_id.as_ref(), TablePart::Row);
        // A cell that stays covers what scrolls under it, in the row's fill, or the table's.
        let stuck_fill: BackgroundColor = recipe
          .part(TablePart::Row)
          .rest()
          .background
          .or_else(|| table_fill.clone())
          .unwrap_or_else(|| PaletteColor::Background.into());
        let cells = self
          .columns
          .iter()
          .enumerate()
          .filter(|(column_index, _)| built(*column_index))
          .map(|(column_index, column)| {
            let mark = span.and_then(|span| span.mark(rank, column_index));
            // A cell is named by its row and its column, as a grid's is: `x-cell-<row>-<column>`.
            let part = cell_recipe(column_index, selected, mark)
              .part(TablePart::Cell)
              .keyed(format!("{index}-{column_index}"))
              .forced(self.forced);
            let content = match (&column.content, column.editor.as_ref().map(|editor| &editor.kind)) {
              // A text that wraps takes the room of its cell, and its lines as many as they need.
              (Content::Text(text), _) if column.wrap => marked(rank, column_index, part.text(&text(row)), &text(row))
                .flex_full(1.0, 1.0, Some(0.0))
                .min_width(Dimension::Px(0.0))
                .into(),
              // A text on one line gives way to the room its column has, and ends in an ellipsis: what reads the
              // tree still has all of it.
              (Content::Text(text), _) => marked(rank, column_index, part.text(&text(row)), &text(row))
                .nowrap()
                .text_overflow(crate::node::node_kind::TextOverflow::Elipsis)
                .flex_full(0.0, 1.0, None)
                .min_width(Dimension::Px(0.0))
                .into(),
              (Content::Element(cell), _) => cell(row),
              // A column edited with a box that builds no cell of its own shows the box.
              (Content::Nothing, Some(EditorKind::Checkbox(checked))) => Row::new()
                .inert(true)
                .child(
                  Checkbox::new(&column.title)
                    .label_hidden(true)
                    .checked(checked(row))
                    .build(theme),
                )
                .into(),
              (Content::Nothing, _) => Row::new().into(),
            };
            let content = match slot {
              Some(Slot::Pending) => Skeleton::block(Length::Fill, 12.0).build(theme),
              Some(Slot::Failed) if Some(column_index) == first_built => {
                let retry = self.slots.as_ref().map(|(_, retry)| retry.clone());
                let mut said = Row::new()
                  .spacing(8.0)
                  .align_items(Alignment::Center)
                  .child(part.text(&self.words.source.failed).nowrap());
                if let Some(retry) = retry {
                  said = said.child(
                    super::button::Button::new(&self.words.source.retry)
                      .variant(super::button::ButtonVariant::Ghost)
                      .size(super::button::ButtonSize::Sm)
                      .on_click(move |_| retry(index))
                      .build(theme),
                  );
                }
                said.into()
              }
              Some(Slot::Failed) => Row::new().into(),
              _ => content,
            };
            // The first column's cell of a row of a tree: its level's indent, and the chevron that opens and closes
            // the rows under it (room for one where there are none); of a row with a detail, the detail's chevron.
            let content = match opener(rank, index).filter(|_| column_index == 0) {
              Some((level, open, toggle)) => tree_cell(
                &toggle_part.clone().keyed(index),
                level,
                open,
                rank,
                toggle,
                content,
                column.wrap,
              ),
              None => content,
            };
            let grid = grid_model.as_ref().map(|grid| {
              let editable = !self.read_only && !self.disabled && column.editor.is_some();
              let being_edited = editing.filter(|editing| (editing.row, editing.column) == (rank, column_index));
              let editor = being_edited.and_then(|editing| {
                let host = grid_host.as_ref()?;
                let editor = column.editor.as_ref()?;
                let ref_ = window.as_ref()?.editor_ref.clone();
                let part = recipe.part(TablePart::Editor);
                let built: Element = match &editor.kind {
                  // Every editor is as wide as its cell's room, whatever the column's width.
                  EditorKind::Text(_) => Input::new(host.draft_text.clone())
                    .id(id_of("editor").unwrap_or_else(|| Arc::from("editor")))
                    .size(InputSize::Sm)
                    .full_width(true)
                    .invalid(editing.refused)
                    .label(&column.title)
                    .build(theme),
                  EditorKind::Number(_) => NumberInput::optional(host.draft_number.clone())
                    .id(id_of("editor").unwrap_or_else(|| Arc::from("editor")))
                    .size(InputSize::Sm)
                    .full_width(true)
                    .invalid(editing.refused)
                    .label(&column.title)
                    .build(theme),
                  EditorKind::Choice(choices, _) => {
                    let committing = grid.clone();
                    Select::new(
                      host.draft_choice.clone(),
                      choices
                        .iter()
                        .enumerate()
                        .map(|(place, choice)| crate::components::SelectOption::new(place, choice.clone())),
                    )
                    .id(id_of("editor").unwrap_or_else(|| Arc::from("editor")))
                    .size(InputSize::Sm)
                    .full_width(true)
                    .invalid(editing.refused)
                    .label(&column.title)
                    .on_change(move |_: &usize| committing.commit(GridStep::Stay))
                    .build(theme)
                  }
                  EditorKind::Checkbox(_) => return None,
                };
                let choice = matches!(editor.kind, EditorKind::Choice(..));
                Some(editor_in(&part, built, choice, grid.clone(), ref_).into())
              });
              GridCell {
                row: rank,
                position,
                editable,
                selected: span.is_some_and(|span| span.anchor != span.extent && span.contains(rank, column_index)),
                editor,
                entry: entry == Some(position)
                  && column_index == focused_column.min(self.columns.len().saturating_sub(1)),
                column_index: column_index + 1 + lead_tracks,
                checkbox: column.editor.as_ref().is_some_and(CellEditor::is_checkbox),
                element: window
                  .as_ref()
                  .and_then(|window| window.cell_refs.get(&(rank, column_index)).cloned())
                  .unwrap_or_default(),
                grid: grid.clone(),
                id: id_of(&format!("cell-{index}-{column_index}")),
              }
            });
            TableCell {
              column: column_index,
              content,
              grid,
              part,
              align: column.align,
              pinned: sticky_of(column_index),
              // A cell of the range has the range's fill, over what scrolls under it too.
              fill: sticky_of(column_index)
                .filter(|_| !mark.is_some_and(|mark| mark.fill))
                .map(|_| stuck_fill.clone()),
              controls: column.controls,
              last: cell_ends(column_index),
              gap: geometry.gap,
              lead: if column_index == 0 && sticky && !checkboxes && !numbered {
                geometry.padding_start
              } else {
                0.0
              },
              trail: trail_of(column_index),
            }
          })
          .collect();
        let name = self.columns.first().and_then(|column| match &column.content {
          Content::Text(text) => Some(Arc::<str>::from(text(row))),
          _ => None,
        });
        TableRow {
          index,
          position,
          tree: lines.tree.as_ref().map(|tree| {
            (
              tree.level.get(rank).copied().unwrap_or(0) + 1,
              tree.branch.get(rank).copied().unwrap_or(false).then(|| tree.open[rank]),
            )
          }),
          number: numbered.then_some(rank + 1),
          number_lead,
          selected,
          disabled: self.disabled,
          name,
          cells,
          checkbox: checkboxes,
          pinned_start: sticky,
          pinned_end: end_pins > 0,
          lead_fill: sticky.then(|| stuck_fill.clone()),
          entry: entry == Some(position),
          grows: tall,
          recipe,
          forced: self.forced,
          tracks: tracks.clone(),
          columns_built: columns_built.clone(),
          grid: grid_model.is_some(),
          geometry,
          actions: (!self.disabled).then(|| model.actions(index)),
          select_label: self.words.select_row.clone(),
          host: window.as_ref().and_then(|window| window.host.clone()),
          element: window
            .as_ref()
            .and_then(|window| window.refs.get(&position).cloned())
            .unwrap_or_default(),
          id: row_id,
        }
      })
      .collect();

    // The details of the rows open, built where their lines are: as wide as the view and staying in it while the
    // columns scroll sideways, measured as they are laid out.
    let view_width = window
      .as_ref()
      .and_then(|window| window.host.as_ref())
      .map_or(0.0, |host| host.state.scroll.viewport_width());
    let detail_part = recipe.part(TablePart::Detail).forced(self.forced);
    let details: Vec<(usize, Element)> = match &self.detail {
      Some(detail) => shown
        .iter()
        .filter_map(|&position| {
          let Some(Shown::Detail(rank)) = lines.line(position) else {
            return None;
          };
          let index = lines.row_of_rank(rank)?;
          let mut line = Row::new()
            .part(&detail_part)
            .align_items(Alignment::Start)
            // What is in it is reached with Tab; the arrows go from row to row past it.
            .focus_group(FocusGroup::none())
            .key(format!("detail-{index}"))
            .child(detail(&self.rows[index]));
          if let Some(element) = window.as_ref().and_then(|window| window.refs.get(&position)) {
            line = line.ref_element(element.clone());
          }
          if view_width > 0.0 {
            line = line.width(Dimension::Px(view_width)).sticky(Sticky::left(0.0));
          }
          if let Some(id) = id_of(&format!("detail-{index}")) {
            line = line.id(id);
          }
          Some((position, line.into()))
        })
        .collect(),
      None => Vec::new(),
    };

    // A mounted grid's headers have a menu, are dragged to another place, and are found by where they are.
    let menu_host = grid_host.as_ref().filter(|_| self.column_menu && !self.disabled);
    let change_layout = menu_host.and_then(|_| self.layout_changer());
    let open_menu = menu_host.and_then(|host| host.menu_open.lock().clone());
    let shown_ids: Arc<Vec<Arc<str>>> = Arc::new(self.columns.iter().map(|column| column.id.clone()).collect());
    let header_drop = menu_host.and_then(|host| *host.header_drop.lock());
    let drop_of = |column: usize| match header_drop {
      Some((from, to)) if to != Some(from) && to != Some(from + 1) => match to {
        Some(to) if to == column => Some(DropSide::Before),
        None if column + 1 == columns_count && from + 1 != columns_count => Some(DropSide::After),
        _ => None,
      },
      _ => None,
    };
    let mut header_refs: HashMap<usize, ElementRef> = HashMap::new();
    let change_filters = self.filter_changer();
    let headers = self
      .columns
      .iter()
      .enumerate()
      .filter(|(column_index, _)| built(*column_index))
      .map(|(column_index, column)| {
        let sort_place = self.sort.iter().position(|sort| sort.column == column.id);
        let sorted = sort_place.map(|at| self.sort[at].direction);
        // Its place in the sort, where the rows are sorted by more than one column.
        let sort_rank = sort_place.filter(|_| self.sort.len() > 1).map(|at| at + 1);
        let sortable = column.sortable && !self.disabled;
        let open = open_menu.as_deref() == Some(&*column.id);
        let drop = drop_of(column_index);
        let header_case = case
          .clone()
          .state(TableState::Sortable, sortable)
          .state(TableState::Sorted, sorted.is_some())
          .state(TableState::Pinned, sticky_of(column_index).is_some())
          .state(TableState::Open, open)
          .state(TableState::DropBefore, drop == Some(DropSide::Before))
          .state(TableState::DropAfter, drop == Some(DropSide::After));
        let sorts = self.write_sort.is_some() || !self.on_sort_change.is_empty();
        // How the rows are filtered by the column, where it filters and the grid's filters change.
        let filter = grid_host
          .clone()
          .zip(change_filters.clone())
          .and_then(|(host, change)| {
            let by = column.filter.as_ref()?;
            let open = host.filter_open.get() && host.filters.column.lock().as_deref() == Some(&*column.id);
            let shape = match by {
              FilterBy::Text => FilterShape::Text,
              FilterBy::Set | FilterBy::Values(_) => FilterShape::Set(if open || self.filter_row {
                self.values_of(&host, column)
              } else {
                Arc::default()
              }),
              FilterBy::Number(_) => FilterShape::Number,
              FilterBy::Date(_) => FilterShape::Date,
              FilterBy::Bool(_) => FilterShape::Bool,
            };
            let (now, id) = (self.filters.clone(), column.id.clone());
            Some(Arc::new(FilterLink {
              id: column.id.clone(),
              title: column.title.clone(),
              shape,
              current: self.filters.get(&column.id).cloned(),
              change: Arc::new(move |next: Option<ColumnFilter>| {
                change(match next {
                  Some(filter) => now.clone().with(&id, filter),
                  None => now.clone().without(&id),
                })
              }),
              open,
              host,
              labels: self.words.filter.clone(),
            }))
          });
        // What a grid's menus write out, and where the text goes: the app's handlers, or the clipboard.
        let export_now: Option<Arc<dyn Fn(ExportScope, ExportFormat) + Send + Sync>> =
          grid_model.as_ref().filter(|_| self.export).map(|grid| {
            let (grid, handlers, window) = (grid.clone(), self.on_export.clone(), super::theme::current_window());
            Arc::new(move |scope: ExportScope, format: ExportFormat| {
              let (text, rows) = grid.export(scope, format);
              if handlers.is_empty() {
                if let Some(window) = &window {
                  window.request_copy(text);
                }
                return;
              }
              let export = GridExport {
                format,
                scope,
                text,
                rows,
              };
              for handler in &handlers {
                handler(&export);
              }
            }) as Arc<dyn Fn(ExportScope, ExportFormat) + Send + Sync>
          });
        let export_link = export_now
          .as_ref()
          .zip(grid_model.as_ref())
          .map(|(_, grid)| export::ExportLink {
            selected: grid.has_selection(),
            labels: self.words.export.clone(),
          });
        if let Some(host) = &grid_host {
          *host.export_now.lock() = export_now.clone();
          *host.export_selected.lock() = export_link.as_ref().is_some_and(|link| link.selected);
        }
        let menu = menu_host.zip(change_layout.clone()).map(|(host, change)| {
          let element = ElementRef::new();
          header_refs.insert(column_index, element);
          let (layout, titles) = &**self
            .arranged
            .as_ref()
            .expect("a grid with a layout changer is arranged");
          let id = column.id.clone();
          let choose: Arc<dyn Fn(MenuChoice) + Send + Sync> = {
            let (id, layout, change) = (id.clone(), layout.clone(), change.clone());
            let (write_sort, sort_heard, now) =
              (self.write_sort.clone(), self.on_sort_change.clone(), self.sort.clone());
            let (filter_host, filter_change, filters_now) =
              (host.clone(), change_filters.clone(), self.filters.clone());
            let export = export_now.clone();
            Arc::new(move |choice: MenuChoice| match choice {
              MenuChoice::Sort(direction) => {
                // A direction sorts by the column alone; "clear" takes it out of the sort, the rest as it is.
                let next: Vec<TableSort> = match direction {
                  Some(direction) => vec![TableSort {
                    column: id.clone(),
                    direction,
                  }],
                  None => now.iter().filter(|sort| sort.column != id).cloned().collect(),
                };
                if let Some(write) = &write_sort {
                  write(next.clone());
                }
                for handler in &sort_heard {
                  handler(&next);
                }
              }
              MenuChoice::Pin(pin) => change(layout.clone().with_pin(&id, pin)),
              MenuChoice::Fit => change(layout.clone().with_fit(&id)),
              MenuChoice::Hide => change(layout.clone().with_hidden(&id, true)),
              MenuChoice::Toggle(other) => {
                let hidden = layout.place(&other).is_some_and(|place| place.hidden);
                change(layout.clone().with_hidden(&other, !hidden));
              }
              MenuChoice::ShowAll => change(layout.clone().with_all_shown()),
              MenuChoice::Filter => {
                *filter_host.filters.column.lock() = Some(id.clone());
                filter_host.filter_open.set(true);
                filter_host.rebuild();
              }
              MenuChoice::ClearFilter => {
                if let Some(change) = &filter_change {
                  change(filters_now.clone().without(&id));
                }
              }
              MenuChoice::ClearAllFilters => {
                if let Some(change) = &filter_change {
                  change(TableFilters::new());
                }
              }
              MenuChoice::Export(scope, format) => {
                if let Some(export) = &export {
                  export(scope, format);
                }
              }
            })
          };
          let set_open: Arc<dyn Fn(bool) + Send + Sync> = {
            let (host, id) = (host.clone(), id.clone());
            Arc::new(move |open: bool| {
              let mut held = host.menu_open.lock();
              if open {
                *held = Some(id.clone());
              } else if held.as_deref() == Some(&*id) {
                *held = None;
              }
              std::mem::drop(held);
              host.rebuild();
            })
          };
          let move_to: Arc<dyn Fn(Option<usize>) + Send + Sync> = {
            let (id, layout, change, shown) = (id.clone(), layout.clone(), change.clone(), shown_ids.clone());
            Arc::new(move |before: Option<usize>| {
              let before = before.and_then(|place| shown.get(place)).map(|id| &**id);
              change(layout.clone().with_moved(&id, before));
            })
          };
          let move_by: Arc<dyn Fn(isize) + Send + Sync> = {
            let move_to = move_to.clone();
            let count = shown_ids.len();
            Arc::new(move |by: isize| {
              let to = (column_index as isize + by).clamp(0, count as isize - 1) as usize;
              if to == column_index {
                return;
              }
              // Before the column at its new place going back, before the one after it going on.
              move_to(if to < column_index {
                Some(to)
              } else {
                (to + 1 < count).then_some(to + 1)
              });
            })
          };
          Arc::new(ColumnMenuLink {
            id: id.clone(),
            title: column.title.clone(),
            sortable: column.sortable && sorts,
            sorted,
            pin: column.pin,
            can_hide: self.columns.len() > 1,
            columns: titles
              .iter()
              .map(|(id, title)| {
                let hidden = layout.place(id).is_some_and(|place| place.hidden);
                (id.clone(), title.clone(), !hidden)
              })
              .collect(),
            open,
            host: host.clone(),
            choose,
            set_open,
            move_by,
            move_to,
            place: column_index,
            labels: self.words.menu.clone(),
            filtering: (column.filter.is_some() && change_filters.is_some())
              .then(|| (self.filters.get(&column.id).is_some(), !self.filters.is_empty())),
            filter_labels: self.words.filter.clone(),
            export: export_link.clone(),
            trigger_label: self.words.menu_trigger.as_ref().map_or_else(
              || {
                crate::core::fill(
                  &crate::ui::current_locale().data.column_menu,
                  &[("column", &column.title)],
                )
              },
              |label| label(&column.title),
            ),
          })
        });
        let press = (sortable && (self.write_sort.is_some() || !self.on_sort_change.is_empty())).then(|| {
          let (write, handlers, now, id) = (
            self.write_sort.clone(),
            self.on_sort_change.clone(),
            self.sort.clone(),
            column.id.clone(),
          );
          Arc::new(move |adding: bool| {
            let next = TableSort::after_press(&now, &id, adding);
            if let Some(write) = &write {
              write(next.clone());
            }
            for handler in &handlers {
              handler(&next);
            }
          }) as Arc<dyn Fn(bool) + Send + Sync>
        });
        let resizable = column.resizable.unwrap_or(self.resizable) && !self.disabled;
        let resize = window
          .as_ref()
          .and_then(|window| window.host.clone())
          .filter(|_| resizable)
          .map(|host| {
            let floor = column.track.min.max(LEAST_COLUMN);
            Arc::new(ResizeLink {
              layout: change_layout.clone().and_then(|change| {
                let (layout, _) = &**self.arranged.as_ref()?;
                let layout = layout.clone();
                Some(
                  Arc::new(move |id: &str, width: f32| change(layout.clone().with_width(id, width)))
                    as Arc<dyn Fn(&str, f32) + Send + Sync>,
                )
              }),
              host,
              column: column_index,
              id: column.id.clone(),
              least: floor,
              most: column.track.max,
              extra: geometry.gap * f32::from(u8::from(column_index + 1 < columns_count))
                + if column_index == 0 && sticky && !checkboxes && !numbered {
                  geometry.padding_start
                } else {
                  0.0
                }
                + trail_of(column_index),
              start: Mutex::new(0.0),
              on_resize: self.on_column_resize.clone(),
              on_resize_end: self.on_column_resize_end.clone(),
              label: self.words.resize.as_ref().map_or_else(
                || {
                  crate::core::fill(
                    &crate::ui::current_locale().data.resize_column,
                    &[("column", &column.title)],
                  )
                },
                |label| label(&column.title),
              ),
            })
          });
        TableHeader {
          column: column_index,
          title: column.title.clone(),
          title_hidden: column.title_hidden,
          align: column.align,
          sortable,
          sorted,
          sort_rank,
          filtered: self.filters.get(&column.id).is_some(),
          filter,
          pinned: sticky_of(column_index),
          last: cell_ends(column_index),
          fill: sticky_of(column_index).and(header_fill.clone()),
          gap: geometry.gap,
          lead: if column_index == 0 && sticky && !checkboxes && !numbered {
            geometry.padding_start
          } else {
            0.0
          },
          trail: trail_of(column_index),
          recipe: self
            .resolve(theme, &header_case)
            .owned_like(&recipe)
            .item(id_of(&format!("header-{}", column.id)).as_ref(), TablePart::HeaderCell),
          press,
          resize,
          element: header_refs.get(&column_index).cloned(),
          key: column.id.clone(),
          menu,
          drop,
          id: id_of(&format!("header-{}", column.id)),
        }
      })
      .collect();
    if let Some(host) = menu_host {
      *host.header_refs.lock() = header_refs;
    }

    // Every row's key is looked up once, however many are selected.
    let held: HashSet<&K> = self.selected.iter().collect();
    // How many of the selected rows are shown, from the selection: no walk of every row.
    let held_shown = if held.is_empty() {
      0
    } else {
      keys.shown_among(held.iter().copied())
    };
    let all = keys.len() > 0 && held_shown >= keys.len();
    let some = held_shown > 0;
    let select_all = (checkboxes && !self.disabled).then(|| {
      let model = model.clone();
      Arc::new(move || model.select_shown(!all)) as Arc<dyn Fn() + Send + Sync>
    });

    let total = self.total.unwrap_or(count);
    let selected_count = self.selected.len();
    // Filtered, the count says how many of all are shown.
    let shown_count = lines.passed.as_ref().map_or(total, |passed| passed.len());
    let count_text = match &self.words.count {
      Some(text) => text(TableCount {
        shown: shown_count,
        total,
        selected: selectable.then_some(selected_count),
      }),
      None => {
        let locale = crate::ui::current_locale();
        let words = &locale.data;
        let fill = crate::core::fill;
        let shown = || {
          fill(
            locale.form(&words.shown_rows, total),
            &[("shown", &shown_count.to_string()), ("count", &total.to_string())],
          )
        };
        if shown_count != total && selectable {
          format!(
            "{} · {}",
            shown(),
            locale.count(&locale.common.selected, selected_count)
          )
        } else if shown_count != total {
          shown()
        } else if selectable {
          fill(
            locale.form(&words.selected_rows, total),
            &[("selected", &selected_count.to_string()), ("count", &total.to_string())],
          )
        } else {
          locale.count(&words.rows, total)
        }
      }
    };

    let body_window = window.as_ref().map(|window| BodyWindow {
      row_height: window.row_height,
      count: line_count,
      heights: window.host.as_ref().map(|host| host.state.heights.clone()),
    });
    let empty = (count == 0 && !self.loading).then(|| {
      self.empty.clone().unwrap_or_else(|| {
        EmptyState::new(&self.words.empty)
          .icon(icons::INBOX)
          .size(EmptyStateSize::Sm)
          .build(theme)
      })
    });

    TableView {
      filter_row: self.filter_row && grid_host.is_some(),
      headers,
      rows,
      groups,
      grouped,
      details,
      summaries,
      summary,
      numbered,
      number_lead,
      numbers_label: self.words.numbers.clone(),
      select_rows: (selectable && !self.disabled && numbered).then(|| {
        let model = model.clone();
        Arc::new(move || model.select_shown(!all)) as Arc<dyn Fn() + Send + Sync>
      }),
      selectable,
      checkboxes,
      grid: grid_model.is_some(),
      pinned_start: sticky,
      pinned_end: end_pins > 0,
      lead_inset,
      selection: match (all, some) {
        (true, _) => Some(true),
        (false, true) => None,
        (false, false) => Some(false),
      },
      select_all,
      select_all_label: self.words.select_all.clone(),
      loading: self.loading,
      loading_rows: self.loading_rows,
      empty,
      footer: footer.then_some(count_text),
      pagination: self.pagination.clone(),
      label: self.label.clone(),
      total,
      height: self.height,
      max_height: self.max_height,
      disabled: self.disabled,
      last_row: row_recipe(false, !footer),
      recipe,
      geometry,
      tracks,
      columns_built,
      window: body_window,
      scroll: window.as_ref().map(|window| window.scroll.clone()),
      on_scroll: window.as_ref().and_then(|window| window.on_scroll.clone()),
      focus_rows: window.as_ref().map(|window| window.rows.clone()),
      scroll_ref: window
        .as_ref()
        .and_then(|window| window.host.as_ref())
        .map(|host| host.state.scroll_ref.clone()),
      id: self.id.clone(),
      key: self.key.clone(),
    }
  }
}

impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> From<Table<R, K>> for Element {
  fn from(table: Table<R, K>) -> Self {
    table.build(&current_theme())
  }
}

/// What a column says of a row as text: its text, or the value its editor edits, or nothing for a cell the app
/// builds. What the rows are grouped by.
fn column_text<R>(column: &TableColumn<R>, row: &R) -> String {
  if let Content::Text(text) = &column.content {
    return text(row);
  }
  match column.editor.as_ref().map(|editor| &editor.kind) {
    Some(EditorKind::Text(text)) => text(row),
    Some(EditorKind::Number(number)) => number(row).to_string(),
    Some(EditorKind::Choice(choices, chosen)) => choices
      .get(chosen(row))
      .map(|choice| choice.to_string())
      .unwrap_or_default(),
    Some(EditorKind::Checkbox(checked)) => checked(row).to_string(),
    None => String::new(),
  }
}

/// The numbers of the recipe a table lays its tracks out with, in pixels.
#[derive(Clone, Copy, Debug, Default)]
pub struct Geometry {
  /// The room between two columns.
  pub gap: f32,
  /// The padding at the start of a row.
  pub padding_start: f32,
  /// The padding at the end of a row.
  pub padding_end: f32,
  /// The width of the column of checkboxes.
  pub select: f32,
}

impl Geometry {
  fn of(recipe: &ResolvedRecipe) -> Self {
    let theme = recipe.theme();
    let spacing =
      |value: Option<crate::node::SpacingValue>| value.map(|value| value.resolve(&theme.spacing(), 0.0)).unwrap_or(0.0);
    let row = recipe.part(TablePart::Row).rest();
    let select = recipe.part(TablePart::Select).rest();
    Self {
      gap: spacing(row.gap),
      padding_start: spacing(row.padding_start.or(row.padding_x)),
      padding_end: spacing(row.padding_end.or(row.padding_x)),
      select: px(select.width, theme).unwrap_or(16.0),
    }
  }
}

/// A length of the recipe in pixels, where it is one.
fn px(length: Option<Length>, theme: &Theme) -> Option<f32> {
  match length?.resolve(theme)? {
    Dimension::Px(px) => Some(px),
    _ => None,
  }
}

/// The rows and the selection, which every row's behavior reads.
struct Model<K> {
  keys: RowKeys<K>,
  selected: Vec<K>,
  select: Option<Arc<dyn Fn(Vec<K>) + Send + Sync>>,
  on_selection_change: Vec<SelectionHeard<K>>,
  on_row_click: Vec<Told<K>>,
  on_row_activate: Vec<Told<K>>,
}

/// How a press or a key selects a row.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Pick {
  /// That row alone.
  Only,
  /// The row in or out of what is selected, the rest as it is.
  Toggle,
  /// The rows from the last selected one to it.
  Range,
}

impl<K: Clone + Eq + Hash + Send + Sync + 'static> Model<K> {
  fn actions(self: &Arc<Self>, index: usize) -> Arc<RowActions> {
    let (pick, click, activate, all) = (self.clone(), self.clone(), self.clone(), self.clone());
    Arc::new(RowActions {
      pick: Arc::new(move |how| pick.pick(index, how)),
      click: Arc::new(move || {
        for handler in &click.on_row_click {
          handler(&click.keys.at(index));
        }
      }),
      activate: Arc::new(move || {
        for handler in &activate.on_row_activate {
          handler(&activate.keys.at(index));
        }
      }),
      select_all: Arc::new(move || all.select_shown(true)),
      selectable: self.select.is_some(),
    })
  }

  fn pick(&self, index: usize, pick: Pick) {
    let key = &self.keys.at(index);
    let next = match pick {
      Pick::Only => vec![key.clone()],
      Pick::Toggle => {
        let mut next = self.selected.clone();
        match next.iter().position(|held| held == key) {
          Some(at) => {
            next.remove(at);
          }
          None => next.push(key.clone()),
        }
        next
      }
      Pick::Range => {
        // From the last one selected to this one, as the rows are shown: the key of each is reckoned, and no other.
        let Some(rank) = self.keys.rank_of(key) else {
          return;
        };
        let anchor = self
          .selected
          .last()
          .and_then(|last| self.keys.rank_of(last))
          .unwrap_or(rank);
        let (start, end) = (anchor.min(rank), anchor.max(rank));
        // What was selected outside the range stays; the anchor is last, where the next range starts.
        let span = self.keys.ranked(start, end);
        let range: HashSet<&K> = span.iter().collect();
        let mut next: Vec<K> = self
          .selected
          .iter()
          .filter(|held| !range.contains(held))
          .cloned()
          .collect();
        let anchor_key = self.keys.get(anchor);
        next.extend(span.iter().filter(|at| Some(*at) != anchor_key.as_ref()).cloned());
        next.extend(anchor_key);
        next
      }
    };
    self.write(next);
  }

  /// Every row the table shows selected, or none of them, the rest of the selection as it is.
  fn select_shown(&self, all: bool) {
    let mut next: Vec<K> = self
      .selected
      .iter()
      .filter(|held| self.keys.rank_of(held).is_none())
      .cloned()
      .collect();
    if all {
      next.extend((0..self.keys.len()).filter_map(|rank| self.keys.get(rank)));
    }
    self.write(next);
  }

  /// Asks for `next` to be selected and tells every handler of it: nothing where it is what is selected now.
  fn write(&self, next: Vec<K>) {
    if let Some(select) = &self.select
      && next != self.selected
    {
      select(next.clone());
      for handler in &self.on_selection_change {
        handler(&next);
      }
    }
  }
}

/// What a row that takes input does, with the type of the table's keys out of sight.
struct RowActions {
  pick: Arc<dyn Fn(Pick) + Send + Sync>,
  click: Arc<dyn Fn() + Send + Sync>,
  activate: Arc<dyn Fn() + Send + Sync>,
  select_all: Arc<dyn Fn() + Send + Sync>,
  selectable: bool,
}

/// The rows a mounted table builds, and what its host does for them.
#[derive(Clone)]
struct Window {
  /// The places of the rows built, in order.
  indices: Vec<usize>,
  /// The row that has the focus, which stays built wherever the rows are scrolled.
  focused: Option<usize>,
  row_height: f32,
  scroll: ScrollState,
  refs: HashMap<usize, ElementRef>,
  /// Which columns are built, where the table builds those in view only: `None` for every one.
  columns: Option<Arc<Vec<bool>>>,
  /// The refs of the cells of a grid, by row and column, and of the editor of the cell being edited.
  cell_refs: HashMap<(usize, usize), ElementRef>,
  editor_ref: ElementRef,
  host: Option<Arc<HostLink>>,
  /// The rows as the body's focus group steps over them, built or not.
  rows: Arc<dyn FocusRows>,
  /// The lines the table shows: its rows, or its groups and the rows of the open ones.
  lines: Arc<Lines>,
  on_scroll: Option<Arc<dyn Fn(ScrollEvent) + Send + Sync>>,
  /// The search's matches and the current one.
  search: Option<(Arc<Vec<Found>>, usize)>,
}

/// Where the rows of a mounted table are: the room the rows that are not built take.
#[derive(Clone, Debug)]
pub struct BodyWindow {
  /// The height of a row: what a row not yet built is taken to be.
  pub row_height: f32,
  /// How many rows there are.
  pub count: usize,
  /// What the rows built measured, where they are not all as high.
  heights: Option<Arc<Mutex<Heights>>>,
}

impl BodyWindow {
  /// The room the lines from `from` up to `to` take: what those built measured, the rest a row's height each.
  pub fn room(&self, from: usize, to: usize) -> f32 {
    match &self.heights {
      Some(heights) => {
        let heights = heights.lock();
        heights.top(to) - heights.top(from)
      }
      None => to.saturating_sub(from) as f32 * self.row_height,
    }
  }
}

/// A table as a renderer sees it: its headers, the rows it builds, and the style of its parts.
pub struct TableView {
  /// The headers of the columns, in order.
  pub headers: Vec<TableHeader>,
  /// The rows built, in order: every row, or those of a mounted table in view and around it.
  pub rows: Vec<TableRow>,
  /// The groups' rows built, where the rows are grouped: each says its place among the lines, as a row does.
  pub groups: Vec<TableGroup>,
  /// Whether the rows are grouped: the body is a nested list, Right opening a group and Left closing it.
  pub grouped: bool,
  /// The details of the rows open that are built, each with its line.
  details: Vec<(usize, Element)>,
  /// The summary rows at the end of the groups open that are built, each with its line.
  pub summaries: Vec<TableSummary>,
  /// The grid's summary row, which stays at its bottom.
  pub summary: Option<TableSummary>,
  /// Whether a grid has the column of its rows' numbers at its start, and how wide its track is.
  pub numbered: bool,
  /// Whether it has a row of filters under its header ([`TableView::filter_row`]).
  filter_row: bool,
  pub number_lead: f32,
  /// What a press of the numbers' header does: every row selected, or none where all are.
  select_rows: Option<Arc<dyn Fn() + Send + Sync>>,
  /// The mark of the numbers' header.
  numbers_label: Arc<str>,
  /// Whether its rows are selected.
  pub selectable: bool,
  /// Whether it has the column of checkboxes.
  pub checkboxes: bool,
  /// Whether its cells take the focus and are edited: a mounted data grid.
  pub grid: bool,
  /// Whether the cells at the start stay while the rows scroll sideways ([`ColumnPin::Start`]).
  pub pinned_start: bool,
  /// Whether the cells at the end stay ([`ColumnPin::End`]).
  pub pinned_end: bool,
  /// Where the first column stays from the start, after the checkbox's.
  pub lead_inset: f32,
  /// Whether every row is selected: `Some(true)` all, `Some(false)` none, `None` some.
  pub selection: Option<bool>,
  select_all: Option<Arc<dyn Fn() + Send + Sync>>,
  /// The name of the header's checkbox.
  select_all_label: Arc<str>,
  pub loading: bool,
  pub loading_rows: usize,
  /// What stands under the header where there are no rows.
  pub empty: Option<Element>,
  /// What the footer says, where the table has one.
  pub footer: Option<String>,
  pub pagination: Option<Element>,
  pub label: Option<Arc<str>>,
  /// How many rows there are in all.
  pub total: usize,
  pub height: Option<Length>,
  pub max_height: Option<Length>,
  pub disabled: bool,
  /// The style of each part of the table, its rows at rest.
  pub recipe: ResolvedRecipe,
  /// The style of the last row, with nothing under it in the box where the table has no footer.
  pub last_row: ResolvedRecipe,
  pub geometry: Geometry,
  /// The tracks of the columns, which the header and every row lay their cells out in.
  pub tracks: ColumnTracks,
  /// Which columns are built, where a mounted table builds those in view only: `None` for every one.
  pub columns_built: Option<Arc<Vec<bool>>>,
  /// Where the rows a mounted table does not build are.
  pub window: Option<BodyWindow>,
  scroll: Option<ScrollState>,
  on_scroll: Option<Arc<dyn Fn(ScrollEvent) + Send + Sync>>,
  focus_rows: Option<Arc<dyn FocusRows>>,
  /// What a mounted grid knows its view by, where a drag of cells past its edge scrolls it.
  scroll_ref: Option<ElementRef>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl TableView {
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    self.recipe.part(part)
  }

  pub fn theme(&self) -> &Theme {
    self.recipe.theme()
  }

  /// The box in the style of its part, with nothing in it yet. It is a table to what reads the tree of the
  /// window, a grid where its rows are selected, with how many rows (the header's among them) and columns.
  pub fn root(&self) -> Column {
    // The tracks are the columns and the checkboxes': there however a renderer took the headers.
    let columns = self.tracks.len();
    let mut root = Column::new()
      .align_items(Alignment::Stretch)
      .part(&self.part(TablePart::Root))
      .describe("a11y_role", if self.selectable || self.grid { "grid" } else { "table" })
      .describe("a11y_rowcount", (self.total + 1).to_string())
      .describe("a11y_colcount", columns.to_string());
    if let Some(label) = &self.label {
      root = root.describe("a11y_name", label.clone());
    }
    if self.selectable {
      root = root.describe("a11y_multiselectable", "true");
    }
    if self.loading {
      root = root.describe("a11y_busy", "true");
    }
    if let Some(height) = self.height.and_then(|height| height.resolve(self.theme())) {
      root = root.height(height);
    }
    if let Some(height) = self.max_height.and_then(|height| height.resolve(self.theme())) {
      root = root.max_height(height);
    }
    if let Some(id) = &self.id {
      root = root.id(id.clone());
    }
    if let Some(key) = &self.key {
      root = root.key(key.clone());
    }
    root
  }

  /// The header row: the header's checkbox, then a header for each column, in the tracks of the columns. It
  /// stays at the top while the rows scroll.
  pub fn header(&mut self) -> Row {
    let part = self.part(TablePart::Header);
    let mut header = Row::new()
      .part(&part)
      .spacing(0.0)
      // Each cell is as high as the row, so that one that stays covers what scrolls under it, and sets what it
      // holds in the middle of that height itself.
      .align_items(Alignment::Stretch)
      .column_tracks(&self.tracks)
      .sticky(Sticky::top(0.0))
      .focus_group(FocusGroup::horizontal().one_stop(true).wrap(false))
      .describe("a11y_role", "row")
      .describe("a11y_rowindex", "1");
    if self.pinned_start {
      header = header.padding_left(0.0);
    }
    if self.pinned_end {
      header = header.padding_right(0.0);
    }
    if self.numbered {
      header = header.child(self.number_header());
    }
    if self.checkboxes {
      header = header.child(self.select_all());
    }
    let headers = std::mem::take(&mut self.headers);
    let placed = headers.into_iter().map(|header| (header.column, header.render()));
    header.with_children(in_tracks(
      self.columns_built.as_ref().map(|built| built.as_slice()),
      placed,
    ))
  }

  /// The header's checkbox, in the cell of the column of checkboxes: checked where every row is selected,
  /// mixed where some are. A press selects every row, or none where all are.
  pub fn select_all(&self) -> Element {
    let part = self.part(TablePart::Select);
    let mut checkbox = Checkbox::new(&self.select_all_label)
      .label_hidden(true)
      .checked(self.selection == Some(true))
      .indeterminate(self.selection.is_none())
      .disabled(self.disabled);
    if let Some(press) = self.select_all.clone() {
      checkbox = checkbox.on_change(move |_| press());
    }
    if let Some(id) = &self.id {
      checkbox = checkbox.id(format!("{id}-select-all"));
    }
    let mut cell =
      lead_cell(&part, self.geometry, self.pinned_start && !self.numbered).child(checkbox.build(self.theme()));
    if self.pinned_start {
      // It covers what scrolls under it, in the header's fill.
      cell = cell.sticky(Sticky::left(self.number_lead));
      if let Some(fill) = self.part(TablePart::Header).rest().background {
        cell = cell.background(fill);
      }
    }
    cell.into()
  }

  /// The header of the column of the rows' numbers: a press selects every row, or none where all are.
  pub fn number_header(&self) -> Element {
    let part = self.part(TablePart::RowNumber);
    let mut cell = Row::new()
      .part(&part)
      .align_items(Alignment::Center)
      .justify(Justify::End)
      .padding_left(self.geometry.padding_start)
      .padding_right(self.geometry.gap)
      .sticky(Sticky::left(0.0))
      .describe("a11y_role", "columnheader")
      .describe("a11y_name", self.select_all_label.clone())
      .child(part.text(&self.numbers_label).nowrap());
    if let Some(fill) = self.part(TablePart::Header).rest().background {
      cell = cell.background(fill);
    }
    if let Some(press) = self.select_rows.clone() {
      cell = cell.on_click(move |event: MouseEvent| {
        event.stop_propagation();
        press();
      });
    }
    if let Some(id) = &self.id {
      cell = cell.id(format!("{id}-numbers"));
    }
    cell.into()
  }

  /// The rows built in their column, one stop of Tab that the arrow keys move through, with the room of the
  /// rows a mounted table does not build before and after them.
  pub fn body(&mut self) -> Column {
    // The rows and the groups' rows in the order of their lines.
    let mut lines: Vec<(usize, Element)> = std::mem::take(&mut self.rows)
      .into_iter()
      .map(|row| (row.position, row.render()))
      .chain(
        std::mem::take(&mut self.groups)
          .into_iter()
          .map(|group| (group.position, group.render())),
      )
      .chain(
        std::mem::take(&mut self.summaries)
          .into_iter()
          .map(|summary| (summary.position.unwrap_or(0), summary.render())),
      )
      .chain(std::mem::take(&mut self.details))
      .collect();
    lines.sort_by_key(|(position, _)| *position);
    let group = FocusGroup::vertical().one_stop(true).wrap(false).nested(self.grouped);
    let mut body = Column::new()
      .align_items(Alignment::Stretch)
      .describe("a11y_role", "rowgroup");
    let (Some(window), Some(focus_rows)) = (self.window.clone(), self.focus_rows.clone()) else {
      // Every row is built: the group steps over the rows themselves, PageUp and PageDown by what the table's
      // scroll shows.
      return body
        .focus_group(group.pages(true))
        .with_children(lines.into_iter().map(|(_, line)| line));
    };
    // A mounted table builds the rows around its view: the group steps over all of them, and a row it goes to
    // that is not built is revealed and focused once it is.
    body = body.focus_group(group).focus_rows(focus_rows);
    let spacer = |from: usize, to: usize| Row::new().height(window.room(from, to)).key(format!("before-{to}"));
    let mut next = 0;
    for (position, line) in lines {
      if position > next {
        body = body.child(spacer(next, position));
      }
      next = position + 1;
      body = body.child(line);
    }
    if window.count > next {
      body = body.child(spacer(next, window.count));
    }
    body
  }

  /// Rows of skeletons in the tracks of the columns, where the rows will be: a line in each cell, the first
  /// column's of a length that changes from row to row. A line is a share of its cell, so it ends inside a
  /// column of any width: the board's lengths (150, 190, 120 and 170 in a name of 176; 70 in a column of 90) as
  /// shares of the columns it draws them in.
  pub fn skeletons(&self) -> Column {
    const FIRST: [f32; 4] = [0.85, 1.0, 0.68, 0.965];
    const OTHER: f32 = 0.78;
    let part = self.part(TablePart::Row);
    let cell = self.part(TablePart::Cell);
    let columns = self.tracks.len() - usize::from(self.checkboxes) - usize::from(self.numbered);
    Column::new()
      .align_items(Alignment::Stretch)
      .with_children((0..self.loading_rows).map(|index| {
        // The last row's part, with no line under it, is its only one: a part given after the tracks would take
        // them away, and the row would fall out of the columns.
        let row_part = if self.loading_rows == index + 1 && self.footer.is_none() {
          self.last_row.part(TablePart::Row)
        } else {
          part.clone()
        };
        let mut row = Row::new()
          .part(&row_part)
          .spacing(0.0)
          .align_items(Alignment::Stretch)
          .column_tracks(&self.tracks)
          .describe("a11y_hidden", "true");
        if self.numbered {
          row = row.child(Row::new());
        }
        if self.checkboxes {
          row = row.child(Row::new());
        }
        row.with_children((0..columns).map(|column| {
          let share = if column == 0 { FIRST[index % FIRST.len()] } else { OTHER };
          let line = Row::new()
            .width(Dimension::Pct(share * 100.0))
            .child(Skeleton::line().width(Length::Fill).build(self.theme()));
          let mut wrapper = Row::new().part(&cell).align_items(Alignment::Center).child(line);
          if column + 1 < columns {
            wrapper = wrapper.padding_right(self.geometry.gap);
          }
          wrapper
        }))
      }))
  }

  /// What stands under the header where there are no rows, as wide as the table.
  pub fn empty(&mut self) -> Option<Row> {
    let empty = self.empty.take()?;
    Some(
      Row::new()
        .justify(Justify::Center)
        .part(&self.part(TablePart::Empty))
        .child(empty),
    )
  }

  /// The footer: the count of the rows at its start, the pagination at its end.
  pub fn footer(&mut self) -> Option<Row> {
    let text = self.footer.take()?;
    let count = self.part(TablePart::Count);
    // In a table too narrow for the count and the pagination on one line, the pagination is under the count.
    let mut footer = Row::new()
      .wrap()
      .align_items(Alignment::Center)
      .justify(Justify::SpaceBetween)
      .part(&self.part(TablePart::Footer))
      .child(Row::new().part(&count).child(count.text(&text).nowrap()));
    if let Some(pagination) = self.pagination.take() {
      footer = footer.child(pagination);
    }
    Some(footer)
  }

  /// How the default renderer builds a table: the header and the rows (or the empty state, or the skeletons)
  /// scrolling both ways in the box, and the footer under them.
  pub fn render(mut self) -> Element {
    let mut content = Column::new()
      .align_items(Alignment::Stretch)
      .measures_column_tracks(&self.tracks);
    // The row of filters is made from the headers, before the header takes them.
    let filters = self.filter_row();
    content = content.child(self.header());
    if let Some(filters) = filters {
      content = content.child(filters);
    }
    // The empty state is in the middle of the table's box, not of its columns, which may be wider: it stands
    // under what scrolls, which holds the header alone.
    let empty = if self.loading { None } else { self.empty() };
    if self.loading {
      content = content.child(self.skeletons());
    } else if empty.is_none() {
      content = content.child(self.body());
      // The summary of every row stays at the bottom of what scrolls, under the rows.
      if let Some(summary) = self.summary.take() {
        content = content.child(summary.render());
      }
    }
    // Its bars run beside the rows: the one down under the header that stays, the one across after the first
    // column that stays.
    let mut scroll = ScrollBoth::new(content)
      .fill_width(true)
      .scrollbars_clear_of_sticky(true)
      // The bar across stands in a strip of its own under the rows while they are wider than the view: no row
      // is ever under it, and a table whose columns fit has no strip.
      .reserve_bar_across(true)
      .part(&self.part(TablePart::Scroll));
    if let Some(empty) = empty {
      let mut root = self.root().child(scroll.flex_full(0.0, 1.0, None));
      let mut empty = Column::new().align_items(Alignment::Stretch).child(empty);
      if self.height.is_some() {
        empty = empty.flex(1.0).justify(Justify::Center);
      }
      root = root.child(empty);
      if let Some(footer) = self.footer() {
        root = root.child(footer);
      }
      return root.into();
    }
    if self.height.is_some() {
      scroll = scroll.flex_full(1.0, 1.0, None);
    } else {
      scroll = scroll.flex_full(0.0, 1.0, None);
    }
    if let Some(state) = self.scroll.clone() {
      scroll = scroll.with_scroll_state(state);
    }
    if let Some(element) = self.scroll_ref.clone() {
      scroll = scroll.ref_element(element);
    }
    if let Some(on_scroll) = self.on_scroll.clone() {
      scroll = scroll.on_scroll(move |event: ScrollEvent| on_scroll(event));
    }
    let mut root = self.root().child(scroll);
    if let Some(footer) = self.footer() {
      root = root.child(footer);
    }
    root.into()
  }
}

/// The cells of a row, each with the column it is of, in the tracks of the columns: where a run of columns is
/// not built (`built` says so), one room spans their tracks in place of a cell each.
pub fn in_tracks(built: Option<&[bool]>, cells: impl IntoIterator<Item = (usize, Element)>) -> Vec<Element> {
  let mut placed = Vec::new();
  let mut next = 0;
  let room = |from: usize, to: usize, placed: &mut Vec<Element>| {
    if to > from {
      placed.push(Row::new().track_span(to - from).key(format!("columns-{from}")).into());
    }
  };
  for (column, cell) in cells {
    room(next, column, &mut placed);
    placed.push(cell);
    next = column + 1;
  }
  if let Some(built) = built {
    room(next, built.len(), &mut placed);
  }
  placed
}

/// The cell of the column of checkboxes, the header's or a row's: as wide as its track, its padding at the
/// start where the cells at the start stay.
fn lead_cell(part: &ResolvedPart, geometry: Geometry, sticky: bool) -> Row {
  let mut cell = Row::new()
    .align_items(Alignment::Center)
    .part(part)
    .width(Dimension::Auto)
    .padding_right(geometry.gap);
  if sticky {
    cell = cell.padding_left(geometry.padding_start);
  }
  cell
}

/// The header of one column as a renderer sees it.
pub struct TableHeader {
  /// Which column it is of, from 0.
  pub column: usize,
  pub title: Arc<str>,
  /// Whether its title is said to what reads the tree and not drawn: the column of a row's actions.
  pub title_hidden: bool,
  pub align: ColumnAlign,
  /// Whether a press of it sorts the rows.
  pub sortable: bool,
  /// Which way the rows are sorted by it, if they are.
  pub sorted: Option<SortDirection>,
  /// Its place in the sort, from 1, where the rows are sorted by more than one column.
  pub sort_rank: Option<usize>,
  /// Whether the rows are filtered by its column.
  pub filtered: bool,
  filter: Option<Arc<FilterLink>>,
  /// Where it stays while the rows scroll sideways, if its column is pinned.
  pub pinned: Option<Sticky>,
  /// Whether it is the last column, with no room after it.
  pub last: bool,
  /// The fill of a header that stays: the header row's, over which the others scroll.
  pub fill: Option<BackgroundColor>,
  gap: f32,
  lead: f32,
  trail: f32,
  /// The style of each part of the header, in its case.
  pub recipe: ResolvedRecipe,
  press: Option<Arc<dyn Fn(bool) + Send + Sync>>,
  resize: Option<Arc<ResizeLink>>,
  /// Where a mounted grid finds the header, which a column dragged by its header goes before.
  element: Option<ElementRef>,
  /// The column's id, which keeps the header the same element wherever the column is.
  key: Arc<str>,
  menu: Option<Arc<ColumnMenuLink>>,
  /// Where a column dragged by its header goes, drawn on this one.
  drop: Option<DropSide>,
  id: Option<Arc<str>>,
}

impl TableHeader {
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    self.recipe.part(part)
  }

  /// The cell in the column's track: the room after it before the next column, its content where the column
  /// has it. It is a column header to what reads the tree, with which way it is sorted where it sorts.
  pub fn cell(&self) -> Row {
    let mut cell = Row::new()
      // Where a column dragged by its header goes, drawn as a line at its start or its end.
      .part(&self.part(TablePart::DropLine))
      .align_items(Alignment::Center)
      .justify(justify(self.align))
      .describe("a11y_role", "columnheader")
      .describe("a11y_name", self.title.clone())
      // A header is the same element wherever the columns built around it start, and wherever its column is
      // moved to.
      .key(format!("header-{}", self.key));
    if self.sortable || self.sorted.is_some() {
      let sort = match self.sorted {
        Some(SortDirection::Ascending) => "ascending",
        Some(SortDirection::Descending) => "descending",
        None => "none",
      };
      cell = cell.describe("a11y_sort", sort);
    }
    if !self.last {
      cell = cell.padding_right(self.gap);
    }
    if self.trail > 0.0 {
      cell = cell.padding_right(self.trail);
    }
    if self.lead > 0.0 {
      cell = cell.padding_left(self.lead);
    }
    if let Some(pinned) = self.pinned {
      cell = cell.sticky(pinned);
    }
    if let Some(fill) = self.fill.clone() {
      cell = cell.background(fill);
    }
    if let Some(element) = &self.element {
      cell = cell.ref_element(element.clone());
    }
    if self.menu.is_some() {
      // Its menu's trigger shows while the pointer is on the header or the focus is in it.
      cell = cell.interaction_group(true);
    }
    cell
  }

  /// What opens the column's menu, at the end of its header: `None` where the header has no menu. It takes no
  /// focus: Alt+Down on the header opens the menu too.
  pub fn menu_trigger(&self) -> Option<Element> {
    let link = self.menu.clone()?;
    let part = self.part(TablePart::MenuTrigger);
    let toggle = link.clone();
    let mut trigger = pressable(&part, false)
      .focusable(false)
      .align_items(Alignment::Center)
      .describe("a11y_name", link.trigger_label.clone())
      .describe("a11y_haspopup", "menu")
      .describe("a11y_expanded", if link.open { "true" } else { "false" })
      .on_click(move |event: MouseEvent| {
        // A press of it is its own: it does not sort the rows.
        event.stop_propagation();
        (toggle.set_open)(!toggle.open);
      })
      .child(glyph(
        &self.part(TablePart::SortIcon),
        Name::new(icons::MORE_VERTICAL),
        false,
      ));
    if let Some(id) = &self.id {
      trigger = trigger.id(format!("{id}-menu"));
    }
    if link.open {
      trigger = trigger.ref_element(link.host.menu_anchor.clone());
    } else if let Some(anchor) = self.filter_anchor() {
      trigger = trigger.ref_element(anchor);
    }
    Some(trigger.into())
  }

  /// The column's menu, while it is open: the sort, the pin, its width to fit its content, hiding it, and the
  /// columns to show or hide.
  pub fn menu(&self) -> Option<Element> {
    let link = self.menu.clone().filter(|link| link.open)?;
    let choose = |choice: MenuChoice| {
      let choose = link.choose.clone();
      move || choose(choice.clone())
    };
    let set_open = link.set_open.clone();
    let mut menu = Menu::controlled(true, link.host.menu_anchor.clone(), move |open| set_open(open))
      .label(&link.trigger_label)
      .placement(Placement::BottomEnd);
    if let Some(id) = &self.id {
      menu = menu.id(format!("{id}-menu-panel"));
    }
    if link.sortable {
      menu = menu
        .item(
          MenuItem::new(&link.labels.sort_ascending)
            .icon(icons::ARROW_UP)
            .checked(link.sorted == Some(SortDirection::Ascending))
            .on_select(choose(MenuChoice::Sort(Some(SortDirection::Ascending)))),
        )
        .item(
          MenuItem::new(&link.labels.sort_descending)
            .icon(icons::ARROW_DOWN)
            .checked(link.sorted == Some(SortDirection::Descending))
            .on_select(choose(MenuChoice::Sort(Some(SortDirection::Descending)))),
        )
        .item(
          MenuItem::new(&link.labels.clear_sort)
            .icon(icons::CHEVRONS_VERTICAL)
            .disabled(link.sorted.is_none())
            .on_select(choose(MenuChoice::Sort(None))),
        )
        .separator();
    }
    if let Some((filtered, any)) = link.filtering {
      let words = &link.filter_labels;
      menu = menu
        .item(
          MenuItem::new(&words.filter)
            .icon(icons::FILTER)
            .checked(filtered)
            .on_select(choose(MenuChoice::Filter)),
        )
        .item(
          MenuItem::new(&words.clear_filter)
            .icon(icons::CLOSE)
            .disabled(!filtered)
            .on_select(choose(MenuChoice::ClearFilter)),
        )
        .item(
          MenuItem::new(&words.clear_all)
            .disabled(!any)
            .on_select(choose(MenuChoice::ClearAllFilters)),
        )
        .separator();
    }
    let hidden = link.columns.iter().any(|(_, _, shown)| !shown);
    let shown = link.columns.iter().filter(|(_, _, shown)| *shown).count();
    let columns = Menu::new(link.host.submenu_open.clone(), link.host.submenu_anchor.clone())
      .label(&crate::ui::current_locale().data.columns)
      .items(link.columns.iter().map(|(id, title, on)| {
        MenuItem::new(title)
          .checked(*on)
          // The last column shown stays.
          .disabled(*on && shown == 1)
          .on_select(choose(MenuChoice::Toggle(id.clone())))
      }))
      .separator()
      .item(
        MenuItem::new(&link.labels.show_all)
          .disabled(!hidden)
          .on_select(choose(MenuChoice::ShowAll)),
      );
    // The rows written out: those shown, and those selected where there are.
    let export = link.export.as_ref().map(|export| {
      let words = &export.labels;
      Menu::new(link.host.export_open.clone(), link.host.export_anchor.clone())
        .label(&words.export)
        .item(
          MenuItem::new(&words.shown_csv).on_select(choose(MenuChoice::Export(ExportScope::Shown, ExportFormat::Csv))),
        )
        .item(
          MenuItem::new(&words.shown_tsv).on_select(choose(MenuChoice::Export(ExportScope::Shown, ExportFormat::Tsv))),
        )
        .item(
          MenuItem::new(&words.selected_csv)
            .disabled(!export.selected)
            .on_select(choose(MenuChoice::Export(ExportScope::Selected, ExportFormat::Csv))),
        )
        .item(
          MenuItem::new(&words.selected_tsv)
            .disabled(!export.selected)
            .on_select(choose(MenuChoice::Export(ExportScope::Selected, ExportFormat::Tsv))),
        )
    });
    let mut menu = menu
      .item(
        MenuItem::new(&link.labels.pin_start)
          .icon(icons::PIN)
          .checked(link.pin == ColumnPin::Start)
          .on_select(choose(MenuChoice::Pin(ColumnPin::Start))),
      )
      .item(
        MenuItem::new(&link.labels.pin_end)
          .icon(icons::PIN)
          .checked(link.pin == ColumnPin::End)
          .on_select(choose(MenuChoice::Pin(ColumnPin::End))),
      )
      .item(
        MenuItem::new(&link.labels.unpin)
          .icon(icons::CLOSE)
          .disabled(link.pin == ColumnPin::None)
          .on_select(choose(MenuChoice::Pin(ColumnPin::None))),
      )
      .separator()
      .item(
        MenuItem::new(&link.labels.size_to_fit)
          .icon(icons::RESIZE_HORIZONTAL)
          .on_select(choose(MenuChoice::Fit)),
      )
      .item(
        MenuItem::new(&link.labels.hide)
          .icon(icons::EYE_OFF)
          .disabled(!link.can_hide)
          .on_select(choose(MenuChoice::Hide)),
      )
      .item(MenuItem::new(&link.labels.columns).icon(icons::EYE).submenu(columns));
    if let Some(export) = export {
      let label = link.export.as_ref().map_or("", |export| export.labels.export.as_str());
      menu = menu
        .separator()
        .item(MenuItem::new(label).icon(icons::DOWNLOAD).submenu(export));
    }
    Some(menu.build(self.recipe.theme()))
  }

  /// The arrow of a column the rows are sorted by, up or down, and the mark of one that sorts.
  pub fn sort_icon(&self) -> Option<Element> {
    let name = match self.sorted {
      Some(SortDirection::Ascending) => icons::ARROW_UP,
      Some(SortDirection::Descending) => icons::ARROW_DOWN,
      None if self.sortable => icons::CHEVRONS_VERTICAL,
      None => return None,
    };
    Some(glyph(&self.part(TablePart::SortIcon), Name::new(name), false))
  }

  /// Its place in the sort after its arrow, where the rows are sorted by more than one column: `None` otherwise.
  pub fn sort_order(&self) -> Option<Element> {
    let rank = self.sort_rank?;
    let part = self.part(TablePart::SortOrder);
    Some(part.text(&rank.to_string()).part(&part).nowrap().into())
  }

  /// What is dragged at the end of the header to make the column wider or narrower: `None` for a column that is
  /// not resized. It is a separator to what reads the tree, with the column's width as its value; the keys that
  /// resize the column are its header's (Ctrl and Left or Right).
  pub fn resizer(&self) -> Option<Row> {
    let link = self.resize.clone()?;
    let width = link.width();
    let (start, moved, end) = (link.clone(), link.clone(), link.clone());
    Some(
      Row::new()
        .part(&self.part(TablePart::Resizer))
        .justify(Justify::Center)
        .align_items(Alignment::Stretch)
        .height(Dimension::Pct(100.0))
        .interaction_group(true)
        .describe("a11y_role", "separator")
        .describe("a11y_orientation", "vertical")
        .describe("a11y_name", link.label.clone())
        .describe("a11y_valuenow", format!("{}", width.round()))
        .start_drag_buttons(MouseButtonMask::LEFT)
        .on_drag_start(move |_: DragEvent| start.begin())
        .on_drag_move(move |event: DragEvent| moved.drag(event.total_delta_x))
        .on_drag_end(move |_: DragEvent| end.finish())
        // A press of it is its own: it does not sort the rows.
        .on_click(|event: MouseEvent| event.stop_propagation())
        .child(Row::new().part(&self.part(TablePart::ResizeLine))),
    )
  }

  /// How the default renderer builds a header: its title and its arrow, pressed to sort where it sorts, and its
  /// resizer at its end where the column is resized.
  pub fn render(self) -> Element {
    let part = self.part(TablePart::HeaderCell);
    let title = if self.title_hidden {
      None
    } else {
      // It gives way to the room the column has, and ends in an ellipsis.
      Some(
        part
          .text(&self.title)
          .nowrap()
          .text_overflow(crate::node::node_kind::TextOverflow::Elipsis)
          .flex_full(0.0, 1.0, None)
          .min_width(Dimension::Px(0.0)),
      )
    };
    let resizer = self.resizer();
    let mut cell = self.cell();
    let label: Element = match self.press.clone().filter(|_| self.sortable) {
      None => {
        let mut label = Row::new()
          .align_items(Alignment::Center)
          .flex_full(0.0, 1.0, None)
          .min_width(Dimension::Px(0.0))
          .part(&part);
        if let Some(title) = title {
          label = label.child(title);
        }
        if let Some(icon) = self.sort_icon() {
          label = label.child(icon);
        }
        if let Some(order) = self.sort_order() {
          label = label.child(order);
        }
        if let Some(mark) = self.filter_mark() {
          label = label.child(mark);
        }
        // A header that is resized or has a menu and is not sorted is a stop of the header's keys.
        if self.resize.is_some() || self.menu.is_some() {
          label = label.tab_index(0).describe("a11y_name", self.title.clone());
          if let Some(id) = &self.id {
            label = label.id(id.clone());
          }
        }
        label.into()
      }
      Some(press) => {
        let mut button = pressable(&part, false)
          .align_items(Alignment::Center)
          .flex_full(0.0, 1.0, None)
          .min_width(Dimension::Px(0.0))
          .describe(
            "a11y_name",
            crate::core::fill(&crate::ui::current_locale().data.sort_by, &[("column", &self.title)]),
          )
          .on_click(move |event: MouseEvent| press(event.shift));
        if let Some(title) = title {
          button = button.child(title);
        }
        if let Some(icon) = self.sort_icon() {
          button = button.child(icon);
        }
        if let Some(order) = self.sort_order() {
          button = button.child(order);
        }
        if let Some(mark) = self.filter_mark() {
          button = button.child(mark);
        }
        if let Some(id) = &self.id {
          button = button.id(id.clone());
        }
        button.into()
      }
    };
    // A header with a menu is dragged to another place among the columns.
    let label = match &self.menu {
      Some(link) => {
        let (start, moved, end) = (link.clone(), link.clone(), link.clone());
        Row::new()
          .flex_full(0.0, 1.0, None)
          .min_width(Dimension::Px(0.0))
          .align_items(Alignment::Center)
          .start_drag_buttons(MouseButtonMask::LEFT)
          .on_drag_start(move |_: DragEvent| {
            *start.host.header_drop.lock() = Some((start.place, Some(start.place)));
          })
          .on_drag_move(move |event: DragEvent| {
            let to = moved.host.drop_place(event.x);
            let mut held = moved.host.header_drop.lock();
            if held.is_some_and(|(_, at)| at != to) {
              *held = Some((moved.place, to));
              drop(held);
              moved.host.rebuild();
            }
          })
          .on_drag_end(move |_: DragEvent| {
            let held = end.host.header_drop.lock().take();
            end.host.rebuild();
            if let Some((from, to)) = held
              && to != Some(from)
              && to != Some(from + 1)
            {
              (end.move_to)(to);
            }
          })
          .child(label)
          .into()
      }
      None => label,
    };
    let trigger = self.menu_trigger();
    let menu = self.menu();
    let (resize_link, menu_link) = (self.resize.clone(), self.menu.clone());
    if resize_link.is_some() || menu_link.is_some() {
      cell = cell.on_key_down(move |event: KeyboardEvent| {
        if !event.focus_within {
          return;
        }
        let command = event.ctrl || event.meta;
        if let Some(link) = &menu_link {
          // Alt+Down, the menu key or Shift+F10 opens the column's menu; Ctrl+Shift+PageUp and PageDown move the
          // column, as a browser moves a tab.
          let key = event.key.as_str();
          if (event.alt && !command && key == "ArrowDown") || key == "ContextMenu" || (event.shift && key == "F10") {
            event.prevent_default();
            (link.set_open)(true);
            return;
          }
          if command && event.shift && !event.alt && matches!(key, "PageUp" | "PageDown") {
            event.prevent_default();
            (link.move_by)(if key == "PageUp" { -1 } else { 1 });
            return;
          }
        }
        let Some(link) = &resize_link else {
          return;
        };
        if !command || event.alt {
          return;
        }
        let step = if event.shift { RESIZE_STEP_SHIFT } else { RESIZE_STEP };
        let by = match event.key.as_str() {
          "ArrowLeft" => -step,
          "ArrowRight" => step,
          _ => return,
        };
        event.prevent_default();
        link.step(by);
      });
    }
    if resizer.is_none() && trigger.is_none() {
      return cell.child(label).into();
    }
    if resizer.is_some() {
      // The label takes the room the resizer leaves, which stands in the room after the column, on its edge.
      let half = px(self.part(TablePart::Resizer).rest().width, self.recipe.theme()).unwrap_or(8.0) / 2.0;
      let after = if self.last { self.trail } else { self.gap };
      cell = cell.padding_right((after / 2.0 - half).max(0.0));
    }
    // The title where its column puts it, in the room the trigger at the header's end leaves.
    let titled = Row::new()
      .flex_full(1.0, 1.0, Some(0.0))
      .min_width(Dimension::Px(0.0))
      .justify(justify(self.align))
      .align_items(Alignment::Center)
      .child(label);
    match trigger {
      Some(trigger) => {
        cell = cell.child(
          Row::new()
            .flex_full(1.0, 1.0, Some(0.0))
            .min_width(Dimension::Px(0.0))
            .align_items(Alignment::Center)
            .spacing(4.0)
            .child(titled)
            .child(trigger),
        );
      }
      None => cell = cell.child(titled),
    }
    if let Some(resizer) = resizer {
      cell = cell.child(resizer);
    }
    if let Some(menu) = menu {
      cell = cell.child(menu);
    }
    if let Some(panel) = self.filter_panel() {
      cell = cell.child(panel);
    }
    cell.into()
  }
}

/// A column's resizer and its table's host: what a drag or a key makes the column, and who is told.
struct ResizeLink {
  /// What keeps the width a person made the column in a grid's arrangement of its columns.
  layout: Option<Arc<dyn Fn(&str, f32) + Send + Sync>>,
  host: Arc<HostLink>,
  column: usize,
  id: Arc<str>,
  /// The least and the most a person makes the column.
  least: f32,
  most: f32,
  /// The room of its track that is not the column's own: the gap after it, and the row's padding it holds.
  extra: f32,
  /// The column's width when the drag started.
  start: Mutex<f32>,
  on_resize: Vec<ResizeHeard>,
  on_resize_end: Vec<ResizeHeard>,
  label: String,
}

impl ResizeLink {
  /// The column's width now: its track's, less what of it is not the column's own.
  fn width(&self) -> f32 {
    (self.host.track_width(self.column) - self.extra).max(0.0)
  }

  fn begin(&self) {
    *self.start.lock() = self.width();
  }

  fn drag(&self, by: f32) {
    let start = *self.start.lock();
    self.set(start + by);
  }

  fn finish(&self) {
    let width = self.width();
    if (width - *self.start.lock()).abs() > 0.01 {
      self.keep(width);
      for handler in &self.on_resize_end {
        handler(&self.id, width);
      }
    }
  }

  /// The width a person made the column, kept in the grid's arrangement of its columns, which is then what it
  /// is drawn at.
  fn keep(&self, width: f32) {
    if let Some(layout) = &self.layout {
      layout(&self.id, width);
      self.host.state.resized.lock().remove(&self.id);
    }
  }

  /// A key: narrower or wider by `by`, and that is the end of it.
  fn step(&self, by: f32) {
    let before = self.width();
    let after = self.set(before + by);
    if (after - before).abs() > 0.01 {
      self.keep(after);
      for handler in &self.on_resize_end {
        handler(&self.id, after);
      }
    }
  }

  /// Makes the column `width`, between its least and its most: its width now.
  fn set(&self, width: f32) -> f32 {
    let width = width.clamp(self.least, self.most.max(self.least));
    if (width - self.width()).abs() > 0.01 && self.host.resize(&self.id, width) {
      for handler in &self.on_resize {
        handler(&self.id, width);
      }
    }
    width
  }
}

fn justify(align: ColumnAlign) -> Justify {
  match align {
    ColumnAlign::Start => Justify::Start,
    ColumnAlign::Center => Justify::Center,
    ColumnAlign::End => Justify::End,
  }
}

/// One cell of a row as a renderer sees it.
pub struct TableCell {
  /// Which column it is of, from 0.
  pub column: usize,
  /// What the column built from the row.
  pub content: Element,
  /// What it is and does in a data grid: `None` in a table.
  pub grid: Option<GridCell>,
  /// The style of the cell, in the case of its column and its row.
  pub part: ResolvedPart,
  pub align: ColumnAlign,
  /// Where it stays while the rows scroll sideways, if its column is pinned.
  pub pinned: Option<Sticky>,
  /// Whether a press in it is its own, not the row's.
  pub controls: bool,
  /// Whether it is the last of the row, with no room after it.
  pub last: bool,
  /// The fill of a cell that stays: the row's, over which the others scroll.
  pub fill: Option<BackgroundColor>,
  gap: f32,
  lead: f32,
  trail: f32,
}

impl TableCell {
  /// How the default renderer builds a cell: its content where its column has it, in the room of its
  /// track. What takes the focus in it is out of the arrow keys of the rows: Tab reaches it from its row.
  pub fn render(mut self) -> Element {
    let mut cell = Row::new()
      .align_items(Alignment::Center)
      .justify(justify(self.align))
      .part(&self.part)
      // A cell is the same element wherever the columns built around it start.
      .key(format!("cell-{}", self.column));
    match self.grid.take() {
      // A cell of a grid is a stop of the grid's keys, and holds its editor while it is edited.
      Some(mut grid) => {
        let editor = grid.editor.take();
        cell = grid
          .dress(self.column, cell, editor.is_some())
          .child(editor.unwrap_or(self.content));
      }
      None => {
        cell = cell
          .describe("a11y_role", "cell")
          .focus_group(FocusGroup::none())
          .child(self.content)
      }
    }
    if !self.last {
      cell = cell.padding_right(self.gap);
    }
    if self.trail > 0.0 {
      cell = cell.padding_right(self.trail);
    }
    if self.lead > 0.0 {
      cell = cell.padding_left(self.lead);
    }
    if let Some(pinned) = self.pinned {
      cell = cell.sticky(pinned);
    }
    if let Some(fill) = self.fill {
      cell = cell.background(fill);
    }
    if self.controls {
      cell = cell.on_click(|event: MouseEvent| event.stop_propagation());
    }
    cell.into()
  }
}

/// The content of a row's first cell in a tree, or of a row with a detail: indented 18 a level, after the chevron
/// that opens and closes it (right closed, down open), or the chevron's room where it does not open; 6 between.
fn tree_cell(
  part: &ResolvedPart,
  level: u32,
  open: Option<bool>,
  rank: usize,
  toggle: Arc<dyn Fn(usize, bool) + Send + Sync>,
  content: Element,
  wraps: bool,
) -> Element {
  let side = part.rest().width.unwrap_or(Length::Icon(IconSize::Sm));
  let chevron: Element = match open {
    Some(open) => Row::new()
      .part(part)
      .align_items(Alignment::Center)
      .justify(Justify::Center)
      .child(
        super::Icon::new(Name::new(if open {
          icons::CHEVRON_DOWN
        } else {
          icons::CHEVRON_RIGHT
        }))
        .size(side)
        .build(part.theme()),
      )
      .on_click(move |event: MouseEvent| {
        // A press of it is its own: it does not select or activate the row.
        event.stop_propagation();
        toggle(rank, !open);
      })
      .into(),
    None => Row::new().part(part).into(),
  };
  let mut cell = Row::new()
    .align_items(Alignment::Center)
    .spacing(6.0)
    .padding_left(18.0 * level as f32)
    .child(chevron)
    .child(content);
  // In the room of its cell: a text that wraps takes all of it, and one on a line gives way to it.
  cell = if wraps {
    cell.flex_full(1.0, 1.0, Some(0.0)).min_width(Dimension::Px(0.0))
  } else {
    cell.flex_full(0.0, 1.0, None).min_width(Dimension::Px(0.0))
  };
  cell.into()
}

/// One row as a renderer sees it: where it is, whether it is selected, its cells, the style of its parts,
/// and its behavior ready to be dressed.
pub struct TableRow {
  /// Its place among the rows, from 0.
  pub index: usize,
  /// Where the rows are a tree: its level, from 1, and whether it is open where rows are under it.
  pub tree: Option<(u32, Option<bool>)>,
  /// Its line among the lines shown, the groups' rows counted: its place where the rows are not grouped.
  pub position: usize,
  /// Its number, from 1 among the rows shown, where the grid shows them, and how wide their track is.
  pub number: Option<usize>,
  number_lead: f32,
  pub selected: bool,
  pub disabled: bool,
  /// What it is called to what reads the tree: the text of its first column, where that is text.
  pub name: Option<Arc<str>>,
  /// Its cells, a column's each.
  pub cells: Vec<TableCell>,
  /// Whether it has a checkbox before its cells.
  pub checkbox: bool,
  /// Whether the cells at its start stay while the rows scroll sideways.
  pub pinned_start: bool,
  /// Whether the cells at its end stay.
  pub pinned_end: bool,
  /// The fill of the cells that stay, the row's, over which the others scroll.
  pub lead_fill: Option<BackgroundColor>,
  /// Whether Tab enters the rows at it.
  pub entry: bool,
  /// Whether it grows with what it holds (a column that wraps its text): the row's height is then its least.
  pub grows: bool,
  /// The style of each part of this row, in its case.
  pub recipe: ResolvedRecipe,
  forced: Option<Interaction>,
  tracks: ColumnTracks,
  columns_built: Option<Arc<Vec<bool>>>,
  /// Whether its cells take the focus, and not the row: a row of a data grid.
  grid: bool,
  geometry: Geometry,
  actions: Option<Arc<RowActions>>,
  /// The name of its checkbox.
  select_label: Arc<str>,
  host: Option<Arc<HostLink>>,
  element: ElementRef,
  id: Option<Arc<str>>,
}

impl TableRow {
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    self.recipe.part(part).forced(self.forced)
  }

  /// The row's behavior with the style of its part, its cells in the tracks of the columns: the element that
  /// is pressed, selected and focused, with nothing in it yet. It is a row to what reads the tree, at its
  /// place (the header's is 1), selected or not where the table selects.
  pub fn button(&self) -> components::Button {
    let mut row = pressable(&self.part(TablePart::Row), self.disabled)
      .justify(Justify::Start)
      // Each cell is as high as the row: a first column that stays covers all of what scrolls under it, not only
      // the line of its text. A cell sets what it holds in the middle of that height.
      .align_items(Alignment::Stretch)
      .spacing(0.0)
      .column_tracks(&self.tracks)
      // The cells that stay cover the row's veil: they take it from the row under the pointer and held down.
      .interaction_group(true)
      .stops_inside(true)
      .ref_element(self.element.clone())
      .describe("a11y_role", "row")
      .describe("a11y_rowindex", (self.position + 2).to_string())
      // A row is the same element wherever the rows built around it start.
      .key(format!("row-{}", self.index));
    if let Some((level, open)) = self.tree {
      row = row.describe("a11y_level", level.to_string());
      if let Some(open) = open {
        row = row.describe("a11y_expanded", if open { "true" } else { "false" });
      }
    }
    if self.grows {
      let least = px(self.part(TablePart::Row).rest().height, self.recipe.theme()).unwrap_or(0.0);
      row = row.height(Dimension::Auto).min_height(least);
    }
    if self.grid {
      // The cells are the stops of a grid's keys, Left and Right a group of the row's own.
      row = row.focusable(false).focus_group(FocusGroup::horizontal().wrap(false));
    } else {
      row = row.focus_entry(self.entry);
      if self.host.is_some() {
        row = row.focus_row(self.position);
      }
    }
    if self.pinned_start {
      row = row.padding_left(0.0);
    }
    if self.pinned_end {
      row = row.padding_right(0.0);
    }
    if let Some(name) = &self.name {
      row = row.describe("a11y_name", name.clone()).focus_label(name.clone());
    }
    if let Some(actions) = &self.actions
      && actions.selectable
    {
      row = row.describe("a11y_selected", if self.selected { "true" } else { "false" });
    }
    if let Some(actions) = self.actions.clone() {
      // A press selects at once, and a second press of the row soon after at the same place activates it. The
      // row has no `on_dblclick`: the engine holds back the click of an element that has one, and the focus with
      // it, until it knows it is not the first of a double press.
      let press = actions.clone();
      let index = self.index;
      // In the touch form a tap opens the row, as a double press does, and is never held back for a second one.
      let touch = super::touch_form();
      row = row.on_click(move |event: MouseEvent| {
        let tapped = touch && event.pointer_kind != crate::app::events::PointerKind::Mouse && !event.from_keyboard;
        if tapped {
          if press.selectable {
            (press.pick)(Pick::Only);
          }
          (press.activate)();
          return;
        }
        if second_press(index, &event) {
          (press.activate)();
          return;
        }
        if press.selectable {
          let pick = if event.shift {
            Pick::Range
          } else if event.ctrl || event.meta || event.from_keyboard {
            // Space adds the focused row or takes it out, as Ctrl with a press does.
            Pick::Toggle
          } else {
            Pick::Only
          };
          (press.pick)(pick);
        }
        (press.click)();
      });
      let element = self.element.clone();
      if actions.selectable {
        // Select all (Ctrl+A, an Edit menu's) on the row focused selects every row; on a button in it, it is the
        // button's, and goes on out.
        let (element, select_all) = (element.clone(), actions.select_all.clone());
        row = row.on_select_all(move |event: EditEvent| {
          if element.focused() {
            select_all();
          } else {
            event.pass_on();
          }
        });
      }
      row = row.on_key_down(move |event: KeyboardEvent| {
        // A key on a button in the row is the button's.
        if !element.focused() || event.composing || event.alt {
          return;
        }
        let command = event.ctrl || event.meta;
        // Enter activates; it does not press the row, which would select it.
        if event.key == "Enter" && !command && !event.shift {
          event.prevent_default();
          (actions.activate)();
        }
      });
      if let Some(host) = &self.host {
        let (host, position) = (host.clone(), self.position);
        row = row.on_focus(move || host.focused(position));
      }
    }
    if let Some(id) = &self.id {
      row = row.id(id.clone());
    }
    row
  }

  /// The checkbox of the row, in the cell of the column of checkboxes: a press of it adds the row or takes it
  /// out, with Shift the rows from the last selected one. The checkbox itself takes neither the focus nor the
  /// press: Space on the row is its press.
  pub fn checkbox(&self) -> Element {
    let part = self.part(TablePart::Select);
    let checkbox = Checkbox::new(&self.select_label)
      .rules(&self.recipe.lend("checkbox"))
      .label_hidden(true)
      .checked(self.selected)
      .disabled(self.disabled)
      .build(self.recipe.theme());
    let numbered = self.number.is_some();
    let mut cell =
      lead_cell(&part, self.geometry, self.pinned_start && !numbered).child(Row::new().inert(true).child(checkbox));
    if self.pinned_start {
      cell = cell.sticky(Sticky::left(self.number_lead));
    }
    if let Some(fill) = self.lead_fill.clone() {
      cell = cell.background(fill);
    }
    if let Some(actions) = self.actions.clone() {
      cell = cell.on_click(move |event: MouseEvent| {
        event.stop_propagation();
        (actions.pick)(if event.shift { Pick::Range } else { Pick::Toggle });
      });
    }
    cell.into()
  }

  /// The cell of the row's number, at the start, which stays there: a press selects the row alone, with Ctrl adds
  /// it or takes it out, with Shift selects from the last selected one. `None` where the grid shows no numbers.
  pub fn number(&self) -> Option<Element> {
    let number = self.number?;
    let part = self.part(TablePart::RowNumber);
    let mut cell = Row::new()
      .part(&part)
      .align_items(Alignment::Center)
      .justify(Justify::End)
      .padding_left(self.geometry.padding_start)
      .padding_right(self.geometry.gap)
      .sticky(Sticky::left(0.0))
      .describe("a11y_role", "rowheader")
      .describe("a11y_name", number.to_string())
      .child(part.text(&number.to_string()).nowrap());
    if let Some(fill) = self.lead_fill.clone() {
      cell = cell.background(fill);
    }
    if let Some(actions) = self.actions.clone().filter(|actions| actions.selectable) {
      cell = cell.on_click(move |event: MouseEvent| {
        event.stop_propagation();
        (actions.pick)(if event.shift {
          Pick::Range
        } else if event.ctrl || event.meta {
          Pick::Toggle
        } else {
          Pick::Only
        });
      });
    }
    if let Some(id) = &self.id {
      cell = cell.id(format!("{id}-number"));
    }
    Some(cell.into())
  }

  /// How the default renderer builds a row: its number, its checkbox, then its cells.
  pub fn render(mut self) -> Element {
    let mut row = self.button();
    if let Some(number) = self.number() {
      row = row.child(number);
    }
    if self.checkbox {
      row = row.child(self.checkbox());
    }
    let cells = std::mem::take(&mut self.cells);
    let placed = cells.into_iter().map(|cell| (cell.column, cell.render()));
    row
      .with_children(in_tracks(
        self.columns_built.as_ref().map(|built| built.as_slice()),
        placed,
      ))
      .into()
  }
}

thread_local! {
  /// The last plain press of a row: which row, when and where. A second one of it soon after at the same place,
  /// as the engine counts a double press, activates it.
  static LAST_PRESS: Cell<Option<(usize, crate::time::Instant, (f32, f32))>> = const { Cell::new(None) };
}

/// Whether a press of the row `index` is the second of a double press: then the next is a first again. A press
/// with Shift, Ctrl or the system key, or from the keyboard, changes the selection and is never one.
fn second_press(index: usize, event: &MouseEvent) -> bool {
  let plain = !(event.from_keyboard || event.shift || event.ctrl || event.meta || event.alt);
  // The time the window was given the press at, and not the time its handler runs at: what holds the two
  // presses of a double press apart is how fast a person pressed, not how long the first took to be handled.
  let now = event.time;
  let near = |(x, y): (f32, f32)| (x - event.x).powi(2) + (y - event.y).powi(2) <= DOUBLE_CLICK_DISTANCE.powi(2);
  LAST_PRESS.with(|last| {
    let second = plain
      && last
        .get()
        .is_some_and(|(row, at, place)| row == index && now.duration_since(at) <= DOUBLE_CLICK_INTERVAL && near(place));
    last.set((plain && !second).then_some((index, now, (event.x, event.y))));
    second
  })
}

/// What a person typed or chose in a cell of a [`DataGrid`](super::DataGrid), told to the app with the row and
/// the column ([`CellEdit`]).
#[derive(Clone, Debug, PartialEq)]
pub enum CellValue {
  /// A text typed in.
  Text(String),
  /// A number typed in.
  Number(f64),
  /// The place of the choice chosen among the column's choices.
  Choice(usize),
  /// A box ticked, or not.
  Checked(bool),
  /// A number or a choice cleared: Delete on its cell. What it is emptied to is the app's, which may refuse it.
  Empty,
}

/// An edit a person committed in a cell: which row (its key), which column (its id), and the new value. The app
/// that takes it changes its rows, and the grid shows them at the next render.
#[derive(Clone, Debug)]
pub struct CellEdit<'a, K> {
  pub row: &'a K,
  pub column: &'a str,
  pub value: CellValue,
}

type EditHeard<K> = Arc<dyn Fn(&CellEdit<K>) -> bool + Send + Sync>;

/// How the cells of a column of a [`DataGrid`](super::DataGrid) are edited in place, with `ui`'s own control in
/// the cell, and the value a row's cell starts the edit from.
pub struct CellEditor<R> {
  kind: EditorKind<R>,
}

enum EditorKind<R> {
  Text(TextFn<R>),
  Number(Arc<dyn Fn(&R) -> f64 + Send + Sync>),
  Choice(Arc<Vec<Arc<str>>>, Arc<dyn Fn(&R) -> usize + Send + Sync>),
  Checkbox(Arc<dyn Fn(&R) -> bool + Send + Sync>),
}

impl<R> Clone for CellEditor<R> {
  fn clone(&self) -> Self {
    Self {
      kind: match &self.kind {
        EditorKind::Text(text) => EditorKind::Text(text.clone()),
        EditorKind::Number(number) => EditorKind::Number(number.clone()),
        EditorKind::Choice(choices, chosen) => EditorKind::Choice(choices.clone(), chosen.clone()),
        EditorKind::Checkbox(checked) => EditorKind::Checkbox(checked.clone()),
      },
    }
  }
}

impl<R> CellEditor<R> {
  /// A text typed in an input: `ui::Input`, from the row's text.
  pub fn text(value: impl Fn(&R) -> String + Send + Sync + 'static) -> Self {
    Self {
      kind: EditorKind::Text(Arc::new(value)),
    }
  }

  /// A number typed in a number input: `ui::NumberInput`, from the row's number.
  pub fn number(value: impl Fn(&R) -> f64 + Send + Sync + 'static) -> Self {
    Self {
      kind: EditorKind::Number(Arc::new(value)),
    }
  }

  /// One of `choices`, chosen in a select: `ui::Select`, from the place of the row's choice.
  pub fn choice<S: AsRef<str>>(
    choices: impl IntoIterator<Item = S>,
    chosen: impl Fn(&R) -> usize + Send + Sync + 'static,
  ) -> Self {
    Self {
      kind: EditorKind::Choice(
        Arc::new(choices.into_iter().map(|choice| Arc::from(choice.as_ref())).collect()),
        Arc::new(chosen),
      ),
    }
  }

  /// A box ticked or not, in the cell itself: a press of it or Space turns it, with no editor to open. A column
  /// edited with one that builds nothing of its own shows the box.
  pub fn checkbox(checked: impl Fn(&R) -> bool + Send + Sync + 'static) -> Self {
    Self {
      kind: EditorKind::Checkbox(Arc::new(checked)),
    }
  }

  fn is_checkbox(&self) -> bool {
    matches!(self.kind, EditorKind::Checkbox(_))
  }
}

/// Where the focus goes from a cell whose edit was committed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum GridStep {
  /// It stays on the cell.
  Stay,
  /// The cell under it: Enter.
  Down,
  /// The cell after it in the row: Tab.
  On,
  /// The cell before it: Shift+Tab.
  Back,
}

/// The edit going on in a cell: where, and whether the app refused what was committed.
#[derive(Clone, Copy, Debug, PartialEq)]
struct Editing {
  row: usize,
  column: usize,
  refused: bool,
}

/// What the focus is to go to once a render has it: a cell, or the editor of the cell being edited.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum FocusAim {
  Cell(usize, usize),
  Editor,
}

/// What a cell of a grid does, with the types of the table's rows and keys out of sight.
trait GridCells: Send + Sync {
  /// The cell `(row, column)` has the focus: an edit going on elsewhere is committed, or put back where it is
  /// refused.
  fn focused(&self, row: usize, column: usize);
  /// Starts editing the cell, from what is typed where a key started it.
  fn start(&self, row: usize, column: usize, typed: Option<String>);
  /// Turns the box of a cell edited with a checkbox.
  fn toggle(&self, row: usize, column: usize);
  /// Commits the edit going on, and moves the focus as `step` says where the app takes it.
  fn commit(&self, step: GridStep);
  /// Puts the edit going on back, and gives the focus back to its cell.
  fn cancel(&self);
  /// Moves the focus to the cell `(row, column)`, where there is one: whether there is.
  fn move_to(&self, row: usize, column: usize) -> bool;
  fn select(&self, row: usize, pick: Pick);
  fn activate(&self, row: usize);
  /// How many rows and columns the grid has.
  fn size(&self) -> (usize, usize);
  /// The row a press of a cell is on: the handlers of a row's press are told.
  fn click(&self, row: usize);
  /// The corner of the range of cells that moves, where there is a range.
  fn extent(&self) -> Option<(usize, usize)>;
  /// The range from its anchor (or `from`, or the focused cell where there is none) to `to`, scrolled into view.
  fn extend(&self, from: Option<(usize, usize)>, to: (usize, usize));
  /// No range: whether there was one.
  fn clear_range(&self) -> bool;
  /// Every cell in the range.
  fn select_cells(&self);
  /// The range's cells, or the one at `at`, as tab-separated text; a cut clears those that are edited.
  fn copy(&self, at: (usize, usize), cut: bool) -> String;
  /// Pastes tab-separated text from the range's first cell, or `at`.
  fn paste(&self, at: (usize, usize), text: &str);
  /// Clears what is edited of the range's cells, or the one at `at`.
  fn clear(&self, at: (usize, usize));
  /// A drag of cells starts at `at`, and is at `(x, y)` now.
  fn drag_start(&self, at: (usize, usize));
  fn drag_to(&self, x: f32, y: f32);
  /// How many rows a page is.
  fn page(&self) -> usize;
  /// The rows of `scope` written in `format`, and how many there are.
  fn export(&self, scope: ExportScope, format: ExportFormat) -> (String, usize);
  /// Whether there is anything selected to write out: rows, or a range of cells.
  fn has_selection(&self) -> bool;
  /// What opens the row: whether it is open where it opens (a row of a tree with rows under it, or a row with a
  /// detail), and the row it is under in a tree. `None` where the rows are no tree and have no detail.
  fn opener(&self, row: usize) -> Option<(Option<bool>, Option<usize>)>;
  /// Opens or closes the row: the rows under it in a tree, or its detail.
  fn open_row(&self, row: usize, open: bool);
}

/// The cells of a grid as its host works them.
struct GridModel<R, K> {
  host: Arc<HostState>,
  /// The range of cells, among the rows and the columns shown, and as the app holds it; what asks for another
  /// and who is told.
  span: Option<Span>,
  range: Option<CellRange<K>>,
  write_range: Option<RangeWrite<K>>,
  on_range: Vec<RangeHeard<K>>,
  /// The text of each column, where it is a text: what a copy takes of a cell that is not edited.
  texts: Arc<Vec<Option<TextFn<R>>>>,
  /// The title of each column shown: the first line of an export.
  titles: Arc<Vec<Arc<str>>>,
  rows: Arc<Vec<R>>,
  /// The keys of the rows shown, by their ranks, and the lines they are on.
  keys: RowKeys<K>,
  lines: Arc<Lines>,
  /// The id and the editor of each column, where it is edited.
  editors: Arc<Vec<(Arc<str>, Option<CellEditor<R>>)>>,
  on_edit: Vec<EditHeard<K>>,
  model: Arc<Model<K>>,
  read_only: bool,
}

impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> GridModel<R, K> {
  fn editor(&self, column: usize) -> Option<&CellEditor<R>> {
    if self.read_only {
      return None;
    }
    self.editors.get(column).and_then(|(_, editor)| editor.as_ref())
  }

  /// Tells the app `value` for the cell: whether every handler took it.
  fn tell(&self, row: usize, column: usize, value: CellValue) -> bool {
    let (Some(key), Some((id, _))) = (self.keys.get(row), self.editors.get(column)) else {
      return false;
    };
    let edit = CellEdit {
      row: &key,
      column: id,
      value,
    };
    // Every handler is told, and the edit is taken only where none refused it.
    self.on_edit.iter().fold(true, |taken, handler| handler(&edit) && taken)
  }

  /// The value typed or chosen in the editor of the cell being edited.
  fn drafted(&self, column: usize) -> Option<CellValue> {
    Some(match &self.editor(column)?.kind {
      EditorKind::Text(_) => CellValue::Text(self.host.draft_text.get()),
      EditorKind::Number(_) => CellValue::Number(self.host.draft_number.get()?),
      EditorKind::Choice(..) => CellValue::Choice(self.host.draft_choice.get()),
      EditorKind::Checkbox(_) => return None,
    })
  }

  fn aim(&self, aim: FocusAim) {
    *self.host.pending_focus.lock() = Some(aim);
    if let FocusAim::Cell(row, column) = aim {
      *self.host.focused.lock() = Some(self.position(row));
      *self.host.focused_column.lock() = Some(column);
    }
    self.host.rebuild();
  }

  /// The row shown at `rank`.
  fn row(&self, rank: usize) -> Option<&R> {
    self.rows.get(self.lines.row_of_rank(rank)?)
  }

  /// The line of the row shown at `rank`.
  fn position(&self, rank: usize) -> usize {
    self.lines.position_of(rank).unwrap_or(rank)
  }

  /// The cell that has the focus, where it is a cell: its rank and its column.
  fn focused_cell(&self) -> Option<(usize, usize)> {
    let position = (*self.host.focused.lock())?;
    Some((self.lines.rank_at(position)?, (*self.host.focused_column.lock())?))
  }
}

impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> GridCells for GridModel<R, K> {
  fn focused(&self, row: usize, column: usize) {
    let position = self.position(row);
    let before = (*self.host.focused.lock(), *self.host.focused_column.lock());
    *self.host.focused.lock() = Some(position);
    *self.host.focused_column.lock() = Some(column);
    // The focus moved to another cell: the range is gone, and the cell is where the next one starts.
    if before != (Some(position), Some(column)) {
      self.write_span(None);
    }
    let editing = *self.host.editing.lock();
    if let Some(editing) = editing
      && (editing.row, editing.column) != (row, column)
    {
      // The focus left the cell being edited for another: what was typed is committed, or put back.
      if let Some(value) = self.drafted(editing.column) {
        self.tell(editing.row, editing.column, value);
      }
      *self.host.editing.lock() = None;
      self.host.rebuild();
    }
  }

  fn start(&self, row: usize, column: usize, typed: Option<String>) {
    let (Some(editor), Some(values)) = (self.editor(column), self.row(row)) else {
      return;
    };
    match &editor.kind {
      EditorKind::Text(text) => self.host.draft_text.set(typed.unwrap_or_else(|| text(values))),
      EditorKind::Number(number) => self.host.draft_number.set(
        typed
          .and_then(|typed| typed.parse::<f64>().ok())
          .or(Some(number(values))),
      ),
      EditorKind::Choice(_, chosen) => self.host.draft_choice.set(chosen(values)),
      EditorKind::Checkbox(_) => {
        self.toggle(row, column);
        return;
      }
    }
    *self.host.editing.lock() = Some(Editing {
      row,
      column,
      refused: false,
    });
    self.aim(FocusAim::Editor);
  }

  fn toggle(&self, row: usize, column: usize) {
    let (Some(editor), Some(values)) = (self.editor(column), self.row(row)) else {
      return;
    };
    if let EditorKind::Checkbox(checked) = &editor.kind {
      self.tell(row, column, CellValue::Checked(!checked(values)));
    }
  }

  fn commit(&self, step: GridStep) {
    let Some(editing) = *self.host.editing.lock() else {
      return;
    };
    let Some(value) = self.drafted(editing.column) else {
      return;
    };
    if !self.tell(editing.row, editing.column, value) {
      // Refused: the editor stays, said to be wrong, and keeps the focus.
      *self.host.editing.lock() = Some(Editing {
        refused: true,
        ..editing
      });
      self.aim(FocusAim::Editor);
      return;
    }
    *self.host.editing.lock() = None;
    let (row, column) = (editing.row, editing.column);
    let columns = self.editors.len();
    let target = match step {
      GridStep::Stay => (row, column),
      GridStep::Down if row + 1 < self.keys.len() => (row + 1, column),
      GridStep::On if column + 1 < columns => (row, column + 1),
      GridStep::Back if column > 0 => (row, column - 1),
      _ => (row, column),
    };
    self.aim(FocusAim::Cell(target.0, target.1));
  }

  fn cancel(&self) {
    let Some(editing) = self.host.editing.lock().take() else {
      return;
    };
    self.aim(FocusAim::Cell(editing.row, editing.column));
  }

  fn move_to(&self, row: usize, column: usize) -> bool {
    if row >= self.keys.len() || column >= self.editors.len() {
      return false;
    }
    self.aim(FocusAim::Cell(row, column));
    true
  }

  fn select(&self, row: usize, pick: Pick) {
    if let Some(index) = self.lines.row_of_rank(row) {
      self.model.pick(index, pick);
    }
  }

  fn activate(&self, row: usize) {
    if let Some(key) = self.keys.get(row) {
      for handler in &self.model.on_row_activate {
        handler(&key);
      }
    }
  }

  fn size(&self) -> (usize, usize) {
    (self.keys.len(), self.editors.len())
  }

  fn click(&self, row: usize) {
    if let Some(key) = self.keys.get(row) {
      for handler in &self.model.on_row_click {
        handler(&key);
      }
    }
  }

  fn extent(&self) -> Option<(usize, usize)> {
    self.span.map(|span| span.extent)
  }

  fn extend(&self, from: Option<(usize, usize)>, to: (usize, usize)) {
    let anchor = self
      .span
      .map(|span| span.anchor)
      .or(from)
      .or_else(|| self.focused_cell())
      .unwrap_or(to);
    let span = Span { anchor, extent: to };
    // A range of one cell is the focused cell alone.
    self.write_span((anchor != to).then_some(span));
    self.host.reveal_cell(self.position(to.0), to.1);
  }

  fn clear_range(&self) -> bool {
    let had = self.span.is_some();
    self.write_span(None);
    had
  }

  fn select_cells(&self) {
    let (rows, columns) = self.size();
    if rows == 0 || columns == 0 {
      return;
    }
    let anchor = self.focused_cell().unwrap_or((0, 0));
    // The focus stays where it is, at a corner of the range.
    let extent = (
      if anchor.0 == 0 { rows - 1 } else { 0 },
      if anchor.1 == 0 { columns - 1 } else { 0 },
    );
    let span = if anchor.0 == 0 || anchor.0 == rows - 1 {
      Span { anchor, extent }
    } else {
      Span {
        anchor: (0, 0),
        extent: (rows - 1, columns - 1),
      }
    };
    self.write_span(Some(span));
  }

  fn copy(&self, at: (usize, usize), cut: bool) -> String {
    self.copy_cells(at, cut)
  }

  fn export(&self, scope: ExportScope, format: ExportFormat) -> (String, usize) {
    self.export_rows(scope, format)
  }

  fn opener(&self, row: usize) -> Option<(Option<bool>, Option<usize>)> {
    if let Some(tree) = &self.lines.tree {
      let open = tree.branch.get(row).copied().unwrap_or(false).then(|| tree.open[row]);
      return Some((open, tree.parent.get(row).copied().flatten()));
    }
    self.host.detail_toggle.lock().as_ref()?;
    let position = self.lines.position_of(row)?;
    Some((Some(self.lines.line(position + 1) == Some(Shown::Detail(row))), None))
  }

  fn open_row(&self, row: usize, open: bool) {
    let toggle = if self.lines.tree.is_some() {
      self.host.tree_toggle.lock().clone()
    } else {
      self.host.detail_toggle.lock().clone()
    };
    if let Some(toggle) = toggle {
      toggle(row, open);
    }
  }

  fn has_selection(&self) -> bool {
    self.span.is_some() || self.model.selected.iter().any(|key| self.keys.rank_of(key).is_some())
  }

  fn paste(&self, at: (usize, usize), text: &str) {
    self.paste_cells(at, text);
  }

  fn clear(&self, at: (usize, usize)) {
    self.clear_cells(at);
  }

  fn drag_start(&self, at: (usize, usize)) {
    *self.host.drag_anchor.lock() = Some(at);
    *self.host.drag_last.lock() = Some(at);
    // A drag has no click to focus the cell it started at: the focus goes there, where the range starts.
    self.aim(FocusAim::Cell(at.0, at.1));
  }

  fn drag_to(&self, x: f32, y: f32) {
    self.host.scroll_toward(x, y);
    let Some(anchor) = *self.host.drag_anchor.lock() else {
      return;
    };
    let Some(cell) = self.host.cell_at(x, y) else {
      return;
    };
    // A drag's handlers may be an earlier render's: what it last asked for is the host's.
    if self.host.drag_last.lock().replace(cell) == Some(cell) {
      return;
    }
    self.write_span_now((anchor != cell).then_some(Span { anchor, extent: cell }));
  }

  fn page(&self) -> usize {
    FocusRows::page(&*self.host)
  }
}

/// What a cell of a grid is and does, as a renderer sees it beside the cell's own: where it is, whether it is
/// edited or being edited, and what its keys and presses do.
pub struct GridCell {
  /// The row it is in: its place among the rows shown, from 0.
  pub row: usize,
  /// The row's line among the lines shown, the groups' rows counted.
  pub position: usize,
  /// Whether a person edits it: its column has an editor and the grid is not read only.
  pub editable: bool,
  /// Whether it is in the range of cells selected.
  pub selected: bool,
  /// The editor in it while it is being edited, in place of its content.
  pub editor: Option<Element>,
  /// Whether it is the cell Tab enters the grid at.
  pub entry: bool,
  /// Its place to what reads the tree: its column's among the grid's, the checkboxes' counted, from 1.
  pub column_index: usize,
  checkbox: bool,
  element: ElementRef,
  grid: Arc<dyn GridCells>,
  id: Option<Arc<str>>,
}

thread_local! {
  /// The last plain press of a cell of a grid: which, when and where. A second one of it soon after at the
  /// same place edits it.
  static LAST_CELL_PRESS: Cell<Option<((usize, usize), crate::time::Instant, (f32, f32))>> = const { Cell::new(None) };
}

fn second_cell_press(cell: (usize, usize), event: &MouseEvent) -> bool {
  let plain = !(event.from_keyboard || event.shift || event.ctrl || event.meta || event.alt);
  let now = event.time;
  let near = |(x, y): (f32, f32)| (x - event.x).powi(2) + (y - event.y).powi(2) <= DOUBLE_CLICK_DISTANCE.powi(2);
  LAST_CELL_PRESS.with(|last| {
    let second = plain
      && last.get().is_some_and(|(at, when, place)| {
        at == cell && now.duration_since(when) <= DOUBLE_CLICK_INTERVAL && near(place)
      });
    last.set((plain && !second).then_some((cell, now, (event.x, event.y))));
    second
  })
}

impl GridCell {
  /// The cell's behavior on the element of the cell: a stop of the grid's keys, its row and its column said to
  /// them, its keys, and a double press that edits it.
  fn dress(&self, column: usize, cell: Row, editing: bool) -> Row {
    let (row, grid, element) = (self.row, self.grid.clone(), self.element.clone());
    let editable = self.editable;
    let checkbox = self.checkbox;
    let mut cell = cell
      .tab_index(0)
      .focus_row(self.position)
      .focus_column(column)
      .focus_entry(self.entry)
      .ref_element(self.element.clone())
      .describe("a11y_role", "gridcell")
      .describe("a11y_colindex", self.column_index.to_string())
      .describe("a11y_readonly", if editable { "false" } else { "true" })
      .describe("a11y_rowindex", (self.position + 2).to_string())
      .on_focus({
        let grid = grid.clone();
        move || grid.focused(row, column)
      })
      .on_click({
        let grid = grid.clone();
        move |event: MouseEvent| {
          // A press of a cell is the cell's: it does not select the row, as its checkbox and Space do.
          event.stop_propagation();
          if event.shift {
            // The press took the range to the cell: the focus stays where the range started.
            event.prevent_default();
            return;
          }
          if event.ctrl || event.meta || event.alt {
            return;
          }
          // A box in a cell turns at a press, once for a double press: its second press is not another turn.
          if checkbox && editable {
            if !second_cell_press((row, column), &event) {
              grid.toggle(row, column);
            }
            return;
          }
          // A second press edits a cell that is edited, and activates the row of one that is not.
          if second_cell_press((row, column), &event) {
            if editable {
              grid.start(row, column, None);
            } else {
              grid.activate(row);
            }
            return;
          }
          grid.click(row);
        }
      });
    if self.selected {
      cell = cell.describe("a11y_selected", "true");
    }
    if let Some(id) = &self.id {
      cell = cell.id(id.clone());
    }
    // A cell being edited leaves its keys, its presses and its clipboard to its editor.
    if editing {
      return cell;
    }
    cell = cell
      // A press with Shift takes the range to the cell and leaves the focus where the range started; a press
      // without it starts again from the cell.
      .on_mouse_down({
        let grid = grid.clone();
        move |event: MouseEvent| {
          if event.button != MouseButton::Left {
            return;
          }
          if event.shift {
            event.prevent_default();
            grid.extend(None, (row, column));
          } else {
            grid.clear_range();
          }
        }
      })
      // A drag from a cell selects the cells from it to the one under the pointer.
      .start_drag_buttons(MouseButtonMask::LEFT)
      .on_drag_start({
        let grid = grid.clone();
        move |_: DragEvent| grid.drag_start((row, column))
      })
      .on_drag_move({
        let grid = grid.clone();
        move |event: DragEvent| grid.drag_to(event.x, event.y)
      })
      .on_copy({
        let grid = grid.clone();
        move |event: CopyEvent| event.set_text(grid.copy((row, column), event.is_cut()))
      })
      .on_paste({
        let grid = grid.clone();
        move |event: PasteEvent| {
          if let Some(text) = event.text() {
            grid.paste((row, column), text);
          }
        }
      })
      // Select all (Ctrl+A, an Edit menu's) on the cell focused selects every cell: the rows are selected with the
      // header's checkbox. An editor in the cell selects its own text.
      .on_select_all({
        let (grid, element) = (grid.clone(), element.clone());
        move |event: EditEvent| {
          if element.focused() {
            grid.select_cells();
          } else {
            event.pass_on();
          }
        }
      });
    cell.on_key_down(move |event: KeyboardEvent| {
      // A key in the cell is the cell's only where the cell has the focus itself, not its editor.
      if !element.focused() || event.composing || event.alt {
        return;
      }
      let command = event.ctrl || event.meta;
      let (rows, columns) = grid.size();
      let (last_row, last_column) = (rows.saturating_sub(1), columns.saturating_sub(1));
      // Shift with a key that moves takes the range's moving corner there, and leaves the focus where it is.
      let (at_row, at_column) = grid.extent().unwrap_or((row, column));
      let reach = match event.key.as_str() {
        _ if !event.shift => None,
        "ArrowUp" if !command => Some((at_row.saturating_sub(1), at_column)),
        "ArrowDown" if !command => Some(((at_row + 1).min(last_row), at_column)),
        "ArrowLeft" if !command => Some((at_row, at_column.saturating_sub(1))),
        "ArrowRight" if !command => Some((at_row, (at_column + 1).min(last_column))),
        "PageUp" if !command => Some((at_row.saturating_sub(grid.page()), at_column)),
        "PageDown" if !command => Some(((at_row + grid.page()).min(last_row), at_column)),
        "Home" if command => Some((0, 0)),
        "End" if command => Some((last_row, last_column)),
        "Home" => Some((at_row, 0)),
        "End" => Some((at_row, last_column)),
        _ => None,
      };
      if let Some(to) = reach {
        event.prevent_default();
        grid.extend(Some((row, column)), to);
        return;
      }
      // In the first column Right opens a row of a tree, or its detail, and Left closes it, or goes to the row it is
      // under.
      if column == 0
        && !command
        && !event.shift
        && let Some((open, parent)) = grid.opener(row)
      {
        match (event.key.as_str(), open, parent) {
          ("ArrowRight", Some(false), _) => {
            event.prevent_default();
            grid.open_row(row, true);
            return;
          }
          ("ArrowLeft", Some(true), _) => {
            event.prevent_default();
            grid.open_row(row, false);
            return;
          }
          ("ArrowLeft", _, Some(parent)) => {
            event.prevent_default();
            grid.move_to(parent, 0);
            return;
          }
          _ => {}
        }
      }
      match event.key.as_str() {
        "Enter" | "F2" if !command && editable && !checkbox => {
          event.prevent_default();
          grid.start(row, column, None);
        }
        "Enter" if !command => {
          event.prevent_default();
          grid.activate(row);
        }
        " " if !command && checkbox && editable => {
          event.prevent_default();
          grid.toggle(row, column);
        }
        " " if !command => {
          event.prevent_default();
          grid.select(row, if event.shift { Pick::Range } else { Pick::Toggle });
        }
        "Tab" if !command => {
          let to = if event.shift {
            column.checked_sub(1)
          } else {
            Some(column + 1)
          };
          if to.is_some_and(|to| grid.move_to(row, to)) {
            event.prevent_default();
          }
        }
        // The ends of the row, and with Ctrl of the grid: cells that may not be built, as the columns and the
        // rows out of view are not.
        "Home" | "End" => {
          event.prevent_default();
          let end = event.key == "End";
          let to_row = match (command, end) {
            (true, true) => last_row,
            (true, false) => 0,
            (false, _) => row,
          };
          grid.move_to(to_row, if end { last_column } else { 0 });
        }
        // Delete clears what is edited of the range, or of the cell; Shift+Delete is a cut, the engine's.
        "Delete" | "Backspace" if !command && !event.shift => {
          event.prevent_default();
          grid.clear((row, column));
        }
        "Escape" if !command => {
          if grid.clear_range() {
            event.prevent_default();
          }
        }
        // What is typed in an editable cell starts its edit with it.
        key if !command && editable && !checkbox && key.chars().count() == 1 && !key.trim().is_empty() => {
          event.prevent_default();
          grid.start(row, column, Some(key.to_owned()));
        }
        _ => {}
      }
    })
  }
}

/// The editor of the cell being edited, in its part: its keys commit it and move on, or put it back.
fn editor_in(part: &ResolvedPart, editor: Element, choice: bool, grid: Arc<dyn GridCells>, element: ElementRef) -> Row {
  Row::new()
    .part(part)
    .align_items(Alignment::Center)
    .width(Dimension::Pct(100.0))
    // It gives way to the cell's room: an editor never reaches past its cell.
    .flex_full(1.0, 1.0, Some(0.0))
    .min_width(Dimension::Px(0.0))
    .ref_element(element)
    .on_key_down(move |event: KeyboardEvent| {
      if !event.focus_within || event.composing {
        return;
      }
      match event.key.as_str() {
        // A select keeps Enter for its menu: its choice commits it.
        "Enter" if !choice && !event.shift && !event.alt => {
          event.prevent_default();
          grid.commit(GridStep::Down);
        }
        "Tab" if !event.alt => {
          event.prevent_default();
          grid.commit(if event.shift { GridStep::Back } else { GridStep::On });
        }
        "Escape" => {
          event.prevent_default();
          grid.cancel();
        }
        _ => {}
      }
    })
    .child(editor)
}

/// What a mounted table's rows tell its host: where the focus is.
struct HostLink {
  state: Arc<HostState>,
}

impl HostLink {
  fn focused(&self, index: usize) {
    *self.state.focused.lock() = Some(index);
  }

  /// How wide the track of the column `column` is now.
  fn track_width(&self, column: usize) -> f32 {
    let metrics = *self.state.metrics.lock();
    let Some(tracks) = self.state.tracks.lock().as_ref().map(|(_, tracks)| tracks.clone()) else {
      return 0.0;
    };
    let viewport = self.state.scroll.viewport_width();
    let available = (viewport > 0.0).then(|| (viewport - metrics.padding_start - metrics.padding_end).max(0.0));
    tracks
      .widths(available, 0.0)
      .get(column + metrics.lead_tracks)
      .copied()
      .unwrap_or(0.0)
  }

  /// Keeps the column of `id` at `width`, and renders the table again: whether that is another width.
  fn resize(&self, id: &Arc<str>, width: f32) -> bool {
    let mut resized = self.state.resized.lock();
    if resized.get(id).is_some_and(|held| (held - width).abs() < 0.01) {
      return false;
    }
    resized.insert(id.clone(), width);
    drop(resized);
    self.state.rebuild();
    true
  }
}

/// The rows of a mounted table as the engine's focus group steps over them: Up and Down, Home and End, PageUp
/// and PageDown go to any row, and one that is not built is scrolled to and built, and focused once it is.
impl FocusRows for HostState {
  fn count(&self) -> usize {
    self.metrics.lock().count
  }

  fn label(&self, _row: usize) -> Option<String> {
    None
  }

  fn enabled(&self, row: usize) -> bool {
    !self.metrics.lock().disabled && !matches!(self.lines.lock().line(row), Some(Shown::Summary(_) | Shown::Detail(_)))
  }

  fn level(&self, row: usize) -> u32 {
    self.lines.lock().level(row)
  }

  fn expanded(&self, row: usize) -> Option<bool> {
    let lines = self.lines.lock();
    match lines.line(row)? {
      Shown::Group(group) => Some(lines.groups[group].open),
      // A row of a tree that has rows under it.
      Shown::Row(rank) => {
        let tree = lines.tree.as_ref()?;
        tree.branch.get(rank).copied().unwrap_or(false).then(|| tree.open[rank])
      }
      Shown::Summary(_) | Shown::Detail(_) => None,
    }
  }

  fn set_expanded(&self, row: usize, open: bool) {
    let group = match self.lines.lock().line(row) {
      Some(Shown::Group(group)) => group,
      Some(Shown::Row(rank)) => {
        let toggle = self.tree_toggle.lock().clone();
        if let Some(toggle) = toggle {
          toggle(rank, open);
        }
        return;
      }
      _ => return,
    };
    let toggle = self.group_toggle.lock().clone();
    if let Some(toggle) = toggle {
      toggle(group, open);
    }
  }

  /// The rows the view holds under the header, less one, so that the row a page goes from stays in view.
  fn page(&self) -> usize {
    let metrics = *self.metrics.lock();
    if metrics.row_height <= 0.0 {
      return 1;
    }
    ((self.scroll.viewport_height() - metrics.header_height) / metrics.row_height)
      .floor()
      .max(2.0) as usize
      - 1
  }

  fn reveal(&self, row: usize) {
    let metrics = *self.metrics.lock();
    if row >= metrics.count || metrics.row_height <= 0.0 {
      return;
    }
    // The row is scrolled to where it shows under the header, and kept built as the focused row is.
    let (top, height) = {
      let heights = self.heights.lock();
      (heights.top(row), heights.height(row))
    };
    let room = (self.scroll.viewport_height() - metrics.header_height).max(height);
    let scroll_y = self.scroll.scroll_y();
    let y = if top < scroll_y {
      top
    } else if top + height > scroll_y + room {
      top + height - room
    } else {
      scroll_y
    };
    self.scroll.set_scroll(self.scroll.scroll_x(), y.max(0.0));
    *self.focused.lock() = Some(row);
    self.rebuild();
  }
}

/// The sizes a mounted table's window is reckoned by.
#[derive(Clone, Copy, Debug, Default)]
struct Metrics {
  row_height: f32,
  header_height: f32,
  count: usize,
  disabled: bool,
  /// The room before the first track of a row, and after the last.
  padding_start: f32,
  padding_end: f32,
  /// The tracks before the first column's: the checkboxes'.
  lead_tracks: usize,
  /// The columns that stay at the start and at the end, built wherever the rows are scrolled.
  pins: (usize, usize),
}

/// The lines a mounted table laid out, and what they were laid out for.
type KeptLines = (
  Arc<dyn std::any::Any + Send + Sync>,
  (Vec<Arc<str>>, Vec<GroupKey>, bool, TableFilters, u64),
  Arc<Lines>,
);

/// Rows built past each edge of what is in view.
const OVERSCAN: usize = 8;
/// Columns built past each side of what is in view.
const OVERSCAN_COLUMNS: usize = 2;
/// Rows built before the table knows how high it is.
const FIRST_ROWS: usize = 40;
/// How wide the table is taken to be before it knows: the columns built at first are those this shows.
const FIRST_WIDTH: f32 = 1920.0;

/// What a mounted table keeps from render to render.
struct HostState {
  scroll: ScrollState,
  /// The tracks of the columns, kept while the columns are the same, with what they measured.
  tracks: Mutex<Option<(Vec<TrackSpec>, ColumnTracks)>>,
  /// The places of the rows built.
  built: Mutex<Vec<usize>>,
  /// Which columns are built, where the columns in view only are.
  columns_built: Mutex<Option<Arc<Vec<bool>>>>,
  /// The column that has, or last had, the focus: built wherever the rows are scrolled.
  focused_column: Mutex<Option<usize>>,
  /// The widths a person made the columns, by their ids.
  resized: Mutex<HashMap<Arc<str>, f32>>,
  /// What is typed or chosen in the editor of the cell being edited, and which cell that is.
  draft_text: Signal<String>,
  draft_number: Signal<Option<f64>>,
  draft_choice: Signal<usize>,
  editing: Mutex<Option<Editing>>,
  /// Where the focus is to go once a render has it.
  pending_focus: Mutex<Option<FocusAim>>,
  /// The refs of the cells built at the last render, by row and column, which a drag of cells finds its cell by.
  cell_refs: Mutex<HashMap<(usize, usize), ElementRef>>,
  /// What scrolls, where a drag of cells past its edge scrolls it.
  scroll_ref: ElementRef,
  /// The cell a drag of cells started at, and the cell it last reached.
  drag_anchor: Mutex<Option<(usize, usize)>>,
  drag_last: Mutex<Option<(usize, usize)>>,
  /// The column whose menu is open, what the menu hangs from, and its list of columns.
  menu_open: Mutex<Option<Arc<str>>>,
  menu_anchor: ElementRef,
  submenu_open: Signal<bool>,
  submenu_anchor: ElementRef,
  /// The export's menu in a column's menu.
  export_open: Signal<bool>,
  export_anchor: ElementRef,
  /// The grid's bar: its query, the density it chose (0 for the app's), its menus of the columns that filter, of
  /// the columns and of the export, what writes the rows out and whether anything is selected to; and the search
  /// of a grid whose app holds none.
  bar_query: Signal<String>,
  bar_density: Signal<u8>,
  bar_filter: (Signal<bool>, ElementRef),
  bar_columns: (Signal<bool>, ElementRef),
  bar_export: (Signal<bool>, ElementRef),
  export_now: Mutex<Option<Arc<dyn Fn(ExportScope, ExportFormat) + Send + Sync>>>,
  export_selected: Mutex<bool>,
  own_search: Mutex<String>,
  bar_chose: Mutex<bool>,
  /// Where each header built is, by its column, and a column dragged by its header: from where to before which.
  header_refs: Mutex<HashMap<usize, ElementRef>>,
  /// What each summary row said, by what it was computed from: kept while that is the same.
  summaries: Mutex<HashMap<SummaryKey, Arc<Vec<Said>>>>,
  header_drop: Mutex<Option<(usize, Option<usize>)>>,
  /// The lines shown, and what opens or closes the group of one: the engine's nested group asks it.
  lines: Mutex<Arc<Lines>>,
  /// The index of the rows' keys, kept while the rows are the same.
  key_index: Mutex<KeptIndex>,
  /// The lines laid out for the rows (held, so that they are the same rows while these are kept), the columns
  /// grouped by, the groups open and whether the groups end in their summaries.
  kept_lines: Mutex<Option<KeptLines>>,
  /// How high the lines are: a row's height each until one built measures otherwise.
  heights: Arc<Mutex<Heights>>,
  /// Whether a column's filter panel is open, which column's, what it hangs from, and what is typed in filters.
  filter_open: Signal<bool>,
  filter_anchor: ElementRef,
  filters: FilterState,
  /// The matches of the search and the current one; the find bar's query, whether it is open, and its toggles.
  search: Mutex<SearchState>,
  find_open: Signal<bool>,
  find_query: Signal<String>,
  find_case: Signal<bool>,
  find_word: Signal<bool>,
  find_regex: Signal<bool>,
  /// The id of a grid the app gives none: every id the grid makes (its cells', its editor's) is under its own, so
  /// that the grids of a page never share one.
  own_id: Arc<str>,
  /// What the grid keeps of the rows the app gives on demand, and the query the app is yet to hear of.
  source: Mutex<Option<Box<dyn std::any::Any + Send>>>,
  source_query: Mutex<Option<(GridQuery, Vec<Arc<dyn Fn(&GridQuery) + Send + Sync>>)>>,
  group_toggle: Mutex<Option<Arc<dyn Fn(usize, bool) + Send + Sync>>>,
  /// Opens or closes the rows under the row of a rank, in a tree; and its detail.
  tree_toggle: Mutex<Option<Arc<dyn Fn(usize, bool) + Send + Sync>>>,
  detail_toggle: Mutex<Option<Arc<dyn Fn(usize, bool) + Send + Sync>>>,
  metrics: Mutex<Metrics>,
  /// The row that has, or last had, the focus, or that a key goes to.
  focused: Mutex<Option<usize>>,
  /// The last asking to bring a row into view that was answered.
  revealed: Mutex<Option<u64>>,
  revision: Signal<u64>,
}

impl HostState {
  /// The rows in view and `margin` rows past each edge, from the scroll as it is.
  fn range(&self, margin: usize) -> Option<(usize, usize)> {
    self.range_at(self.scroll.scroll_y(), margin)
  }

  /// The lines in view with the scroll at `scroll_y`, and `margin` lines past each edge.
  fn range_at(&self, scroll_y: f32, margin: usize) -> Option<(usize, usize)> {
    let metrics = *self.metrics.lock();
    let viewport = self.scroll.viewport_height();
    if metrics.row_height <= 0.0 || viewport <= 0.0 || metrics.count == 0 {
      return None;
    }
    let heights = self.heights.lock();
    let first = heights.position_at(scroll_y.max(0.0));
    let last = heights.position_at(scroll_y.max(0.0) + viewport) + 1;
    Some((first.saturating_sub(margin), (last + margin).min(metrics.count)))
  }

  /// Keeps what the line at `position` measured, as it is laid out: a line above the one at the top of the view
  /// that is higher or lower than it was taken to be moves the scroll by as much, so that what is in view stays.
  /// Whether it measured otherwise than it was taken to be.
  fn measured(&self, position: usize, height: f32) -> bool {
    let scroll_y = self.scroll.scroll_y();
    let change = {
      let mut heights = self.heights.lock();
      let anchor = heights.position_at(scroll_y);
      let change = heights.measure(position, height);
      if position >= anchor { 0.0 } else { change }
    };
    if change != 0.0 {
      self
        .scroll
        .set_scroll(self.scroll.scroll_x(), (scroll_y + change).max(0.0));
    }
    change != 0.0
  }

  /// Whether the rows that are to be built, those in view and half the overscan around them, are not all
  /// built, or rows far from them are (the first rows built before the table knew its height): the table is
  /// rendered again with the rows around what is in view.
  fn needs_rows(&self) -> bool {
    let (Some((first, last)), Some((far_first, far_last))) = (self.range(OVERSCAN / 2), self.range(OVERSCAN * 2))
    else {
      return false;
    };
    let focused = *self.focused.lock();
    let built = self.built.lock();
    (first..last).any(|index| !built.contains(&index))
      || built
        .iter()
        .any(|index| (*index < far_first || *index >= far_last) && Some(*index) != focused)
  }

  fn rebuild(&self) {
    self.revision.update(|revision| *revision = revision.wrapping_add(1));
  }

  /// Which columns are in view at `scroll_x`, with [`OVERSCAN_COLUMNS`] past each side, the pinned ones and the
  /// focused one: `None` where every column is in view anyway. Before the table knows its width it builds the
  /// columns a wide window shows ([`FIRST_WIDTH`]), not the hundreds it may have.
  fn columns_at(&self, scroll_x: f32) -> Option<Vec<bool>> {
    let metrics = *self.metrics.lock();
    let tracks = self.tracks.lock().as_ref()?.1.clone();
    let viewport = match self.scroll.viewport_width() {
      width if width > 0.0 => width,
      _ => FIRST_WIDTH,
    };
    let available = (viewport - metrics.padding_start - metrics.padding_end).max(0.0);
    let widths = tracks.widths(Some(available), 0.0);
    let columns = widths.len().saturating_sub(metrics.lead_tracks);
    let mut x = metrics.padding_start;
    let mut seen = vec![false; columns];
    for (track, width) in widths.iter().enumerate() {
      if let Some(column) = track.checked_sub(metrics.lead_tracks) {
        seen[column] = x + width >= scroll_x && x <= scroll_x + viewport;
      }
      x += width;
    }
    if seen.iter().all(|seen| *seen) {
      return None;
    }
    let mut built = seen.clone();
    for (column, seen) in seen.iter().enumerate() {
      if *seen {
        let (from, to) = (
          column.saturating_sub(OVERSCAN_COLUMNS),
          (column + OVERSCAN_COLUMNS + 1).min(columns),
        );
        built[from..to].iter_mut().for_each(|built| *built = true);
      }
    }
    let (start, end) = metrics.pins;
    built[..start.min(columns)].iter_mut().for_each(|built| *built = true);
    built[columns.saturating_sub(end)..]
      .iter_mut()
      .for_each(|built| *built = true);
    if let Some(column) = *self.focused_column.lock()
      && column < columns
    {
      built[column] = true;
    }
    Some(built)
  }

  /// Whether a column in view at `scroll_x` is not built, or the table built only some columns and now sees
  /// them all: it is rendered again.
  fn needs_columns(&self, scroll_x: f32) -> bool {
    let wanted = self.columns_at(scroll_x);
    let built = self.columns_built.lock().clone();
    match (wanted, built) {
      (None, None) => false,
      (None, Some(built)) => built.iter().any(|built| !built),
      (Some(_), None) => false,
      (Some(wanted), Some(built)) => {
        // Only a column that came into view: one that left keeps its cells until the next render.
        wanted
          .iter()
          .zip(built.iter())
          .enumerate()
          .any(|(column, (wanted, built))| *wanted && !built && self.seen_at(column, scroll_x))
      }
    }
  }

  /// Whether the column `column` is in view at `scroll_x`.
  fn seen_at(&self, column: usize, scroll_x: f32) -> bool {
    let metrics = *self.metrics.lock();
    let Some(tracks) = self.tracks.lock().as_ref().map(|(_, tracks)| tracks.clone()) else {
      return true;
    };
    let viewport = self.scroll.viewport_width();
    let available = (viewport - metrics.padding_start - metrics.padding_end).max(0.0);
    let widths = tracks.widths(Some(available), 0.0);
    let track = column + metrics.lead_tracks;
    let x = metrics.padding_start + widths[..track.min(widths.len())].iter().sum::<f32>();
    let width = widths.get(track).copied().unwrap_or(0.0);
    x + width >= scroll_x && x <= scroll_x + viewport
  }
}

struct TableProps<R, K> {
  table: Arc<Mutex<Table<R, K>>>,
}

impl<R, K> PartialEq for TableProps<R, K> {
  fn eq(&self, _: &Self) -> bool {
    false
  }
}

impl<R, K> crate::app::component::DevtoolsInspectable for TableProps<R, K> {}

/// What keeps a mounted table: the scroll of its rows, the widths of its columns, which rows are built.
struct TableHost<R, K> {
  state: Arc<HostState>,
  /// The range of cells of a grid whose app holds none.
  own_range: Arc<Mutex<Option<CellRange<K>>>>,
  /// The arrangement of a grid's columns whose app holds none.
  own_layout: Arc<Mutex<Option<ColumnLayout>>>,
  /// The groups open of a table whose app holds none.
  own_open: Arc<Mutex<Vec<GroupKey>>>,
  /// The rows of a tree open, and the rows whose detail is open, where the app holds none.
  own_expanded: Arc<Mutex<Vec<K>>>,
  own_details: Arc<Mutex<Vec<K>>>,
  _marker: PhantomData<fn() -> R>,
}

impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> TableHost<R, K> {
  /// Makes the requests of the rows the app gives on demand, where it gives them so.
  fn started(self, ctx: &mut Ctx) -> Self {
    let source = ctx.props::<TableProps<R, K>>().table.lock().source.clone();
    if let Some(source) = source {
      source.start(ctx, &mut self.state.source.lock());
    }
    self
  }

  /// The find bar over the grid: its query the grid's search, its count the matches, its arrows the next match
  /// and the one before.
  /// The bar over a grid, as the board's Table Toolbar draws it, built from a `Toolbar`: a search field of 220 that
  /// is the grid's search, a tag for each filter set (its × takes it off), Filter (a menu of the columns that
  /// filter, each opening its filter's panel), and at the end the density, the columns to show and Export.
  fn grid_bar(&self, ctx: &mut Ctx, table: &Table<R, K>) -> Element {
    use super::{
      Choice, InputSize, SearchField, SearchFieldPart, Tag, TagPart, TagVariant, ToggleGroup, ToggleGroupPart, Toolbar,
      ToolbarItem, ToolbarPart,
      button::{Button, ButtonPart, ButtonSize, ButtonVariant},
    };
    let state = &self.state;
    let words = table.words.bar.clone();
    // The query the grid searches is the field's: one the app clears elsewhere is cleared in the field.
    if state.bar_query.get_untracked() != *table.search {
      state.bar_query.set(table.search.to_string());
    }
    let (write, heard) = (table.write_search.clone(), table.on_search_change.clone());
    let id_of = |what: &str| table.id.as_ref().map(|id| format!("{id}-{what}"));
    let mut search = SearchField::new(state.bar_query.clone())
      .placeholder(&words.search)
      .label(&words.search)
      .size(InputSize::Sm)
      .clearable(false)
      .style(SearchFieldPart::Root, |s| s.width(220.0))
      .on_input(move |event| {
        if let Some(write) = &write {
          write(event.new_value.clone());
        }
        for handler in &heard {
          handler(&event.new_value);
        }
      });
    if let Some(id) = id_of("bar-search") {
      search = search.id(id);
    }
    let mut bar = Toolbar::new()
      .label(&words.bar)
      .overflow(true)
      // The board's: 52 high, padded 12 across, 8 between, the large radius.
      .style(ToolbarPart::Root, |s| {
        s.width(Length::Fill)
          .height(52.0)
          .padding_x(12.0)
          .gap(8.0)
          .radius(RadiusSize::Lg)
      })
      .item(ToolbarItem::element(search).stays(true));
    // A tag for each filter set, its × taking it off.
    let change_filters = table.filter_changer();
    let titles: HashMap<Arc<str>, Arc<str>> = table
      .arranged
      .as_ref()
      .map(|arranged| arranged.1.iter().cloned().collect())
      .unwrap_or_default();
    for (id, filter) in table.filters.iter() {
      let title = titles.get(id).cloned().unwrap_or_else(|| Arc::from(id));
      let (change, now, id) = (change_filters.clone(), table.filters.clone(), id.to_owned());
      let tag_id = id_of(&format!("bar-filter-{id}"));
      let mut tag = Tag::new(&format!("{title}: {}", export::filter_said(filter)))
        .variant(TagVariant::Info)
        .on_remove(move || {
          if let Some(change) = &change {
            change(now.clone().without(&id));
          }
        })
        .style(TagPart::Root, |s| s.height(26.0))
        .style(TagPart::Label, |s| s.font_size(12.0));
      if let Some(tag_id) = tag_id {
        tag = tag.id(tag_id);
      }
      bar = bar.item(ToolbarItem::element(tag));
    }
    // Filter: a menu of the columns that filter, each opening the panel of its filter under its header.
    let filtering: Vec<(Arc<str>, Arc<str>)> = table
      .columns
      .iter()
      .filter(|column| column.filter.is_some())
      .map(|column| (column.id.clone(), column.title.clone()))
      .collect();
    if !filtering.is_empty() && change_filters.is_some() {
      let (open, anchor) = state.bar_filter.clone();
      let menu = Menu::new(open.clone(), anchor.clone())
        .label(&words.filter)
        .items(filtering.into_iter().map(|(id, title)| {
          let host = state.clone();
          MenuItem::new(&title).on_select(move || {
            *host.filters.column.lock() = Some(id.clone());
            host.filter_open.set(true);
            host.rebuild();
          })
        }))
        .build(&current_theme());
      let mut button = Button::new(&words.filter)
        .icon(icons::PLUS)
        .variant(ButtonVariant::Outline)
        .size(ButtonSize::Xs)
        // The board's is 26 high, its icon and its text 12, padded 8.
        .style(ButtonPart::Root, |s| s.height(26.0).padding_x(8.0).gap(4.0))
        .style(ButtonPart::Icon, |s| s.square(12.0))
        .style(ButtonPart::Label, |s| s.font_size(12.0))
        .on_click(move |_| open.update(|open| *open = !*open));
      if let Some(id) = id_of("bar-filter") {
        button = button.id(id);
      }
      bar = bar.item(ToolbarItem::element(
        Row::new()
          .overflow_visible()
          .ref_element(anchor)
          .child(button.build(&current_theme()))
          .child(menu),
      ));
    }
    bar = bar.item(Toolbar::spacer());
    // The density, which the grid takes.
    let rebuild = state.clone();
    bar = bar.item(
      ToggleGroup::new(
        state.bar_density.clone(),
        [
          Choice::new(2u8, &words.comfortable).icon(icons::ROWS_2).icon_only(true),
          Choice::new(3u8, &words.default).icon(icons::ROWS_3).icon_only(true),
          Choice::new(4u8, &words.compact).icon(icons::ROWS_4).icon_only(true),
        ],
      )
      .on_change(move |_| {
        *rebuild.bar_chose.lock() = true;
        rebuild.rebuild();
      })
      // The board's cells are 30 with icons of 14.
      .style(ToggleGroupPart::Item, |s| s.square(30.0))
      .label(&words.density)
      .id(id_of("bar-density").unwrap_or_else(|| "bar-density".to_owned())),
    );
    // The columns to show, as a column's menu lists them.
    if let (Some(arranged), Some(change)) = (table.arranged.clone(), table.layout_changer()) {
      let (open, anchor) = state.bar_columns.clone();
      let (layout, titles) = &*arranged;
      let shown = layout.columns.iter().filter(|place| !place.hidden).count();
      let menu = Menu::new(open.clone(), anchor.clone())
        .label(&words.columns)
        .items(titles.iter().map(|(id, title)| {
          let hidden = layout.place(id).is_some_and(|place| place.hidden);
          let (change, layout, id) = (change.clone(), layout.clone(), id.clone());
          MenuItem::new(title)
            .checked(!hidden)
            // The last column shown stays.
            .disabled(!hidden && shown == 1)
            .on_select(move || change(layout.clone().with_hidden(&id, !hidden)))
        }))
        .build(&current_theme());
      let mut button = Button::new(&words.columns)
        .trailing_icon(icons::CHEVRON_DOWN)
        .variant(ButtonVariant::Outline)
        // The board's is 30 high, padded 14 before its label and 10 after its chevron.
        .style(ButtonPart::Root, |s| s.height(30.0).padding_start(14.0).padding_end(10.0))
        .on_click(move |_| open.update(|open| *open = !*open));
      if let Some(id) = id_of("bar-columns") {
        button = button.id(id);
      }
      bar = bar.item(ToolbarItem::element(
        Row::new()
          .overflow_visible()
          .ref_element(anchor)
          .child(button.build(&current_theme()))
          .child(menu),
      ));
    }
    // Export: the rows shown or selected, as the column's menus write them.
    if table.export {
      let (open, anchor) = state.bar_export.clone();
      let selected = *state.export_selected.lock();
      let export = |scope: ExportScope, format: ExportFormat| {
        let host = state.clone();
        move || {
          let now = host.export_now.lock().clone();
          if let Some(now) = now {
            now(scope, format);
          }
        }
      };
      let labels = table.words.export.clone();
      let menu = Menu::new(open.clone(), anchor.clone())
        .label(&words.export)
        .item(MenuItem::new(&labels.shown_csv).on_select(export(ExportScope::Shown, ExportFormat::Csv)))
        .item(MenuItem::new(&labels.shown_tsv).on_select(export(ExportScope::Shown, ExportFormat::Tsv)))
        .item(
          MenuItem::new(&labels.selected_csv)
            .disabled(!selected)
            .on_select(export(ExportScope::Selected, ExportFormat::Csv)),
        )
        .item(
          MenuItem::new(&labels.selected_tsv)
            .disabled(!selected)
            .on_select(export(ExportScope::Selected, ExportFormat::Tsv)),
        )
        .build(&current_theme());
      let mut button = Button::new(&words.export)
        .icon(icons::DOWNLOAD)
        .variant(ButtonVariant::Outline)
        .style(ButtonPart::Root, |s| s.height(30.0).padding_x(10.0))
        .on_click(move |_| open.update(|open| *open = !*open));
      if let Some(id) = id_of("bar-export") {
        button = button.id(id);
      }
      bar = bar.item(ToolbarItem::element(
        Row::new()
          .overflow_visible()
          .ref_element(anchor)
          .child(button.build(&current_theme()))
          .child(menu),
      ));
    }
    if let Some(id) = id_of("bar") {
      bar = bar.id(id);
    }
    bar.key("grid-bar").mount(ctx)
  }

  fn find_bar(&self, ctx: &mut Ctx, table: &Table<R, K>) -> Element {
    let state = &self.state;
    // The query the app gives is the bar's: one it clears elsewhere is cleared in the bar.
    if state.find_query.get_untracked() != *table.search {
      state.find_query.set(table.search.to_string());
    }
    let matches = state.search.lock().matches();
    let (write, heard) = (table.write_search.clone(), table.on_search_change.clone());
    let (next, previous) = (state.clone(), state.clone());
    let mut bar = super::find_bar::FindBar::new(state.find_query.clone())
      .open(state.find_open.clone())
      .match_case(state.find_case.clone())
      .whole_word(state.find_word.clone())
      .matches(matches.current.map_or(0, |current| current + 1), matches.count)
      .on_query(move |query: &str| {
        if let Some(write) = &write {
          write(query.to_owned());
        }
        for handler in &heard {
          handler(query);
        }
      })
      .on_next(move || {
        next.step_match(1);
        next.rebuild();
      })
      .on_previous(move || {
        previous.step_match(-1);
        previous.rebuild();
      });
    if let Some(with) = &table.find_bar_with {
      bar = with(bar);
    }
    bar.mount(ctx)
  }
}

impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> Component for TableHost<R, K> {
  type Props = TableProps<R, K>;

  fn create(ctx: &mut Ctx) -> Self {
    Self {
      state: Arc::new(HostState {
        scroll: ScrollState::new(),
        tracks: Mutex::new(None),
        built: Mutex::new(Vec::new()),
        columns_built: Mutex::new(None),
        focused_column: Mutex::new(None),
        resized: Mutex::new(HashMap::new()),
        draft_text: ctx.signal(String::new()),
        draft_number: ctx.signal(None),
        draft_choice: ctx.signal(0),
        editing: Mutex::new(None),
        pending_focus: Mutex::new(None),
        cell_refs: Mutex::new(HashMap::new()),
        scroll_ref: ElementRef::new(),
        drag_anchor: Mutex::new(None),
        drag_last: Mutex::new(None),
        menu_open: Mutex::new(None),
        menu_anchor: ElementRef::new(),
        submenu_open: ctx.signal(false),
        submenu_anchor: ElementRef::new(),
        export_open: ctx.signal(false),
        export_anchor: ElementRef::new(),
        bar_query: ctx.signal(String::new()),
        bar_density: ctx.signal(0_u8),
        bar_filter: (ctx.signal(false), ElementRef::new()),
        bar_columns: (ctx.signal(false), ElementRef::new()),
        bar_export: (ctx.signal(false), ElementRef::new()),
        export_now: Mutex::new(None),
        export_selected: Mutex::new(false),
        own_search: Mutex::new(String::new()),
        bar_chose: Mutex::new(false),
        header_refs: Mutex::new(HashMap::new()),
        summaries: Mutex::new(HashMap::new()),
        header_drop: Mutex::new(None),
        lines: Mutex::new(Arc::new(Lines::default())),
        key_index: Mutex::default(),
        kept_lines: Mutex::default(),
        heights: Arc::default(),
        filter_open: ctx.signal(false),
        filter_anchor: ElementRef::new(),
        filters: FilterState::default(),
        search: Mutex::default(),
        find_open: ctx.signal(false),
        find_query: ctx.signal(String::new()),
        find_case: ctx.signal(false),
        find_word: ctx.signal(false),
        find_regex: ctx.signal(false),
        own_id: {
          static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
          Arc::from(format!(
            "grid-{}",
            NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
          ))
        },
        source: Mutex::new(None),
        source_query: Mutex::new(None),
        group_toggle: Mutex::new(None),
        tree_toggle: Mutex::new(None),
        detail_toggle: Mutex::new(None),
        metrics: Mutex::new(Metrics::default()),
        focused: Mutex::new(None),
        revealed: Mutex::new(None),
        revision: ctx.signal(0_u64),
      }),
      own_range: Arc::new(Mutex::new(None)),
      own_layout: Arc::new(Mutex::new(None)),
      own_open: Arc::new(Mutex::new(Vec::new())),
      own_expanded: Arc::new(Mutex::new(Vec::new())),
      own_details: Arc::new(Mutex::new(Vec::new())),
      _marker: PhantomData,
    }
    .started(ctx)
  }

  fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
    let mut table = ctx.props::<TableProps<R, K>>().table.lock().clone();
    if table.grid && table.id.is_none() {
      table.id = Some(self.state.own_id.clone());
    }
    // Over a source the rows are those that have come for the sort, the filters and the search asked for: the
    // groups are not the grid's to make.
    if let Some(source) = table.source.clone() {
      let query = GridQuery {
        sort: table.sort.clone(),
        filters: table.filters.clone(),
        search: table.search.to_string(),
      };
      let (rows, _) = source.rows(&mut self.state.source.lock(), &query);
      table.rows = rows;
      table.group_by.clear();
      if let Some(query) = source.untold(&mut self.state.source.lock()) {
        *self.state.source_query.lock() = Some((query, table.on_query_change.clone()));
      }
    }
    let _ = self.state.revision.get();
    // The widths a person made the columns are the table's.
    table.resized = Arc::new(self.state.resized.lock().clone());
    // A grid whose app holds no range of cells keeps its own.
    if table.grid && table.write_cell_range.is_none() {
      table.cell_range = self.own_range.lock().clone();
      let (own, state) = (self.own_range.clone(), self.state.clone());
      table.write_cell_range = Some(Arc::new(move |next| {
        *own.lock() = next;
        state.rebuild();
      }));
    }
    // The groups a table whose app holds none keeps open itself.
    if !table.group_by.is_empty() && table.write_open.is_none() {
      table.open_groups = self.own_open.lock().clone();
      let (own, state) = (self.own_open.clone(), self.state.clone());
      table.write_open = Some(Arc::new(move |next| {
        *own.lock() = next;
        state.rebuild();
      }));
    }
    // A grid with its bar searches what the bar's field says: the app's search, or its own where the app holds none.
    // The bar's density is the grid's once a person chose one.
    if table.grid && table.toolbar {
      if table.write_search.is_none() {
        table.search = Arc::from(self.state.own_search.lock().as_str());
        let (own, state) = (self.state.clone(), self.state.clone());
        table.write_search = Some(Arc::new(move |next| {
          *own.own_search.lock() = next;
          state.rebuild();
        }));
      }
      let chosen = self.state.bar_density.get();
      if *self.state.bar_chose.lock() {
        table.density = Some(density_of(chosen));
      } else {
        // Until a person chooses, the bar shows the density the grid has: the app's, or the theme's.
        let now = density_code(table.density.unwrap_or_else(|| current_theme().density()));
        if chosen != now {
          self.state.bar_density.set(now);
        }
      }
    }
    // The rows of a tree open, and the details, that a table whose app holds none keeps itself.
    if table.tree_parent.is_some() && table.write_expanded.is_none() {
      table.expanded = self.own_expanded.lock().clone();
      let (own, state) = (self.own_expanded.clone(), self.state.clone());
      table.write_expanded = Some(Arc::new(move |next| {
        *own.lock() = next;
        state.rebuild();
      }));
    }
    if table.detail.is_some() && table.write_details.is_none() {
      table.details_open = self.own_details.lock().clone();
      let (own, state) = (self.own_details.clone(), self.state.clone());
      table.write_details = Some(Arc::new(move |next| {
        *own.lock() = next;
        state.rebuild();
      }));
    }
    table.find_grouping();
    // A grid's columns as a person arranged them: in their order, the hidden ones out, their widths and pins.
    if table.grid {
      if table.write_column_layout.is_none() {
        table.column_layout = self.own_layout.lock().clone();
        let (own, state) = (self.own_layout.clone(), self.state.clone());
        table.write_column_layout = Some(Arc::new(move |next| {
          *own.lock() = Some(next);
          state.rebuild();
        }));
      }
      let columns = std::mem::take(&mut table.columns);
      let full = table.column_layout.clone().unwrap_or_default().full(&columns);
      let titles = full
        .columns
        .iter()
        .filter_map(|place| {
          let column = columns.iter().find(|column| column.id == place.id)?;
          Some((column.id.clone(), column.title.clone()))
        })
        .collect();
      table.columns = full.arrange(columns);
      table.arranged = Some(Arc::new((full, titles)));
    }
    let theme = current_theme();
    let state = &self.state;

    // The tracks are kept while the columns are the same: what they measured stays.
    let specs = table.track_specs(&theme);
    let tracks = {
      let mut held = state.tracks.lock();
      match &*held {
        Some((kept, tracks)) if *kept == specs => tracks.clone(),
        _ => {
          let tracks = ColumnTracks::new(specs.clone());
          *held = Some((specs, tracks.clone()));
          // Columns of other widths wrap their text otherwise: the lines are measured again.
          *state.heights.lock() = Heights::default();
          tracks
        }
      }
    };

    let recipe = table.resolve(&theme, &table.case());
    let row_height = px(recipe.part(TablePart::Row).rest().height, &theme).unwrap_or(0.0);
    let header_height = px(recipe.part(TablePart::Header).rest().height, &theme).unwrap_or(0.0);
    // The lines shown are what the window is reckoned in: the rows, or the groups and the rows of the open ones.
    // Rows grouped are laid out once for the rows given, the columns grouped by and the groups open: a render
    // that changes none of them (a row selected, a scroll) takes the lines as they were.
    let lines = {
      let mut kept = state.kept_lines.lock();
      let wanted = (
        table
          .grouping
          .iter()
          .map(|column| column.id.clone())
          .collect::<Vec<_>>(),
        table.open_groups.clone(),
        table.summarized(),
        table.filters.clone(),
        {
          // The tree's rows open and the details open, as one number.
          use std::hash::Hasher;
          let mut hasher = std::collections::hash_map::DefaultHasher::new();
          (table.tree_parent.is_some(), &table.expanded).hash(&mut hasher);
          (table.detail.is_some(), &table.details_open).hash(&mut hasher);
          hasher.finish()
        },
      );
      match &*kept {
        Some((rows, held, lines))
          if held == &wanted
            && rows
              .downcast_ref::<Arc<Vec<R>>>()
              .is_some_and(|rows| Arc::ptr_eq(rows, &table.rows)) =>
        {
          lines.clone()
        }
        _ => {
          let lines = Arc::new(table.lines());
          *kept = Some((Arc::new(table.rows.clone()), wanted, lines.clone()));
          lines
        }
      }
    };
    *state.lines.lock() = lines.clone();
    let count = lines.len();
    // What the lines measured holds while they and the row's height are the same.
    {
      let mut heights = state.heights.lock();
      if !heights.is_for(row_height, count) {
        *heights = Heights::new(row_height, count);
      }
    }
    let geometry = Geometry::of(&recipe);
    let pins = table.pins();
    let lead_tracks = table.lead_tracks();
    *state.metrics.lock() = Metrics {
      row_height,
      header_height,
      count,
      disabled: table.disabled,
      // A row whose cells stay at an end holds its padding there in its pinned cell, in a track: the numbers' at
      // the start where it has them.
      padding_start: if pins.0 > 0 || table.numbered() {
        0.0
      } else {
        geometry.padding_start
      },
      padding_end: if pins.1 > 0 { 0.0 } else { geometry.padding_end },
      lead_tracks,
      pins,
    };
    // The columns in view, and the pinned and the focused one; every column before the table knows its width.
    let columns_built = state.columns_at(state.scroll.scroll_x()).map(Arc::new);
    *state.columns_built.lock() = columns_built.clone();

    // The matches of the search in the rows shown, kept while nothing they are found in changes.
    let searched = (!table.search.is_empty() && table.source.is_none()).then(|| {
      let options = super::highlight::FindOptions {
        match_case: state.find_case.get() || table.search_options.match_case,
        whole_word: state.find_word.get() || table.search_options.whole_word,
        regex: state.find_regex.get() || table.search_options.regex,
      };
      let texts: Vec<Option<TextFn<R>>> = table
        .columns
        .iter()
        .map(|column| match &column.content {
          Content::Text(text) => Some(text.clone()),
          _ => None,
        })
        .collect();
      let ids = table.columns.iter().map(|column| column.id.clone()).collect();
      let mut search = state.search.lock();
      search.heard = table.on_matches_change.clone();
      let found = search.find(&table.rows, &lines, ids, &texts, &table.search, options);
      (found, search.current)
    });
    if searched.is_none() {
      let mut search = state.search.lock();
      search.heard = table.on_matches_change.clone();
      search.find(
        &table.rows,
        &lines,
        Vec::new(),
        &[],
        "",
        super::highlight::FindOptions::default(),
      );
    }
    // A recipe whose rows have no height in pixels builds every row: where they are cannot be reckoned.
    let window = (row_height > 0.0).then(|| {
      let (first, last) = state.range(OVERSCAN).unwrap_or((0, FIRST_ROWS.min(count)));
      let focused = state.focused.lock().filter(|index| *index < count);
      let mut indices: Vec<usize> = (first..last.min(count)).collect();
      if let Some(focused) = focused
        && !indices.contains(&focused)
      {
        indices.push(focused);
        indices.sort_unstable();
      }
      *state.built.lock() = indices.clone();
      let refs: HashMap<usize, ElementRef> = indices.iter().map(|index| (*index, ElementRef::new())).collect();
      // Rows that grow with what they hold are measured as they are laid out, before they are painted: the table
      // is built again at once where one is higher or lower than it was taken to be.
      if table.tall() {
        for (position, reference) in &refs {
          let (watcher, position) = (state.clone(), *position);
          reference.observe_rect_before_paint(move |rect| {
            if watcher.measured(position, rect.height) {
              watcher.rebuild();
            }
          });
        }
      }
      // A grid's cells have refs of their own, by the rank of their row, which the focus is sent to.
      let cell_refs: HashMap<(usize, usize), ElementRef> = if table.grid {
        let built = |column: usize| columns_built.as_ref().is_none_or(|built| built[column]);
        indices
          .iter()
          .filter_map(|position| lines.rank_at(*position))
          .flat_map(|row| {
            (0..table.columns.len())
              .filter(|column| built(*column))
              .map(move |column| ((row, column), ElementRef::new()))
          })
          .collect()
      } else {
        HashMap::new()
      };
      *state.cell_refs.lock() = cell_refs.clone();
      let editor_ref = ElementRef::new();
      let pending = *state.pending_focus.lock();
      match pending {
        Some(FocusAim::Editor) => {
          ctx.focus(&editor_ref);
          *state.pending_focus.lock() = None;
        }
        Some(FocusAim::Cell(row, column)) => match cell_refs.get(&(row, column)) {
          Some(target) => {
            ctx.focus(target);
            *state.pending_focus.lock() = None;
          }
          // A row not built yet is scrolled to; the focus goes to it once it is.
          None
            if lines
              .position_of(row)
              .is_some_and(|position| !indices.contains(&position)) =>
          {
            state.reveal(lines.position_of(row).unwrap_or(row))
          }
          None => *state.pending_focus.lock() = None,
        },
        None => {}
      }
      let watcher = state.clone();
      let on_scroll: Arc<dyn Fn(ScrollEvent) + Send + Sync> = Arc::new(move |event: ScrollEvent| {
        // Handlers run before the scroll moves: the rows are reckoned from where it is going.
        let metrics = *watcher.metrics.lock();
        let scroll_y = (watcher.scroll.scroll_y() - event.delta_y).max(0.0);
        let Some((first, last)) = watcher.range_at(scroll_y, 0) else {
          return;
        };
        let built = watcher.built.lock().clone();
        let scroll_x = (watcher.scroll.scroll_x() - event.delta_x).max(0.0);
        if (first.saturating_sub(OVERSCAN / 2)..(last + OVERSCAN / 2).min(metrics.count))
          .any(|index| !built.contains(&index))
          || watcher.needs_columns(scroll_x)
        {
          watcher.rebuild();
        }
      });
      Window {
        indices,
        focused,
        row_height,
        scroll: state.scroll.clone(),
        refs,
        cell_refs,
        editor_ref,
        columns: columns_built,
        host: Some(Arc::new(HostLink { state: state.clone() })),
        rows: state.clone(),
        lines: lines.clone(),
        on_scroll: Some(on_scroll),
        search: searched.clone(),
      }
    });
    // Over a source: the ranges of the rows built are asked for, and the view is told where each range is.
    let slots = table.source.clone().map(|source| {
      if let Some(window) = &window
        && let (Some(first), Some(last)) = (window.indices.first(), window.indices.last())
      {
        source.want(&mut state.source.lock(), *first, *last);
      }
      let slots = source.slots(&mut state.source.lock());
      let (again, retried) = (source.clone(), state.clone());
      let retry: Arc<dyn Fn(usize) + Send + Sync> = Arc::new(move |index| {
        again.retry(&mut retried.source.lock(), index);
        retried.rebuild();
      });
      (slots, retry)
    });
    table.slots = slots;
    let find = table.find_bar.then(|| self.find_bar(ctx, &table));
    let view = table.view(&theme, tracks, window);
    let mut element = match theme.renderer::<Table<R, K>>() {
      Some(renderer) => renderer(view),
      None => view.render(),
    };
    // The bar of the grid's tools over it.
    if table.grid && table.toolbar {
      let bar = self.grid_bar(ctx, &table);
      element = Column::new()
        .spacing(8.0)
        .width(Dimension::Pct(100.0))
        .child(bar)
        .child(element)
        .into();
    }
    if !table.find_bar && table.search.is_empty() {
      return element;
    }
    // F3 and Shift+F3 go through the matches; Ctrl+F opens the find bar over the grid.
    let (keys, opens) = (state.clone(), table.find_bar);
    let mut stack = components::Stack::new()
      .width(Dimension::Pct(100.0))
      .on_key_down(move |event: KeyboardEvent| {
        let command = event.ctrl || event.meta;
        if event.key == "F3" {
          event.prevent_default();
          keys.step_match(if event.shift { -1 } else { 1 });
          keys.rebuild();
        } else if opens && command && !event.alt && (event.code == "KeyF" || event.key.eq_ignore_ascii_case("f")) {
          event.prevent_default();
          keys.find_open.set(true);
        }
      })
      .child(element);
    if let Some(bar) = find {
      stack = stack.child(bar);
    }
    stack.into()
  }

  fn after_layout(&self) {
    // A scroll the handler did not see (the bar dragged, a row scrolled into view) or a height first known.
    if self.state.needs_rows() || self.state.needs_columns(self.state.scroll.scroll_x()) {
      self.state.rebuild();
    }
    // The app hears what the rows are asked by, once a person has changed it.
    let props_source = self.state.source_query.lock().take();
    if let Some((query, heard)) = props_source {
      for handler in &heard {
        handler(&query);
      }
    }
    // The app hears how many matches the search has once the render that found them is laid out.
    let untold = self.state.search.lock().untold();
    if let Some((matches, heard)) = untold {
      for handler in &heard {
        handler(matches);
      }
    }
  }
}

impl<R: Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static> crate::ui::Styled for Table<R, K> {
  type Component = Self;

  fn rules(self, rules: &crate::ui::Recipe) -> Self {
    Self::rules(self, rules)
  }

  fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
    Self::state(self, state, on)
  }
}

impl<R: Send + Sync + 'static, K: Send + Sync + 'static> Themed for Table<R, K> {
  const NAME: &'static str = "table";
  type Renderer = TableRenderer;

  fn parts() -> crate::ui::Declared {
    TablePart::declared()
  }

  fn states() -> crate::ui::Declared {
    TableState::declared()
  }

  /// The design's table (`pencil/lurq-design-system.pen`: Table, Table/Empty, Table/Loading, and the Data
  /// Grid for what a table whose rows are selected has): a box with the large radius, an inner edge of 1 in
  /// the border's color and the background's fill. The header is 40 high on the bar's fill with a line of 1
  /// under it, its titles 13/500 in the muted foreground; a row is 44 high with a line of 1 under it but the
  /// last, its text 13 in the foreground. Both are padded 16 at each side with 16 between columns, 12 where
  /// the rows have checkboxes, whose column is 16 wide. The column the rows are sorted by has its title in the
  /// foreground and an arrow of 14 4 after it. A selected row is in the highlight (the board's `$accent`). A
  /// column of numbers is mono 12 in the muted foreground at its end. The footer is 44 high, padded 16 at the
  /// start and 8 at the end, its count 13 in the muted foreground. Where the board draws nothing: under the
  /// pointer and held down a row has the veils of those states, a sortable header's title is in the
  /// foreground under the pointer, the focus from the keyboard is the ring inside a row, a sortable header
  /// has a muted mark that it sorts, and a disabled table is faded. The densities are a step of the theme's
  /// rows around the board's 44: 36 compact, 52 comfortable and touch, the header 4 lower.
  fn recipe() -> Recipe {
    use PaletteColor::{Background, Foreground, Highlight, Muted, MutedForeground, Ring, StateHover, StatePressed};
    use TablePart::{
      Count, Empty, FilterIcon, FilterLabel, FilterRow, Footer, Header, HeaderCell, Root, Row, Select, SortIcon,
      SortOrder,
    };
    use TableState::{Disabled, Last, Numeric, Selectable, Selected, Sortable, Sorted};
    let line = || Line::inside(BorderSize::Sm, PaletteColor::Border);
    let range_edge = || Line::inside(BorderSize::Md, Ring);
    Recipe::new()
      .base(Root, |s| {
        s.width(Length::Fill)
          .radius(RadiusSize::Lg)
          .border(line())
          .background(Background)
          .foreground(Foreground)
      })
      // What scrolls clips what is in it to its own corners, the box's: a clip of its own inside the box would cut
      // the header's fill square over the box's round corners.
      .base(TablePart::Scroll, |s| s.radius(RadiusSize::Lg))
      .state(TableState::Footer, TablePart::Scroll, |s| {
        s.corners(RadiusSize::Lg, RadiusSize::Lg, 0.0, 0.0)
      })
      .base(Header, |s| {
        s.height(40.0)
          .padding_x(16.0)
          .gap(16.0)
          .background(PaletteColor::Bar)
          .underline(line())
      })
      .base(HeaderCell, |s| {
        s.gap(4.0)
          .typography(TypographyStyle::Body)
          .font_size(13.0)
          .weight(FontWeight::Medium)
          .foreground(MutedForeground)
      })
      .base(SortIcon, |s| s.square(IconSize::Sm))
      .base(FilterIcon, |s| s.square(IconSize::Sm).foreground(Foreground))
      .base(FilterLabel, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(12.0)
          .foreground(MutedForeground)
      })
      .base(FilterRow, |s| {
        s.height(44.0)
          .padding_x(16.0)
          .gap(16.0)
          .background(Background)
          .underline(line())
      })
      .base(SortOrder, |s| {
        s.typography(TypographyStyle::Mono)
          .font_size(11.0)
          .foreground(MutedForeground)
      })
      // A column's edge: 8 of room that takes the pointer, a line of 2 in the ring's color under it and while it is
      // dragged, nothing at rest, as the board draws no line between headers.
      .base(TablePart::Resizer, |s| s.width(8.0).cursor(CursorIcon::ColResize))
      .base(TablePart::ResizeLine, |s| s.width(2.0).radius(RadiusSize::Full))
      // A grid's column menu: a ghost button of 24 at the end of the header, its mark muted, shown while the
      // pointer is on the header, the focus is in it, or the menu is open.
      .base(TablePart::MenuTrigger, |s| {
        fading(
          s.square(24.0)
            .radius(RadiusSize::Sm)
            .foreground(MutedForeground)
            .cursor(CursorIcon::Pointer)
            .opacity(0.0),
        )
      })
      .within(TablePart::MenuTrigger, |s| s.opacity(1.0))
      .state(TableState::Open, TablePart::MenuTrigger, |s| s.opacity(1.0).background(Muted))
      .hovered(TablePart::MenuTrigger, |s| s.veil(StateHover).foreground(Foreground))
      // Where a column dragged by its header goes: a line of 2 in the ring's color.
      .state(TableState::DropBefore, TablePart::DropLine, |s| s.start_line(range_edge()))
      .state(TableState::DropAfter, TablePart::DropLine, |s| s.end_line(range_edge()))
      .within_hovered(TablePart::ResizeLine, |s| s.background(Ring))
      .within_pressed(TablePart::ResizeLine, |s| s.background(Ring))
      .state(Sortable, HeaderCell, |s| fading(s.radius(RadiusSize::Sm).cursor(CursorIcon::Pointer)))
      .hovered(HeaderCell, |s| s.foreground(Foreground))
      .focus_visible(HeaderCell, |s| s.focus_ring())
      .state(Sorted, HeaderCell, |s| s.foreground(Foreground))
      .base(Row, |s| {
        fading(
          s.height(44.0)
            .padding_x(16.0)
            .gap(16.0)
            .underline(line())
            .cursor(CursorIcon::Pointer),
        )
      })
      .hovered(Row, |s| s.veil(StateHover))
      .pressed(Row, |s| s.veil(StatePressed))
      .focus_visible(Row, |s| s.outline(Outline::new(BorderSize::Md, Ring).offset(-2.0)))
      .state(Selected, Row, |s| s.background(Highlight))
      .state(Last, Row, |s| s.underline(Line::inside(0.0, PaletteColor::Border)))
      .state(Selectable, Header, |s| s.gap(12.0))
      .state(Selectable, Row, |s| s.gap(12.0))
      .base(TablePart::Cell, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(13.0)
          .foreground(Foreground)
      })
      .state(TableState::Mono, TablePart::Cell, |s| s.font_family(FontFamily::Mono))
      .state(TableState::Muted, TablePart::Cell, |s| s.foreground(MutedForeground))
      .state(Numeric, TablePart::Cell, |s| {
        s.font_family(FontFamily::Mono)
          .font_size(12.0)
          .foreground(MutedForeground)
      })
      .base(Select, |s| s.width(16.0).cursor(CursorIcon::Pointer))
      // The rows' numbers: mono 12 in the muted foreground at the end of their column, the selected row's in the
      // foreground, pressed to select.
      .base(TablePart::RowNumber, |s| {
        s.font_family(FontFamily::Mono)
          .font_size(12.0)
          .foreground(MutedForeground)
      })
      .state(Selectable, TablePart::RowNumber, |s| s.cursor(CursorIcon::Pointer))
      .state(Selected, TablePart::RowNumber, |s| s.foreground(Foreground))
      .when(Selector::new().state(TableState::Pinned).within().hovered(), TablePart::RowNumber, |s| {
        s.veil(StateHover)
      })
      .when(Selector::new().state(TableState::Pinned).within().pressed(), TablePart::RowNumber, |s| {
        s.veil(StatePressed)
      })
      // A cell of a data grid takes the focus: from the keyboard, the ring inside it, as a row's.
      .focus_visible(TablePart::Cell, |s| s.outline(Outline::new(BorderSize::Md, Ring).offset(-2.0)))
      .base(TablePart::Editor, |s| s.width(Length::Fill))
      // The range of cells of a grid: one area in the selection's fill, edged in the ring's color, the focused cell
      // in it on its row's fill.
      .state(TableState::InRange, TablePart::Cell, |s| s.background(PaletteColor::Selection))
      .state(TableState::RangeTop, TablePart::Cell, |s| s.top_line(range_edge()))
      .state(TableState::RangeBottom, TablePart::Cell, |s| s.underline(range_edge()))
      .state(TableState::RangeStart, TablePart::Cell, |s| s.start_line(range_edge()))
      .state(TableState::RangeEnd, TablePart::Cell, |s| s.end_line(range_edge()))
      // The cells that stay cover the row's veil with its fill: they have it themselves.
      .when(Selector::new().state(TableState::Pinned).within().hovered(), TablePart::Cell, |s| {
        s.veil(StateHover)
      })
      .when(Selector::new().state(TableState::Pinned).within().pressed(), TablePart::Cell, |s| {
        s.veil(StatePressed)
      })
      .when(Selector::new().state(TableState::Pinned).within().hovered(), Select, |s| s.veil(StateHover))
      .when(Selector::new().state(TableState::Pinned).within().pressed(), Select, |s| s.veil(StatePressed))
      // A group's row: as high as a row, on the muted fill, its chevron muted, its value 13/500 in the foreground
      // and its count mono 12 muted after it.
      .base(TablePart::GroupRow, |s| {
        fading(
          s.height(44.0)
            .background(Muted)
            .underline(line())
            .cursor(CursorIcon::Pointer),
        )
      })
      .hovered(TablePart::GroupRow, |s| s.veil(StateHover))
      .pressed(TablePart::GroupRow, |s| s.veil(StatePressed))
      .focus_visible(TablePart::GroupRow, |s| s.outline(Outline::new(BorderSize::Md, Ring).offset(-2.0)))
      .base(TablePart::Chevron, |s| s.square(IconSize::Sm).foreground(MutedForeground))
      // A row's detail: on the muted fill under its row, 12 down and 16 across, the line under it a row's.
      .base(TablePart::Detail, |s| {
        s.padding_x(16.0)
          .padding_y(12.0)
          .background(Muted)
          .underline(line())
      })
      // The chevron of a row of a tree or of a row with a detail, 6 before the cell's content, pressed.
      .base(TablePart::TreeToggle, |s| {
        fading(
          s.square(IconSize::Sm)
            .radius(RadiusSize::Sm)
            .cursor(CursorIcon::Pointer)
            .foreground(MutedForeground),
        )
      })
      .hovered(TablePart::TreeToggle, |s| s.foreground(Foreground))
      .base(TablePart::GroupLabel, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(13.0)
          .weight(FontWeight::Medium)
          .foreground(Foreground)
      })
      .base(TablePart::GroupCount, |s| {
        s.font_family(FontFamily::Mono)
          .font_size(12.0)
          .foreground(MutedForeground)
      })
      .density(Density::Compact, TablePart::GroupRow, |s| s.height(Density::Default))
      .density(Density::Comfortable, TablePart::GroupRow, |s| s.height(52.0))
      .density(Density::Touch, TablePart::GroupRow, |s| s.height(52.0))
      // A summary row: as high as a row, on the muted fill with a line over it, its labels 12 in the muted foreground
      // and its values 13/500 in the foreground.
      .base(TablePart::Summary, |s| {
        s.height(44.0)
          .padding_x(16.0)
          .gap(16.0)
          .background(Muted)
          .top_line(line())
      })
      .state(Selectable, TablePart::Summary, |s| s.gap(12.0))
      .base(TablePart::SummaryLabel, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(12.0)
          .foreground(MutedForeground)
      })
      .base(TablePart::SummaryValue, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(13.0)
          .weight(FontWeight::Medium)
          .foreground(Foreground)
      })
      .density(Density::Compact, TablePart::Summary, |s| s.height(Density::Default))
      .density(Density::Comfortable, TablePart::Summary, |s| s.height(52.0))
      .density(Density::Touch, TablePart::Summary, |s| s.height(52.0))
      .base(Empty, |s| s.width(Length::Fill))
      .base(Footer, |s| s.min_height(44.0).padding_start(16.0).padding_end(8.0))
      .base(Count, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(13.0)
          .foreground(MutedForeground)
      })
      .density(Density::Compact, Row, |s| s.height(Density::Default))
      .density(Density::Compact, Header, |s| s.height(32.0))
      .density(Density::Comfortable, Row, |s| s.height(52.0))
      .density(Density::Comfortable, Header, |s| s.height(48.0))
      .density(Density::Touch, Row, |s| s.height(52.0))
      .density(Density::Touch, Header, |s| s.height(48.0))
      .state(Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
      .state(Disabled, Row, |s| s.cursor(CursorIcon::Default))
  }
}

/// A density as the grid's bar holds it: 2 comfortable, 3 the default, 4 compact.
fn density_code(density: Density) -> u8 {
  match density {
    Density::Comfortable | Density::Touch => 2,
    Density::Compact => 4,
    _ => 3,
  }
}

/// The density of the grid's bar's choice.
fn density_of(code: u8) -> Density {
  match code {
    2 => Density::Comfortable,
    4 => Density::Compact,
    _ => Density::Default,
  }
}