| 1 | use gpui::{ |
| 2 | AnyElement, App, Axis, ClickEvent, InteractiveElement as _, IntoElement, ParentElement, Pixels, |
| 3 | RenderOnce, StatefulInteractiveElement as _, StyleRefinement, Styled, Window, div, |
| 4 | prelude::FluentBuilder as _, px, |
| 5 | }; |
| 6 | use gpui_base::TestSupportExt as _; |
| 7 | |
| 8 | use crate::{ActiveTheme as _, AxisExt, Icon, Size, StyleSized, StyledExt as _, ThemeStyled as _}; |
| 9 | |
| 10 | /// The trigger part of a stepper item. |
| 11 | #[derive(IntoElement)] |
| 12 | pub(super) struct StepperTrigger { |
| 13 | step: usize, |
| 14 | checked_step: usize, |
| 15 | style: StyleRefinement, |
| 16 | icon: Option<Icon>, |
| 17 | icon_size: Pixels, |
| 18 | children: Vec<AnyElement>, |
| 19 | layout: Axis, |
| 20 | disabled: bool, |
| 21 | text_center: bool, |
| 22 | size: Size, |
| 23 | on_click: Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>, |
| 24 | } |
| 25 | |
| 26 | impl StepperTrigger { |
| 27 | pub(super) fn new() -> Self { |
| 28 | Self { |
| 29 | step: 0, |
| 30 | checked_step: 0, |
| 31 | icon: None, |
| 32 | icon_size: px(24.), |
| 33 | layout: Axis::Horizontal, |
| 34 | disabled: false, |
| 35 | size: Size::default(), |
| 36 | children: Vec::new(), |
| 37 | text_center: false, |
| 38 | style: StyleRefinement::default(), |
| 39 | on_click: Box::new(|_, _, _| {}), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub(super) fn step(mut self, ix: usize) -> Self { |
| 44 | self.step = ix; |
| 45 | self |
| 46 | } |
| 47 | |
| 48 | pub(super) fn checked_step(mut self, checked_step: usize) -> Self { |
| 49 | self.checked_step = checked_step; |
| 50 | self |
| 51 | } |
| 52 | |
| 53 | pub(super) fn layout(mut self, layout: Axis) -> Self { |
| 54 | self.layout = layout; |
| 55 | self |
| 56 | } |
| 57 | |
| 58 | pub(super) fn icon(mut self, icon: Option<impl Into<Icon>>) -> Self { |
| 59 | self.icon = icon.map(|i| i.into()); |
| 60 | self |
| 61 | } |
| 62 | |
| 63 | pub(super) fn icon_size(mut self, size: Pixels) -> Self { |
| 64 | self.icon_size = size; |
| 65 | self |
| 66 | } |
| 67 | |
| 68 | pub(super) fn disabled(mut self, disabled: bool) -> Self { |
| 69 | self.disabled = disabled; |
| 70 | self |
| 71 | } |
| 72 | |
| 73 | pub(super) fn with_size(mut self, size: Size) -> Self { |
| 74 | self.size = size; |
| 75 | self |
| 76 | } |
| 77 | |
| 78 | pub(super) fn text_center(mut self, center: bool) -> Self { |
| 79 | self.text_center = center; |
| 80 | self |
| 81 | } |
| 82 | |
| 83 | pub(super) fn on_click<F>(mut self, f: F) -> Self |
| 84 | where |
| 85 | F: Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 86 | { |
| 87 | self.on_click = Box::new(f); |
| 88 | self |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl Styled for StepperTrigger { |
| 93 | fn style(&mut self) -> &mut StyleRefinement { |
| 94 | &mut self.style |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl ParentElement for StepperTrigger { |
| 99 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 100 | self.children.extend(elements); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | impl RenderOnce for StepperTrigger { |
| 105 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 106 | let is_checked = self.step <= self.checked_step; |
| 107 | |
| 108 | div() |
| 109 | .id(("trigger", self.step)) |
| 110 | .test_support() |
| 111 | .when(self.layout.is_horizontal(), |this| this.v_flex().gap_1()) |
| 112 | .when(self.layout.is_vertical(), |this| this.h_flex().gap_2()) |
| 113 | .items_start() |
| 114 | .when(self.text_center, |this| this.items_center()) |
| 115 | .input_text_size(self.size.smaller()) |
| 116 | .refine_style(&self.style) |
| 117 | .child( |
| 118 | div() |
| 119 | .id("indicator") |
| 120 | .size(self.icon_size) |
| 121 | .overflow_hidden() |
| 122 | .flex() |
| 123 | .rounded_full_style(cx) |
| 124 | .items_center() |
| 125 | .justify_center() |
| 126 | .bg(cx.theme().tokens.secondary) |
| 127 | .when(!self.disabled && !is_checked, |this| { |
| 128 | this.hover(|this| this.bg(cx.theme().tokens.secondary_hover)) |
| 129 | .active(|this| this.bg(cx.theme().tokens.secondary_active)) |
| 130 | }) |
| 131 | .text_color(cx.theme().secondary_foreground) |
| 132 | .when(is_checked, |this| { |
| 133 | this.bg(cx.theme().tokens.primary) |
| 134 | .text_color(cx.theme().primary_foreground) |
| 135 | }) |
| 136 | .when(self.size != Size::XSmall, |this| { |
| 137 | this.map(|this| { |
| 138 | this.child(if let Some(icon) = self.icon { |
| 139 | icon.into_any_element() |
| 140 | } else { |
| 141 | div().child(format!("{}", self.step + 1)).into_any_element() |
| 142 | }) |
| 143 | }) |
| 144 | }), |
| 145 | ) |
| 146 | .children(self.children) |
| 147 | .when(!self.disabled, |this| { |
| 148 | this.on_click(move |event, window, cx| { |
| 149 | (self.on_click)(event, window, cx); |
| 150 | }) |
| 151 | }) |
| 152 | } |
| 153 | } |