Skip to main content

gpui_component/stepper/
item.rs

1use gpui::{
2    AnyElement, App, Axis, ClickEvent, Half, InteractiveElement as _, IntoElement, ParentElement,
3    Pixels, RenderOnce, Role, StatefulInteractiveElement as _, StyleRefinement, Styled, Window,
4    div, prelude::FluentBuilder as _, px, relative,
5};
6use gpui_base::{LogicalStyledExt as _, TestSupportExt as _};
7
8use crate::{
9    ActiveTheme as _, AxisExt, Icon, Sizable, Size, StyledExt as _,
10    stepper::trigger::StepperTrigger,
11};
12
13/// A step item within a [`Stepper`].
14#[derive(IntoElement)]
15pub struct StepperItem {
16    step: usize,
17    checked_step: usize,
18    style: StyleRefinement,
19    icon: Option<Icon>,
20    children: Vec<AnyElement>,
21    layout: Axis,
22    disabled: bool,
23    size: Size,
24    is_last: bool,
25    text_center: bool,
26    on_click: Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
27}
28
29impl StepperItem {
30    pub fn new() -> Self {
31        Self {
32            step: 0,
33            checked_step: 0,
34            style: StyleRefinement::default(),
35            icon: None,
36            layout: Axis::Horizontal,
37            disabled: false,
38            size: Size::default(),
39            is_last: false,
40            text_center: false,
41            children: Vec::new(),
42            on_click: Box::new(|_, _, _| {}),
43        }
44    }
45
46    /// Set the icon of the stepper item.
47    pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
48        self.icon = Some(icon.into());
49        self
50    }
51
52    /// Set disabled state of the stepper item.
53    ///
54    /// Will override the stepper's disabled state if set to true.
55    ///
56    /// Default is false.
57    pub fn disabled(mut self, disabled: bool) -> Self {
58        self.disabled = disabled;
59        self
60    }
61
62    pub(super) fn text_center(mut self, center: bool) -> Self {
63        self.text_center = center;
64        self
65    }
66
67    pub(super) fn step(mut self, ix: usize) -> Self {
68        self.step = ix;
69        self
70    }
71
72    pub(super) fn checked_step(mut self, checked_step: usize) -> Self {
73        self.checked_step = checked_step;
74        self
75    }
76
77    pub(super) fn layout(mut self, layout: Axis) -> Self {
78        self.layout = layout;
79        self
80    }
81
82    pub(super) fn is_last(mut self, is_last: bool) -> Self {
83        self.is_last = is_last;
84        self
85    }
86
87    pub(super) fn on_click<F>(mut self, f: F) -> Self
88    where
89        F: Fn(&ClickEvent, &mut Window, &mut App) + 'static,
90    {
91        self.on_click = Box::new(f);
92        self
93    }
94}
95
96impl ParentElement for StepperItem {
97    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
98        self.children.extend(elements);
99    }
100}
101
102impl Sizable for StepperItem {
103    fn with_size(mut self, size: impl Into<Size>) -> Self {
104        self.size = size.into();
105        self
106    }
107}
108
109impl Styled for StepperItem {
110    fn style(&mut self) -> &mut StyleRefinement {
111        &mut self.style
112    }
113}
114
115impl RenderOnce for StepperItem {
116    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
117        let is_passed = self.step < self.checked_step;
118        let icon_size = match self.size {
119            Size::XSmall => px(8.),
120            Size::Small => px(18.),
121            Size::Large => px(32.),
122            _ => px(24.),
123        };
124
125        div()
126            .id(("stepper-item", self.step))
127            .test_support()
128            .role(Role::ListItem)
129            .aria_position_in_set(self.step + 1)
130            .relative()
131            .when(self.layout.is_horizontal(), |this| {
132                this.h_flex().flex_row_dir(cx)
133            })
134            .when(self.layout.is_vertical(), |this| this.v_flex())
135            .when(!self.is_last, |this| this.flex_1())
136            .when(self.text_center, |this| this.flex_1().justify_center())
137            .items_start()
138            .refine_style(&self.style)
139            .child(
140                StepperTrigger::new()
141                    .icon(self.icon)
142                    .icon_size(icon_size)
143                    .step(self.step)
144                    .with_size(self.size)
145                    .checked_step(self.checked_step)
146                    .text_center(self.text_center)
147                    .layout(self.layout)
148                    .disabled(self.disabled)
149                    .children(self.children)
150                    .on_click({
151                        let on_click = self.on_click;
152                        move |e, window, cx| {
153                            on_click(e, window, cx);
154                        }
155                    }),
156            )
157            .when(!self.is_last, |this| {
158                this.child(
159                    StepperSeparator::new()
160                        .with_size(self.size)
161                        .layout(self.layout)
162                        .text_center(self.text_center)
163                        .icon_size(icon_size)
164                        .checked(is_passed),
165                )
166            })
167    }
168}
169
170/// A separator between stepper items.
171///
172/// Default is `absolute` positioned.
173#[derive(IntoElement)]
174struct StepperSeparator {
175    size: Size,
176    checked: bool,
177    icon_size: Pixels,
178    layout: Axis,
179    style: StyleRefinement,
180    text_center: bool,
181}
182
183impl StepperSeparator {
184    fn new() -> Self {
185        Self {
186            size: Size::default(),
187            checked: false,
188            icon_size: px(24.),
189            layout: Axis::Horizontal,
190            style: StyleRefinement::default(),
191            text_center: false,
192        }
193    }
194
195    fn with_size(mut self, size: Size) -> Self {
196        self.size = size;
197        self
198    }
199
200    fn text_center(mut self, center: bool) -> Self {
201        self.text_center = center;
202        self
203    }
204
205    fn layout(mut self, layout: Axis) -> Self {
206        self.layout = layout;
207        self
208    }
209
210    fn icon_size(mut self, size: Pixels) -> Self {
211        self.icon_size = size;
212        self
213    }
214
215    fn checked(mut self, checked: bool) -> Self {
216        self.checked = checked;
217        self
218    }
219}
220
221impl Styled for StepperSeparator {
222    fn style(&mut self) -> &mut StyleRefinement {
223        &mut self.style
224    }
225}
226
227impl RenderOnce for StepperSeparator {
228    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
229        let icon_size = self.icon_size;
230        let text_center = self.text_center;
231        let separator_wide = match self.size {
232            Size::XSmall => px(1.5),
233            Size::Large => px(3.),
234            _ => px(2.),
235        };
236
237        let gap = px(4.);
238
239        div()
240            .absolute()
241            .flex_1()
242            .when(self.layout.is_horizontal(), |this| {
243                this.h(separator_wide).mt(icon_size.half()).map(|this| {
244                    if !text_center {
245                        this.ms(icon_size + gap, cx).me(gap, cx).left_0().right_0()
246                    } else {
247                        this.mx(icon_size.half() + gap)
248                            .inset_s(relative(0.5), cx)
249                            .inset_e(relative(-0.5), cx)
250                    }
251                })
252            })
253            .when(self.layout.is_vertical(), |this| {
254                this.w(separator_wide).ms(icon_size.half(), cx).map(|this| {
255                    if !text_center {
256                        this.mt(icon_size + gap).mb(gap).top_0().bottom_0()
257                    } else {
258                        this.mx(icon_size.half() + gap)
259                            .top(relative(0.5))
260                            .bottom(relative(-0.5))
261                    }
262                })
263            })
264            .refine_style(&self.style)
265            .bg(cx.theme().border)
266            .when(self.checked, |this| this.bg(cx.theme().tokens.primary))
267    }
268}