gpui_material_components/navigation/
stepper.rs1use std::rc::Rc;
2
3use gpui::{AnyElement, App, ElementId, Hsla, IntoElement, SharedString, Window};
4
5mod render;
6
7#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
9pub enum StepperOrientation {
10 #[default]
12 Horizontal,
13 Vertical,
15}
16
17#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
19pub enum StepStatus {
20 #[default]
22 Upcoming,
23 Active,
25 Completed,
27 Error,
29 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
45pub 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 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 pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
72 self.supporting = Some(text.into());
73 self
74 }
75
76 pub fn status(mut self, status: StepStatus) -> Self {
78 self.status = Some(status);
79 self
80 }
81
82 pub fn icon(mut self, icon: impl IntoElement) -> Self {
84 self.icon = Some(icon.into_any_element());
85 self
86 }
87
88 pub fn content(mut self, content: impl IntoElement) -> Self {
90 self.content = Some(content.into_any_element());
91 self
92 }
93
94 pub fn editable(mut self, editable: bool) -> Self {
96 self.editable = editable;
97 self
98 }
99
100 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#[derive(Clone, Copy, Debug, Default, PartialEq)]
113pub struct StepperColors {
114 pub active_container: Option<Hsla>,
116 pub active_content: Option<Hsla>,
118 pub completed_container: Option<Hsla>,
120 pub completed_content: Option<Hsla>,
122 pub upcoming_container: Option<Hsla>,
124 pub upcoming_content: Option<Hsla>,
126 pub error_container: Option<Hsla>,
128 pub error_content: Option<Hsla>,
130 pub connector: Option<Hsla>,
132 pub active_connector: Option<Hsla>,
134 pub label: Option<Hsla>,
136 pub supporting: Option<Hsla>,
138}
139
140pub(super) type StepSelect = Rc<dyn Fn(usize, &mut Window, &mut App)>;
141
142#[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: Option<SharedString>,
153}
154
155impl Stepper {
156 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: None,
167 }
168 }
169
170 pub fn orientation(mut self, orientation: StepperOrientation) -> Self {
172 self.orientation = orientation;
173 self
174 }
175
176 pub fn active_step(mut self, index: usize) -> Self {
179 self.active_step = Some(index);
180 self
181 }
182
183 pub fn non_linear(mut self, non_linear: bool) -> Self {
185 self.non_linear = non_linear;
186 self
187 }
188
189 pub fn colors(mut self, colors: StepperColors) -> Self {
191 self.colors = colors;
192 self
193 }
194
195 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 pub fn accessible_label(mut self, label: impl Into<SharedString>) -> Self {
204 self.accessible_label = Some(label.into());
205 self
206 }
207}
208
209pub(super) fn resolve_step_status(
210 explicit: Option<StepStatus>,
211 index: usize,
212 active_step: Option<usize>,
213) -> StepStatus {
214 if let Some(status) = explicit {
215 return status;
216 }
217 match active_step {
218 Some(active) if index < active => StepStatus::Completed,
219 Some(active) if index == active => StepStatus::Active,
220 Some(_) => StepStatus::Upcoming,
221 None => StepStatus::Upcoming,
222 }
223}
224
225pub(super) fn step_is_interactive(
226 status: StepStatus,
227 editable: bool,
228 non_linear: bool,
229 has_on_select: bool,
230) -> bool {
231 if !has_on_select || status == StepStatus::Disabled {
232 return false;
233 }
234 non_linear
235 || editable
236 || matches!(
237 status,
238 StepStatus::Completed | StepStatus::Active | StepStatus::Error
239 )
240}
241
242#[cfg(test)]
243mod tests {
244 use super::*;
245
246 #[test]
247 fn active_step_derives_statuses_unless_explicitly_overridden() {
248 assert_eq!(resolve_step_status(None, 0, Some(1)), StepStatus::Completed);
249 assert_eq!(resolve_step_status(None, 1, Some(1)), StepStatus::Active);
250 assert_eq!(resolve_step_status(None, 2, Some(1)), StepStatus::Upcoming);
251 assert_eq!(
252 resolve_step_status(Some(StepStatus::Error), 1, Some(1)),
253 StepStatus::Error
254 );
255 assert_eq!(
256 resolve_step_status(Some(StepStatus::Disabled), 0, Some(1)),
257 StepStatus::Disabled
258 );
259 assert_eq!(resolve_step_status(None, 0, None), StepStatus::Upcoming);
260 }
261
262 #[test]
263 fn linear_and_non_linear_interactivity_respect_disabled_and_editable_rules() {
264 assert!(!step_is_interactive(StepStatus::Active, false, true, false));
265 assert!(!step_is_interactive(StepStatus::Disabled, true, true, true));
266 assert!(!step_is_interactive(
267 StepStatus::Upcoming,
268 false,
269 false,
270 true
271 ));
272 assert!(step_is_interactive(StepStatus::Upcoming, true, false, true));
273 assert!(step_is_interactive(StepStatus::Upcoming, false, true, true));
274 assert!(step_is_interactive(
275 StepStatus::Completed,
276 false,
277 false,
278 true
279 ));
280 assert!(step_is_interactive(StepStatus::Error, false, false, true));
281 }
282}