fc_ui/capabilities/controls/
radio.rs1use crate::capabilities::foundation::theme::use_theme;
29use gpui::{prelude::FluentBuilder as _, *};
30use std::rc::Rc;
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
34pub enum RadioLayout {
35 #[default]
37 Vertical,
38 Horizontal,
40}
41
42#[derive(IntoElement)]
44pub struct Radio {
45 base: Stateful<Div>,
46 id: ElementId,
47 label: Option<SharedString>,
48 checked: bool,
49 disabled: bool,
50 on_click: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
51 style: StyleRefinement,
52}
53
54impl Radio {
55 pub fn new(id: impl Into<ElementId>) -> Self {
57 let id = id.into();
58 Self {
59 id: id.clone(),
60 base: div().id(id),
61 label: None,
62 checked: false,
63 disabled: false,
64 on_click: None,
65 style: StyleRefinement::default(),
66 }
67 }
68
69 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
71 self.label = Some(label.into());
72 self
73 }
74
75 pub fn checked(mut self, checked: bool) -> Self {
77 self.checked = checked;
78 self
79 }
80
81 pub fn disabled(mut self, disabled: bool) -> Self {
83 self.disabled = disabled;
84 self
85 }
86
87 pub fn on_click(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
89 self.on_click = Some(Rc::new(handler));
90 self
91 }
92}
93
94impl InteractiveElement for Radio {
95 fn interactivity(&mut self) -> &mut Interactivity {
96 self.base.interactivity()
97 }
98}
99
100impl StatefulInteractiveElement for Radio {}
101
102impl Styled for Radio {
103 fn style(&mut self) -> &mut StyleRefinement {
104 &mut self.style
105 }
106}
107
108impl RenderOnce for Radio {
109 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
110 let theme = use_theme(cx);
111 let user_style = self.style;
112
113 let focus_handle = window
114 .use_keyed_state(self.id.clone(), cx, |_, cx| cx.focus_handle())
115 .read(cx)
116 .clone();
117 let is_focused = focus_handle.is_focused(window);
118
119 let (border_color, bg, dot_opacity) = if self.checked {
120 (theme.tokens.primary, theme.tokens.primary, 1.0)
121 } else {
122 (theme.tokens.input, theme.tokens.background, 0.0)
123 };
124
125 let (border_color, bg) = if self.disabled {
126 (border_color.opacity(0.5), bg.opacity(0.5))
127 } else {
128 (border_color, bg)
129 };
130
131 let shadow_xs = BoxShadow {
132 offset: theme.tokens.shadow_xs.offset,
133 blur_radius: theme.tokens.shadow_xs.blur_radius,
134 spread_radius: theme.tokens.shadow_xs.spread_radius,
135 inset: false,
136 color: theme.tokens.shadow_xs.color,
137 };
138 let focus_ring = theme.tokens.focus_ring_light();
139
140 self.base
141 .when(!self.disabled, |this| {
142 this.track_focus(&focus_handle.tab_index(0).tab_stop(true))
143 })
144 .flex()
145 .gap(px(8.0))
146 .items_center()
147 .text_sm()
148 .font_family(theme.tokens.font_family.clone())
149 .text_color(if self.disabled {
150 theme.tokens.muted_foreground
151 } else {
152 theme.tokens.foreground
153 })
154 .when(is_focused && !self.disabled, |this| {
155 this.shadow(smallvec::smallvec![focus_ring])
156 })
157 .rounded(theme.tokens.radius_md)
158 .child(
159 div()
160 .relative()
161 .size(px(16.0))
162 .flex_shrink_0()
163 .rounded_full()
164 .border_1()
165 .border_color(border_color)
166 .bg(bg)
167 .when(!self.disabled, |this| {
168 this.shadow(smallvec::smallvec![shadow_xs])
169 })
170 .child(
171 div()
172 .absolute()
173 .top(px(3.0))
174 .left(px(3.0))
175 .size(px(8.0))
176 .rounded_full()
177 .bg(theme.tokens.primary_foreground)
178 .opacity(dot_opacity),
179 ),
180 )
181 .when_some(self.label, |this, label| {
182 this.child(div().line_height(relative(1.0)).child(label))
183 })
184 .when(!self.disabled, |this| {
185 this.cursor(CursorStyle::PointingHand)
186 .on_mouse_down(MouseButton::Left, |_, window, _| {
187 window.prevent_default();
188 })
189 .when_some(self.on_click, |this, handler| {
190 this.on_click(move |_, window, cx| {
191 window.prevent_default();
192 cx.stop_propagation();
193 handler(window, cx);
194 })
195 })
196 })
197 .map(|this| {
198 let mut div = this;
199 div.style().refine(&user_style);
200 div
201 })
202 }
203}
204
205#[derive(IntoElement)]
220pub struct RadioGroup {
221 id: ElementId,
222 radios: Vec<Radio>,
223 layout: RadioLayout,
224 selected_index: Option<usize>,
225 disabled: bool,
226 on_change: Option<Rc<dyn Fn(&usize, &mut Window, &mut App)>>,
227}
228
229impl RadioGroup {
230 pub fn new(id: impl Into<ElementId>) -> Self {
232 Self {
233 id: id.into(),
234 radios: Vec::new(),
235 layout: RadioLayout::default(),
236 selected_index: None,
237 disabled: false,
238 on_change: None,
239 }
240 }
241
242 pub fn vertical(id: impl Into<ElementId>) -> Self {
244 Self::new(id)
245 }
246
247 pub fn horizontal(id: impl Into<ElementId>) -> Self {
249 Self::new(id).layout(RadioLayout::Horizontal)
250 }
251
252 pub fn layout(mut self, layout: RadioLayout) -> Self {
254 self.layout = layout;
255 self
256 }
257
258 pub fn selected_index(mut self, index: Option<usize>) -> Self {
260 self.selected_index = index;
261 self
262 }
263
264 pub fn disabled(mut self, disabled: bool) -> Self {
266 self.disabled = disabled;
267 self
268 }
269
270 pub fn on_change(mut self, handler: impl Fn(&usize, &mut Window, &mut App) + 'static) -> Self {
272 self.on_change = Some(Rc::new(handler));
273 self
274 }
275
276 pub fn child(mut self, child: impl Into<Radio>) -> Self {
278 self.radios.push(child.into());
279 self
280 }
281
282 pub fn children(mut self, children: impl IntoIterator<Item = impl Into<Radio>>) -> Self {
284 self.radios.extend(children.into_iter().map(Into::into));
285 self
286 }
287}
288
289impl RenderOnce for RadioGroup {
290 fn render(self, _: &mut Window, _cx: &mut App) -> impl IntoElement {
291 let on_change = self.on_change;
292 let disabled = self.disabled;
293 let selected_ix = self.selected_index;
294
295 div()
296 .id(self.id)
297 .flex()
298 .when(self.layout == RadioLayout::Vertical, |this| this.flex_col())
299 .when(self.layout == RadioLayout::Horizontal, |this| {
300 this.flex_row().flex_wrap()
301 })
302 .gap(px(12.0))
303 .children(self.radios.into_iter().enumerate().map(|(ix, radio)| {
304 let checked = selected_ix == Some(ix);
305 radio.checked(checked).disabled(disabled).when_some(
306 on_change.clone(),
307 |this, on_change| {
308 this.on_click(move |window, cx| {
309 on_change(&ix, window, cx);
310 })
311 },
312 )
313 }))
314 }
315}
316
317impl From<&'static str> for Radio {
319 fn from(label: &'static str) -> Self {
320 Self::new(label).label(label)
321 }
322}
323
324impl From<SharedString> for Radio {
325 fn from(label: SharedString) -> Self {
326 Self::new(label.clone()).label(label)
327 }
328}
329
330impl From<String> for Radio {
331 fn from(label: String) -> Self {
332 let shared: SharedString = label.into();
333 Self::new(shared.clone()).label(shared)
334 }
335}