1use crate::TestSupportExt as _;
2use gpui::{
3 AnyElement, App, Div, ElementId, InteractiveElement, Interactivity, IntoElement, ParentElement,
4 RenderOnce, Role, SharedString, Stateful, StatefulInteractiveElement, StyleRefinement, Styled,
5 Window, div, prelude::FluentBuilder as _,
6};
7
8macro_rules! table_part {
9 ($name:ident, $role:expr, $docs:literal) => {
10 #[doc = $docs]
11 #[derive(IntoElement)]
12 pub struct $name {
13 base: crate::ObservedElement<Stateful<Div>>,
14 style: StyleRefinement,
15 children: Vec<AnyElement>,
16 }
17
18 impl $name {
19 #[doc = concat!("Create ", $docs)]
20 pub fn new(id: impl Into<ElementId>) -> Self {
21 Self {
22 base: div().id(id).test_support(),
23 style: StyleRefinement::default(),
24 children: Vec::new(),
25 }
26 }
27 }
28
29 impl Styled for $name {
30 fn style(&mut self) -> &mut StyleRefinement {
31 &mut self.style
32 }
33 }
34
35 impl ParentElement for $name {
36 fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) {
37 self.children.extend(children);
38 }
39 }
40
41 impl InteractiveElement for $name {
42 fn interactivity(&mut self) -> &mut Interactivity {
43 self.base.interactivity()
44 }
45
46 fn track_focus(mut self, handle: &gpui::FocusHandle) -> Self {
47 self.base = self.base.track_focus(handle);
48 self
49 }
50 }
51
52 impl StatefulInteractiveElement for $name {}
53
54 impl RenderOnce for $name {
55 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
56 self.base
57 .role($role)
58 .children(self.children)
59 .refine_style(&self.style)
60 }
61 }
62 };
63}
64
65#[derive(IntoElement)]
67pub struct Table {
68 base: crate::ObservedElement<Stateful<Div>>,
69 style: StyleRefinement,
70 children: Vec<AnyElement>,
71 row_count: Option<usize>,
72 column_count: Option<usize>,
73 accessibility_label: Option<SharedString>,
74}
75
76impl Table {
77 pub fn new(id: impl Into<ElementId>) -> Self {
79 Self {
80 base: div().id(id).test_support(),
81 style: StyleRefinement::default(),
82 children: Vec::new(),
83 row_count: None,
84 column_count: None,
85 accessibility_label: None,
86 }
87 }
88
89 pub fn row_count(mut self, count: usize) -> Self {
93 self.row_count = Some(count);
94 self
95 }
96
97 pub fn column_count(mut self, count: usize) -> Self {
100 self.column_count = Some(count);
101 self
102 }
103
104 pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
106 self.accessibility_label = Some(label.into());
107 self
108 }
109}
110
111impl Styled for Table {
112 fn style(&mut self) -> &mut StyleRefinement {
113 &mut self.style
114 }
115}
116
117impl ParentElement for Table {
118 fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) {
119 self.children.extend(children);
120 }
121}
122
123impl InteractiveElement for Table {
124 fn interactivity(&mut self) -> &mut Interactivity {
125 self.base.interactivity()
126 }
127
128 fn track_focus(mut self, handle: &gpui::FocusHandle) -> Self {
129 self.base = self.base.track_focus(handle);
130 self
131 }
132}
133
134impl StatefulInteractiveElement for Table {}
135
136impl RenderOnce for Table {
137 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
138 self.base
139 .role(Role::Table)
140 .when_some(self.accessibility_label, |this, label| {
141 this.aria_label(label)
142 })
143 .when_some(self.row_count, |this, count| this.aria_row_count(count))
144 .when_some(self.column_count, |this, count| {
145 this.aria_column_count(count)
146 })
147 .children(self.children)
148 .refine_style(&self.style)
149 }
150}
151table_part!(
152 TableHeader,
153 Role::RowGroup,
154 "An unstyled table header group."
155);
156table_part!(TableBody, Role::RowGroup, "An unstyled table body group.");
157
158#[derive(IntoElement)]
160pub struct TableRow {
161 base: crate::ObservedElement<Stateful<Div>>,
162 style: StyleRefinement,
163 row_index: usize,
164 children: Vec<AnyElement>,
165}
166
167impl TableRow {
168 pub fn new(id: impl Into<ElementId>, row_index: usize) -> Self {
170 Self {
171 base: div().id(id).test_support(),
172 style: StyleRefinement::default(),
173 row_index,
174 children: Vec::new(),
175 }
176 }
177}
178
179impl Styled for TableRow {
180 fn style(&mut self) -> &mut StyleRefinement {
181 &mut self.style
182 }
183}
184
185impl ParentElement for TableRow {
186 fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) {
187 self.children.extend(children);
188 }
189}
190
191impl InteractiveElement for TableRow {
192 fn interactivity(&mut self) -> &mut Interactivity {
193 self.base.interactivity()
194 }
195
196 fn track_focus(mut self, handle: &gpui::FocusHandle) -> Self {
197 self.base = self.base.track_focus(handle);
198 self
199 }
200}
201
202impl StatefulInteractiveElement for TableRow {}
203
204impl RenderOnce for TableRow {
205 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
206 self.base
207 .role(Role::Row)
208 .aria_row_index(self.row_index)
209 .children(self.children)
210 .refine_style(&self.style)
211 }
212}
213
214macro_rules! table_cell {
215 ($name:ident, $role:expr, $docs:literal) => {
216 #[doc = $docs]
217 #[derive(IntoElement)]
218 pub struct $name {
219 base: crate::ObservedElement<Stateful<Div>>,
220 style: StyleRefinement,
221 column_index: usize,
222 children: Vec<AnyElement>,
223 }
224
225 impl $name {
226 #[doc = concat!("Create ", $docs, " with a one-based accessibility index.")]
227 pub fn new(id: impl Into<ElementId>, column_index: usize) -> Self {
228 Self {
229 base: div().id(id).test_support(),
230 style: StyleRefinement::default(),
231 column_index,
232 children: Vec::new(),
233 }
234 }
235 }
236
237 impl Styled for $name {
238 fn style(&mut self) -> &mut StyleRefinement {
239 &mut self.style
240 }
241 }
242
243 impl ParentElement for $name {
244 fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) {
245 self.children.extend(children);
246 }
247 }
248
249 impl InteractiveElement for $name {
250 fn interactivity(&mut self) -> &mut Interactivity {
251 self.base.interactivity()
252 }
253
254 fn track_focus(mut self, handle: &gpui::FocusHandle) -> Self {
255 self.base = self.base.track_focus(handle);
256 self
257 }
258 }
259
260 impl StatefulInteractiveElement for $name {}
261
262 impl RenderOnce for $name {
263 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
264 self.base
265 .role($role)
266 .aria_column_index(self.column_index)
267 .children(self.children)
268 .refine_style(&self.style)
269 }
270 }
271 };
272}
273
274table_cell!(
275 TableHead,
276 Role::ColumnHeader,
277 "An unstyled table column header."
278);
279table_cell!(TableCell, Role::Cell, "An unstyled table data cell.");
280
281#[derive(IntoElement)]
283pub struct TableCaption {
284 base: crate::ObservedElement<Stateful<Div>>,
285 style: StyleRefinement,
286 children: Vec<AnyElement>,
287}
288
289impl TableCaption {
290 pub fn new(id: impl Into<ElementId>) -> Self {
292 Self {
293 base: div().id(id).test_support(),
294 style: StyleRefinement::default(),
295 children: Vec::new(),
296 }
297 }
298}
299
300impl Styled for TableCaption {
301 fn style(&mut self) -> &mut StyleRefinement {
302 &mut self.style
303 }
304}
305
306impl ParentElement for TableCaption {
307 fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) {
308 self.children.extend(children);
309 }
310}
311
312impl InteractiveElement for TableCaption {
313 fn interactivity(&mut self) -> &mut Interactivity {
314 self.base.interactivity()
315 }
316
317 fn track_focus(mut self, handle: &gpui::FocusHandle) -> Self {
318 self.base = self.base.track_focus(handle);
319 self
320 }
321}
322
323impl StatefulInteractiveElement for TableCaption {}
324
325impl RenderOnce for TableCaption {
326 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
327 self.base.children(self.children).refine_style(&self.style)
328 }
329}
330
331#[cfg(test)]
332mod tests {
333 use super::*;
334 use gpui::{Context, Element as _, Modifiers, Render, TestAppContext, accesskit, point, px};
335 use std::{cell::Cell, rc::Rc};
336
337 #[gpui::test]
338 fn table_projects_its_accessible_name(cx: &mut TestAppContext) {
339 let window = cx.add_empty_window();
340 window.update(|window, cx| {
341 let mut node = accesskit::Node::new(Role::Table);
342 Table::new("positions")
343 .accessibility_label("Open positions")
344 .render(window, cx)
345 .into_element()
346 .write_a11y_info(&mut node);
347
348 assert_eq!(node.label(), Some("Open positions"));
349 });
350 }
351
352 #[gpui::test]
353 fn row_and_cells_project_accessibility_indices(cx: &mut TestAppContext) {
354 let window = cx.add_empty_window();
355 window.update(|window, cx| {
356 let mut row = accesskit::Node::new(Role::Row);
357 TableRow::new("row", 3)
358 .render(window, cx)
359 .into_element()
360 .write_a11y_info(&mut row);
361 assert_eq!(row.row_index(), Some(3));
362
363 let mut head = accesskit::Node::new(Role::GenericContainer);
364 TableHead::new("head", 2)
365 .render(window, cx)
366 .into_element()
367 .write_a11y_info(&mut head);
368 assert_eq!(head.column_index(), Some(2));
369
370 let mut cell = accesskit::Node::new(Role::Cell);
371 TableCell::new("cell", 4)
372 .render(window, cx)
373 .into_element()
374 .write_a11y_info(&mut cell);
375 assert_eq!(cell.column_index(), Some(4));
376 });
377 }
378
379 struct TableHarness {
380 clicks: Rc<Cell<usize>>,
381 }
382
383 impl Render for TableHarness {
384 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
385 let clicks = self.clicks.clone();
386 Table::new("table")
387 .debug_selector(|| "base-table".into())
388 .w(px(120.))
389 .h(px(60.))
390 .on_click(move |_, _, _| clicks.set(clicks.get() + 1))
391 .child(
392 TableBody::new("body").child(
393 TableRow::new("row", 1)
394 .child(TableCell::new("cell", 1).child(
395 div().debug_selector(|| "table-child".into()).size(px(20.)),
396 )),
397 ),
398 )
399 }
400 }
401
402 #[gpui::test]
403 fn table_forwards_children_instance_style_and_pointer_interaction(cx: &mut TestAppContext) {
404 let clicks = Rc::new(Cell::new(0));
405 let (_, cx) = cx.add_window_view({
406 let clicks = clicks.clone();
407 move |_, _| TableHarness { clicks }
408 });
409 cx.update(|window, cx| window.draw(cx).clear(cx));
410
411 let table = cx.debug_bounds("base-table").expect("table is rendered");
412 assert_eq!(table.size.width, px(120.));
413 assert_eq!(table.size.height, px(60.));
414 let child = cx.debug_bounds("table-child").expect("child is rendered");
415 assert_eq!(child.size.width, px(20.));
416 assert_eq!(child.size.height, px(20.));
417
418 cx.simulate_click(point(px(10.), px(10.)), Modifiers::default());
419 cx.simulate_click(point(px(100.), px(50.)), Modifiers::default());
420 assert_eq!(clicks.get(), 2);
421 }
422}