gpui_material_components/settings_tiles/
radio.rs1use 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#[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 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 pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
42 self.fields.supporting = Some(text.into());
43 self
44 }
45
46 pub fn overline(mut self, text: impl Into<SharedString>) -> Self {
48 self.fields.overline = Some(text.into());
49 self
50 }
51
52 pub fn leading(mut self, leading: impl IntoElement) -> Self {
54 self.fields.leading = Some(leading.into_any_element());
55 self
56 }
57
58 pub fn trailing(mut self, trailing: impl IntoElement) -> Self {
61 self.trailing = Some(trailing.into_any_element());
62 self
63 }
64
65 pub fn trailing_radio(mut self, trailing: bool) -> Self {
67 self.trailing_radio = trailing;
68 self
69 }
70
71 pub fn disabled(mut self, disabled: bool) -> Self {
73 self.fields.disabled = disabled;
74 self
75 }
76
77 pub fn variant(mut self, variant: ListVariant) -> Self {
79 self.fields.variant = variant;
80 self
81 }
82
83 pub fn colors(mut self, colors: ListItemColors) -> Self {
85 self.fields.colors = colors;
86 self
87 }
88
89 pub fn radio_colors(mut self, colors: RadioColors) -> Self {
91 self.radio_colors = colors;
92 self
93 }
94
95 pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
97 self.fields.focus = Some(focus.clone());
98 self
99 }
100
101 pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
103 self.fields.source = Some(source.clone());
104 self
105 }
106
107 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}