frust-material 0.6.1

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
//! The M3 Expressive **carousel**: a snapping row (or column) of items whose
//! extents morph as they cross the viewport, in the three reference layouts —
//! `hero`, `contained`, and `uncontained`.
//!
//! Ported from material_3_expressive v1.0.8 (MIT, © 2026 Paa Developments),
//! `lib/components/carousel/` — `m3e_carousel.dart`, `components/
//! m3e_carousel_view.dart`, `m3e_carousel_wrapper.dart`,
//! `m3e_sliver_weighted_carousel_layout.dart`,
//! `m3e_sliver_fixed_extent_carousel.dart`, `m3e_carousel_scroll_physics.dart`,
//! `m3e_carousel_position.dart`, `utils/m3e_carousel_scroll_helper.dart`,
//! `styles/m3e_carousel_theme.dart`, `enums/m3e_carousel_type.dart` (retrieved
//! 2026-08-19). Those files are themselves vendored from `m3_carousel` (MIT,
//! © 2024 Paa) and derived from Flutter's `CarouselView` — **BSD-3-Clause,
//! © 2014 The Flutter Authors**; both notices are in `plugins/material/NOTICE`.
//! [`layout`] and [`physics`] carry the same header over the halves of the port
//! they own.
//!
//! # The three layouts, and the weights behind them
//!
//! Every layout but `uncontained` is the same weighted solver with a different
//! weight list — `M3ECarousel._applyLayoutWeights`' own table:
//!
//! | Layout | Weights | Shape |
//! |---|---|---|
//! | [`CarouselLayout::Hero`] + [`HeroAlignment::Start`] | `[8, 2]` | one large item, one small peek after it |
//! | [`CarouselLayout::Hero`] + [`HeroAlignment::Center`] | `[2, 6, 2]` | a peek, the focal item, a peek |
//! | [`CarouselLayout::Hero`] + [`HeroAlignment::End`] | `[2, 8]` | one small peek, then the large item |
//! | [`CarouselLayout::Contained`] | `[5, 4, 1]` | large / medium / small, all inside the bounds |
//! | [`CarouselLayout::Contained`] + [`CarouselView::extended`] | `[4, 3, 2, 1]` | four descending items |
//! | [`CarouselLayout::Uncontained`] | — | uniform [`CarouselView::item_extent`] items, scrolling to the edge |
//!
//! A weight list is the *visible window*: each weight's share of the viewport is
//! one slot, and an item **morphs between adjacent slots** as the leading item
//! scrolls off — the signature M3E behavior, pinned by [`layout`]'s tests at
//! several viewport widths and scroll offsets.
//!
//! # Items are laid out once, at the largest slot, and clipped
//!
//! A slot's extent changes continuously during a scroll, but re-laying every
//! child out per frame would make a drag a relayout storm. Upstream solves this
//! in `M3ECarouselWrapper` — "item content is laid out at the largest slot size
//! and clipped to the current item bounds; scrolling only moves that clip
//! window" — and this port takes the same route: every child is laid out at
//! [`WeightedLayout::max_child_extent`](layout::WeightedLayout::max_child_extent)
//! (or the fixed item extent) once per `layout`, and `paint` clips it to its
//! current slot with a **rounded** clip, centring the content inside. The
//! rounded clip is load-bearing, not decoration: baseline `Clip` does not
//! visually clip, so `PaintScene::push_clip_rounded` is what actually shapes an
//! item.
//!
//! # Snapping, and the velocity that picks the target
//!
//! A press captures the pointer and forwards to the item under it; crossing
//! [`frust::input::TOUCH_SLOP`] takes the gesture over (cancelling the item's
//! own press) and turns it into a scroll. The release picks a target through
//! [`physics::snap_offset`] — the nearest item boundary, or one further along
//! when the release velocity clears [`physics::SNAP_VELOCITY_TOLERANCE`] — and
//! a [`SETTLE_DURATION`](physics::SETTLE_DURATION) ramp takes the content
//! there, advanced from the shell frame clock in `paint` like every other
//! motion in this crate. `Theme.motion.reduce_motion` jumps to the target
//! instead and asks for no further frames.
//!
//! Velocity comes from a [`frust::input::VelocityTracker`] fed with the last
//! *painted* frame time, since the event pass carries no clock of its own —
//! the same seam baseline `ScrollView` uses, and the reason the event body is
//! parameterised on an explicit timestamp ([`CarouselWidget::event_at`]).
//!
//! A mouse wheel over the carousel moves it and settles the same way. Upstream
//! has no wheel path at all (its carousel is swipe-driven under
//! `NeverScrollableScrollPhysics`); this is the port's desktop completion of
//! it.
//!
//! # Controlled and uncontrolled
//!
//! Uncontrolled by default: a drag or a settle moves the carousel's own index
//! and reports it through [`CarouselView::on_change`].
//! [`CarouselView::selected`] flips it to this catalog's controlled contract —
//! the carousel then reports the *requested* index and moves nothing until the
//! next `rebuild` feeds a confirmed one back down. Both that and
//! [`CarouselView::initial_item`] name the **focal** item (the one in the first
//! max-weight slot), which is what upstream's `M3ECarouselController.initialItem`
//! means for a weighted carousel.
//!
//! # Deliberately not ported
//!
//! * **Free scroll** (`M3ECarousel.freeScroll`) — upstream's own default is
//!   off, and with it on the widget swaps in a `ScrollSpringSimulation` plus
//!   Flutter's ballistic scroll machinery. Snapping is the only mode here.
//! * **Infinite looping** (`infinite`) — needs the sliver's cycle-length
//!   pixel correction; a finite item list is the whole surface here.
//! * **Lazy item building** (`M3ECarouselView.builder`) — this view takes a
//!   finite list of views like every other container in this catalog.
//! * **The tap pulse** (`M3ECarouselWrapper`'s `fixedPulseDelta`) — it animates
//!   a per-item clip window against content laid out with a pulse budget on
//!   top of the max slot. A pressed item takes this crate's shared
//!   [`state_layer`](crate::state_layer) overlay instead, which is the M3
//!   interaction affordance the rest of the catalog uses.

pub(crate) mod layout;
pub(crate) mod physics;

use std::rc::Rc;

use frust::authoring::{
    AnyView, BoxConstraints, BuildCtx, ChangeFlags, ChildPod, ErasedArgCallback, EventCtx,
    EventResult, InputEvent, LayoutCtx, PaintCtx, PaintScene, PointerButton, PointerEvent,
    PointerPhase, Role, ScrollDelta, SemanticsCtx, TypedArgCallback, View, ViewSeq, Widget,
};
use frust::input::{TOUCH_SLOP, VelocityTracker, WHEEL_LINE_PX};
use frust::{AnimationController, FrameTime, Theme};
use kurbo::{Point, Rect, Size};
use peniko::Color;

use self::layout::{
    CONTAINED_EXTENDED_WEIGHTS, CONTAINED_WEIGHTS, FixedLayout, HERO_CENTER_WEIGHTS,
    HERO_END_WEIGHTS, HERO_START_WEIGHTS, Slot, WeightedLayout,
};
use crate::press::presses;
use crate::state_layer::StateLayer;

/// Space around each item, in logical px (`M3ECarouselTheme.itemPadding`,
/// `EdgeInsets.all(4)`) — inset from the item's own slot, so adjacent items
/// show an 8px gutter between them.
pub const CAROUSEL_ITEM_PADDING: f64 = 4.0;

/// Unthemed-fallback item corner radius, in logical px
/// (`M3ECarouselTheme.defaultBorderRadiusValue`). A theme resolves this from
/// `shape.extra_large`, which carries the same 28dp value.
pub const CAROUSEL_ITEM_RADIUS: f64 = 28.0;

/// Default main-axis extent of an [`CarouselLayout::Uncontained`] item, in
/// logical px (`M3ECarouselTheme.defaultUncontainedItemExtent`).
pub const UNCONTAINED_ITEM_EXTENT: f64 = 270.0;

/// Default shrink floor for an [`CarouselLayout::Uncontained`] carousel, in
/// logical px (`M3ECarouselTheme.defaultUncontainedShrinkExtent`) — how small
/// an item may get at the viewport edge before it slides off instead.
pub const UNCONTAINED_SHRINK_EXTENT: f64 = 150.0;

/// Offset differences below which the content counts as settled, in logical px.
const OFFSET_EPSILON: f64 = 1e-3;

/// Unthemed-fallback state-layer content color for a pressed item (a theme
/// resolves this from `colors.on_surface`).
const ON_SURFACE: Color = Color::from_rgb8(0x1D, 0x1B, 0x20);

/// The carousel's structural layout (`M3ECarouselType`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CarouselLayout {
    /// One large, prominent item with small peeking neighbours — the
    /// reference's own default. Positioned by [`HeroAlignment`].
    #[default]
    Hero,
    /// Large / medium / small items, all kept inside the carousel's bounds.
    /// [`CarouselView::extended`] adds a fourth, smaller step.
    Contained,
    /// Uniform items that scroll to the edge of the container, sized by
    /// [`CarouselView::item_extent`].
    Uncontained,
}

/// Where a [`CarouselLayout::Hero`] carousel puts its focal item
/// (`M3ECarouselHeroAlignment`).
///
/// Named for the scroll axis rather than the screen: `Start` is the left edge
/// of a horizontal carousel and the top of a vertical one (upstream's `left`),
/// `End` the right/bottom (upstream's `right`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum HeroAlignment {
    /// Focal item at the leading edge, one small peek after it (`[8, 2]`).
    Start,
    /// Focal item between two small peeks (`[2, 6, 2]`) — the reference default.
    #[default]
    Center,
    /// One small peek, then the focal item at the trailing edge (`[2, 8]`).
    End,
}

/// The axis a carousel scrolls along (`M3ECarousel.axis`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CarouselAxis {
    /// Items flow along the x axis.
    #[default]
    Horizontal,
    /// Items flow along the y axis.
    Vertical,
}

/// What a carousel reports when the item under the layout changes
/// (`M3ECarouselChangeDetails`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CarouselChange {
    /// The first visible layout slot's item.
    pub leading_index: usize,
    /// The item in the largest (focal) slot — equal to
    /// [`leading_index`](Self::leading_index) for an uncontained carousel,
    /// which has no larger slot to be focal in.
    pub focal_index: usize,
    /// How many items the carousel holds.
    pub item_count: usize,
}

impl CarouselChange {
    /// Whether `index` is the current focal (largest) item.
    pub fn is_focal(&self, index: usize) -> bool {
        index == self.focal_index
    }
}

/// A declarative M3E carousel. See the [module docs](self).
pub struct CarouselView<State: 'static> {
    items: Vec<AnyView<State>>,
    layout: CarouselLayout,
    hero_alignment: HeroAlignment,
    extended: bool,
    axis: CarouselAxis,
    item_extent: f64,
    shrink_extent: Option<f64>,
    item_radius: Option<f64>,
    width: Option<f64>,
    height: Option<f64>,
    consume_max_weight: bool,
    initial_item: usize,
    selected: Option<usize>,
    on_change: Option<TypedArgCallback<State, CarouselChange>>,
    on_tap: Option<TypedArgCallback<State, usize>>,
}

/// A hero carousel over `items` — the reference's own default layout, focal
/// item centred. See the [module docs](self).
pub fn carousel<State: 'static, M>(items: impl ViewSeq<State, M>) -> CarouselView<State> {
    let mut erased = Vec::new();
    items.extend_views(&mut erased);
    CarouselView {
        items: erased,
        layout: CarouselLayout::default(),
        hero_alignment: HeroAlignment::default(),
        extended: false,
        axis: CarouselAxis::default(),
        item_extent: UNCONTAINED_ITEM_EXTENT,
        shrink_extent: None,
        item_radius: None,
        width: None,
        height: None,
        consume_max_weight: false,
        initial_item: 0,
        selected: None,
        on_change: None,
        on_tap: None,
    }
}

/// A [`CarouselLayout::Hero`] carousel over `items` — one large item plus
/// peeks, placed by `alignment`.
pub fn hero_carousel<State: 'static, M>(
    items: impl ViewSeq<State, M>,
    alignment: HeroAlignment,
) -> CarouselView<State> {
    carousel(items).hero_alignment(alignment)
}

/// A [`CarouselLayout::Contained`] carousel over `items` — large/medium/small
/// items kept inside the bounds.
pub fn contained_carousel<State: 'static, M>(items: impl ViewSeq<State, M>) -> CarouselView<State> {
    carousel(items).layout(CarouselLayout::Contained)
}

/// A [`CarouselLayout::Uncontained`] carousel over `items` — uniform
/// `item_extent`-wide items scrolling to the container's edge.
pub fn uncontained_carousel<State: 'static, M>(
    items: impl ViewSeq<State, M>,
    item_extent: f64,
) -> CarouselView<State> {
    carousel(items)
        .layout(CarouselLayout::Uncontained)
        .item_extent(item_extent)
}

impl<State: 'static> CarouselView<State> {
    /// Set the structural layout (`type`).
    pub fn layout(mut self, layout: CarouselLayout) -> Self {
        self.layout = layout;
        self
    }

    /// Where a [`CarouselLayout::Hero`] carousel's focal item sits. Ignored by
    /// the other two layouts.
    pub fn hero_alignment(mut self, alignment: HeroAlignment) -> Self {
        self.hero_alignment = alignment;
        self
    }

    /// Show four descending items instead of three (`isExtended`). Applies to
    /// [`CarouselLayout::Contained`] only.
    pub fn extended(mut self, extended: bool) -> Self {
        self.extended = extended;
        self
    }

    /// Set the scroll axis (default [`CarouselAxis::Horizontal`]).
    pub fn axis(mut self, axis: CarouselAxis) -> Self {
        self.axis = axis;
        self
    }

    /// Main-axis extent of an [`CarouselLayout::Uncontained`] item, in logical
    /// px (default [`UNCONTAINED_ITEM_EXTENT`]). Capped at the viewport by the
    /// solver; ignored by the weighted layouts, which derive every extent from
    /// the viewport instead.
    pub fn item_extent(mut self, extent: f64) -> Self {
        self.item_extent = extent;
        self
    }

    /// How small an item may shrink at the viewport edge before it slides off
    /// instead, in logical px (`shrinkExtent`). Defaults to `0.0` for the
    /// weighted layouts and [`UNCONTAINED_SHRINK_EXTENT`] for
    /// [`CarouselLayout::Uncontained`], matching the reference's own two
    /// defaults. Never exceeds the smallest slot.
    pub fn shrink_extent(mut self, extent: f64) -> Self {
        self.shrink_extent = Some(extent);
        self
    }

    /// Item corner radius, in logical px (`childElementBorderRadius`).
    /// Unset resolves `shape.extra_large` from the theme, then
    /// [`CAROUSEL_ITEM_RADIUS`].
    pub fn item_radius(mut self, radius: f64) -> Self {
        self.item_radius = Some(radius);
        self
    }

    /// Pin the carousel's own width, in logical px (`M3ECarousel.width`).
    /// Unset takes the incoming constraint.
    pub fn width(mut self, width: f64) -> Self {
        self.width = Some(width);
        self
    }

    /// Pin the carousel's own height, in logical px (`M3ECarousel.height`).
    /// Unset takes the incoming constraint.
    pub fn height(mut self, height: f64) -> Self {
        self.height = Some(height);
        self
    }

    /// Let the leading items expand into the largest slot
    /// (`consumeMaxWeight`), leaving a phantom gap before item 0 while they do.
    ///
    /// Off by default, which is the choice upstream's own top-level
    /// `M3ECarousel` makes: with it on, a theme rebuild can strand the extra
    /// leading extent offstage.
    pub fn consume_max_weight(mut self, consume: bool) -> Self {
        self.consume_max_weight = consume;
        self
    }

    /// The **focal** item to start on (`M3ECarouselController.initialItem`).
    /// Only read on the first build; use [`selected`](Self::selected) to drive
    /// the carousel afterwards.
    pub fn initial_item(mut self, index: usize) -> Self {
        self.initial_item = index;
        self
    }

    /// Drive the carousel from the app: it shows exactly this **focal** item
    /// and reports every requested index through
    /// [`on_change`](Self::on_change) instead of moving itself (see the
    /// [module docs](self)).
    pub fn selected(mut self, focal_index: usize) -> Self {
        self.selected = Some(focal_index);
        self
    }

    /// Report the leading/focal item whenever either changes (`onChange`).
    pub fn on_change<F: Fn(&mut State, CarouselChange) + 'static>(mut self, on_change: F) -> Self {
        self.on_change = Some(Rc::new(on_change));
        self
    }

    /// Report a tap on an item, by index (`onTap`).
    pub fn on_tap<F: Fn(&mut State, usize) + 'static>(mut self, on_tap: F) -> Self {
        self.on_tap = Some(Rc::new(on_tap));
        self
    }

    /// The item-sizing rule this view's layout knobs resolve to.
    fn sizing(&self) -> Sizing {
        match self.layout {
            CarouselLayout::Hero => Sizing::Weighted(match self.hero_alignment {
                HeroAlignment::Start => HERO_START_WEIGHTS,
                HeroAlignment::Center => HERO_CENTER_WEIGHTS,
                HeroAlignment::End => HERO_END_WEIGHTS,
            }),
            CarouselLayout::Contained => Sizing::Weighted(if self.extended {
                CONTAINED_EXTENDED_WEIGHTS
            } else {
                CONTAINED_WEIGHTS
            }),
            CarouselLayout::Uncontained => Sizing::Fixed {
                item_extent: self.item_extent,
            },
        }
    }

    /// The shrink floor, defaulted per layout (see
    /// [`shrink_extent`](Self::shrink_extent)).
    fn resolved_shrink_extent(&self) -> f64 {
        self.shrink_extent.unwrap_or(match self.layout {
            CarouselLayout::Uncontained => UNCONTAINED_SHRINK_EXTENT,
            _ => 0.0,
        })
    }
}

impl<State: 'static> View<State> for CarouselView<State> {
    type Element = CarouselWidget;

    fn build(&self, ctx: &mut BuildCtx<'_>) -> CarouselWidget {
        let sizing = self.sizing();
        let mut widget = CarouselWidget {
            pods: self
                .items
                .iter()
                .map(|item| frust::authoring::build_child(item, ctx))
                .collect(),
            sizing,
            axis: self.axis,
            shrink_extent: self.resolved_shrink_extent(),
            item_radius: self.item_radius,
            width: self.width,
            height: self.height,
            consume_max_weight: self.consume_max_weight,
            controlled: self.selected.is_some(),
            index: 0,
            offset: 0.0,
            settle: physics::settle_controller(),
            settle_from: 0.0,
            settle_to: 0.0,
            main: 0.0,
            cross: 0.0,
            slots: Vec::new(),
            drag: None,
            pressed: None,
            state: StateLayer::new(),
            tracker: VelocityTracker::new(),
            last_frame_time: FrameTime::ZERO,
            reported: (0, 0),
            on_change: self
                .on_change
                .as_ref()
                .map(frust::authoring::erase_callback_arg),
            on_tap: self
                .on_tap
                .as_ref()
                .map(frust::authoring::erase_callback_arg),
        };
        widget.index = widget.leading_for_focal(self.selected.unwrap_or(self.initial_item));
        widget.reported = widget.indices_for_leading(widget.index);
        widget
    }

    fn rebuild(
        &self,
        prev: &Self,
        element: &mut CarouselWidget,
        ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        let mut flags = frust::authoring::rebuild_children(
            &prev.items,
            &self.items,
            &mut element.pods,
            ctx,
            |view: &AnyView<State>| view,
            |_| None,
        );
        let sizing = self.sizing();
        let shrink_extent = self.resolved_shrink_extent();
        if element.sizing != sizing
            || element.axis != self.axis
            || element.shrink_extent != shrink_extent
            || element.width != self.width
            || element.height != self.height
            || element.consume_max_weight != self.consume_max_weight
        {
            element.sizing = sizing;
            element.axis = self.axis;
            element.shrink_extent = shrink_extent;
            element.width = self.width;
            element.height = self.height;
            element.consume_max_weight = self.consume_max_weight;
            // A different weight list re-slots every item: the carousel returns
            // to its own index rather than keeping a now-meaningless offset,
            // exactly as `M3ECarousel.didUpdateWidget` jumps back to 0.
            element.settle.stop();
            element.offset = element.offset_for(element.index);
            flags |= ChangeFlags::LAYOUT | ChangeFlags::PAINT;
        }
        if element.item_radius != self.item_radius {
            element.item_radius = self.item_radius;
            flags |= ChangeFlags::PAINT;
        }
        element.controlled = self.selected.is_some();
        let last = element.pods.len().saturating_sub(1);
        if let Some(selected) = self.selected {
            // Controlled: the app is the source of truth, so adopt the
            // confirmed focal item and settle onto it.
            let index = element.leading_for_focal(selected);
            if element.index != index {
                element.index = index;
                element.reported = element.indices_for_leading(index);
                element.start_settle();
                flags |= ChangeFlags::PAINT;
            }
        } else if element.index > last {
            // A shrunk item list cannot leave the index past the end.
            element.index = last;
            element.reported = element.indices_for_leading(last);
            element.start_settle();
            flags |= ChangeFlags::PAINT;
        }
        element.on_change = self
            .on_change
            .as_ref()
            .map(frust::authoring::erase_callback_arg);
        element.on_tap = self
            .on_tap
            .as_ref()
            .map(frust::authoring::erase_callback_arg);
        flags
    }

    fn teardown(&self, element: &mut CarouselWidget, ctx: &mut BuildCtx<'_>) {
        for (view, pod) in self.items.iter().zip(element.pods.iter_mut()) {
            frust::authoring::teardown_child(view, pod, ctx);
        }
    }
}

/// How a carousel sizes its items: by weight share of the viewport, or at one
/// fixed extent.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Sizing {
    /// The weighted layouts (hero, contained) and their weight list.
    Weighted(&'static [u16]),
    /// The uncontained layout and its per-item main-axis extent.
    Fixed { item_extent: f64 },
}

/// One of the two solvers in [`layout`], resolved against a live viewport and
/// scroll offset.
enum Solver {
    Weighted(WeightedLayout<'static>),
    Fixed(FixedLayout),
}

impl Solver {
    /// Every visible item's viewport-space geometry, in index order.
    fn slots(&self, item_count: usize) -> Vec<Slot> {
        match self {
            Solver::Weighted(l) => l.slots(item_count),
            Solver::Fixed(l) => l.slots(item_count),
        }
    }

    /// The scroll distance one item step moves the content.
    fn stride(&self) -> f64 {
        match self {
            Solver::Weighted(l) => l.first_child_extent(),
            Solver::Fixed(l) => l.stride(),
        }
    }

    /// The largest extent an item can occupy — the size every child is laid
    /// out at (see the [module docs](self)).
    fn slot_extent(&self) -> f64 {
        match self {
            Solver::Weighted(l) => l.max_child_extent(),
            Solver::Fixed(l) => l.stride(),
        }
    }

    /// The furthest the content may scroll.
    fn max_scroll_offset(&self, item_count: usize) -> f64 {
        match self {
            Solver::Weighted(l) => l.max_scroll_offset(item_count),
            Solver::Fixed(l) => l.max_scroll_offset(item_count),
        }
    }
}

/// A live drag on the carousel.
struct Drag {
    /// Main-axis coordinate of the `Down`.
    start: f64,
    /// The scroll offset the drag started from.
    from: f64,
    /// The item the `Down` landed on, if any.
    item: Option<usize>,
    /// Whether the gesture has crossed the slop and become a scroll — until it
    /// does, every move still belongs to the item under the pointer.
    scrolling: bool,
}

/// The retained widget for a [`CarouselView`]. See the [module docs](self).
pub struct CarouselWidget {
    pods: Vec<ChildPod>,
    sizing: Sizing,
    axis: CarouselAxis,
    shrink_extent: f64,
    item_radius: Option<f64>,
    width: Option<f64>,
    height: Option<f64>,
    consume_max_weight: bool,
    /// Whether the app drives the index (see the [module docs](self)).
    controlled: bool,
    /// The **slot item** the carousel rests on (or is settling onto): the one
    /// whose leading edge a resting offset sits at, so `offset == index *
    /// stride`. Not always the item *reported* as leading — see
    /// [`CarouselWidget::indices_for_leading`].
    index: usize,
    /// The current scroll offset, in logical px.
    offset: f64,
    settle: AnimationController,
    /// The offset the running settle started from, and the one it ends at.
    settle_from: f64,
    settle_to: f64,
    /// Main/cross extents resolved at layout.
    main: f64,
    cross: f64,
    /// The visible items' geometry, recomputed whenever the offset moves.
    slots: Vec<Slot>,
    drag: Option<Drag>,
    /// The item a `Down` armed, cleared on `Up`/`Cancel` or a slop takeover.
    pressed: Option<usize>,
    state: StateLayer,
    tracker: VelocityTracker,
    /// The most recent frame time seen at paint, reused as the event pass's
    /// timestamp for velocity tracking (the event pass carries no clock).
    last_frame_time: FrameTime,
    /// The last `(leading, focal)` pair reported through `on_change`.
    reported: (usize, usize),
    on_change: Option<ErasedArgCallback<CarouselChange>>,
    on_tap: Option<ErasedArgCallback<usize>>,
}

impl CarouselWidget {
    /// The item in the first visible slot at rest — the
    /// [`CarouselChange::leading_index`] the carousel would report.
    pub fn leading_index(&self) -> usize {
        self.indices_for_leading(self.index).0
    }

    /// The item in the largest (focal) slot at rest.
    pub fn focal_index(&self) -> usize {
        self.indices_for_leading(self.index).1
    }

    /// The current scroll offset, in logical px.
    pub fn offset(&self) -> f64 {
        self.offset
    }

    /// The solver for this carousel at `offset`.
    fn solver(&self, offset: f64) -> Solver {
        match self.sizing {
            Sizing::Weighted(weights) => Solver::Weighted(WeightedLayout::new(
                weights,
                self.main,
                self.shrink_extent,
                offset,
                self.consume_max_weight,
            )),
            Sizing::Fixed { item_extent } => Solver::Fixed(FixedLayout::new(
                item_extent,
                self.shrink_extent,
                self.main,
                offset,
            )),
        }
    }

    /// The index of the first maximum weight in the active weight list — the
    /// distance from the leading item to the focal one. `0` for the
    /// uncontained layout, which has no focal slot.
    fn focal_slot(&self) -> usize {
        match self.sizing {
            Sizing::Weighted(weights) => {
                let max = weights.iter().copied().max().unwrap_or(0);
                weights.iter().position(|w| *w == max).unwrap_or(0)
            }
            Sizing::Fixed { .. } => 0,
        }
    }

    /// The slot item that puts `focal` in the focal slot — the inverse of
    /// [`Self::indices_for_leading`], and upstream's `_getInitialLeadingItem`.
    fn leading_for_focal(&self, focal: usize) -> usize {
        let last = self.pods.len().saturating_sub(1);
        let raw = if self.consume_max_weight {
            focal
        } else {
            focal.saturating_sub(self.focal_slot())
        };
        raw.min(last)
    }

    /// The `(leading, focal)` pair a **slot item** reads as — the item index
    /// `offset / stride` names, which is not the reported leading item under
    /// `consume_max_weight` (the leading slots then hold items *before* it).
    ///
    /// Upstream computes the focal item from the already-shifted leading index
    /// (`_focalIndexForLeading(_reportedLeadingIndex(..))`), which double-counts
    /// the shift for the first `focal_slot` items; the focal item is derived
    /// from the unshifted slot item here instead, so item 0 reads as focal when
    /// it is the one actually occupying the largest slot.
    fn indices_for_leading(&self, raw: usize) -> (usize, usize) {
        let last = self.pods.len().saturating_sub(1);
        let raw = raw.min(last);
        if self.consume_max_weight {
            (raw.saturating_sub(self.focal_slot()), raw)
        } else {
            (raw, (raw + self.focal_slot()).min(last))
        }
    }

    /// The `(leading, focal)` pair a scroll offset reads as — upstream's
    /// `_reportedLeadingIndex`, which rounds so both swipe directions report at
    /// the halfway mark.
    fn indices_at(&self, offset: f64) -> (usize, usize) {
        let raw = physics::nearest_item(offset, self.solver(offset).stride());
        self.indices_for_leading(raw)
    }

    /// The scroll offset that rests on slot item `index`.
    fn offset_for(&self, index: usize) -> f64 {
        let solver = self.solver(self.offset);
        let target = index as f64 * solver.stride();
        // Ordered explicitly rather than `clamp`: `max_scroll_offset` is
        // computed and floored to `0.0` here (`layout`'s clamp discipline).
        target
            .max(0.0)
            .min(solver.max_scroll_offset(self.pods.len()).max(0.0))
    }

    /// The furthest the content may scroll at the current viewport.
    fn max_offset(&self) -> f64 {
        self.solver(self.offset)
            .max_scroll_offset(self.pods.len())
            .max(0.0)
    }

    /// Whether there is anything to scroll at all.
    fn scrollable(&self) -> bool {
        self.max_offset() > 0.0
    }

    /// Set the offset, bounded to the scrollable range, and re-slot.
    fn set_offset(&mut self, offset: f64) {
        let offset = if offset.is_finite() { offset } else { 0.0 };
        // Ordered explicitly rather than `clamp` (see `offset_for`).
        self.offset = offset.max(0.0).min(self.max_offset());
        self.sync_geometry();
    }

    /// Recompute the visible slots for the current offset and place every pod.
    ///
    /// Called from `layout`, from the drag arm (so a drag moves the content
    /// without a relayout) and from `paint` while settling.
    fn sync_geometry(&mut self) {
        self.slots = self.solver(self.offset).slots(self.pods.len());
        let content = self.content_size();
        let first_visible = self.slots.first().map(|s| s.index);
        for (index, pod) in self.pods.iter_mut().enumerate() {
            let origin = match self.slots.iter().find(|s| s.index == index) {
                Some(slot) => {
                    // The child is laid out at the largest slot and centred in
                    // the current one, so only the clip window moves (module
                    // docs). Half a slot of it can overhang either edge.
                    let along =
                        slot.offset + (slot.extent - content_main(self.axis, content)) / 2.0;
                    let across = (self.cross - content_cross(self.axis, content)) / 2.0;
                    axis_point(self.axis, along, across)
                }
                None => {
                    // Not visible: parked wholly outside the carousel's own
                    // bounds so no pointer inside it can ever hit the pod
                    // (`route_event` hit-tests by pod bounds).
                    let along = if first_visible.is_some_and(|first| index < first) {
                        -content_main(self.axis, content) - 1.0
                    } else {
                        self.main + 1.0
                    };
                    axis_point(self.axis, along, 0.0)
                }
            };
            pod.set_origin(origin);
        }
    }

    /// The size every child is laid out at: the largest slot, inset by the item
    /// padding on all four sides.
    fn content_size(&self) -> Size {
        let slot = self.solver(self.offset).slot_extent();
        let main = (slot - CAROUSEL_ITEM_PADDING * 2.0).max(0.0);
        let cross = (self.cross - CAROUSEL_ITEM_PADDING * 2.0).max(0.0);
        match self.axis {
            CarouselAxis::Horizontal => Size::new(main, cross),
            CarouselAxis::Vertical => Size::new(cross, main),
        }
    }

    /// A visible item's clip window in widget-local coordinates: its slot,
    /// inset by the item padding.
    fn item_rect(&self, slot: &Slot) -> Rect {
        let main = (slot.extent - CAROUSEL_ITEM_PADDING * 2.0).max(0.0);
        let cross = (self.cross - CAROUSEL_ITEM_PADDING * 2.0).max(0.0);
        let origin = axis_point(
            self.axis,
            slot.offset + CAROUSEL_ITEM_PADDING,
            CAROUSEL_ITEM_PADDING,
        );
        let size = match self.axis {
            CarouselAxis::Horizontal => Size::new(main, cross),
            CarouselAxis::Vertical => Size::new(cross, main),
        };
        Rect::from_origin_size(origin, size)
    }

    /// The item whose clip window contains widget-local `pos`, if any.
    fn item_at(&self, pos: Point) -> Option<usize> {
        self.slots
            .iter()
            .find(|slot| self.item_rect(slot).contains(pos))
            .map(|slot| slot.index)
    }

    /// A position's main-axis component under this carousel's axis.
    fn main_of(&self, point: Point) -> f64 {
        match self.axis {
            CarouselAxis::Horizontal => point.x,
            CarouselAxis::Vertical => point.y,
        }
    }

    /// The last painted frame time in milliseconds — the event pass's
    /// timestamp source for velocity tracking (see [`Self::last_frame_time`]).
    fn event_time_ms(&self) -> f64 {
        self.last_frame_time.as_secs_f64() * 1000.0
    }

    /// Start settling from wherever the content is onto `target`.
    fn settle_to(&mut self, target: f64) {
        self.settle_from = self.offset;
        self.settle_to = target;
        self.settle = physics::settle_controller();
        self.settle.forward();
    }

    /// Start settling back onto the carousel's own index.
    fn start_settle(&mut self) {
        let target = self.offset_for(self.index);
        self.settle_to(target);
    }

    /// Report `(leading, focal)` for `offset` if either changed since the last
    /// report.
    fn report(&mut self, ctx: &mut EventCtx, offset: f64) {
        let pair = self.indices_at(offset);
        if pair == self.reported {
            return;
        }
        self.reported = pair;
        let count = self.pods.len();
        if let Some(on_change) = self.on_change.as_mut() {
            on_change(
                ctx,
                CarouselChange {
                    leading_index: pair.0,
                    focal_index: pair.1,
                    item_count: count,
                },
            );
        }
    }

    /// Forward a synthesized `Cancel` to the item a drag has taken the gesture
    /// away from.
    fn cancel_item(&mut self, ctx: &mut EventCtx, item: usize, pos: Point) {
        if let Some(pod) = self.pods.get_mut(item) {
            let cancel = InputEvent::Pointer(PointerEvent {
                phase: PointerPhase::Cancel,
                position: pos,
                button: PointerButton::Primary,
            });
            pod.event_child(ctx, &cancel);
            pod.set_active(false);
        }
    }

    /// Clear the recorded focus link on every item but the one a `Down` just
    /// landed on and left focused — the blur
    /// [`frust::authoring::route_event`] performs for containers that route
    /// through it, which this widget's hand-rolled routing owes too
    /// (`docs/CODE_STANDARDS.md`'s focus-routing rule).
    fn blur_unhit(&mut self, hit: Option<usize>) {
        let kept = hit.filter(|index| self.pods.get(*index).is_some_and(ChildPod::is_focused));
        for (index, pod) in self.pods.iter_mut().enumerate() {
            if Some(index) != kept && pod.is_focused() {
                pod.set_focused(false);
            }
        }
    }

    /// The event body, parameterised on an explicit timestamp so the velocity
    /// math is deterministic in tests; [`Widget::event`] supplies the real
    /// clock (see the [module docs](self)).
    fn event_at(&mut self, ctx: &mut EventCtx, event: &InputEvent, t_ms: f64) -> EventResult {
        // A broadcast is not user input: it reaches every child ahead of the
        // capture/hit-test branches and is never consumed.
        if event.is_broadcast() {
            return frust::authoring::route_event(&mut self.pods, ctx, event);
        }
        if let InputEvent::Scroll { delta, .. } = event
            && self.drag.is_none()
            && self.scrollable()
        {
            return self.wheel(ctx, delta);
        }
        let InputEvent::Pointer(p) = event else {
            // Key/Ime are focus-routed; the carousel itself has no key handling.
            return frust::authoring::route_event(&mut self.pods, ctx, event);
        };

        // A live gesture owns every pointer pass until it ends.
        if self.drag.is_some() {
            return self.drag_event(ctx, event, p, t_ms);
        }

        match p.phase {
            PointerPhase::Down => {
                let item = self.item_at(p.position);
                if !presses(p) {
                    // A secondary press is a context gesture: it reaches the
                    // item but starts no drag and arms no press.
                    let result = self.forward_to_item(ctx, event, item);
                    self.blur_unhit(item);
                    return result;
                }
                let main = self.main_of(p.position);
                self.drag = Some(Drag {
                    start: main,
                    from: self.offset,
                    item,
                    scrolling: false,
                });
                self.tracker.clear();
                self.tracker.record(t_ms, main);
                self.settle.stop();
                self.pressed = item;
                self.state.set_pressed(item.is_some());
                ctx.capture_pointer();
                self.forward_to_item(ctx, event, item);
                self.blur_unhit(item);
                ctx.request_redraw();
                EventResult::Handled
            }
            PointerPhase::Move => {
                // An uncaptured move is a hover pass: the item under the
                // pointer claims for itself.
                self.forward_to_item(ctx, event, self.item_at(p.position));
                EventResult::Ignored
            }
            PointerPhase::Up | PointerPhase::Cancel => EventResult::Ignored,
        }
    }

    /// The captured arm: every pass between the `Down` that armed a drag and
    /// the `Up`/`Cancel` that ends it.
    fn drag_event(
        &mut self,
        ctx: &mut EventCtx,
        event: &InputEvent,
        p: &PointerEvent,
        t_ms: f64,
    ) -> EventResult {
        let main = self.main_of(p.position);
        match p.phase {
            PointerPhase::Down => EventResult::Handled,
            PointerPhase::Move => {
                self.tracker.record(t_ms, main);
                let Some(drag) = self.drag.as_ref() else {
                    return EventResult::Handled;
                };
                let (from, start, item, scrolling) =
                    (drag.from, drag.start, drag.item, drag.scrolling);
                if scrolling {
                    self.set_offset(from - (main - start));
                    self.report(ctx, self.offset);
                    ctx.request_redraw();
                    return EventResult::Handled;
                }
                if self.scrollable() && (main - start).abs() > TOUCH_SLOP {
                    // Take the gesture over: cancel the item's own press and
                    // scroll from here.
                    if let Some(item) = item {
                        self.cancel_item(ctx, item, p.position);
                    }
                    self.pressed = None;
                    self.state.set_pressed(false);
                    if let Some(drag) = self.drag.as_mut() {
                        drag.scrolling = true;
                    }
                    ctx.request_redraw();
                    return EventResult::Handled;
                }
                self.forward_to_item(ctx, event, item);
                EventResult::Handled
            }
            PointerPhase::Up => {
                let Some(drag) = self.drag.take() else {
                    return EventResult::Handled;
                };
                self.pressed = None;
                self.state.set_pressed(false);
                if drag.scrolling {
                    self.release(ctx);
                } else {
                    self.forward_to_item(ctx, event, drag.item);
                    // Fire on up-inside: the release must land on the same item
                    // the press did.
                    if let Some(item) = drag.item
                        && self.item_at(p.position) == Some(item)
                        && let Some(on_tap) = self.on_tap.as_mut()
                    {
                        on_tap(ctx, item);
                    }
                }
                if let Some(item) = drag.item
                    && let Some(pod) = self.pods.get_mut(item)
                {
                    pod.set_active(false);
                }
                ctx.request_redraw();
                EventResult::Handled
            }
            PointerPhase::Cancel => {
                // Internal flags only — a `Cancel` arm never reaches app state
                // and never fires a callback.
                let drag = self.drag.take();
                self.pressed = None;
                self.state.set_pressed(false);
                if let Some(drag) = &drag
                    && let Some(item) = drag.item
                {
                    if !drag.scrolling {
                        self.forward_to_item(ctx, event, Some(item));
                    }
                    if let Some(pod) = self.pods.get_mut(item) {
                        pod.set_active(false);
                    }
                }
                self.start_settle();
                ctx.request_redraw();
                EventResult::Handled
            }
        }
    }

    /// Choose and start the settle a released scroll lands on, reporting the
    /// item it will land on right away (the paint-driven ramp that follows
    /// carries no `EventCtx` to report from).
    fn release(&mut self, ctx: &mut EventCtx) {
        // The content moves opposite the finger, so a forward scroll is a
        // negative finger velocity (`physics`' sign convention).
        let scroll_velocity = -self.tracker.velocity();
        self.settle_snapped(ctx, scroll_velocity);
    }

    /// Snap onto the item a scroll at `scroll_velocity` lands on, reporting it.
    fn settle_snapped(&mut self, ctx: &mut EventCtx, scroll_velocity: f64) {
        let stride = self.solver(self.offset).stride();
        let target = physics::snap_offset(self.offset, stride, scroll_velocity, self.max_offset());
        self.report(ctx, target);
        if self.controlled {
            // Controlled: the content still has to settle back onto the item
            // the app last confirmed.
            self.start_settle();
            return;
        }
        self.index = physics::nearest_item(target, stride).min(self.pods.len().saturating_sub(1));
        self.settle_to(target);
    }

    /// The mouse-wheel arm: move the content by the wheel delta, then settle
    /// onto the nearest item boundary.
    ///
    /// Upstream has no wheel path of its own (its carousel is swipe-driven
    /// under `NeverScrollableScrollPhysics`); this is the port's desktop
    /// completion of it, taking [`crate::button_group`]'s per-axis delta
    /// selection and baseline `ScrollView`'s sign convention (a positive delta
    /// moves the content forward).
    fn wheel(&mut self, ctx: &mut EventCtx, delta: &ScrollDelta) -> EventResult {
        let (dx, dy) = match delta {
            ScrollDelta::Lines(x, y) => (x * WHEEL_LINE_PX, y * WHEEL_LINE_PX),
            ScrollDelta::Pixels(x, y) => (*x, *y),
        };
        // A horizontal carousel takes the wheel's horizontal axis when it
        // carries one, and its vertical axis otherwise (the usual shift-less
        // mouse over a horizontal strip).
        let main = match self.axis {
            CarouselAxis::Horizontal if dx != 0.0 => dx,
            CarouselAxis::Horizontal | CarouselAxis::Vertical => dy,
        };
        self.set_offset(self.offset + main);
        // A wheel tick carries no velocity of its own: it always settles onto
        // the nearest boundary.
        self.settle_snapped(ctx, 0.0);
        ctx.request_redraw();
        EventResult::Handled
    }

    /// Forward an event to one item's pod, if there is one under the pointer.
    fn forward_to_item(
        &mut self,
        ctx: &mut EventCtx,
        event: &InputEvent,
        item: Option<usize>,
    ) -> EventResult {
        match item.and_then(|index| self.pods.get_mut(index)) {
            Some(pod) => pod.event_child(ctx, event),
            None => EventResult::Ignored,
        }
    }

    /// Advance the settle ramp from the shell frame clock, moving the content
    /// and asking for the next frame while it runs.
    fn advance_settle(&mut self, ctx: &mut PaintCtx, reduce_motion: bool) {
        if self.drag.is_some() || !self.settle.is_animating() {
            return;
        }
        let next = if reduce_motion {
            self.settle.stop();
            self.settle_to
        } else {
            if self.settle.advance(ctx.frame_time()) {
                ctx.request_frame();
            }
            let ramp = self.settle.value_clamped();
            self.settle_from + (self.settle_to - self.settle_from) * ramp
        };
        if (next - self.offset).abs() > OFFSET_EPSILON {
            self.set_offset(next);
        }
    }
}

/// The main-axis component of a size under `axis`.
fn content_main(axis: CarouselAxis, size: Size) -> f64 {
    match axis {
        CarouselAxis::Horizontal => size.width,
        CarouselAxis::Vertical => size.height,
    }
}

/// The cross-axis component of a size under `axis`.
fn content_cross(axis: CarouselAxis, size: Size) -> f64 {
    match axis {
        CarouselAxis::Horizontal => size.height,
        CarouselAxis::Vertical => size.width,
    }
}

/// A `(main, cross)` pair mapped onto `(x, y)` under `axis` — the one place
/// the vertical carousel differs from the horizontal one.
fn axis_point(axis: CarouselAxis, main: f64, cross: f64) -> Point {
    match axis {
        CarouselAxis::Horizontal => Point::new(main, cross),
        CarouselAxis::Vertical => Point::new(cross, main),
    }
}

/// The resolved item corner radius: the explicit builder value, then the
/// theme's `shape.extra_large`, then [`CAROUSEL_ITEM_RADIUS`].
fn resolve_radius(explicit: Option<f64>, theme: Option<&Theme>) -> f64 {
    explicit.unwrap_or(match theme {
        Some(theme) => theme.shape.extra_large,
        None => CAROUSEL_ITEM_RADIUS,
    })
}

/// The resolved state-layer content color for a pressed item. Themed:
/// `colors.on_surface`. Unthemed: [`ON_SURFACE`].
fn resolve_content_color(theme: Option<&Theme>) -> Color {
    match theme {
        Some(theme) => theme.scheme().on_surface,
        None => ON_SURFACE,
    }
}

impl Widget for CarouselWidget {
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        let max = bc.max();
        // A carousel derives every extent from its viewport, so an unbounded
        // axis has nothing to derive from: it takes the pinned width/height if
        // one was given and collapses otherwise (upstream requires a bounded
        // box too — `M3ECarousel` reads `width ?? constraints.maxWidth`).
        let width = self
            .width
            .unwrap_or(if max.width.is_finite() {
                max.width
            } else {
                0.0
            })
            .max(0.0);
        let height = self
            .height
            .unwrap_or(if max.height.is_finite() {
                max.height
            } else {
                0.0
            })
            .max(0.0);
        let size = Size::new(width, height);
        (self.main, self.cross) = match self.axis {
            CarouselAxis::Horizontal => (width, height),
            CarouselAxis::Vertical => (height, width),
        };

        let content = self.content_size();
        for pod in self.pods.iter_mut() {
            pod.layout_child(ctx, &BoxConstraints::tight(content));
        }

        // A resize rescales every extent, and the carousel must stay on its own
        // item unless a gesture or a settle currently owns the offset.
        if self.drag.is_none() && !self.settle.is_animating() {
            self.index = self.index.min(self.pods.len().saturating_sub(1));
            let target = self.offset_for(self.index);
            self.offset = target;
        }
        self.set_offset(self.offset);
        bc.constrain(size)
    }

    fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut dyn PaintScene) {
        // Record the shared frame clock so the between-frames event pass has a
        // timestamp for velocity tracking.
        self.last_frame_time = ctx.frame_time();
        let (radius, content_color, reduce_motion) = {
            let theme = Theme::from_paint_ctx(ctx);
            (
                resolve_radius(self.item_radius, theme),
                resolve_content_color(theme),
                theme.is_some_and(|t| t.motion.reduce_motion),
            )
        };
        self.advance_settle(ctx, reduce_motion);

        let origin = ctx.origin();
        let size = ctx.size();
        scene.push_clip(origin, size);
        // `slots` is a snapshot; the pods it names are borrowed mutably below.
        let slots = std::mem::take(&mut self.slots);
        for slot in &slots {
            let local = self.item_rect(slot);
            if local.width() <= 0.0 || local.height() <= 0.0 {
                continue;
            }
            let at = Point::new(origin.x + local.x0, origin.y + local.y0);
            let item_size = local.size();
            // The rounded clip *is* the item's shape: baseline `Clip` does not
            // visually clip (module docs). The radius is deliberately not
            // capped here — `kurbo::RoundedRect::from_rect` clamps it to half
            // the shortest side, so an item shrinking past `2 × radius` reads
            // as a pill rather than as an artefact.
            scene.push_clip_rounded(at, item_size, radius);
            if let Some(pod) = self.pods.get_mut(slot.index) {
                pod.paint_child(ctx, scene);
            }
            if self.pressed == Some(slot.index) {
                self.state.paint(
                    ctx,
                    scene,
                    Rect::from_origin_size(at, item_size),
                    radius,
                    content_color,
                );
            }
            scene.pop_clip();
        }
        self.slots = slots;
        scene.pop_clip();
    }

    fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
        self.event_at(ctx, event, self.event_time_ms())
    }

    fn semantics(&self, ctx: &mut SemanticsCtx) {
        let pods = &self.pods;
        ctx.push_container(
            Role::Group,
            |_| {},
            |ctx| {
                for pod in pods {
                    pod.semantics_child(ctx);
                }
            },
        );
    }

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

#[cfg(test)]
mod tests {
    use super::*;
    use frust::authoring::{PointerButton, PointerEvent, PointerPhase, any};
    use std::any::Any;

    /// A 600×200 carousel: three `[5, 4, 1]` slots of 300/240/60px.
    const WINDOW: Size = Size::new(600.0, 200.0);
    /// The contained layout's leading-slot extent at [`WINDOW`] — one item step.
    const STRIDE: f64 = 300.0;

    #[derive(Default)]
    struct Reports {
        changes: Vec<CarouselChange>,
        taps: Vec<usize>,
        presses: usize,
    }

    /// A leaf that fills whatever it is given and counts the presses that reach
    /// it, generic over the app state.
    struct Item;

    /// The retained half of [`Item`].
    struct ItemWidget;

    impl<S: 'static> View<S> for Item {
        type Element = ItemWidget;
        fn build(&self, _ctx: &mut BuildCtx<'_>) -> ItemWidget {
            ItemWidget
        }
        fn rebuild(
            &self,
            _prev: &Self,
            _element: &mut ItemWidget,
            _ctx: &mut BuildCtx<'_>,
        ) -> ChangeFlags {
            ChangeFlags::NONE
        }
    }

    impl Widget for ItemWidget {
        fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
            bc.max()
        }
        fn paint(&mut self, _ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {}
        fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
            if let InputEvent::Pointer(p) = event
                && p.phase == PointerPhase::Down
            {
                ctx.state_mut::<Reports>().presses += 1;
                // An item with its own focusable content, so the carousel's
                // blur bookkeeping has something to clear.
                ctx.request_focus();
                return EventResult::Handled;
            }
            EventResult::Ignored
        }
    }

    #[derive(Default)]
    struct RecordingScene {
        clips: Vec<(Point, Size)>,
        rounded: Vec<(Point, Size, f64)>,
        rrects: Vec<(Point, Size, f64, Color)>,
    }

    impl PaintScene for RecordingScene {
        fn fill_rect(&mut self, _o: Point, _s: Size, _c: Color) {}
        fn draw_text(&mut self, _o: Point, _t: &str) {}
        fn push_clip(&mut self, origin: Point, size: Size) {
            self.clips.push((origin, size));
        }
        fn push_clip_rounded(&mut self, origin: Point, size: Size, radius: f64) {
            self.rounded.push((origin, size, radius));
        }
        fn pop_clip(&mut self) {}
        fn fill_rounded_rect(&mut self, o: Point, s: Size, radius: f64, color: Color) {
            self.rrects.push((o, s, radius, color));
        }
    }

    fn items(count: usize) -> Vec<AnyView<Reports>> {
        (0..count).map(|_| any(Item)).collect()
    }

    fn view(count: usize) -> CarouselView<Reports> {
        contained_carousel(items(count))
            .on_change(|s: &mut Reports, change: CarouselChange| s.changes.push(change))
            .on_tap(|s: &mut Reports, index: usize| s.taps.push(index))
    }

    fn build(v: &CarouselView<Reports>) -> CarouselWidget {
        let mut counter = 0u64;
        View::<Reports>::build(v, &mut BuildCtx::new(&mut counter))
    }

    fn laid_out(v: &CarouselView<Reports>) -> CarouselWidget {
        let mut w = build(v);
        let mut ctx = LayoutCtx::new();
        w.layout(&mut ctx, &BoxConstraints::loose(WINDOW));
        w
    }

    fn ev(phase: PointerPhase, x: f64, y: f64) -> InputEvent {
        InputEvent::Pointer(PointerEvent {
            phase,
            position: Point::new(x, y),
            button: PointerButton::Primary,
        })
    }

    fn secondary_ev(phase: PointerPhase, x: f64, y: f64) -> InputEvent {
        InputEvent::Pointer(PointerEvent {
            phase,
            position: Point::new(x, y),
            button: PointerButton::Secondary,
        })
    }

    /// Dispatch one event at an explicit timestamp (ms), the seam the velocity
    /// math is parameterised on.
    fn dispatch(w: &mut CarouselWidget, state: &mut Reports, event: &InputEvent, t_ms: f64) {
        let state_any: &mut dyn Any = state;
        let mut ctx = EventCtx::new(state_any, Point::ZERO, WINDOW);
        w.event_at(&mut ctx, event, t_ms);
    }

    fn paint(w: &mut CarouselWidget) -> RecordingScene {
        let mut ctx = PaintCtx::new(Point::ZERO, WINDOW);
        let mut scene = RecordingScene::default();
        w.paint(&mut ctx, &mut scene);
        scene
    }

    /// Drive a started settle to completion without a shell clock, then apply
    /// the resulting offset the way `paint` would.
    fn finish_settle(w: &mut CarouselWidget) {
        w.settle.advance(FrameTime::from_nanos(0));
        w.settle.advance(FrameTime::from_nanos(2_000_000_000));
        let value = w.settle.value_clamped();
        let next = w.settle_from + (w.settle_to - w.settle_from) * value;
        w.set_offset(next);
    }

    // ---- layout wiring ----------------------------------------------------

    #[test]
    fn every_child_is_laid_out_at_the_largest_slot_less_its_padding() {
        let w = laid_out(&view(6));
        for pod in &w.pods {
            assert_eq!(
                pod.size(),
                Size::new(
                    STRIDE - CAROUSEL_ITEM_PADDING * 2.0,
                    WINDOW.height - CAROUSEL_ITEM_PADDING * 2.0
                ),
                "a slot's extent changes with the scroll; the child's does not"
            );
        }
    }

    #[test]
    fn the_visible_slots_are_the_layouts_own_weights() {
        let w = laid_out(&view(6));
        assert_eq!(
            w.slots.iter().map(|s| s.extent).collect::<Vec<_>>(),
            vec![300.0, 240.0, 60.0]
        );
    }

    #[test]
    fn items_beyond_the_viewport_are_parked_outside_the_carousels_bounds() {
        let w = laid_out(&view(6));
        for pod in &w.pods[3..] {
            assert!(
                pod.origin().x >= WINDOW.width || pod.origin().x + pod.size().width <= 0.0,
                "an invisible item must be unhittable, not merely unpainted"
            );
        }
    }

    #[test]
    fn a_vertical_carousel_slots_along_the_y_axis() {
        let w = laid_out(&view(6).axis(CarouselAxis::Vertical));
        // 200px of main axis over `[5, 4, 1]`: 100/80/20.
        assert_eq!(
            w.slots.iter().map(|s| s.extent).collect::<Vec<_>>(),
            vec![100.0, 80.0, 20.0]
        );
        assert_eq!(w.pods[0].origin().y, CAROUSEL_ITEM_PADDING);
        assert_eq!(
            w.pods[0].size().width,
            WINDOW.width - CAROUSEL_ITEM_PADDING * 2.0
        );
    }

    #[test]
    fn an_unbounded_axis_collapses_unless_a_size_is_pinned() {
        let mut w = build(&view(6));
        let mut ctx = LayoutCtx::new();
        let size = w.layout(
            &mut ctx,
            &BoxConstraints::new(Size::ZERO, Size::new(f64::INFINITY, f64::INFINITY)),
        );
        assert_eq!(size, Size::ZERO);

        let mut w = build(&view(6).width(400.0).height(120.0));
        let mut ctx = LayoutCtx::new();
        let size = w.layout(
            &mut ctx,
            &BoxConstraints::new(Size::ZERO, Size::new(f64::INFINITY, f64::INFINITY)),
        );
        assert_eq!(size, Size::new(400.0, 120.0));
    }

    #[test]
    fn the_hero_layouts_focal_item_is_offset_from_the_leading_one() {
        let w = laid_out(&view(6).layout(CarouselLayout::Hero));
        assert_eq!(w.focal_slot(), 1, "`[2, 6, 2]`'s max weight is in slot 1");
        assert_eq!(w.leading_index(), 0);
        assert_eq!(w.focal_index(), 1);

        let start = laid_out(
            &view(6)
                .layout(CarouselLayout::Hero)
                .hero_alignment(HeroAlignment::Start),
        );
        assert_eq!(start.focal_slot(), 0, "`[8, 2]` leads with its max weight");
    }

    #[test]
    fn an_uncontained_carousel_uses_a_fixed_extent_and_the_list_scroll_bound() {
        let w = laid_out(&uncontained_carousel(items(6), 200.0));
        assert_eq!(
            w.slots.iter().map(|s| s.extent).collect::<Vec<_>>(),
            vec![200.0, 200.0, 200.0]
        );
        assert_eq!(w.max_offset(), 6.0 * 200.0 - WINDOW.width);
        assert_eq!(
            w.focal_index(),
            w.leading_index(),
            "no focal slot to shift to"
        );
    }

    #[test]
    fn the_initial_item_names_the_focal_item_not_the_leading_one() {
        let w = laid_out(&view(8).layout(CarouselLayout::Hero).initial_item(3));
        assert_eq!(
            w.leading_index(),
            2,
            "`[2, 6, 2]` shifts the focal item by 1"
        );
        assert_eq!(w.focal_index(), 3);
        assert_eq!(
            w.offset(),
            2.0 * 120.0,
            "and rests on the leading slot item"
        );
    }

    #[test]
    fn consume_max_weight_makes_the_slot_item_the_focal_one() {
        // With the leading items allowed to expand into the max slot, the item
        // the offset rests on is the focal one and the *leading* one is the
        // phantom-shifted index behind it.
        let w = laid_out(
            &view(8)
                .layout(CarouselLayout::Hero)
                .consume_max_weight(true)
                .initial_item(3),
        );
        assert_eq!(w.focal_index(), 3);
        assert_eq!(w.leading_index(), 2);
        assert_eq!(w.offset(), 3.0 * 120.0);

        let at_rest = laid_out(
            &view(8)
                .layout(CarouselLayout::Hero)
                .consume_max_weight(true),
        );
        assert_eq!(
            at_rest.focal_index(),
            0,
            "item 0 occupies the largest slot at offset 0"
        );
    }

    // ---- paint ------------------------------------------------------------

    #[test]
    fn each_visible_item_is_painted_inside_its_own_rounded_clip() {
        let mut w = laid_out(&view(6));
        let scene = paint(&mut w);
        assert_eq!(scene.clips.len(), 1, "the carousel clips its own viewport");
        assert_eq!(scene.rounded.len(), 3, "one rounded clip per visible item");
        let (origin, size, radius) = scene.rounded[0];
        assert_eq!(
            origin,
            Point::new(CAROUSEL_ITEM_PADDING, CAROUSEL_ITEM_PADDING)
        );
        assert_eq!(
            size,
            Size::new(
                STRIDE - CAROUSEL_ITEM_PADDING * 2.0,
                WINDOW.height - CAROUSEL_ITEM_PADDING * 2.0
            )
        );
        assert_eq!(radius, CAROUSEL_ITEM_RADIUS);
    }

    #[test]
    fn a_theme_resolves_the_item_radius_and_an_explicit_value_wins() {
        let theme = crate::baseline();
        let mut w = laid_out(&view(6));
        let mut ctx = PaintCtx::new(Point::ZERO, WINDOW).with_theme(&theme);
        let mut scene = RecordingScene::default();
        w.paint(&mut ctx, &mut scene);
        assert_eq!(scene.rounded[0].2, theme.shape.extra_large);

        let mut w = laid_out(&view(6).item_radius(4.0));
        let mut ctx = PaintCtx::new(Point::ZERO, WINDOW).with_theme(&theme);
        let mut scene = RecordingScene::default();
        w.paint(&mut ctx, &mut scene);
        assert_eq!(scene.rounded[0].2, 4.0);
    }

    #[test]
    fn a_pressed_item_paints_the_state_layer_over_its_own_clip() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 100.0, 100.0),
            0.0,
        );
        let scene = paint(&mut w);
        assert_eq!(scene.rrects.len(), 1, "exactly the pressed item's overlay");
        assert_eq!(
            scene.rrects[0].0,
            Point::new(CAROUSEL_ITEM_PADDING, CAROUSEL_ITEM_PADDING)
        );
        assert_eq!(
            scene.rrects[0].3.components[3],
            crate::interaction::PRESSED_OPACITY
        );

        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 100.0, 100.0),
            16.0,
        );
        let scene = paint(&mut w);
        assert!(scene.rrects.is_empty(), "the overlay clears on release");
    }

    // ---- gestures and snapping --------------------------------------------

    #[test]
    fn a_press_reaches_the_item_under_it_and_a_tap_reports_that_item() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 400.0, 100.0),
            0.0,
        );
        assert_eq!(
            state.presses, 1,
            "the item under the pointer sees the press"
        );
        assert_eq!(w.pressed, Some(1));
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 400.0, 100.0),
            16.0,
        );
        assert_eq!(state.taps, vec![1]);
        assert!(w.pressed.is_none());
    }

    #[test]
    fn a_release_outside_the_pressed_item_reports_no_tap() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 100.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 400.0, 100.0),
            16.0,
        );
        assert!(state.taps.is_empty());
    }

    #[test]
    fn a_secondary_press_reaches_the_item_but_arms_no_drag() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &secondary_ev(PointerPhase::Down, 100.0, 100.0),
            0.0,
        );
        assert!(w.drag.is_none());
        assert!(w.pressed.is_none());
    }

    #[test]
    fn a_drag_past_the_slop_takes_the_gesture_over_and_moves_the_content() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 400.0, 100.0),
            0.0,
        );
        // Inside the slop: still the item's gesture.
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 398.0, 100.0),
            8.0,
        );
        assert_eq!(w.offset, 0.0);
        assert_eq!(w.pressed, Some(1), "the press survives a sub-slop wobble");
        // Past it: the carousel takes over and the press is released.
        let past = 400.0 - TOUCH_SLOP - 5.0;
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, past, 100.0),
            16.0,
        );
        assert!(w.pressed.is_none());
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 250.0, 100.0),
            24.0,
        );
        assert_eq!(w.offset, 150.0, "the content follows the finger");
    }

    #[test]
    fn a_slow_release_mid_item_settles_onto_the_nearest_boundary() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        // Drag 160px (past the 150px half-item mark) slowly enough that the
        // release velocity is below the tolerance.
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 400.0, 100.0),
            400.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 340.0, 100.0),
            900.0,
        );
        assert_eq!(w.offset, 160.0);
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 340.0, 100.0),
            1000.0,
        );
        assert_eq!(w.settle_to, STRIDE, "settles forward to the item boundary");
        finish_settle(&mut w);
        assert_eq!(w.offset, STRIDE);
        assert_eq!(w.leading_index(), 1);
    }

    #[test]
    fn a_slow_release_before_the_halfway_mark_settles_back() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 440.0, 100.0),
            500.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 420.0, 100.0),
            1000.0,
        );
        assert_eq!(w.offset, 80.0);
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 420.0, 100.0),
            1100.0,
        );
        assert_eq!(w.settle_to, 0.0);
        finish_settle(&mut w);
        assert_eq!(w.offset, 0.0);
        assert_eq!(w.leading_index(), 0);
    }

    #[test]
    fn a_short_fast_fling_still_advances_one_whole_item() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        // The first move crosses the slop and only takes the gesture over; the
        // second scrolls. 60px in 16ms is 3750px/s — far past the fling
        // tolerance, and well short of the 150px a slow release would need.
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 470.0, 100.0),
            8.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 440.0, 100.0),
            16.0,
        );
        assert_eq!(w.offset, 60.0);
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 440.0, 100.0),
            16.0,
        );
        assert_eq!(w.settle_to, STRIDE, "a fling advances one item");
        finish_settle(&mut w);
        assert_eq!(w.leading_index(), 1);
    }

    #[test]
    fn a_backward_fling_returns_to_the_previous_item() {
        let mut w = laid_out(&view(6));
        w.set_offset(STRIDE);
        w.index = 1;
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 300.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 330.0, 100.0),
            8.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 360.0, 100.0),
            16.0,
        );
        assert_eq!(
            w.offset, 240.0,
            "only 60px back — a slow release would stay"
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 360.0, 100.0),
            16.0,
        );
        assert_eq!(w.settle_to, 0.0);
        finish_settle(&mut w);
        assert_eq!(w.leading_index(), 0);
    }

    #[test]
    fn a_scroll_never_leaves_the_scrollable_range() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 100.0, 100.0),
            8.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, -5000.0, 100.0),
            16.0,
        );
        assert_eq!(w.offset, w.max_offset());
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 9000.0, 100.0),
            24.0,
        );
        assert_eq!(w.offset, 0.0);
    }

    #[test]
    fn a_carousel_with_nothing_to_scroll_never_takes_a_gesture_over() {
        // Three items in three slots: `max_scroll_offset` is zero.
        let mut w = laid_out(&view(3));
        assert!(!w.scrollable());
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 100.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, -500.0, 100.0),
            16.0,
        );
        assert_eq!(w.offset, 0.0);
        assert_eq!(w.pressed, Some(0), "the press stays with the item");
    }

    #[test]
    fn a_wheel_tick_moves_the_content_and_settles_onto_a_boundary() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        let wheel = InputEvent::Scroll {
            position: Point::new(100.0, 100.0),
            delta: frust::authoring::ScrollDelta::Pixels(180.0, 0.0),
        };
        dispatch(&mut w, &mut state, &wheel, 0.0);
        assert_eq!(w.offset, 180.0, "the content follows the wheel");
        assert_eq!(w.settle_to, STRIDE, "and settles onto the nearest boundary");
        finish_settle(&mut w);
        assert_eq!(w.leading_index(), 1);
    }

    #[test]
    fn a_press_on_another_item_clears_the_previous_ones_focus_link() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 100.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 100.0, 100.0),
            16.0,
        );
        assert!(
            w.pods[0].is_focused(),
            "the item claimed focus on its press"
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 400.0, 100.0),
            32.0,
        );
        assert!(!w.pods[0].is_focused(), "a press elsewhere blurs it");
        assert!(w.pods[1].is_focused());
    }

    #[test]
    fn a_cancel_clears_the_gesture_and_settles_back_without_reporting() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 350.0, 100.0),
            16.0,
        );
        let reported = state.changes.len();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Cancel, 350.0, 100.0),
            24.0,
        );
        assert!(w.drag.is_none());
        assert!(w.pressed.is_none());
        assert_eq!(w.settle_to, 0.0, "back onto the item it started from");
        assert_eq!(state.changes.len(), reported, "a cancel reports nothing");
        assert!(state.taps.is_empty());
    }

    // ---- reporting and the controlled contract ----------------------------

    #[test]
    fn a_scroll_reports_the_leading_and_focal_item_at_the_halfway_mark() {
        let mut w = laid_out(&view(8).layout(CarouselLayout::Hero));
        let mut state = Reports::default();
        // `[2, 6, 2]` over 600px: a 120px stride, so halfway is 60px.
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        // Crosses the slop, taking the gesture over without scrolling yet.
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 470.0, 100.0),
            100.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 450.0, 100.0),
            200.0,
        );
        assert!(state.changes.is_empty(), "50px in: still nearest to item 0");
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 420.0, 100.0),
            400.0,
        );
        assert_eq!(
            state.changes.last().copied(),
            Some(CarouselChange {
                leading_index: 1,
                focal_index: 2,
                item_count: 8
            })
        );
    }

    #[test]
    fn a_controlled_carousel_reports_the_request_and_moves_nothing() {
        let mut w = laid_out(&view(6).selected(1));
        assert!(w.controlled);
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 400.0, 100.0),
            400.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 340.0, 100.0),
            900.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 340.0, 100.0),
            1000.0,
        );
        assert!(!state.changes.is_empty(), "the request is still reported");
        assert_eq!(
            w.settle_to,
            w.offset_for(w.index),
            "it settles back onto the app-confirmed item"
        );
    }

    #[test]
    fn a_rebuild_with_a_new_selection_settles_onto_it() {
        let mut counter = 0u64;
        let mut ctx = BuildCtx::new(&mut counter);
        let first = view(8).selected(0);
        let mut w = View::<Reports>::build(&first, &mut ctx);
        let mut layout_ctx = LayoutCtx::new();
        w.layout(&mut layout_ctx, &BoxConstraints::loose(WINDOW));
        let next = view(8).selected(3);
        View::<Reports>::rebuild(&next, &first, &mut w, &mut ctx);
        assert_eq!(w.leading_index(), 3);
        finish_settle(&mut w);
        assert_eq!(w.offset, STRIDE * 3.0);
    }

    #[test]
    fn reduce_motion_lands_on_the_target_without_asking_for_a_frame() {
        let mut theme = crate::baseline();
        theme.motion.reduce_motion = true;
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 400.0, 100.0),
            400.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 340.0, 100.0),
            900.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 340.0, 100.0),
            1000.0,
        );
        let mut ctx = PaintCtx::new(Point::ZERO, WINDOW).with_theme(&theme);
        let mut scene = RecordingScene::default();
        w.paint(&mut ctx, &mut scene);
        assert_eq!(w.offset, STRIDE, "jumped straight to the target");
        assert!(!ctx.needs_frame(), "and asked for no continuation frame");
    }

    #[test]
    fn a_settle_asks_for_the_next_frame_while_it_runs() {
        let mut w = laid_out(&view(6));
        let mut state = Reports::default();
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Down, 500.0, 100.0),
            0.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 400.0, 100.0),
            400.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Move, 340.0, 100.0),
            900.0,
        );
        dispatch(
            &mut w,
            &mut state,
            &ev(PointerPhase::Up, 340.0, 100.0),
            1000.0,
        );
        let mut ctx = PaintCtx::new(Point::ZERO, WINDOW);
        let mut scene = RecordingScene::default();
        w.paint(&mut ctx, &mut scene);
        assert!(ctx.needs_frame());
    }

    #[test]
    fn an_empty_carousel_lays_out_and_paints_without_panicking() {
        let mut w = laid_out(&view(0));
        assert!(w.slots.is_empty());
        let scene = paint(&mut w);
        assert!(scene.rounded.is_empty());
        let mut state = Reports::default();
        dispatch(&mut w, &mut state, &ev(PointerPhase::Down, 10.0, 10.0), 0.0);
        dispatch(&mut w, &mut state, &ev(PointerPhase::Up, 10.0, 10.0), 16.0);
        assert!(state.taps.is_empty());
    }

    #[test]
    fn a_zero_sized_carousel_degrades_instead_of_dividing_by_its_viewport() {
        let mut w = build(&view(6));
        let mut ctx = LayoutCtx::new();
        let size = w.layout(&mut ctx, &BoxConstraints::tight(Size::ZERO));
        assert_eq!(size, Size::ZERO);
        assert!(w.slots.is_empty());
        assert_eq!(w.max_offset(), 0.0);
        let scene = paint(&mut w);
        assert!(scene.rounded.is_empty());
    }
}