use std::rc::Rc;
use gpui::{
AnyElement, App, ElementId, Entity, FocusHandle, IntoElement, RenderOnce, Role, SharedString,
Window, accesskit::Toggled,
};
use gpui_material_foundation::InteractionSource;
use crate::{ListItem, ListItemColors, ListVariant, Radio, RadioColors, action::Activate};
use super::common::{TileFields, apply_standalone_variant};
#[derive(IntoElement)]
pub struct RadioTile {
fields: TileFields,
selected: bool,
trailing: Option<AnyElement>,
trailing_radio: bool,
radio_colors: RadioColors,
on_select: Option<Activate>,
}
impl RadioTile {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>, selected: bool) -> Self {
Self {
fields: TileFields::new(id, label),
selected,
trailing: None,
trailing_radio: true,
radio_colors: RadioColors::default(),
on_select: 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 trailing(mut self, trailing: impl IntoElement) -> Self {
self.trailing = Some(trailing.into_any_element());
self
}
pub fn trailing_radio(mut self, trailing: bool) -> Self {
self.trailing_radio = trailing;
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 radio_colors(mut self, colors: RadioColors) -> Self {
self.radio_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_select(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_select = Some(Rc::new(handler));
self
}
pub(super) fn into_list_item(mut self) -> ListItem {
let radio = Radio::new((self.fields.id.clone(), "radio"), self.fields.label.clone())
.selected(self.selected)
.disabled(self.fields.disabled)
.colors(self.radio_colors)
.decorative(true);
let toggled = if self.selected {
Toggled::True
} else {
Toggled::False
};
let leading = self.fields.leading.take();
let mut item = self
.fields
.into_list_item()
.checked_toggled(toggled)
.checked_role(Role::RadioButton)
.checked_selects_row(false);
if self.trailing_radio {
item = item.trailing(radio);
if let Some(leading) = leading {
item = item.leading(leading);
}
} else {
item = item.leading(radio);
if let Some(trailing) = self.trailing {
item = item.trailing(trailing);
}
}
if let Some(on_select) = self.on_select {
let already_selected = self.selected;
item = item.on_activate(move |window, cx| {
if !already_selected {
on_select(window, cx);
}
});
}
item
}
}
impl RenderOnce for RadioTile {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let variant = self.fields.variant;
apply_standalone_variant(self.into_list_item(), variant)
}
}