| 1 | use gpui::{ |
| 2 | AnyElement, App, Axis, Div, ElementId, InteractiveElement, Interactivity, IntoElement, |
| 3 | ParentElement, RenderOnce, Role, Stateful, StatefulInteractiveElement, StyleRefinement, Styled, |
| 4 | Window, accesskit, div, |
| 5 | }; |
| 6 | use smallvec::SmallVec; |
| 7 | |
| 8 | use crate::StyledExt as _; |
| 9 | |
| 10 | /// An unstyled container for a set of radio elements. |
| 11 | #[derive(IntoElement)] |
| 12 | pub struct RadioGroup { |
| 13 | base: Stateful<Div>, |
| 14 | style: StyleRefinement, |
| 15 | axis: Axis, |
| 16 | children: SmallVec<[AnyElement; 4]>, |
| 17 | } |
| 18 | |
| 19 | impl RadioGroup { |
| 20 | pub fn new(id: impl Into<ElementId>) -> Self { |
| 21 | Self { |
| 22 | base: div().id(id.into()), |
| 23 | style: StyleRefinement::default(), |
| 24 | axis: Axis::Vertical, |
| 25 | children: SmallVec::new(), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /// Sets the semantic axis of the group. |
| 30 | pub fn axis(mut self, axis: Axis) -> Self { |
| 31 | self.axis = axis; |
| 32 | self |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl Styled for RadioGroup { |
| 37 | fn style(&mut self) -> &mut StyleRefinement { |
| 38 | &mut self.style |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl ParentElement for RadioGroup { |
| 43 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 44 | self.children.extend(elements); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl InteractiveElement for RadioGroup { |
| 49 | fn interactivity(&mut self) -> &mut Interactivity { |
| 50 | self.base.interactivity() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl StatefulInteractiveElement for RadioGroup {} |
| 55 | |
| 56 | impl RenderOnce for RadioGroup { |
| 57 | fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement { |
| 58 | self.base |
| 59 | .role(Role::RadioGroup) |
| 60 | .aria_orientation(match self.axis { |
| 61 | Axis::Horizontal => accesskit::Orientation::Horizontal, |
| 62 | Axis::Vertical => accesskit::Orientation::Vertical, |
| 63 | }) |
| 64 | .children(self.children) |
| 65 | .refine_style(&self.style) |
| 66 | } |
| 67 | } |