use std::rc::Rc;
use gpui::{div, prelude::*, px, App, ElementId, SharedString, StyleRefinement, Window};
use crate::sx::SxStyled;
use crate::{
hooks::EventHandler,
icon::{Icon, IconName},
styles,
styles::ApplyStyleOverrides,
theme::ActiveTheme,
};
#[derive(IntoElement)]
pub struct Checkbox {
id: ElementId,
checked: bool,
label: Option<SharedString>,
disabled: bool,
on_change: Option<EventHandler<bool>>,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(Checkbox);
impl Checkbox {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
checked: false,
label: None,
disabled: false,
on_change: None,
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
#[must_use]
pub fn checked(mut self, checked: bool) -> Self {
self.checked = checked;
self
}
#[must_use]
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
#[must_use]
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
#[must_use]
pub fn on_change(mut self, handler: impl Fn(&bool, &mut Window, &mut App) + 'static) -> Self {
self.on_change = Some(Rc::new(handler));
self
}
}
styles! {
CHECKBOX = {
row: { display: flex, align: center, gap: 2, text: sm, radius: 1 },
interactive: { cursor: pointer, focus: { shadow: ring } },
inert: { opacity: 0.5 },
check_box: {
display: flex,
flex: none,
align: center,
justify: center,
size: 4,
border: 1,
border_color: input,
background: background,
shadow: xs,
},
checked: { border_color: primary, background: primary },
}
}
impl RenderOnce for Checkbox {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let checked = self.checked;
let is_interactive = !self.disabled;
let check_box = div()
.rounded(px(4.).min(theme.radius))
.sx((&CHECKBOX.check_box, checked.then_some(&CHECKBOX.checked)))
.when(checked, |check_box| {
check_box.child(
Icon::new(IconName::Check)
.size(px(14.))
.color(theme.colors.primary_foreground),
)
});
div()
.id(self.id)
.child(check_box)
.when_some(self.label, |row, label| {
row.child(crate::components::bidi_text::text(label))
})
.when(is_interactive, |row| {
row.tab_index(0).when_some(self.on_change, |row, handler| {
row.on_click(move |_, window, cx| handler(&!checked, window, cx))
})
})
.sx((
&CHECKBOX.row,
if is_interactive {
&CHECKBOX.interactive
} else {
&CHECKBOX.inert
},
&self.sx,
))
.apply_style_overrides(&self.style_overrides)
}
}