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: 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: "Steps".into(),
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 {
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}