mitsuami_core/a11y.rs
1//! Accessibility model. Every node carries semantics from day one. The test
2//! driver uses the same roles, names and actions as assistive technology.
3
4use crate::NodeId;
5use crate::geometry::Rect;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum Role {
9 /// Not exposed; children are exposed in its place.
10 None,
11 Window,
12 Group,
13 StaticText,
14 Heading,
15 Button,
16 /// A button that stays pressed until it's pressed again (a
17 /// `ToggleButton`): AppKit's toggle, GTK's toggle button, ARIA's
18 /// pressed button, Qt's and UIA's checkable button. `checked` while
19 /// it's pressed.
20 ToggleButton,
21 /// A button that opens a menu of actions (a `MenuButton`): AppKit's
22 /// menu button, Qt's `ButtonMenu`, a button with a menu pop-up on GTK
23 /// and in ARIA, UIA's expand-collapse button.
24 MenuButton,
25 TextField,
26 /// A field for search text (a `SearchInput`): AppKit's search field,
27 /// GTK's search box, ARIA's searchbox, a text field on Qt and UIA (the
28 /// edit in an auto-suggest box). Its value is the text.
29 SearchField,
30 /// A field for text over many lines (a `TextArea`): AppKit's text
31 /// area, a multi-line text field elsewhere (GTK's and Qt's multi-line
32 /// state, UIA's multi-line edit). Its value is the text.
33 TextArea,
34 Checkbox,
35 Switch,
36 /// Radio buttons, one of which at most is checked (a `RadioGroup`):
37 /// its radio buttons.
38 RadioGroup,
39 /// An option of a `RadioGroup`, named by its text, checked while it's
40 /// the one chosen.
41 RadioButton,
42 /// A button that pops up a list of options to choose one from (a
43 /// `Select`). Its value is the chosen option.
44 ComboBox,
45 /// How far along a task is. Its value is the percentage, unless it
46 /// isn't known.
47 ProgressBar,
48 Image,
49 /// A line between groups of content (a `Separator`), which assistive
50 /// technology announces or skips as the platform does.
51 Separator,
52 Slider,
53 /// A field for a number with buttons that step it (a `NumberInput`).
54 /// Its value is the number.
55 SpinButton,
56 List,
57 ListItem,
58 /// Rows of cells under column headers (a `Table`): its headers, then
59 /// its rows.
60 Table,
61 /// A column's header in a `Table`, named by its title. Its value is
62 /// the table's sort order while the table is sorted by it.
63 ColumnHeader,
64 /// A row of a `Table`, named by its cells' text, selected while it's
65 /// selected: its cells.
66 Row,
67 /// A cell of a `Table`'s row, named by its text.
68 Cell,
69 ScrollArea,
70 /// Pages with a tab each, one shown (a `Tabs`): its tabs, then the
71 /// shown page's content.
72 TabGroup,
73 /// A tab of a `TabGroup`, named by its title, selected while its page
74 /// shows; or of a `TabList`, selected while it's chosen.
75 Tab,
76 /// Tabs without pages (`DocumentTabs`): its tabs, then its new-tab
77 /// button, if it has one.
78 TabList,
79}
80
81/// Overrides and additions to the semantics a widget derives on its own.
82#[derive(Clone, Debug, Default, PartialEq)]
83pub struct A11yProps {
84 pub role: Option<Role>,
85 pub label: Option<String>,
86 pub description: Option<String>,
87 /// Current value, as read out: "3 of 5", "50%".
88 pub value: Option<String>,
89 pub labelled_by: Option<NodeId>,
90 /// Remove this node and its subtree from the accessibility tree.
91 pub hidden: bool,
92}
93
94impl A11yProps {
95 pub fn new(role: Role) -> A11yProps {
96 A11yProps { role: Some(role), ..A11yProps::default() }
97 }
98
99 pub fn label(mut self, label: impl Into<String>) -> A11yProps {
100 self.label = Some(label.into());
101 self
102 }
103
104 pub fn description(mut self, description: impl Into<String>) -> A11yProps {
105 self.description = Some(description.into());
106 self
107 }
108
109 pub fn value(mut self, value: impl Into<String>) -> A11yProps {
110 self.value = Some(value.into());
111 self
112 }
113
114 pub fn is_empty(&self) -> bool {
115 *self == A11yProps::default()
116 }
117
118 /// These semantics, with every field `overrides` sets taking its place.
119 pub fn overridden_by(&self, overrides: &A11yProps) -> A11yProps {
120 A11yProps {
121 role: overrides.role.or(self.role),
122 label: overrides.label.clone().or_else(|| self.label.clone()),
123 description: overrides.description.clone().or_else(|| self.description.clone()),
124 value: overrides.value.clone().or_else(|| self.value.clone()),
125 labelled_by: overrides.labelled_by.or(self.labelled_by),
126 hidden: overrides.hidden || self.hidden,
127 }
128 }
129}
130
131/// Something assistive technology (or a test) can ask a control to do.
132#[derive(Clone, Debug, PartialEq)]
133pub enum A11yAction {
134 /// Press / click / toggle, whatever the control's primary action is;
135 /// `DocumentTabs`' new-tab button.
136 Activate,
137 Focus,
138 /// Replace a text field's text, choose the `Select` or `RadioGroup`
139 /// option, or the tab, with this text, or move a `Slider` or `NumberInput` to this number.
140 SetValue(String),
141 /// Step an adjustable control (a slider, a spin button) up or down.
142 Increment,
143 Decrement,
144 ScrollIntoView,
145 /// Select a `List` row or a `Table` row (on a row or one of its cells;
146 /// in a single-selection list, instead of the selected one; in a
147 /// multiple-selection list, as the only one).
148 Select,
149 /// Press the header of a `Table`'s column, by its index, as assistive
150 /// technology presses one: the table sorts by that column, or the
151 /// other way round, as the platform does. Only sortable columns.
152 PressHeader(usize),
153 /// Close the tab of `DocumentTabs` at this index, as assistive
154 /// technology presses its close button: the app is asked. Only tabs
155 /// that close.
156 CloseTab(usize),
157 /// Move the tab of `DocumentTabs` at `from` to `to`, as the user drags
158 /// it there (assistive technology can't drag; the platform's keys move
159 /// tabs one place at a time). Only tabs that move.
160 MoveTab {
161 from: usize,
162 to: usize,
163 },
164 /// Choose the item of the node's context menu with this id, as
165 /// assistive technology does once it has shown the menu. The menu
166 /// itself never opens.
167 ContextMenuItem(u32),
168 /// Choose the item of a `MenuButton`'s menu with this id, as
169 /// assistive technology does once the button has shown it. The menu
170 /// itself never opens. (`Activate` would open it, so a menu button
171 /// doesn't take it.)
172 MenuItem(u32),
173}
174
175#[derive(Clone, Debug, PartialEq, Eq)]
176pub enum ActionError {
177 UnknownNode,
178 Disabled,
179 /// A read-only text field can't be edited.
180 ReadOnly,
181 Unsupported,
182}
183
184impl std::fmt::Display for ActionError {
185 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
186 f.write_str(match self {
187 ActionError::UnknownNode => "the node does not exist",
188 ActionError::Disabled => "the control is disabled",
189 ActionError::ReadOnly => "the control is read-only",
190 ActionError::Unsupported => "the control does not support this action",
191 })
192 }
193}
194
195/// A node of the computed accessibility tree.
196#[derive(Clone, Debug, PartialEq)]
197pub struct A11yNode {
198 pub id: NodeId,
199 pub role: Role,
200 pub name: Option<String>,
201 pub description: Option<String>,
202 pub value: Option<String>,
203 pub checked: Option<bool>,
204 /// Checkboxes: in the mixed state, whatever `checked` says.
205 pub mixed: bool,
206 /// Text fields and areas: shown, but not editable.
207 pub read_only: bool,
208 /// Text fields: the text is hidden, and not in `value`.
209 pub password: bool,
210 /// List rows: whether the row is selected.
211 pub selected: Option<bool>,
212 pub enabled: bool,
213 pub test_id: Option<String>,
214 /// In window coordinates.
215 pub frame: Rect,
216 pub children: Vec<A11yNode>,
217}
218
219impl A11yNode {
220 /// Depth-first iteration over this node and all descendants.
221 pub fn walk(&self) -> Vec<&A11yNode> {
222 // One list, filled from a stack: a list per subtree, appended to
223 // its parent's, copied each node once per level above it.
224 let mut out = Vec::new();
225 let mut stack = vec![self];
226 while let Some(node) = stack.pop() {
227 out.push(node);
228 stack.extend(node.children.iter().rev());
229 }
230 out
231 }
232}