quickgui 0.1.4

A damage-driven, GPU-accelerated desktop GUI foundation
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
use std::sync::Arc;

use crate::{
    AccessibilityOrientation, AccessibilityRole, AccessibilityValueRange, Element, ElementId,
    KeyBinding, PointerEvent, PointerPhase, Size, StateAccessor, ViewContext, div,
};

/// Maximum thumbs retained by one slider.
///
/// A slider is a fixed-size value; the bound keeps the retained array small enough to copy while
/// still covering multi-handle range selection.
pub const MAX_SLIDER_THUMBS: usize = 8;

const SLIDER_KEY_CONTEXT: &str = "Slider";
const SLIDER_TRACK_ID_TAG: u64 = 0x5f3a_11c8_9d47_2b60;
const SLIDER_RANGE_ID_TAG: u64 = 0x18d6_7b04_ee31_a5c9;
const SLIDER_LABEL_ID_TAG: u64 = 0x4c1b_7e93_a0d6_28f5;
const SLIDER_VALUE_ID_TAG: u64 = 0xd370_29ae_51fc_6b48;
const SLIDER_CONTROL_ID_TAG: u64 = 0x83e6_05b1_9c7d_f420;
const SLIDER_THUMB_ID_TAG: u64 = 0xc70e_2a95_4413_86fd;

/// Move the active slider thumb one step toward the maximum.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SliderIncrement;
/// Move the active slider thumb one step toward the minimum.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SliderDecrement;
/// Move the active slider thumb one large step toward the maximum.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SliderLargeIncrement;
/// Move the active slider thumb one large step toward the minimum.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SliderLargeDecrement;
/// Move the active slider thumb to its minimum.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SliderMinimum;
/// Move the active slider thumb to its maximum.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SliderMaximum;

/// Contextual bindings used by [`Slider::key_with`] and [`SliderThumb::key_with`].
///
/// `Shift` selects the large step on the same arrow keys, matching the desktop convention that a
/// modified arrow moves by the same amount as PageUp and PageDown.
pub fn slider_key_bindings() -> [KeyBinding; 14] {
    [
        KeyBinding::new("right", SliderIncrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("up", SliderIncrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("left", SliderDecrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("down", SliderDecrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new(
            "shift-right",
            SliderLargeIncrement,
            Some(SLIDER_KEY_CONTEXT),
        ),
        KeyBinding::new("shift-up", SliderLargeIncrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("shift-left", SliderLargeDecrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("shift-down", SliderLargeDecrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("pageup", SliderLargeIncrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("pagedown", SliderLargeDecrement, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("home", SliderMinimum, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("end", SliderMaximum, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("platform-left", SliderMinimum, Some(SLIDER_KEY_CONTEXT)),
        KeyBinding::new("platform-right", SliderMaximum, Some(SLIDER_KEY_CONTEXT)),
    ]
}

/// Where a thumb sits relative to the value position along the track.
///
/// This is Base UI's `thumbAlignment`. It changes only the geometry an application derives from
/// [`SliderThumb::offset`]; the numeric contract is identical either way.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SliderThumbAlignment {
    /// Center the thumb on the value, so it overhangs the track at both ends.
    #[default]
    Center,
    /// Inset the thumb so its own box always stays inside the track.
    Edge,
}

/// What one captured pointer event did to a slider.
///
/// [`SliderState::apply_pointer`] keeps its original `bool` return; this is the richer result an
/// application needs to fire Base UI's `onValueCommitted` exactly once per drag and to restyle a
/// thumb while it is being dragged.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SliderPointerChange {
    /// Whether a value, the active thumb, or the dragging flag changed.
    ///
    /// This is what an application invalidates on, because a drag that changed no value still
    /// changes how the thumb is styled.
    pub changed: bool,
    /// Whether a value or the active thumb changed, which is what
    /// [`SliderState::apply_pointer`] returns.
    pub values_changed: bool,
    /// Whether this event ended a drag, which is when a committed value is final.
    pub committed: bool,
}

/// A copyable render-state snapshot for one slider thumb.
///
/// Base UI publishes the same facts as `data-index`, `data-dragging`, `data-disabled`, and the
/// thumb's own value; QuickGUI has no style sheet, so the application reads them here.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SliderThumbState {
    /// The thumb's position in the declared value list, Base UI's `data-index`.
    pub index: usize,
    /// The thumb's current value.
    pub value: f64,
    /// The `0.0..=1.0` position of the value along the track.
    pub fraction: f32,
    /// Whether typed keyboard actions target this thumb.
    pub active: bool,
    /// Whether a captured pointer is currently dragging the slider.
    pub dragging: bool,
    /// Whether the slider refuses changes.
    pub disabled: bool,
}

/// Layout and keyboard axis for one slider.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SliderOrientation {
    #[default]
    Horizontal,
    Vertical,
}

impl SliderOrientation {
    const fn accessibility(self) -> AccessibilityOrientation {
        match self {
            Self::Horizontal => AccessibilityOrientation::Horizontal,
            Self::Vertical => AccessibilityOrientation::Vertical,
        }
    }
}

/// Controlled, allocation-free bounded value state for one slider.
///
/// The state is a plain copyable value. It owns the numeric contract—bounds, step snapping, thumb
/// ordering, and the active thumb—while the application owns every visual declaration and decides
/// when a change invalidates its window. It retains no allocation, task, timer, observer, or idle
/// scheduler source.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SliderState {
    minimum: f64,
    maximum: f64,
    step: f64,
    large_step: f64,
    values: [f64; MAX_SLIDER_THUMBS],
    thumbs: usize,
    active: usize,
    min_steps_between: u16,
    thumb_alignment: SliderThumbAlignment,
    orientation: SliderOrientation,
    disabled: bool,
    dragging: bool,
}

impl Default for SliderState {
    fn default() -> Self {
        Self::new(0.0, 1.0, 0.0)
    }
}

impl SliderState {
    /// Create a single-thumb slider.
    ///
    /// Non-finite bounds fall back to `0.0..=1.0`, an inverted range is swapped, and an empty
    /// range keeps one degenerate value at the minimum.
    pub fn new(minimum: f64, maximum: f64, value: f64) -> Self {
        let (minimum, maximum) = normalized_bounds(minimum, maximum);
        let mut state = Self {
            minimum,
            maximum,
            step: 1.0,
            large_step: 0.0,
            values: [minimum; MAX_SLIDER_THUMBS],
            thumbs: 1,
            active: 0,
            min_steps_between: 0,
            thumb_alignment: SliderThumbAlignment::Center,
            orientation: SliderOrientation::Horizontal,
            disabled: false,
            dragging: false,
        };
        state.values[0] = state.snap(value);
        state
    }

    /// Create a multi-thumb range slider.
    ///
    /// # Panics
    ///
    /// Panics when `values` is empty or longer than [`MAX_SLIDER_THUMBS`].
    pub fn range(minimum: f64, maximum: f64, values: &[f64]) -> Self {
        assert!(!values.is_empty(), "a slider needs at least one thumb");
        assert!(
            values.len() <= MAX_SLIDER_THUMBS,
            "a slider retains at most {MAX_SLIDER_THUMBS} thumbs"
        );
        let mut state = Self::new(minimum, maximum, values[0]);
        state.thumbs = values.len();
        for (index, value) in values.iter().enumerate() {
            state.values[index] = state.snap(*value);
        }
        state.enforce_order(0);
        state
    }

    /// Replace the step used by arrow keys and pointer snapping.
    ///
    /// A non-finite or non-positive step selects continuous movement, which snaps nothing.
    #[must_use]
    pub fn step(mut self, step: f64) -> Self {
        self.step = if step.is_finite() && step > 0.0 {
            step
        } else {
            0.0
        };
        for index in 0..self.thumbs {
            self.values[index] = self.snap(self.values[index]);
        }
        self.enforce_order(0);
        self
    }

    /// Replace the amount moved by PageUp, PageDown, and shifted arrows.
    ///
    /// The default is ten ordinary steps.
    #[must_use]
    pub fn large_step(mut self, large_step: f64) -> Self {
        self.large_step = if large_step.is_finite() && large_step > 0.0 {
            large_step
        } else {
            0.0
        };
        self
    }

    /// Keep adjacent thumbs at least this many steps apart, Base UI's `minStepsBetweenValues`.
    ///
    /// The gap is measured in whole steps, so a continuous slider — one with no step — ignores it.
    /// Existing values are re-ordered outward from the first thumb, and every later movement is
    /// clamped so the gap can never close.
    #[must_use]
    pub fn min_steps_between_values(mut self, steps: usize) -> Self {
        self.min_steps_between = steps.min(u16::MAX as usize) as u16;
        self.enforce_order(0);
        self
    }

    /// Choose where a thumb sits relative to its value, Base UI's `thumbAlignment`.
    #[must_use]
    pub const fn thumb_alignment(mut self, alignment: SliderThumbAlignment) -> Self {
        self.thumb_alignment = alignment;
        self
    }

    #[must_use]
    pub const fn orientation(mut self, orientation: SliderOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    #[must_use]
    pub const fn vertical(self) -> Self {
        self.orientation(SliderOrientation::Vertical)
    }

    #[must_use]
    pub const fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    pub const fn minimum(&self) -> f64 {
        self.minimum
    }

    pub const fn maximum(&self) -> f64 {
        self.maximum
    }

    pub const fn step_value(&self) -> f64 {
        self.step
    }

    /// The amount one large step moves.
    ///
    /// Without an explicit [`Self::large_step`] this is ten ordinary steps, or one tenth of the
    /// range for a continuous slider.
    pub const fn large_step_value(&self) -> f64 {
        if self.large_step > 0.0 {
            self.large_step
        } else if self.step > 0.0 {
            self.step * 10.0
        } else {
            (self.maximum - self.minimum) / 10.0
        }
    }

    pub const fn axis(&self) -> SliderOrientation {
        self.orientation
    }

    pub const fn is_disabled(&self) -> bool {
        self.disabled
    }

    /// The minimum whole-step gap kept between adjacent thumbs.
    pub const fn min_steps_between_values_value(&self) -> usize {
        self.min_steps_between as usize
    }

    /// Where a thumb sits relative to its value.
    pub const fn thumb_alignment_value(&self) -> SliderThumbAlignment {
        self.thumb_alignment
    }

    /// Whether a captured pointer is currently dragging this slider, Base UI's `data-dragging`.
    pub const fn is_dragging(&self) -> bool {
        self.dragging
    }

    /// End a drag started by [`Self::apply_pointer`], returning whether the flag changed.
    ///
    /// A captured pointer normally clears this itself; call it when the application cancels a drag
    /// for its own reasons.
    pub fn end_drag(&mut self) -> bool {
        std::mem::replace(&mut self.dragging, false)
    }

    /// The distance between adjacent thumbs required by [`Self::min_steps_between_values`].
    fn minimum_gap(&self) -> f64 {
        if self.step > 0.0 {
            self.step * f64::from(self.min_steps_between)
        } else {
            0.0
        }
    }

    /// The leading-edge offset of one thumb along the track, honoring the thumb alignment.
    ///
    /// `track_length` and `thumb_length` are measured along the slider's own axis, and the result
    /// runs in that axis's visual direction — left to right for a horizontal slider, top to bottom
    /// for a vertical one, so a vertical minimum sits at the bottom.
    pub fn thumb_offset(&self, index: usize, track_length: f32, thumb_length: f32) -> f32 {
        let fraction = match self.orientation {
            SliderOrientation::Horizontal => self.fraction(index),
            SliderOrientation::Vertical => 1.0 - self.fraction(index),
        };
        let track_length = if track_length.is_finite() {
            track_length.max(0.0)
        } else {
            0.0
        };
        let thumb_length = if thumb_length.is_finite() {
            thumb_length.max(0.0)
        } else {
            0.0
        };
        match self.thumb_alignment {
            SliderThumbAlignment::Center => fraction * track_length - thumb_length * 0.5,
            SliderThumbAlignment::Edge => fraction * (track_length - thumb_length).max(0.0),
        }
    }

    pub const fn thumb_count(&self) -> usize {
        self.thumbs
    }

    pub const fn active_thumb(&self) -> usize {
        self.active
    }

    /// The first thumb's value, which is the whole value of a single-thumb slider.
    pub const fn value(&self) -> f64 {
        self.values[0]
    }

    pub fn values(&self) -> &[f64] {
        &self.values[..self.thumbs]
    }

    /// Read one thumb value, returning `None` past [`Self::thumb_count`].
    pub fn thumb_value(&self, index: usize) -> Option<f64> {
        (index < self.thumbs).then(|| self.values[index])
    }

    /// The inclusive bounds one thumb may move between, honoring its neighbors.
    pub fn thumb_bounds(&self, index: usize) -> Option<(f64, f64)> {
        if index >= self.thumbs {
            return None;
        }
        let gap = self.minimum_gap();
        let lower = if index == 0 {
            self.minimum
        } else {
            self.values[index - 1] + gap
        };
        let upper = if index + 1 == self.thumbs {
            self.maximum
        } else {
            self.values[index + 1] - gap
        };
        // A range too narrow for the declared gap collapses to the lower bound rather than
        // inverting, so a thumb can never be asked to move past its neighbour.
        Some((lower.min(self.maximum), upper.max(lower).min(self.maximum)))
    }

    /// Make one thumb the target of keyboard actions, returning whether it changed.
    pub fn set_active_thumb(&mut self, index: usize) -> bool {
        if index >= self.thumbs || self.active == index {
            return false;
        }
        self.active = index;
        true
    }

    /// Replace one thumb value, returning whether it changed.
    ///
    /// The value is clamped into the slider bounds, snapped to the step, and then clamped between
    /// its neighboring thumbs so ordering can never invert.
    pub fn set_thumb_value(&mut self, index: usize, value: f64) -> bool {
        if index >= self.thumbs || self.disabled {
            return false;
        }
        let Some((lower, upper)) = self.thumb_bounds(index) else {
            return false;
        };
        let value = self.snap(value).clamp(lower, upper);
        if self.values[index] == value {
            return false;
        }
        self.values[index] = value;
        true
    }

    /// Replace the first thumb value, returning whether it changed.
    pub fn set_value(&mut self, value: f64) -> bool {
        self.set_thumb_value(0, value)
    }

    /// Move the active thumb by `steps` ordinary steps, returning whether it changed.
    ///
    /// A continuous slider uses one percent of its range per step.
    pub fn step_active(&mut self, steps: f64) -> bool {
        let step = if self.step > 0.0 {
            self.step
        } else {
            (self.maximum - self.minimum) / 100.0
        };
        let index = self.active;
        let Some(current) = self.thumb_value(index) else {
            return false;
        };
        self.set_thumb_value(index, current + step * steps)
    }

    /// Move the active thumb by one large step, returning whether it changed.
    pub fn large_step_active(&mut self, forward: bool) -> bool {
        let delta = self.large_step_value();
        let index = self.active;
        let Some(current) = self.thumb_value(index) else {
            return false;
        };
        self.set_thumb_value(index, current + if forward { delta } else { -delta })
    }

    /// Move the active thumb to its lowest reachable value, returning whether it changed.
    pub fn active_to_minimum(&mut self) -> bool {
        let index = self.active;
        self.set_thumb_value(index, self.minimum)
    }

    /// Move the active thumb to its highest reachable value, returning whether it changed.
    pub fn active_to_maximum(&mut self) -> bool {
        let index = self.active;
        self.set_thumb_value(index, self.maximum)
    }

    /// The `0.0..=1.0` position of one thumb along its track.
    ///
    /// Applications use this to place a caller-owned thumb and size a caller-owned range fill.
    /// A vertical slider still returns zero at its minimum; invert it in the caller's layout.
    pub fn fraction(&self, index: usize) -> f32 {
        self.thumb_value(index)
            .map(|value| self.fraction_of(value))
            .unwrap_or(0.0)
    }

    /// The `0.0..=1.0` position of an arbitrary value along the track.
    pub fn fraction_of(&self, value: f64) -> f32 {
        let span = self.maximum - self.minimum;
        if span <= 0.0 {
            return 0.0;
        }
        (((value - self.minimum) / span) as f32).clamp(0.0, 1.0)
    }

    /// The snapped value at one `0.0..=1.0` track position.
    pub fn value_for_fraction(&self, fraction: f32) -> f64 {
        self.value_for_exact_fraction(f64::from(fraction))
    }

    fn value_for_exact_fraction(&self, fraction: f64) -> f64 {
        let fraction = if fraction.is_finite() {
            fraction.clamp(0.0, 1.0)
        } else {
            0.0
        };
        self.snap(self.minimum + fraction * (self.maximum - self.minimum))
    }

    /// The snapped value under one pointer offset measured along a track of `length` pixels.
    ///
    /// A vertical track measures from its top, where the maximum lives, matching desktop sliders.
    pub fn value_at(&self, offset: f32, length: f32) -> f64 {
        if !length.is_finite() || length <= 0.0 {
            return self.minimum;
        }
        let raw = (f64::from(offset) / f64::from(length)).clamp(0.0, 1.0);
        let fraction = match self.orientation {
            SliderOrientation::Horizontal => raw,
            SliderOrientation::Vertical => 1.0 - raw,
        };
        self.value_for_exact_fraction(fraction)
    }

    /// The thumb whose value is closest to `value`, preferring the lower index on a tie.
    pub fn nearest_thumb(&self, value: f64) -> usize {
        let mut best = 0;
        let mut distance = f64::INFINITY;
        for index in 0..self.thumbs {
            let candidate = (self.values[index] - value).abs();
            if candidate < distance {
                distance = candidate;
                best = index;
            }
        }
        best
    }

    /// Apply one captured pointer event over a track of the given size.
    ///
    /// The application owns the track's layout, so it passes the size it declared. A press picks
    /// the nearest thumb, makes it active, and jumps it to the pointer; subsequent moves drag that
    /// same thumb even outside the track. Returns whether any value or the active thumb changed.
    pub fn apply_pointer(&mut self, event: &PointerEvent, track: Size) -> bool {
        // The dragging flag is maintained either way; this entry point keeps reporting only
        // whether a value or the active thumb moved, exactly as it always has.
        self.apply_pointer_change(event, track).values_changed
    }

    /// Apply one captured pointer event and report whether the drag also ended.
    ///
    /// This is the same arithmetic as [`Self::apply_pointer`] with Base UI's `onValueCommitted`
    /// boundary exposed: `committed` is true exactly on the event that releases or cancels the
    /// capture, which is when the application persists the value it has been previewing. The
    /// dragging flag is maintained here too, so [`Self::is_dragging`] can restyle the thumb.
    pub fn apply_pointer_change(
        &mut self,
        event: &PointerEvent,
        track: Size,
    ) -> SliderPointerChange {
        if self.disabled {
            return SliderPointerChange::default();
        }
        let ends = matches!(event.phase, PointerPhase::Up | PointerPhase::Cancel);
        let dragging = match event.phase {
            PointerPhase::Down | PointerPhase::Move => true,
            PointerPhase::Up | PointerPhase::Cancel => false,
        };
        let dragging_changed = std::mem::replace(&mut self.dragging, dragging) != dragging;
        let values_changed = self.apply_pointer_values(event, track);
        SliderPointerChange {
            changed: values_changed || dragging_changed,
            values_changed,
            committed: ends,
        }
    }

    fn apply_pointer_values(&mut self, event: &PointerEvent, track: Size) -> bool {
        if self.disabled {
            return false;
        }
        let (offset, length) = match self.orientation {
            SliderOrientation::Horizontal => (event.local_position.x, track.width),
            SliderOrientation::Vertical => (event.local_position.y, track.height),
        };
        let value = self.value_at(offset, length);
        let mut changed = false;
        if event.phase == PointerPhase::Down {
            changed |= self.set_active_thumb(self.nearest_thumb(value));
        }
        if matches!(
            event.phase,
            PointerPhase::Down | PointerPhase::Move | PointerPhase::Up
        ) {
            let index = self.active;
            changed |= self.set_thumb_value(index, value);
        }
        changed
    }

    fn snap(&self, value: f64) -> f64 {
        if !value.is_finite() {
            return self.minimum;
        }
        let value = value.clamp(self.minimum, self.maximum);
        if self.step <= 0.0 {
            return value;
        }
        let steps = ((value - self.minimum) / self.step).round();
        (self.minimum + steps * self.step).clamp(self.minimum, self.maximum)
    }

    fn enforce_order(&mut self, from: usize) {
        let gap = self.minimum_gap();
        for index in from.max(1)..self.thumbs {
            let floor = (self.values[index - 1] + gap).min(self.maximum);
            if self.values[index] < floor {
                self.values[index] = floor;
            }
        }
    }
}

fn normalized_bounds(minimum: f64, maximum: f64) -> (f64, f64) {
    if !minimum.is_finite() || !maximum.is_finite() {
        return (0.0, 1.0);
    }
    if maximum < minimum {
        (maximum, minimum)
    } else {
        (minimum, maximum)
    }
}

/// A controlled, unstyled slider descriptor.
///
/// The application owns the track, range fill, thumb, tick marks, labels, colors, and motion.
/// QuickGUI supplies stable part identities, the Slider role with numeric value/min/max/step and
/// orientation, captured pointer arithmetic, and typed keyboard actions.
///
/// A single-thumb slider projects the Slider role on its root. A multi-thumb slider projects a
/// group root and one Slider role per thumb, each bounded by its neighbors, matching the WAI-ARIA
/// multi-thumb pattern.
#[derive(Clone, Copy, Debug, PartialEq)]
#[must_use = "a Slider descriptor has no effect until its parts are mounted"]
pub struct Slider {
    root_id: ElementId,
    state: SliderState,
}

impl Slider {
    pub fn new(root_id: impl Into<ElementId>, state: &SliderState) -> Self {
        Self {
            root_id: root_id.into(),
            state: *state,
        }
    }

    pub const fn root_id(self) -> ElementId {
        self.root_id
    }

    pub const fn state(self) -> SliderState {
        self.state
    }

    pub fn track_id(self) -> ElementId {
        derived_slider_id(self.root_id, SLIDER_TRACK_ID_TAG, 0)
    }

    pub fn range_id(self) -> ElementId {
        derived_slider_id(self.root_id, SLIDER_RANGE_ID_TAG, 0)
    }

    pub fn thumb_id(self, index: usize) -> ElementId {
        derived_slider_id(self.root_id, SLIDER_THUMB_ID_TAG, index as u64)
    }

    /// Stable identity of the label part.
    pub fn label_id(self) -> ElementId {
        derived_slider_id(self.root_id, SLIDER_LABEL_ID_TAG, 0)
    }

    /// Stable identity of the value part.
    pub fn value_id(self) -> ElementId {
        derived_slider_id(self.root_id, SLIDER_VALUE_ID_TAG, 0)
    }

    /// Stable identity of the control part.
    pub fn control_id(self) -> ElementId {
        derived_slider_id(self.root_id, SLIDER_CONTROL_ID_TAG, 0)
    }

    /// Whether this slider projects its value on the root instead of on each thumb.
    pub const fn is_single_thumb(self) -> bool {
        self.state.thumbs == 1
    }

    /// Decorate an application-owned root without adding layout or appearance.
    ///
    /// A single-thumb slider becomes the focusable Slider itself. Pair it with
    /// [`Self::key_with`] to attach the typed keyboard actions.
    pub fn root_with(self, root: Element) -> Element {
        let disabled = self.state.disabled || root.accessibility.disabled;
        let root = root
            .id(self.root_id)
            .cursor_default()
            .app_region_no_drag()
            .user_select_none()
            .disabled(disabled);
        let root = root
            .accessibility_labelled_by(self.label_id())
            .accessibility_described_by(self.value_id());
        if self.is_single_thumb() {
            root.accessibility_role(AccessibilityRole::Slider)
                .accessibility_orientation(self.state.orientation.accessibility())
                .accessibility_value_range(
                    AccessibilityValueRange::new(
                        self.state.values[0],
                        self.state.minimum,
                        self.state.maximum,
                    )
                    .step(self.state.step),
                )
                .focusable()
                .tab_index(0)
                .key_context(SLIDER_KEY_CONTEXT)
        } else {
            root.accessibility_role(AccessibilityRole::Group)
                .accessibility_orientation(self.state.orientation.accessibility())
        }
    }
    /// Create the unstyled root part. Use [`Self::root_with`] to supply an existing element.
    pub fn root(self) -> Element {
        self.root_with(crate::div())
    }

    /// Attach QuickGUI's typed slider keyboard actions to a single-thumb root.
    ///
    /// Install [`slider_key_bindings`] once on the application keymap. Multi-thumb sliders use
    /// [`SliderThumb::key_with`] instead so each thumb owns its own focus.
    pub fn key_with<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        root: Element,
        access: fn(&mut V) -> &mut SliderState,
    ) -> Element {
        self.key_with_accessor(cx, root, StateAccessor::from(access))
    }
    /// Create the unstyled key part. Use [`Self::key_with`] to supply an existing element.
    pub fn key<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        access: fn(&mut V) -> &mut SliderState,
    ) -> Element {
        self.key_with(cx, crate::div(), access)
    }

    /// Attach the typed slider keyboard actions against a per-instance state accessor.
    pub fn key_with_accessor<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        root: Element,
        access: StateAccessor<V, SliderState>,
    ) -> Element {
        bind_slider_actions(cx, root, self.root_id, &access, None)
    }

    /// Decorate an application-owned track.
    ///
    /// Base UI uses [`Self::control_with`] as the larger interactive surface. A declaration that
    /// omits Control can keep the earlier behavior by attaching a
    /// [`crate::ViewContext::pointer_listener`] registered for [`Self::track_id`] and forwarding
    /// the event to [`SliderState::apply_pointer`] with the size the caller laid out.
    pub fn track_with(self, track: Element) -> Element {
        track
            .id(self.track_id())
            .accessibility_hidden(true)
            .cursor_default()
            .app_region_no_drag()
            .user_select_none()
    }
    /// Create the unstyled track part. Use [`Self::track_with`] to supply an existing element.
    pub fn track(self) -> Element {
        self.track_with(crate::div())
    }

    /// Decorate the application-owned fill between the slider's minimum and its value.
    pub fn range_with(self, range: Element) -> Element {
        range.id(self.range_id()).accessibility_hidden(true)
    }
    /// Create the unstyled range part. Use [`Self::range_with`] to supply an existing element.
    pub fn range(self) -> Element {
        self.range_with(crate::div())
    }

    /// Base UI's Indicator name for the filled part of the track.
    ///
    /// This is the same decorator as [`Self::range_with`]; both names stay supported.
    pub fn indicator_with(self, indicator: Element) -> Element {
        self.range_with(indicator)
    }
    /// Create the unstyled indicator part. Use [`Self::indicator_with`] to supply an existing element.
    pub fn indicator(self) -> Element {
        self.indicator_with(crate::div())
    }

    /// Decorate the caller-owned interactive area that carries the slider's pointer capture.
    ///
    /// Base UI separates the Control — the region a press acts on — from the Track it paints.
    /// Attach a [`crate::ViewContext::pointer_listener`] registered for [`Self::control_id`] and
    /// forward the event to [`SliderState::apply_pointer_change`] with the size the caller laid
    /// out. A caller that omits this part can keep pointer capture on [`Self::track_with`].
    pub fn control_with(self, control: Element) -> Element {
        control
            .id(self.control_id())
            .accessibility_hidden(true)
            .cursor_default()
            .app_region_no_drag()
            .user_select_none()
    }
    /// Create the unstyled control part. Use [`Self::control_with`] to supply an existing element.
    pub fn control(self) -> Element {
        self.control_with(crate::div())
    }

    /// Assign the stable mounted label target the slider points at.
    ///
    /// A single-thumb slider takes its accessible name from this part. A multi-thumb slider names
    /// its group from it, and each thumb still needs its own accessible name.
    pub fn label_with(self, label: Element) -> Element {
        label.id(self.label_id())
    }
    /// Create the unstyled label part. Use [`Self::label_with`] to supply an existing element.
    pub fn label(self) -> Element {
        self.label_with(crate::div())
    }

    /// Assign the stable mounted value target the slider points at.
    ///
    /// Put [`Self::display_value`] inside it; QuickGUI never renders the text itself.
    pub fn value_with(self, value: Element) -> Element {
        value.id(self.value_id())
    }
    /// Create the unstyled value part. Use [`Self::value_with`] to supply an existing element.
    pub fn value(self) -> Element {
        self.value_with(crate::div())
    }

    /// Format every thumb value into one string for a caller-owned value part.
    ///
    /// This is Base UI's Root `format` prop applied through the shared [`crate::ValueFormat`].
    /// A range slider joins its thumbs with an en dash, which is the shape Base UI's Value renders
    /// by default.
    pub fn display_value(self, format: &crate::ValueFormat) -> Arc<str> {
        let mut text = String::new();
        for index in 0..self.state.thumbs {
            if index > 0 {
                text.push_str(" – ");
            }
            text.push_str(&format.apply(self.state.values[index], self.state.maximum));
        }
        Arc::from(text)
    }

    /// Describe one thumb.
    ///
    /// Indices at or past [`SliderState::thumb_count`] return `None` so a caller-driven loop stays
    /// bounded by the state instead of by its own arithmetic.
    pub fn thumb(self, index: usize) -> Option<SliderThumb> {
        let (lower, upper) = self.state.thumb_bounds(index)?;
        Some(SliderThumb {
            slider: self,
            index,
            value: self.state.values[index],
            lower,
            upper,
        })
    }
}

/// A copyable declaration for one slider thumb.
#[derive(Clone, Copy, Debug, PartialEq)]
#[must_use = "a SliderThumb descriptor has no effect until its part is mounted"]
pub struct SliderThumb {
    slider: Slider,
    index: usize,
    value: f64,
    lower: f64,
    upper: f64,
}

impl SliderThumb {
    pub const fn index(self) -> usize {
        self.index
    }

    pub const fn value(self) -> f64 {
        self.value
    }

    /// The inclusive bounds this thumb may move between, honoring its neighbors.
    pub const fn bounds(self) -> (f64, f64) {
        (self.lower, self.upper)
    }

    /// The `0.0..=1.0` position of this thumb along the track.
    pub fn fraction(self) -> f32 {
        self.slider.state.fraction_of(self.value)
    }

    pub const fn is_active(self) -> bool {
        self.slider.state.active == self.index
    }

    pub fn thumb_id(self) -> ElementId {
        self.slider.thumb_id(self.index)
    }

    /// Whether a captured pointer is currently dragging this slider, Base UI's `data-dragging`.
    pub const fn is_dragging(self) -> bool {
        self.slider.state.dragging
    }

    /// A copyable render-state snapshot the application styles this thumb from.
    pub fn state(self) -> SliderThumbState {
        SliderThumbState {
            index: self.index,
            value: self.value,
            fraction: self.fraction(),
            active: self.is_active(),
            dragging: self.slider.state.dragging,
            disabled: self.slider.state.disabled,
        }
    }

    /// The leading-edge offset of this thumb along the track, honoring the thumb alignment.
    ///
    /// `track_length` and `thumb_length` are measured along the slider's own axis. This is the one
    /// place Base UI's `thumbAlignment` changes anything: `Center` lets the thumb overhang both
    /// ends, `Edge` keeps its box inside the track.
    pub fn offset(self, track_length: f32, thumb_length: f32) -> f32 {
        self.slider
            .state
            .thumb_offset(self.index, track_length, thumb_length)
    }

    /// Format this thumb's value, Base UI's Thumb `getAriaValueText` equivalent.
    ///
    /// Pass the result to `Element::accessibility_value` on the thumb; QuickGUI never renders it.
    pub fn value_text(self, format: &crate::ValueFormat) -> Arc<str> {
        format.apply(self.value, self.slider.state.maximum)
    }

    /// Decorate an application-owned thumb without adding layout or appearance.
    ///
    /// A single-thumb slider keeps its value on the root, so its thumb is decorative and hidden
    /// from assistive technology. Every thumb of a multi-thumb slider is an independently
    /// focusable Slider bounded by its neighbors.
    pub fn thumb_with(self, thumb: Element) -> Element {
        let disabled = self.slider.state.disabled || thumb.accessibility.disabled;
        let thumb = thumb.id(self.thumb_id());
        if self.slider.is_single_thumb() {
            return thumb.accessibility_hidden(true);
        }
        thumb
            .accessibility_role(AccessibilityRole::Slider)
            .accessibility_orientation(self.slider.state.orientation.accessibility())
            .accessibility_value_range(
                AccessibilityValueRange::new(self.value, self.lower, self.upper)
                    .step(self.slider.state.step),
            )
            .focusable()
            .tab_index(0)
            .key_context(SLIDER_KEY_CONTEXT)
            .cursor_default()
            .app_region_no_drag()
            .user_select_none()
            .disabled(disabled)
    }
    /// Create the unstyled thumb part. Use [`Self::thumb_with`] to supply an existing element.
    pub fn thumb(self) -> Element {
        self.thumb_with(crate::div())
    }

    /// Attach QuickGUI's typed slider keyboard actions to this thumb.
    ///
    /// Focusing the thumb also makes it the active thumb, so arrows move the thumb the user sees.
    pub fn key_with<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        thumb: Element,
        access: fn(&mut V) -> &mut SliderState,
    ) -> Element {
        self.key_with_accessor(cx, thumb, StateAccessor::from(access))
    }
    /// Create the unstyled key part. Use [`Self::key_with`] to supply an existing element.
    pub fn key<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        access: fn(&mut V) -> &mut SliderState,
    ) -> Element {
        self.key_with(cx, crate::div(), access)
    }

    /// Attach this thumb's typed keyboard actions against a per-instance state accessor.
    pub fn key_with_accessor<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        thumb: Element,
        access: StateAccessor<V, SliderState>,
    ) -> Element {
        bind_slider_actions(cx, thumb, self.thumb_id(), &access, Some(self.index))
    }
}

fn activate_slider_thumb<V: 'static>(
    view: &mut V,
    access: &StateAccessor<V, SliderState>,
    thumb: Option<usize>,
) {
    if let Some(index) = thumb {
        access.get(view).set_active_thumb(index);
    }
}

fn bind_slider_actions<V: 'static>(
    cx: &mut ViewContext<'_, V>,
    element: Element,
    id: ElementId,
    access_source: &StateAccessor<V, SliderState>,
    thumb: Option<usize>,
) -> Element {
    let access = access_source.clone();
    let increment = cx.action_listener(id, move |view, _: &SliderIncrement, cx| {
        activate_slider_thumb(view, &access, thumb);
        if access.get(view).step_active(1.0) {
            cx.invalidate();
        }
    });
    let access = access_source.clone();
    let decrement = cx.action_listener(id, move |view, _: &SliderDecrement, cx| {
        activate_slider_thumb(view, &access, thumb);
        if access.get(view).step_active(-1.0) {
            cx.invalidate();
        }
    });
    let access = access_source.clone();
    let large_increment = cx.action_listener(id, move |view, _: &SliderLargeIncrement, cx| {
        activate_slider_thumb(view, &access, thumb);
        if access.get(view).large_step_active(true) {
            cx.invalidate();
        }
    });
    let access = access_source.clone();
    let large_decrement = cx.action_listener(id, move |view, _: &SliderLargeDecrement, cx| {
        activate_slider_thumb(view, &access, thumb);
        if access.get(view).large_step_active(false) {
            cx.invalidate();
        }
    });
    let access = access_source.clone();
    let minimum = cx.action_listener(id, move |view, _: &SliderMinimum, cx| {
        activate_slider_thumb(view, &access, thumb);
        if access.get(view).active_to_minimum() {
            cx.invalidate();
        }
    });
    let access = access_source.clone();
    let maximum = cx.action_listener(id, move |view, _: &SliderMaximum, cx| {
        activate_slider_thumb(view, &access, thumb);
        if access.get(view).active_to_maximum() {
            cx.invalidate();
        }
    });
    element
        .on_action(increment)
        .on_action(decrement)
        .on_action(large_increment)
        .on_action(large_decrement)
        .on_action(minimum)
        .on_action(maximum)
}

/// Create an unstyled controlled single-thumb slider root.
///
/// This shorthand is equivalent to `Slider::new(id, state).root_with(div())`.
pub fn slider(id: impl Into<ElementId>, state: &SliderState) -> Element {
    Slider::new(id, state).root_with(div())
}

fn derived_slider_id(scope: ElementId, tag: u64, index: u64) -> ElementId {
    let mut hash = scope
        .as_u64()
        .rotate_left(23)
        .wrapping_add(index.rotate_right(11))
        ^ tag;
    hash ^= hash >> 30;
    hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
    hash ^= hash >> 27;
    hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
    hash ^= hash >> 31;
    if hash == 0 || hash == u64::MAX || hash == scope.as_u64() {
        hash ^= tag.rotate_left(31);
    }
    ElementId::new(hash)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        AppRegion, Application, Color, CursorStyle, IntoElement, Modifiers, MouseButton, Point,
        UserSelect, Vector, View, WindowOptions, text,
    };

    fn pointer_event(phase: PointerPhase, x: f32, y: f32) -> PointerEvent {
        PointerEvent {
            size: Size::new(200.0, 20.0),
            phase,
            position: Point::new(x, y),
            origin: Point::new(x, y),
            local_position: Point::new(x, y),
            local_origin: Point::new(x, y),
            delta: Vector::ZERO,
            button: MouseButton::Left,
            modifiers: Modifiers::empty(),
        }
    }

    #[test]
    fn captured_pointer_selects_then_drags_one_thumb() {
        let mut state = SliderState::range(0.0, 100.0, &[20.0, 80.0]).step(10.0);
        let track = Size::new(200.0, 20.0);

        assert!(state.apply_pointer(&pointer_event(PointerPhase::Down, 150.0, 10.0), track));
        assert_eq!(state.active_thumb(), 1);
        assert_eq!(state.values(), &[20.0, 80.0]);

        assert!(state.apply_pointer(&pointer_event(PointerPhase::Move, 190.0, 10.0), track));
        assert_eq!(state.values(), &[20.0, 100.0]);

        // Capture continues outside the track and stays clamped by the lower thumb.
        assert!(state.apply_pointer(&pointer_event(PointerPhase::Move, -400.0, 10.0), track));
        assert_eq!(state.values(), &[20.0, 20.0]);
        assert!(!state.apply_pointer(&pointer_event(PointerPhase::Up, -400.0, 10.0), track));

        let mut vertical = SliderState::new(0.0, 100.0, 0.0).step(10.0).vertical();
        // Pressing the bottom of a vertical track is already the minimum, so nothing changes.
        assert!(!vertical.apply_pointer(&pointer_event(PointerPhase::Down, 5.0, 20.0), track));
        assert_eq!(vertical.value(), 0.0);
        assert!(vertical.apply_pointer(&pointer_event(PointerPhase::Down, 5.0, 5.0), track));
        assert_eq!(vertical.value(), 80.0);

        let mut disabled = SliderState::new(0.0, 1.0, 0.5).disabled(true);
        assert!(!disabled.apply_pointer(&pointer_event(PointerPhase::Down, 200.0, 0.0), track));
        assert!(!state.apply_pointer(&pointer_event(PointerPhase::Cancel, 0.0, 0.0), track));
    }

    #[test]
    fn state_snaps_clamps_and_orders_bounded_values() {
        let mut state = SliderState::new(0.0, 100.0, 42.0).step(5.0);
        assert_eq!(state.minimum(), 0.0);
        assert_eq!(state.maximum(), 100.0);
        assert_eq!(state.value(), 40.0);
        assert!(state.set_value(97.0));
        assert_eq!(state.value(), 95.0);
        assert!(!state.set_value(96.0));
        assert!(state.set_value(1_000.0));
        assert_eq!(state.value(), 100.0);
        assert!(state.set_value(f64::NAN));
        assert_eq!(state.value(), 0.0);

        assert!(state.step_active(1.0));
        assert_eq!(state.value(), 5.0);
        assert!(state.large_step_active(true));
        assert_eq!(state.value(), 55.0);
        assert!(state.large_step_active(false));
        assert_eq!(state.value(), 5.0);
        assert!(state.active_to_maximum());
        assert_eq!(state.value(), 100.0);
        assert!(state.active_to_minimum());
        assert_eq!(state.value(), 0.0);
        assert!(!state.active_to_minimum());

        let inverted = SliderState::new(10.0, -10.0, 0.0);
        assert_eq!(inverted.minimum(), -10.0);
        assert_eq!(inverted.maximum(), 10.0);
        let broken = SliderState::new(f64::NAN, f64::INFINITY, 0.5);
        assert_eq!((broken.minimum(), broken.maximum()), (0.0, 1.0));

        let mut range = SliderState::range(0.0, 10.0, &[8.0, 2.0]);
        assert_eq!(range.thumb_count(), 2);
        assert_eq!(range.values(), &[8.0, 8.0]);
        assert!(range.set_thumb_value(0, 3.0));
        assert_eq!(range.values(), &[3.0, 8.0]);
        assert!(range.set_thumb_value(1, 1.0));
        assert_eq!(range.values(), &[3.0, 3.0]);
        assert_eq!(range.thumb_bounds(0), Some((0.0, 3.0)));
        assert_eq!(range.thumb_bounds(1), Some((3.0, 10.0)));
        assert_eq!(range.thumb_bounds(2), None);
        assert!(!range.set_thumb_value(2, 5.0));
        assert!(range.set_active_thumb(1));
        assert!(!range.set_active_thumb(1));
        assert!(!range.set_active_thumb(9));
        assert_eq!(range.active_thumb(), 1);

        let disabled = &mut SliderState::new(0.0, 1.0, 0.5).disabled(true);
        assert!(!disabled.set_value(0.75));
        assert!(disabled.is_disabled());
    }

    #[test]
    fn pointer_geometry_is_orientation_aware_and_bounded() {
        let horizontal = SliderState::new(0.0, 200.0, 0.0).step(0.0);
        assert_eq!(horizontal.value_at(50.0, 100.0), 100.0);
        assert_eq!(horizontal.value_at(-40.0, 100.0), 0.0);
        assert_eq!(horizontal.value_at(400.0, 100.0), 200.0);
        assert_eq!(horizontal.value_at(50.0, 0.0), 0.0);
        assert_eq!(horizontal.fraction_of(50.0), 0.25);

        let vertical = SliderState::new(0.0, 200.0, 0.0).step(0.0).vertical();
        assert_eq!(vertical.value_at(0.0, 100.0), 200.0);
        assert_eq!(vertical.value_at(100.0, 100.0), 0.0);
        assert_eq!(vertical.value_at(25.0, 100.0), 150.0);

        let degenerate = SliderState::new(5.0, 5.0, 5.0);
        assert_eq!(degenerate.fraction(0), 0.0);
        assert_eq!(degenerate.value_for_fraction(1.0), 5.0);
        assert_eq!(degenerate.value_for_fraction(f32::NAN), 5.0);
    }

    #[test]
    fn parts_add_exact_semantics_without_appearance() {
        let state = SliderState::new(0.0, 10.0, 4.0).step(2.0);
        let slider = Slider::new("volume", &state);
        let root = slider.root_with(div().w(240.0).bg(Color::rgb8(1, 2, 3)));
        assert_eq!(root.explicit_id, Some("volume".into()));
        assert_eq!(root.accessibility.role, AccessibilityRole::Slider);
        assert_eq!(
            root.accessibility.orientation,
            Some(AccessibilityOrientation::Horizontal)
        );
        assert_eq!(
            root.accessibility.value_range.as_deref(),
            Some(&AccessibilityValueRange::new(4.0, 0.0, 10.0).step(2.0))
        );
        assert!(root.focusable);
        assert_eq!(root.tab_index, 0);
        assert_eq!(root.cursor_style, Some(CursorStyle::Arrow));
        assert_eq!(root.app_region, Some(AppRegion::NoDrag));
        assert_eq!(root.user_select, UserSelect::None);
        assert_eq!(root.visual.background, Some(Color::rgb8(1, 2, 3)));
        assert!(root.transition.is_none());

        let track = slider.track_with(div().h(4.0).bg(Color::rgb8(4, 5, 6)));
        assert_eq!(track.explicit_id, Some(slider.track_id()));
        assert!(track.accessibility.hidden);
        assert_eq!(track.visual.background, Some(Color::rgb8(4, 5, 6)));

        let range = slider.range_with(div().bg(Color::rgb8(7, 8, 9)));
        assert_eq!(range.explicit_id, Some(slider.range_id()));
        assert!(range.accessibility.hidden);

        let thumb = slider.thumb(0).expect("first thumb");
        assert_eq!(thumb.value(), 4.0);
        assert_eq!(thumb.fraction(), 0.4);
        assert!(thumb.is_active());
        let thumb_element = thumb.thumb_with(div().size(12.0, 12.0));
        assert_eq!(thumb_element.explicit_id, Some(slider.thumb_id(0)));
        assert!(thumb_element.accessibility.hidden);
        assert!(slider.thumb(1).is_none());

        let ids = [
            slider.root_id(),
            slider.track_id(),
            slider.range_id(),
            slider.thumb_id(0),
            slider.thumb_id(1),
        ];
        for (index, id) in ids.iter().enumerate() {
            assert!(!ids[..index].contains(id));
        }

        let range_state = SliderState::range(0.0, 100.0, &[20.0, 80.0]).step(10.0);
        let range_slider = Slider::new("price", &range_state);
        let range_root = range_slider.root_with(div());
        assert_eq!(range_root.accessibility.role, AccessibilityRole::Group);
        assert!(!range_root.focusable);
        let lower = range_slider.thumb(0).expect("lower thumb");
        let lower_element = lower.thumb_with(div());
        assert_eq!(lower_element.accessibility.role, AccessibilityRole::Slider);
        assert_eq!(
            lower_element.accessibility.value_range.as_deref(),
            Some(&AccessibilityValueRange::new(20.0, 0.0, 80.0).step(10.0))
        );
        assert!(lower_element.focusable);
        let upper = range_slider.thumb(1).expect("upper thumb");
        assert_eq!(upper.bounds(), (20.0, 100.0));
        assert!(!upper.is_active());

        let disabled_state = SliderState::new(0.0, 1.0, 0.5).disabled(true);
        let disabled = Slider::new("muted", &disabled_state).root_with(div());
        assert!(disabled.accessibility.disabled);
    }

    #[derive(Default)]
    struct SliderView {
        volume: SliderState,
        price: SliderState,
    }

    impl SliderView {
        fn build() -> Self {
            Self {
                volume: SliderState::new(0.0, 100.0, 40.0).step(5.0),
                price: SliderState::range(0.0, 100.0, &[20.0, 80.0]).step(10.0),
            }
        }

        fn volume(view: &mut Self) -> &mut SliderState {
            &mut view.volume
        }

        fn price(view: &mut Self) -> &mut SliderState {
            &mut view.price
        }
    }

    impl View for SliderView {
        fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            let volume = Slider::new("volume", &self.volume);
            let volume_root = volume.key_with(cx, volume.root_with(div()), Self::volume);
            let drag = cx.pointer_listener(volume.track_id(), |view, event, cx| {
                if view.volume.apply_pointer(event, Size::new(200.0, 20.0)) {
                    cx.invalidate();
                }
            });
            let volume_thumb = volume.thumb(0).expect("volume thumb");

            let price = Slider::new("price", &self.price);
            let lower = price.thumb(0).expect("lower thumb");
            let upper = price.thumb(1).expect("upper thumb");
            let lower_element = lower.key_with(cx, lower.thumb_with(div()), Self::price);
            let upper_element = upper.key_with(cx, upper.thumb_with(div()), Self::price);

            div()
                .child(text("Slider gallery"))
                .child(
                    volume_root.child(
                        volume
                            .track_with(div().w(200.0).h(20.0).on_pointer(drag))
                            .child(volume.range_with(div()))
                            .child(volume_thumb.thumb_with(div())),
                    ),
                )
                .child(
                    price
                        .root_with(div())
                        .child(price.track_with(div().w(200.0).h(20.0)))
                        .child(lower_element)
                        .child(upper_element),
                )
        }
    }

    #[test]
    fn keyboard_pointer_and_accessibility_paths_stay_deterministic() {
        let (mut cx, view) = Application::new()
            .bind_keys(slider_key_bindings())
            .into_test_context(WindowOptions::default(), SliderView::build())
            .unwrap();
        let window = view.window_handle();
        let volume = Slider::new("volume", &SliderState::new(0.0, 100.0, 40.0));
        let price = Slider::new("price", &SliderState::range(0.0, 100.0, &[20.0, 80.0]));

        cx.focus(window, volume.root_id()).unwrap();
        cx.simulate_keystrokes(window, "right").unwrap();
        assert_eq!(cx.read(view, |view| view.volume.value()).unwrap(), 45.0);
        cx.simulate_keystrokes(window, "left left").unwrap();
        assert_eq!(cx.read(view, |view| view.volume.value()).unwrap(), 35.0);
        cx.simulate_keystrokes(window, "pageup").unwrap();
        assert_eq!(cx.read(view, |view| view.volume.value()).unwrap(), 85.0);
        cx.simulate_keystrokes(window, "shift-down").unwrap();
        assert_eq!(cx.read(view, |view| view.volume.value()).unwrap(), 35.0);
        cx.simulate_keystrokes(window, "home").unwrap();
        assert_eq!(cx.read(view, |view| view.volume.value()).unwrap(), 0.0);
        cx.simulate_keystrokes(window, "end").unwrap();
        assert_eq!(cx.read(view, |view| view.volume.value()).unwrap(), 100.0);

        cx.focus(window, price.thumb_id(1)).unwrap();
        cx.simulate_keystrokes(window, "right").unwrap();
        assert_eq!(
            cx.read(view, |view| view.price.values().to_vec()).unwrap(),
            vec![20.0, 90.0]
        );
        cx.simulate_keystrokes(window, "home").unwrap();
        assert_eq!(
            cx.read(view, |view| view.price.values().to_vec()).unwrap(),
            vec![20.0, 20.0]
        );

        let update = cx.accessibility_update(window).unwrap();
        let node = |id: ElementId| {
            update
                .nodes
                .iter()
                .find_map(|(node_id, node)| (node_id.0 == id.as_u64()).then_some(node))
                .expect("slider accessibility node")
        };
        let root = node(volume.root_id());
        assert_eq!(root.role(), accesskit::Role::Slider);
        assert_eq!(root.numeric_value(), Some(100.0));
        assert_eq!(root.min_numeric_value(), Some(0.0));
        assert_eq!(root.max_numeric_value(), Some(100.0));
        assert_eq!(root.numeric_value_step(), Some(5.0));
        assert_eq!(root.orientation(), Some(accesskit::Orientation::Horizontal));
        let upper = node(price.thumb_id(1));
        assert_eq!(upper.role(), accesskit::Role::Slider);
        assert_eq!(upper.numeric_value(), Some(20.0));
        assert_eq!(upper.min_numeric_value(), Some(20.0));
        assert_eq!(upper.max_numeric_value(), Some(100.0));

        let renders = cx.render_count(window).unwrap();
        cx.run_until_idle().unwrap();
        assert_eq!(cx.render_count(window).unwrap(), renders);
    }

    /// One view that renders a data-driven list of sliders through the accessor entry point.
    ///
    /// This is exactly the shape a host renderer has: one `View` implementation, many declared
    /// instances, and no way to hand each one a distinct non-capturing `fn` pointer.
    struct SliderListView {
        sliders: Vec<SliderState>,
    }

    impl SliderListView {
        fn slider_id(index: usize) -> ElementId {
            ElementId::new(index as u64 + 1)
        }
    }

    impl View for SliderListView {
        fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            let mut root = div();
            for index in 0..self.sliders.len() {
                let state = self.sliders[index];
                let slider = Slider::new(Self::slider_id(index), &state);
                root = root.child(slider.key_with_accessor(
                    cx,
                    slider.root_with(div()),
                    StateAccessor::new(move |view: &mut Self| &mut view.sliders[index]),
                ));
            }
            root
        }
    }

    #[test]
    fn per_instance_accessors_keep_declared_instances_independent() {
        let (mut cx, view) = Application::new()
            .bind_keys(slider_key_bindings())
            .into_test_context(
                WindowOptions::default(),
                SliderListView {
                    sliders: vec![
                        SliderState::new(0.0, 100.0, 10.0).step(5.0),
                        SliderState::new(0.0, 100.0, 60.0).step(5.0),
                        SliderState::new(0.0, 100.0, 90.0).step(5.0),
                    ],
                },
            )
            .unwrap();
        let window = view.window_handle();
        let values = |cx: &mut crate::TestAppContext| {
            cx.read(view, |view| {
                view.sliders
                    .iter()
                    .map(SliderState::value)
                    .collect::<Vec<_>>()
            })
            .unwrap()
        };

        cx.focus(window, SliderListView::slider_id(0)).unwrap();
        cx.simulate_keystrokes(window, "right right").unwrap();
        assert_eq!(values(&mut cx), vec![20.0, 60.0, 90.0]);

        cx.focus(window, SliderListView::slider_id(2)).unwrap();
        cx.simulate_keystrokes(window, "left").unwrap();
        assert_eq!(values(&mut cx), vec![20.0, 60.0, 85.0]);

        cx.focus(window, SliderListView::slider_id(1)).unwrap();
        cx.simulate_keystrokes(window, "home").unwrap();
        assert_eq!(values(&mut cx), vec![20.0, 0.0, 85.0]);

        // The accessor adds no idle source: a settled window still renders nothing extra.
        let renders = cx.render_count(window).unwrap();
        cx.run_until_idle().unwrap();
        assert_eq!(cx.render_count(window).unwrap(), renders);
    }

    #[test]
    fn bindings_are_contextual_and_complete() {
        let bindings = slider_key_bindings();
        assert_eq!(bindings.len(), 14);
        assert!(
            bindings.iter().all(
                |binding| binding.context_predicate().is_some_and(|context| context
                    .depth_of(&[crate::KeyContext::parse(SLIDER_KEY_CONTEXT).unwrap()])
                    .is_some())
            )
        );
    }

    #[test]
    #[should_panic(expected = "a slider needs at least one thumb")]
    fn empty_range_is_rejected() {
        let _ = SliderState::range(0.0, 1.0, &[]);
    }

    #[test]
    #[should_panic(expected = "at most")]
    fn too_many_thumbs_are_rejected() {
        let _ = SliderState::range(0.0, 1.0, &[0.0; MAX_SLIDER_THUMBS + 1]);
    }

    #[test]
    fn shorthand_is_a_semantic_unstyled_root() {
        let state = SliderState::new(0.0, 1.0, 0.25);
        let element = slider("brightness", &state);
        assert_eq!(element.accessibility.role, AccessibilityRole::Slider);
        assert!(element.children.is_empty());
        assert_eq!(element.visual.background, None);
    }

    #[test]
    fn minimum_steps_between_values_hold_a_gap_open_in_both_directions() {
        let state = SliderState::range(0.0, 100.0, &[10.0, 20.0, 30.0])
            .step(5.0)
            .min_steps_between_values(4);
        assert_eq!(state.min_steps_between_values_value(), 4);
        // The declared values are re-ordered outward from the first thumb to open the gap.
        assert_eq!(state.values(), &[10.0, 30.0, 50.0]);
        assert_eq!(state.thumb_bounds(0), Some((0.0, 10.0)));
        assert_eq!(state.thumb_bounds(1), Some((30.0, 30.0)));
        assert_eq!(state.thumb_bounds(2), Some((50.0, 100.0)));

        let mut state = SliderState::range(0.0, 100.0, &[0.0, 50.0])
            .step(5.0)
            .min_steps_between_values(2);
        assert!(state.set_thumb_value(0, 100.0));
        assert_eq!(state.values(), &[40.0, 50.0]);
        // The second thumb is already sitting on its own floor, so it cannot move down at all.
        assert!(!state.set_thumb_value(1, 0.0));
        assert_eq!(state.values(), &[40.0, 50.0]);

        // A continuous slider has no step to count, so the gap is inert.
        let continuous = SliderState::range(0.0, 100.0, &[20.0, 40.0])
            .step(f64::NAN)
            .min_steps_between_values(3);
        assert_eq!(continuous.step_value(), 0.0);
        assert_eq!(continuous.thumb_bounds(0), Some((0.0, 40.0)));
        assert_eq!(continuous.thumb_bounds(1), Some((20.0, 100.0)));

        // The bound is retained rather than trusted.
        assert_eq!(
            SliderState::new(0.0, 1.0, 0.0)
                .min_steps_between_values(usize::MAX)
                .min_steps_between_values_value(),
            u16::MAX as usize
        );
    }

    #[test]
    fn thumb_alignment_only_changes_the_offset_the_application_lays_out() {
        let centered = SliderState::new(0.0, 100.0, 25.0);
        assert_eq!(
            centered.thumb_alignment_value(),
            SliderThumbAlignment::Center
        );
        // 0.25 * 200 - 16/2
        assert_eq!(centered.thumb_offset(0, 200.0, 16.0), 42.0);
        assert_eq!(centered.thumb_offset(0, 200.0, 0.0), 50.0);

        let edged = centered.thumb_alignment(SliderThumbAlignment::Edge);
        // 0.25 * (200 - 16)
        assert_eq!(edged.thumb_offset(0, 200.0, 16.0), 46.0);
        assert_eq!(edged.value(), centered.value());
        assert_eq!(edged.thumb_bounds(0), centered.thumb_bounds(0));

        // A vertical slider runs its offset downward, so the minimum sits at the bottom.
        let vertical = SliderState::new(0.0, 100.0, 25.0).vertical();
        assert_eq!(vertical.thumb_offset(0, 200.0, 0.0), 150.0);
        let vertical_edge = vertical.thumb_alignment(SliderThumbAlignment::Edge);
        assert_eq!(vertical_edge.thumb_offset(0, 200.0, 20.0), 135.0);

        // Non-finite geometry never escapes into layout.
        assert_eq!(centered.thumb_offset(0, f32::NAN, 16.0), -8.0);
        assert_eq!(edged.thumb_offset(0, 10.0, 40.0), 0.0);

        let slider = Slider::new("range", &SliderState::range(0.0, 100.0, &[20.0, 80.0]));
        let thumb = slider.thumb(1).expect("second thumb");
        assert_eq!(thumb.offset(200.0, 10.0), 155.0);
    }

    #[test]
    fn a_captured_drag_reports_its_commit_boundary_and_dragging_state() {
        let track = Size::new(200.0, 20.0);
        let mut state = SliderState::new(0.0, 100.0, 0.0).step(1.0);
        assert!(!state.is_dragging());

        let down =
            state.apply_pointer_change(&pointer_event(PointerPhase::Down, 100.0, 10.0), track);
        assert!(down.changed);
        assert!(down.values_changed);
        assert!(!down.committed);
        assert!(state.is_dragging());
        assert_eq!(state.value(), 50.0);

        let moved =
            state.apply_pointer_change(&pointer_event(PointerPhase::Move, 150.0, 10.0), track);
        assert!(moved.values_changed);
        assert!(!moved.committed);
        assert!(state.is_dragging());
        assert_eq!(state.value(), 75.0);

        // Releasing without moving still commits, and it is the only event that does.
        let up = state.apply_pointer_change(&pointer_event(PointerPhase::Up, 150.0, 10.0), track);
        assert!(!up.values_changed);
        assert!(up.changed);
        assert!(up.committed);
        assert!(!state.is_dragging());

        let cancelled = {
            let mut state = SliderState::new(0.0, 100.0, 0.0);
            state.apply_pointer_change(&pointer_event(PointerPhase::Down, 100.0, 10.0), track);
            state.apply_pointer_change(&pointer_event(PointerPhase::Cancel, 100.0, 10.0), track)
        };
        assert!(cancelled.committed);

        // A disabled slider never drags and never commits.
        let mut disabled = SliderState::new(0.0, 100.0, 10.0).disabled(true);
        let change =
            disabled.apply_pointer_change(&pointer_event(PointerPhase::Down, 100.0, 10.0), track);
        assert_eq!(change, SliderPointerChange::default());
        assert!(!disabled.is_dragging());

        let mut ended = SliderState::new(0.0, 1.0, 0.0);
        ended.apply_pointer_change(&pointer_event(PointerPhase::Down, 10.0, 10.0), track);
        assert!(ended.end_drag());
        assert!(!ended.end_drag());
    }

    #[test]
    fn base_ui_slider_parts_carry_stable_identities_relations_and_no_appearance() {
        let mut state = SliderState::range(0.0, 100.0, &[20.0, 80.0]).step(1.0);
        state.apply_pointer_change(
            &pointer_event(PointerPhase::Down, 40.0, 10.0),
            Size::new(200.0, 20.0),
        );
        let slider = Slider::new("volume", &state);

        let ids = [
            slider.label_id(),
            slider.value_id(),
            slider.control_id(),
            slider.track_id(),
            slider.range_id(),
            slider.thumb_id(0),
            slider.thumb_id(1),
        ];
        for (index, id) in ids.iter().enumerate() {
            assert_ne!(*id, slider.root_id());
            assert!(!ids[..index].contains(id));
        }

        let root = slider.root_with(div());
        assert_eq!(
            root.accessibility.relations.labelled_by(),
            Some(slider.label_id())
        );
        assert_eq!(
            root.accessibility.relations.described_by(),
            Some(slider.value_id())
        );
        assert_eq!(root.visual.background, None);

        let control = slider.control_with(div().bg(Color::rgb8(1, 2, 3)));
        assert_eq!(control.explicit_id, Some(slider.control_id()));
        assert!(control.accessibility.hidden);
        assert_eq!(control.app_region, Some(AppRegion::NoDrag));
        assert_eq!(control.visual.background, Some(Color::rgb8(1, 2, 3)));
        assert_eq!(control.user_select, UserSelect::None);

        // Indicator is Base UI's name for the existing range part.
        assert_eq!(
            slider.indicator_with(div()).explicit_id,
            Some(slider.range_id())
        );
        assert!(slider.indicator_with(div()).accessibility.hidden);
        assert_eq!(
            slider.label_with(text("Volume")).explicit_id,
            Some(slider.label_id())
        );
        assert_eq!(
            slider.value_with(text("20 – 80")).explicit_id,
            Some(slider.value_id())
        );

        let format = crate::ValueFormat::percent();
        assert_eq!(slider.display_value(&format).as_ref(), "20% – 80%");
        let single = Slider::new("one", &SliderState::new(0.0, 100.0, 30.0));
        assert_eq!(single.display_value(&format).as_ref(), "30%");

        let thumb = slider.thumb(1).expect("second thumb");
        assert_eq!(thumb.value_text(&format).as_ref(), "80%");
        assert_eq!(
            thumb.state(),
            SliderThumbState {
                index: 1,
                value: 80.0,
                fraction: 0.8,
                active: false,
                dragging: true,
                disabled: false,
            }
        );
        assert!(thumb.is_dragging());
        let active = slider.thumb(0).expect("first thumb");
        assert!(active.state().active);
        assert_eq!(active.state().index, 0);
    }
}