1use crate::capabilities::foundation::theme::use_theme;
2use crate::capabilities::primitives::icon::Icon;
3use crate::capabilities::primitives::icon_source::IconSource;
4use gpui::{prelude::FluentBuilder as _, *};
5use std::collections::HashSet;
6use std::rc::Rc;
7
8#[derive(Copy, Clone, Debug, PartialEq, Eq)]
9pub enum StepperSize {
10 Sm,
11 Md,
12 Lg,
13}
14
15impl StepperSize {
16 fn indicator_size(&self) -> Pixels {
17 match self {
18 StepperSize::Sm => px(24.0),
19 StepperSize::Md => px(32.0),
20 StepperSize::Lg => px(40.0),
21 }
22 }
23
24 fn icon_size(&self) -> Pixels {
25 match self {
26 StepperSize::Sm => px(12.0),
27 StepperSize::Md => px(16.0),
28 StepperSize::Lg => px(20.0),
29 }
30 }
31
32 fn title_size(&self) -> Pixels {
33 match self {
34 StepperSize::Sm => px(12.0),
35 StepperSize::Md => px(14.0),
36 StepperSize::Lg => px(16.0),
37 }
38 }
39
40 fn description_size(&self) -> Pixels {
41 match self {
42 StepperSize::Sm => px(10.0),
43 StepperSize::Md => px(12.0),
44 StepperSize::Lg => px(14.0),
45 }
46 }
47
48 fn connector_thickness(&self) -> Pixels {
49 match self {
50 StepperSize::Sm => px(2.0),
51 StepperSize::Md => px(2.0),
52 StepperSize::Lg => px(3.0),
53 }
54 }
55}
56
57#[derive(Copy, Clone, Debug, PartialEq, Eq)]
58pub enum StepperOrientation {
59 Horizontal,
60 Vertical,
61}
62
63#[derive(Copy, Clone, Debug, PartialEq, Eq)]
64pub enum StepStatus {
65 Pending,
66 Current,
67 Completed,
68 Error,
69}
70
71#[derive(Clone, Debug)]
72pub struct StepItem {
73 pub title: SharedString,
74 pub description: Option<SharedString>,
75 pub icon: Option<IconSource>,
76 pub status: StepStatus,
77}
78
79impl StepItem {
80 pub fn new(title: impl Into<SharedString>) -> Self {
81 Self {
82 title: title.into(),
83 description: None,
84 icon: None,
85 status: StepStatus::Pending,
86 }
87 }
88
89 pub fn description(mut self, description: impl Into<SharedString>) -> Self {
90 self.description = Some(description.into());
91 self
92 }
93
94 pub fn icon(mut self, icon: impl Into<IconSource>) -> Self {
95 self.icon = Some(icon.into());
96 self
97 }
98
99 pub fn status(mut self, status: StepStatus) -> Self {
100 self.status = status;
101 self
102 }
103}
104
105pub struct StepperState {
106 steps: Vec<StepItem>,
107 current_step: usize,
108 completed_steps: HashSet<usize>,
109 linear: bool,
110 focus_handle: FocusHandle,
111}
112
113impl StepperState {
114 pub fn new(cx: &mut Context<Self>) -> Self {
115 Self {
116 steps: Vec::new(),
117 current_step: 0,
118 completed_steps: HashSet::new(),
119 linear: true,
120 focus_handle: cx.focus_handle(),
121 }
122 }
123
124 pub fn with_steps(mut self, steps: Vec<StepItem>) -> Self {
125 self.steps = steps;
126 self
127 }
128
129 pub fn with_linear(mut self, linear: bool) -> Self {
130 self.linear = linear;
131 self
132 }
133
134 pub fn steps(&self) -> &[StepItem] {
135 &self.steps
136 }
137
138 pub fn set_steps(&mut self, steps: Vec<StepItem>, cx: &mut Context<Self>) {
139 self.steps = steps;
140 self.current_step = 0;
141 self.completed_steps.clear();
142 cx.notify();
143 }
144
145 pub fn current_step(&self) -> usize {
146 self.current_step
147 }
148
149 pub fn set_current_step(&mut self, index: usize, cx: &mut Context<Self>) {
150 if index < self.steps.len() && self.is_step_accessible(index) {
151 self.current_step = index;
152 cx.notify();
153 }
154 }
155
156 pub fn is_linear(&self) -> bool {
157 self.linear
158 }
159
160 pub fn set_linear(&mut self, linear: bool, cx: &mut Context<Self>) {
161 self.linear = linear;
162 cx.notify();
163 }
164
165 pub fn next(&mut self, cx: &mut Context<Self>) -> bool {
166 if self.current_step + 1 < self.steps.len() {
167 self.mark_completed(self.current_step, cx);
168 self.current_step += 1;
169 cx.notify();
170 return true;
171 }
172 false
173 }
174
175 pub fn previous(&mut self, cx: &mut Context<Self>) -> bool {
176 if self.current_step > 0 {
177 self.current_step -= 1;
178 cx.notify();
179 return true;
180 }
181 false
182 }
183
184 pub fn go_to(&mut self, index: usize, cx: &mut Context<Self>) -> bool {
185 if index < self.steps.len() && self.is_step_accessible(index) {
186 self.current_step = index;
187 cx.notify();
188 return true;
189 }
190 false
191 }
192
193 pub fn mark_completed(&mut self, index: usize, cx: &mut Context<Self>) {
194 if index < self.steps.len() {
195 self.completed_steps.insert(index);
196 cx.notify();
197 }
198 }
199
200 pub fn unmark_completed(&mut self, index: usize, cx: &mut Context<Self>) {
201 self.completed_steps.remove(&index);
202 cx.notify();
203 }
204
205 pub fn is_completed(&self, index: usize) -> bool {
206 self.completed_steps.contains(&index)
207 }
208
209 pub fn is_step_accessible(&self, index: usize) -> bool {
210 if !self.linear {
211 return true;
212 }
213
214 if index == 0 {
215 return true;
216 }
217
218 if index <= self.current_step {
219 return true;
220 }
221
222 for i in 0..index {
223 if !self.completed_steps.contains(&i) && i != self.current_step {
224 return false;
225 }
226 }
227
228 index == self.current_step + 1 || self.completed_steps.contains(&(index - 1))
229 }
230
231 pub fn set_step_error(&mut self, index: usize, cx: &mut Context<Self>) {
232 if index < self.steps.len() {
233 self.steps[index].status = StepStatus::Error;
234 cx.notify();
235 }
236 }
237
238 pub fn clear_step_error(&mut self, index: usize, cx: &mut Context<Self>) {
239 if index < self.steps.len() && self.steps[index].status == StepStatus::Error {
240 self.steps[index].status = StepStatus::Pending;
241 cx.notify();
242 }
243 }
244}
245
246impl Focusable for StepperState {
247 fn focus_handle(&self, _: &App) -> FocusHandle {
248 self.focus_handle.clone()
249 }
250}
251
252impl Render for StepperState {
253 fn render(&mut self, _: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
254 div()
255 }
256}
257
258#[derive(IntoElement)]
259pub struct Stepper {
260 state: Entity<StepperState>,
261 size: StepperSize,
262 orientation: StepperOrientation,
263 clickable: bool,
264 show_connector: bool,
265 on_step_change: Option<Rc<dyn Fn(usize, &mut Window, &mut App) + 'static>>,
266 style: StyleRefinement,
267}
268
269impl Stepper {
270 pub fn new(state: Entity<StepperState>) -> Self {
271 Self {
272 state,
273 size: StepperSize::Md,
274 orientation: StepperOrientation::Horizontal,
275 clickable: true,
276 show_connector: true,
277 on_step_change: None,
278 style: StyleRefinement::default(),
279 }
280 }
281
282 pub fn size(mut self, size: StepperSize) -> Self {
283 self.size = size;
284 self
285 }
286
287 pub fn horizontal(mut self) -> Self {
288 self.orientation = StepperOrientation::Horizontal;
289 self
290 }
291
292 pub fn vertical(mut self) -> Self {
293 self.orientation = StepperOrientation::Vertical;
294 self
295 }
296
297 pub fn clickable(mut self, clickable: bool) -> Self {
298 self.clickable = clickable;
299 self
300 }
301
302 pub fn show_connector(mut self, show: bool) -> Self {
303 self.show_connector = show;
304 self
305 }
306
307 pub fn on_step_change(
308 mut self,
309 handler: impl Fn(usize, &mut Window, &mut App) + 'static,
310 ) -> Self {
311 self.on_step_change = Some(Rc::new(handler));
312 self
313 }
314
315 fn render_step_indicator(
316 &self,
317 index: usize,
318 status: StepStatus,
319 step: &StepItem,
320 theme: &crate::capabilities::foundation::theme::Theme,
321 ) -> Div {
322 let indicator_size = self.size.indicator_size();
323 let icon_size = self.size.icon_size();
324
325 let (bg_color, border_color, text_color) = match status {
326 StepStatus::Completed => (
327 theme.tokens.primary,
328 theme.tokens.primary,
329 theme.tokens.primary_foreground,
330 ),
331 StepStatus::Current => (
332 theme.tokens.background,
333 theme.tokens.primary,
334 theme.tokens.primary,
335 ),
336 StepStatus::Error => (
337 theme.tokens.destructive,
338 theme.tokens.destructive,
339 theme.tokens.destructive_foreground,
340 ),
341 StepStatus::Pending => (
342 theme.tokens.background,
343 theme.tokens.border,
344 theme.tokens.muted_foreground,
345 ),
346 };
347
348 div()
349 .flex()
350 .items_center()
351 .justify_center()
352 .size(indicator_size)
353 .rounded(indicator_size / 2.0)
354 .bg(bg_color)
355 .border_2()
356 .border_color(border_color)
357 .flex_shrink_0()
358 .child(match (status, &step.icon) {
359 (StepStatus::Completed, _) => div()
360 .child(Icon::new("check").size(icon_size).color(text_color))
361 .into_any_element(),
362 (StepStatus::Error, _) => div()
363 .child(Icon::new("x").size(icon_size).color(text_color))
364 .into_any_element(),
365 (_, Some(icon_source)) => div()
366 .child(
367 Icon::new(icon_source.clone())
368 .size(icon_size)
369 .color(text_color),
370 )
371 .into_any_element(),
372 (_, None) => div()
373 .text_size(self.size.title_size())
374 .font_weight(FontWeight::SEMIBOLD)
375 .text_color(text_color)
376 .child(format!("{}", index + 1))
377 .into_any_element(),
378 })
379 }
380
381 fn render_connector(
382 &self,
383 is_completed: bool,
384 theme: &crate::capabilities::foundation::theme::Theme,
385 ) -> Div {
386 let thickness = self.size.connector_thickness();
387 let color = if is_completed {
388 theme.tokens.primary
389 } else {
390 theme.tokens.border
391 };
392
393 match self.orientation {
394 StepperOrientation::Horizontal => div().flex_1().h(thickness).bg(color).mx(px(8.0)),
395 StepperOrientation::Vertical => div()
396 .w(thickness)
397 .flex_1()
398 .min_h(px(24.0))
399 .bg(color)
400 .my(px(4.0)),
401 }
402 }
403
404 fn render_horizontal(
405 self,
406 window: &mut Window,
407 theme: crate::capabilities::foundation::theme::Theme,
408 steps: Vec<StepItem>,
409 current_step: usize,
410 completed_steps: HashSet<usize>,
411 accessible: Vec<bool>,
412 user_style: StyleRefinement,
413 ) -> Div {
414 let steps_len = steps.len();
415 let state = self.state.clone();
416
417 div()
418 .flex()
419 .w_full()
420 .map(|this| {
421 let mut d = this;
422 d.style().refine(&user_style);
423 d
424 })
425 .children(steps.into_iter().enumerate().map(|(index, step)| {
426 let status = if step.status == StepStatus::Error {
427 StepStatus::Error
428 } else if index == current_step {
429 StepStatus::Current
430 } else if completed_steps.contains(&index) {
431 StepStatus::Completed
432 } else {
433 StepStatus::Pending
434 };
435
436 let is_accessible = accessible.get(index).copied().unwrap_or(false);
437
438 let is_last = index == steps_len - 1;
439 let on_change = self.on_step_change.clone();
440 let state_clone = state.clone();
441 let clickable = self.clickable && is_accessible;
442
443 div()
444 .flex()
445 .flex_1()
446 .items_center()
447 .child(
448 div()
449 .flex()
450 .flex_col()
451 .items_center()
452 .gap(px(8.0))
453 .when(clickable, |this| {
454 this.cursor(CursorStyle::PointingHand).on_mouse_down(
455 MouseButton::Left,
456 window.listener_for(
457 &state_clone,
458 move |state, _, window, cx| {
459 if state.go_to(index, cx) {
460 if let Some(ref handler) = on_change {
461 handler(index, window, cx);
462 }
463 }
464 },
465 ),
466 )
467 })
468 .when(!is_accessible, |this| this.opacity(0.5))
469 .child(self.render_step_indicator(index, status, &step, &theme))
470 .child(
471 div()
472 .flex()
473 .flex_col()
474 .items_center()
475 .gap(px(2.0))
476 .child(
477 div()
478 .text_size(self.size.title_size())
479 .font_weight(FontWeight::MEDIUM)
480 .text_color(if status == StepStatus::Current {
481 theme.tokens.foreground
482 } else {
483 theme.tokens.muted_foreground
484 })
485 .child(step.title.clone()),
486 )
487 .when_some(step.description.clone(), |this, desc| {
488 this.child(
489 div()
490 .text_size(self.size.description_size())
491 .text_color(theme.tokens.muted_foreground)
492 .child(desc),
493 )
494 }),
495 ),
496 )
497 .when(self.show_connector && !is_last, |this| {
498 this.child(self.render_connector(completed_steps.contains(&index), &theme))
499 })
500 }))
501 }
502
503 fn render_vertical(
504 self,
505 window: &mut Window,
506 theme: crate::capabilities::foundation::theme::Theme,
507 steps: Vec<StepItem>,
508 current_step: usize,
509 completed_steps: HashSet<usize>,
510 accessible: Vec<bool>,
511 user_style: StyleRefinement,
512 ) -> Div {
513 let steps_len = steps.len();
514 let state = self.state.clone();
515
516 div()
517 .flex()
518 .flex_col()
519 .map(|this| {
520 let mut d = this;
521 d.style().refine(&user_style);
522 d
523 })
524 .children(steps.into_iter().enumerate().map(|(index, step)| {
525 let status = if step.status == StepStatus::Error {
526 StepStatus::Error
527 } else if index == current_step {
528 StepStatus::Current
529 } else if completed_steps.contains(&index) {
530 StepStatus::Completed
531 } else {
532 StepStatus::Pending
533 };
534
535 let is_accessible = accessible.get(index).copied().unwrap_or(false);
536
537 let is_last = index == steps_len - 1;
538 let on_change = self.on_step_change.clone();
539 let state_clone = state.clone();
540 let clickable = self.clickable && is_accessible;
541 let indicator_size = self.size.indicator_size();
542
543 div()
544 .flex()
545 .child(
546 div()
547 .flex()
548 .flex_col()
549 .items_center()
550 .child(
551 div()
552 .when(clickable, |this| {
553 this.cursor(CursorStyle::PointingHand).on_mouse_down(
554 MouseButton::Left,
555 window.listener_for(
556 &state_clone.clone(),
557 move |state, _, window, cx| {
558 if state.go_to(index, cx) {
559 if let Some(ref handler) = on_change {
560 handler(index, window, cx);
561 }
562 }
563 },
564 ),
565 )
566 })
567 .when(!is_accessible, |this| this.opacity(0.5))
568 .child(
569 self.render_step_indicator(index, status, &step, &theme),
570 ),
571 )
572 .when(self.show_connector && !is_last, |this| {
573 this.child(
574 self.render_connector(completed_steps.contains(&index), &theme),
575 )
576 }),
577 )
578 .child(
579 div()
580 .flex()
581 .flex_col()
582 .pl(px(12.0))
583 .pt(px(4.0))
584 .pb(if is_last { px(0.0) } else { px(24.0) })
585 .min_h(indicator_size + px(8.0))
586 .child(
587 div()
588 .text_size(self.size.title_size())
589 .font_weight(FontWeight::MEDIUM)
590 .text_color(if status == StepStatus::Current {
591 theme.tokens.foreground
592 } else {
593 theme.tokens.muted_foreground
594 })
595 .child(step.title.clone()),
596 )
597 .when_some(step.description.clone(), |this, desc| {
598 this.child(
599 div()
600 .text_size(self.size.description_size())
601 .text_color(theme.tokens.muted_foreground)
602 .mt(px(2.0))
603 .child(desc),
604 )
605 }),
606 )
607 }))
608 }
609}
610
611impl Styled for Stepper {
612 fn style(&mut self) -> &mut StyleRefinement {
613 &mut self.style
614 }
615}
616
617impl RenderOnce for Stepper {
618 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
619 let theme = use_theme(cx);
620 let state = self.state.read(cx);
621 let steps = state.steps.clone();
622 let current_step = state.current_step;
623 let completed_steps = state.completed_steps.clone();
624 let accessible: Vec<bool> = (0..steps.len())
625 .map(|index| state.is_step_accessible(index))
626 .collect();
627 let user_style = self.style.clone();
628
629 match self.orientation {
630 StepperOrientation::Horizontal => self.render_horizontal(
631 window,
632 theme,
633 steps,
634 current_step,
635 completed_steps,
636 accessible,
637 user_style,
638 ),
639 StepperOrientation::Vertical => self.render_vertical(
640 window,
641 theme,
642 steps,
643 current_step,
644 completed_steps,
645 accessible,
646 user_style,
647 ),
648 }
649 }
650}