frust-material 0.6.0

Material 3 (Expressive) design-system catalog for Frust: HCT seed-color tokens, shape morphing, icons, widgets and bundled Roboto fonts.
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
// Ported from material_3_expressive v1.0.8 (MIT, © 2026 Paa Developments),
// `lib/components/toolbars/` — `m3e_toolbars.dart`,
// `m3e_toolbar_scroll_behavior.dart`, `controllers/m3e_toolbar_visibility_controller.dart`,
// `components/` (`m3e_toolbar_build.dart`, `m3e_toolbar_body.dart`,
// `m3e_toolbar_actions_row.dart`, `m3e_toolbar_fab_layout.dart`,
// `m3e_toolbar_fab_slot.dart`, `m3e_toolbar_overflow_menu.dart`,
// `m3e_toolbar_icon_button.dart`, `m3e_toolbar_measure_size.dart`,
// `m3e_toolbar_title_block.dart`, `m3e_toolbar_expanding_actions.dart`),
// `enums/m3e_toolbar_enums.dart`, `models/m3e_toolbar_item.dart`,
// `res/m3e_toolbar_tokens.dart`, `styles/m3e_toolbar_theme.dart`, and
// `utils/` (`m3e_toolbar_item_layout.dart`, `m3e_toolbar_spring_motion.dart`)
// (retrieved 2026-08-20). Upstream:
// https://github.com/paadevelopments/material_3_expressive
// Porting decisions (each restated with its reference site in the module docs
// below): expansion and scroll-hide are app-owned controlled props rather than
// the reference's internal `_expanded` mirror and ticker-driven visibility
// controller; the adjacent FAB is morphed through this widget's own tight
// constraints rather than the reference's `FittedBox` rescale; the overflow
// panel is a second, app-mounted view rather than something this widget hosts;
// and the vertical axis, the title/subtitle block and the labeled-action width
// spring stay unported.

//! The M3 Expressive **floating/docked toolbar**
//! (m3.material.io/components/toolbars/specs; Compose
//! `HorizontalFloatingToolbar`/`FlexibleBottomAppBar`).
//!
//! [`ToolbarView`]/[`ToolbarWidget`] mirror [`super::appbar`]'s slot
//! conventions (read that module first): **leading**, **center**, and
//! **trailing** slots, each an ordered `Vec` of opaque [`AnyView`] children
//! this widget lays out and routes events to but never paints or tints itself.
//! An optional **fab** slot (`.fab(...)`) is the adjacent prominent action.
//! Alongside the opaque slots sits a *typed* [`ToolbarAction`] list
//! ([`ToolbarView::actions`], upstream's own `actions`) — the only slot this
//! widget composes itself, because it is the one that has to survive the
//! overflow split below.
//!
//! # Variants ([`ToolbarVariant`]) and color styles ([`ToolbarColorStyle`])
//!
//! * **Floating**: self-sized (hugs its content), a fully-rounded pill
//!   (`shape.full`, resolved to a true pill via [`frust::ShapeScale::resolve`]
//!   against the bar's own fixed height). "Floating with inset margins above
//!   content" describes the *placement* a caller gives it (a [`frust::Stack`]
//!   with [`frust::Align`]/[`frust::Padding`] around this self-sized widget) —
//!   this widget implements no margin/anchoring logic of its own.
//! * **Docked**: fills the available width (`bc.max().width`, like
//!   [`super::appbar`]/[`super::navbar`]), flat geometry (`shape.none`) — the
//!   flat replacement for the static bottom-app-bar pattern. The leading group
//!   hugs the leading edge, the trailing group (+ the fab slot, if any) the
//!   trailing edge in reading order, and the center group is centered as a
//!   whole between them.
//!
//! `M3EToolbarTheme.colors` maps the two color styles onto four roles each:
//!
//! | Style | container | content | fab container | fab content |
//! |---|---|---|---|---|
//! | [`ToolbarColorStyle::Standard`] | `surface_container` | `on_surface` | `primary_container` | `on_primary_container` |
//! | [`ToolbarColorStyle::Vibrant`] | `primary_container` | `on_primary_container` | `tertiary_container` | `on_tertiary_container` |
//!
//! [`ToolbarColors::resolve`] is that table as a public function. Only
//! **container** is applied here; the other three are *advisory*, and for the
//! same reason: content is caller-owned. The fab slot is an opaque caller view
//! like every other slot, so a caller pairs a vibrant bar with
//! [`crate::FabColor::Tertiary`] (upstream's own
//! `style == vibrant ? M3EFabColor.tertiary : M3EFabColor.primary`) itself; and
//! `content` reaches upstream's own inline actions only through
//! `M3EToolbarTheme.scopedTheme`, a `Theme`-override push around the whole
//! content band that has no analogue on this framework's public surface — the
//! same ambient-tint gap [`mod@super::appbar`] documents for its own slots. A
//! vibrant bar's actions therefore keep [`mod@crate::icon_button`]'s own
//! `on_surface_variant` ink; an app that needs the vibrant pairing supplies its
//! own tinted views in the `center` slot instead of typed actions.
//!
//! # Metrics
//!
//! Every value is a `M3EToolbarTokens` constant:
//!
//! | Token | Value | Constant |
//! |---|---|---|
//! | `containerSize` | 64 | [`TOOLBAR_HEIGHT`] |
//! | `floatingContentPadding` | 8 | [`TOOLBAR_FLOATING_PAD`] |
//! | `dockedHorizontalPadding` | 16 | [`TOOLBAR_DOCKED_PAD_X`] |
//! | `containerBetweenSpace` | 4 | [`TOOLBAR_GAP`] |
//! | `toolbarToFabGap` | 8 | [`TOOLBAR_TO_FAB_GAP`] |
//! | `screenOffset` | 16 | [`TOOLBAR_SCREEN_OFFSET`] |
//! | `fabBaseline` | 56 | [`TOOLBAR_FAB_BASELINE`] |
//! | `fabMedium` | 80 | [`TOOLBAR_FAB_MEDIUM`] |
//! | `elevationNone` / `elevationWithFabExpanded` | 0 / 1 | [`ToolbarElevation`] |
//!
//! The elevation pair is a **behavior change** against this module's previous
//! shape, which pinned an M3 level-2 shadow under every floating pill: upstream
//! rests at `elevationNone` and only lifts to `elevationWithFabExpanded` when a
//! FAB is attached. [`ToolbarView::elevation`] reaches the old look explicitly
//! ([`ToolbarElevation::Level2`]) — upstream's own `elevation` override.
//!
//! # The FAB morph (80 → 56), and what the fab seam can carry
//!
//! With a FAB attached, a **floating** bar lays out as
//! `RenderM3EToolbarHorizontalFabLayout` does: a box
//! `pill + toolbarToFabGap + fabBaseline` wide and `fabMedium` (80) tall, the
//! pill vertically centered in it and the FAB pinned to
//! [`ToolbarFabPosition`]'s edge. The FAB's square is
//! `lerp(80, 56, progress)` — upstream's `_resolvedFabSize` — so it is **80dp
//! while the bar is collapsed and 56dp once expanded**, and the pill reveals
//! from the FAB's side across the same ramp (`toolbarWidth = natural *
//! progress`, clipped to the revealed window, faded through
//! `Interval(0.5, 1, easeIn)` — [`ToolbarMorph::reveal_alpha`]). `progress` is driven by
//! [`ToolbarView::expanded`] on
//! [`MaterialSpring::EXPRESSIVE_SPATIAL_FAST`](crate::MaterialSpring::EXPRESSIVE_SPATIAL_FAST)
//! ([`TOOLBAR_EXPAND_SPRING`]), upstream's own `m3eToolbarExpandMotion()`.
//! [`ToolbarView::fab_expands_toolbar`] pins the pill open and the FAB at 56
//! (upstream's `fabExpandsToolbar: false`).
//!
//! **Who owns the toggle**: upstream's FAB press flips an internal `_expanded`
//! mirror. The fab slot here is an opaque caller view holding its own press
//! handler, so expansion is a *controlled prop* instead — the same
//! never-self-mutating contract [`mod@crate::navigation_rail`] documents for its
//! own expansion. The app flips `expanded` from the FAB's `on_press`.
//!
//! **The fab-seam degrade, stated plainly**: upstream sizes its FAB by wrapping
//! it in a `FittedBox` — a *uniform rescale* of the whole button, container,
//! corner radius and icon alike. There is no rescale seam here, so the morph is
//! applied as **tight constraints** on the fab slot instead: the pod's laid-out
//! rect is the morphing square, which is what [`mod@crate::fab`] fills its
//! container, shadow and state layer against, and what hit-testing and the
//! accessibility bounds read. What tight constraints cannot reach is
//! [`mod@crate::fab`]'s *icon centering*, which is computed against its own
//! [`crate::FabSize`] tier (56dp for the default `Medium`) rather than the box
//! it is handed — so at the collapsed 80dp end the icon reads 12dp off-center.
//! Closing that needs either an edit to `fab.rs` (out of this module's scope)
//! or painting a child at a size it was not laid out at (which would desync
//! hit-testing from the visual). An app that wants a pixel-exact collapsed end
//! supplies its own fab view sized off [`ToolbarMorph::size`].
//!
//! # Scroll-hide: the app owns the wiring
//!
//! A widget here has no window or scroll handle, so — exactly as
//! [`super::appbar`]'s collapse contract documents — the hide is a **controlled
//! prop**: the app installs [`on_scroll`](frust::ScrollView::on_scroll) on its
//! own scroll view, decides visibility, and feeds it back down.
//!
//! ```
//! use frust::{Component, ScrollInfo, View, column, scroll_view, text};
//! use frust_material::{ToolbarScrollHide, ToolbarView, floating_toolbar};
//!
//! struct App {
//!     /// The last offset the body reported — the hide's delta source.
//!     scrolled: f64,
//!     /// Upstream's `M3EToolbarVisibilityController`, as a plain value.
//!     hide: ToolbarScrollHide,
//! }
//!
//! struct Inbox;
//!
//! impl Component for Inbox {
//!     type State = App;
//!
//!     fn init(&self) -> App {
//!         App {
//!             scrolled: 0.0,
//!             hide: ToolbarScrollHide::for_bar(80.0),
//!         }
//!     }
//!
//!     fn build(&self, state: &mut App) -> impl View<App> {
//!         column().child(Self::bar(state)).child(Self::body())
//!     }
//! }
//!
//! impl Inbox {
//!     fn bar(state: &App) -> ToolbarView<App> {
//!         floating_toolbar().visible(!state.hide.is_hidden())
//!     }
//!
//!     fn body() -> impl View<App> {
//!         scroll_view(text("content")).on_scroll(|state: &mut App, info: ScrollInfo| {
//!             let delta = info.offset - state.scrolled;
//!             state.scrolled = info.offset;
//!             state.hide.scroll(delta);
//!         })
//!     }
//! }
//! ```
//!
//! [`ToolbarScrollHide`] is upstream's `M3EToolbarVisibilityController` as a
//! pure value (its ticker belongs to the widget, not the controller): the same
//! `offset -= delta` accumulation clamped into `[offset_limit, 0]`, the same
//! `collapsedFraction`, the same `-(extent + screenOffset)` measured limit, and
//! the same `|velocity| > 150` settle rule ([`TOOLBAR_SETTLE_VELOCITY`]).
//!
//! Two props consume it, mirroring upstream's own two paths:
//!
//! * [`ToolbarView::visible`] — the animated `show()`/`hide()` path, sprung on
//!   [`TOOLBAR_EXPAND_SPRING`] (upstream's controller shares the expand
//!   motion).
//! * [`ToolbarView::hidden_fraction`] — the 1:1 drag path (upstream's plain
//!   `offset` setter during a scroll update): pinned every frame, no spring.
//!
//! The bar slides along [`ToolbarExitDirection`] by
//! `fraction × exit_extent`, clipped to its own box — upstream's
//! `ClipRect(Transform.translate(...))` pair. The extent is
//! [`ToolbarView::exit_extent`] or, unset, the measured
//! `cross extent + screenOffset` (`M3EToolbarMeasureSize`'s own rule).
//!
//! **Both the hide offset and the FAB square are layout values**, so an
//! in-flight ramp calls [`frust::authoring::PaintCtx::request_layout`] rather
//! than merely asking for another frame — the layout-skip class
//! [`mod@crate::navigation_rail`] documents at length, including its
//! settling-frame refinement (the frame that both stops animating *and* lands
//! on the target still needs the relayout). `Theme.motion.reduce_motion` snaps
//! instead, and a snap that actually moved a value requests exactly one
//! relayout.
//!
//! # Overflow
//!
//! `M3EToolbarItemLayout.partitionInline` keeps every non-action slot inline
//! and overflows only surplus actions: an expand-trigger action always stays
//! inline and *reserves* one slot (`actionBudget = maxInline - reservedTrigger`),
//! and the first `actionBudget` remaining actions stay inline in order — the
//! rest overflow. [`partition_actions`] is that rule as a pure function;
//! [`ToolbarView::max_inline_actions`] is `maxInlineActions` (default
//! [`TOOLBAR_MAX_INLINE_ACTIONS`], 4).
//!
//! When anything overflows, a trailing trigger ([`ToolbarView::overflow_icon`],
//! upstream's `more_vert`) joins the inline row and reports
//! [`ToolbarView::on_overflow`] on press. The hidden tail presents as an
//! anchored menu through [`ToolbarView::overflow_menu`] — the same two-piece
//! shape [`mod@crate::button_group`]'s `Popup` overflow establishes, and for the
//! same reason: [`mod@crate::overlay::anchored`] is a plain widget with no
//! navigator to reach out through, so the app mounts the panel itself (the top
//! of its own [`frust::Stack`]), anchored to the [`OverlayAnchor`] handed to
//! [`ToolbarView::overflow_anchor`], and toggles `open` rather than unmounting
//! it (the kept-mounted pattern). Activating a row runs that action's own
//! `on_press`, exactly as its inline button would.
//!
//! One additive divergence: upstream's overflow entry is label-only, while a
//! [`ToolbarAction`] always carries an [`frust::IconSource`], so the same glyph
//! the inline button shows leads the overflowed row.
//!
//! # Semantics
//!
//! The whole bar is one [`Role::Toolbar`] container node (the accesskit role
//! purpose-built for this UI region), optionally labelled with
//! [`ToolbarView::semantic_label`], whose children are every slot pod in visual
//! order — leading, center, inline actions, the overflow trigger, trailing,
//! then the fab slot — via
//! [`frust::authoring::SemanticsCtx::push_container`].
//!
//! # Not ported
//!
//! * **The vertical axis** (`VerticalFloatingToolbar`,
//!   `M3EToolbarVerticalFabLayout`) — a horizontal bar is the whole of this
//!   module's v1; the vertical layout is a second, independent geometry.
//! * **The title/subtitle block** (`M3EToolbarTitleBlock`, `centerTitle`) — a
//!   toolbar has no mandatory title, and the `center` slot already carries any
//!   caller view a title would be.
//! * **The labeled-action width spring** (`M3EToolbarIconButton`'s in-button
//!   label morph, `pillActiveSpring`, `reservedLabeledSelectionExtent`) —
//!   [`mod@crate::icon_button`] has no label slot to morph, so an active action
//!   changes variant only. A [`ToolbarAction::label`] is still the overflow
//!   row's title, its other upstream use.
//! * **`safeArea` / `MediaQuery.viewPadding`** — this catalog's chrome widgets
//!   never self-inset (`docs/CODE_STANDARDS.md`'s
//!   self-sizing-chrome-consumes-its-own-inset rule leaves the choice to the
//!   composer); a caller wraps the bar in `frust::safe_area(...)`.
//! * **`clipBehavior` / `foregroundColor` / `scopedTheme` / `M3EToolbarTheme`
//!   as a theme extension** — slot content is opaque here, there is no ambient
//!   `Theme`-override push to scope a foreground onto (see the color table
//!   above), and every color/shape below resolves from [`frust::Theme`]
//!   directly (MaterialTokens-only), an explicit builder value winning over
//!   both.
//! * **`ExcludeFocus` while hidden** — a fully-hidden bar's slots are
//!   translated outside its own box, so pointer input cannot reach them; a
//!   focus-routed key event still can, which an app avoids by not focusing a
//!   hidden bar.
//!
//! # Clamp discipline
//!
//! `f64::clamp` panics if `min > max` or either bound is NaN — a hard abort
//! under `panic = "abort"`, not a catchable error (see [`crate::slider`]'s own
//! core for the same rule stated at length, which this one mirrors). Two
//! caller-fed values are the module's non-finite entry points, and both are
//! funneled to a safe value right where they enter (the module's private
//! `finite_or_zero`) rather than trusted downstream:
//!
//! * [`ToolbarScrollHide::new`] (and [`ToolbarScrollHide::for_bar`], which
//!   delegates to it) funnels its `exit_extent`/`cross_extent` construction
//!   parameter through `finite_or_zero` before it can reach `limit` — an
//!   unguarded non-finite `limit` would abort `set_offset`'s clamp on the
//!   very next [`ToolbarScrollHide::scroll`].
//! * `ToolbarWidget::layout`'s `extent` (the resolved
//!   [`ToolbarView::exit_extent`] prop) is the same `finite_or_zero` funnel:
//!   unguarded, a non-finite `exit_extent` would carry through `shift` into
//!   `pill_origin`, which `reveal_window` then clamps *against* (`x0` is the
//!   second clamp's `min` bound) — a caller-fed extent reaching a clamp
//!   *bound*, not just a clamped value.
//!
//! That leaves two clamp shapes in the module tree:
//!
//! - **Bounds are literal `0.0`/`1.0` (or another literal constant)**
//!   (`clamp01`, `layout`'s `progress.clamp(0.0, REVEAL_OVERSHOOT)`) —
//!   trivially ordered, never a hazard.
//! - **Bounds are already-finite by construction, funneled at the one point
//!   caller input enters** (`set_offset`'s `self.limit`, `reveal_window`'s
//!   `box_size.width`/`x0`) — both trace back to a `finite_or_zero` funnel
//!   rather than a re-check at every site.
//!
//! A new clamp against a computed bound must fit one of these two shapes, not
//! introduce a third.
//!
//! # Attribution
//!
//! See `plugins/material/NOTICE`'s "MIT License — Additional Copyright Holders
//! (Vendored Components)" section and its Module Attribution Header Convention.

use std::rc::Rc;
use std::time::Duration;

use frust::authoring::{
    AnyView, BoxConstraints, BuildCtx, ChangeFlags, ChildPod, EventCtx, EventResult, InputEvent,
    LayoutCtx, PaintCtx, PaintScene, Role, SemanticsCtx, View, ViewSeq, Widget,
};
use frust::{
    AnimationController, Curve, FrameTime, IconSource, ShapeScale, SpringDesc, Theme, Tween, icon,
};
use kurbo::{Point, Size, Vec2};
use peniko::Color;

use crate::icon_button::{IconButtonSize, IconButtonVariant, icon_button};
use crate::menu::{MenuNode, MenuSelection, menu, menu_entry};
use crate::overlay::{OverlayAlign, OverlayAnchor, overlay_anchor};

// ---- Tokens (`res/m3e_toolbar_tokens.dart`'s `M3EToolbarTokens`) -----------

/// Cross-axis container size of both toolbar variants, in logical px —
/// `M3EToolbarTokens.containerSize`. The same M3 bar-height family
/// [`super::appbar`]'s `HEIGHT` and [`super::navbar`]'s `HEIGHT` share.
pub const TOOLBAR_HEIGHT: f64 = 64.0;

/// Content inset of the **floating** pill on every edge, in logical px —
/// `M3EToolbarTokens.floatingContentPadding` (`M3EToolbarTheme.metricsFor`
/// resolves it as `EdgeInsets.all(8)`).
pub const TOOLBAR_FLOATING_PAD: f64 = 8.0;

/// Horizontal content inset of the **docked** bar, in logical px —
/// `M3EToolbarTokens.dockedHorizontalPadding` (its vertical inset is
/// [`TOOLBAR_FLOATING_PAD`], which this fixed-height port has no use for).
pub const TOOLBAR_DOCKED_PAD_X: f64 = 16.0;

/// Gap between adjacent slot elements, in logical px —
/// `M3EToolbarTokens.containerBetweenSpace`, which
/// `M3EToolbarTheme.metricsFor` publishes as its `gap`.
pub const TOOLBAR_GAP: f64 = 4.0;

/// Gap between the pill and an adjacent FAB, in logical px —
/// `M3EToolbarTokens.toolbarToFabGap`.
pub const TOOLBAR_TO_FAB_GAP: f64 = 8.0;

/// Extra distance a scroll-hidden bar travels past its own extent, in logical
/// px — `M3EToolbarTokens.screenOffset`, the margin
/// `M3EToolbarMeasureSize`'s measured `offsetLimit` adds so the bar clears the
/// screen edge entirely.
pub const TOOLBAR_SCREEN_OFFSET: f64 = 16.0;

/// The adjacent FAB's **expanded** square, in logical px —
/// `M3EToolbarTokens.fabBaseline`. Also the width the pill's box always
/// reserves for the FAB, whatever the morph is currently painting.
pub const TOOLBAR_FAB_BASELINE: f64 = 56.0;

/// The adjacent FAB's **collapsed** square, in logical px —
/// `M3EToolbarTokens.fabMedium`, which is also the whole box's height whenever
/// a FAB is attached.
pub const TOOLBAR_FAB_MEDIUM: f64 = 80.0;

/// Default number of actions kept inline before the overflow split —
/// `M3EToolbar.maxInlineActions`'s own default.
pub const TOOLBAR_MAX_INLINE_ACTIONS: usize = 4;

/// The expand/hide spring — upstream's `m3eToolbarExpandMotion()`, i.e.
/// `M3EMotion.expressiveSpatialFast` (stiffness 800, damping ratio 0.6).
/// Restated as a [`SpringDesc`] rather than read from a theme for the reason
/// [`mod@crate::navigation_rail`]'s own springs document: a `fling` needs one, and
/// paint/event code reads no theme for motion. A test pins it to
/// [`MaterialSpring::EXPRESSIVE_SPATIAL_FAST`](crate::MaterialSpring::EXPRESSIVE_SPATIAL_FAST).
pub const TOOLBAR_EXPAND_SPRING: SpringDesc = SpringDesc {
    mass: 1.0,
    stiffness: 800.0,
    damping_ratio: 0.6,
};

/// Fling velocity above which a scroll settle commits to the direction of
/// travel instead of the nearer end —
/// `M3EToolbarVisibilityController.settle`'s own `velocity.abs() > 150`.
pub const TOOLBAR_SETTLE_VELOCITY: f64 = 150.0;

/// Fraction of the expand ramp the revealing pill stays fully transparent for
/// — the start of upstream's `Interval(0.5, 1, curve: Curves.easeIn)`. See
/// [`ToolbarMorph::reveal_alpha`], which is that interval as a function
/// (`frust::Curve::interval`'s own `SegmentedCurve` return type is not
/// re-exported through the facade, so it cannot be named in a `const` here).
pub const TOOLBAR_REVEAL_FADE_START: f64 = 0.5;

/// Widest pill-reveal overshoot the layout honors — upstream's own
/// `_progress.clamp(0.0, 1.2)` on the revealed width, which lets the expand
/// spring's overshoot read as travel rather than being flattened.
const REVEAL_OVERSHOOT: f64 = 1.2;

/// Nominal period seeding a ramp [`AnimationController`]'s clock; both ramps
/// here are spring-driven via `fling`, so this backs construction only.
const RAMP_PERIOD: Duration = Duration::from_millis(300);

/// Launch velocity for a ramp leg. Sub-visible on purpose: a spring's shape
/// comes from its displacement, so only the sign matters, and it is always
/// positive here because a leg runs `0 → 1` in progress space — the same
/// sign-only convention [`mod@crate::navigation_rail`]'s own travel legs document.
const RAMP_LAUNCH_VELOCITY: f64 = 1e-3;

/// Progress difference under which a ramp counts as already heading somewhere
/// (a retarget guard, so an unchanged prop never relaunches a settled spring).
const RAMP_EPSILON: f64 = 1e-6;

// ---- Unthemed fallbacks ----------------------------------------------------

/// Unthemed fallback for `colors.surface_container` — matches
/// [`super::navbar`]'s own `CONTAINER` constant exactly (same M3 role).
const SURFACE_CONTAINER: Color = Color::from_rgb8(0xF3, 0xED, 0xF7);
/// Unthemed fallback for `colors.on_surface`.
const ON_SURFACE: Color = Color::from_rgb8(0x1D, 0x1B, 0x20);
/// Unthemed fallback for `colors.primary_container` (matches [`mod@crate::fab`]'s).
const PRIMARY_CONTAINER: Color = Color::from_rgb8(0xEA, 0xDD, 0xFF);
/// Unthemed fallback for `colors.on_primary_container`.
const ON_PRIMARY_CONTAINER: Color = Color::from_rgb8(0x21, 0x00, 0x5D);
/// Unthemed fallback for `colors.tertiary_container`.
const TERTIARY_CONTAINER: Color = Color::from_rgb8(0xFF, 0xD8, 0xE4);
/// Unthemed fallback for `colors.on_tertiary_container`.
const ON_TERTIARY_CONTAINER: Color = Color::from_rgb8(0x31, 0x11, 0x1D);

/// Unthemed-fallback shadow y-offset at M3 elevation level 1, matching
/// `crate::tokens::elevation().level1`'s own `dp / 2.0 + 1.0` at `dp = 1.0`.
const LEVEL1_SHADOW_Y_OFFSET: f64 = 1.5;
/// Unthemed-fallback shadow blur std-dev at M3 elevation level 1.
const LEVEL1_SHADOW_BLUR: f64 = 1.0;
/// Unthemed-fallback shadow y-offset at M3 elevation level 2 (`dp = 3.0`).
const LEVEL2_SHADOW_Y_OFFSET: f64 = 2.5;
/// Unthemed-fallback shadow blur std-dev at M3 elevation level 2.
const LEVEL2_SHADOW_BLUR: f64 = 3.0;
/// Unthemed-fallback shadow color (opaque black at `crate::tokens::elevation()`'s
/// `0.3` alpha — identical at every level).
const FALLBACK_SHADOW_COLOR: Color = Color::new([0.0, 0.0, 0.0, 0.3]);

/// The accessible name of the overflow trigger — upstream's own
/// `tooltip: 'More options'`, overridable per bar with
/// [`ToolbarView::overflow_label`].
const OVERFLOW_LABEL: &str = "More options";

// ---- Enums -----------------------------------------------------------------

/// The M3X toolbar container variant — `M3EToolbarPlacement`. See the
/// [module docs](self).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ToolbarVariant {
    /// A self-sized, fully-rounded pill floating over content.
    #[default]
    Floating,
    /// A full-width, flat bar docked to an edge (bottom typical).
    Docked,
}

/// Standard vs vibrant container color mapping — `M3EToolbarColorStyle`. See
/// the [module docs](self)' color table.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ToolbarColorStyle {
    /// `surface_container` container, `on_surface` content — the default.
    #[default]
    Standard,
    /// `primary_container` container, `on_primary_container` content.
    Vibrant,
}

/// The bar's resting elevation — upstream's `elevationNone` /
/// `elevationWithFabExpanded` pair, plus the level this module used to pin
/// unconditionally.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ToolbarElevation {
    /// No shadow at all — `M3EToolbarTokens.elevationNone`, the resting
    /// default of a bar with no FAB.
    #[default]
    None,
    /// M3 elevation level 1 — `M3EToolbarTokens.elevationWithFabExpanded`, the
    /// resting default once a FAB is attached.
    Level1,
    /// M3 elevation level 2 — no upstream token; the level this module pinned
    /// under every floating pill before the reference's own pair was ported.
    Level2,
}

/// Which edge of the box an adjacent FAB occupies — `M3EToolbarFabPosition`'s
/// horizontal pair (its `top`/`bottom` belong to the unported vertical axis).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ToolbarFabPosition {
    /// The FAB leads the pill.
    Start,
    /// The FAB trails the pill — the default.
    #[default]
    End,
}

/// Direction a bar slides while scroll-hidden — `M3EToolbarExitDirection`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ToolbarExitDirection {
    /// Exits upwards.
    Top,
    /// Exits downwards — the default (a bottom-docked bar).
    #[default]
    Bottom,
    /// Exits toward the leading edge.
    Start,
    /// Exits toward the trailing edge.
    End,
}

impl ToolbarExitDirection {
    /// Whether this direction travels on the vertical axis, which is what
    /// picks the measured extent's own axis (`M3EToolbarMeasureSize`'s
    /// `vertical ? size.height : size.width`).
    pub fn is_vertical(self) -> bool {
        matches!(self, Self::Top | Self::Bottom)
    }

    /// The translation a `distance` of travel produces — upstream's
    /// `_exitOffset`, restated for a non-negative distance (its own `offset`
    /// is `≤ 0`, so every arm there carries a sign flip this one does not).
    pub fn offset(self, distance: f64) -> Vec2 {
        match self {
            Self::Top => Vec2::new(0.0, -distance),
            Self::Bottom => Vec2::new(0.0, distance),
            Self::Start => Vec2::new(-distance, 0.0),
            Self::End => Vec2::new(distance, 0.0),
        }
    }
}

/// The icon-button density a bar renders its typed actions at — the legacy
/// `M3EToolbarSize`, which maps to icon-button density and never to container
/// height (`M3EToolbarTheme.iconButtonSize`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ToolbarSize {
    /// `IconButtonSize::Xs`.
    Small,
    /// `IconButtonSize::Sm` — the default.
    #[default]
    Medium,
    /// `IconButtonSize::Sm`, same as [`Self::Medium`] upstream.
    Large,
}

impl ToolbarSize {
    /// The icon-button tier this density resolves — `iconButtonSize`'s own
    /// `small → xs`, `medium | large → sm`.
    pub fn icon_button_size(self) -> IconButtonSize {
        match self {
            Self::Small => IconButtonSize::Xs,
            Self::Medium | Self::Large => IconButtonSize::Sm,
        }
    }
}

// ---- Colors ----------------------------------------------------------------

/// The four resolved roles of one [`ToolbarColorStyle`] —
/// `M3EToolbarColors`, as produced by `M3EToolbarTheme.colors`.
///
/// ```
/// use frust_material::{ToolbarColorStyle, ToolbarColors, baseline};
///
/// let theme = baseline();
/// let vibrant = ToolbarColors::resolve(Some(&theme), ToolbarColorStyle::Vibrant);
/// assert_eq!(vibrant.container, theme.scheme().primary_container);
/// assert_eq!(vibrant.fab_container, theme.scheme().tertiary_container);
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ToolbarColors {
    /// The bar container's fill.
    pub container: Color,
    /// The ink every content slot the bar owns is tinted with.
    pub content: Color,
    /// The container an adjacent FAB should take — advisory here (the fab slot
    /// is an opaque caller view; see the [module docs](self)).
    pub fab_container: Color,
    /// The ink an adjacent FAB should take — advisory, as above.
    pub fab_content: Color,
}

impl ToolbarColors {
    /// Resolve `style` against `theme`, falling back to the M3 baseline
    /// literals when no theme is threaded. See the [module docs](self)' table.
    pub fn resolve(theme: Option<&Theme>, style: ToolbarColorStyle) -> Self {
        match (theme, style) {
            (Some(theme), ToolbarColorStyle::Standard) => {
                let s = theme.scheme();
                Self {
                    container: s.surface_container,
                    content: s.on_surface,
                    fab_container: s.primary_container,
                    fab_content: s.on_primary_container,
                }
            }
            (Some(theme), ToolbarColorStyle::Vibrant) => {
                let s = theme.scheme();
                Self {
                    container: s.primary_container,
                    content: s.on_primary_container,
                    fab_container: s.tertiary_container,
                    fab_content: s.on_tertiary_container,
                }
            }
            (None, ToolbarColorStyle::Standard) => Self {
                container: SURFACE_CONTAINER,
                content: ON_SURFACE,
                fab_container: PRIMARY_CONTAINER,
                fab_content: ON_PRIMARY_CONTAINER,
            },
            (None, ToolbarColorStyle::Vibrant) => Self {
                container: PRIMARY_CONTAINER,
                content: ON_PRIMARY_CONTAINER,
                fab_container: TERTIARY_CONTAINER,
                fab_content: ON_TERTIARY_CONTAINER,
            },
        }
    }
}

/// `value` clamped into `[0, 1]`, mapping NaN to the resting `0.0` rather than
/// propagating it into a layout (the same guard [`super::appbar`] applies).
fn clamp01(value: f64) -> f64 {
    if value.is_nan() {
        0.0
    } else {
        value.clamp(0.0, 1.0)
    }
}

/// Linear interpolation from `a` to `b` at `t` (deliberately unclamped — the
/// FAB square reads the expand spring's overshoot as real travel, exactly as
/// upstream's own `_resolvedFabSize` does).
fn lerp(a: f64, b: f64, t: f64) -> f64 {
    a + (b - a) * t
}

/// `constraint` if finite, `0.0` otherwise — the degenerate unbounded-width
/// case every full-width bar in this catalog resolves the same way.
fn finite_or_zero(constraint: f64) -> f64 {
    if constraint.is_finite() {
        constraint
    } else {
        0.0
    }
}

/// Return `color` with its alpha channel replaced by `alpha`.
fn with_alpha(color: Color, alpha: f32) -> Color {
    let c = color.components;
    Color::new([c[0], c[1], c[2], alpha])
}

/// The resolved corner radius for `variant`, against a bar of `width`×`height`.
/// Themed: `shape.full` (floating, resolved to a true pill) / `shape.none`
/// (docked). Unthemed: the equivalent literal tokens
/// (`crate::tokens::shape_scale`'s own values), resolved the same way.
fn resolve_radius(theme: Option<&Theme>, variant: ToolbarVariant, width: f64, height: f64) -> f64 {
    let token = match (theme, variant) {
        (Some(theme), ToolbarVariant::Floating) => theme.shape.full,
        (Some(theme), ToolbarVariant::Docked) => theme.shape.none,
        (None, ToolbarVariant::Floating) => f64::INFINITY,
        (None, ToolbarVariant::Docked) => 0.0,
    };
    ShapeScale::resolve(token, width, height)
}

/// The resolved `(blur_std_dev, y_offset, color)` shadow parameters for
/// `level`, or `None` at [`ToolbarElevation::None`] (upstream's
/// `elevationNone`, which paints nothing at all). Themed: the matching
/// `theme.elevation` `ShadowSpec`, colored by `colors.shadow` at its
/// `color_alpha`. Unthemed: the matching `LEVEL{1,2}_SHADOW_*` constants.
fn resolve_shadow(theme: Option<&Theme>, level: ToolbarElevation) -> Option<(f64, f64, Color)> {
    match (theme, level) {
        (_, ToolbarElevation::None) => None,
        (Some(theme), level) => {
            let spec = match level {
                ToolbarElevation::Level1 => theme.elevation.level1,
                _ => theme.elevation.level2,
            };
            let shadow = spec.shadow(theme.brightness);
            let color = with_alpha(theme.scheme().shadow, shadow.color_alpha);
            Some((shadow.blur_std_dev, shadow.y_offset, color))
        }
        (None, ToolbarElevation::Level1) => Some((
            LEVEL1_SHADOW_BLUR,
            LEVEL1_SHADOW_Y_OFFSET,
            FALLBACK_SHADOW_COLOR,
        )),
        (None, ToolbarElevation::Level2) => Some((
            LEVEL2_SHADOW_BLUR,
            LEVEL2_SHADOW_Y_OFFSET,
            FALLBACK_SHADOW_COLOR,
        )),
    }
}

// ---- The expand-ramp geometry (pure) ---------------------------------------

/// The expand ramp's pure geometry: what the bar's expand progress does to the
/// adjacent FAB's square and to the revealing pill's opacity — an uninhabited
/// namespace type (no instance is ever constructed), the same shape
/// [`crate::MaterialSpring`]'s own token namespace uses.
///
/// ```
/// use frust_material::ToolbarMorph;
///
/// // Collapsed is the 80dp end; expanded the 56dp one.
/// assert_eq!(ToolbarMorph::size(0.0), 80.0);
/// assert_eq!(ToolbarMorph::size(1.0), 56.0);
/// assert_eq!(ToolbarMorph::size(0.5), 68.0);
///
/// // The spring's overshoot past 1 is real travel, never clamped away.
/// assert!(ToolbarMorph::size(1.1) < 56.0);
/// // …but never negative, whatever the input.
/// assert_eq!(ToolbarMorph::size(f64::NAN), 80.0);
///
/// // The pill stays invisible through the ramp's first half.
/// assert_eq!(ToolbarMorph::reveal_alpha(0.5), 0.0);
/// assert_eq!(ToolbarMorph::reveal_alpha(1.0), 1.0);
/// ```
#[derive(Debug)]
pub enum ToolbarMorph {}

impl ToolbarMorph {
    /// The FAB's square at expand `progress` — `_resolvedFabSize`'s own
    /// `fabMedium + (fabBaseline - fabMedium) * progress`, floored at zero and
    /// NaN-mapped to the collapsed (resting) end.
    pub fn size(progress: f64) -> f64 {
        if progress.is_nan() {
            return TOOLBAR_FAB_MEDIUM;
        }
        lerp(TOOLBAR_FAB_MEDIUM, TOOLBAR_FAB_BASELINE, progress).max(0.0)
    }

    /// The revealing pill's opacity at expand `progress` — upstream's
    /// `Interval(0.5, 1, curve: Curves.easeIn)`, whose control points
    /// ([`Curve::EaseIn`], `cubic-bezier(0.42, 0, 1, 1)`) are Flutter's
    /// `Curves.easeIn` exactly. See [`TOOLBAR_REVEAL_FADE_START`].
    pub fn reveal_alpha(progress: f64) -> f64 {
        Curve::EaseIn
            .interval(TOOLBAR_REVEAL_FADE_START, 1.0)
            .transform(clamp01(progress))
    }
}

// ---- Scroll-hide (pure) ----------------------------------------------------

/// Upstream's `M3EToolbarVisibilityController` as a pure value: the scroll
/// accumulation, clamp, and settle rule an app runs from its own
/// [`on_scroll`](frust::ScrollView::on_scroll), with the ticker left to the
/// widget (see the [module docs](self)' scroll-hide section).
///
/// `offset` is always `≤ 0` and clamped into `[offset_limit, 0]`.
///
/// ```
/// use frust_material::{TOOLBAR_HEIGHT, ToolbarScrollHide, TOOLBAR_SCREEN_OFFSET};
///
/// // The measured limit is `-(extent + screenOffset)`.
/// let mut hide = ToolbarScrollHide::for_bar(TOOLBAR_HEIGHT);
/// assert_eq!(hide.offset_limit(), -(TOOLBAR_HEIGHT + TOOLBAR_SCREEN_OFFSET));
///
/// // Scrolling down hides; scrolling back up returns.
/// hide.scroll(40.0);
/// assert_eq!(hide.offset(), -40.0);
/// hide.scroll(-15.0);
/// assert_eq!(hide.offset(), -25.0);
///
/// // A fling past the velocity threshold commits to its direction.
/// assert!(!hide.settle(400.0));
/// assert!(hide.is_hidden());
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ToolbarScrollHide {
    limit: f64,
    offset: f64,
}

impl ToolbarScrollHide {
    /// A controller with a fixed exit distance — upstream's `exitExtent`,
    /// which pins `offsetLimit` to `-|extent|`. A non-finite `exit_extent`
    /// (NaN or ±∞) degrades to a resting `0.0` exit distance rather than
    /// carrying a non-finite `limit` into [`Self::set_offset`]'s clamp — see
    /// the [module docs](self)' clamp discipline.
    pub fn new(exit_extent: f64) -> Self {
        Self {
            limit: -finite_or_zero(exit_extent).abs(),
            offset: 0.0,
        }
    }

    /// A controller whose exit distance is measured off the bar — upstream's
    /// `M3EToolbarMeasureSize` rule, `-(cross_extent + screenOffset)`. A
    /// non-finite `cross_extent` degrades the same way [`Self::new`]'s own
    /// guard does — this delegates to it, so the guard applies whether the
    /// non-finite value reaches `new` directly or through here.
    pub fn for_bar(cross_extent: f64) -> Self {
        Self::new(cross_extent.abs() + TOOLBAR_SCREEN_OFFSET)
    }

    /// The current translation, in `[offset_limit, 0]`.
    pub fn offset(&self) -> f64 {
        self.offset
    }

    /// The most negative translation this controller allows.
    pub fn offset_limit(&self) -> f64 {
        self.limit
    }

    /// Consume a scroll `delta` (positive scrolling down) — upstream's
    /// `_updateOffset`'s `offset -= delta`, clamped. A non-finite delta is
    /// ignored rather than propagated into the offset.
    pub fn scroll(&mut self, delta: f64) {
        if !delta.is_finite() {
            return;
        }
        self.set_offset(self.offset - delta);
    }

    /// How far hidden the bar is, `0.0` fully visible through `1.0` fully
    /// hidden — upstream's `collapsedFraction`.
    pub fn collapsed_fraction(&self) -> f64 {
        if self.limit == 0.0 {
            return 0.0;
        }
        clamp01(self.offset / self.limit)
    }

    /// Whether the bar is fully off-screen — upstream's `isHidden`.
    pub fn is_hidden(&self) -> bool {
        self.collapsed_fraction() >= 1.0
    }

    /// Snap fully visible — upstream's `show()` target.
    pub fn show(&mut self) {
        self.offset = 0.0;
    }

    /// Snap fully hidden — upstream's `hide()` target.
    pub fn hide(&mut self) {
        self.offset = self.limit;
    }

    /// Flip about the halfway point — upstream's `toggle()`.
    pub fn toggle(&mut self) {
        if self.collapsed_fraction() < 0.5 {
            self.hide();
        } else {
            self.show();
        }
    }

    /// Settle after a fling at `velocity` (positive scrolling down), reporting
    /// whether the bar ends up **visible** — upstream's `settle`: past
    /// [`TOOLBAR_SETTLE_VELOCITY`] the direction of travel wins, otherwise the
    /// nearer end does. Already-settled at either end, nothing moves.
    pub fn settle(&mut self, velocity: f64) -> bool {
        if self.offset == 0.0 || self.offset == self.limit {
            return self.offset == 0.0;
        }
        let hide = if velocity.abs() > TOOLBAR_SETTLE_VELOCITY {
            velocity > 0.0
        } else {
            self.collapsed_fraction() >= 0.5
        };
        if hide {
            self.hide();
        } else {
            self.show();
        }
        !hide
    }

    /// Clamp and store `value` — upstream's `offset` setter, whose clamp
    /// bounds swap when the limit is (degenerately) positive.
    fn set_offset(&mut self, value: f64) {
        self.offset = if self.limit <= 0.0 {
            value.clamp(self.limit, 0.0)
        } else {
            value.clamp(0.0, self.limit)
        };
    }
}

// ---- The overflow capacity rule (pure) -------------------------------------

/// The inline/overflow split of one action list —
/// `M3EToolbarItemLayout.partitionInline`'s own result shape, as flat index
/// lists.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ToolbarPartition {
    /// Indices kept in the bar, in order.
    pub inline: Vec<usize>,
    /// Indices moved to the overflow menu, in order.
    pub overflow: Vec<usize>,
}

/// Split `count` actions into the inline and overflow halves —
/// `M3EToolbarItemLayout.partitionInline`.
///
/// An expand-trigger action (`trigger`) always stays inline **and** reserves
/// one of `max_inline`'s slots (upstream's `reservedTrigger`), so the budget
/// available to ordinary actions is `max_inline - 1` when one is present.
///
/// ```
/// use frust_material::{TOOLBAR_MAX_INLINE_ACTIONS, partition_actions};
///
/// // Six actions at the default budget: four inline, two overflowed.
/// let split = partition_actions(6, TOOLBAR_MAX_INLINE_ACTIONS, None);
/// assert_eq!(split.inline, vec![0, 1, 2, 3]);
/// assert_eq!(split.overflow, vec![4, 5]);
///
/// // A trigger stays inline and costs one budget slot.
/// let split = partition_actions(6, TOOLBAR_MAX_INLINE_ACTIONS, Some(5));
/// assert_eq!(split.inline, vec![0, 1, 2, 5]);
/// assert_eq!(split.overflow, vec![3, 4]);
/// ```
pub fn partition_actions(
    count: usize,
    max_inline: usize,
    trigger: Option<usize>,
) -> ToolbarPartition {
    let trigger = trigger.filter(|index| *index < count);
    let budget = max_inline.saturating_sub(usize::from(trigger.is_some()));
    let mut partition = ToolbarPartition::default();
    let mut used = 0usize;
    for index in 0..count {
        if Some(index) == trigger {
            partition.inline.push(index);
        } else if used < budget {
            partition.inline.push(index);
            used += 1;
        } else {
            partition.overflow.push(index);
        }
    }
    partition
}

// ---- Typed actions ---------------------------------------------------------

/// A view-held, typed press callback (erased per-action on build).
type OnPress<State> = Rc<dyn Fn(&mut State)>;

/// One typed action in [`ToolbarView::actions`] — `M3EToolbarAction`, the
/// upstream item kind that can move to the overflow menu (its
/// `M3EToolbarWidget` sibling, which always stays inline, is this port's
/// opaque `center` slot).
pub struct ToolbarAction<State: 'static> {
    icon: IconSource,
    label: Option<String>,
    tooltip: Option<String>,
    semantic_label: Option<String>,
    enabled: bool,
    destructive: bool,
    active: bool,
    expand_trigger: bool,
    on_press: OnPress<State>,
}

/// Create a toolbar action painting `icon`, running `on_press` on release
/// inside its bounds.
pub fn toolbar_action<State: 'static, F: Fn(&mut State) + 'static>(
    icon: IconSource,
    on_press: F,
) -> ToolbarAction<State> {
    ToolbarAction {
        icon,
        label: None,
        tooltip: None,
        semantic_label: None,
        enabled: true,
        destructive: false,
        active: false,
        expand_trigger: false,
        on_press: Rc::new(on_press),
    }
}

impl<State: 'static> ToolbarAction<State> {
    /// The action's label — upstream's `label`. Used as the overflow row's
    /// title; the in-button label morph it also drives upstream is unported
    /// (see the [module docs](self)' *Not ported*).
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// The action's tooltip text — upstream's `tooltip`. This catalog's icon
    /// button carries no tooltip slot, so this contributes an accessible name
    /// (behind [`Self::semantic_label`]) and an overflow-row title (behind
    /// [`Self::label`]) only.
    pub fn tooltip(mut self, tooltip: impl Into<String>) -> Self {
        self.tooltip = Some(tooltip.into());
        self
    }

    /// The action's accessible name — upstream's `semanticLabel`.
    pub fn semantic_label(mut self, label: impl Into<String>) -> Self {
        self.semantic_label = Some(label.into());
        self
    }

    /// Whether the action accepts presses (`enabled`, default `true`).
    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// Whether the overflow row reads destructive (`isDestructive`).
    pub fn destructive(mut self, destructive: bool) -> Self {
        self.destructive = destructive;
        self
    }

    /// Whether the inline button reads selected (`active`) — filled icon-button
    /// styling, per `M3EToolbarIconButton._resolvedVariant`.
    pub fn active(mut self, active: bool) -> Self {
        self.active = active;
        self
    }

    /// Mark this action as the bar's expand/collapse trigger
    /// (`isExpandTrigger`): it always stays inline, reserves one inline slot,
    /// and takes the same filled styling an active action does. At most one
    /// action should set it — the first one wins.
    ///
    /// The toggle itself stays app-owned (see the [module docs](self)'
    /// controlled-prop note): a trigger's own `on_press` is where an app flips
    /// [`ToolbarView::expanded`].
    pub fn expand_trigger(mut self, trigger: bool) -> Self {
        self.expand_trigger = trigger;
        self
    }

    /// The title this action's overflow row takes — upstream's
    /// `label ?? tooltip ?? semanticLabel ?? 'Action {n}'` fallback chain.
    pub fn menu_label(&self, index: usize) -> String {
        self.label
            .as_deref()
            .or(self.tooltip.as_deref())
            .or(self.semantic_label.as_deref())
            .map(str::to_owned)
            .unwrap_or_else(|| format!("Action {}", index + 1))
    }

    /// The inline icon-button view for this action.
    fn inline_view(&self, size: ToolbarSize) -> AnyView<State> {
        let on_press = self.on_press.clone();
        let mut button = icon_button::<State, _>(
            frust::authoring::any::<State, _>(icon(self.icon)),
            move |state: &mut State| on_press(state),
        )
        .size(size.icon_button_size())
        .enabled(self.enabled)
        .variant(if self.active || self.expand_trigger {
            IconButtonVariant::Filled
        } else {
            IconButtonVariant::Standard
        });
        if let Some(label) = self.semantic_label.as_deref().or(self.tooltip.as_deref()) {
            button = button.semantic_label(label);
        }
        frust::authoring::any::<State, _>(button)
    }
}

// ---- Motion driver ---------------------------------------------------------

/// One spring-driven `0 → 1` ramp (the expand progress, the hide fraction).
struct RampTrack {
    /// The progress `layout` reads this frame — **unclamped**, so the spring's
    /// overshoot is real travel; every consumer clamps at its own use site
    /// exactly as upstream does.
    value: f64,
    /// The progress the current leg is heading for.
    target: f64,
    /// Maps the controller's `0 → 1` leg onto `from → target`.
    tween: Tween<f64>,
    driver: AnimationController,
}

impl RampTrack {
    /// A ramp resting at `value` with no leg in flight — a bar built already
    /// expanded shows its full pill on the first frame rather than animating
    /// into it.
    fn resting(value: f64) -> Self {
        Self {
            value,
            target: value,
            tween: Tween::new(value, value),
            driver: AnimationController::new(RAMP_PERIOD),
        }
    }

    /// Launch a leg toward `target` on [`TOOLBAR_EXPAND_SPRING`], starting
    /// from wherever the ramp currently reads (so an interrupted leg reverses
    /// smoothly). A no-op if already heading there.
    fn set_target(&mut self, target: f64) {
        if (self.target - target).abs() < RAMP_EPSILON {
            return;
        }
        self.target = target;
        self.tween = Tween::new(self.value, target);
        self.driver = AnimationController::new(RAMP_PERIOD);
        self.driver
            .fling(RAMP_LAUNCH_VELOCITY, TOOLBAR_EXPAND_SPRING);
    }

    /// Jump to `to`, cancelling any leg (a prop-pinned fraction, or a fresh
    /// build).
    fn jump(&mut self, to: f64) {
        self.value = to;
        self.target = to;
        self.tween = Tween::new(to, to);
        self.driver.stop();
    }

    /// Jump to the target, cancelling any leg (the reduce-motion path).
    /// Returns whether that actually moved the value.
    fn snap(&mut self) -> bool {
        let moved = self.value != self.target;
        self.jump(self.target);
        moved
    }

    /// Advance one frame, returning whether the leg is still in flight.
    fn advance(&mut self, now: FrameTime) -> bool {
        let animating = self.driver.advance(now);
        self.value = self.tween.lerp(self.driver.value());
        animating
    }
}

// ---- The view --------------------------------------------------------------

/// A view-held, typed overflow-trigger callback.
type OnOverflow<State> = Rc<dyn Fn(&mut State)>;

/// A declarative M3X toolbar. See the [module docs](self).
pub struct ToolbarView<State: 'static> {
    variant: ToolbarVariant,
    color_style: ToolbarColorStyle,
    elevation: Option<ToolbarElevation>,
    size: ToolbarSize,
    leading: Vec<AnyView<State>>,
    center: Vec<AnyView<State>>,
    trailing: Vec<AnyView<State>>,
    fab: Option<AnyView<State>>,
    actions: Vec<ToolbarAction<State>>,
    max_inline_actions: usize,
    overflow_icon: IconSource,
    overflow_label: String,
    on_overflow: Option<OnOverflow<State>>,
    overflow_anchor: Option<OverlayAnchor>,
    expanded: bool,
    fab_expands_toolbar: bool,
    fab_position: ToolbarFabPosition,
    visible: bool,
    hidden_fraction: Option<f64>,
    exit_direction: ToolbarExitDirection,
    exit_extent: Option<f64>,
    semantic_label: Option<String>,
}

/// Build a [`ToolbarView`] of `variant` with every prop at its upstream
/// default.
fn toolbar<State: 'static>(variant: ToolbarVariant) -> ToolbarView<State> {
    ToolbarView {
        variant,
        color_style: ToolbarColorStyle::default(),
        elevation: None,
        size: ToolbarSize::default(),
        leading: Vec::new(),
        center: Vec::new(),
        trailing: Vec::new(),
        fab: None,
        actions: Vec::new(),
        max_inline_actions: TOOLBAR_MAX_INLINE_ACTIONS,
        overflow_icon: crate::icons::MORE_VERT,
        overflow_label: OVERFLOW_LABEL.to_owned(),
        on_overflow: None,
        overflow_anchor: None,
        expanded: true,
        fab_expands_toolbar: true,
        fab_position: ToolbarFabPosition::default(),
        visible: true,
        hidden_fraction: None,
        exit_direction: ToolbarExitDirection::default(),
        exit_extent: None,
        semantic_label: None,
    }
}

/// Create a floating toolbar (self-sized pill, elevated above content) with
/// no slots attached — attach them with [`ToolbarView::leading`]/
/// [`ToolbarView::center`]/[`ToolbarView::trailing`]/[`ToolbarView::actions`]/
/// [`ToolbarView::fab`].
pub fn floating_toolbar<State: 'static>() -> ToolbarView<State> {
    toolbar(ToolbarVariant::Floating)
}

/// PascalCase alias for [`floating_toolbar`], matching the container view-fn
/// vocabulary.
#[allow(non_snake_case)]
pub fn FloatingToolbar<State: 'static>() -> ToolbarView<State> {
    floating_toolbar()
}

/// Create a docked toolbar (full-width, flat bar) with no slots attached.
pub fn docked_toolbar<State: 'static>() -> ToolbarView<State> {
    toolbar(ToolbarVariant::Docked)
}

/// PascalCase alias for [`docked_toolbar`], matching the container view-fn
/// vocabulary.
#[allow(non_snake_case)]
pub fn DockedToolbar<State: 'static>() -> ToolbarView<State> {
    docked_toolbar()
}

impl<State: 'static> ToolbarView<State> {
    /// Attach leading slot children, in reading order (hugs the leading edge).
    /// Tint/styling is each supplied view's own responsibility.
    pub fn leading<M>(mut self, leading: impl ViewSeq<State, M>) -> Self {
        self.leading.clear();
        leading.extend_views(&mut self.leading);
        self
    }

    /// Attach center slot children, in reading order — upstream's inline
    /// `M3EToolbarWidget` items, which never overflow. They lead the typed
    /// [`Self::actions`] within the same center group.
    pub fn center<M>(mut self, center: impl ViewSeq<State, M>) -> Self {
        self.center.clear();
        center.extend_views(&mut self.center);
        self
    }

    /// Attach trailing slot children, in reading order (hugs the trailing
    /// edge, or the fab slot if also attached).
    pub fn trailing<M>(mut self, trailing: impl ViewSeq<State, M>) -> Self {
        self.trailing.clear();
        trailing.extend_views(&mut self.trailing);
        self
    }

    /// Attach the optional adjacent FAB — upstream's `floatingActionButton`.
    /// On a **floating** bar this takes the adjacent morph layout described in
    /// the [module docs](self); on a **docked** one it simply trails the row,
    /// as it always has (upstream has no docked FAB at all).
    pub fn fab(mut self, fab: impl View<State>) -> Self {
        self.fab = Some(AnyView::new(fab));
        self
    }

    /// Attach the typed action list — upstream's `actions`, the only slot
    /// subject to the overflow split.
    pub fn actions(mut self, actions: Vec<ToolbarAction<State>>) -> Self {
        self.actions = actions;
        self
    }

    /// How many actions stay inline before overflowing (`maxInlineActions`,
    /// default [`TOOLBAR_MAX_INLINE_ACTIONS`]).
    pub fn max_inline_actions(mut self, max: usize) -> Self {
        self.max_inline_actions = max;
        self
    }

    /// Replace the overflow trigger's glyph (`overflowIcon`, default
    /// `more_vert`).
    pub fn overflow_icon(mut self, icon: IconSource) -> Self {
        self.overflow_icon = icon;
        self
    }

    /// Replace the overflow trigger's accessible name (upstream's own
    /// `'More options'`).
    pub fn overflow_label(mut self, label: impl Into<String>) -> Self {
        self.overflow_label = label.into();
        self
    }

    /// Fired when the overflow trigger is pressed — where an app flips the
    /// open flag it hands [`Self::overflow_menu`].
    pub fn on_overflow<F: Fn(&mut State) + 'static>(mut self, on_overflow: F) -> Self {
        self.on_overflow = Some(Rc::new(on_overflow));
        self
    }

    /// Capture the overflow trigger's window rect into `anchor` on every
    /// paint, so [`Self::overflow_menu`] has a rect to place against — the
    /// same [`OverlayAnchor`] handoff [`mod@crate::button_group`]'s own popup
    /// overflow takes.
    pub fn overflow_anchor(mut self, anchor: &OverlayAnchor) -> Self {
        self.overflow_anchor = Some(anchor.clone());
        self
    }

    /// Pick the standard or vibrant color resolution (`colorStyle`).
    pub fn color_style(mut self, style: ToolbarColorStyle) -> Self {
        self.color_style = style;
        self
    }

    /// Override the resting elevation (upstream's `elevation`). Unset, a bar
    /// rests at [`ToolbarElevation::None`], or [`ToolbarElevation::Level1`]
    /// once a FAB is attached.
    pub fn elevation(mut self, elevation: ToolbarElevation) -> Self {
        self.elevation = Some(elevation);
        self
    }

    /// The icon-button density typed actions render at (`size`).
    pub fn size(mut self, size: ToolbarSize) -> Self {
        self.size = size;
        self
    }

    /// Whether the pill is expanded (`expanded`, default `true`) — the
    /// controlled prop driving the FAB morph and pill reveal. See the
    /// [module docs](self) for who owns the toggle.
    pub fn expanded(mut self, expanded: bool) -> Self {
        self.expanded = expanded;
        self
    }

    /// Whether an adjacent FAB morphs with the pill (`fabExpandsToolbar`,
    /// default `true`). Set `false` to pin the pill open and the FAB at
    /// [`TOOLBAR_FAB_BASELINE`].
    pub fn fab_expands_toolbar(mut self, expands: bool) -> Self {
        self.fab_expands_toolbar = expands;
        self
    }

    /// Which edge an adjacent FAB occupies (`fabPosition`, default
    /// [`ToolbarFabPosition::End`]).
    pub fn fab_position(mut self, position: ToolbarFabPosition) -> Self {
        self.fab_position = position;
        self
    }

    /// Whether the bar is shown (`show()`/`hide()`, default `true`) — sprung
    /// on [`TOOLBAR_EXPAND_SPRING`]. Ignored while [`Self::hidden_fraction`]
    /// pins a value.
    pub fn visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }

    /// Pin the hidden fraction directly, `0.0` visible through `1.0` hidden —
    /// upstream's plain `offset` setter during a scroll drag. `Some(f)`
    /// overrides [`Self::visible`] and applies with no spring; `None` (the
    /// default) hands the ramp back to `visible`, resuming from wherever the
    /// pin left it.
    pub fn hidden_fraction(mut self, fraction: Option<f64>) -> Self {
        self.hidden_fraction = fraction;
        self
    }

    /// The direction the bar slides while hidden (`exitDirection`, default
    /// [`ToolbarExitDirection::Bottom`]).
    pub fn exit_direction(mut self, direction: ToolbarExitDirection) -> Self {
        self.exit_direction = direction;
        self
    }

    /// Override the hide travel distance (`exitExtent`). Unset, the bar
    /// measures its own cross extent and adds [`TOOLBAR_SCREEN_OFFSET`].
    pub fn exit_extent(mut self, extent: Option<f64>) -> Self {
        self.exit_extent = extent;
        self
    }

    /// The accessible name of the bar as a whole (`semanticLabel`).
    pub fn semantic_label(mut self, label: impl Into<String>) -> Self {
        self.semantic_label = Some(label.into());
        self
    }

    /// The index of the expand-trigger action, if any (the first one wins —
    /// upstream asserts at most one).
    fn trigger_index(&self) -> Option<usize> {
        self.actions.iter().position(|a| a.expand_trigger)
    }

    /// This bar's inline/overflow split (see [`partition_actions`]).
    fn partition(&self) -> ToolbarPartition {
        partition_actions(
            self.actions.len(),
            self.max_inline_actions,
            self.trigger_index(),
        )
    }

    /// The overflowed rows, as `(node, target)` pairs: the menu row to render
    /// and the action callback activating it runs.
    fn overflow_entries(&self) -> Vec<(MenuNode, OnPress<State>)> {
        self.partition()
            .overflow
            .into_iter()
            .map(|index| {
                let action = &self.actions[index];
                let node: MenuNode = menu_entry(action.menu_label(index))
                    .leading(action.icon)
                    .enabled(action.enabled)
                    .destructive(action.destructive)
                    .into();
                (node, action.on_press.clone())
            })
            .collect()
    }

    /// The composed views for the typed actions plus, when anything
    /// overflowed, the trailing overflow trigger — the center group's own
    /// tail.
    fn action_views(&self) -> Vec<AnyView<State>> {
        let split = self.partition();
        let mut views: Vec<AnyView<State>> = split
            .inline
            .iter()
            .map(|index| self.actions[*index].inline_view(self.size))
            .collect();
        if !split.overflow.is_empty() {
            views.push(self.trigger_view());
        }
        views
    }

    /// The overflow trigger's own view — an icon button reporting
    /// [`Self::on_overflow`], wrapped in [`overlay_anchor`] when an anchor is
    /// wired so its window rect reaches [`Self::overflow_menu`].
    fn trigger_view(&self) -> AnyView<State> {
        let on_overflow = self.on_overflow.clone();
        let trigger = icon_button::<State, _>(
            frust::authoring::any::<State, _>(icon(self.overflow_icon)),
            move |state: &mut State| {
                if let Some(callback) = &on_overflow {
                    callback(state);
                }
            },
        )
        .size(self.size.icon_button_size())
        .semantic_label(self.overflow_label.clone());
        match &self.overflow_anchor {
            Some(anchor) => frust::authoring::any::<State, _>(overlay_anchor(anchor, trigger)),
            None => frust::authoring::any::<State, _>(trigger),
        }
    }

    /// The anchored overflow panel for the actions that did not fit — mount
    /// this separately as the top of the app's own [`frust::Stack`], anchored
    /// to the same [`OverlayAnchor`] handed to [`Self::overflow_anchor`], and
    /// toggle `open` rather than unmounting it (the kept-mounted pattern
    /// [`mod@crate::overlay::anchored`] documents).
    ///
    /// Each overflowed action becomes a [`crate::menu::MenuEntry`] row led by
    /// its own glyph; activating one runs that action's `on_press`, exactly as
    /// its inline button would. The placement is upstream's own
    /// `M3EMenuAnchorPosition.bottomEnd`.
    pub fn overflow_menu(&self, open: bool) -> AnyView<State> {
        let anchor = self.overflow_anchor.clone().unwrap_or_default();
        let entries = self.overflow_entries();
        let nodes: Vec<MenuNode> = entries.iter().map(|(node, _)| node.clone()).collect();
        let targets: Vec<OnPress<State>> = entries.into_iter().map(|(_, target)| target).collect();

        frust::authoring::any::<State, _>(
            menu(nodes, move |state: &mut State, selection: MenuSelection| {
                if let Some(target) = targets.get(selection.index) {
                    target(state);
                }
            })
            .anchor(&anchor)
            .align(OverlayAlign::End)
            .open(open),
        )
    }

    /// The bar's resting elevation — the explicit override, else upstream's
    /// own `_hasFab ? elevationWithFab : elevation`.
    fn resolved_elevation(&self) -> ToolbarElevation {
        self.elevation.unwrap_or(if self.fab.is_some() {
            ToolbarElevation::Level1
        } else {
            ToolbarElevation::None
        })
    }

    /// The expand progress this configuration rests at: a bar with no morph to
    /// run is always fully expanded (upstream's own `startExpanded`).
    fn resting_progress(&self) -> f64 {
        if self.morphs() && !self.expanded {
            0.0
        } else {
            1.0
        }
    }

    /// Whether this bar runs the adjacent FAB morph at all — upstream's
    /// `_usesFabExpand` (`_hasFab && fabExpandsToolbar`, floating only).
    fn morphs(&self) -> bool {
        self.variant == ToolbarVariant::Floating && self.fab.is_some() && self.fab_expands_toolbar
    }

    /// Whether an adjacent FAB sits beside the pill rather than inside the row
    /// — upstream's `_hasFab` on a floating bar, morphing or not.
    fn adjacent_fab(&self) -> bool {
        self.variant == ToolbarVariant::Floating && self.fab.is_some()
    }
}

/// Collect `view`'s slot children into one ordered slice of [`AnyView`]
/// references — leading, center, the composed action views (held in
/// `actions`), trailing, then the fab slot. This is the shape both
/// `build`/`teardown` and [`frust::authoring::rebuild_children`] walk.
///
/// The composed action views cannot live in the view itself (they are derived
/// from the split, fresh per pass), so they are passed in alongside — the
/// one-slot storage handoff [`super::appbar`]'s own `title_ref` uses,
/// generalized to a list.
fn slot_views<'a, State: 'static>(
    view: &'a ToolbarView<State>,
    actions: &'a [AnyView<State>],
) -> Vec<&'a AnyView<State>> {
    let mut views = Vec::with_capacity(
        view.leading.len()
            + view.center.len()
            + actions.len()
            + view.trailing.len()
            + usize::from(view.fab.is_some()),
    );
    views.extend(view.leading.iter());
    views.extend(view.center.iter());
    views.extend(actions.iter());
    views.extend(view.trailing.iter());
    if let Some(fab) = &view.fab {
        views.push(fab);
    }
    views
}

// ---- The widget ------------------------------------------------------------

/// The retained widget for a [`ToolbarView`].
pub struct ToolbarWidget {
    variant: ToolbarVariant,
    color_style: ToolbarColorStyle,
    elevation: ToolbarElevation,
    /// Every slot pod, in the fixed order [`slot_views`] produces: leading,
    /// center, inline actions (plus the overflow trigger), trailing, then the
    /// fab slot — the one list [`frust::authoring::route_event`] hit-tests and
    /// [`frust::authoring::rebuild_children`] reconciles.
    slots: Vec<ChildPod>,
    leading_count: usize,
    center_count: usize,
    trailing_count: usize,
    has_fab: bool,
    adjacent_fab: bool,
    fab_expands: bool,
    fab_position: ToolbarFabPosition,
    semantic_label: Option<String>,
    exit_direction: ToolbarExitDirection,
    exit_extent: Option<f64>,
    /// The app-pinned hidden fraction, if any (bypasses [`Self::hide`]).
    hidden_pinned: Option<f64>,
    /// `0` collapsed through `1` expanded — the FAB morph and pill reveal.
    expand: RampTrack,
    /// `0` visible through `1` hidden — the scroll-hide travel.
    hide: RampTrack,
    /// The pill's own box, in bar-local px (set by `layout`, read by `paint`).
    pill_size: Size,
    /// The pill's origin within the bar's box, hide shift already applied.
    pill_origin: Point,
    /// The clamped expand progress `paint` clips and fades the pill with.
    reveal: f64,
}

impl<State: 'static> View<State> for ToolbarView<State> {
    type Element = ToolbarWidget;

    fn build(&self, ctx: &mut BuildCtx<'_>) -> ToolbarWidget {
        let actions = self.action_views();
        let slots = slot_views(self, &actions)
            .into_iter()
            .map(|view| frust::authoring::build_child(view, ctx))
            .collect();
        let hidden = self
            .hidden_fraction
            .map_or(if self.visible { 0.0 } else { 1.0 }, clamp01);
        ToolbarWidget {
            variant: self.variant,
            color_style: self.color_style,
            elevation: self.resolved_elevation(),
            slots,
            leading_count: self.leading.len(),
            center_count: self.center.len() + actions.len(),
            trailing_count: self.trailing.len(),
            has_fab: self.fab.is_some(),
            adjacent_fab: self.adjacent_fab(),
            fab_expands: self.fab_expands_toolbar,
            fab_position: self.fab_position,
            semantic_label: self.semantic_label.clone(),
            exit_direction: self.exit_direction,
            exit_extent: self.exit_extent,
            hidden_pinned: self.hidden_fraction.map(clamp01),
            expand: RampTrack::resting(self.resting_progress()),
            hide: RampTrack::resting(hidden),
            pill_size: Size::ZERO,
            pill_origin: Point::ZERO,
            reveal: 1.0,
        }
    }

    fn rebuild(
        &self,
        prev: &Self,
        element: &mut ToolbarWidget,
        ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        let mut flags = ChangeFlags::NONE;

        let prev_actions = prev.action_views();
        let next_actions = self.action_views();
        let prev_views = slot_views(prev, &prev_actions);
        let next_views = slot_views(self, &next_actions);
        flags |= frust::authoring::rebuild_children(
            &prev_views,
            &next_views,
            &mut element.slots,
            ctx,
            |view: &&AnyView<State>| *view,
            |_| None,
        );

        let center_count = self.center.len() + next_actions.len();
        for (slot, next) in [
            (&mut element.leading_count, self.leading.len()),
            (&mut element.center_count, center_count),
            (&mut element.trailing_count, self.trailing.len()),
        ] {
            if *slot != next {
                *slot = next;
                flags |= ChangeFlags::LAYOUT;
            }
        }

        let now_fab = self.fab.is_some();
        if element.has_fab != now_fab {
            element.has_fab = now_fab;
            flags |= ChangeFlags::LAYOUT;
        }
        let adjacent = self.adjacent_fab();
        if element.adjacent_fab != adjacent {
            element.adjacent_fab = adjacent;
            flags |= ChangeFlags::LAYOUT;
        }
        if element.fab_expands != self.fab_expands_toolbar {
            element.fab_expands = self.fab_expands_toolbar;
            flags |= ChangeFlags::LAYOUT;
        }
        if element.fab_position != self.fab_position {
            element.fab_position = self.fab_position;
            flags |= ChangeFlags::LAYOUT;
        }
        if element.variant != self.variant {
            element.variant = self.variant;
            flags |= ChangeFlags::LAYOUT | ChangeFlags::PAINT;
        }
        if element.color_style != self.color_style {
            element.color_style = self.color_style;
            flags |= ChangeFlags::PAINT;
        }
        let elevation = self.resolved_elevation();
        if element.elevation != elevation {
            element.elevation = elevation;
            flags |= ChangeFlags::PAINT;
        }
        if element.exit_direction != self.exit_direction {
            element.exit_direction = self.exit_direction;
            flags |= ChangeFlags::LAYOUT;
        }
        if element.exit_extent != self.exit_extent {
            element.exit_extent = self.exit_extent;
            flags |= ChangeFlags::LAYOUT;
        }
        if element.semantic_label != self.semantic_label {
            element.semantic_label = self.semantic_label.clone();
        }

        // Both ramps are layout values, so a retarget asks for a relayout
        // rather than only a repaint; `paint` keeps asking while the leg runs.
        let progress = self.resting_progress();
        if (element.expand.target - progress).abs() >= RAMP_EPSILON {
            element.expand.set_target(progress);
            flags |= ChangeFlags::LAYOUT;
        }

        let pinned = self.hidden_fraction.map(clamp01);
        element.hidden_pinned = pinned;
        match pinned {
            // A pinned fraction applies with no spring, and keeps the ramp in
            // step so an unpin resumes from where the drag left it.
            Some(fraction) => {
                if element.hide.value != fraction {
                    element.hide.jump(fraction);
                    flags |= ChangeFlags::LAYOUT;
                }
            }
            None => {
                let hidden = if self.visible { 0.0 } else { 1.0 };
                if (element.hide.target - hidden).abs() >= RAMP_EPSILON {
                    element.hide.set_target(hidden);
                    flags |= ChangeFlags::LAYOUT;
                }
            }
        }

        flags
    }

    fn teardown(&self, element: &mut ToolbarWidget, ctx: &mut BuildCtx<'_>) {
        let actions = self.action_views();
        for (view, pod) in slot_views(self, &actions)
            .into_iter()
            .zip(element.slots.iter_mut())
        {
            frust::authoring::teardown_child(view, pod, ctx);
        }
    }
}

impl ToolbarWidget {
    /// Whether the adjacent FAB morphs with the pill (upstream's
    /// `_usesFabExpand`).
    fn morphs(&self) -> bool {
        self.adjacent_fab && self.fab_expands
    }

    /// The index of the fab pod, when there is one.
    fn fab_index(&self) -> Option<usize> {
        self.has_fab.then(|| self.slots.len() - 1)
    }

    /// How many pods lay out inside the pill's row: everything but an adjacent
    /// FAB, which takes the morph layout instead.
    fn row_len(&self) -> usize {
        let row = self.leading_count + self.center_count + self.trailing_count;
        if self.has_fab && !self.adjacent_fab {
            row + 1
        } else {
            row
        }
    }

    /// The hidden fraction this frame — the app's pin, else the ramp.
    fn hidden(&self) -> f64 {
        self.hidden_pinned
            .unwrap_or_else(|| clamp01(self.hide.value))
    }

    /// The revealed window of the bar's box, in bar-local px: the sub-rect the
    /// pill is clipped to while the reveal runs (upstream's own clip, further
    /// intersected with this widget's own box so nothing paints outside it).
    fn reveal_window(&self, box_size: Size) -> (Point, Size) {
        if !self.morphs() || self.reveal >= 1.0 {
            return (Point::ZERO, box_size);
        }
        let natural = self.pill_size.width;
        let (x0, x1) = match self.fab_position {
            // Reveals from the trailing edge: the pill's own leading edge
            // walks left as `progress` grows.
            ToolbarFabPosition::End => (
                self.pill_origin.x + natural * (1.0 - self.reveal),
                box_size.width,
            ),
            // Reveals from the leading edge instead; the pill itself stays put.
            ToolbarFabPosition::Start => (0.0, self.pill_origin.x + natural * self.reveal),
        };
        let x0 = x0.clamp(0.0, box_size.width);
        let x1 = x1.clamp(x0, box_size.width);
        (Point::new(x0, 0.0), Size::new(x1 - x0, box_size.height))
    }
}

impl Widget for ToolbarWidget {
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        let progress = if self.morphs() {
            self.expand.value
        } else {
            1.0
        };
        self.reveal = clamp01(progress);

        let row_len = self.row_len();
        let row_bc = BoxConstraints::loose(Size::new(f64::INFINITY, TOOLBAR_HEIGHT));
        let sizes: Vec<Size> = self.slots[..row_len]
            .iter_mut()
            .map(|pod| pod.layout_child(ctx, &row_bc))
            .collect();

        // The row, laid out in pill-local coordinates.
        let pill_width = match self.variant {
            ToolbarVariant::Floating => {
                // Self-sized: one contiguous row, a `TOOLBAR_GAP` between every
                // adjacent pair (naturally collapsing across an empty group's
                // boundary, since the gap is per-item, not per-group) and
                // `TOOLBAR_FLOATING_PAD` at both ends.
                let mut x = TOOLBAR_FLOATING_PAD;
                for (pod, size) in self.slots[..row_len].iter_mut().zip(sizes.iter()) {
                    pod.set_origin(Point::new(x, (TOOLBAR_HEIGHT - size.height) / 2.0));
                    x += size.width + TOOLBAR_GAP;
                }
                if row_len > 0 {
                    x -= TOOLBAR_GAP;
                }
                x + TOOLBAR_FLOATING_PAD
            }
            ToolbarVariant::Docked => {
                let width = finite_or_zero(bc.max().width);

                // Leading: left-anchored, sequential.
                let mut left = TOOLBAR_DOCKED_PAD_X;
                for (pod, size) in self.slots[..self.leading_count]
                    .iter_mut()
                    .zip(sizes[..self.leading_count].iter())
                {
                    pod.set_origin(Point::new(left, (TOOLBAR_HEIGHT - size.height) / 2.0));
                    left += size.width + TOOLBAR_GAP;
                }
                if self.leading_count > 0 {
                    left -= TOOLBAR_GAP;
                }

                // Trailing (+ the in-row fab, if any): right-anchored, reverse
                // order, so the tail lands with the last item flush against
                // the trailing edge and reading order preserved.
                let tail_start = self.leading_count + self.center_count;
                let mut right = width - TOOLBAR_DOCKED_PAD_X;
                for (pod, size) in self.slots[tail_start..row_len]
                    .iter_mut()
                    .zip(sizes[tail_start..row_len].iter())
                    .rev()
                {
                    right -= size.width;
                    pod.set_origin(Point::new(right, (TOOLBAR_HEIGHT - size.height) / 2.0));
                    right -= TOOLBAR_GAP;
                }
                if tail_start < row_len {
                    right += TOOLBAR_GAP;
                }

                // Center: centered as a group within `[left, right]`.
                let center_range = self.leading_count..tail_start;
                let mut center_total: f64 =
                    sizes[center_range.clone()].iter().map(|s| s.width).sum();
                if center_range.len() > 1 {
                    center_total += TOOLBAR_GAP * (center_range.len() - 1) as f64;
                }
                let available = (right - left).max(0.0);
                let mut cx = left + ((available - center_total) / 2.0).max(0.0);
                for (pod, size) in self.slots[center_range.clone()]
                    .iter_mut()
                    .zip(sizes[center_range].iter())
                {
                    pod.set_origin(Point::new(cx, (TOOLBAR_HEIGHT - size.height) / 2.0));
                    cx += size.width + TOOLBAR_GAP;
                }

                width
            }
        };
        self.pill_size = Size::new(pill_width, TOOLBAR_HEIGHT);

        // The bar's own box, and where the pill sits inside it — upstream's
        // `RenderM3EToolbarHorizontalFabLayout.performLayout` whenever a FAB
        // sits beside the pill.
        let (box_size, pill_origin) = if self.adjacent_fab {
            let total = Size::new(
                pill_width + TOOLBAR_TO_FAB_GAP + TOOLBAR_FAB_BASELINE,
                TOOLBAR_FAB_MEDIUM,
            );
            let revealed = pill_width * progress.clamp(0.0, REVEAL_OVERSHOOT);
            let x = match self.fab_position {
                ToolbarFabPosition::End => pill_width - revealed,
                ToolbarFabPosition::Start => total.width - pill_width,
            };
            (
                total,
                Point::new(x, (TOOLBAR_FAB_MEDIUM - TOOLBAR_HEIGHT) / 2.0),
            )
        } else {
            (self.pill_size, Point::ZERO)
        };

        // The adjacent FAB's own square. Tight constraints are what carry the
        // morph here — see the [module docs](self)' fab-seam note.
        if self.adjacent_fab
            && let Some(index) = self.fab_index()
        {
            let fab_size = if self.fab_expands {
                ToolbarMorph::size(progress)
            } else {
                TOOLBAR_FAB_BASELINE
            };
            let pod = &mut self.slots[index];
            pod.layout_child(ctx, &BoxConstraints::tight(Size::new(fab_size, fab_size)));
            let x = match self.fab_position {
                ToolbarFabPosition::End => box_size.width - fab_size,
                ToolbarFabPosition::Start => 0.0,
            };
            pod.set_origin(Point::new(x, (TOOLBAR_FAB_MEDIUM - fab_size) / 2.0));
        }

        // The scroll-hide travel: a layout value, applied to every pod (and to
        // the pill origin `paint` fills against) so hit-testing follows the
        // visual exactly.
        let hidden = self.hidden();
        // `finite_or_zero`-funneled: `exit_extent` is caller-fed, and a
        // non-finite value here would carry through `shift` into
        // `pill_origin` and, from there, into `reveal_window`'s clamp bound
        // — see the [module docs](self)' clamp discipline.
        let extent = finite_or_zero(self.exit_extent.unwrap_or(
            if self.exit_direction.is_vertical() {
                box_size.height + TOOLBAR_SCREEN_OFFSET
            } else {
                box_size.width + TOOLBAR_SCREEN_OFFSET
            },
        ));
        let shift = self.exit_direction.offset(hidden * extent.abs());
        self.pill_origin = pill_origin + shift;

        // Row pods sit in pill-local coordinates; lift them into the box, with
        // the travel already folded into the pill's own origin.
        let pill_shift = self.pill_origin.to_vec2();
        if pill_shift != Vec2::ZERO {
            for pod in self.slots[..row_len].iter_mut() {
                let origin = pod.origin();
                pod.set_origin(origin + pill_shift);
            }
        }
        // An adjacent FAB was placed in box coordinates already, so it takes
        // the travel alone.
        if self.adjacent_fab
            && shift != Vec2::ZERO
            && let Some(index) = self.fab_index()
        {
            let origin = self.slots[index].origin();
            self.slots[index].set_origin(origin + shift);
        }

        bc.constrain(box_size)
    }

    fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut dyn PaintScene) {
        // Resolve every theme-derived value before the `&mut ctx` reborrows
        // below (`request_layout`) — `theme` borrows `ctx` shared, so its last
        // use must precede them for this to typecheck under NLL.
        let theme = Theme::from_paint_ctx(ctx);
        let colors = ToolbarColors::resolve(theme, self.color_style);
        let shadow = resolve_shadow(theme, self.elevation);
        let radius = resolve_radius(
            theme,
            self.variant,
            self.pill_size.width,
            self.pill_size.height,
        );
        let reduce_motion = theme.is_some_and(|t| t.motion.reduce_motion);
        let now = ctx.frame_time();

        // Both ramps drive layout values (the FAB square, the hide travel), so
        // an in-flight leg needs an explicit relayout, not merely another
        // frame — the layout-skip class the module docs describe. Either arm
        // asks: still in flight (the next frame moves it again), or the value
        // moved this frame — the *settling* frame is the one that both stops
        // animating and lands on the target, so gating on the animating flag
        // alone would leave the bar a fraction short of rest.
        let moved = if reduce_motion {
            let expand = self.expand.snap();
            let hide = self.hide.snap();
            expand || hide
        } else {
            let before = (self.expand.value, self.hide.value);
            let expand = self.expand.advance(now);
            let hide = self.hide.advance(now);
            expand || hide || (self.expand.value, self.hide.value) != before
        };
        if moved {
            ctx.request_layout();
        }

        let origin = ctx.origin();
        let box_size = ctx.size();
        let alpha = if self.morphs() {
            ToolbarMorph::reveal_alpha(self.reveal) as f32
        } else {
            1.0
        };
        let (window_origin, window_size) = self.reveal_window(box_size);
        let clipping =
            window_origin != Point::ZERO || window_size != box_size || self.hidden() > 0.0;

        if clipping {
            scene.push_clip(
                Point::new(origin.x + window_origin.x, origin.y + window_origin.y),
                window_size,
            );
        }
        if alpha < 1.0 {
            scene.push_layer(origin, box_size, alpha);
        }

        let pill_origin = Point::new(origin.x + self.pill_origin.x, origin.y + self.pill_origin.y);
        if let Some((blur, y_offset, shadow_color)) = shadow {
            scene.draw_shadow(
                Point::new(pill_origin.x, pill_origin.y + y_offset),
                self.pill_size,
                radius,
                blur,
                shadow_color,
            );
        }
        scene.fill_rounded_rect(pill_origin, self.pill_size, radius, colors.container);

        let row_len = self.row_len();
        for pod in self.slots[..row_len].iter_mut() {
            pod.paint_child(ctx, scene);
        }

        if alpha < 1.0 {
            scene.pop_layer();
        }
        if clipping {
            scene.pop_clip();
        }

        // An adjacent FAB is outside the pill's reveal clip and fade entirely
        // (upstream paints it after, unclipped) — it is the one thing on
        // screen while the pill is fully collapsed.
        if self.adjacent_fab
            && let Some(index) = self.fab_index()
        {
            self.slots[index].paint_child(ctx, scene);
        }
    }

    fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
        frust::authoring::route_event(&mut self.slots, ctx, event)
    }

    fn semantics(&self, ctx: &mut SemanticsCtx) {
        let slots = &self.slots;
        let label = self.semantic_label.as_deref();
        ctx.push_container(
            Role::Toolbar,
            |node| {
                if let Some(label) = label {
                    node.set_label(label);
                }
            },
            |ctx| {
                for pod in slots {
                    pod.semantics_child(ctx);
                }
            },
        );
    }

    frust::authoring::visit_children!(slots);
}

#[cfg(test)]
mod tests {
    use super::*;
    use frust::authoring::BuildCtx;
    use frust_widgets::test_support::leaf_any;

    fn ctx(counter: &mut u64) -> BuildCtx<'_> {
        BuildCtx::new(counter)
    }

    /// Records each `fill_rounded_rect` call's `(origin, size, radius,
    /// color)` — this widget paints its container as a rounded rect, even at
    /// a docked-variant radius of `0.0`, so the shared
    /// [`frust_widgets::test_support::RecordingScene`] (which only records
    /// `fill_rect`) can't observe it.
    #[derive(Default)]
    struct RRectRecorder {
        rrects: Vec<(Point, Size, f64, Color)>,
        shadows: Vec<(Point, Size)>,
        clips: usize,
        layers: Vec<f32>,
    }
    impl PaintScene for RRectRecorder {
        fn fill_rect(&mut self, _o: Point, _s: Size, _c: Color) {}
        fn draw_text(&mut self, _o: Point, _t: &str) {}
        fn fill_rounded_rect(&mut self, o: Point, s: Size, radius: f64, color: Color) {
            self.rrects.push((o, s, radius, color));
        }
        fn draw_shadow(&mut self, o: Point, s: Size, _r: f64, _sd: f64, _c: Color) {
            self.shadows.push((o, s));
        }
        fn push_clip(&mut self, _o: Point, _s: Size) {
            self.clips += 1;
        }
        fn push_layer(&mut self, _o: Point, _s: Size, alpha: f32) {
            self.layers.push(alpha);
        }
    }

    fn build(view: &ToolbarView<()>) -> ToolbarWidget {
        let mut counter = 0u64;
        View::<()>::build(view, &mut ctx(&mut counter))
    }

    fn layout(w: &mut ToolbarWidget, bc: &BoxConstraints) -> Size {
        let mut lctx = LayoutCtx::new();
        w.layout(&mut lctx, bc)
    }

    /// Paint one frame `millis` into the ramp, reporting whether a relayout
    /// was requested (the in-flight/settling signal).
    fn paint_at(w: &mut ToolbarWidget, millis: u64, theme: Option<&Theme>) -> bool {
        let mut scene = RRectRecorder::default();
        let mut pctx = PaintCtx::for_test(
            Point::ZERO,
            Size::new(400.0, TOOLBAR_FAB_MEDIUM),
            FrameTime::from_nanos(millis * 1_000_000),
        );
        if let Some(theme) = theme {
            pctx = pctx.with_theme(theme);
        }
        w.paint(&mut pctx, &mut scene);
        pctx.needs_layout()
    }

    // ---- Metrics ----------------------------------------------------------

    #[test]
    fn the_ported_tokens_match_the_reference_table_exactly() {
        assert_eq!(TOOLBAR_HEIGHT, 64.0);
        assert_eq!(TOOLBAR_FLOATING_PAD, 8.0);
        assert_eq!(TOOLBAR_DOCKED_PAD_X, 16.0);
        assert_eq!(TOOLBAR_GAP, 4.0);
        assert_eq!(TOOLBAR_TO_FAB_GAP, 8.0);
        assert_eq!(TOOLBAR_SCREEN_OFFSET, 16.0);
        assert_eq!(TOOLBAR_FAB_BASELINE, 56.0);
        assert_eq!(TOOLBAR_FAB_MEDIUM, 80.0);
        assert_eq!(TOOLBAR_MAX_INLINE_ACTIONS, 4);
        assert_eq!(TOOLBAR_SETTLE_VELOCITY, 150.0);
    }

    #[test]
    fn the_expand_spring_is_the_reference_expressive_spatial_fast_token() {
        let token = crate::MaterialSpring::EXPRESSIVE_SPATIAL_FAST;
        assert_eq!(TOOLBAR_EXPAND_SPRING.stiffness, token.stiffness);
        assert_eq!(TOOLBAR_EXPAND_SPRING.damping_ratio, token.damping_ratio);
        assert_eq!(TOOLBAR_EXPAND_SPRING.mass, 1.0);
    }

    // ---- Layout: the two variants -----------------------------------------

    #[test]
    fn floating_layout_preserves_slot_order_and_applies_inset_pill_geometry() {
        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .center(vec![leaf_any(20.0, 20.0)])
            .trailing(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0));
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));

        // A floating bar with a FAB reserves the adjacent morph box.
        assert_eq!(size.height, TOOLBAR_FAB_MEDIUM);
        assert!(size.width < 800.0);

        // Slot order preserved: leading < center < trailing, left to right.
        assert!(w.slots[0].origin().x < w.slots[1].origin().x);
        assert!(w.slots[1].origin().x < w.slots[2].origin().x);

        // Inset from the pill's leading edge by `TOOLBAR_FLOATING_PAD`.
        assert_eq!(w.slots[0].origin().x, TOOLBAR_FLOATING_PAD);

        // A fully-rounded pill: radius resolves to half the fixed bar height
        // (the shorter side), the `ShapeScale::full` clamp.
        let radius = resolve_radius(
            None,
            ToolbarVariant::Floating,
            w.pill_size.width,
            w.pill_size.height,
        );
        assert_eq!(radius, TOOLBAR_HEIGHT / 2.0);
    }

    #[test]
    fn a_floating_toolbar_without_a_fab_hugs_its_own_content() {
        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .trailing(vec![leaf_any(24.0, 24.0)]);
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));

        assert_eq!(size.height, TOOLBAR_HEIGHT);
        assert_eq!(
            size.width,
            TOOLBAR_FLOATING_PAD * 2.0 + 24.0 + TOOLBAR_GAP + 24.0,
            "pad + item + gap + item + pad"
        );
        assert_eq!(
            w.slots[1].origin().x + w.slots[1].size().width,
            size.width - TOOLBAR_FLOATING_PAD
        );
    }

    #[test]
    fn docked_layout_spans_the_available_width_with_flat_geometry() {
        let view: ToolbarView<()> = docked_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .trailing(vec![leaf_any(24.0, 24.0)]);
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(400.0, 200.0)));
        assert_eq!(size, Size::new(400.0, TOOLBAR_HEIGHT));

        let radius = resolve_radius(None, ToolbarVariant::Docked, size.width, size.height);
        assert_eq!(radius, 0.0, "docked variant is flat (square corners)");

        assert_eq!(w.slots[0].origin().x, TOOLBAR_DOCKED_PAD_X);
        assert_eq!(
            w.slots[1].origin().x + w.slots[1].size().width,
            400.0 - TOOLBAR_DOCKED_PAD_X
        );
    }

    #[test]
    fn docked_layout_centers_the_center_group_between_leading_and_trailing() {
        let view: ToolbarView<()> = docked_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .center(vec![leaf_any(20.0, 20.0)])
            .trailing(vec![leaf_any(24.0, 24.0)]);
        let mut w = build(&view);
        layout(&mut w, &BoxConstraints::loose(Size::new(400.0, 200.0)));

        assert!(w.slots[0].origin().x < w.slots[1].origin().x);
        assert!(w.slots[1].origin().x < w.slots[2].origin().x);

        let leading_end = w.slots[0].origin().x + w.slots[0].size().width;
        let trailing_start = w.slots[2].origin().x;
        let center_start = w.slots[1].origin().x;
        let center_end = center_start + w.slots[1].size().width;
        let left_gap = center_start - leading_end;
        let right_gap = trailing_start - center_end;
        assert!(
            (left_gap - right_gap).abs() < 0.01,
            "the center group is centered in the remaining space \
             (left_gap={left_gap}, right_gap={right_gap})"
        );
    }

    #[test]
    fn docked_layout_places_fab_flush_against_the_trailing_edge_after_trailing() {
        let view: ToolbarView<()> = docked_toolbar()
            .trailing(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0));
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(400.0, 200.0)));

        // Docked keeps the fab in-row: the box is still one bar tall.
        assert_eq!(size, Size::new(400.0, TOOLBAR_HEIGHT));
        assert!(w.slots[0].origin().x < w.slots[1].origin().x);
        assert_eq!(
            w.slots[1].origin().x + w.slots[1].size().width,
            400.0 - TOOLBAR_DOCKED_PAD_X,
            "the fab slot sits flush against the trailing edge"
        );
    }

    // ---- The FAB morph ----------------------------------------------------

    #[test]
    fn the_fab_morphs_from_80_collapsed_to_56_expanded() {
        assert_eq!(ToolbarMorph::size(0.0), TOOLBAR_FAB_MEDIUM);
        assert_eq!(ToolbarMorph::size(1.0), TOOLBAR_FAB_BASELINE);
        assert_eq!(ToolbarMorph::size(0.5), 68.0);
        // The expand spring's overshoot is real travel, never clamped away…
        assert!(ToolbarMorph::size(1.2) < TOOLBAR_FAB_BASELINE);
        // …but the square never goes negative, and NaN rests collapsed.
        assert_eq!(ToolbarMorph::size(100.0), 0.0);
        assert_eq!(ToolbarMorph::size(f64::NAN), TOOLBAR_FAB_MEDIUM);
    }

    #[test]
    fn a_collapsed_bar_lays_its_fab_out_at_the_80dp_end_and_hides_the_pill() {
        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0))
            .expanded(false);
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));

        let fab = w.slots[w.fab_index().expect("a fab pod")].size();
        assert_eq!(fab, Size::new(TOOLBAR_FAB_MEDIUM, TOOLBAR_FAB_MEDIUM));
        assert_eq!(size.height, TOOLBAR_FAB_MEDIUM);
        assert_eq!(w.reveal, 0.0, "collapsed: nothing of the pill is revealed");

        // The reveal window opens past the pill's own trailing edge, so none
        // of the pill paints at all — only the FAB is on screen.
        let (window_origin, _) = w.reveal_window(size);
        assert!(window_origin.x >= w.pill_origin.x + w.pill_size.width);
    }

    /// A NaN `exit_extent` prop must not corrupt `pill_origin` into a value
    /// `reveal_window`'s clamp would abort on — `x0` becomes the second
    /// clamp's `min` bound there, so an unguarded NaN reaching it panics on
    /// `assert!(min <= max)`, not merely on the clamped value itself. This is
    /// the layout/paint counterpart to the constructor guard above; see the
    /// module docs' clamp discipline.
    #[test]
    fn a_non_finite_exit_extent_prop_reaches_layout_and_paint_without_panicking() {
        for extent in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            let view: ToolbarView<()> = floating_toolbar()
                .leading(vec![leaf_any(24.0, 24.0)])
                .fab(leaf_any(40.0, 40.0))
                .expanded(false)
                .exit_extent(Some(extent));
            let mut w = build(&view);
            let size = layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));

            assert!(w.pill_origin.x.is_finite());
            assert!(w.pill_origin.y.is_finite());

            let (window_origin, window_size) = w.reveal_window(size);
            assert!(window_origin.x.is_finite());
            assert!(window_size.width.is_finite());

            // Painting must not panic either — the same clamp reruns inside
            // `paint`.
            paint_at(&mut w, 0, None);
        }
    }

    #[test]
    fn an_expanded_bar_lays_its_fab_out_at_the_56dp_end_beside_the_pill() {
        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0));
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));

        let index = w.fab_index().expect("a fab pod");
        assert_eq!(
            w.slots[index].size(),
            Size::new(TOOLBAR_FAB_BASELINE, TOOLBAR_FAB_BASELINE)
        );
        // The box always reserves `fabBaseline` beside the pill, whatever the
        // morph is currently painting.
        assert_eq!(
            size.width,
            w.pill_size.width + TOOLBAR_TO_FAB_GAP + TOOLBAR_FAB_BASELINE
        );
        // Expanded, the pill sits flush at the box's leading edge and the FAB
        // flush at its trailing one.
        assert_eq!(w.pill_origin.x, 0.0);
        assert_eq!(
            w.slots[index].origin().x + TOOLBAR_FAB_BASELINE,
            size.width,
            "the fab hugs the trailing edge"
        );
        assert_eq!(w.reveal, 1.0);
    }

    #[test]
    fn pinning_fab_expands_toolbar_off_keeps_the_fab_at_the_baseline_square() {
        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0))
            .fab_expands_toolbar(false)
            .expanded(false);
        let mut w = build(&view);
        layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));

        let index = w.fab_index().expect("a fab pod");
        assert_eq!(
            w.slots[index].size(),
            Size::new(TOOLBAR_FAB_BASELINE, TOOLBAR_FAB_BASELINE)
        );
        assert_eq!(w.reveal, 1.0, "the pill stays open");
    }

    #[test]
    fn a_start_positioned_fab_leads_the_pill() {
        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0))
            .fab_position(ToolbarFabPosition::Start);
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));

        let index = w.fab_index().expect("a fab pod");
        assert_eq!(w.slots[index].origin().x, 0.0);
        assert_eq!(w.pill_origin.x, size.width - w.pill_size.width);
    }

    #[test]
    fn the_expand_ramp_settles_onto_the_target_and_stops_requesting_layout() {
        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0))
            .expanded(false);
        let next: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0));

        let mut counter = 0u64;
        let mut w = View::<()>::build(&view, &mut ctx(&mut counter));
        layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));
        assert!(!paint_at(&mut w, 0, None), "a resting bar asks for nothing");

        let flags = View::<()>::rebuild(&next, &view, &mut w, &mut ctx(&mut counter));
        assert!(flags.needs_layout(), "a retarget is a layout change");

        let mut frames = 0u64;
        loop {
            layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));
            let still = paint_at(&mut w, frames * 16, None);
            frames += 1;
            if !still {
                break;
            }
            assert!(frames < 400, "the ramp settles well inside 400 frames");
        }
        layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));
        assert_eq!(w.expand.value, 1.0, "the settled ramp lands on the target");
        assert_eq!(
            w.slots[w.fab_index().expect("a fab pod")].size().width,
            TOOLBAR_FAB_BASELINE
        );
    }

    #[test]
    fn reduce_motion_snaps_both_ramps_and_requests_exactly_one_relayout() {
        let mut theme = crate::baseline();
        theme.motion.reduce_motion = true;

        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0))
            .expanded(false);
        let next: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0));

        let mut counter = 0u64;
        let mut w = View::<()>::build(&view, &mut ctx(&mut counter));
        layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));
        View::<()>::rebuild(&next, &view, &mut w, &mut ctx(&mut counter));

        assert!(
            paint_at(&mut w, 16, Some(&theme)),
            "the snap moved a layout value, so it asks for one relayout"
        );
        assert_eq!(w.expand.value, 1.0, "snapped");
        assert!(
            !paint_at(&mut w, 32, Some(&theme)),
            "and never asks again once there is nothing left to move"
        );
    }

    // ---- Scroll-hide ------------------------------------------------------

    #[test]
    fn the_measured_exit_limit_is_the_bar_extent_plus_the_screen_offset() {
        let hide = ToolbarScrollHide::for_bar(TOOLBAR_HEIGHT);
        assert_eq!(
            hide.offset_limit(),
            -(TOOLBAR_HEIGHT + TOOLBAR_SCREEN_OFFSET)
        );
        assert_eq!(hide.collapsed_fraction(), 0.0);
        assert!(!hide.is_hidden());

        // An explicit extent pins the limit instead, sign-independently.
        assert_eq!(ToolbarScrollHide::new(-40.0).offset_limit(), -40.0);
    }

    /// A degenerate (NaN/∞) construction parameter must degrade to a resting
    /// controller, never panic — the module docs' clamp-discipline
    /// funnel-at-entry rule, exercised through both constructors and a
    /// following `scroll()` sequence (the site that would otherwise abort on
    /// a non-finite `limit`).
    #[test]
    fn a_non_finite_construction_parameter_degrades_instead_of_panicking() {
        for degenerate in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            let mut hide = ToolbarScrollHide::new(degenerate);
            assert_eq!(hide.offset_limit(), 0.0, "degrades to a resting limit");
            // A subsequent scroll/settle/toggle sequence must not panic —
            // the site that would otherwise abort on a non-finite `limit` —
            // and every reachable state stays finite.
            hide.scroll(40.0);
            hide.scroll(-1_000.0);
            hide.settle(1_000.0);
            hide.toggle();
            assert!(hide.offset().is_finite());

            let mut hide = ToolbarScrollHide::for_bar(degenerate);
            assert_eq!(
                hide.offset_limit(),
                0.0,
                "for_bar degrades the same way, delegating to new"
            );
            hide.scroll(degenerate);
            hide.scroll(40.0);
            hide.toggle();
            assert!(hide.offset().is_finite());
        }
    }

    #[test]
    fn scrolling_accumulates_into_a_clamped_hidden_fraction() {
        let mut hide = ToolbarScrollHide::new(80.0);
        hide.scroll(40.0);
        assert_eq!(hide.offset(), -40.0);
        assert_eq!(hide.collapsed_fraction(), 0.5);

        // Scrolling back up returns the bar.
        hide.scroll(-15.0);
        assert_eq!(hide.offset(), -25.0);

        // Both ends clamp, and a non-finite delta is ignored outright.
        hide.scroll(1_000.0);
        assert_eq!(hide.offset(), -80.0);
        assert!(hide.is_hidden());
        hide.scroll(f64::NAN);
        assert_eq!(hide.offset(), -80.0);
        hide.scroll(-1_000.0);
        assert_eq!(hide.offset(), 0.0);
    }

    #[test]
    fn a_settle_commits_to_the_fling_direction_past_the_velocity_threshold() {
        let mut hide = ToolbarScrollHide::new(80.0);
        hide.scroll(10.0);
        // A downward fling hides even from barely-moved.
        assert!(!hide.settle(TOOLBAR_SETTLE_VELOCITY + 1.0));
        assert!(hide.is_hidden());

        let mut hide = ToolbarScrollHide::new(80.0);
        hide.scroll(70.0);
        // An upward fling shows even from nearly-hidden.
        assert!(hide.settle(-(TOOLBAR_SETTLE_VELOCITY + 1.0)));
        assert_eq!(hide.offset(), 0.0);

        // Under the threshold the nearer end wins.
        let mut hide = ToolbarScrollHide::new(80.0);
        hide.scroll(30.0);
        assert!(hide.settle(10.0));
        let mut hide = ToolbarScrollHide::new(80.0);
        hide.scroll(50.0);
        assert!(!hide.settle(10.0));

        // Already settled, nothing moves.
        let mut hide = ToolbarScrollHide::new(80.0);
        assert!(hide.settle(1_000.0));
        assert_eq!(hide.offset(), 0.0);
    }

    #[test]
    fn toggle_flips_about_the_halfway_point() {
        let mut hide = ToolbarScrollHide::new(80.0);
        hide.toggle();
        assert!(hide.is_hidden());
        hide.toggle();
        assert_eq!(hide.offset(), 0.0);
    }

    #[test]
    fn a_pinned_hidden_fraction_translates_every_slot_along_the_exit_direction() {
        let visible: ToolbarView<()> = docked_toolbar().leading(vec![leaf_any(24.0, 24.0)]);
        let mut w = build(&visible);
        layout(&mut w, &BoxConstraints::loose(Size::new(400.0, 200.0)));
        let resting = w.slots[0].origin();

        let hidden: ToolbarView<()> = docked_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .hidden_fraction(Some(1.0));
        let mut counter = 0u64;
        let flags = View::<()>::rebuild(&hidden, &visible, &mut w, &mut ctx(&mut counter));
        assert!(flags.needs_layout(), "the hide travel is a layout value");
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(400.0, 200.0)));

        // Bottom exit: the whole bar slides down by its own extent plus the
        // screen offset, so nothing of it remains inside the box.
        let travel = size.height + TOOLBAR_SCREEN_OFFSET;
        assert_eq!(w.slots[0].origin().y, resting.y + travel);
        assert_eq!(w.pill_origin.y, travel);
        assert!(w.slots[0].origin().y >= size.height);
    }

    #[test]
    fn every_exit_direction_translates_on_its_own_axis() {
        for (direction, expected) in [
            (ToolbarExitDirection::Top, Vec2::new(0.0, -10.0)),
            (ToolbarExitDirection::Bottom, Vec2::new(0.0, 10.0)),
            (ToolbarExitDirection::Start, Vec2::new(-10.0, 0.0)),
            (ToolbarExitDirection::End, Vec2::new(10.0, 0.0)),
        ] {
            assert_eq!(direction.offset(10.0), expected);
        }
        assert!(ToolbarExitDirection::Top.is_vertical());
        assert!(!ToolbarExitDirection::End.is_vertical());
    }

    #[test]
    fn an_explicit_exit_extent_overrides_the_measured_one() {
        let view: ToolbarView<()> = docked_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .hidden_fraction(Some(1.0))
            .exit_extent(Some(20.0));
        let mut w = build(&view);
        layout(&mut w, &BoxConstraints::loose(Size::new(400.0, 200.0)));
        assert_eq!(w.pill_origin.y, 20.0);
    }

    #[test]
    fn the_visible_prop_springs_the_hide_travel_and_settles_hidden() {
        let visible: ToolbarView<()> = docked_toolbar().leading(vec![leaf_any(24.0, 24.0)]);
        let mut counter = 0u64;
        let mut w = View::<()>::build(&visible, &mut ctx(&mut counter));
        layout(&mut w, &BoxConstraints::loose(Size::new(400.0, 200.0)));

        let hidden: ToolbarView<()> = docked_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .visible(false);
        View::<()>::rebuild(&hidden, &visible, &mut w, &mut ctx(&mut counter));

        let mut frames = 0u64;
        loop {
            layout(&mut w, &BoxConstraints::loose(Size::new(400.0, 200.0)));
            let still = paint_at(&mut w, frames * 16, None);
            frames += 1;
            if !still {
                break;
            }
            assert!(frames < 400, "the hide ramp settles well inside 400 frames");
        }
        assert_eq!(w.hidden(), 1.0, "the settled ramp lands fully hidden");
    }

    // ---- Overflow ---------------------------------------------------------

    #[test]
    fn the_capacity_rule_matches_the_reference_partition() {
        // Everything fits: nothing overflows and there is no trigger slot.
        let split = partition_actions(3, TOOLBAR_MAX_INLINE_ACTIONS, None);
        assert_eq!(split.inline, vec![0, 1, 2]);
        assert!(split.overflow.is_empty());

        // Surplus actions overflow in order.
        let split = partition_actions(7, TOOLBAR_MAX_INLINE_ACTIONS, None);
        assert_eq!(split.inline, vec![0, 1, 2, 3]);
        assert_eq!(split.overflow, vec![4, 5, 6]);

        // An expand trigger always stays inline and reserves one slot.
        let split = partition_actions(7, TOOLBAR_MAX_INLINE_ACTIONS, Some(0));
        assert_eq!(split.inline, vec![0, 1, 2, 3]);
        assert_eq!(split.overflow, vec![4, 5, 6]);
        let split = partition_actions(7, TOOLBAR_MAX_INLINE_ACTIONS, Some(6));
        assert_eq!(split.inline, vec![0, 1, 2, 6]);
        assert_eq!(split.overflow, vec![3, 4, 5]);

        // A zero budget overflows everything but the trigger.
        let split = partition_actions(3, 0, Some(1));
        assert_eq!(split.inline, vec![1]);
        assert_eq!(split.overflow, vec![0, 2]);

        // An out-of-range trigger index is ignored rather than reserving.
        let split = partition_actions(2, 1, Some(9));
        assert_eq!(split.inline, vec![0]);
        assert_eq!(split.overflow, vec![1]);
    }

    fn action(label: &str) -> ToolbarAction<Vec<String>> {
        let owned = label.to_owned();
        toolbar_action::<Vec<String>, _>(crate::icons::MORE_HORIZ, move |log: &mut Vec<String>| {
            log.push(owned.clone())
        })
        .label(label)
    }

    #[test]
    fn overflowed_actions_become_menu_rows_routing_back_to_their_own_action() {
        let view: ToolbarView<Vec<String>> = floating_toolbar()
            .actions(vec![
                action("one"),
                action("two"),
                action("three"),
                action("four"),
                action("five"),
                action("six"),
            ])
            .max_inline_actions(4);

        let entries = view.overflow_entries();
        assert_eq!(entries.len(), 2, "two actions past the budget");
        let labels: Vec<String> = entries
            .iter()
            .map(|(node, _)| match node {
                MenuNode::Entry(entry) => entry.label().to_owned(),
                _ => unreachable!("an overflowed action is always a plain entry"),
            })
            .collect();
        assert_eq!(labels, vec!["five".to_owned(), "six".to_owned()]);

        // Activating row `k` runs the action it was split from.
        let mut log: Vec<String> = Vec::new();
        entries[1].1(&mut log);
        assert_eq!(log, vec!["six".to_owned()]);
    }

    #[test]
    fn an_overflowed_row_falls_back_through_the_reference_label_chain() {
        let bare = toolbar_action::<(), _>(crate::icons::MORE_VERT, |_| {});
        assert_eq!(bare.menu_label(3), "Action 4");
        assert_eq!(
            toolbar_action::<(), _>(crate::icons::MORE_VERT, |_| {})
                .tooltip("Tip")
                .menu_label(0),
            "Tip"
        );
        assert_eq!(
            toolbar_action::<(), _>(crate::icons::MORE_VERT, |_| {})
                .tooltip("Tip")
                .label("Label")
                .menu_label(0),
            "Label"
        );
        assert_eq!(
            toolbar_action::<(), _>(crate::icons::MORE_VERT, |_| {})
                .semantic_label("Named")
                .menu_label(0),
            "Named"
        );
    }

    #[test]
    fn the_overflow_trigger_joins_the_row_only_once_something_overflows() {
        let fits: ToolbarView<Vec<String>> =
            floating_toolbar().actions(vec![action("one"), action("two")]);
        let mut w = build_any(&fits);
        assert_eq!(w.center_count, 2, "no trigger while everything fits");

        let spills: ToolbarView<Vec<String>> = floating_toolbar().actions(vec![
            action("one"),
            action("two"),
            action("three"),
            action("four"),
            action("five"),
        ]);
        w = build_any(&spills);
        assert_eq!(w.center_count, 5, "four inline actions plus the trigger");
        assert_eq!(w.slots.len(), 5);
    }

    fn build_any<State: 'static>(view: &ToolbarView<State>) -> ToolbarWidget {
        let mut counter = 0u64;
        View::<State>::build(view, &mut ctx(&mut counter))
    }

    // ---- Paint -------------------------------------------------------------

    #[test]
    fn unthemed_paint_uses_the_standard_fallback_container() {
        let view: ToolbarView<()> = docked_toolbar();
        let mut w = build(&view);
        layout(&mut w, &BoxConstraints::loose(Size::new(300.0, 100.0)));
        let mut scene = RRectRecorder::default();
        let mut pctx = PaintCtx::new(Point::ZERO, Size::new(300.0, TOOLBAR_HEIGHT));
        w.paint(&mut pctx, &mut scene);
        assert_eq!(scene.rrects[0].1, Size::new(300.0, TOOLBAR_HEIGHT));
        assert_eq!(scene.rrects[0].3, SURFACE_CONTAINER);
        assert!(
            scene.shadows.is_empty(),
            "a bar with no fab rests at `elevationNone`"
        );
    }

    #[test]
    fn themed_paint_resolves_surface_container() {
        let theme = crate::baseline();
        let view: ToolbarView<()> = floating_toolbar().leading(vec![leaf_any(24.0, 24.0)]);
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(300.0, 100.0)));

        let mut scene = RRectRecorder::default();
        let mut pctx = PaintCtx::new(Point::ZERO, size).with_theme(&theme);
        w.paint(&mut pctx, &mut scene);
        assert_eq!(scene.rrects[0].3, theme.scheme().surface_container);
        assert_eq!(
            scene.rrects[0].2,
            ShapeScale::resolve(theme.shape.full, size.width, size.height)
        );
    }

    #[test]
    fn the_vibrant_style_resolves_the_reference_primary_container_pair() {
        let theme = crate::baseline();
        let standard = ToolbarColors::resolve(Some(&theme), ToolbarColorStyle::Standard);
        assert_eq!(standard.container, theme.scheme().surface_container);
        assert_eq!(standard.content, theme.scheme().on_surface);
        assert_eq!(standard.fab_container, theme.scheme().primary_container);
        assert_eq!(standard.fab_content, theme.scheme().on_primary_container);

        let vibrant = ToolbarColors::resolve(Some(&theme), ToolbarColorStyle::Vibrant);
        assert_eq!(vibrant.container, theme.scheme().primary_container);
        assert_eq!(vibrant.content, theme.scheme().on_primary_container);
        assert_eq!(vibrant.fab_container, theme.scheme().tertiary_container);
        assert_eq!(vibrant.fab_content, theme.scheme().on_tertiary_container);

        // Unthemed, the same four roles resolve to their baseline literals.
        let unthemed = ToolbarColors::resolve(None, ToolbarColorStyle::Vibrant);
        assert_eq!(unthemed.container, PRIMARY_CONTAINER);
        assert_eq!(unthemed.fab_container, TERTIARY_CONTAINER);

        let view: ToolbarView<()> = docked_toolbar().color_style(ToolbarColorStyle::Vibrant);
        let mut w = build(&view);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(300.0, 100.0)));
        let mut scene = RRectRecorder::default();
        let mut pctx = PaintCtx::new(Point::ZERO, size).with_theme(&theme);
        w.paint(&mut pctx, &mut scene);
        assert_eq!(scene.rrects[0].3, theme.scheme().primary_container);
    }

    #[test]
    fn the_elevation_pair_follows_the_reference_rule_with_an_explicit_override() {
        // No fab: `elevationNone`, i.e. no shadow at all.
        let plain: ToolbarView<()> = floating_toolbar();
        assert_eq!(plain.resolved_elevation(), ToolbarElevation::None);
        assert!(resolve_shadow(None, ToolbarElevation::None).is_none());

        // With a fab: `elevationWithFabExpanded`, i.e. M3 level 1.
        let with_fab: ToolbarView<()> = floating_toolbar().fab(leaf_any(40.0, 40.0));
        assert_eq!(with_fab.resolved_elevation(), ToolbarElevation::Level1);
        let (blur, y, _) =
            resolve_shadow(None, ToolbarElevation::Level1).expect("a level-1 shadow");
        assert_eq!((blur, y), (LEVEL1_SHADOW_BLUR, LEVEL1_SHADOW_Y_OFFSET));

        // The explicit override wins over both.
        let pinned: ToolbarView<()> = floating_toolbar()
            .fab(leaf_any(40.0, 40.0))
            .elevation(ToolbarElevation::Level2);
        assert_eq!(pinned.resolved_elevation(), ToolbarElevation::Level2);
        let (blur, y, _) =
            resolve_shadow(None, ToolbarElevation::Level2).expect("a level-2 shadow");
        assert_eq!((blur, y), (LEVEL2_SHADOW_BLUR, LEVEL2_SHADOW_Y_OFFSET));

        let theme = crate::baseline();
        let (blur, y, _) =
            resolve_shadow(Some(&theme), ToolbarElevation::Level1).expect("a themed shadow");
        let spec = theme.elevation.level1.shadow(theme.brightness);
        assert_eq!((blur, y), (spec.blur_std_dev, spec.y_offset));
    }

    #[test]
    fn a_revealing_pill_is_clipped_and_faded_while_the_fab_stays_opaque() {
        let view: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0))
            .expanded(false);
        let mut w = build(&view);
        layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));

        let mut scene = RRectRecorder::default();
        let mut pctx = PaintCtx::new(Point::ZERO, Size::new(200.0, TOOLBAR_FAB_MEDIUM));
        w.paint(&mut pctx, &mut scene);
        assert_eq!(scene.clips, 1, "the pill paints inside a reveal clip");
        assert_eq!(
            scene.layers,
            vec![0.0],
            "collapsed, the pill is fully faded out"
        );

        // The fade only starts halfway through the ramp — upstream's own
        // `Interval(0.5, 1, easeIn)`.
        assert_eq!(ToolbarMorph::reveal_alpha(0.0), 0.0);
        assert_eq!(ToolbarMorph::reveal_alpha(0.5), 0.0);
        assert_eq!(ToolbarMorph::reveal_alpha(1.0), 1.0);
        assert!(ToolbarMorph::reveal_alpha(0.75) > 0.0);
    }

    // ---- Semantics + rebuild ----------------------------------------------

    #[test]
    fn semantics_node_is_toolbar_forwarding_every_slot() {
        // A leaf without its own `semantics()` override (e.g. `leaf_any`)
        // contributes no node — use `Text` children instead, so the
        // "forwards every slot" assertion is meaningful.
        fn logic(_state: &mut ()) -> ToolbarView<()> {
            docked_toolbar()
                .semantic_label("Editing")
                .leading(vec![frust::authoring::any::<(), _>(frust::text("Leading"))])
                .trailing(vec![frust::authoring::any::<(), _>(frust::text(
                    "Trailing",
                ))])
        }
        let mut root: frust_core::RenderRoot<(), ToolbarView<()>> = frust_core::RenderRoot::new();
        let mut state = ();
        root.rebuild(&mut logic, &mut state);
        let mut tcx = frust::authoring::text::TextContext::new();
        root.layout_with_text(
            Size::new(300.0, TOOLBAR_HEIGHT),
            &mut tcx as &mut dyn std::any::Any,
        );
        let update = root.semantics();
        let (_, node) = update
            .nodes
            .iter()
            .find(|(_, n)| n.role() == Role::Toolbar)
            .expect("a Toolbar node is contributed");
        assert_eq!(node.children().len(), 2, "leading + trailing forwarded");
        assert_eq!(node.label(), Some("Editing"));
    }

    #[test]
    fn rebuild_adopts_new_slot_counts() {
        let mut counter = 0u64;
        let prev: ToolbarView<()> = floating_toolbar().leading(vec![leaf_any(24.0, 24.0)]);
        let mut w = View::<()>::build(&prev, &mut ctx(&mut counter));
        assert_eq!(w.leading_count, 1);

        let next: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .trailing(vec![leaf_any(24.0, 24.0), leaf_any(24.0, 24.0)]);
        let flags = View::<()>::rebuild(&next, &prev, &mut w, &mut ctx(&mut counter));
        assert!(flags.needs_layout());
        assert_eq!(w.trailing_count, 2);
        assert_eq!(w.slots.len(), 3);
    }

    #[test]
    fn attaching_a_fab_switches_a_floating_bar_onto_the_adjacent_layout() {
        let mut counter = 0u64;
        let prev: ToolbarView<()> = floating_toolbar().leading(vec![leaf_any(24.0, 24.0)]);
        let mut w = View::<()>::build(&prev, &mut ctx(&mut counter));
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));
        assert_eq!(size.height, TOOLBAR_HEIGHT);

        let next: ToolbarView<()> = floating_toolbar()
            .leading(vec![leaf_any(24.0, 24.0)])
            .fab(leaf_any(40.0, 40.0));
        let flags = View::<()>::rebuild(&next, &prev, &mut w, &mut ctx(&mut counter));
        assert!(flags.needs_layout());
        assert!(w.adjacent_fab);
        let size = layout(&mut w, &BoxConstraints::loose(Size::new(800.0, 200.0)));
        assert_eq!(size.height, TOOLBAR_FAB_MEDIUM);
    }
}