Skip to main content

gpui_material_components/navigation/
stepper.rs

1use std::rc::Rc;
2
3use gpui::{AnyElement, App, ElementId, Hsla, IntoElement, SharedString, Window};
4
5mod render;
6
7/// Layout axis for a [`Stepper`].
8#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
9pub enum StepperOrientation {
10    /// Arrange steps in a horizontal row with connectors between them.
11    #[default]
12    Horizontal,
13    /// Stack steps vertically with optional inline body content under the active step.
14    Vertical,
15}
16
17/// Visual and semantic status of one step in a [`Stepper`].
18#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
19pub enum StepStatus {
20    /// Step has not yet been reached.
21    #[default]
22    Upcoming,
23    /// Step is currently active.
24    Active,
25    /// Step has been completed.
26    Completed,
27    /// Step has a validation or processing error.
28    Error,
29    /// Step is disabled and cannot be selected.
30    Disabled,
31}
32
33impl StepStatus {
34    pub(super) fn accessible_label(self) -> &'static str {
35        match self {
36            Self::Upcoming => "upcoming",
37            Self::Active => "current",
38            Self::Completed => "completed",
39            Self::Error => "error",
40            Self::Disabled => "disabled",
41        }
42    }
43}
44
45/// One step entry in a [`Stepper`].
46pub struct StepItem {
47    pub(super) id: SharedString,
48    pub(super) label: SharedString,
49    pub(super) supporting: Option<SharedString>,
50    pub(super) status: Option<StepStatus>,
51    pub(super) icon: Option<AnyElement>,
52    pub(super) content: Option<AnyElement>,
53    pub(super) editable: bool,
54}
55
56impl StepItem {
57    /// Creates a step entry with a unique ID and visible label.
58    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
59        Self {
60            id: id.into(),
61            label: label.into(),
62            supporting: None,
63            status: None,
64            icon: None,
65            content: None,
66            editable: false,
67        }
68    }
69
70    /// Optional supporting subtitle shown below the step label.
71    pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
72        self.supporting = Some(text.into());
73        self
74    }
75
76    /// Explicitly sets the step status, overriding any status derived from `Stepper::active_step`.
77    pub fn status(mut self, status: StepStatus) -> Self {
78        self.status = Some(status);
79        self
80    }
81
82    /// Replaces the default numbered or status glyph inside the step indicator badge.
83    pub fn icon(mut self, icon: impl IntoElement) -> Self {
84        self.icon = Some(icon.into_any_element());
85        self
86    }
87
88    /// Body content displayed beneath this step when active in vertical orientation.
89    pub fn content(mut self, content: impl IntoElement) -> Self {
90        self.content = Some(content.into_any_element());
91        self
92    }
93
94    /// Marks this step as selectable in linear mode even if it is upcoming.
95    pub fn editable(mut self, editable: bool) -> Self {
96        self.editable = editable;
97        self
98    }
99
100    /// Convenience helper to disable a step.
101    pub fn disabled(mut self, disabled: bool) -> Self {
102        if disabled {
103            self.status = Some(StepStatus::Disabled);
104        } else if self.status == Some(StepStatus::Disabled) {
105            self.status = None;
106        }
107        self
108    }
109}
110
111/// Per-instance pigment overrides for [`Stepper`]. Omitted fields use the window theme.
112#[derive(Clone, Copy, Debug, Default, PartialEq)]
113pub struct StepperColors {
114    /// Indicator background color for the active step.
115    pub active_container: Option<Hsla>,
116    /// Indicator foreground color for the active step.
117    pub active_content: Option<Hsla>,
118    /// Indicator background color for completed steps.
119    pub completed_container: Option<Hsla>,
120    /// Indicator foreground color for completed steps.
121    pub completed_content: Option<Hsla>,
122    /// Indicator background color for upcoming steps.
123    pub upcoming_container: Option<Hsla>,
124    /// Indicator foreground color for upcoming steps.
125    pub upcoming_content: Option<Hsla>,
126    /// Indicator background color for error steps.
127    pub error_container: Option<Hsla>,
128    /// Indicator foreground color for error steps.
129    pub error_content: Option<Hsla>,
130    /// Connector line color between upcoming steps.
131    pub connector: Option<Hsla>,
132    /// Connector line color along completed/active transitions.
133    pub active_connector: Option<Hsla>,
134    /// Foreground color for step title labels.
135    pub label: Option<Hsla>,
136    /// Foreground color for supporting subtitle labels.
137    pub supporting: Option<Hsla>,
138}
139
140pub(super) type StepSelect = Rc<dyn Fn(usize, &mut Window, &mut App)>;
141
142/// Horizontal or vertical Material 3 multi-step indicator and navigator.
143#[derive(IntoElement)]
144pub struct Stepper {
145    pub(super) id: ElementId,
146    pub(super) steps: Vec<StepItem>,
147    pub(super) orientation: StepperOrientation,
148    pub(super) active_step: Option<usize>,
149    pub(super) non_linear: bool,
150    pub(super) colors: StepperColors,
151    pub(super) on_select: Option<StepSelect>,
152    pub(super) accessible_label: SharedString,
153}
154
155impl Stepper {
156    /// Creates a horizontal linear stepper with the given element ID and ordered steps.
157    pub fn new(id: impl Into<ElementId>, steps: impl IntoIterator<Item = StepItem>) -> Self {
158        Self {
159            id: id.into(),
160            steps: steps.into_iter().collect(),
161            orientation: StepperOrientation::default(),
162            active_step: None,
163            non_linear: false,
164            colors: StepperColors::default(),
165            on_select: None,
166            accessible_label: "Steps".into(),
167        }
168    }
169
170    /// Chooses horizontal (default) or vertical layout.
171    pub fn orientation(mut self, orientation: StepperOrientation) -> Self {
172        self.orientation = orientation;
173        self
174    }
175
176    /// Marks steps `< index` as `Completed`, `== index` as `Active`, and `> index` as `Upcoming`
177    /// unless a step explicitly sets its own `StepItem::status(...)`.
178    pub fn active_step(mut self, index: usize) -> Self {
179        self.active_step = Some(index);
180        self
181    }
182
183    /// Allows activating any non-disabled step (including upcoming steps) when `on_select` is set.
184    pub fn non_linear(mut self, non_linear: bool) -> Self {
185        self.non_linear = non_linear;
186        self
187    }
188
189    /// Overrides indicator, connector, and label colors independently.
190    pub fn colors(mut self, colors: StepperColors) -> Self {
191        self.colors = colors;
192        self
193    }
194
195    /// Invoked with the zero-based step index when a selectable step is activated.
196    pub fn on_select(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
197        self.on_select = Some(Rc::new(handler));
198        self
199    }
200
201    /// Sets the accessible name for the stepper container. Defaults to `"Steps"`.
202    pub fn accessible_label(mut self, label: impl Into<SharedString>) -> Self {
203        self.accessible_label = label.into();
204        self
205    }
206}
207
208pub(super) fn resolve_step_status(
209    explicit: Option<StepStatus>,
210    index: usize,
211    active_step: Option<usize>,
212) -> StepStatus {
213    if let Some(status) = explicit {
214        return status;
215    }
216    match active_step {
217        Some(active) if index < active => StepStatus::Completed,
218        Some(active) if index == active => StepStatus::Active,
219        Some(_) => StepStatus::Upcoming,
220        None => StepStatus::Upcoming,
221    }
222}
223
224pub(super) fn step_is_interactive(
225    status: StepStatus,
226    editable: bool,
227    non_linear: bool,
228    has_on_select: bool,
229) -> bool {
230    if !has_on_select || status == StepStatus::Disabled {
231        return false;
232    }
233    non_linear
234        || editable
235        || matches!(
236            status,
237            StepStatus::Completed | StepStatus::Active | StepStatus::Error
238        )
239}
240
241#[cfg(test)]
242mod tests {
243    use super::*;
244
245    #[test]
246    fn active_step_derives_statuses_unless_explicitly_overridden() {
247        assert_eq!(resolve_step_status(None, 0, Some(1)), StepStatus::Completed);
248        assert_eq!(resolve_step_status(None, 1, Some(1)), StepStatus::Active);
249        assert_eq!(resolve_step_status(None, 2, Some(1)), StepStatus::Upcoming);
250        assert_eq!(
251            resolve_step_status(Some(StepStatus::Error), 1, Some(1)),
252            StepStatus::Error
253        );
254        assert_eq!(
255            resolve_step_status(Some(StepStatus::Disabled), 0, Some(1)),
256            StepStatus::Disabled
257        );
258        assert_eq!(resolve_step_status(None, 0, None), StepStatus::Upcoming);
259    }
260
261    #[test]
262    fn linear_and_non_linear_interactivity_respect_disabled_and_editable_rules() {
263        assert!(!step_is_interactive(StepStatus::Active, false, true, false));
264        assert!(!step_is_interactive(StepStatus::Disabled, true, true, true));
265        assert!(!step_is_interactive(
266            StepStatus::Upcoming,
267            false,
268            false,
269            true
270        ));
271        assert!(step_is_interactive(StepStatus::Upcoming, true, false, true));
272        assert!(step_is_interactive(StepStatus::Upcoming, false, true, true));
273        assert!(step_is_interactive(
274            StepStatus::Completed,
275            false,
276            false,
277            true
278        ));
279        assert!(step_is_interactive(StepStatus::Error, false, false, true));
280    }
281}