gpui_table_runtime/
text.rs1use gpui_kit::{
3 A11ySubtreeBuilder, App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId,
4 IntoElement as _, LayoutId, Pixels, Role, SharedString, Window, accesskit,
5};
6
7pub fn text_cell(value: impl Into<SharedString>) -> gpui_kit::AnyElement {
11 let value = value.into();
12 CellText {
13 inner: value.clone().into_element(),
14 value,
15 }
16 .into_any_element()
17}
18
19struct CellText<E> {
20 inner: E,
21 value: SharedString,
22}
23impl<E> CellText<E> {
24 fn text_run(&self) -> accesskit::Node {
25 let mut node = accesskit::Node::new(Role::TextRun);
26 node.set_value(self.value.to_string());
27 node.set_character_lengths(
28 self.value
29 .chars()
30 .map(|c| c.len_utf8() as u8)
31 .collect::<Vec<_>>(),
32 );
33 node
34 }
35}
36impl<E: Element> Element for CellText<E> {
37 type RequestLayoutState = E::RequestLayoutState;
38 type PrepaintState = E::PrepaintState;
39
40 fn id(&self) -> Option<ElementId> {
41 Some("table-cell-text".into())
42 }
43
44 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
45 self.inner.source_location()
46 }
47
48 fn request_layout(
49 &mut self,
50 id: Option<&GlobalElementId>,
51 inspector: Option<&InspectorElementId>,
52 window: &mut Window,
53 cx: &mut App,
54 ) -> (LayoutId, Self::RequestLayoutState) {
55 self.inner.request_layout(id, inspector, window, cx)
56 }
57
58 fn prepaint(
59 &mut self,
60 id: Option<&GlobalElementId>,
61 inspector: Option<&InspectorElementId>,
62 bounds: Bounds<Pixels>,
63 layout: &mut Self::RequestLayoutState,
64 window: &mut Window,
65 cx: &mut App,
66 ) -> Self::PrepaintState {
67 self.inner
68 .prepaint(id, inspector, bounds, layout, window, cx)
69 }
70
71 fn paint(
72 &mut self,
73 id: Option<&GlobalElementId>,
74 inspector: Option<&InspectorElementId>,
75 bounds: Bounds<Pixels>,
76 layout: &mut Self::RequestLayoutState,
77 prepaint: &mut Self::PrepaintState,
78 window: &mut Window,
79 cx: &mut App,
80 ) {
81 self.inner
82 .paint(id, inspector, bounds, layout, prepaint, window, cx)
83 }
84
85 fn a11y_role(&self) -> Option<Role> {
86 Some(Role::Label)
87 }
88
89 fn write_a11y_info(&self, node: &mut accesskit::Node) {
90 node.set_label(self.value.to_string());
91 }
92
93 fn a11y_synthetic_children(
94 &mut self,
95 _: &mut Self::PrepaintState,
96 builder: &mut A11ySubtreeBuilder,
97 ) {
98 let id = builder.synthetic_node_id(0);
99 let mut run = self.text_run();
100 if let Some(bounds) = builder.parent_node().bounds() {
101 run.set_bounds(bounds);
102 }
103 builder.push_child(id, run);
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110 #[test]
111 fn cell_text_exposes_the_displayed_unicode_without_focus_or_actions() {
112 let value: SharedString = "Parity recipient — Montréal 🦀".into();
113 let cell = CellText {
114 inner: value.clone().into_element(),
115 value: value.clone(),
116 };
117 assert_eq!(cell.a11y_role(), Some(Role::Label));
118 let mut node = accesskit::Node::new(cell.a11y_role().unwrap());
119 cell.write_a11y_info(&mut node);
120 assert_eq!(node.label(), Some(value.as_ref()));
121 assert!(!node.supports_action(accesskit::Action::Focus));
122 assert!(!node.supports_action(accesskit::Action::Click));
123 assert_eq!(cell.id(), Some("table-cell-text".into()));
124 let run = cell.text_run();
125 assert_eq!(run.value(), Some(value.as_ref()));
126 assert_eq!(
127 run.character_lengths()
128 .iter()
129 .map(|n| usize::from(*n))
130 .sum::<usize>(),
131 value.len()
132 );
133 assert_eq!(run.character_lengths().len(), value.chars().count());
134 }
135}