Skip to main content

guise/input/
radiogroup.rs

1//! `RadioGroup` — a controlled set of mutually-exclusive [`Radio`]s.
2//!
3//! The parent owns the selected index; the group wires exclusivity and reports
4//! the new index through `on_change`. This is the ergonomic layer over the bare
5//! `Radio`, which leaves grouping to the caller.
6
7use std::rc::Rc;
8
9use gpui::prelude::*;
10use gpui::{div, px, App, IntoElement, SharedString, Window};
11
12use super::Radio;
13use crate::reactive::Binding;
14use crate::theme::{theme, ColorName, Size};
15
16type GroupHandler = Rc<dyn Fn(usize, &mut Window, &mut App) + 'static>;
17
18/// A vertical group of radios with a single selected value.
19#[derive(IntoElement)]
20pub struct RadioGroup {
21    options: Vec<SharedString>,
22    value: Option<usize>,
23    color: ColorName,
24    size: Size,
25    label: Option<SharedString>,
26    binding: Option<Binding<usize>>,
27    on_change: Option<GroupHandler>,
28}
29
30impl RadioGroup {
31    pub fn new() -> Self {
32        RadioGroup {
33            options: Vec::new(),
34            value: None,
35            color: ColorName::Blue,
36            size: Size::Sm,
37            label: None,
38            binding: None,
39            on_change: None,
40        }
41    }
42
43    pub fn options<I, S>(mut self, options: I) -> Self
44    where
45        I: IntoIterator<Item = S>,
46        S: Into<SharedString>,
47    {
48        self.options = options.into_iter().map(Into::into).collect();
49        self
50    }
51
52    /// The currently selected index.
53    pub fn value(mut self, value: usize) -> Self {
54        self.value = Some(value);
55        self
56    }
57
58    pub fn color(mut self, color: ColorName) -> Self {
59        self.color = color;
60        self
61    }
62
63    pub fn size(mut self, size: Size) -> Self {
64        self.size = size;
65        self
66    }
67
68    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
69        self.label = Some(label.into());
70        self
71    }
72
73    /// Two-way bind the selected index. Overrides `value`; clicking a radio
74    /// writes its index back through the binding, then runs any `on_change`.
75    pub fn bind(mut self, binding: Binding<usize>) -> Self {
76        self.binding = Some(binding);
77        self
78    }
79
80    /// Called with the newly selected index when a radio is clicked.
81    pub fn on_change(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
82        self.on_change = Some(Rc::new(handler));
83        self
84    }
85}
86
87impl Default for RadioGroup {
88    fn default() -> Self {
89        RadioGroup::new()
90    }
91}
92
93impl RenderOnce for RadioGroup {
94    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
95        let t = theme(cx);
96        let gap = t.spacing(Size::Xs);
97        let text = t.text().hsla();
98        let font = t.font_size(Size::Sm);
99        let value = self
100            .binding
101            .as_ref()
102            .map_or(self.value, |b| Some(b.get(cx)));
103
104        let mut column = div().flex().flex_col().gap(px(gap));
105        if let Some(label) = self.label.clone() {
106            column = column.child(div().text_size(px(font)).text_color(text).child(label));
107        }
108
109        for (i, option) in self.options.iter().enumerate() {
110            let mut radio = Radio::new(("guise-radiogroup", i))
111                .label(option.clone())
112                .checked(value == Some(i))
113                .color(self.color)
114                .size(self.size);
115            if self.binding.is_some() || self.on_change.is_some() {
116                let binding = self.binding.clone();
117                let handler = self.on_change.clone();
118                radio = radio.on_change(move |_ev, window, cx| {
119                    if let Some(binding) = &binding {
120                        binding.set(cx, i);
121                    }
122                    if let Some(handler) = &handler {
123                        handler(i, window, cx);
124                    }
125                });
126            }
127            column = column.child(radio);
128        }
129        column
130    }
131}