freya_components/
chip.rs

1use freya_core::prelude::*;
2use torin::size::Size;
3
4use crate::{
5    get_theme,
6    icons::tick::TickIcon,
7    theming::component_themes::{
8        ChipTheme,
9        ChipThemePartial,
10    },
11};
12
13#[derive(Debug, Default, PartialEq, Clone, Copy)]
14pub enum ChipStatus {
15    /// Default state.
16    #[default]
17    Idle,
18    /// Mouse is hovering the chip.
19    Hovering,
20}
21
22// TODO: Add layout and style variants
23// TODO: Ability to hide/customize icon
24///
25/// Chip component.
26///
27/// ```rust
28/// # use freya::prelude::*;
29/// fn app() -> impl IntoElement {
30///     Chip::new().child("Chip")
31/// }
32/// # use freya_testing::prelude::*;
33/// # launch_doc(|| {
34/// #   rect().center().expanded().child(app())
35/// # }, "./images/gallery_chip.png").render();
36/// ```
37///
38/// # Preview
39/// ![Chip Preview][chip]
40#[cfg_attr(feature = "docs",
41    doc = embed_doc_image::embed_image!("chip", "images/gallery_chip.png"),
42)]
43#[derive(PartialEq)]
44pub struct Chip {
45    pub(crate) theme: Option<ChipThemePartial>,
46    children: Vec<Element>,
47    on_press: Option<EventHandler<Event<PressEventData>>>,
48    selected: bool,
49    enabled: bool,
50    key: DiffKey,
51}
52
53impl Default for Chip {
54    fn default() -> Self {
55        Self {
56            theme: None,
57            children: Vec::new(),
58            on_press: None,
59            selected: false,
60            enabled: true,
61            key: DiffKey::None,
62        }
63    }
64}
65
66impl Chip {
67    pub fn new() -> Self {
68        Self::default()
69    }
70
71    pub fn theme(mut self, theme: ChipThemePartial) -> Self {
72        self.theme = Some(theme);
73        self
74    }
75
76    pub fn selected(mut self, selected: impl Into<bool>) -> Self {
77        self.selected = selected.into();
78        self
79    }
80
81    pub fn enabled(mut self, enabled: impl Into<bool>) -> Self {
82        self.enabled = enabled.into();
83        self
84    }
85
86    pub fn on_press(mut self, handler: impl Into<EventHandler<Event<PressEventData>>>) -> Self {
87        self.on_press = Some(handler.into());
88        self
89    }
90}
91
92impl ChildrenExt for Chip {
93    fn get_children(&mut self) -> &mut Vec<Element> {
94        &mut self.children
95    }
96}
97
98impl KeyExt for Chip {
99    fn write_key(&mut self) -> &mut DiffKey {
100        &mut self.key
101    }
102}
103
104impl Component for Chip {
105    fn render(&self) -> impl IntoElement {
106        let theme = get_theme!(&self.theme, chip);
107        let mut status = use_state(|| ChipStatus::Idle);
108        let focus = use_focus();
109        let focus_status = use_focus_status(focus);
110
111        let ChipTheme {
112            background,
113            hover_background,
114            selected_background,
115            border_fill,
116            selected_border_fill,
117            hover_border_fill,
118            focus_border_fill,
119            padding,
120            margin,
121            corner_radius,
122            width,
123            height,
124            color,
125            hover_color,
126            selected_color,
127            hover_icon_fill,
128            selected_icon_fill,
129        } = theme;
130
131        let enabled = use_reactive(&self.enabled);
132        use_drop(move || {
133            if status() == ChipStatus::Hovering && enabled() {
134                Cursor::set(CursorIcon::default());
135            }
136        });
137
138        let on_press = self.on_press.clone();
139        let on_press = move |e: Event<PressEventData>| {
140            focus.request_focus();
141            if let Some(on_press) = &on_press {
142                on_press.call(e);
143            }
144        };
145
146        let on_pointer_enter = move |_| {
147            status.set(ChipStatus::Hovering);
148            if enabled() {
149                Cursor::set(CursorIcon::Pointer);
150            } else {
151                Cursor::set(CursorIcon::NotAllowed);
152            }
153        };
154
155        let on_pointer_leave = move |_| {
156            if status() == ChipStatus::Hovering {
157                Cursor::set(CursorIcon::default());
158                status.set(ChipStatus::Idle);
159            }
160        };
161
162        let background = match status() {
163            ChipStatus::Hovering if enabled() => hover_background,
164            _ if self.selected => selected_background,
165            _ => background,
166        };
167        let color = match status() {
168            ChipStatus::Hovering if enabled() => hover_color,
169            _ if self.selected => selected_color,
170            _ => color,
171        };
172        let border_fill = match status() {
173            ChipStatus::Hovering if enabled() => hover_border_fill,
174            _ if self.selected => selected_border_fill,
175            _ => border_fill,
176        };
177        let icon_fill = match status() {
178            ChipStatus::Hovering if self.selected && enabled() => Some(hover_icon_fill),
179            _ if self.selected => Some(selected_icon_fill),
180            _ => None,
181        };
182        let border = if self.enabled && focus_status() == FocusStatus::Keyboard {
183            Border::new()
184                .fill(focus_border_fill)
185                .width(2.)
186                .alignment(BorderAlignment::Inner)
187        } else {
188            Border::new()
189                .fill(border_fill.mul_if(!self.enabled, 0.9))
190                .width(1.)
191                .alignment(BorderAlignment::Inner)
192        };
193
194        rect()
195            .a11y_id(focus.a11y_id())
196            .a11y_focusable(self.enabled)
197            .a11y_role(AccessibilityRole::Button)
198            .maybe(self.enabled, |rect| rect.on_press(on_press))
199            .on_pointer_enter(on_pointer_enter)
200            .on_pointer_leave(on_pointer_leave)
201            .width(width)
202            .height(height)
203            .padding(padding)
204            .margin(margin)
205            .overflow(Overflow::Clip)
206            .border(border)
207            .corner_radius(corner_radius)
208            .color(color.mul_if(!self.enabled, 0.9))
209            .background(background.mul_if(!self.enabled, 0.9))
210            .center()
211            .horizontal()
212            .spacing(4.)
213            .maybe_child(icon_fill.map(|icon_fill| {
214                TickIcon::new()
215                    .fill(icon_fill)
216                    .width(Size::px(12.))
217                    .height(Size::px(12.))
218            }))
219            .children(self.children.clone())
220    }
221
222    fn render_key(&self) -> DiffKey {
223        self.key.clone().or(self.default_key())
224    }
225}