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]
17 Idle,
18 Hovering,
20}
21
22#[derive(PartialEq)]
25pub struct Chip {
26 pub(crate) theme: Option<ChipThemePartial>,
27 children: Vec<Element>,
28 on_press: Option<EventHandler<Event<PressEventData>>>,
29 selected: bool,
30 enabled: bool,
31 key: DiffKey,
32}
33
34impl Default for Chip {
35 fn default() -> Self {
36 Self {
37 theme: None,
38 children: Vec::new(),
39 on_press: None,
40 selected: false,
41 enabled: true,
42 key: DiffKey::None,
43 }
44 }
45}
46
47impl Chip {
48 pub fn new() -> Self {
49 Self::default()
50 }
51
52 pub fn theme(mut self, theme: ChipThemePartial) -> Self {
53 self.theme = Some(theme);
54 self
55 }
56
57 pub fn selected(mut self, selected: impl Into<bool>) -> Self {
58 self.selected = selected.into();
59 self
60 }
61
62 pub fn enabled(mut self, enabled: impl Into<bool>) -> Self {
63 self.enabled = enabled.into();
64 self
65 }
66
67 pub fn on_press(mut self, handler: impl Into<EventHandler<Event<PressEventData>>>) -> Self {
68 self.on_press = Some(handler.into());
69 self
70 }
71}
72
73impl ChildrenExt for Chip {
74 fn get_children(&mut self) -> &mut Vec<Element> {
75 &mut self.children
76 }
77}
78
79impl KeyExt for Chip {
80 fn write_key(&mut self) -> &mut DiffKey {
81 &mut self.key
82 }
83}
84
85impl Render for Chip {
86 fn render(&self) -> impl IntoElement {
87 let theme = get_theme!(&self.theme, chip);
88 let mut status = use_state(|| ChipStatus::Idle);
89 let focus = use_focus();
90 let focus_status = use_focus_status(focus);
91
92 let ChipTheme {
93 background,
94 hover_background,
95 selected_background,
96 border_fill,
97 selected_border_fill,
98 hover_border_fill,
99 focus_border_fill,
100 padding,
101 margin,
102 corner_radius,
103 width,
104 height,
105 color,
106 hover_color,
107 selected_color,
108 hover_icon_fill,
109 selected_icon_fill,
110 } = theme;
111
112 let on_press = self.on_press.clone();
113 let on_press = move |e: Event<PressEventData>| {
114 focus.request_focus();
115 if let Some(on_press) = &on_press {
116 on_press.call(e);
117 }
118 };
119
120 let on_pointer_enter = move |_| {
121 Cursor::set(CursorIcon::Pointer);
122 status.set(ChipStatus::Hovering);
123 };
124
125 let on_pointer_leave = move |_| {
126 if status() == ChipStatus::Hovering {
127 Cursor::set(CursorIcon::default());
128 status.set(ChipStatus::Idle);
129 }
130 };
131
132 let background = match status() {
133 ChipStatus::Hovering => hover_background,
134 _ if self.selected => selected_background,
135 _ => background,
136 };
137 let color = match status() {
138 ChipStatus::Hovering => hover_color,
139 _ if self.selected => selected_color,
140 _ => color,
141 };
142 let border_fill = match status() {
143 ChipStatus::Hovering => hover_border_fill,
144 _ if self.selected => selected_border_fill,
145 _ => border_fill,
146 };
147 let icon_fill = match status() {
148 ChipStatus::Hovering if self.selected => Some(hover_icon_fill),
149 _ if self.selected => Some(selected_icon_fill),
150 _ => None,
151 };
152 let border = if focus_status() == FocusStatus::Keyboard {
153 Border::new()
154 .fill(focus_border_fill)
155 .width(2.)
156 .alignment(BorderAlignment::Inner)
157 } else {
158 Border::new()
159 .fill(border_fill.mul_if(!self.enabled, 0.9))
160 .width(1.)
161 .alignment(BorderAlignment::Inner)
162 };
163
164 rect()
165 .a11y_id(focus.a11y_id())
166 .a11y_focusable(self.enabled)
167 .a11y_role(AccessibilityRole::Button)
168 .maybe(self.enabled, |rect| {
169 rect.on_press(on_press).on_pointer_enter(on_pointer_enter)
170 })
171 .on_pointer_leave(on_pointer_leave)
172 .width(width)
173 .height(height)
174 .padding(padding)
175 .margin(margin)
176 .overflow(Overflow::Clip)
177 .border(border)
178 .corner_radius(corner_radius)
179 .color(color.mul_if(!self.enabled, 0.9))
180 .background(background.mul_if(!self.enabled, 0.9))
181 .center()
182 .horizontal()
183 .spacing(4.)
184 .maybe_child(icon_fill.map(|icon_fill| {
185 TickIcon::new()
186 .fill(icon_fill)
187 .width(Size::px(12.))
188 .height(Size::px(12.))
189 }))
190 .children(self.children.clone())
191 }
192
193 fn render_key(&self) -> DiffKey {
194 self.key.clone().or(self.default_key())
195 }
196}