Skip to main content

bhoo/systems/material/
progress.rs

1//! Pinned Compose Material 3 progress timelines (Apache-2.0, Copyright 2022 The Android Open Source Project).
2//! Independent elapsed-time evaluation; no per-indicator timer or texture.
3use crate::timing::Easing;
4use crate::*;
5
6/// Material 3 Expressive wavy progress configuration (pinned to MDC-Android `BaseProgressIndicatorSpec`).
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct WavyProgressOptions {
9    pub wavelength: f32,
10    pub amplitude: f32,
11    pub wave_speed: f32,
12    pub track_thickness: f32,
13    pub stop_indicator_size: f32,
14    pub track_gap: f32,
15}
16impl WavyProgressOptions {
17    /// Pinned M3 Expressive linear wavy defaults (`wavelength: 40dp`, `amplitude: 3dp`).
18    pub const fn linear() -> Self {
19        Self {
20            wavelength: 40.0,
21            amplitude: 3.0,
22            wave_speed: 40.0,
23            track_thickness: 4.0,
24            stop_indicator_size: 4.0,
25            track_gap: 4.0,
26        }
27    }
28    /// Pinned M3 Expressive circular wavy defaults (`wavelength: 16dp`, `amplitude: 3dp`).
29    pub const fn circular() -> Self {
30        Self {
31            wavelength: 16.0,
32            amplitude: 3.0,
33            wave_speed: 40.0,
34            track_thickness: 4.0,
35            stop_indicator_size: 4.0,
36            track_gap: 4.0,
37        }
38    }
39}
40impl Default for WavyProgressOptions {
41    fn default() -> Self {
42        Self::linear()
43    }
44}
45
46/// Smooth M3 amplitude ramp in `[0, 1]`, tapering to `0` near `0%` and `100%`
47/// (`DeterminateDrawable.FULL_AMPLITUDE_PROGRESS_MIN = 0.1`, `MAX = 0.9`).
48pub fn wavy_amplitude_ramp(progress: f32) -> f32 {
49    if !progress.is_finite() {
50        return 0.0;
51    }
52    let p = progress.clamp(0.0, 1.0);
53    const MIN_RAMP: f32 = 0.1;
54    const MAX_RAMP: f32 = 0.9;
55    let edge = if p < MIN_RAMP {
56        p / MIN_RAMP
57    } else if p > MAX_RAMP {
58        (1.0 - p) / (1.0 - MAX_RAMP)
59    } else {
60        1.0
61    };
62    let t = edge.clamp(0.0, 1.0);
63    t * t * (3.0 - 2.0 * t)
64}
65
66pub(crate) fn sanitize_wavy(options: WavyProgressOptions, default_wavelength: f32) -> WavyProgressOptions {
67    let valid_pos = |v: f32, fallback: f32| if v.is_finite() && v > 0.0 { v } else { fallback };
68    let valid_nonneg = |v: f32, fallback: f32| if v.is_finite() && v >= 0.0 { v } else { fallback };
69    WavyProgressOptions {
70        wavelength: valid_pos(options.wavelength, default_wavelength),
71        amplitude: valid_nonneg(options.amplitude, 3.0),
72        wave_speed: if options.wave_speed.is_finite() { options.wave_speed } else { 40.0 },
73        track_thickness: valid_pos(options.track_thickness, 4.0),
74        stop_indicator_size: valid_nonneg(options.stop_indicator_size, 4.0),
75        track_gap: valid_nonneg(options.track_gap, 4.0),
76    }
77}
78#[derive(Clone, Copy)]
79pub(crate) struct ClassicProgressColors {
80    pub primary: Color,
81    pub background: Color,
82    pub secondary: Color,
83    pub indeterminate: Color,
84}
85impl ClassicProgressColors {
86    pub(crate) fn from_theme(theme: Theme) -> Self {
87        Self { primary: theme.primary, background: theme.surface_variant, secondary: theme.primary.opacity(0.38), indeterminate: theme.primary }
88    }
89}
90pub fn linear_segments(elapsed: f64) -> [[f32; 2]; 2] {
91    let ms = (elapsed.max(0.0) % 1.750) * 1000.0;
92    let eased = |delay: f64, duration: f64| {
93        Easing::EmphasizedAccelerate.sample(((ms - delay) / duration).clamp(0.0, 1.0) as f32)
94    };
95    [
96        [eased(250.0, 1000.0), eased(0.0, 1000.0)],
97        [eased(900.0, 850.0), eased(650.0, 850.0)],
98    ]
99}
100pub fn circular_sample(elapsed: f64) -> (f32, f32) {
101    let t = elapsed.max(0.0) % 6.0;
102    let quadrant = (t / 1.5).floor();
103    let local = t % 1.5;
104    let extra = quadrant as f32 * 90.0
105        + 90.0 * Easing::EmphasizedDecelerate.sample((local / 0.3).min(1.0) as f32);
106    let p = if t < 3.0 {
107        0.1 + 0.77 * Easing::Standard.sample((t / 3.0) as f32)
108    } else {
109        0.87 - 0.77 * Easing::Standard.sample(((t - 3.0) / 3.0) as f32)
110    };
111    (
112        ((t / 6.0 * 1080.0) as f32 + extra).to_radians(),
113        p * std::f32::consts::TAU,
114    )
115}
116impl Frame<'_> {
117    pub fn progress_circular(&mut self, id: u64, label: &str, value: Option<f32>) {
118        let mut b = self.allocate(40.0);
119        b.width = b.width.min(40.0);
120        let t = self.ui.theme;
121        let value = value.filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0));
122        let visible = b.intersect(self.clip);
123        let moving = value.is_none()
124            && visible.width > 0.0
125            && visible.height > 0.0
126            && !self.reduced_motion();
127        let (start, sweep) = if let Some(value) = value {
128            let v = self.ui.motion.effect(id, 3, value, value, Spring::PROGRESS);
129            (-std::f32::consts::FRAC_PI_2, v * std::f32::consts::TAU)
130        } else if moving {
131            self.ui.continuous = true;
132            circular_sample(self.ui.motion.elapsed(id, 31))
133        } else {
134            (-std::f32::consts::FRAC_PI_2, std::f32::consts::FRAC_PI_2)
135        };
136        let gap =
137            (8.0 / (std::f32::consts::PI * b.width.max(1.0)) * std::f32::consts::TAU).min(sweep);
138        self.commands.push(DrawCommand::Arc {
139            bounds: b,
140            start: start + sweep + gap,
141            sweep: (std::f32::consts::TAU - sweep - 2.0 * gap).max(0.0),
142            stroke: 4.0,
143            color: t.control,
144            rounded: true,
145        });
146        self.commands.push(DrawCommand::Arc {
147            bounds: b,
148            start,
149            sweep,
150            stroke: 4.0,
151            color: t.primary,
152            rounded: true,
153        });
154        self.semantic(
155            Response {
156                id,
157                bounds: b,
158                enabled: self.enabled,
159                ..Response::default()
160            },
161            Role::Progress,
162            label,
163            false,
164            None,
165            value,
166            value.map(|_| [0.0, 1.0]),
167        );
168    }
169    /// Material 3 Expressive wavy circular progress indicator (`48dp` default container).
170    /// Draws a smooth sinusoidal active wave separated by `track_gap` from the straight inactive arc.
171    pub fn wavy_circular_progress(
172        &mut self,
173        id: u64,
174        label: &str,
175        value: Option<f32>,
176        options: WavyProgressOptions,
177    ) {
178        let opt = sanitize_wavy(options, 16.0);
179        let size = 48.0_f32.max(opt.track_thickness + opt.amplitude * 2.0 + 16.0);
180        let mut b = self.allocate(size);
181        b.width = b.width.min(size);
182        let t = self.ui.theme;
183        let value = value.filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0));
184        let visible = b.intersect(self.clip);
185        let is_visible = visible.width > 0.0 && visible.height > 0.0;
186        let reduced = self.reduced_motion();
187        let (start, sweep, amp_factor, gap_scale) = if let Some(v) = value {
188            let a = self.ui.motion.effect(id, 3, v, v, Spring::PROGRESS);
189            (
190                -std::f32::consts::FRAC_PI_2,
191                a * std::f32::consts::TAU,
192                wavy_amplitude_ramp(a),
193                (a / 0.01).clamp(0.0, 1.0),
194            )
195        } else if is_visible && !reduced {
196            self.ui.continuous = true;
197            let (s, sw) = circular_sample(self.ui.motion.elapsed(id, 31));
198            (s, sw, 1.0, 1.0)
199        } else {
200            (
201                -std::f32::consts::FRAC_PI_2,
202                std::f32::consts::FRAC_PI_2,
203                1.0,
204                1.0,
205            )
206        };
207        let cx = b.x + b.width * 0.5;
208        let cy = b.y + b.height * 0.5;
209        let half_t = opt.track_thickness * 0.5;
210        let base_r = (b.width.min(b.height) * 0.5 - half_t - opt.amplitude).max(4.0);
211        let circumference = std::f32::consts::TAU * base_r;
212        let cycles = (circumference / opt.wavelength).round().max(3.0);
213        let adjusted_wavelength = circumference / cycles;
214        let effective_amp = opt.amplitude * amp_factor;
215        let phase = if is_visible && !reduced && effective_amp > 0.001 && opt.wave_speed.abs() > 0.001
216        {
217            self.ui.continuous = true;
218            let elapsed = self.ui.motion.elapsed(id, 32) as f32;
219            (elapsed * opt.wave_speed / adjusted_wavelength) * std::f32::consts::TAU
220        } else {
221            0.0
222        };
223        let gap_rad = (((opt.track_gap + opt.track_thickness) / base_r) * gap_scale).min(sweep);
224        let arc_side = 2.0 * base_r + opt.track_thickness;
225        let arc_bounds = Rect {
226            x: cx - arc_side * 0.5,
227            y: cy - arc_side * 0.5,
228            width: arc_side,
229            height: arc_side,
230        };
231        let inactive_sweep = (std::f32::consts::TAU - sweep - 2.0 * gap_rad).max(0.0);
232        if inactive_sweep > 0.001 {
233            self.commands.push(DrawCommand::Arc {
234                bounds: arc_bounds,
235                start: start + sweep + gap_rad,
236                sweep: inactive_sweep,
237                stroke: opt.track_thickness,
238                color: t.secondary_container,
239                rounded: true,
240            });
241        }
242        if sweep > 0.001 {
243            if effective_amp <= 0.05 {
244                self.commands.push(DrawCommand::Arc {
245                    bounds: arc_bounds,
246                    start,
247                    sweep,
248                    stroke: opt.track_thickness,
249                    color: t.primary,
250                    rounded: true,
251                });
252            } else {
253                let arc_len = sweep * base_r;
254                let steps = ((arc_len / 2.0).ceil() as usize)
255                    .max((cycles * (sweep / std::f32::consts::TAU) * 16.0).ceil() as usize)
256                    .clamp(12, 240);
257                let mut prev = [0.0_f32; 2];
258                for i in 0..=steps {
259                    let frac = i as f32 / steps as f32;
260                    let theta = start + sweep * frac;
261                    let r = base_r + effective_amp * (cycles * theta - phase).sin();
262                    let curr = [cx + r * theta.cos(), cy + r * theta.sin()];
263                    if i > 0 {
264                        self.paint_line(prev, curr, opt.track_thickness, t.primary);
265                    }
266                    prev = curr;
267                }
268            }
269        }
270        self.semantic(
271            Response {
272                id,
273                bounds: b,
274                enabled: self.enabled,
275                ..Response::default()
276            },
277            Role::Progress,
278            label,
279            false,
280            None,
281            value,
282            value.map(|_| [0.0, 1.0]),
283        );
284    }
285}
286/// MDC's classic two-second CSS timeline, retained as a distinct progress variant.
287/// Values are from Angular Material 22.2.6 / Google LLC (MIT).
288pub fn classic_linear_segments(elapsed: f64) -> [[f32; 2]; 2] {
289    let t = (elapsed.max(0.0) % 2.0) as f32 / 2.0;
290    let segment = |a: f32, b: f32, from: f32, to: f32, curve: [f32; 4]| {
291        from + (to - from) * Easing::Cubic(curve).sample(((t - a) / (b - a)).clamp(0.0, 1.0))
292    };
293    let primary = if t < 0.2 {
294        0.0
295    } else if t < 0.5915 {
296        segment(
297            0.2,
298            0.5915,
299            0.0,
300            0.836_714_2,
301            [0.5, 0.0, 0.701_732, 0.495_819],
302        )
303    } else {
304        segment(
305            0.5915,
306            1.0,
307            0.836_714_2,
308            2.006_110_7,
309            [0.302_435, 0.381_352, 0.55, 0.956_352],
310        )
311    } - 1.451_666_1;
312    let pscale = if t < 0.3665 {
313        0.08
314    } else if t < 0.6915 {
315        segment(
316            0.3665,
317            0.6915,
318            0.08,
319            0.661_479,
320            [0.334_731, 0.124_82, 0.785_844, 1.0],
321        )
322    } else {
323        segment(0.6915, 1.0, 0.661_479, 0.08, [0.06, 0.11, 0.6, 1.0])
324    };
325    let secondary = if t < 0.25 {
326        segment(
327            0.0,
328            0.25,
329            0.0,
330            0.376_519_14,
331            [0.15, 0.0, 0.515_058, 0.409_685],
332        )
333    } else if t < 0.4835 {
334        segment(
335            0.25,
336            0.4835,
337            0.376_519_14,
338            0.843_861_64,
339            [0.310_33, 0.284_058, 0.8, 0.733_712],
340        )
341    } else {
342        segment(
343            0.4835,
344            1.0,
345            0.843_861_64,
346            1.602_777_8,
347            [0.4, 0.627_035, 0.6, 0.902_026],
348        )
349    } - 0.548_888_9;
350    let sscale = if t < 0.1915 {
351        segment(
352            0.0,
353            0.1915,
354            0.08,
355            0.457_104,
356            [0.205_028, 0.057_051, 0.576_61, 0.453_971],
357        )
358    } else if t < 0.4415 {
359        segment(
360            0.1915,
361            0.4415,
362            0.457_104,
363            0.727_96,
364            [0.152_313, 0.196_432, 0.648_374, 1.004_315],
365        )
366    } else {
367        segment(
368            0.4415,
369            1.0,
370            0.727_96,
371            0.08,
372            [0.257_759, -0.003_163, 0.211_762, 1.381_79],
373        )
374    };
375    [
376        [primary.clamp(0.0, 1.0), (primary + pscale).clamp(0.0, 1.0)],
377        [
378            secondary.clamp(0.0, 1.0),
379            (secondary + sscale).clamp(0.0, 1.0),
380        ],
381    ]
382}
383/// Exact composite angles of MDC's rotating layer and clipped half-circle keyframes.
384pub fn classic_circular_sample(elapsed: f64) -> (f32, f32) {
385    let t = elapsed.max(0.0);
386    let phase = (t % 1.333) / 1.333;
387    let local = if phase < 0.5 {
388        phase * 2.0
389    } else {
390        (1.0 - phase) * 2.0
391    } as f32;
392    let curve = Easing::Cubic([0.4, 0.0, 0.2, 1.0]);
393    let left = 265.0 - 135.0 * curve.sample(local);
394    let layer = t / 0.6665;
395    let angle = 135.0 * (layer.floor() as f32 + curve.sample(layer.fract() as f32))
396        + (t / 1.568_235_294_117_6 * 360.0) as f32;
397    (
398        (angle + left).to_radians(),
399        (540.0 - 2.0 * left).to_radians(),
400    )
401}
402impl Frame<'_> {
403    pub fn progress_classic(&mut self, id: u64, label: &str, value: Option<f32>) {
404        self.progress_classic_with_secondary(id, label, value, None);
405    }
406    /// Linear progress with an optional buffered portion behind the primary bar.
407    pub fn progress_classic_with_secondary(&mut self, id: u64, label: &str, value: Option<f32>, secondary: Option<f32>) {
408        self.progress_classic_directional(id, label, value, secondary, false);
409    }
410    /// Linear progress with optional right-to-left mirroring.
411    pub fn progress_classic_directional(&mut self, id: u64, label: &str, value: Option<f32>, secondary: Option<f32>, rtl: bool) {
412        self.progress_classic_tinted(id, label, value, secondary, rtl, ClassicProgressColors::from_theme(self.ui.theme));
413    }
414    pub(crate) fn progress_classic_tinted(&mut self, id: u64, label: &str, value: Option<f32>, secondary: Option<f32>, rtl: bool, colors: ClassicProgressColors) {
415        use crate::timing::TweenSpec;
416        let b = self.allocate(4.0);
417        let value = value.filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0));
418        let segments = if let Some(v) = value {
419            let v = self.ui.motion.tween(
420                id,
421                3,
422                v,
423                v,
424                TweenSpec::new(0.250, Easing::Cubic([0.4, 0.0, 0.6, 1.0])),
425                TweenSpec::new(0.250, Easing::Cubic([0.4, 0.0, 0.6, 1.0])),
426            );
427            [[0.0, v], [0.0, 0.0]]
428        } else if self.continuous(b) {
429            classic_linear_segments(self.ui.motion.elapsed(id, 31))
430        } else {
431            [[0.0, 0.4], [0.0, 0.0]]
432        };
433        self.rect(b, 0.0, colors.background);
434        if value.is_some() && let Some(buffer) = secondary.filter(|v| v.is_finite()) {
435            let width = b.width * buffer.clamp(0.0, 1.0);
436            self.rect(Rect { x: if rtl { b.x + b.width - width } else { b.x }, width, ..b }, 0.0, colors.secondary);
437        }
438        for [tail, head] in segments {
439            if head > tail {
440                self.rect(
441                    Rect {
442                        x: b.x + b.width * if rtl { 1.0 - head } else { tail },
443                        width: b.width * (head - tail),
444                        ..b
445                    },
446                    0.0,
447                    if value.is_some() { colors.primary } else { colors.indeterminate },
448                );
449            }
450        }
451        self.semantic(
452            Response {
453                id,
454                bounds: b,
455                enabled: self.enabled,
456                ..Response::default()
457            },
458            Role::Progress,
459            label,
460            false,
461            None,
462            value,
463            value.map(|_| [0.0, 1.0]),
464        );
465    }
466    pub fn progress_circular_classic(
467        &mut self,
468        id: u64,
469        label: &str,
470        value: Option<f32>,
471        diameter: f32,
472    ) {
473        self.progress_circular_classic_tinted(id, label, value, diameter, ClassicProgressColors::from_theme(self.ui.theme));
474    }
475    pub(crate) fn progress_circular_classic_tinted(&mut self, id: u64, label: &str, value: Option<f32>, diameter: f32, colors: ClassicProgressColors) {
476        use crate::timing::TweenSpec;
477        let mut b = self.allocate(diameter);
478        b.width = b.width.min(diameter);
479        let value = value.filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0));
480        let (start, sweep) = if let Some(v) = value {
481            let a = self.ui.motion.tween(
482                id,
483                3,
484                v,
485                v,
486                TweenSpec::new(0.5, Easing::StandardDecelerate),
487                TweenSpec::new(0.5, Easing::StandardDecelerate),
488            );
489            (-std::f32::consts::FRAC_PI_2, a * std::f32::consts::TAU)
490        } else if self.continuous(b) {
491            classic_circular_sample(self.ui.motion.elapsed(id, 31))
492        } else {
493            (-std::f32::consts::FRAC_PI_2, std::f32::consts::TAU * 0.4)
494        };
495        let stroke = diameter / 10.0;
496        let radius = (diameter - 10.0) * 0.5 * diameter / (diameter - 10.0 + stroke);
497        let inset = (diameter - 2.0 * radius - stroke) * 0.5;
498        self.commands.push(DrawCommand::Arc {
499            bounds: b.inset(inset.max(0.0)),
500            start,
501            sweep,
502            stroke,
503            color: if value.is_some() { colors.primary } else { colors.indeterminate },
504            rounded: false,
505        });
506        self.semantic(
507            Response {
508                id,
509                bounds: b,
510                enabled: self.enabled,
511                ..Response::default()
512            },
513            Role::Progress,
514            label,
515            false,
516            None,
517            value,
518            value.map(|_| [0.0, 1.0]),
519        );
520    }
521}
522#[cfg(test)]
523#[path = "progress.spec.rs"]
524mod tests;
525
526impl Frame<'_> {
527    /// The caller owns semantics of the work. None means visible indeterminate progress.
528    pub fn progress(&mut self, id: u64, label: &str, value: Option<f32>) {
529        let b = self.allocate(12.0);
530        let t = self.ui.theme;
531        let track = Rect {
532            y: b.y + 4.0,
533            height: 4.0,
534            ..b
535        };
536        let value = value.filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0));
537        let segments = if let Some(v) = value {
538            let a = self.ui.motion.effect(id, 3, v, v, Spring::PROGRESS);
539            [[0.0, a], [0.0, 0.0]]
540        } else {
541            let visible = self.clip.intersect(b);
542            if visible.width > 0.0
543                && visible.height > 0.0
544                && self.ui.motion_preference == MotionPreference::Full
545            {
546                self.ui.continuous = true;
547                crate::progress::linear_segments(self.ui.motion.elapsed(id, 31))
548            } else {
549                [[0.0, 0.3], [0.0, 0.0]]
550            }
551        };
552        // Draw only inactive portions, leaving the 4dp gap plus round-cap space.
553        let gap = (8.0 / track.width.max(1.0)).min(1.0);
554        let mut edges = [segments[0], segments[1]];
555        if edges[0][0] > edges[1][0] {
556            edges.swap(0, 1);
557        }
558        let mut cursor = 0.0;
559        for [tail, head] in edges {
560            if head <= tail {
561                continue;
562            }
563            let end = (tail - gap).max(cursor);
564            if end > cursor {
565                self.rect(
566                    Rect {
567                        x: track.x + cursor * track.width,
568                        width: (end - cursor) * track.width,
569                        ..track
570                    },
571                    2.0,
572                    t.control,
573                );
574            }
575            cursor = cursor.max((head + gap).min(1.0));
576        }
577        if cursor < 1.0 {
578            self.rect(
579                Rect {
580                    x: track.x + cursor * track.width,
581                    width: (1.0 - cursor) * track.width,
582                    ..track
583                },
584                2.0,
585                t.control,
586            );
587        }
588        for [tail, head] in segments {
589            if head > tail {
590                self.rect(
591                    Rect {
592                        x: track.x + tail * track.width,
593                        width: (head - tail) * track.width,
594                        ..track
595                    },
596                    2.0,
597                    t.primary,
598                );
599            }
600        }
601        if value.is_some_and(|v| v < 1.0) {
602            self.rect(
603                Rect {
604                    x: track.x + track.width - 4.0,
605                    width: 4.0,
606                    ..track
607                },
608                2.0,
609                t.primary,
610            );
611        }
612        self.semantic(
613            Response {
614                id,
615                bounds: b,
616                enabled: self.enabled,
617                ..Response::default()
618            },
619            Role::Progress,
620            label,
621            false,
622            None,
623            value,
624            value.map(|_| [0.0, 1.0]),
625        );
626    }
627    /// Material 3 Expressive wavy linear progress indicator.
628    /// Draws a smooth sinusoidal active wave separated by `track_gap` from the straight
629    /// inactive track and a trailing `stop_indicator_size` dot.
630    pub fn wavy_linear_progress(
631        &mut self,
632        id: u64,
633        label: &str,
634        value: Option<f32>,
635        options: crate::progress::WavyProgressOptions,
636    ) {
637        let opt = crate::progress::sanitize_wavy(options, 40.0);
638        let height = (opt.track_thickness + opt.amplitude * 2.0).max(12.0);
639        let b = self.allocate(height);
640        let t = self.ui.theme;
641        let cy = b.y + b.height * 0.5;
642        let half_t = opt.track_thickness * 0.5;
643        let value = value.filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0));
644        let visible = self.clip.intersect(b);
645        let is_visible = visible.width > 0.0 && visible.height > 0.0;
646        let reduced = self.reduced_motion();
647        let (segments, amp_factor, gap_scale) = if let Some(v) = value {
648            let a = self.ui.motion.effect(id, 3, v, v, Spring::PROGRESS);
649            (
650                [[0.0, a], [0.0, 0.0]],
651                crate::progress::wavy_amplitude_ramp(a),
652                (a / 0.01).clamp(0.0, 1.0),
653            )
654        } else if is_visible && !reduced {
655            self.ui.continuous = true;
656            (
657                crate::progress::linear_segments(self.ui.motion.elapsed(id, 31)),
658                1.0,
659                1.0,
660            )
661        } else {
662            ([[0.0, 0.35], [0.0, 0.0]], 1.0, 1.0)
663        };
664        let effective_amp = opt.amplitude * amp_factor;
665        let phase = if is_visible && !reduced && effective_amp > 0.001 && opt.wave_speed.abs() > 0.001
666        {
667            self.ui.continuous = true;
668            let elapsed = self.ui.motion.elapsed(id, 32) as f32;
669            (elapsed * opt.wave_speed / opt.wavelength) * std::f32::consts::TAU
670        } else {
671            0.0
672        };
673        let gap_frac = ((opt.track_gap * gap_scale) / b.width.max(1.0)).min(1.0);
674        let mut edges = segments;
675        if edges[0][0] > edges[1][0] {
676            edges.swap(0, 1);
677        }
678        let track_rect = |start_f: f32, end_f: f32| Rect {
679            x: b.x + start_f * b.width,
680            y: cy - half_t,
681            width: ((end_f - start_f) * b.width).max(0.0),
682            height: opt.track_thickness,
683        };
684        let mut cursor = 0.0_f32;
685        for [tail, head] in edges {
686            if head <= tail {
687                continue;
688            }
689            let end = (tail - gap_frac).max(cursor);
690            if end > cursor {
691                let r = track_rect(cursor, end);
692                if r.width > 0.0 {
693                    self.rect(r, half_t.min(r.width * 0.5), t.secondary_container);
694                }
695            }
696            cursor = cursor.max((head + gap_frac).min(1.0));
697        }
698        if cursor < 1.0 {
699            let r = track_rect(cursor, 1.0);
700            if r.width > 0.0 {
701                self.rect(r, half_t.min(r.width * 0.5), t.secondary_container);
702            }
703        }
704        for [tail, head] in segments {
705            if head <= tail {
706                continue;
707            }
708            let x0 = b.x + tail * b.width;
709            let x1 = b.x + head * b.width;
710            let seg_w = x1 - x0;
711            if seg_w <= 0.0 {
712                continue;
713            }
714            if seg_w <= opt.track_thickness || effective_amp <= 0.05 {
715                self.rect(
716                    Rect {
717                        x: x0,
718                        y: cy - half_t,
719                        width: seg_w,
720                        height: opt.track_thickness,
721                    },
722                    half_t.min(seg_w * 0.5),
723                    t.primary,
724                );
725            } else {
726                let sx0 = x0 + half_t;
727                let sx1 = x1 - half_t;
728                let span = (sx1 - sx0).max(0.0);
729                let step = (opt.wavelength / 16.0).clamp(1.5, 4.0);
730                let steps = ((span / step).ceil() as usize).clamp(4, 512);
731                let mut prev = [0.0_f32; 2];
732                for i in 0..=steps {
733                    let frac = i as f32 / steps as f32;
734                    let x = sx0 + span * frac;
735                    let y = cy
736                        + effective_amp
737                            * (std::f32::consts::TAU * (x - b.x) / opt.wavelength - phase).sin();
738                    let curr = [x, y];
739                    if i > 0 {
740                        self.paint_line(prev, curr, opt.track_thickness, t.primary);
741                    }
742                    prev = curr;
743                }
744            }
745        }
746        if value.is_some_and(|v| v < 1.0) && opt.stop_indicator_size > 0.0 {
747            let stop = opt.stop_indicator_size.min(opt.track_thickness);
748            self.rect(
749                Rect {
750                    x: (b.x + b.width - stop).max(b.x),
751                    y: cy - stop * 0.5,
752                    width: stop,
753                    height: stop,
754                },
755                stop * 0.5,
756                t.primary,
757            );
758        }
759        self.semantic(
760            Response {
761                id,
762                bounds: b,
763                enabled: self.enabled,
764                ..Response::default()
765            },
766            Role::Progress,
767            label,
768            false,
769            None,
770            value,
771            value.map(|_| [0.0, 1.0]),
772        );
773    }
774}