use std::rc::Rc;
use gpui::{
App, ElementId, Entity, FocusHandle, IntoElement, RenderOnce, Role, SharedString, Window,
};
use gpui_material_foundation::InteractionSource;
use crate::{ListItem, ListItemColors, ListVariant, Switch, SwitchColors, action::Change};
use super::common::{TileFields, apply_standalone_variant};
#[derive(IntoElement)]
pub struct SwitchTile {
fields: TileFields,
checked: bool,
switch_colors: SwitchColors,
on_change: Option<Change>,
}
impl SwitchTile {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>, checked: bool) -> Self {
Self {
fields: TileFields::new(id, label),
checked,
switch_colors: SwitchColors::default(),
on_change: None,
}
}
pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
self.fields.supporting = Some(text.into());
self
}
pub fn overline(mut self, text: impl Into<SharedString>) -> Self {
self.fields.overline = Some(text.into());
self
}
pub fn leading(mut self, leading: impl IntoElement) -> Self {
self.fields.leading = Some(leading.into_any_element());
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.fields.disabled = disabled;
self
}
pub fn variant(mut self, variant: ListVariant) -> Self {
self.fields.variant = variant;
self
}
pub fn colors(mut self, colors: ListItemColors) -> Self {
self.fields.colors = colors;
self
}
pub fn switch_colors(mut self, colors: SwitchColors) -> Self {
self.switch_colors = colors;
self
}
pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
self.fields.focus = Some(focus.clone());
self
}
pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
self.fields.source = Some(source.clone());
self
}
pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_change = Some(Rc::new(handler));
self
}
pub(super) fn into_list_item(self) -> ListItem {
let switch = Switch::new(
(self.fields.id.clone(), "switch"),
self.fields.label.clone(),
self.checked,
)
.disabled(self.fields.disabled)
.colors(self.switch_colors)
.decorative(true);
let mut item = self
.fields
.into_list_item()
.checked(self.checked)
.checked_role(Role::Switch)
.checked_selects_row(false)
.trailing(switch);
if let Some(on_change) = self.on_change {
item = item.on_checked_change(move |next, window, cx| on_change(next, window, cx));
}
item
}
}
impl RenderOnce for SwitchTile {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let variant = self.fields.variant;
apply_standalone_variant(self.into_list_item(), variant)
}
}