Skip to main content

gpui_component/stepper/
stepper.rs

1use std::rc::Rc;
2
3use gpui::{
4    App, Axis, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce,
5    StyleRefinement, Styled, Window, div, prelude::FluentBuilder as _,
6};
7
8use gpui_base::LogicalStyledExt as _;
9
10use crate::{AxisExt, Sizable, Size, StyledExt as _, stepper::StepperItem};
11
12/// A step-by-step progress for users to navigate through a series of steps or stages.
13#[derive(IntoElement)]
14pub struct Stepper {
15    id: ElementId,
16    style: StyleRefinement,
17    items: Vec<StepperItem>,
18    step: usize,
19    layout: Axis,
20    disabled: bool,
21    size: Size,
22    text_center: bool,
23    on_click: Rc<dyn Fn(&usize, &mut Window, &mut App) + 'static>,
24}
25
26impl Stepper {
27    /// Creates a new stepper with the given ID.
28    ///
29    /// Default use is horizontal layout with step 0 selected.
30    pub fn new(id: impl Into<ElementId>) -> Self {
31        Self {
32            id: id.into(),
33            style: StyleRefinement::default(),
34            items: Vec::new(),
35            step: 0,
36            layout: Axis::Horizontal,
37            disabled: false,
38            size: Size::default(),
39            text_center: false,
40            on_click: Rc::new(|_, _, _| {}),
41        }
42    }
43
44    /// Set whether to center the text within each stepper item.
45    pub fn text_center(mut self, center: bool) -> Self {
46        self.text_center = center;
47        self
48    }
49
50    /// Set the layout of the stepper, default is horizontal.
51    pub fn layout(mut self, layout: Axis) -> Self {
52        self.layout = layout;
53        self
54    }
55
56    /// Sets the layout of the stepper to Vertical.
57    pub fn vertical(mut self) -> Self {
58        self.layout = Axis::Vertical;
59        self
60    }
61
62    /// Sets the selected index of the stepper, default is 0.
63    pub fn selected_index(mut self, index: usize) -> Self {
64        self.step = index;
65        self
66    }
67
68    /// Adds a stepper item to the stepper.
69    pub fn item(mut self, item: StepperItem) -> Self {
70        self.items.push(item);
71        self
72    }
73
74    /// Add multiple stepper items to the stepper.
75    pub fn items(mut self, items: impl IntoIterator<Item = StepperItem>) -> Self {
76        self.items.extend(items);
77        self
78    }
79
80    /// Set the disabled state of the stepper, default is false.
81    pub fn disabled(mut self, disabled: bool) -> Self {
82        self.disabled = disabled;
83        self
84    }
85
86    /// Add an on_click handler for when a step is clicked.
87    ///
88    /// The first parameter is the `step` of currently clicked item.
89    pub fn on_click<F>(mut self, f: F) -> Self
90    where
91        F: Fn(&usize, &mut Window, &mut App) + 'static,
92    {
93        self.on_click = Rc::new(f);
94        self
95    }
96}
97
98impl Sizable for Stepper {
99    fn with_size(mut self, size: impl Into<Size>) -> Self {
100        self.size = size.into();
101        self
102    }
103}
104
105impl Styled for Stepper {
106    fn style(&mut self) -> &mut StyleRefinement {
107        &mut self.style
108    }
109}
110
111impl RenderOnce for Stepper {
112    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
113        let total_items = self.items.len();
114        div()
115            .id(self.id)
116            .w_full()
117            .when(self.layout.is_horizontal(), |this| {
118                this.h_flex().flex_row_dir(cx)
119            })
120            .when(self.layout.is_vertical(), |this| this.v_flex())
121            .refine_style(&self.style)
122            .children(self.items.into_iter().enumerate().map(|(step, item)| {
123                let is_last = step + 1 == total_items;
124                item.step(step)
125                    .with_size(self.size)
126                    .checked_step(self.step)
127                    .layout(self.layout)
128                    .text_center(self.text_center)
129                    .when(self.disabled, |this| this.disabled(true))
130                    .is_last(is_last)
131                    .on_click({
132                        let on_click = self.on_click.clone();
133                        move |_, window, cx| {
134                            on_click(&step, window, cx);
135                        }
136                    })
137            }))
138    }
139}