gpui_material_components/settings_tiles/
section.rs1use gpui::{
2 AnyElement, App, ElementId, FocusHandle, InteractiveElement, IntoElement, ParentElement,
3 RenderOnce, Role, SharedString, StatefulInteractiveElement, Styled, Window, div,
4 prelude::FluentBuilder, px,
5};
6use gpui_base::TestSupportExt;
7use gpui_material_foundation::current_material_theme;
8use gpui_material_theme::{ColorRole, CornerShape, Direction, TypeRole};
9
10use crate::{
11 ListItem, ListVariant, Text,
12 list::{compute_roving_tab_stop, handle_vertical_roving_key, segmented_corners},
13};
14
15#[cfg(feature = "settings-tiles")]
16use super::{CheckboxTile, ChoiceTile, RadioTile, SliderTile, SwitchTile};
17
18pub enum ListSectionItem {
20 Row(ListItem),
22 #[cfg(feature = "settings-tiles")]
24 Switch(SwitchTile),
25 #[cfg(feature = "settings-tiles")]
27 Checkbox(CheckboxTile),
28 #[cfg(feature = "settings-tiles")]
30 Radio(RadioTile),
31 #[cfg(feature = "settings-tiles")]
33 Slider(SliderTile),
34 #[cfg(feature = "settings-tiles")]
36 Choice(ChoiceTile),
37}
38
39impl From<ListItem> for ListSectionItem {
40 fn from(value: ListItem) -> Self {
41 Self::Row(value)
42 }
43}
44
45#[cfg(feature = "settings-tiles")]
46impl From<SwitchTile> for ListSectionItem {
47 fn from(value: SwitchTile) -> Self {
48 Self::Switch(value)
49 }
50}
51
52#[cfg(feature = "settings-tiles")]
53impl From<CheckboxTile> for ListSectionItem {
54 fn from(value: CheckboxTile) -> Self {
55 Self::Checkbox(value)
56 }
57}
58
59#[cfg(feature = "settings-tiles")]
60impl From<RadioTile> for ListSectionItem {
61 fn from(value: RadioTile) -> Self {
62 Self::Radio(value)
63 }
64}
65
66#[cfg(feature = "settings-tiles")]
67impl From<SliderTile> for ListSectionItem {
68 fn from(value: SliderTile) -> Self {
69 Self::Slider(value)
70 }
71}
72
73#[cfg(feature = "settings-tiles")]
74impl From<ChoiceTile> for ListSectionItem {
75 fn from(value: ChoiceTile) -> Self {
76 Self::Choice(value)
77 }
78}
79
80enum PreparedItem {
81 Row(Box<ListItem>),
82 #[cfg(feature = "settings-tiles")]
83 Choice(Box<ChoiceTile>),
84}
85
86impl PreparedItem {
87 fn from_section_item(item: ListSectionItem, window: &Window, cx: &App) -> Self {
88 #[cfg(not(feature = "settings-tiles"))]
89 let _ = (window, cx);
90 match item {
91 ListSectionItem::Row(row) => Self::Row(Box::new(row)),
92 #[cfg(feature = "settings-tiles")]
93 ListSectionItem::Switch(tile) => Self::Row(Box::new(tile.into_list_item())),
94 #[cfg(feature = "settings-tiles")]
95 ListSectionItem::Checkbox(tile) => Self::Row(Box::new(tile.into_list_item())),
96 #[cfg(feature = "settings-tiles")]
97 ListSectionItem::Radio(tile) => Self::Row(Box::new(tile.into_list_item())),
98 #[cfg(feature = "settings-tiles")]
99 ListSectionItem::Slider(tile) => Self::Row(Box::new(tile.into_list_item(window, cx))),
100 #[cfg(feature = "settings-tiles")]
101 ListSectionItem::Choice(tile) => Self::Choice(Box::new(tile)),
102 }
103 }
104
105 fn id(&self) -> &ElementId {
106 match self {
107 Self::Row(row) => row.id(),
108 #[cfg(feature = "settings-tiles")]
109 Self::Choice(tile) => tile.id(),
110 }
111 }
112
113 fn custom_focus(&self) -> Option<FocusHandle> {
114 match self {
115 Self::Row(row) => row.custom_focus(),
116 #[cfg(feature = "settings-tiles")]
117 Self::Choice(tile) => tile.custom_focus(),
118 }
119 }
120
121 fn takes_focus(&self, cx: &App) -> bool {
122 match self {
123 Self::Row(row) => row.takes_focus(cx),
124 #[cfg(feature = "settings-tiles")]
125 Self::Choice(tile) => tile.takes_focus(),
126 }
127 }
128
129 fn is_selected(&self) -> bool {
130 match self {
131 Self::Row(row) => row.is_selected(),
132 #[cfg(feature = "settings-tiles")]
133 Self::Choice(_) => false,
134 }
135 }
136
137 fn render(
138 self,
139 resting: CornerShape,
140 selected_shape: CornerShape,
141 focus: Option<(FocusHandle, bool)>,
142 window: &mut Window,
143 cx: &mut App,
144 ) -> AnyElement {
145 match self {
146 Self::Row(row) => (*row).row(resting, selected_shape, focus, window, cx),
147 #[cfg(feature = "settings-tiles")]
148 Self::Choice(tile) => {
149 (*tile).render_in_section(resting, selected_shape, focus, window, cx)
150 }
151 }
152 }
153}
154
155#[derive(IntoElement)]
159pub struct ListSection {
160 id: ElementId,
161 title: Option<SharedString>,
162 variant: ListVariant,
163 items: Vec<ListSectionItem>,
164}
165
166impl ListSection {
167 pub fn new(id: impl Into<ElementId>) -> Self {
169 Self {
170 id: id.into(),
171 title: None,
172 variant: ListVariant::default(),
173 items: Vec::new(),
174 }
175 }
176
177 pub fn segmented(id: impl Into<ElementId>) -> Self {
179 Self::new(id).variant(ListVariant::Segmented)
180 }
181
182 pub fn title(mut self, title: impl Into<SharedString>) -> Self {
184 self.title = Some(title.into());
185 self
186 }
187
188 pub fn variant(mut self, variant: ListVariant) -> Self {
190 self.variant = variant;
191 self
192 }
193
194 pub fn item(mut self, item: impl Into<ListSectionItem>) -> Self {
196 self.items.push(item.into());
197 self
198 }
199
200 pub fn items<I>(mut self, items: impl IntoIterator<Item = I>) -> Self
202 where
203 I: Into<ListSectionItem>,
204 {
205 self.items.extend(items.into_iter().map(Into::into));
206 self
207 }
208}
209
210impl RenderOnce for ListSection {
211 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
212 window.with_element_namespace((self.id.clone(), "material-list-section"), |window| {
213 let direction = current_material_theme(window, cx).layout.direction;
214 let rtl = direction == Direction::Rtl;
215 let prepared: Vec<_> = self
216 .items
217 .into_iter()
218 .map(|item| PreparedItem::from_section_item(item, window, cx))
219 .collect();
220 let last = prepared.len().saturating_sub(1);
221 let focus: Vec<_> = prepared
222 .iter()
223 .map(|item| {
224 item.custom_focus().unwrap_or_else(|| {
225 window
226 .use_keyed_state((item.id().clone(), "focus"), cx, |_, cx| {
227 cx.focus_handle()
228 })
229 .read(cx)
230 .clone()
231 })
232 })
233 .collect();
234 let enabled: Vec<_> = prepared.iter().map(|item| item.takes_focus(cx)).collect();
235 let selected: Vec<_> = prepared.iter().map(PreparedItem::is_selected).collect();
236 let tab_stop = compute_roving_tab_stop(&focus, &enabled, &selected, window);
237 let navigation = focus.clone();
238 let list_label = self.title.clone();
239 let heading = self.title.map(|title| {
240 div()
241 .id((self.id.clone(), "section-heading"))
242 .test_support()
243 .w_full()
244 .flex()
245 .when(rtl, Styled::justify_end)
246 .px(px(16.))
247 .pt(px(8.))
248 .pb(px(6.))
249 .child(
250 Text::new(title)
251 .id((self.id.clone(), "section-title"))
252 .role(TypeRole::LabelMedium)
253 .color(ColorRole::OnSurfaceVariant),
254 )
255 });
256 let list = div()
257 .id((self.id.clone(), "list"))
258 .role(Role::List)
259 .when_some(list_label, |list, label| list.aria_label(label))
260 .test_support()
261 .flex()
262 .flex_col()
263 .w_full()
264 .on_key_down(move |event, window, cx| {
265 handle_vertical_roving_key(&navigation, &enabled, event, direction, window, cx);
268 })
269 .when(self.variant == ListVariant::Segmented, |list| {
270 list.gap(px(2.))
271 })
272 .children(prepared.into_iter().zip(focus).enumerate().map(
273 |(ix, (item, focus))| {
274 let (resting, selected_shape) = segmented_corners(self.variant, ix, last);
275 item.render(
276 resting,
277 selected_shape,
278 Some((focus, Some(ix) == tab_stop)),
279 window,
280 cx,
281 )
282 },
283 ));
284 div()
285 .id(self.id)
286 .test_support()
287 .flex()
288 .flex_col()
289 .w_full()
290 .children(heading)
291 .child(list)
292 })
293 }
294}