Skip to main content

bhoo/systems/material/
slider.rs

1use crate::*;
2
3#[derive(Clone, Copy, Default)]
4pub(crate) struct SliderTintColors {
5    pub primary: Option<Color>, pub background: Option<Color>,
6    pub secondary: Option<Color>, pub thumb: Option<Color>,
7    pub tick: Option<(f32,f32,f32,Color)>,
8    pub split_track: bool,
9    pub custom_thumb: Option<Option<(f32,f32,f32,Color)>>,
10}
11
12/// Configuration for Material 3 Expressive sliders and range sliders.
13#[derive(Clone, Copy, Debug, PartialEq)]
14pub struct SliderExpressiveOptions {
15    pub step_size: f32,
16    pub min_separation: f32,
17    pub show_value_bubble: bool,
18    pub enabled: bool,
19}
20impl Default for SliderExpressiveOptions {
21    fn default() -> Self {
22        Self {
23            step_size: 0.0,
24            min_separation: 0.0,
25            show_value_bubble: true,
26            enabled: true,
27        }
28    }
29}
30
31impl Frame<'_> {
32    pub fn slider(
33        &mut self,
34        id: u64,
35        label: &str,
36        value: &mut f32,
37        range: std::ops::RangeInclusive<f32>,
38        step: f32,
39        enabled: bool,
40    ) -> Response {
41        self.slider_directional(id, label, value, range, step, enabled, false)
42    }
43    #[allow(clippy::too_many_arguments)]
44    pub fn slider_directional(
45        &mut self, id: u64, label: &str, value: &mut f32,
46        range: std::ops::RangeInclusive<f32>, step: f32, enabled: bool, rtl: bool,
47    ) -> Response {
48        self.slider_directional_with_secondary(id, label, value, range, step, enabled, rtl, None)
49    }
50    /// Directional slider with an optional buffered fraction behind the primary track.
51    #[allow(clippy::too_many_arguments)]
52    pub fn slider_directional_with_secondary(
53        &mut self, id: u64, label: &str, value: &mut f32,
54        range: std::ops::RangeInclusive<f32>, step: f32, enabled: bool, rtl: bool,
55        secondary: Option<f32>,
56    ) -> Response {
57        self.slider_directional_tinted(id,label,value,range,step,enabled,rtl,secondary,SliderTintColors::default())
58    }
59    #[allow(clippy::too_many_arguments)]
60    pub(crate) fn slider_directional_tinted(
61        &mut self, id:u64, label:&str, value:&mut f32,
62        range:std::ops::RangeInclusive<f32>, step:f32, enabled:bool, rtl:bool,
63        secondary:Option<f32>, colors:SliderTintColors,
64    )->Response {
65        let b = self.allocate(48.0);
66        let mut r = self.interact(id, b, enabled);
67        let min = *range.start();
68        let max = *range.end();
69        assert!(
70            min.is_finite() && max.is_finite() && max > min,
71            "Slider range must be finite and increasing"
72        );
73        let old = *value;
74        *value = if value.is_finite() {
75            value.clamp(min, max)
76        } else {
77            min
78        };
79        let start = b.x + 11.0;
80        let width = (b.width - 22.0).max(1.0);
81        if r.enabled && !self.input.cancelled && !self.input.blurred {
82            if self.ui.active == Some(id)
83                && self.ui.capture == Capture::Pointer
84                && let Some(p) = self.input.pointer
85            {
86                let fraction = ((p[0] - start) / width).clamp(0.0, 1.0);
87                *value = min + (if rtl { 1.0 - fraction } else { fraction }) * (max - min);
88            }
89            let increment = if step.is_finite() && step > 0.0 {
90                step
91            } else {
92                (max - min) / 100.0
93            };
94            if let Some(key) = self.key_for(r) {
95                match key {
96                    Key::Left if rtl => *value += increment,
97                    Key::Right if rtl => *value -= increment,
98                    Key::Left | Key::Down => *value -= increment,
99                    Key::Right | Key::Up => *value += increment,
100                    Key::Home => *value = min,
101                    Key::End => *value = max,
102                    _ => {}
103                }
104            }
105            if step.is_finite() && step > 0.0 && *value != old {
106                *value = min + ((*value - min) / step).round() * step;
107            }
108            *value = value.clamp(min, max);
109        }
110        r.changed = *value != old;
111        let amount = (*value - min) / (max - min);
112        let t = self.ui.theme;
113        let track = Rect {
114            x: b.x + 8.0,
115            y: b.y + 22.0,
116            width: (b.width - 16.0).max(0.0),
117            height: 4.0,
118        };
119        let x = start + width * if rtl { 1.0 - amount } else { amount };
120        let thumb=match colors.custom_thumb {
121            Some(Some((w,h,r,color)))=>Some((w,h,r,colors.thumb.unwrap_or(color))),
122            Some(None)=>None,
123            None=>Some((20.0,20.0,10.0,colors.thumb.unwrap_or(if r.enabled{t.primary}else{t.text.opacity(0.38)}))),
124        };
125        let draw_track=|this:&mut Self| {
126        this.rect(
127            track,
128            2.0,
129            colors.background.unwrap_or(if r.enabled {
130                t.surface_variant
131            } else {
132                t.text.opacity(0.12)
133            }),
134        );
135        if let Some(secondary) = secondary.filter(|v| v.is_finite()) {
136            let buffered = secondary.clamp(0.0, 1.0);
137            let buffered_width = track.width * buffered;
138            this.rect(Rect { x: if rtl { track.x + track.width - buffered_width } else { track.x }, width: buffered_width, ..track }, 2.0,
139                colors.secondary.unwrap_or(if r.enabled { t.primary.opacity(0.38) } else { t.text.opacity(0.12) }));
140        }
141        this.rect(
142            Rect {
143                x: if rtl { x } else { track.x },
144                width: if rtl { (track.x + track.width - x).max(0.0) } else { (x - track.x).max(0.0) },
145                ..track
146            },
147            2.0,
148            colors.primary.unwrap_or(if r.enabled {
149                t.primary
150            } else {
151                t.text.opacity(0.38)
152            }),
153        );
154        if let Some((tick_width,tick_height,radius,color))=colors.tick {
155            let count=(max-min).round().clamp(0.0,1024.0) as usize;
156            if count>1 {
157                for i in 0..=count {
158                    let center=start+width*i as f32/count as f32;
159                    this.rect(Rect{x:center-tick_width/2.0,y:b.y+24.0-tick_height/2.0,width:tick_width,height:tick_height},radius,color);
160                }
161            }
162        }
163        };
164        if colors.split_track && let Some((thumb_width,thumb_height,_,_))=thumb {
165            let clip=self.clip;
166            let left=x-thumb_width/2.0;let right=x+thumb_width/2.0;
167            let top=b.y+24.0-thumb_height/2.0;let bottom=b.y+24.0+thumb_height/2.0;
168            let regions=[
169                Rect{x:clip.x,y:clip.y,width:(left-clip.x).max(0.0),height:clip.height},
170                Rect{x:right,y:clip.y,width:(clip.x+clip.width-right).max(0.0),height:clip.height},
171                Rect{x:left,y:clip.y,width:thumb_width,height:(top-clip.y).max(0.0)},
172                Rect{x:left,y:bottom,width:thumb_width,height:(clip.y+clip.height-bottom).max(0.0)},
173            ];
174            for (index,region) in regions.into_iter().enumerate() {
175                if index>=2 && thumb_height>=4.0 && colors.tick.is_none_or(|(_,height,_,_)|height<=thumb_height) {continue;}
176                let region=region.intersect(clip);
177                if region.width>0.0 && region.height>0.0 {
178                    self.commands.push(DrawCommand::PushClip(region));draw_track(self);self.commands.push(DrawCommand::PopClip);
179                }
180            }
181        } else {draw_track(self);}
182        self.state_layer(
183            Response {
184                bounds: Rect {
185                    x: x - 20.0,
186                    y: b.y + 4.0,
187                    width: 40.0,
188                    height: 40.0,
189                },
190                ..r
191            },
192            20.0,
193            t.primary,
194        );
195        if let Some((w,h,radius,color))=thumb {self.rect(Rect{x:x-w/2.0,y:b.y+24.0-h/2.0,width:w,height:h},radius,color);}
196        self.focus_ring(r);
197        self.semantic(
198            r,
199            Role::Slider,
200            label,
201            false,
202            None,
203            Some(*value),
204            Some([min, max]),
205        );
206        r
207    }
208    /// Two independently focusable endpoints; drag follows input without animation.
209    pub fn range_slider(
210        &mut self,
211        ids: [u64; 2],
212        label: &str,
213        values: &mut [f32; 2],
214        range: std::ops::RangeInclusive<f32>,
215        step: f32,
216        enabled: bool,
217    ) -> [Response; 2] {
218        let min = *range.start();
219        let max = *range.end();
220        assert!(min.is_finite() && max.is_finite() && min < max);
221        for value in values.iter_mut() {
222            *value = if value.is_finite() {
223                value.clamp(min, max)
224            } else {
225                min
226            };
227        }
228        if values[0] > values[1] {
229            values.swap(0, 1);
230        }
231        let previous = *values;
232        let b = self.allocate(48.0);
233        let width = (b.width - 22.0).max(1.0);
234        let start = b.x + 11.0;
235        let mut responses = [Response::default(); 2];
236        for i in 0..2 {
237            let x = start + (values[i] - min) / (max - min) * width;
238            responses[i] = self.interact(
239                ids[i],
240                Rect {
241                    x: x - 12.0,
242                    width: 24.0,
243                    ..b
244                },
245                enabled,
246            );
247        }
248        if enabled
249            && self.enabled
250            && self.input.pressed
251            && !self.input.cancelled
252            && self.ui.active.is_none()
253            && let Some(p) = self.input.pointer
254            && b.intersect(self.clip).contains(p)
255        {
256            let value = min + ((p[0] - start) / width).clamp(0.0, 1.0) * (max - min);
257            let i = usize::from((value - values[1]).abs() < (value - values[0]).abs());
258            self.ui.active = Some(ids[i]);
259            self.ui.capture = Capture::Pointer;
260            self.ui.focused = Some(ids[i]);
261            self.ui.keyboard_focus = false;
262        }
263        for i in 0..2 {
264            let r = &mut responses[i];
265            r.focused = self.ui.focused == Some(ids[i]);
266            if r.enabled && !self.input.cancelled && !self.input.blurred {
267                if self.ui.active == Some(ids[i])
268                    && self.ui.capture == Capture::Pointer
269                    && let Some(p) = self.input.pointer
270                {
271                    values[i] = min + ((p[0] - start) / width).clamp(0.0, 1.0) * (max - min);
272                }
273                let increment = if step.is_finite() && step > 0.0 {
274                    step
275                } else {
276                    (max - min) / 100.0
277                };
278                if let Some(key) = self.key_for(*r) {
279                    match key {
280                        Key::Left | Key::Down => values[i] -= increment,
281                        Key::Right | Key::Up => values[i] += increment,
282                        Key::Home => values[i] = min,
283                        Key::End => values[i] = max,
284                        _ => {}
285                    }
286                }
287                if step.is_finite() && step > 0.0 && values[i] != previous[i] {
288                    values[i] = min + ((values[i] - min) / step).round() * step;
289                }
290                values[i] = if i == 0 {
291                    values[i].clamp(min, values[1])
292                } else {
293                    values[i].clamp(values[0], max)
294                };
295            }
296            r.changed = values[i] != previous[i];
297        }
298        let t = self.ui.theme;
299        let x0 = start + (values[0] - min) / (max - min) * width;
300        let x1 = start + (values[1] - min) / (max - min) * width;
301        self.rect(
302            Rect {
303                x: b.x + 8.0,
304                y: b.y + 22.0,
305                width: (b.width - 16.0).max(0.0),
306                height: 4.0,
307            },
308            2.0,
309            t.surface_variant,
310        );
311        self.rect(
312            Rect {
313                x: x0,
314                y: b.y + 22.0,
315                width: x1 - x0,
316                height: 4.0,
317            },
318            2.0,
319            t.primary
320                .opacity(if enabled && self.enabled { 1.0 } else { 0.38 }),
321        );
322        for i in 0..2 {
323            let x = if i == 0 { x0 } else { x1 };
324            self.state_layer(
325                Response {
326                    bounds: Rect {
327                        x: x - 20.0,
328                        y: b.y + 4.0,
329                        width: 40.0,
330                        height: 40.0,
331                    },
332                    ..responses[i]
333                },
334                20.0,
335                t.primary,
336            );
337            self.rect(
338                Rect {
339                    x: x - 10.0,
340                    y: b.y + 14.0,
341                    width: 20.0,
342                    height: 20.0,
343                },
344                10.0,
345                t.primary
346                    .opacity(if responses[i].enabled { 1.0 } else { 0.38 }),
347            );
348            responses[i].bounds = Rect {
349                x: x - 12.0,
350                width: 24.0,
351                ..b
352            };
353            self.focus_ring(responses[i]);
354            self.semantic(
355                responses[i],
356                Role::Slider,
357                label,
358                false,
359                None,
360                Some(values[i]),
361                Some([min, max]),
362            );
363        }
364        responses
365    }
366
367    fn paint_slider_value_bubble(&mut self, hx: f32, anchor_y: f32, value: f32) {
368        let viewport = self.viewport();
369        let text = if (value - value.round()).abs() < 0.005 {
370            format!("{}", value.round() as i64)
371        } else {
372            format!("{value:.1}")
373        };
374        let style = TextStyle {
375            size: 12.0,
376            weight: 500,
377            align: 1,
378            padding: 8.0,
379            letter_spacing: 0.4,
380            ..TextStyle::default()
381        };
382        let pill_w = (self.text_width(&text, style) + 20.0).max(40.0);
383        let pill_h = 32.0;
384        let pill_x = (hx - pill_w * 0.5).clamp(
385            viewport.x + 4.0,
386            (viewport.x + viewport.width - pill_w - 4.0).max(viewport.x + 4.0),
387        );
388        let pill_y = anchor_y - pill_h - 6.0;
389        let pill = Rect {
390            x: pill_x,
391            y: pill_y,
392            width: pill_w,
393            height: pill_h,
394        };
395        let t = self.ui.theme;
396        let tip_x = hx.clamp(pill.x + 8.0, pill.x + pill.width - 8.0);
397        let tip_base_y = pill.y + pill.height - 2.0;
398        let tip_y = pill.y + pill.height + 4.0;
399        self.ui.overlays.push(DrawCommand::RoundedRect {
400            bounds: pill,
401            radius: 16.0,
402            color: t.inverse_surface,
403        });
404        self.ui.overlays.push(DrawCommand::Line {
405            from: [tip_x - 4.0, tip_base_y],
406            to: [tip_x, tip_y],
407            width: 4.0,
408            color: t.inverse_surface,
409        });
410        self.ui.overlays.push(DrawCommand::Line {
411            from: [tip_x + 4.0, tip_base_y],
412            to: [tip_x, tip_y],
413            width: 4.0,
414            color: t.inverse_surface,
415        });
416        let cached = self.ui.text(&text);
417        self.ui.overlays.push(DrawCommand::Text {
418            bounds: pill,
419            text: cached,
420            color: t.inverse_on_surface,
421            style,
422        });
423    }
424
425    /// Material 3 Expressive slider: 16dp rounded tracks, 4dp×44dp vertical pill handle
426    /// with 6dp track gaps, 4dp stop indicator / discrete ticks, and teardrop value bubble.
427    pub fn expressive_slider(
428        &mut self,
429        id: u64,
430        label: &str,
431        value: &mut f32,
432        range: std::ops::RangeInclusive<f32>,
433        options: SliderExpressiveOptions,
434    ) -> Response {
435        let min = *range.start();
436        let max = *range.end();
437        assert!(
438            min.is_finite() && max.is_finite() && max > min,
439            "Slider range must be finite and increasing"
440        );
441        let step = if options.step_size.is_finite() && options.step_size > 0.0 {
442            options.step_size
443        } else {
444            0.0
445        };
446        let b = self.allocate(48.0);
447        let mut r = self.interact(id, b, options.enabled);
448        let old = *value;
449        *value = if value.is_finite() {
450            value.clamp(min, max)
451        } else {
452            min
453        };
454        let track_x = b.x + 8.0;
455        let track_w = (b.width - 16.0).max(1.0);
456        let track_right = track_x + track_w;
457        let start = track_x + 2.0;
458        let travel = (track_w - 4.0).max(1.0);
459        if r.enabled && !self.input.cancelled && !self.input.blurred {
460            if self.ui.active == Some(id)
461                && self.ui.capture == Capture::Pointer
462                && let Some(p) = self.input.pointer
463            {
464                let fraction = ((p[0] - start) / travel).clamp(0.0, 1.0);
465                *value = min + fraction * (max - min);
466            }
467            let increment = if step > 0.0 {
468                step
469            } else {
470                (max - min) / 100.0
471            };
472            if let Some(key) = self.key_for(r) {
473                match key {
474                    Key::Left | Key::Down => *value -= increment,
475                    Key::Right | Key::Up => *value += increment,
476                    Key::Home => *value = min,
477                    Key::End => *value = max,
478                    _ => {}
479                }
480            }
481            if step > 0.0 && *value != old {
482                *value = min + ((*value - min) / step).round() * step;
483            }
484            *value = value.clamp(min, max);
485        }
486        r.changed = *value != old;
487        let amount = (*value - min) / (max - min);
488        let hx = start + travel * amount;
489        let cy = b.y + 24.0;
490        let track_y = cy - 8.0;
491        let gap_half = 8.0; // 2dp half-handle + 6dp handle-to-track gap
492        let t = self.ui.theme;
493        let active_color = if r.enabled {
494            t.primary
495        } else {
496            t.text.opacity(0.38)
497        };
498        let inactive_color = if r.enabled {
499            t.secondary_container
500        } else {
501            t.text.opacity(0.12)
502        };
503        let active_stop_color = if r.enabled {
504            t.on_primary
505        } else {
506            t.inverse_on_surface.opacity(0.38)
507        };
508        let inactive_stop_color = if r.enabled {
509            t.on_secondary_container
510        } else {
511            t.text.opacity(0.38)
512        };
513        let active_end = (hx - gap_half).clamp(track_x, track_right);
514        if active_end > track_x {
515            let w = active_end - track_x;
516            self.rect(
517                Rect {
518                    x: track_x,
519                    y: track_y,
520                    width: w,
521                    height: 16.0,
522                },
523                8.0_f32.min(w * 0.5),
524                active_color,
525            );
526        }
527        let inactive_start = (hx + gap_half).clamp(track_x, track_right);
528        if track_right > inactive_start {
529            let w = track_right - inactive_start;
530            self.rect(
531                Rect {
532                    x: inactive_start,
533                    y: track_y,
534                    width: w,
535                    height: 16.0,
536                },
537                8.0_f32.min(w * 0.5),
538                inactive_color,
539            );
540        }
541        if step > 0.0 {
542            let steps = ((max - min) / step).round().clamp(1.0, 128.0) as usize;
543            for i in 0..=steps {
544                let tx = start + travel * (i as f32 / steps as f32);
545                if (tx - hx).abs() < gap_half {
546                    continue;
547                }
548                self.rect(
549                    Rect {
550                        x: tx - 2.0,
551                        y: cy - 2.0,
552                        width: 4.0,
553                        height: 4.0,
554                    },
555                    2.0,
556                    if tx < hx {
557                        active_stop_color
558                    } else {
559                        inactive_stop_color
560                    },
561                );
562            }
563        } else if inactive_start <= track_right - 8.0 {
564            self.rect(
565                Rect {
566                    x: track_right - 8.0,
567                    y: cy - 2.0,
568                    width: 4.0,
569                    height: 4.0,
570                },
571                2.0,
572                inactive_stop_color,
573            );
574        }
575        self.rect(
576            Rect {
577                x: hx - 2.0,
578                y: cy - 22.0,
579                width: 4.0,
580                height: 44.0,
581            },
582            2.0,
583            active_color,
584        );
585        if options.show_value_bubble && r.enabled && (r.pressed || r.focused) {
586            self.paint_slider_value_bubble(hx, cy - 22.0, *value);
587        }
588        self.focus_ring(r);
589        self.semantic(
590            r,
591            Role::Slider,
592            label,
593            false,
594            None,
595            Some(*value),
596            Some([min, max]),
597        );
598        r
599    }
600
601    /// Material 3 Expressive range slider with two 4dp×44dp vertical pill handles,
602    /// 6dp track gaps, `min_separation` clamping, discrete ticks, and teardrop value bubble.
603    pub fn expressive_range_slider(
604        &mut self,
605        ids: [u64; 2],
606        label: &str,
607        values: &mut [f32; 2],
608        range: std::ops::RangeInclusive<f32>,
609        options: SliderExpressiveOptions,
610    ) -> [Response; 2] {
611        let min = *range.start();
612        let max = *range.end();
613        assert!(min.is_finite() && max.is_finite() && min < max);
614        let step = if options.step_size.is_finite() && options.step_size > 0.0 {
615            options.step_size
616        } else {
617            0.0
618        };
619        let min_sep = if options.min_separation.is_finite() && options.min_separation > 0.0 {
620            options.min_separation.min(max - min)
621        } else {
622            0.0
623        };
624        for value in values.iter_mut() {
625            *value = if value.is_finite() {
626                value.clamp(min, max)
627            } else {
628                min
629            };
630        }
631        if values[0] > values[1] {
632            values.swap(0, 1);
633        }
634        if values[1] - values[0] < min_sep {
635            values[1] = (values[0] + min_sep).min(max);
636            if values[1] - values[0] < min_sep {
637                values[0] = (values[1] - min_sep).max(min);
638            }
639        }
640        let previous = *values;
641        let b = self.allocate(48.0);
642        let track_x = b.x + 8.0;
643        let track_w = (b.width - 16.0).max(1.0);
644        let track_right = track_x + track_w;
645        let start = track_x + 2.0;
646        let travel = (track_w - 4.0).max(1.0);
647        let mut responses = [Response::default(); 2];
648        for i in 0..2 {
649            let x = start + (values[i] - min) / (max - min) * travel;
650            responses[i] = self.interact(
651                ids[i],
652                Rect {
653                    x: x - 12.0,
654                    width: 24.0,
655                    ..b
656                },
657                options.enabled,
658            );
659        }
660        if options.enabled
661            && self.enabled
662            && self.input.pressed
663            && !self.input.cancelled
664            && self.ui.active.is_none()
665            && let Some(p) = self.input.pointer
666            && b.intersect(self.clip).contains(p)
667        {
668            let value = min + ((p[0] - start) / travel).clamp(0.0, 1.0) * (max - min);
669            let i = usize::from((value - values[1]).abs() < (value - values[0]).abs());
670            self.ui.active = Some(ids[i]);
671            self.ui.capture = Capture::Pointer;
672            self.ui.focused = Some(ids[i]);
673            self.ui.keyboard_focus = false;
674            responses[i].pressed = true;
675        }
676        for i in 0..2 {
677            let r = &mut responses[i];
678            r.focused = r.enabled && self.ui.focused == Some(ids[i]);
679            r.pressed = r.enabled
680                && self.ui.active == Some(ids[i])
681                && !self.input.released
682                && !self.input.cancelled;
683            if r.enabled && !self.input.cancelled && !self.input.blurred {
684                if self.ui.active == Some(ids[i])
685                    && self.ui.capture == Capture::Pointer
686                    && let Some(p) = self.input.pointer
687                {
688                    values[i] = min + ((p[0] - start) / travel).clamp(0.0, 1.0) * (max - min);
689                }
690                let increment = if step > 0.0 {
691                    step
692                } else {
693                    (max - min) / 100.0
694                };
695                if let Some(key) = self.key_for(*r) {
696                    match key {
697                        Key::Left | Key::Down => values[i] -= increment,
698                        Key::Right | Key::Up => values[i] += increment,
699                        Key::Home => values[i] = min,
700                        Key::End => values[i] = max,
701                        _ => {}
702                    }
703                }
704                if step > 0.0 && values[i] != previous[i] {
705                    values[i] = min + ((values[i] - min) / step).round() * step;
706                }
707                if i == 0 {
708                    let upper = (values[1] - min_sep).max(min);
709                    if step > 0.0 && values[0] > upper {
710                        let floored = min + ((upper - min) / step).floor() * step;
711                        values[0] = floored.clamp(min, upper);
712                    } else {
713                        values[0] = values[0].clamp(min, upper);
714                    }
715                } else {
716                    let lower = (values[0] + min_sep).min(max);
717                    if step > 0.0 && values[1] < lower {
718                        let ceiled = min + ((lower - min) / step).ceil() * step;
719                        values[1] = ceiled.clamp(lower, max);
720                    } else {
721                        values[1] = values[1].clamp(lower, max);
722                    }
723                }
724            }
725            r.changed = values[i] != previous[i];
726        }
727        let hx0 = start + (values[0] - min) / (max - min) * travel;
728        let hx1 = start + (values[1] - min) / (max - min) * travel;
729        let cy = b.y + 24.0;
730        let track_y = cy - 8.0;
731        let gap_half = 8.0; // 2dp half-handle + 6dp handle-to-track gap
732        let t = self.ui.theme;
733        let is_enabled = options.enabled && self.enabled;
734        let active_color = if is_enabled {
735            t.primary
736        } else {
737            t.text.opacity(0.38)
738        };
739        let inactive_color = if is_enabled {
740            t.secondary_container
741        } else {
742            t.text.opacity(0.12)
743        };
744        let active_stop_color = if is_enabled {
745            t.on_primary
746        } else {
747            t.inverse_on_surface.opacity(0.38)
748        };
749        let inactive_stop_color = if is_enabled {
750            t.on_secondary_container
751        } else {
752            t.text.opacity(0.38)
753        };
754        // Left inactive track
755        let left_end = (hx0 - gap_half).clamp(track_x, track_right);
756        if left_end > track_x {
757            let w = left_end - track_x;
758            self.rect(
759                Rect {
760                    x: track_x,
761                    y: track_y,
762                    width: w,
763                    height: 16.0,
764                },
765                8.0_f32.min(w * 0.5),
766                inactive_color,
767            );
768        }
769        // Center active track
770        let mid_start = (hx0 + gap_half).clamp(track_x, track_right);
771        let mid_end = (hx1 - gap_half).clamp(track_x, track_right);
772        if mid_end > mid_start {
773            let w = mid_end - mid_start;
774            self.rect(
775                Rect {
776                    x: mid_start,
777                    y: track_y,
778                    width: w,
779                    height: 16.0,
780                },
781                8.0_f32.min(w * 0.5),
782                active_color,
783            );
784        }
785        // Right inactive track
786        let right_start = (hx1 + gap_half).clamp(track_x, track_right);
787        if track_right > right_start {
788            let w = track_right - right_start;
789            self.rect(
790                Rect {
791                    x: right_start,
792                    y: track_y,
793                    width: w,
794                    height: 16.0,
795                },
796                8.0_f32.min(w * 0.5),
797                inactive_color,
798            );
799        }
800        if step > 0.0 {
801            let steps = ((max - min) / step).round().clamp(1.0, 128.0) as usize;
802            for i in 0..=steps {
803                let tx = start + travel * (i as f32 / steps as f32);
804                if (tx - hx0).abs() < gap_half || (tx - hx1).abs() < gap_half {
805                    continue;
806                }
807                self.rect(
808                    Rect {
809                        x: tx - 2.0,
810                        y: cy - 2.0,
811                        width: 4.0,
812                        height: 4.0,
813                    },
814                    2.0,
815                    if tx > hx0 && tx < hx1 {
816                        active_stop_color
817                    } else {
818                        inactive_stop_color
819                    },
820                );
821            }
822        } else {
823            if left_end >= track_x + 8.0 {
824                self.rect(
825                    Rect {
826                        x: track_x + 4.0,
827                        y: cy - 2.0,
828                        width: 4.0,
829                        height: 4.0,
830                    },
831                    2.0,
832                    inactive_stop_color,
833                );
834            }
835            if right_start <= track_right - 8.0 {
836                self.rect(
837                    Rect {
838                        x: track_right - 8.0,
839                        y: cy - 2.0,
840                        width: 4.0,
841                        height: 4.0,
842                    },
843                    2.0,
844                    inactive_stop_color,
845                );
846            }
847        }
848        for i in 0..2 {
849            let hx = if i == 0 { hx0 } else { hx1 };
850            self.rect(
851                Rect {
852                    x: hx - 2.0,
853                    y: cy - 22.0,
854                    width: 4.0,
855                    height: 44.0,
856                },
857                2.0,
858                active_color,
859            );
860            responses[i].bounds = Rect {
861                x: hx - 12.0,
862                width: 24.0,
863                ..b
864            };
865            if options.show_value_bubble
866                && responses[i].enabled
867                && (responses[i].pressed || responses[i].focused)
868            {
869                self.paint_slider_value_bubble(hx, cy - 22.0, values[i]);
870            }
871            self.focus_ring(responses[i]);
872            self.semantic(
873                responses[i],
874                Role::Slider,
875                label,
876                false,
877                None,
878                Some(values[i]),
879                Some([min, max]),
880            );
881        }
882        responses
883    }
884
885    /// Thick horizontal Material 3 Expressive pill-fill slider (brightness/media HUD style)
886    /// with optional embedded `SystemIcon` inside the active pill.
887    pub fn pill_slider(
888        &mut self,
889        id: u64,
890        bounds: Rect,
891        label: &str,
892        value: &mut f32,
893        range: std::ops::RangeInclusive<f32>,
894        icon: Option<SystemIcon>,
895        enabled: bool,
896    ) -> Response {
897        let min = *range.start();
898        let max = (*range.end()).max(min + 1e-6);
899        *value = value.clamp(min, max);
900        let mut r = self.interact(id, bounds, enabled);
901        let mut frac = ((*value - min) / (max - min)).clamp(0.0, 1.0);
902        if r.pressed && let Some(p) = self.input.pointer {
903            let next = ((p[0] - bounds.x) / bounds.width.max(1.0)).clamp(0.0, 1.0);
904            let next_val = min + next * (max - min);
905            if (next_val - *value).abs() > 1e-5 {
906                *value = next_val;
907                frac = next;
908                r.changed = true;
909            }
910        }
911        if r.focused && enabled {
912            let step = (max - min) * 0.05;
913            if self.pressed_key(Key::Right) || self.pressed_key(Key::Up) {
914                *value = (*value + step).clamp(min, max);
915                frac = ((*value - min) / (max - min)).clamp(0.0, 1.0);
916                r.changed = true;
917            } else if self.pressed_key(Key::Left) || self.pressed_key(Key::Down) {
918                *value = (*value - step).clamp(min, max);
919                frac = ((*value - min) / (max - min)).clamp(0.0, 1.0);
920                r.changed = true;
921            }
922        }
923        let t = self.theme();
924        let track_h = (bounds.height * 0.25).clamp(4.0, 12.0);
925        let track = Rect {
926            x: bounds.x,
927            y: bounds.y + (bounds.height - track_h) * 0.5,
928            width: bounds.width,
929            height: track_h,
930        };
931        self.paint_rect(track, track_h * 0.5, t.surface_highest);
932        let fill_w = (bounds.height + (bounds.width - bounds.height).max(0.0) * frac).min(bounds.width);
933        let pill = Rect { width: fill_w, ..bounds };
934        let rad = (pill.height * 0.5).min(pill.width * 0.5);
935        self.paint_rect(pill, rad, if r.enabled { t.primary } else { t.text.opacity(0.12) });
936        if let Some(ic) = icon {
937            let sz = (bounds.height * 0.52).clamp(16.0, 24.0);
938            let icon_rect = Rect {
939                x: pill.x + (pill.width - sz - 10.0).max(8.0),
940                y: pill.y + (pill.height - sz) * 0.5,
941                width: sz,
942                height: sz,
943            };
944            self.paint_system_icon(icon_rect, ic, if r.enabled { t.on_primary } else { t.text.opacity(0.38) });
945        }
946        self.focus_ring(r);
947        self.semantic(r, Role::Slider, label, false, None, Some(*value), Some([min, max]));
948        r
949    }
950
951    /// Vertical Material 3 Expressive pill-fill slider (volume HUD style) with bottom-anchored
952    /// active fill and optional embedded `SystemIcon`.
953    pub fn vertical_pill_slider(
954        &mut self,
955        id: u64,
956        bounds: Rect,
957        label: &str,
958        value: &mut f32,
959        range: std::ops::RangeInclusive<f32>,
960        icon: Option<SystemIcon>,
961        enabled: bool,
962    ) -> Response {
963        let min = *range.start();
964        let max = (*range.end()).max(min + 1e-6);
965        *value = value.clamp(min, max);
966        let mut r = self.interact(id, bounds, enabled);
967        let mut frac = ((*value - min) / (max - min)).clamp(0.0, 1.0);
968        if r.pressed && let Some(p) = self.input.pointer {
969            let next = (1.0 - (p[1] - bounds.y) / bounds.height.max(1.0)).clamp(0.0, 1.0);
970            let next_val = min + next * (max - min);
971            if (next_val - *value).abs() > 1e-5 {
972                *value = next_val;
973                frac = next;
974                r.changed = true;
975            }
976        }
977        if r.focused && enabled {
978            let step = (max - min) * 0.05;
979            if self.pressed_key(Key::Up) || self.pressed_key(Key::Right) {
980                *value = (*value + step).clamp(min, max);
981                frac = ((*value - min) / (max - min)).clamp(0.0, 1.0);
982                r.changed = true;
983            } else if self.pressed_key(Key::Down) || self.pressed_key(Key::Left) {
984                *value = (*value - step).clamp(min, max);
985                frac = ((*value - min) / (max - min)).clamp(0.0, 1.0);
986                r.changed = true;
987            }
988        }
989        let t = self.theme();
990        let rad = (bounds.width * 0.5).min(bounds.height * 0.5);
991        self.paint_rect(bounds, rad, t.surface_highest);
992        let min_fill = bounds.width.min(bounds.height);
993        let fill_h = (min_fill + (bounds.height - min_fill).max(0.0) * frac).min(bounds.height);
994        let fill = Rect {
995            x: bounds.x,
996            y: bounds.y + bounds.height - fill_h,
997            width: bounds.width,
998            height: fill_h,
999        };
1000        self.paint_rect(fill, rad, if r.enabled { t.primary } else { t.text.opacity(0.12) });
1001        if let Some(ic) = icon {
1002            let sz = (bounds.width * 0.48).clamp(16.0, 24.0);
1003            let icon_rect = Rect {
1004                x: fill.x + (fill.width - sz) * 0.5,
1005                y: fill.y + fill.height - sz - 10.0,
1006                width: sz,
1007                height: sz,
1008            };
1009            self.paint_system_icon(icon_rect, ic, if r.enabled { t.on_primary } else { t.text.opacity(0.38) });
1010        }
1011        self.focus_ring(r);
1012        self.semantic(r, Role::Slider, label, false, None, Some(*value), Some([min, max]));
1013        r
1014    }
1015}
1016
1017#[cfg(test)]
1018#[path = "slider.spec.rs"]
1019mod tests;