waft-material 0.8.1

Material Design widgets for Waft
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
//! Flutter counterpart: material_ui's `slider_parts.dart`.
//!
//! [`SliderTickMarkShape`] and [`SliderTrackShape`] are abstract classes users subclass, so
//! traits held as [`SliderTickMarkShapeRef`] and [`SliderTrackShapeRef`]; the mixin
//! [`BaseSliderTrackShape`] is a trait with provided bodies that a track's
//! [`SliderTrackShape`] methods call. A parameter Dart declares optional with no default in
//! the abstract signature is an `Option`, so an implementation applies its own default as
//! Dart's override does. A shape's `paint` takes the `App`: the animations and the parent box
//! live there, and the `Tween` and `ColorTween` Dart makes on each paint are arena objects
//! that [`evaluate_tween`] and [`evaluate_color_tween`] make and evaluate.

use std::any::Any;
use std::fmt::Debug;
use std::rc::Rc;

use waft_animation::{Animatable, AnyAnimation, ColorTween, Tween};
use waft_embedder::{
    Canvas, ClipOp, Color, FillRule, Offset, Paint, PaintStyle, Path, PathBuilder, RRect, Radius,
    Rect, Size, Stroke, TextDirection, rrect_radii_elliptical,
};
use waft_foundation::App;
use waft_painting::{BorderRadius, ClipContext, PaintingBinding, TextPainter, draw_rrect};
use waft_rendering::{AnyRenderBox, PaintingContext, RenderView};
use waft_widgets::WidgetStates;

use crate::colors::Colors;

use crate::slider_theme::SliderThemeData;
use crate::slider_value_indicator_shape::SliderComponentShape;

/// Dart's `ColorTween(begin: begin, end: end).evaluate(animation)`.
pub(crate) fn evaluate_color_tween(
    app: &mut App,
    begin: Option<Color>,
    end: Option<Color>,
    animation: AnyAnimation<f64>,
) -> Option<Color> {
    let tween = ColorTween::new(app, begin, end);
    tween.evaluate(app, animation)
}

/// Dart's `Tween<double>(begin: begin, end: end).evaluate(animation)`.
pub(crate) fn evaluate_tween(
    app: &mut App,
    begin: f64,
    end: f64,
    animation: AnyAnimation<f64>,
) -> f64 {
    let tween = Tween::new(app, Some(begin), Some(end));
    tween.evaluate(app, animation)
}

/// Dart's `canvas.drawShadow(path, color, elevation, transparentOccluder)`, with the device
/// pixel ratio of the view `parent_box` paints into, which the engine reads from the implicit
/// view; 1 outside a view, as the engine's is without metrics.
pub(crate) fn draw_shadow(
    app: &App,
    canvas: &mut Canvas,
    parent_box: AnyRenderBox,
    path: &Path,
    color: Color,
    elevation: f64,
    transparent_occluder: bool,
) {
    let device_pixel_ratio = parent_box
        .as_object()
        .owner(app)
        .and_then(|owner| owner.root_node(app))
        .and_then(|root| root.downcast::<RenderView>(app))
        .map_or(1.0, |view| view.configuration(app).device_pixel_ratio);
    canvas.draw_shadow(
        path.as_valo(),
        path.fill_type,
        color.into(),
        elevation as f32,
        transparent_occluder,
        device_pixel_ratio as f32,
    );
}

/// Base class for `Slider` tick mark shapes.
///
/// Create a subclass of this if you would like a custom slider tick mark shape.
///
/// The tick mark painting can be skipped by specifying
/// [`no_tick_mark`](<dyn SliderTickMarkShape>::no_tick_mark) for
/// `SliderThemeData::tick_mark_shape`.
///
/// Equality is [`eq_shape`](Self::eq_shape): identity, as Dart's default `==`, unless the
/// shape overrides it.
///
/// See also:
///
///  * [`RoundSliderTickMarkShape`], which is the default `Slider`'s tick mark
///    shape that paints a solid circle.
///  * [`SliderTrackShape`], which can be used to create custom shapes for the
///    `Slider`'s track.
///  * [`SliderComponentShape`], which can be used to create custom shapes for
///    the `Slider`'s thumb, overlay, and value indicator and the
///    `RangeSlider`'s overlay.
pub trait SliderTickMarkShape: Any + Debug {
    /// Returns the preferred size of the shape.
    ///
    /// It is used to help position the tick marks within the slider.
    ///
    /// the `slider_theme` argument is the theme assigned to the `Slider` that this
    /// shape belongs to.
    ///
    /// The `is_enabled` argument is false when `Slider::on_changed` is null and true
    /// otherwise. When true, the slider will respond to input.
    fn get_preferred_size(&self, slider_theme: &SliderThemeData, is_enabled: bool) -> Size;

    /// Paints the slider track.
    ///
    /// The `context` argument is the same as the one that includes the `Slider`'s
    /// render box.
    ///
    /// The `center` argument is the offset for where this shape's center should be
    /// painted. This offset is relative to the origin of the `context` canvas.
    ///
    /// The `parent_box` argument is the `RenderBox` of the `Slider`. Its attributes,
    /// such as size, can be used to assist in painting this shape.
    ///
    /// the `slider_theme` argument is the theme assigned to the `Slider` that this
    /// shape belongs to.
    ///
    /// The `enable_animation` argument is an animation triggered when the `Slider`
    /// is enabled, and it reverses when the slider is disabled. The `Slider` is
    /// enabled when `Slider::on_changed` is not null. Use this to paint
    /// intermediate frames for this shape when the slider changes enabled state.
    ///
    /// The `is_enabled` argument is false when `Slider::on_changed` is null and true
    /// otherwise. When true, the slider will respond to input.
    ///
    /// The `text_direction` argument can be used to determine how the tick marks
    /// are painting depending on whether they are on an active track segment or
    /// not. The track segment between the start of the slider and the thumb is
    /// the active track segment. The track segment between the thumb and the end
    /// of the slider is the inactive track segment. In LTR text direction, the
    /// start of the slider is on the left, and in RTL text direction, the start
    /// of the slider is on the right.
    #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        center: Offset,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        enable_animation: AnyAnimation<f64>,
        thumb_center: Offset,
        is_enabled: bool,
        text_direction: TextDirection,
    );

    /// Downcast support for Dart's `is` checks.
    fn as_any(&self) -> &dyn Any;

    /// Dart's `==`: identity unless a shape overrides it.
    fn eq_shape(&self, other: &dyn SliderTickMarkShape) -> bool {
        std::ptr::eq(self.as_any(), other.as_any())
    }
}

/// What a field or parameter Dart types as `SliderTickMarkShape` becomes.
pub type SliderTickMarkShapeRef = Rc<dyn SliderTickMarkShape>;

impl PartialEq for dyn SliderTickMarkShape {
    fn eq(&self, other: &dyn SliderTickMarkShape) -> bool {
        self.eq_shape(other)
    }
}

thread_local! {
    static NO_TICK_MARK: SliderTickMarkShapeRef = Rc::new(EmptySliderTickMarkShape);
}

impl dyn SliderTickMarkShape {
    /// Special instance of [`SliderTickMarkShape`] to skip the tick mark painting.
    ///
    /// Each call hands out the same instance, as Dart's `static final` field is one object.
    ///
    /// See also:
    ///
    ///  * `SliderThemeData::tick_mark_shape`, which is the shape that the `Slider`
    ///    uses when painting tick marks.
    pub fn no_tick_mark() -> SliderTickMarkShapeRef {
        NO_TICK_MARK.with(Rc::clone)
    }
}

/// Base class for slider track shapes.
///
/// The slider's thumb moves along the track. A discrete slider's tick marks
/// are drawn after the track, but before the thumb, and are aligned with the
/// track.
///
/// The [`get_preferred_rect`](Self::get_preferred_rect) helps position the slider thumb and
/// tick marks relative to the track.
///
/// Equality is [`eq_shape`](Self::eq_shape): identity, as Dart's default `==`, unless the
/// shape overrides it.
///
/// See also:
///
///  * [`RoundedRectSliderTrackShape`] for the default `Slider`'s track shape that
///    paints a stadium-like track.
///  * [`SliderTickMarkShape`], which can be used to create custom shapes for the
///    `Slider`'s tick marks.
///  * [`SliderComponentShape`], which can be used to create custom shapes for
///    the `Slider`'s thumb, overlay, and value indicator and the
///    `RangeSlider`'s overlay.
pub trait SliderTrackShape: Any + Debug {
    /// Returns the preferred bounds of the shape.
    ///
    /// It is used to provide horizontal boundaries for the thumb's position, and
    /// to help position the slider thumb and tick marks relative to the track.
    ///
    /// The `parent_box` argument can be used to help determine the preferredRect relative to
    /// attributes of the render box of the slider itself, such as size.
    ///
    /// The `offset` argument is relative to the caller's bounding box. It can be used to
    /// convert gesture coordinates from global to slider-relative coordinates; Dart's
    /// default is `Offset.zero`.
    ///
    /// the `slider_theme` argument is the theme assigned to the `Slider` that this
    /// shape belongs to.
    ///
    /// The `is_enabled` argument is false when `Slider::on_changed` is null and true
    /// otherwise. When true, the slider will respond to input. `None` is Dart's argument
    /// left out.
    ///
    /// The `is_discrete` argument is true if `Slider::divisions` is non-null. When
    /// true, the slider will render tick marks on top of the track. `None` is Dart's
    /// argument left out.
    fn get_preferred_rect(
        &self,
        app: &App,
        parent_box: AnyRenderBox,
        offset: Offset,
        slider_theme: &SliderThemeData,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
    ) -> Rect;

    /// Paints the track shape based on the state passed to it.
    ///
    /// The `context` argument is the same as the one that includes the `Slider`'s
    /// render box.
    ///
    /// The `offset` argument the offset of the origin of the `parent_box` to the
    /// origin of its `context` canvas. This shape must be painted relative to
    /// this offset. See `PaintingContextCallback`.
    ///
    /// The `parent_box` argument is the `RenderBox` of the `Slider`. Its attributes,
    /// such as size, can be used to assist in painting this shape.
    ///
    /// the `slider_theme` argument is the theme assigned to the `Slider` that this
    /// shape belongs to.
    ///
    /// The `enable_animation` argument is an animation triggered when the `Slider`
    /// is enabled, and it reverses when the slider is disabled. The `Slider` is
    /// enabled when `Slider::on_changed` is not null. Use this to paint
    /// intermediate frames for this shape when the slider changes enabled state.
    ///
    /// The `thumb_center` argument is the offset of the center of the thumb
    /// relative to the origin of the `PaintingContext::canvas`. It can be used as
    /// the point that divides the track into 2 segments.
    ///
    /// The `secondary_offset` argument is the offset of the secondary value
    /// relative to the origin of the `PaintingContext::canvas`.
    ///
    /// If not null, the track is divided into 3 segments.
    ///
    /// The `is_enabled` argument is false when `Slider::on_changed` is null and true
    /// otherwise. When true, the slider will respond to input. `None` is Dart's argument
    /// left out.
    ///
    /// The `is_discrete` argument is true if `Slider::divisions` is non-null. When
    /// true, the slider will render tick marks on top of the track. `None` is Dart's
    /// argument left out.
    ///
    /// The `text_direction` argument can be used to determine how the track
    /// segments are painted depending on whether they are active or not.
    ///
    /// The track segment between the start of the slider and the thumb is the
    /// active track segment. The track segment between the thumb and the end of the
    /// slider is the inactive track segment. In `TextDirection::Ltr`, the start of
    /// the slider is on the left, and in `TextDirection::Rtl`, the start of the
    /// slider is on the right.
    #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        enable_animation: AnyAnimation<f64>,
        thumb_center: Offset,
        secondary_offset: Option<Offset>,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
        text_direction: TextDirection,
    );

    /// Whether the track shape is rounded.
    ///
    /// This is used to determine the correct position of the thumb in relation to the track.
    fn is_rounded(&self) -> bool {
        false
    }

    /// Downcast support for Dart's `is` checks.
    fn as_any(&self) -> &dyn Any;

    /// Dart's `==`: identity unless a shape overrides it.
    fn eq_shape(&self, other: &dyn SliderTrackShape) -> bool {
        std::ptr::eq(self.as_any(), other.as_any())
    }
}

/// What a field or parameter Dart types as `SliderTrackShape` becomes.
pub type SliderTrackShapeRef = Rc<dyn SliderTrackShape>;

impl PartialEq for dyn SliderTrackShape {
    fn eq(&self, other: &dyn SliderTrackShape) -> bool {
        self.eq_shape(other)
    }
}

/// Base track shape that provides an implementation of
/// [`get_preferred_rect`](Self::get_preferred_rect) for default sizing.
///
/// The height is set from `SliderThemeData::track_height` and the width of the
/// parent box less the larger of the widths of `SliderThemeData::thumb_shape` and
/// `SliderThemeData::overlay_shape`.
///
/// A track that mixes it in calls these bodies from its [`SliderTrackShape`] methods.
///
/// See also:
///
///  * [`RectangularSliderTrackShape`], which is a track shape with sharp
///    rectangular edges
///  * [`RoundedRectSliderTrackShape`], which is a track shape with round
///    stadium-like edges.
pub trait BaseSliderTrackShape {
    /// Returns a rect that represents the track bounds that fits within the
    /// `Slider`.
    ///
    /// The width is the width of the `Slider` or `RangeSlider`, but padded by
    /// the max of the overlay and thumb radius. The height is defined by the
    /// `SliderThemeData::track_height`.
    ///
    /// The [`Rect`] is centered both horizontally and vertically within the slider
    /// bounds.
    fn get_preferred_rect(
        &self,
        app: &App,
        parent_box: AnyRenderBox,
        offset: Offset,
        slider_theme: &SliderThemeData,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
    ) -> Rect {
        let is_enabled = is_enabled.unwrap_or(false);
        let is_discrete = is_discrete.unwrap_or(false);
        let thumb_width = slider_theme
            .thumb_shape
            .as_ref()
            .expect("a slider theme with a thumb shape")
            .get_preferred_size(is_enabled, is_discrete)
            .width();
        let overlay_width = slider_theme
            .overlay_shape
            .as_ref()
            .expect("a slider theme with an overlay shape")
            .get_preferred_size(is_enabled, is_discrete)
            .width();
        let mut track_height = slider_theme
            .track_height
            .expect("a slider theme with a track height");
        debug_assert!(overlay_width >= 0.0);
        debug_assert!(track_height >= 0.0);

        // If the track colors are transparent, then override only the track height
        // to maintain overall Slider width.
        if slider_theme.active_track_color == Some(Colors::TRANSPARENT)
            && slider_theme.inactive_track_color == Some(Colors::TRANSPARENT)
        {
            track_height = 0.0;
        }

        let parent_size = parent_box.size(app);
        let track_left = offset.dx()
            + if slider_theme.padding.is_none() {
                (overlay_width / 2.0).max(thumb_width / 2.0)
            } else {
                0.0
            };
        let track_top = offset.dy() + (parent_size.height() - track_height) / 2.0;
        let track_right = track_left + parent_size.width()
            - if slider_theme.padding.is_none() {
                thumb_width.max(overlay_width)
            } else {
                0.0
            };
        let track_bottom = track_top + track_height;
        // If the parentBox's size less than slider's size the trackRight will be less than trackLeft, so switch them.
        Rect::from_ltrb(
            track_left.min(track_right),
            track_top,
            track_left.max(track_right),
            track_bottom,
        )
    }

    /// Whether the track shape is rounded. This is used to determine the correct
    /// position of the thumb in relation to the track. Defaults to false.
    fn is_rounded(&self) -> bool {
        false
    }
}

/// A `Slider` track that's a simple rectangle.
///
/// It paints a solid colored rectangle, vertically centered in the
/// `parent_box`. The track rectangle extends to the bounds of the `parent_box`,
/// but is padded by the `RoundSliderOverlayShape` radius. The height is defined
/// by the `SliderThemeData::track_height`. The color is determined by the
/// `Slider`'s enabled state and the track segment's active state which are
/// defined by:
///   `SliderThemeData::active_track_color`,
///   `SliderThemeData::inactive_track_color`,
///   `SliderThemeData::disabled_active_track_color`,
///   `SliderThemeData::disabled_inactive_track_color`.
///
/// The track segment between the start of the slider and the thumb is the
/// active track segment. The track segment between the thumb and the end of the
/// slider is the inactive track segment. In `TextDirection::Ltr`, the start of
/// the slider is on the left, and in `TextDirection::Rtl`, the start of the
/// slider is on the right.
///
/// See also:
///
///  * `Slider`, for the component that is meant to display this shape.
///  * [`SliderThemeData`], where an instance of this class is set to inform the
///    slider of the visual details of the its track.
///  * [`SliderTrackShape`], which can be used to create custom shapes for the
///    `Slider`'s track.
///  * [`RoundedRectSliderTrackShape`], for a similar track with rounded edges.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RectangularSliderTrackShape;

impl RectangularSliderTrackShape {
    /// Creates a slider track that draws 2 rectangles.
    pub const fn new() -> RectangularSliderTrackShape {
        RectangularSliderTrackShape
    }
}

impl BaseSliderTrackShape for RectangularSliderTrackShape {}

impl SliderTrackShape for RectangularSliderTrackShape {
    fn get_preferred_rect(
        &self,
        app: &App,
        parent_box: AnyRenderBox,
        offset: Offset,
        slider_theme: &SliderThemeData,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
    ) -> Rect {
        BaseSliderTrackShape::get_preferred_rect(
            self,
            app,
            parent_box,
            offset,
            slider_theme,
            is_enabled,
            is_discrete,
        )
    }

    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        enable_animation: AnyAnimation<f64>,
        thumb_center: Offset,
        secondary_offset: Option<Offset>,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
        text_direction: TextDirection,
    ) {
        let is_discrete = is_discrete.unwrap_or(false);
        let is_enabled = is_enabled.unwrap_or(false);
        debug_assert!(slider_theme.disabled_active_track_color.is_some());
        debug_assert!(slider_theme.disabled_inactive_track_color.is_some());
        debug_assert!(slider_theme.active_track_color.is_some());
        debug_assert!(slider_theme.inactive_track_color.is_some());
        debug_assert!(slider_theme.thumb_shape.is_some());
        // If the slider [SliderThemeData.trackHeight] is less than or equal to 0,
        // then it makes no difference whether the track is painted or not,
        // therefore the painting can be a no-op.
        if slider_theme
            .track_height
            .expect("a slider theme with a track height")
            <= 0.0
        {
            return;
        }

        // Assign the track segment paints, which are left: active, right: inactive,
        // but reversed for right to left text.
        let active_paint = Paint::from_color(
            evaluate_color_tween(
                app,
                slider_theme.disabled_active_track_color,
                slider_theme.active_track_color,
                enable_animation,
            )
            .expect("both track colors are set")
            .into(),
        );
        let inactive_paint = Paint::from_color(
            evaluate_color_tween(
                app,
                slider_theme.disabled_inactive_track_color,
                slider_theme.inactive_track_color,
                enable_animation,
            )
            .expect("both track colors are set")
            .into(),
        );
        let (left_track_paint, right_track_paint) = match text_direction {
            TextDirection::Ltr => (&active_paint, &inactive_paint),
            TextDirection::Rtl => (&inactive_paint, &active_paint),
        };

        let track_rect = SliderTrackShape::get_preferred_rect(
            self,
            app,
            parent_box,
            offset,
            slider_theme,
            Some(is_enabled),
            Some(is_discrete),
        );

        let left_track_segment = Rect::from_ltrb(
            track_rect.left,
            track_rect.top,
            thumb_center.dx(),
            track_rect.bottom,
        );
        if !left_track_segment.is_empty() {
            context
                .canvas()
                .draw_rect(left_track_segment, left_track_paint);
        }
        let right_track_segment = Rect::from_ltrb(
            thumb_center.dx(),
            track_rect.top,
            track_rect.right,
            track_rect.bottom,
        );
        if !right_track_segment.is_empty() {
            context
                .canvas()
                .draw_rect(right_track_segment, right_track_paint);
        }

        let show_secondary_track =
            secondary_offset.is_some_and(|secondary_offset| match text_direction {
                TextDirection::Rtl => secondary_offset.dx() < thumb_center.dx(),
                TextDirection::Ltr => secondary_offset.dx() > thumb_center.dx(),
            });

        if let Some(secondary_offset) = secondary_offset.filter(|_| show_secondary_track) {
            let secondary_track_paint = Paint::from_color(
                evaluate_color_tween(
                    app,
                    slider_theme.disabled_secondary_active_track_color,
                    slider_theme.secondary_active_track_color,
                    enable_animation,
                )
                .expect("both secondary track colors are set")
                .into(),
            );
            let secondary_track_segment = match text_direction {
                TextDirection::Rtl => Rect::from_ltrb(
                    secondary_offset.dx(),
                    track_rect.top,
                    thumb_center.dx(),
                    track_rect.bottom,
                ),
                TextDirection::Ltr => Rect::from_ltrb(
                    thumb_center.dx(),
                    track_rect.top,
                    secondary_offset.dx(),
                    track_rect.bottom,
                ),
            };
            if !secondary_track_segment.is_empty() {
                context
                    .canvas()
                    .draw_rect(secondary_track_segment, &secondary_track_paint);
            }
        }
    }

    fn is_rounded(&self) -> bool {
        BaseSliderTrackShape::is_rounded(self)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Value equality, as Dart's canonicalized `const` instances are identical.
    fn eq_shape(&self, other: &dyn SliderTrackShape) -> bool {
        other.as_any().downcast_ref::<Self>() == Some(self)
    }
}

/// Dart's `_TrackSegment`: a horizontal span of the track, and whether it is wide enough to
/// be painted.
#[derive(Clone, Copy)]
struct TrackSegment {
    draw: bool,
    left: f64,
    right: f64,
}

/// The default shape of a `Slider`'s track.
///
/// It paints a solid colored rectangle with rounded edges, vertically centered
/// in the `parent_box`. The track rectangle extends to the bounds of the
/// `parent_box`, but is padded by the larger of `RoundSliderOverlayShape`'s
/// radius and [`RoundSliderThumbShape`]'s radius. The height is defined by the
/// `SliderThemeData::track_height`. The color is determined by the `Slider`'s
/// enabled state and the track segment's active state which are defined by:
///   `SliderThemeData::active_track_color`,
///   `SliderThemeData::inactive_track_color`,
///   `SliderThemeData::disabled_active_track_color`,
///   `SliderThemeData::disabled_inactive_track_color`.
///
/// The track segment between the start of the slider and the thumb is the
/// active track segment. The track segment between the thumb and the end of the
/// slider is the inactive track segment. In `TextDirection::Ltr`, the start of
/// the slider is on the left, and in `TextDirection::Rtl`, the start of the
/// slider is on the right.
///
/// See also:
///
///  * `Slider`, for the component that is meant to display this shape.
///  * [`SliderThemeData`], where an instance of this class is set to inform the
///    slider of the visual details of the its track.
///  * [`SliderTrackShape`], which can be used to create custom shapes for the
///    `Slider`'s track.
///  * [`RectangularSliderTrackShape`], for a similar track with sharp edges.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RoundedRectSliderTrackShape;

impl RoundedRectSliderTrackShape {
    /// Create a slider track that draws two rectangles with rounded outer edges.
    pub const fn new() -> RoundedRectSliderTrackShape {
        RoundedRectSliderTrackShape
    }

    /// Dart's `paint` with its extra `additionalActiveTrackHeight` argument, which the
    /// [`SliderTrackShape::paint`] override passes as Dart's default, 2.
    #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
    pub fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        enable_animation: AnyAnimation<f64>,
        text_direction: TextDirection,
        thumb_center: Offset,
        secondary_offset: Option<Offset>,
        is_discrete: bool,
        is_enabled: bool,
        additional_active_track_height: f64,
    ) {
        debug_assert!(slider_theme.disabled_active_track_color.is_some());
        debug_assert!(slider_theme.disabled_inactive_track_color.is_some());
        debug_assert!(slider_theme.active_track_color.is_some());
        debug_assert!(slider_theme.inactive_track_color.is_some());
        debug_assert!(slider_theme.thumb_shape.is_some());
        // If the slider [SliderThemeData.trackHeight] is less than or equal to 0,
        // then it makes no difference whether the track is painted or not,
        // therefore the painting can be a no-op.
        let Some(track_height) = slider_theme.track_height.filter(|height| *height > 0.0) else {
            return;
        };

        // Assign the track segment paints, which are leading: active and
        // trailing: inactive.
        let active_paint = Paint::from_color(
            evaluate_color_tween(
                app,
                slider_theme.disabled_active_track_color,
                slider_theme.active_track_color,
                enable_animation,
            )
            .expect("both track colors are set")
            .into(),
        );
        let inactive_paint = Paint::from_color(
            evaluate_color_tween(
                app,
                slider_theme.disabled_inactive_track_color,
                slider_theme.inactive_track_color,
                enable_animation,
            )
            .expect("both track colors are set")
            .into(),
        );

        let track_rect = SliderTrackShape::get_preferred_rect(
            self,
            app,
            parent_box,
            offset,
            slider_theme,
            Some(is_enabled),
            Some(is_discrete),
        );
        let track_radius = Radius::circular(track_rect.height() / 2.0);
        let active_track_radius =
            Radius::circular((track_rect.height() + additional_active_track_height) / 2.0);
        let is_ltr = text_direction == TextDirection::Ltr;

        // Both segments extend past the thumb center by half of the track height.
        let half_track_height = track_height / 2.0;
        let left_track = TrackSegment {
            draw: thumb_center.dx() > track_rect.left + half_track_height,
            left: track_rect.left,
            right: thumb_center.dx() + half_track_height,
        };
        let right_track = TrackSegment {
            draw: thumb_center.dx() < track_rect.right - half_track_height,
            left: thumb_center.dx() - half_track_height,
            right: track_rect.right,
        };
        let (inactive_track, active_track) = if is_ltr {
            (right_track, left_track)
        } else {
            (left_track, right_track)
        };

        // The segment painted last has its rounded end drawn on top of the other one,
        // so paint the inactive segment first to keep the active one on top in both
        // text directions.
        if inactive_track.draw {
            draw_rrect(
                context.canvas(),
                RRect::from_ltrbr(
                    inactive_track.left,
                    track_rect.top,
                    inactive_track.right,
                    track_rect.bottom,
                    track_radius,
                ),
                &inactive_paint,
            );
        }
        if active_track.draw {
            draw_rrect(
                context.canvas(),
                RRect::from_ltrbr(
                    active_track.left,
                    track_rect.top - (additional_active_track_height / 2.0),
                    active_track.right,
                    track_rect.bottom + (additional_active_track_height / 2.0),
                    active_track_radius,
                ),
                &active_paint,
            );
        }

        let show_secondary_track = secondary_offset.is_some_and(|secondary_offset| {
            if is_ltr {
                secondary_offset.dx() > thumb_center.dx()
            } else {
                secondary_offset.dx() < thumb_center.dx()
            }
        });

        if let Some(secondary_offset) = secondary_offset.filter(|_| show_secondary_track) {
            let secondary_track_paint = Paint::from_color(
                evaluate_color_tween(
                    app,
                    slider_theme.disabled_secondary_active_track_color,
                    slider_theme.secondary_active_track_color,
                    enable_animation,
                )
                .expect("both secondary track colors are set")
                .into(),
            );
            if is_ltr {
                draw_rrect(
                    context.canvas(),
                    RRect::from_ltrb_and_corners(
                        thumb_center.dx(),
                        track_rect.top,
                        secondary_offset.dx(),
                        track_rect.bottom,
                        Radius::ZERO,
                        track_radius,
                        track_radius,
                        Radius::ZERO,
                    ),
                    &secondary_track_paint,
                );
            } else {
                draw_rrect(
                    context.canvas(),
                    RRect::from_ltrb_and_corners(
                        secondary_offset.dx(),
                        track_rect.top,
                        thumb_center.dx(),
                        track_rect.bottom,
                        track_radius,
                        Radius::ZERO,
                        Radius::ZERO,
                        track_radius,
                    ),
                    &secondary_track_paint,
                );
            }
        }
    }
}

impl BaseSliderTrackShape for RoundedRectSliderTrackShape {}

impl SliderTrackShape for RoundedRectSliderTrackShape {
    fn get_preferred_rect(
        &self,
        app: &App,
        parent_box: AnyRenderBox,
        offset: Offset,
        slider_theme: &SliderThemeData,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
    ) -> Rect {
        BaseSliderTrackShape::get_preferred_rect(
            self,
            app,
            parent_box,
            offset,
            slider_theme,
            is_enabled,
            is_discrete,
        )
    }

    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        enable_animation: AnyAnimation<f64>,
        thumb_center: Offset,
        secondary_offset: Option<Offset>,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
        text_direction: TextDirection,
    ) {
        RoundedRectSliderTrackShape::paint(
            self,
            app,
            context,
            offset,
            parent_box,
            slider_theme,
            enable_animation,
            text_direction,
            thumb_center,
            secondary_offset,
            is_discrete.unwrap_or(false),
            is_enabled.unwrap_or(false),
            2.0,
        );
    }

    fn is_rounded(&self) -> bool {
        true
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Value equality, as Dart's canonicalized `const` instances are identical.
    fn eq_shape(&self, other: &dyn SliderTrackShape) -> bool {
        other.as_any().downcast_ref::<Self>() == Some(self)
    }
}

/// The default shape of each `Slider` tick mark.
///
/// Tick marks are only displayed if the slider is discrete, which can be done
/// by setting the `Slider::divisions` to an integer value.
///
/// It paints a solid circle, centered in the on the track.
/// The color is determined by the `Slider`'s enabled state and track's active
/// states. These colors are defined in:
///   `SliderThemeData::active_track_color`,
///   `SliderThemeData::inactive_track_color`,
///   `SliderThemeData::disabled_active_track_color`,
///   `SliderThemeData::disabled_inactive_track_color`.
///
/// See also:
///
///  * `Slider`, which includes tick marks defined by this shape.
///  * `SliderTheme`, which can be used to configure the tick mark shape of all
///    sliders in a widget subtree.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RoundSliderTickMarkShape {
    /// The preferred radius of the round tick mark.
    ///
    /// If it is not provided, then 1/4 of the `SliderThemeData::track_height` is used.
    pub tick_mark_radius: Option<f64>,
}

impl RoundSliderTickMarkShape {
    /// Create a slider tick mark that draws a circle.
    pub const fn new() -> RoundSliderTickMarkShape {
        RoundSliderTickMarkShape {
            tick_mark_radius: None,
        }
    }

    /// Dart `RoundSliderTickMarkShape(tickMarkRadius:)`.
    pub const fn tick_mark_radius(mut self, tick_mark_radius: f64) -> RoundSliderTickMarkShape {
        self.tick_mark_radius = Some(tick_mark_radius);
        self
    }
}

impl SliderTickMarkShape for RoundSliderTickMarkShape {
    fn get_preferred_size(&self, slider_theme: &SliderThemeData, _is_enabled: bool) -> Size {
        debug_assert!(slider_theme.track_height.is_some());
        // The tick marks are tiny circles. If no radius is provided, then the
        // radius is defaulted to be a fraction of the
        // [SliderThemeData.trackHeight]. The fraction is 1/4.
        Size::from_radius(self.tick_mark_radius.unwrap_or_else(|| {
            slider_theme
                .track_height
                .expect("a slider theme with a track height")
                / 4.0
        }))
    }

    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        center: Offset,
        _parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        enable_animation: AnyAnimation<f64>,
        thumb_center: Offset,
        is_enabled: bool,
        text_direction: TextDirection,
    ) {
        debug_assert!(slider_theme.disabled_active_tick_mark_color.is_some());
        debug_assert!(slider_theme.disabled_inactive_tick_mark_color.is_some());
        debug_assert!(slider_theme.active_tick_mark_color.is_some());
        debug_assert!(slider_theme.inactive_tick_mark_color.is_some());
        // The paint color of the tick mark depends on its position relative
        // to the thumb and the text direction.
        let x_offset = center.dx() - thumb_center.dx();
        let (begin, end) = match text_direction {
            TextDirection::Ltr if x_offset > 0.0 => (
                slider_theme.disabled_inactive_tick_mark_color,
                slider_theme.inactive_tick_mark_color,
            ),
            TextDirection::Rtl if x_offset < 0.0 => (
                slider_theme.disabled_inactive_tick_mark_color,
                slider_theme.inactive_tick_mark_color,
            ),
            TextDirection::Ltr | TextDirection::Rtl => (
                slider_theme.disabled_active_tick_mark_color,
                slider_theme.active_tick_mark_color,
            ),
        };
        let paint = Paint::from_color(
            evaluate_color_tween(app, begin, end, enable_animation)
                .expect("both tick mark colors are set")
                .into(),
        );

        // The tick marks are tiny circles that are the same height as the track.
        let tick_mark_radius = self.get_preferred_size(slider_theme, is_enabled).width() / 2.0;
        if tick_mark_radius > 0.0 {
            context
                .canvas()
                .draw_circle(center, tick_mark_radius as f32, &paint);
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Value equality, as Dart's canonicalized `const` instances are identical.
    fn eq_shape(&self, other: &dyn SliderTickMarkShape) -> bool {
        other.as_any().downcast_ref::<Self>() == Some(self)
    }
}

/// A special version of [`SliderTickMarkShape`] that has a zero size and paints
/// nothing.
///
/// This class is used to create a special instance of a [`SliderTickMarkShape`]
/// that will not paint any tick mark shape. A static reference is stored in
/// [`no_tick_mark`](<dyn SliderTickMarkShape>::no_tick_mark). When this value is specified for
/// `SliderThemeData::tick_mark_shape`, the tick mark painting is skipped.
#[derive(Debug)]
struct EmptySliderTickMarkShape;

impl SliderTickMarkShape for EmptySliderTickMarkShape {
    fn get_preferred_size(&self, _slider_theme: &SliderThemeData, _is_enabled: bool) -> Size {
        Size::ZERO
    }

    fn paint(
        &self,
        _app: &mut App,
        _context: &mut PaintingContext,
        _center: Offset,
        _parent_box: AnyRenderBox,
        _slider_theme: &SliderThemeData,
        _enable_animation: AnyAnimation<f64>,
        _thumb_center: Offset,
        _is_enabled: bool,
        _text_direction: TextDirection,
    ) {
        // no-op.
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// The default shape of a `Slider`'s thumb.
///
/// There is a shadow for the resting, pressed, hovered, and focused state.
///
/// See also:
///
///  * `Slider`, which includes a thumb defined by this shape.
///  * `SliderTheme`, which can be used to configure the thumb shape of all
///    sliders in a widget subtree.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RoundSliderThumbShape {
    /// The preferred radius of the round thumb shape when the slider is enabled.
    ///
    /// If it is not provided, then the Material Design default of 10 is used.
    pub enabled_thumb_radius: f64,

    /// The preferred radius of the round thumb shape when the slider is disabled.
    ///
    /// If no disabledRadius is provided, then it is equal to the
    /// [`enabled_thumb_radius`](Self::enabled_thumb_radius)
    pub disabled_thumb_radius: Option<f64>,

    /// The resting elevation adds shadow to the unpressed thumb.
    ///
    /// The default is 1.
    ///
    /// Use 0 for no shadow. The higher the value, the larger the shadow. For
    /// example, a value of 12 will create a very large shadow.
    pub elevation: f64,

    /// The pressed elevation adds shadow to the pressed thumb.
    ///
    /// The default is 6.
    ///
    /// Use 0 for no shadow. The higher the value, the larger the shadow. For
    /// example, a value of 12 will create a very large shadow.
    pub pressed_elevation: f64,
}

impl RoundSliderThumbShape {
    /// Create a slider thumb that draws a circle.
    pub const fn new() -> RoundSliderThumbShape {
        RoundSliderThumbShape {
            enabled_thumb_radius: 10.0,
            disabled_thumb_radius: None,
            elevation: 1.0,
            pressed_elevation: 6.0,
        }
    }

    /// Dart `RoundSliderThumbShape(enabledThumbRadius:)`.
    pub const fn enabled_thumb_radius(
        mut self,
        enabled_thumb_radius: f64,
    ) -> RoundSliderThumbShape {
        self.enabled_thumb_radius = enabled_thumb_radius;
        self
    }

    /// Dart `RoundSliderThumbShape(disabledThumbRadius:)`.
    pub const fn disabled_thumb_radius(
        mut self,
        disabled_thumb_radius: f64,
    ) -> RoundSliderThumbShape {
        self.disabled_thumb_radius = Some(disabled_thumb_radius);
        self
    }

    /// Dart `RoundSliderThumbShape(elevation:)`.
    pub const fn elevation(mut self, elevation: f64) -> RoundSliderThumbShape {
        self.elevation = elevation;
        self
    }

    /// Dart `RoundSliderThumbShape(pressedElevation:)`.
    pub const fn pressed_elevation(mut self, pressed_elevation: f64) -> RoundSliderThumbShape {
        self.pressed_elevation = pressed_elevation;
        self
    }

    /// Dart's `_disabledThumbRadius`.
    fn effective_disabled_thumb_radius(&self) -> f64 {
        self.disabled_thumb_radius
            .unwrap_or(self.enabled_thumb_radius)
    }
}

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

impl SliderComponentShape for RoundSliderThumbShape {
    fn get_preferred_size(&self, is_enabled: bool, _is_discrete: bool) -> Size {
        Size::from_radius(if is_enabled {
            self.enabled_thumb_radius
        } else {
            self.effective_disabled_thumb_radius()
        })
    }

    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        center: Offset,
        activation_animation: AnyAnimation<f64>,
        enable_animation: AnyAnimation<f64>,
        _is_discrete: bool,
        _label_painter: &mut TextPainter,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        _text_direction: TextDirection,
        _value: f64,
        _text_scale_factor: f64,
        _size_with_overflow: Size,
    ) {
        debug_assert!(slider_theme.disabled_thumb_color.is_some());
        debug_assert!(slider_theme.thumb_color.is_some());

        let color = evaluate_color_tween(
            app,
            slider_theme.disabled_thumb_color,
            slider_theme.thumb_color,
            enable_animation,
        )
        .expect("both thumb colors are set");
        let radius = evaluate_tween(
            app,
            self.effective_disabled_thumb_radius(),
            self.enabled_thumb_radius,
            enable_animation,
        );

        let evaluated_elevation = evaluate_tween(
            app,
            self.elevation,
            self.pressed_elevation,
            activation_animation,
        );
        let mut path = PathBuilder::new();
        path.arc(center, radius as f32, 0.0, std::f32::consts::TAU);
        let path = Path::new(path.build());

        draw_shadow(
            app,
            context.canvas(),
            parent_box,
            &path,
            Colors::BLACK,
            evaluated_elevation,
            true,
        );

        context
            .canvas()
            .draw_circle(center, radius as f32, &Paint::from_color(color.into()));
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Value equality, as Dart's canonicalized `const` instances are identical.
    fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
        other.as_any().downcast_ref::<Self>() == Some(self)
    }
}

/// The default shape of a Material 3 `Slider`'s value indicator.
///
/// See also:
///
///  * `Slider`, which includes a value indicator defined by this shape.
///  * `SliderTheme`, which can be used to configure the slider value indicator
///    of all sliders in a widget subtree.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct DropSliderValueIndicatorShape;

impl DropSliderValueIndicatorShape {
    const PATH_PAINTER: DropSliderValueIndicatorPathPainter = DropSliderValueIndicatorPathPainter;

    /// Create a slider value indicator that resembles a drop shape.
    pub const fn new() -> DropSliderValueIndicatorShape {
        DropSliderValueIndicatorShape
    }

    /// Dart's `getPreferredSize` with its optional `labelPainter` and `textScaleFactor`,
    /// which this shape needs.
    pub fn get_preferred_size(
        &self,
        _is_enabled: bool,
        _is_discrete: bool,
        label_painter: Option<&TextPainter>,
        text_scale_factor: Option<f64>,
    ) -> Size {
        debug_assert!(label_painter.is_some());
        debug_assert!(text_scale_factor.is_some_and(|factor| factor >= 0.0));
        Self::PATH_PAINTER.get_preferred_size(
            label_painter.expect("a label painter"),
            text_scale_factor.expect("a text scale factor"),
        )
    }
}

impl SliderComponentShape for DropSliderValueIndicatorShape {
    fn get_preferred_size(&self, is_enabled: bool, is_discrete: bool) -> Size {
        DropSliderValueIndicatorShape::get_preferred_size(self, is_enabled, is_discrete, None, None)
    }

    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        center: Offset,
        activation_animation: AnyAnimation<f64>,
        _enable_animation: AnyAnimation<f64>,
        _is_discrete: bool,
        label_painter: &mut TextPainter,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        _text_direction: TextDirection,
        _value: f64,
        text_scale_factor: f64,
        size_with_overflow: Size,
    ) {
        let scale = activation_animation.value(app);
        Self::PATH_PAINTER.paint(
            app,
            parent_box,
            context.canvas(),
            center,
            scale,
            label_painter,
            text_scale_factor,
            size_with_overflow,
            slider_theme
                .value_indicator_color
                .expect("a slider theme with a value indicator color"),
            slider_theme.value_indicator_stroke_color,
        );
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Value equality, as Dart's canonicalized `const` instances are identical.
    fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
        other.as_any().downcast_ref::<Self>() == Some(self)
    }
}

/// Dart's `_DropSliderValueIndicatorPathPainter`.
struct DropSliderValueIndicatorPathPainter;

impl DropSliderValueIndicatorPathPainter {
    const TRIANGLE_HEIGHT: f64 = 10.0;
    const LABEL_PADDING: f64 = 8.0;
    const PREFERRED_HEIGHT: f64 = 32.0;
    const MIN_LABEL_WIDTH: f64 = 20.0;
    const MIN_RECT_HEIGHT: f64 = 28.0;
    const RECT_Y_OFFSET: f64 = 6.0;
    const BOTTOM_TIP_Y_OFFSET: f64 = 16.0;
    const PREFERRED_HALF_HEIGHT: f64 = Self::PREFERRED_HEIGHT / 2.0;
    const UPPER_RECT_RADIUS: f64 = 4.0;

    fn get_preferred_size(&self, label_painter: &TextPainter, text_scale_factor: f64) -> Size {
        let width = Self::MIN_LABEL_WIDTH.max(label_painter.width())
            + Self::LABEL_PADDING * 2.0 * text_scale_factor;
        Size::new(width, Self::PREFERRED_HEIGHT * text_scale_factor)
    }

    #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
    fn get_horizontal_shift(
        &self,
        app: &App,
        parent_box: AnyRenderBox,
        center: Offset,
        label_painter: &TextPainter,
        text_scale_factor: f64,
        size_with_overflow: Size,
        scale: f64,
    ) -> f64 {
        debug_assert!(!size_with_overflow.is_empty());

        const EDGE_PADDING: f64 = 8.0;
        let rectangle_width = self.upper_rectangle_width(label_painter, scale);

        // Value indicator draws on the Overlay and by using the global Offset
        // we are making sure we use the bounds of the Overlay instead of the Slider.
        let global_center = parent_box.local_to_global(app, center, None);

        // The rectangle must be shifted towards the center so that it minimizes the
        // chance of it rendering outside the bounds of the render box. If the shift
        // is negative, then the lobe is shifted from right to left, and if it is
        // positive, then the lobe is shifted from left to right.
        let overflow_left = 0.0_f64.max(rectangle_width / 2.0 - global_center.dx() + EDGE_PADDING);
        let overflow_right = 0.0_f64.max(
            rectangle_width / 2.0
                - (size_with_overflow.width() - global_center.dx() - EDGE_PADDING),
        );

        if rectangle_width < size_with_overflow.width() {
            overflow_left - overflow_right
        } else if overflow_left - overflow_right > 0.0 {
            overflow_left - (EDGE_PADDING * text_scale_factor)
        } else {
            -overflow_right + (EDGE_PADDING * text_scale_factor)
        }
    }

    fn upper_rectangle_width(&self, label_painter: &TextPainter, scale: f64) -> f64 {
        let unscaled_width = Self::MIN_LABEL_WIDTH.max(label_painter.width()) + Self::LABEL_PADDING;
        unscaled_width * scale
    }

    fn adjust_border_radius(&self, rect: Rect) -> BorderRadius {
        const RECTNESS: f64 = 0.0;
        BorderRadius::lerp(
            Some(BorderRadius::all(Radius::circular(Self::UPPER_RECT_RADIUS))),
            Some(BorderRadius::all(Radius::circular(
                rect.shortest_side() / 2.0,
            ))),
            1.0 - RECTNESS,
        )
        .expect("both radii are set")
    }

    #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
    fn paint(
        &self,
        app: &mut App,
        parent_box: AnyRenderBox,
        canvas: &mut Canvas,
        center: Offset,
        scale: f64,
        label_painter: &mut TextPainter,
        text_scale_factor: f64,
        size_with_overflow: Size,
        background_paint_color: Color,
        stroke_paint_color: Option<Color>,
    ) {
        if scale == 0.0 {
            // Zero scale essentially means "do not draw anything", so it's safe to just return.
            return;
        }
        debug_assert!(!size_with_overflow.is_empty());
        let rectangle_width = self.upper_rectangle_width(label_painter, scale);
        let horizontal_shift = self.get_horizontal_shift(
            app,
            parent_box,
            center,
            label_painter,
            text_scale_factor,
            size_with_overflow,
            scale,
        );
        let upper_rect = Rect::from_ltwh(
            -rectangle_width / 2.0 + horizontal_shift,
            -Self::RECT_Y_OFFSET - Self::MIN_RECT_HEIGHT,
            rectangle_width,
            Self::MIN_RECT_HEIGHT,
        );

        let fill_paint = Paint::from_color(background_paint_color.into());

        canvas.save();
        canvas.translate(
            center.dx() as f32,
            (center.dy() - Self::BOTTOM_TIP_Y_OFFSET) as f32,
        );
        canvas.scale(scale as f32, scale as f32);

        let adjusted_border_radius = self.adjust_border_radius(upper_rect);
        let border_rect = adjusted_border_radius
            .resolve(label_painter.text_direction())
            .to_rrect(upper_rect);
        let mut triangle_path = PathBuilder::new();
        // Dart's `lineTo` on an empty path starts its contour at the origin.
        triangle_path.move_to((0.0, 0.0));
        triangle_path
            .line_to((-Self::TRIANGLE_HEIGHT as f32, -Self::TRIANGLE_HEIGHT as f32))
            .line_to((Self::TRIANGLE_HEIGHT as f32, -Self::TRIANGLE_HEIGHT as f32))
            .close();
        triangle_path.rrect_radii_elliptical(
            border_rect.outer_rect(),
            rrect_radii_elliptical(border_rect),
        );
        let triangle_path = triangle_path.build();

        if let Some(stroke_paint_color) = stroke_paint_color {
            let stroke_paint = Paint {
                color: stroke_paint_color.into(),
                style: PaintStyle::Stroke(Stroke::new(1.0)),
                ..Paint::default()
            };
            canvas.draw_path(&triangle_path, FillRule::NonZero, &stroke_paint);
        }

        canvas.draw_path(&triangle_path, FillRule::NonZero, &fill_paint);

        // The label text is centered within the value indicator.
        let bottom_tip_to_upper_rect_translate_y =
            -Self::PREFERRED_HALF_HEIGHT / 2.0 - upper_rect.height();
        canvas.translate(0.0, bottom_tip_to_upper_rect_translate_y as f32);
        let box_center = Offset::new(horizontal_shift, upper_rect.height() / 1.75);
        let half_label_painter_offset =
            Offset::new(label_painter.width() / 2.0, label_painter.height() / 2.0);
        let label_offset = box_center - half_label_painter_offset;
        let fonts = PaintingBinding::instance(app).fonts(app);
        label_painter.paint(app.get_mut(fonts), canvas, label_offset);
        canvas.restore();
    }
}

/// The bar shape of a `Slider`'s thumb.
///
/// When the slider is enabled, the `ColorScheme::primary` color is used for the
/// thumb. When the slider is disabled, the `ColorScheme::on_surface` color with an
/// opacity of 0.38 is used for the thumb.
///
/// The thumb bar shape width is reduced when the thumb is pressed.
///
/// If `SliderThemeData::thumb_size` is null, then the thumb size is 4 pixels for the width
/// and 44 pixels for the height.
///
/// This is the default thumb shape for `Slider`. If `ThemeData::use_material3` is false,
/// then the default thumb shape is [`RoundSliderThumbShape`].
///
/// See also:
///
///  * `Slider`, which includes an overlay defined by this shape.
///  * `SliderTheme`, which can be used to configure the overlay shape of all
///    sliders in a widget subtree.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct HandleThumbShape;

impl HandleThumbShape {
    /// Create a slider thumb that draws a bar.
    pub const fn new() -> HandleThumbShape {
        HandleThumbShape
    }
}

impl SliderComponentShape for HandleThumbShape {
    fn get_preferred_size(&self, _is_enabled: bool, _is_discrete: bool) -> Size {
        Size::new(4.0, 44.0)
    }

    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        center: Offset,
        _activation_animation: AnyAnimation<f64>,
        enable_animation: AnyAnimation<f64>,
        _is_discrete: bool,
        _label_painter: &mut TextPainter,
        _parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        _text_direction: TextDirection,
        _value: f64,
        _text_scale_factor: f64,
        _size_with_overflow: Size,
    ) {
        debug_assert!(slider_theme.disabled_thumb_color.is_some());
        debug_assert!(slider_theme.thumb_color.is_some());
        debug_assert!(slider_theme.thumb_size.is_some());

        let color = evaluate_color_tween(
            app,
            slider_theme.disabled_thumb_color,
            slider_theme.thumb_color,
            enable_animation,
        )
        .expect("both thumb colors are set");

        let thumb_size = slider_theme
            .thumb_size
            .as_ref()
            .expect("a slider theme with a thumb size")
            .resolve(&WidgetStates::new())
            .expect("a thumb size for no states"); // This is resolved in the paint method.
        let rrect = RRect::from_rect_and_radius(
            Rect::from_center(center, thumb_size.width(), thumb_size.height()),
            Radius::circular(thumb_size.shortest_side() / 2.0),
        );
        draw_rrect(context.canvas(), rrect, &Paint::from_color(color.into()));
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Value equality, as Dart's canonicalized `const` instances are identical.
    fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
        other.as_any().downcast_ref::<Self>() == Some(self)
    }
}

/// The gapped shape of a `Slider`'s track.
///
/// The [`GappedSliderTrackShape`] consists of active and inactive
/// tracks. The active track uses the `SliderThemeData::active_track_color` and the
/// inactive track uses the `SliderThemeData::inactive_track_color`.
///
/// The track shape uses circular corner radius for the edge corners and a corner radius
/// of 2 pixels for the inside corners.
///
/// Between the active and inactive tracks there is a gap of size
/// `SliderThemeData::track_gap`. If the `SliderThemeData::thumb_shape` is
/// [`HandleThumbShape`] and the thumb is pressed, the thumb's width is reduced; as a result,
/// the track gap size in [`GappedSliderTrackShape`] is also reduced.
///
/// If `SliderThemeData::track_gap` is null, then the track gap size defaults to 6 pixels.
///
/// This is the default track shape for `Slider`. If `ThemeData::use_material3` is false,
/// then the default track shape is [`RoundedRectSliderTrackShape`].
///
/// See also:
///
///  * `Slider`, which includes an overlay defined by this shape.
///  * `SliderTheme`, which can be used to configure the overlay shape of all
///    sliders in a widget subtree.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct GappedSliderTrackShape;

impl GappedSliderTrackShape {
    /// Create a slider track that draws two rectangles with rounded outer edges.
    pub const fn new() -> GappedSliderTrackShape {
        GappedSliderTrackShape
    }

    /// Dart's `paint` with its extra `additionalActiveTrackHeight` argument, which the
    /// [`SliderTrackShape::paint`] override passes as Dart's default, 2.
    #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
    pub fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        enable_animation: AnyAnimation<f64>,
        text_direction: TextDirection,
        thumb_center: Offset,
        secondary_offset: Option<Offset>,
        is_discrete: bool,
        is_enabled: bool,
        _additional_active_track_height: f64,
    ) {
        debug_assert!(slider_theme.disabled_active_track_color.is_some());
        debug_assert!(slider_theme.disabled_inactive_track_color.is_some());
        debug_assert!(slider_theme.active_track_color.is_some());
        debug_assert!(slider_theme.inactive_track_color.is_some());
        debug_assert!(slider_theme.thumb_shape.is_some());
        debug_assert!(slider_theme.track_gap.is_some());
        debug_assert!(
            slider_theme
                .track_gap
                .is_some_and(|gap| !gap.is_sign_negative())
        );
        // If the slider [SliderThemeData.trackHeight] is less than or equal to 0,
        // then it makes no difference whether the track is painted or not,
        // therefore the painting can be a no-op.
        let Some(track_height) = slider_theme.track_height.filter(|height| *height > 0.0) else {
            return;
        };

        // Assign the track segment paints, which are left: active, right: inactive,
        // but reversed for right to left text.
        let active_paint = Paint::from_color(
            evaluate_color_tween(
                app,
                slider_theme.disabled_active_track_color,
                slider_theme.active_track_color,
                enable_animation,
            )
            .expect("both track colors are set")
            .into(),
        );
        let inactive_paint = Paint::from_color(
            evaluate_color_tween(
                app,
                slider_theme.disabled_inactive_track_color,
                slider_theme.inactive_track_color,
                enable_animation,
            )
            .expect("both track colors are set")
            .into(),
        );
        let (left_track_paint, right_track_paint) = match text_direction {
            TextDirection::Ltr => (&active_paint, &inactive_paint),
            TextDirection::Rtl => (&inactive_paint, &active_paint),
        };

        // Gap, starting from the middle of the thumb.
        let track_gap = slider_theme
            .track_gap
            .expect("a slider theme with a track gap");

        let track_rect = SliderTrackShape::get_preferred_rect(
            self,
            app,
            parent_box,
            offset,
            slider_theme,
            Some(is_enabled),
            Some(is_discrete),
        );

        let track_corner_radius = Radius::circular(track_rect.shortest_side() / 2.0);
        const TRACK_INSIDE_CORNER_RADIUS: Radius = Radius::circular(2.0);

        let track_rrect = RRect::from_rect_and_corners(
            track_rect,
            track_corner_radius,
            track_corner_radius,
            track_corner_radius,
            track_corner_radius,
        );

        let left_rrect = RRect::from_ltrb_and_corners(
            track_rect.left,
            track_rect.top,
            track_rect.left.max(thumb_center.dx() - track_gap),
            track_rect.bottom,
            track_corner_radius,
            TRACK_INSIDE_CORNER_RADIUS,
            TRACK_INSIDE_CORNER_RADIUS,
            track_corner_radius,
        );

        let right_rrect = RRect::from_ltrb_and_corners(
            thumb_center.dx() + track_gap,
            track_rect.top,
            track_rect.right,
            track_rect.bottom,
            TRACK_INSIDE_CORNER_RADIUS,
            track_corner_radius,
            track_corner_radius,
            TRACK_INSIDE_CORNER_RADIUS,
        );

        let canvas = context.canvas();
        canvas.save();
        canvas.clip_rrect_radii_elliptical(
            track_rrect.outer_rect(),
            rrect_radii_elliptical(track_rrect),
            ClipOp::Intersect,
            true,
        );
        let draw_left_track = thumb_center.dx() > (left_rrect.left + (track_height / 2.0));
        let draw_right_track = thumb_center.dx() < (right_rrect.right - (track_height / 2.0));
        if draw_left_track {
            draw_rrect(canvas, left_rrect, left_track_paint);
        }
        if draw_right_track {
            draw_rrect(canvas, right_rrect, right_track_paint);
        }

        let is_ltr = text_direction == TextDirection::Ltr;
        let show_secondary_track = secondary_offset.is_some_and(|secondary_offset| match is_ltr {
            true => secondary_offset.dx() > thumb_center.dx() + track_gap,
            false => secondary_offset.dx() < thumb_center.dx() - track_gap,
        });

        if let Some(secondary_offset) = secondary_offset.filter(|_| show_secondary_track) {
            let secondary_track_paint = Paint::from_color(
                evaluate_color_tween(
                    app,
                    slider_theme.disabled_secondary_active_track_color,
                    slider_theme.secondary_active_track_color,
                    enable_animation,
                )
                .expect("both secondary track colors are set")
                .into(),
            );
            let canvas = context.canvas();
            if is_ltr {
                draw_rrect(
                    canvas,
                    RRect::from_ltrb_and_corners(
                        thumb_center.dx() + track_gap,
                        track_rect.top,
                        secondary_offset.dx(),
                        track_rect.bottom,
                        TRACK_INSIDE_CORNER_RADIUS,
                        track_corner_radius,
                        track_corner_radius,
                        TRACK_INSIDE_CORNER_RADIUS,
                    ),
                    &secondary_track_paint,
                );
            } else {
                draw_rrect(
                    canvas,
                    RRect::from_ltrb_and_corners(
                        secondary_offset.dx(),
                        track_rect.top,
                        thumb_center.dx() - track_gap,
                        track_rect.bottom,
                        track_corner_radius,
                        TRACK_INSIDE_CORNER_RADIUS,
                        TRACK_INSIDE_CORNER_RADIUS,
                        track_corner_radius,
                    ),
                    &secondary_track_paint,
                );
            }
        }
        let canvas = context.canvas();
        canvas.restore();

        const STOP_INDICATOR_RADIUS: f64 = 2.0;
        let stop_indicator_trailing_space = track_height / 2.0;
        let stop_indicator_offset = Offset::new(
            if text_direction == TextDirection::Ltr {
                track_rect.center_right().dx() - stop_indicator_trailing_space
            } else {
                track_rect.center_left().dx() + stop_indicator_trailing_space
            },
            track_rect.center().dy(),
        );

        let show_stop_indicator = if text_direction == TextDirection::Ltr {
            thumb_center.dx() < stop_indicator_offset.dx()
        } else {
            thumb_center.dx() > stop_indicator_offset.dx()
        };
        if show_stop_indicator && !is_discrete {
            let stop_indicator_rect =
                Rect::from_circle(stop_indicator_offset, STOP_INDICATOR_RADIUS);
            canvas.draw_circle(
                stop_indicator_rect.center(),
                STOP_INDICATOR_RADIUS as f32,
                &active_paint,
            );
        }
    }
}

impl BaseSliderTrackShape for GappedSliderTrackShape {}

impl SliderTrackShape for GappedSliderTrackShape {
    fn get_preferred_rect(
        &self,
        app: &App,
        parent_box: AnyRenderBox,
        offset: Offset,
        slider_theme: &SliderThemeData,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
    ) -> Rect {
        BaseSliderTrackShape::get_preferred_rect(
            self,
            app,
            parent_box,
            offset,
            slider_theme,
            is_enabled,
            is_discrete,
        )
    }

    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        enable_animation: AnyAnimation<f64>,
        thumb_center: Offset,
        secondary_offset: Option<Offset>,
        is_enabled: Option<bool>,
        is_discrete: Option<bool>,
        text_direction: TextDirection,
    ) {
        GappedSliderTrackShape::paint(
            self,
            app,
            context,
            offset,
            parent_box,
            slider_theme,
            enable_animation,
            text_direction,
            thumb_center,
            secondary_offset,
            is_discrete.unwrap_or(false),
            is_enabled.unwrap_or(false),
            2.0,
        );
    }

    fn is_rounded(&self) -> bool {
        true
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Value equality, as Dart's canonicalized `const` instances are identical.
    fn eq_shape(&self, other: &dyn SliderTrackShape) -> bool {
        other.as_any().downcast_ref::<Self>() == Some(self)
    }
}

/// The rounded rectangle shape of a `Slider`'s value indicator.
///
/// If the `SliderThemeData::value_indicator_color` is null, then the shape uses the
/// `ColorScheme::inverse_surface` color to draw the value indicator.
///
/// If the `SliderThemeData::value_indicator_text_style` is null, then the indicator label
/// text style defaults to `TextTheme::label_medium` with the color set to
/// `ColorScheme::on_inverse_surface`. If the `ThemeData::use_material3` is set to false, then
/// the indicator label text style defaults to `TextTheme::body_large` with the color set to
/// `ColorScheme::on_inverse_surface`.
///
/// If the `SliderThemeData::value_indicator_stroke_color` is provided, then the value
/// indicator is drawn with a stroke border with the color provided.
///
/// This is the default value indicator shape for `Slider`. If `ThemeData::use_material3` is
/// false, then the default value indicator shape is `RectangularSliderValueIndicatorShape`.
///
/// See also:
///
///  * `Slider`, which includes a value indicator defined by this shape.
///  * `SliderTheme`, which can be used to configure the slider value indicator
///    of all sliders in a widget subtree.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RoundedRectSliderValueIndicatorShape;

impl RoundedRectSliderValueIndicatorShape {
    const PATH_PAINTER: RoundedRectSliderValueIndicatorPathPainter =
        RoundedRectSliderValueIndicatorPathPainter;

    /// Create a slider value indicator that resembles a rounded rectangle.
    pub const fn new() -> RoundedRectSliderValueIndicatorShape {
        RoundedRectSliderValueIndicatorShape
    }

    /// Dart's `getPreferredSize` with its optional `labelPainter` and `textScaleFactor`,
    /// which this shape needs.
    pub fn get_preferred_size(
        &self,
        _is_enabled: bool,
        _is_discrete: bool,
        label_painter: Option<&TextPainter>,
        text_scale_factor: Option<f64>,
    ) -> Size {
        debug_assert!(label_painter.is_some());
        debug_assert!(text_scale_factor.is_some_and(|factor| factor >= 0.0));
        Self::PATH_PAINTER.get_preferred_size(
            label_painter.expect("a label painter"),
            text_scale_factor.expect("a text scale factor"),
        )
    }
}

impl SliderComponentShape for RoundedRectSliderValueIndicatorShape {
    fn get_preferred_size(&self, is_enabled: bool, is_discrete: bool) -> Size {
        RoundedRectSliderValueIndicatorShape::get_preferred_size(
            self,
            is_enabled,
            is_discrete,
            None,
            None,
        )
    }

    fn paint(
        &self,
        app: &mut App,
        context: &mut PaintingContext,
        center: Offset,
        activation_animation: AnyAnimation<f64>,
        _enable_animation: AnyAnimation<f64>,
        _is_discrete: bool,
        label_painter: &mut TextPainter,
        parent_box: AnyRenderBox,
        slider_theme: &SliderThemeData,
        _text_direction: TextDirection,
        _value: f64,
        text_scale_factor: f64,
        size_with_overflow: Size,
    ) {
        let scale = activation_animation.value(app);
        Self::PATH_PAINTER.paint(
            app,
            parent_box,
            context.canvas(),
            center,
            scale,
            label_painter,
            text_scale_factor,
            size_with_overflow,
            slider_theme
                .value_indicator_color
                .expect("a slider theme with a value indicator color"),
            slider_theme.value_indicator_stroke_color,
        );
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Value equality, as Dart's canonicalized `const` instances are identical.
    fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
        other.as_any().downcast_ref::<Self>() == Some(self)
    }
}

/// Dart's `_RoundedRectSliderValueIndicatorPathPainter`.
struct RoundedRectSliderValueIndicatorPathPainter;

impl RoundedRectSliderValueIndicatorPathPainter {
    const LABEL_PADDING: f64 = 10.0;
    const PREFERRED_HEIGHT: f64 = 32.0;
    const MIN_LABEL_WIDTH: f64 = 16.0;
    const RECT_Y_OFFSET: f64 = 10.0;
    const BOTTOM_TIP_Y_OFFSET: f64 = 16.0;
    const PREFERRED_HALF_HEIGHT: f64 = Self::PREFERRED_HEIGHT / 2.0;

    fn get_preferred_size(&self, label_painter: &TextPainter, text_scale_factor: f64) -> Size {
        let width = Self::MIN_LABEL_WIDTH.max(label_painter.width())
            + (Self::LABEL_PADDING * 2.0) * text_scale_factor;
        Size::new(width, Self::PREFERRED_HEIGHT * text_scale_factor)
    }

    #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
    fn get_horizontal_shift(
        &self,
        app: &App,
        parent_box: AnyRenderBox,
        center: Offset,
        label_painter: &TextPainter,
        text_scale_factor: f64,
        size_with_overflow: Size,
        scale: f64,
    ) -> f64 {
        debug_assert!(!size_with_overflow.is_empty());

        const EDGE_PADDING: f64 = 8.0;
        let rectangle_width = self.upper_rectangle_width(label_painter, scale);

        // Value indicator draws on the Overlay and by using the global Offset
        // we are making sure we use the bounds of the Overlay instead of the Slider.
        let global_center = parent_box.local_to_global(app, center, None);

        // The rectangle must be shifted towards the center so that it minimizes the
        // chance of it rendering outside the bounds of the render box. If the shift
        // is negative, then the lobe is shifted from right to left, and if it is
        // positive, then the lobe is shifted from left to right.
        let overflow_left = 0.0_f64.max(rectangle_width / 2.0 - global_center.dx() + EDGE_PADDING);
        let overflow_right = 0.0_f64.max(
            rectangle_width / 2.0
                - (size_with_overflow.width() - global_center.dx() - EDGE_PADDING),
        );

        if rectangle_width < size_with_overflow.width() {
            overflow_left - overflow_right
        } else if overflow_left - overflow_right > 0.0 {
            overflow_left - (EDGE_PADDING * text_scale_factor)
        } else {
            -overflow_right + (EDGE_PADDING * text_scale_factor)
        }
    }

    fn upper_rectangle_width(&self, label_painter: &TextPainter, scale: f64) -> f64 {
        let unscaled_width =
            Self::MIN_LABEL_WIDTH.max(label_painter.width()) + (Self::LABEL_PADDING * 2.0);
        unscaled_width * scale
    }

    #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
    fn paint(
        &self,
        app: &mut App,
        parent_box: AnyRenderBox,
        canvas: &mut Canvas,
        center: Offset,
        scale: f64,
        label_painter: &mut TextPainter,
        text_scale_factor: f64,
        size_with_overflow: Size,
        background_paint_color: Color,
        stroke_paint_color: Option<Color>,
    ) {
        if scale == 0.0 {
            // Zero scale essentially means "do not draw anything", so it's safe to just return.
            return;
        }
        debug_assert!(!size_with_overflow.is_empty());

        let rectangle_width = self.upper_rectangle_width(label_painter, scale);
        let horizontal_shift = self.get_horizontal_shift(
            app,
            parent_box,
            center,
            label_painter,
            text_scale_factor,
            size_with_overflow,
            scale,
        );

        let upper_rect = Rect::from_ltwh(
            -rectangle_width / 2.0 + horizontal_shift,
            -Self::RECT_Y_OFFSET - Self::PREFERRED_HEIGHT,
            rectangle_width,
            Self::PREFERRED_HEIGHT,
        );

        let fill_paint = Paint::from_color(background_paint_color.into());

        canvas.save();
        // Prepare the canvas for the base of the tooltip, which is relative to the
        // center of the thumb.
        canvas.translate(
            center.dx() as f32,
            (center.dy() - Self::BOTTOM_TIP_Y_OFFSET) as f32,
        );
        canvas.scale(scale as f32, scale as f32);

        let rrect =
            RRect::from_rect_and_radius(upper_rect, Radius::circular(upper_rect.height() / 2.0));
        if let Some(stroke_paint_color) = stroke_paint_color {
            let stroke_paint = Paint {
                color: stroke_paint_color.into(),
                style: PaintStyle::Stroke(Stroke::new(1.0)),
                ..Paint::default()
            };
            draw_rrect(canvas, rrect, &stroke_paint);
        }

        draw_rrect(canvas, rrect, &fill_paint);

        // The label text is centered within the value indicator.
        let bottom_tip_to_upper_rect_translate_y =
            -Self::PREFERRED_HALF_HEIGHT / 2.0 - upper_rect.height();
        canvas.translate(0.0, bottom_tip_to_upper_rect_translate_y as f32);
        let box_center = Offset::new(horizontal_shift, upper_rect.height() / 2.3);
        let half_label_painter_offset =
            Offset::new(label_painter.width() / 2.0, label_painter.height() / 2.0);
        let label_offset = box_center - half_label_painter_offset;
        let fonts = PaintingBinding::instance(app).fonts(app);
        label_painter.paint(app.get_mut(fonts), canvas, label_offset);
        canvas.restore();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::slider_value_indicator_shape::{RoundSliderOverlayShape, SliderComponentShapeRef};

    #[test]
    fn track_shape_is_rounded_defaults() {
        assert!(!SliderTrackShape::is_rounded(
            &RectangularSliderTrackShape::new()
        ));
        assert!(SliderTrackShape::is_rounded(
            &RoundedRectSliderTrackShape::new()
        ));
        assert!(SliderTrackShape::is_rounded(&GappedSliderTrackShape::new()));
    }

    #[test]
    fn round_slider_thumb_shape_prefers_its_enabled_or_disabled_radius() {
        let shape = RoundSliderThumbShape::new();
        assert_eq!(shape.get_preferred_size(true, false), Size::new(20.0, 20.0));
        assert_eq!(
            shape.get_preferred_size(false, false),
            Size::new(20.0, 20.0)
        );
        let shape = shape.enabled_thumb_radius(7.0).disabled_thumb_radius(4.0);
        assert_eq!(shape.get_preferred_size(true, false), Size::new(14.0, 14.0));
        assert_eq!(shape.get_preferred_size(false, false), Size::new(8.0, 8.0));
    }

    #[test]
    fn handle_thumb_shape_prefers_four_by_forty_four() {
        assert_eq!(
            HandleThumbShape::new().get_preferred_size(true, true),
            Size::new(4.0, 44.0)
        );
    }

    #[test]
    fn round_tick_mark_defaults_to_a_quarter_of_the_track_height() {
        let theme = SliderThemeData::new().track_height(8.0);
        assert_eq!(
            RoundSliderTickMarkShape::new().get_preferred_size(&theme, true),
            Size::new(4.0, 4.0)
        );
        assert_eq!(
            RoundSliderTickMarkShape::new()
                .tick_mark_radius(5.0)
                .get_preferred_size(&theme, true),
            Size::new(10.0, 10.0)
        );
        assert_eq!(
            <dyn SliderTickMarkShape>::no_tick_mark().get_preferred_size(&theme, true),
            Size::ZERO
        );
    }

    #[test]
    fn built_in_shapes_compare_by_value_and_the_empty_one_by_identity() {
        let a: SliderTrackShapeRef = Rc::new(RoundedRectSliderTrackShape::new());
        let b: SliderTrackShapeRef = Rc::new(RoundedRectSliderTrackShape::new());
        let c: SliderTrackShapeRef = Rc::new(GappedSliderTrackShape::new());
        assert!(*a == *b);
        assert!(*a != *c);

        let thumb: SliderComponentShapeRef =
            Rc::new(RoundSliderThumbShape::new().enabled_thumb_radius(7.0));
        let same: SliderComponentShapeRef =
            Rc::new(RoundSliderThumbShape::new().enabled_thumb_radius(7.0));
        let other: SliderComponentShapeRef = Rc::new(RoundSliderThumbShape::new());
        let overlay: SliderComponentShapeRef = Rc::new(RoundSliderOverlayShape::new());
        assert!(*thumb == *same);
        assert!(*thumb != *other);
        assert!(*thumb != *overlay);

        let tick: SliderTickMarkShapeRef = Rc::new(RoundSliderTickMarkShape::new());
        let same_tick: SliderTickMarkShapeRef = Rc::new(RoundSliderTickMarkShape::new());
        assert!(*tick == *same_tick);
        assert!(Rc::ptr_eq(
            &<dyn SliderTickMarkShape>::no_tick_mark(),
            &<dyn SliderTickMarkShape>::no_tick_mark()
        ));
        assert!(*tick != *<dyn SliderTickMarkShape>::no_tick_mark());
    }
}