1use super::*;
2
3impl A11yRole {
5 #[must_use]
16 pub const fn native_role_name(&self, platform: PlatformKind) -> &'static str {
17 match platform {
18 PlatformKind::MacOs => match *self {
19 Self::Window => "NSAccessibilityWindowRole",
20 Self::Region | Self::Navigation | Self::Group => ROLE_MAC_GROUP,
21 Self::Heading | Self::Text => ROLE_MAC_STATIC_TEXT,
22 Self::Button => "NSAccessibilityButtonRole",
23 Self::TextInput => "NSAccessibilityTextFieldRole",
24 Self::Checkbox | Self::Switch => "NSAccessibilityCheckBoxRole",
25 Self::Slider => "NSAccessibilitySliderRole",
26 Self::Tab => "NSAccessibilityRadioButtonRole",
27 Self::TabList => "NSAccessibilityTabGroupRole",
28 Self::List => "NSAccessibilityListRole",
29 Self::ListItem => "NSAccessibilityRowRole",
30 Self::Dialog | Self::Alert => "NSAccessibilitySheetRole",
31 Self::ProgressBar => "NSAccessibilityProgressIndicatorRole",
32 Self::Image | Self::Canvas => "NSAccessibilityImageRole",
33 Self::ScrollView => "NSAccessibilityScrollAreaRole",
34 Self::Link => "NSAccessibilityLinkRole",
35 Self::Menu => "NSAccessibilityMenuRole",
36 Self::MenuItem => "NSAccessibilityMenuItemRole",
37 },
38 PlatformKind::Ios | PlatformKind::IpadOs => match *self {
39 Self::Button | Self::Tab | Self::MenuItem => "UIAccessibilityTraitButton",
40 Self::Heading => "UIAccessibilityTraitHeader",
41 Self::Link => "UIAccessibilityTraitLink",
42 Self::Image | Self::Canvas => "UIAccessibilityTraitImage",
43 Self::Slider | Self::ProgressBar => "UIAccessibilityTraitAdjustable",
44 Self::Text | Self::Alert => "UIAccessibilityTraitStaticText",
45 _ => "UIAccessibilityTraitNone",
46 },
47 PlatformKind::Windows => match *self {
48 Self::Window | Self::Dialog => "UIA_WindowControlTypeId",
49 Self::Button => "UIA_ButtonControlTypeId",
50 Self::TextInput => "UIA_EditControlTypeId",
51 Self::Checkbox | Self::Switch => "UIA_CheckBoxControlTypeId",
52 Self::Slider => "UIA_SliderControlTypeId",
53 Self::Tab => "UIA_TabItemControlTypeId",
54 Self::TabList => "UIA_TabControlTypeId",
55 Self::List => "UIA_ListControlTypeId",
56 Self::ListItem => "UIA_ListItemControlTypeId",
57 Self::ProgressBar => "UIA_ProgressBarControlTypeId",
58 Self::Image | Self::Canvas => "UIA_ImageControlTypeId",
59 Self::Link => "UIA_HyperlinkControlTypeId",
60 Self::Menu => "UIA_MenuControlTypeId",
61 Self::MenuItem => "UIA_MenuItemControlTypeId",
62 Self::Text | Self::Heading | Self::Alert => "UIA_TextControlTypeId",
63 Self::ScrollView => "UIA_PaneControlTypeId",
64 Self::Group | Self::Region | Self::Navigation => "UIA_GroupControlTypeId",
65 },
66 PlatformKind::Linux => match *self {
67 Self::Window => "ATK_ROLE_FRAME",
68 Self::Dialog => "ATK_ROLE_DIALOG",
69 Self::Alert => "ATK_ROLE_ALERT",
70 Self::Button => "ATK_ROLE_PUSH_BUTTON",
71 Self::TextInput => "ATK_ROLE_ENTRY",
72 Self::Checkbox => "ATK_ROLE_CHECK_BOX",
73 Self::Switch => "ATK_ROLE_TOGGLE_BUTTON",
74 Self::Slider => "ATK_ROLE_SLIDER",
75 Self::Tab => "ATK_ROLE_PAGE_TAB",
76 Self::TabList => "ATK_ROLE_PAGE_TAB_LIST",
77 Self::List => "ATK_ROLE_LIST",
78 Self::ListItem => "ATK_ROLE_LIST_ITEM",
79 Self::ProgressBar => "ATK_ROLE_PROGRESS_BAR",
80 Self::Image | Self::Canvas => "ATK_ROLE_IMAGE",
81 Self::ScrollView => "ATK_ROLE_SCROLL_PANE",
82 Self::Link => "ATK_ROLE_LINK",
83 Self::Menu => "ATK_ROLE_MENU",
84 Self::MenuItem => "ATK_ROLE_MENU_ITEM",
85 Self::Heading => "ATK_ROLE_HEADING",
86 Self::Text => "ATK_ROLE_LABEL",
87 Self::Group | Self::Region | Self::Navigation => "ATK_ROLE_PANEL",
88 },
89 PlatformKind::Android | PlatformKind::Wearable => match *self {
90 Self::Button | Self::MenuItem => "android.widget.Button",
91 Self::TextInput => "android.widget.EditText",
92 Self::Checkbox => "android.widget.CheckBox",
93 Self::Switch => "android.widget.Switch",
94 Self::Slider => "android.widget.SeekBar",
95 Self::ProgressBar => "android.widget.ProgressBar",
96 Self::Image | Self::Canvas => "android.widget.ImageView",
97 Self::ScrollView => "android.widget.ScrollView",
98 Self::List => "android.widget.ListView",
99 Self::Text | Self::Heading | Self::Alert | Self::Link => "android.widget.TextView",
100 _ => "android.view.View",
101 },
102 PlatformKind::HarmonyOs => match *self {
103 Self::Button | Self::MenuItem => ROLE_OHOS_BUTTON,
104 Self::TextInput => ROLE_OHOS_TEXT_INPUT,
105 Self::Checkbox => ROLE_OHOS_CHECKBOX,
106 Self::Switch => ROLE_OHOS_TOGGLE,
107 Self::Slider => ROLE_OHOS_SLIDER,
108 Self::ProgressBar => ROLE_OHOS_PROGRESS,
109 Self::Image | Self::Canvas => ROLE_OHOS_IMAGE,
110 Self::ScrollView => ROLE_OHOS_SCROLL,
111 Self::List => ROLE_OHOS_LIST,
112 Self::ListItem => ROLE_OHOS_LIST_ITEM,
113 Self::Dialog | Self::Alert => ROLE_OHOS_DIALOG,
114 Self::Tab | Self::TabList => ROLE_OHOS_TABS,
115 Self::Menu => ROLE_OHOS_MENU,
116 Self::Text | Self::Heading | Self::Link => ROLE_OHOS_TEXT,
117 _ => ROLE_OHOS_COMPONENT,
118 },
119 PlatformKind::Terminal | PlatformKind::TerminalCli => match *self {
120 Self::Window | Self::Dialog => ROLE_TUI_WINDOW,
121 Self::Button | Self::MenuItem | Self::Link | Self::Tab => ROLE_TUI_BUTTON,
122 Self::TextInput | Self::Checkbox | Self::Switch | Self::Slider => ROLE_TUI_INPUT,
123 Self::List | Self::ListItem | Self::ScrollView | Self::TabList | Self::Menu => {
124 ROLE_TUI_LIST
125 }
126 Self::Text | Self::Heading | Self::Alert | Self::ProgressBar => ROLE_TUI_TEXT,
127 _ => ROLE_TUI_REGION,
128 },
129 PlatformKind::Web
130 | PlatformKind::MiniApp
131 | PlatformKind::QuickApp
132 | PlatformKind::Headless => match *self {
133 Self::Window => "application",
134 Self::Region | Self::ScrollView => ROLE_ARIA_REGION,
135 Self::Navigation => "navigation",
136 Self::Heading => "heading",
137 Self::Text => "paragraph",
138 Self::Button => "button",
139 Self::TextInput => "textbox",
140 Self::Checkbox => "checkbox",
141 Self::Switch => "switch",
142 Self::Slider => "slider",
143 Self::Tab => "tab",
144 Self::TabList => "tablist",
145 Self::List => "list",
146 Self::ListItem => "listitem",
147 Self::Dialog => "dialog",
148 Self::Alert => "alert",
149 Self::ProgressBar => "progressbar",
150 Self::Image | Self::Canvas => "img",
151 Self::Link => "link",
152 Self::Menu => "menu",
153 Self::MenuItem => "menuitem",
154 Self::Group => "group",
155 },
156 }
157 }
158}
159
160impl A11yProps {
162 #[must_use]
172 pub fn checked_state(&self) -> Option<bool> {
173 let Self { checked, .. } = self;
174 *checked
175 }
176}
177
178impl A11yTree {
180 #[must_use]
190 pub fn from_scene(tree: &SceneTree) -> Self {
191 let root: NodeId = tree.get_root();
192 let focused_id: Option<NodeId> = tree.get_focus().focused_node();
193 let initial_cap: usize = (tree.live_node_count() / 2).max(1);
194 let mut nodes: Vec<A11yNode> = Vec::with_capacity(initial_cap);
195 Self::collect_node(tree, root, root, focused_id, &mut nodes);
196 Self::new(root, nodes, focused_id)
197 }
198
199 #[must_use]
209 #[inline(always)]
210 fn is_node_exposed(node: &SceneNode) -> bool {
211 if node.style_ref().layout_ref().get_display() == Display::None {
212 return false;
213 }
214 !node.a11y_props_ref().is_some_and(A11yProps::get_hidden)
215 }
216
217 fn collect_node(
227 tree: &SceneTree,
228 node_id: NodeId,
229 root: NodeId,
230 focused_id: Option<NodeId>,
231 out: &mut Vec<A11yNode>,
232 ) {
233 let Some(node) = tree.get_node(node_id) else {
234 return;
235 };
236 if node.style_ref().layout_ref().get_display() == Display::None {
237 return;
238 }
239 let explicit_ref: Option<&A11yProps> = node.a11y_props_ref();
240 if explicit_ref.is_some_and(A11yProps::get_hidden) {
241 return;
242 }
243 let props: A11yProps = match explicit_ref {
244 Some(explicit) => explicit.clone(),
245 None => {
246 let mut inferred: A11yProps = match node.get_kind() {
247 NodeKind::Text(blend_factor) => {
248 A11yProps::new(A11yRole::Text, blend_factor.clone())
249 }
250 NodeKind::Input(state) => {
251 let mut point_val: A11yProps =
252 A11yProps::new(A11yRole::TextInput, state.get_placeholder().clone());
253 point_val
254 .set_value(state.get_value().clone())
255 .set_disabled(state.get_disabled());
256 point_val
257 }
258 NodeKind::Image(_) => A11yProps::new(A11yRole::Image, String::new()),
259 NodeKind::Canvas(_) => A11yProps::new(A11yRole::Canvas, String::new()),
260 NodeKind::ScrollView => A11yProps::new(A11yRole::ScrollView, String::new()),
261 NodeKind::Portal => A11yProps::new(A11yRole::Dialog, String::new()),
262 NodeKind::View | NodeKind::Fragment => {
263 let role: A11yRole = if node_id == root {
264 A11yRole::Window
265 } else if node
266 .get_handlers()
267 .handler_for(&UiEventKind::Click(Point::default()))
268 .is_some()
269 {
270 A11yRole::Button
271 } else {
272 A11yRole::Group
273 };
274 A11yProps::new(role, String::new())
275 }
276 };
277 if node.get_interaction().get_focused() {
278 inferred.set_live_region(A11yLiveRegion::Polite);
279 }
280 inferred
281 }
282 };
283 let child_count: usize = tree.child_count(node_id);
284 let visible_children: Vec<NodeId> = if child_count == 0 {
285 Vec::new()
286 } else {
287 let mut vis: Vec<NodeId> = Vec::with_capacity(child_count);
288 for cid in tree.children_iter(node_id) {
289 if tree.get_node(cid).is_some_and(Self::is_node_exposed) {
290 vis.push(cid);
291 }
292 }
293 vis
294 };
295 out.push(A11yNode::new(
296 node_id,
297 props,
298 node.get_layout_box().get_bounds(),
299 focused_id == Some(node_id),
300 visible_children,
301 ));
302 for child_id in tree.children_iter(node_id) {
303 Self::collect_node(tree, child_id, root, focused_id, out);
304 }
305 }
306
307 #[must_use]
318 pub fn find(&self, id: NodeId) -> Option<&A11yNode> {
319 self.get_nodes()
320 .iter()
321 .find(|node: &&A11yNode| node.get_id() == id)
322 }
323
324 #[must_use]
334 pub fn focused_node(&self) -> Option<NodeId> {
335 let Self { focused_id, .. } = self;
336 *focused_id
337 }
338
339 #[must_use]
350 pub fn format_summary(&self, platform: PlatformKind) -> String {
351 let mut out: String = String::with_capacity(self.get_nodes().len().saturating_mul(24));
352 for node in self.get_nodes().iter() {
353 let role: &str = node.get_props().get_role().native_role_name(platform);
354 let label: &str = node.get_props().get_label().as_str();
355 out.push_str(role);
356 if !label.is_empty() {
357 out.push_str(": ");
358 out.push_str(label);
359 }
360 out.push('\n');
361 }
362 out
363 }
364}