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]
11 Horizontal,
12 Vertical,
13}
14
15#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
17pub enum StepStatus {
18 #[default]
19 Upcoming,
20 Active,
21 Completed,
22 Error,
23 Disabled,
24}
25
26impl StepStatus {
27 pub(super) fn accessible_label(self) -> &'static str {
28 match self {
29 Self::Upcoming => "upcoming",
30 Self::Active => "current",
31 Self::Completed => "completed",
32 Self::Error => "error",
33 Self::Disabled => "disabled",
34 }
35 }
36}
37
38pub struct StepItem {
40 pub(super) id: SharedString,
41 pub(super) label: SharedString,
42 pub(super) supporting: Option<SharedString>,
43 pub(super) status: Option<StepStatus>,
44 pub(super) icon: Option<AnyElement>,
45 pub(super) content: Option<AnyElement>,
46 pub(super) editable: bool,
47}
48
49impl StepItem {
50 pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
51 Self {
52 id: id.into(),
53 label: label.into(),
54 supporting: None,
55 status: None,
56 icon: None,
57 content: None,
58 editable: false,
59 }
60 }
61
62 pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
64 self.supporting = Some(text.into());
65 self
66 }
67
68 pub fn status(mut self, status: StepStatus) -> Self {
70 self.status = Some(status);
71 self
72 }
73
74 pub fn icon(mut self, icon: impl IntoElement) -> Self {
76 self.icon = Some(icon.into_any_element());
77 self
78 }
79
80 pub fn content(mut self, content: impl IntoElement) -> Self {
82 self.content = Some(content.into_any_element());
83 self
84 }
85
86 pub fn editable(mut self, editable: bool) -> Self {
88 self.editable = editable;
89 self
90 }
91
92 pub fn disabled(mut self, disabled: bool) -> Self {
94 if disabled {
95 self.status = Some(StepStatus::Disabled);
96 } else if self.status == Some(StepStatus::Disabled) {
97 self.status = None;
98 }
99 self
100 }
101}
102
103#[derive(Clone, Copy, Debug, Default, PartialEq)]
105pub struct StepperColors {
106 pub active_container: Option<Hsla>,
107 pub active_content: Option<Hsla>,
108 pub completed_container: Option<Hsla>,
109 pub completed_content: Option<Hsla>,
110 pub upcoming_container: Option<Hsla>,
111 pub upcoming_content: Option<Hsla>,
112 pub error_container: Option<Hsla>,
113 pub error_content: Option<Hsla>,
114 pub connector: Option<Hsla>,
115 pub active_connector: Option<Hsla>,
116 pub label: Option<Hsla>,
117 pub supporting: Option<Hsla>,
118}
119
120pub(super) type StepSelect = Rc<dyn Fn(usize, &mut Window, &mut App)>;
121
122#[derive(IntoElement)]
124pub struct Stepper {
125 pub(super) id: ElementId,
126 pub(super) steps: Vec<StepItem>,
127 pub(super) orientation: StepperOrientation,
128 pub(super) active_step: Option<usize>,
129 pub(super) non_linear: bool,
130 pub(super) colors: StepperColors,
131 pub(super) on_select: Option<StepSelect>,
132 pub(super) accessible_label: SharedString,
133}
134
135impl Stepper {
136 pub fn new(id: impl Into<ElementId>, steps: impl IntoIterator<Item = StepItem>) -> Self {
137 Self {
138 id: id.into(),
139 steps: steps.into_iter().collect(),
140 orientation: StepperOrientation::default(),
141 active_step: None,
142 non_linear: false,
143 colors: StepperColors::default(),
144 on_select: None,
145 accessible_label: "Steps".into(),
146 }
147 }
148
149 pub fn orientation(mut self, orientation: StepperOrientation) -> Self {
151 self.orientation = orientation;
152 self
153 }
154
155 pub fn active_step(mut self, index: usize) -> Self {
158 self.active_step = Some(index);
159 self
160 }
161
162 pub fn non_linear(mut self, non_linear: bool) -> Self {
164 self.non_linear = non_linear;
165 self
166 }
167
168 pub fn colors(mut self, colors: StepperColors) -> Self {
170 self.colors = colors;
171 self
172 }
173
174 pub fn on_select(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
176 self.on_select = Some(Rc::new(handler));
177 self
178 }
179
180 pub fn accessible_label(mut self, label: impl Into<SharedString>) -> Self {
182 self.accessible_label = label.into();
183 self
184 }
185}
186
187pub(super) fn resolve_step_status(
188 explicit: Option<StepStatus>,
189 index: usize,
190 active_step: Option<usize>,
191) -> StepStatus {
192 if let Some(status) = explicit {
193 return status;
194 }
195 match active_step {
196 Some(active) if index < active => StepStatus::Completed,
197 Some(active) if index == active => StepStatus::Active,
198 Some(_) => StepStatus::Upcoming,
199 None => StepStatus::Upcoming,
200 }
201}
202
203pub(super) fn step_is_interactive(
204 status: StepStatus,
205 editable: bool,
206 non_linear: bool,
207 has_on_select: bool,
208) -> bool {
209 if !has_on_select || status == StepStatus::Disabled {
210 return false;
211 }
212 non_linear
213 || editable
214 || matches!(
215 status,
216 StepStatus::Completed | StepStatus::Active | StepStatus::Error
217 )
218}
219
220#[cfg(test)]
221mod tests {
222 use super::*;
223
224 #[test]
225 fn active_step_derives_statuses_unless_explicitly_overridden() {
226 assert_eq!(resolve_step_status(None, 0, Some(1)), StepStatus::Completed);
227 assert_eq!(resolve_step_status(None, 1, Some(1)), StepStatus::Active);
228 assert_eq!(resolve_step_status(None, 2, Some(1)), StepStatus::Upcoming);
229 assert_eq!(
230 resolve_step_status(Some(StepStatus::Error), 1, Some(1)),
231 StepStatus::Error
232 );
233 assert_eq!(
234 resolve_step_status(Some(StepStatus::Disabled), 0, Some(1)),
235 StepStatus::Disabled
236 );
237 assert_eq!(resolve_step_status(None, 0, None), StepStatus::Upcoming);
238 }
239
240 #[test]
241 fn linear_and_non_linear_interactivity_respect_disabled_and_editable_rules() {
242 assert!(!step_is_interactive(StepStatus::Active, false, true, false));
243 assert!(!step_is_interactive(StepStatus::Disabled, true, true, true));
244 assert!(!step_is_interactive(
245 StepStatus::Upcoming,
246 false,
247 false,
248 true
249 ));
250 assert!(step_is_interactive(StepStatus::Upcoming, true, false, true));
251 assert!(step_is_interactive(StepStatus::Upcoming, false, true, true));
252 assert!(step_is_interactive(
253 StepStatus::Completed,
254 false,
255 false,
256 true
257 ));
258 assert!(step_is_interactive(StepStatus::Error, false, false, true));
259 }
260}