fc_ui/capabilities/controls/
checkbox.rs1use crate::capabilities::foundation::theme::use_theme;
4use crate::capabilities::primitives::icon::{Icon, IconSize as IconSizeEnum};
5use gpui::{prelude::FluentBuilder as _, *};
6use std::rc::Rc;
7
8#[derive(Copy, Clone, Debug, PartialEq, Eq)]
9pub enum CheckboxSize {
10 Sm,
11 Md,
12}
13#[derive(IntoElement)]
14pub struct Checkbox {
15 id: ElementId,
16 base: Stateful<Div>,
17 checked: bool,
18 indeterminate: bool,
19 disabled: bool,
20 label: Option<SharedString>,
21 on_click: Option<Rc<dyn Fn(&bool, &mut Window, &mut App)>>,
22 size: CheckboxSize,
23 style: StyleRefinement,
24 checked_icon: SharedString,
26 indeterminate_icon: SharedString,
27}
28
29impl Checkbox {
30 pub fn new(id: impl Into<ElementId>) -> Self {
31 let id = id.into();
32 Self {
33 id: id.clone(),
34 base: div().id(id),
35 checked: false,
36 indeterminate: false,
37 disabled: false,
38 label: None,
39 on_click: None,
40 size: CheckboxSize::Md,
41 style: StyleRefinement::default(),
42 checked_icon: "check".into(),
43 indeterminate_icon: "minus".into(),
44 }
45 }
46
47 pub fn checked(mut self, checked: bool) -> Self {
48 self.checked = checked;
49 self
50 }
51
52 pub fn indeterminate(mut self, indeterminate: bool) -> Self {
53 self.indeterminate = indeterminate;
54 self
55 }
56
57 pub fn disabled(mut self, disabled: bool) -> Self {
58 self.disabled = disabled;
59 self
60 }
61
62 pub fn label<T: Into<SharedString>>(mut self, label: T) -> Self {
63 self.label = Some(label.into());
64 self
65 }
66
67 pub fn on_click<F>(mut self, handler: F) -> Self
68 where
69 F: Fn(&bool, &mut Window, &mut App) + 'static,
70 {
71 self.on_click = Some(Rc::new(handler));
72 self
73 }
74
75 pub fn size(mut self, size: CheckboxSize) -> Self {
76 self.size = size;
77 self
78 }
79
80 pub fn checked_icon(mut self, icon: impl Into<SharedString>) -> Self {
81 self.checked_icon = icon.into();
82 self
83 }
84
85 pub fn indeterminate_icon(mut self, icon: impl Into<SharedString>) -> Self {
86 self.indeterminate_icon = icon.into();
87 self
88 }
89}
90
91impl Styled for Checkbox {
92 fn style(&mut self) -> &mut StyleRefinement {
93 &mut self.style
94 }
95}
96
97impl InteractiveElement for Checkbox {
98 fn interactivity(&mut self) -> &mut Interactivity {
99 self.base.interactivity()
100 }
101}
102
103impl StatefulInteractiveElement for Checkbox {}
104
105impl RenderOnce for Checkbox {
106 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
107 let theme = use_theme(cx);
108
109 let size = match self.size {
110 CheckboxSize::Sm => px(16.0),
111 CheckboxSize::Md => px(20.0),
112 };
113
114 let border_radius = theme.tokens.radius_sm;
115 let checked = self.checked;
116 let indeterminate = self.indeterminate;
117 let aria_label = self.label.clone();
118 let aria_toggled = if indeterminate {
119 Toggled::Mixed
120 } else {
121 checked.into()
122 };
123
124 let (bg, border, fg) = if self.disabled {
125 (
126 theme.tokens.muted,
127 theme.tokens.muted_foreground.opacity(0.3),
128 theme.tokens.muted_foreground,
129 )
130 } else if checked || indeterminate {
131 (
132 theme.tokens.primary,
133 theme.tokens.primary,
134 theme.tokens.primary_foreground,
135 )
136 } else {
137 (
138 theme.tokens.background,
139 theme.tokens.border,
140 theme.tokens.primary_foreground,
141 )
142 };
143
144 let focus_handle = window
145 .use_keyed_state(self.id.clone(), cx, |_, cx| cx.focus_handle())
146 .read(cx)
147 .clone();
148
149 let user_style = self.style;
150
151 self.base
152 .role(Role::CheckBox)
153 .when_some(aria_label, |this, label| this.aria_label(label))
154 .aria_toggled(aria_toggled)
155 .aria_disabled(self.disabled)
156 .when(!self.disabled, |this| {
157 this.track_focus(&focus_handle.tab_index(0).tab_stop(true))
158 })
159 .flex()
160 .items_center()
161 .gap(px(8.0))
162 .child(
163 div()
164 .size(size)
165 .flex()
166 .items_center()
167 .justify_center()
168 .bg(bg)
169 .border_1()
170 .border_color(border)
171 .rounded(border_radius)
172 .cursor(if self.disabled {
173 CursorStyle::Arrow
174 } else {
175 CursorStyle::PointingHand
176 })
177 .when(self.disabled, |this| this.opacity(0.6))
178 .when(!self.disabled && !checked && !indeterminate, |this| {
179 this.hover(|style| style.border_color(theme.tokens.primary.opacity(0.5)))
180 })
181 .child(checkbox_icon(
182 self.id.clone(),
183 checked,
184 indeterminate,
185 fg,
186 self.size,
187 self.checked_icon.clone(),
188 self.indeterminate_icon.clone(),
189 window,
190 cx,
191 )),
192 )
193 .when_some(self.label, |this, label| {
194 this.child(
195 div()
196 .text_size(match self.size {
197 CheckboxSize::Sm => px(13.0),
198 CheckboxSize::Md => px(14.0),
199 })
200 .font_family(theme.tokens.font_family.clone())
201 .text_color(if self.disabled {
202 theme.tokens.muted_foreground
203 } else {
204 theme.tokens.foreground
205 })
206 .cursor(if self.disabled {
207 CursorStyle::Arrow
208 } else {
209 CursorStyle::PointingHand
210 })
211 .child(label),
212 )
213 })
214 .on_mouse_down(MouseButton::Left, |_, window, _| {
215 window.prevent_default();
216 })
217 .when(!self.disabled, |this| {
218 this.when_some(self.on_click, |this, on_click| {
219 let on_click_for_key = on_click.clone();
220 this.on_click(move |_, window, cx| {
221 cx.stop_propagation();
222 let new_checked = !checked;
223 (on_click)(&new_checked, window, cx);
224 })
225 .on_key_down(move |event, window, cx| {
226 if event.keystroke.key == "space" {
227 let new_checked = !checked;
228 (on_click_for_key)(&new_checked, window, cx);
229 cx.stop_propagation();
230 }
231 })
232 })
233 })
234 .map(|this| {
235 let mut div = this;
236 div.style().refine(&user_style);
237 div
238 })
239 }
240}
241
242fn checkbox_icon(
243 id: ElementId,
244 checked: bool,
245 indeterminate: bool,
246 color: Hsla,
247 size: CheckboxSize,
248 checked_icon: SharedString,
249 indeterminate_icon: SharedString,
250 window: &mut Window,
251 cx: &mut App,
252) -> impl IntoElement {
253 let toggle_state = window.use_keyed_state(id.clone(), cx, |_, _| (checked, indeterminate));
254
255 let icon_size = match size {
256 CheckboxSize::Sm => px(10.0),
257 CheckboxSize::Md => px(14.0),
258 };
259
260 let (prev_checked, prev_indeterminate) = *toggle_state.read(cx);
261
262 let needs_animation = prev_checked != checked || prev_indeterminate != indeterminate;
263
264 if needs_animation {
265 let duration = std::time::Duration::from_millis(150);
266 cx.spawn({
267 let toggle_state = toggle_state.clone();
268 async move |cx| {
269 cx.background_executor().timer(duration).await;
270 _ = toggle_state.update(cx, |state, _| {
271 *state = (checked, indeterminate);
272 });
273 }
274 })
275 .detach();
276 }
277
278 let opacity = if needs_animation {
279 if checked || indeterminate {
280 0.0
281 } else {
282 1.0
283 }
284 } else {
285 if checked || indeterminate {
286 1.0
287 } else {
288 0.0
289 }
290 };
291
292 let icon_name = if checked && !indeterminate {
293 Some(checked_icon)
294 } else if indeterminate {
295 Some(indeterminate_icon)
296 } else {
297 None
298 };
299
300 div()
301 .size_full()
302 .flex()
303 .items_center()
304 .justify_center()
305 .opacity(opacity)
306 .when_some(icon_name, |this, icon| {
307 this.child(
308 Icon::new(icon.as_ref())
309 .size(IconSizeEnum::Custom(icon_size))
310 .color(color),
311 )
312 })
313}