Skip to main content

gpui_material_components/settings_tiles/
radio.rs

1use std::rc::Rc;
2
3use gpui::{
4    AnyElement, App, ElementId, Entity, FocusHandle, IntoElement, RenderOnce, Role, SharedString,
5    Window, accesskit::Toggled,
6};
7use gpui_material_foundation::InteractionSource;
8
9use crate::{ListItem, ListItemColors, ListVariant, Radio, RadioColors, action::Activate};
10
11use super::common::{TileFields, apply_standalone_variant};
12
13/// Settings convenience row wrapping [`ListItem`] with a synchronized decorative [`Radio`].
14///
15/// Clicking anywhere on an unselected row or pressing Enter or Space invokes `.on_select`
16/// with a single focus stop and `Role::RadioButton` semantics.
17#[derive(IntoElement)]
18pub struct RadioTile {
19    fields: TileFields,
20    selected: bool,
21    trailing: Option<AnyElement>,
22    trailing_radio: bool,
23    radio_colors: RadioColors,
24    on_select: Option<Activate>,
25}
26
27impl RadioTile {
28    /// Creates a radio settings row with an element ID, headline label, and controlled selected state.
29    pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>, selected: bool) -> Self {
30        Self {
31            fields: TileFields::new(id, label),
32            selected,
33            trailing: None,
34            trailing_radio: true,
35            radio_colors: RadioColors::default(),
36            on_select: None,
37        }
38    }
39
40    /// Sets secondary supporting text displayed below the headline.
41    pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
42        self.fields.supporting = Some(text.into());
43        self
44    }
45
46    /// Sets overline text displayed above the headline.
47    pub fn overline(mut self, text: impl Into<SharedString>) -> Self {
48        self.fields.overline = Some(text.into());
49        self
50    }
51
52    /// Sets an optional leading icon or graphic when the radio indicator is in the trailing slot.
53    pub fn leading(mut self, leading: impl IntoElement) -> Self {
54        self.fields.leading = Some(leading.into_any_element());
55        self
56    }
57
58    /// Trailing decorative content used when `.trailing_radio(false)` places the radio
59    /// in the leading slot.
60    pub fn trailing(mut self, trailing: impl IntoElement) -> Self {
61        self.trailing = Some(trailing.into_any_element());
62        self
63    }
64
65    /// Places the decorative radio indicator in the trailing slot (`true`, default) or leading slot (`false`).
66    pub fn trailing_radio(mut self, trailing: bool) -> Self {
67        self.trailing_radio = trailing;
68        self
69    }
70
71    /// Disables row interaction and dims the radio indicator when `true`.
72    pub fn disabled(mut self, disabled: bool) -> Self {
73        self.fields.disabled = disabled;
74        self
75    }
76
77    /// Sets standalone row styling (`Standard` or `Segmented`).
78    pub fn variant(mut self, variant: ListVariant) -> Self {
79        self.fields.variant = variant;
80        self
81    }
82
83    /// Overrides container and slot colors on the underlying [`ListItem`].
84    pub fn colors(mut self, colors: ListItemColors) -> Self {
85        self.fields.colors = colors;
86        self
87    }
88
89    /// Overrides indicator colors on the decorative [`Radio`].
90    pub fn radio_colors(mut self, colors: RadioColors) -> Self {
91        self.radio_colors = colors;
92        self
93    }
94
95    /// Uses a caller-owned focus handle for this tile.
96    pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
97        self.fields.focus = Some(focus.clone());
98        self
99    }
100
101    /// Reports hover, focus, press, and disabled states through a caller-owned source.
102    pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
103        self.fields.source = Some(source.clone());
104        self
105    }
106
107    /// Invoked when an unselected radio tile is activated.
108    pub fn on_select(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
109        self.on_select = Some(Rc::new(handler));
110        self
111    }
112
113    pub(super) fn into_list_item(mut self) -> ListItem {
114        let radio = Radio::new((self.fields.id.clone(), "radio"), self.fields.label.clone())
115            .selected(self.selected)
116            .disabled(self.fields.disabled)
117            .colors(self.radio_colors)
118            .decorative(true);
119
120        let toggled = if self.selected {
121            Toggled::True
122        } else {
123            Toggled::False
124        };
125
126        let leading = self.fields.leading.take();
127        let mut item = self
128            .fields
129            .into_list_item()
130            .checked_toggled(toggled)
131            .checked_role(Role::RadioButton)
132            .checked_selects_row(false);
133
134        if self.trailing_radio {
135            item = item.trailing(radio);
136            if let Some(leading) = leading {
137                item = item.leading(leading);
138            }
139        } else {
140            item = item.leading(radio);
141            if let Some(trailing) = self.trailing {
142                item = item.trailing(trailing);
143            }
144        }
145
146        if let Some(on_select) = self.on_select {
147            let already_selected = self.selected;
148            item = item.on_activate(move |window, cx| {
149                if !already_selected {
150                    on_select(window, cx);
151                }
152            });
153        }
154        item
155    }
156}
157
158impl RenderOnce for RadioTile {
159    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
160        let variant = self.fields.variant;
161        apply_standalone_variant(self.into_list_item(), variant)
162    }
163}