Skip to main content

bhoo/systems/material/
stepper.rs

1//! Material horizontal linear Stepper widget.
2use crate::*;
3
4/// Caller-owned state for `Frame::stepper`.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub struct StepperState {
7    pub active: usize,
8    pub completed_mask: u64,
9}
10
11impl StepperState {
12    pub fn is_completed(&self, index: usize) -> bool {
13        index < 64 && (self.completed_mask & (1u64 << index)) != 0
14    }
15
16    pub fn set_completed(&mut self, index: usize, done: bool) {
17        if index < 64 {
18            if done {
19                self.completed_mask |= 1u64 << index;
20            } else {
21                self.completed_mask &= !(1u64 << index);
22            }
23        }
24    }
25}
26
27impl Frame<'_> {
28    /// Material horizontal stepper with step circles, completed checkmarks, connector lines,
29    /// and a scoped body closure for the active step. Returns `true` when `state.active` changed.
30    pub fn stepper(
31        &mut self,
32        id: u64,
33        steps: &[&str],
34        state: &mut StepperState,
35        linear: bool,
36        body_height: f32,
37        body: impl FnOnce(&mut Self, usize),
38    ) -> bool {
39        if steps.is_empty() {
40            return false;
41        }
42        state.active = state.active.min(steps.len() - 1);
43        let t = self.theme();
44        let mut changed = false;
45        let header = self.reserve(72.0);
46        let n = steps.len();
47        let slot_w = header.width / n as f32;
48        for (i, &label) in steps.iter().enumerate() {
49            let active = i == state.active;
50            let done = state.is_completed(i);
51            let allowed = !linear || i <= state.active || state.is_completed(i.saturating_sub(1));
52            let cell = Rect { x: header.x + i as f32 * slot_w, width: slot_w, ..header };
53            let r = self.target(id.wrapping_add(i as u64), cell, label, allowed, Role::Tab);
54            self.state_layer(r, 8.0, t.on_surface_variant);
55            self.focus_ring(r);
56            if r.clicked && allowed && state.active != i {
57                state.active = i;
58                changed = true;
59            }
60            let badge = Rect { x: cell.x + 12.0, y: cell.y + (cell.height - 24.0) * 0.5, width: 24.0, height: 24.0 };
61            let badge_bg = if active || done { t.primary } else { t.surface_highest };
62            let badge_fg = if active || done { t.on_primary } else { t.on_surface_variant };
63            self.paint_rect(badge, 12.0, if allowed { badge_bg } else { t.text.opacity(0.12) });
64            if done {
65                let cx = badge.x + 12.0;
66                let cy = badge.y + 12.0;
67                self.paint_line([cx - 4.5, cy], [cx - 1.0, cy + 3.5], 2.0, badge_fg);
68                self.paint_line([cx - 1.0, cy + 3.5], [cx + 5.0, cy - 3.5], 2.0, badge_fg);
69            } else {
70                let num = (i + 1).to_string();
71                self.styled_text(badge, &num, badge_fg, TextStyle { size: 12.0, weight: 500, align: 1, ..TextStyle::default() });
72            }
73            let text_x = badge.x + 32.0;
74            let text_w = (cell.x + cell.width - text_x - 16.0).max(0.0);
75            self.styled_text(
76                Rect { x: text_x, width: text_w, ..cell },
77                label,
78                if !allowed {
79                    t.text.opacity(0.38)
80                } else if active {
81                    t.text
82                } else {
83                    t.on_surface_variant
84                },
85                TextStyle { size: 14.0, weight: if active { 500 } else { 400 }, ..TextStyle::default() },
86            );
87            if i + 1 < n {
88                let lw = self.text_width(label, TextStyle { size: 14.0, weight: 500, ..TextStyle::default() });
89                let line_x0 = (text_x + lw + 10.0).min(cell.x + cell.width - 4.0);
90                let line_x1 = header.x + (i + 1) as f32 * slot_w + 6.0;
91                if line_x1 > line_x0 + 4.0 {
92                    let ly = header.y + header.height * 0.5;
93                    self.paint_line([line_x0, ly], [line_x1, ly], 1.0, t.outline_variant);
94                }
95            }
96        }
97        if body_height > 0.0 {
98            let area = self.reserve(body_height);
99            self.within(area, |f| body(f, state.active));
100        }
101        changed
102    }
103}
104
105#[cfg(test)]
106#[path = "stepper.spec.rs"]
107mod tests;