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 /// A button showing a colour that picks another (a `ColorWell`). Its
57 /// value is the colour, as `#rrggbb`.
58 ColorWell,
59 List,
60 ListItem,
61 /// Rows of cells under column headers (a `Table`): its headers, then
62 /// its rows.
63 Table,
64 /// A column's header in a `Table`, named by its title. Its value is
65 /// the table's sort order while the table is sorted by it.
66 ColumnHeader,
67 /// A row of a `Table`, named by its cells' text, selected while it's
68 /// selected: its cells.
69 Row,
70 /// A cell of a `Table`'s row, named by its text.
71 Cell,
72 /// Rows that nest, opened and closed (a `Tree`): its items that show,
73 /// in order, each with its level.
74 Tree,
75 /// A row of a `Tree`, named by its text, with its `level`, `expanded`
76 /// while it's open (`None` if it never opens), and selected while it's
77 /// selected.
78 TreeItem,
79 ScrollArea,
80 /// Pages with a tab each, one shown (a `Tabs`): its tabs, then the
81 /// shown page's content.
82 TabGroup,
83 /// A tab of a `TabGroup`, named by its title, selected while its page
84 /// shows; or of a `TabList`, selected while it's chosen.
85 Tab,
86 /// Tabs without pages (`DocumentTabs`): its tabs, then its new-tab
87 /// button, if it has one.
88 TabList,
89}
90
91/// Overrides and additions to the semantics a widget derives on its own.
92#[derive(Clone, Debug, Default, PartialEq)]
93pub struct A11yProps {
94 pub role: Option<Role>,
95 pub label: Option<String>,
96 pub description: Option<String>,
97 /// Current value, as read out: "3 of 5", "50%".
98 pub value: Option<String>,
99 pub labelled_by: Option<NodeId>,
100 /// Remove this node and its subtree from the accessibility tree.
101 pub hidden: bool,
102}
103
104impl A11yProps {
105 pub fn new(role: Role) -> A11yProps {
106 A11yProps { role: Some(role), ..A11yProps::default() }
107 }
108
109 pub fn label(mut self, label: impl Into<String>) -> A11yProps {
110 self.label = Some(label.into());
111 self
112 }
113
114 pub fn description(mut self, description: impl Into<String>) -> A11yProps {
115 self.description = Some(description.into());
116 self
117 }
118
119 pub fn value(mut self, value: impl Into<String>) -> A11yProps {
120 self.value = Some(value.into());
121 self
122 }
123
124 pub fn is_empty(&self) -> bool {
125 *self == A11yProps::default()
126 }
127
128 /// These semantics, with every field `overrides` sets taking its place.
129 pub fn overridden_by(&self, overrides: &A11yProps) -> A11yProps {
130 A11yProps {
131 role: overrides.role.or(self.role),
132 label: overrides.label.clone().or_else(|| self.label.clone()),
133 description: overrides.description.clone().or_else(|| self.description.clone()),
134 value: overrides.value.clone().or_else(|| self.value.clone()),
135 labelled_by: overrides.labelled_by.or(self.labelled_by),
136 hidden: overrides.hidden || self.hidden,
137 }
138 }
139}
140
141/// Something assistive technology (or a test) can ask a control to do.
142#[derive(Clone, Debug, PartialEq)]
143pub enum A11yAction {
144 /// Press / click / toggle, whatever the control's primary action is;
145 /// `DocumentTabs`' new-tab button.
146 Activate,
147 Focus,
148 /// Replace a text field's text, choose the `Select` or `RadioGroup`
149 /// option, or the tab, with this text, or move a `Slider` or `NumberInput` to this number,
150 /// or set a `ColorWell` to this colour (`#rrggbb`).
151 SetValue(String),
152 /// Step an adjustable control (a slider, a spin button) up or down.
153 Increment,
154 Decrement,
155 ScrollIntoView,
156 /// Open a `Tree`'s item, as assistive technology does: its children
157 /// show. Only items that open.
158 Expand,
159 /// Close a `Tree`'s item: its children's rows go.
160 Collapse,
161 /// Select a `List` row or a `Table` row (on a row or one of its cells;
162 /// in a single-selection list, instead of the selected one; in a
163 /// multiple-selection list, as the only one).
164 Select,
165 /// Press the header of a `Table`'s column, by its index, as assistive
166 /// technology presses one: the table sorts by that column, or the
167 /// other way round, as the platform does. Only sortable columns.
168 PressHeader(usize),
169 /// Close the tab of `DocumentTabs` at this index, as assistive
170 /// technology presses its close button: the app is asked. Only tabs
171 /// that close.
172 CloseTab(usize),
173 /// Move the tab of `DocumentTabs` at `from` to `to`, as the user drags
174 /// it there (assistive technology can't drag; the platform's keys move
175 /// tabs one place at a time). Only tabs that move.
176 MoveTab {
177 from: usize,
178 to: usize,
179 },
180 /// Choose the item of the node's context menu with this id, as
181 /// assistive technology does once it has shown the menu. The menu
182 /// itself never opens.
183 ContextMenuItem(u32),
184 /// Choose the item of a `MenuButton`'s or `SplitButton`'s menu with
185 /// this id, as assistive technology does once the button has shown it.
186 /// The menu itself never opens. (`Activate` would open a menu button's,
187 /// so a menu button doesn't take it; a split button's presses it.)
188 MenuItem(u32),
189 /// Choose a `SearchInput`'s suggestion at this index, as the user picks
190 /// one from the list under the field.
191 ChooseSuggestion(usize),
192}
193
194#[derive(Clone, Debug, PartialEq, Eq)]
195pub enum ActionError {
196 UnknownNode,
197 Disabled,
198 /// A read-only text field can't be edited.
199 ReadOnly,
200 Unsupported,
201}
202
203impl std::fmt::Display for ActionError {
204 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
205 f.write_str(match self {
206 ActionError::UnknownNode => "the node does not exist",
207 ActionError::Disabled => "the control is disabled",
208 ActionError::ReadOnly => "the control is read-only",
209 ActionError::Unsupported => "the control does not support this action",
210 })
211 }
212}
213
214/// A node of the computed accessibility tree.
215#[derive(Clone, Debug, PartialEq)]
216pub struct A11yNode {
217 pub id: NodeId,
218 pub role: Role,
219 pub name: Option<String>,
220 pub description: Option<String>,
221 pub value: Option<String>,
222 pub checked: Option<bool>,
223 /// Checkboxes: in the mixed state, whatever `checked` says.
224 pub mixed: bool,
225 /// Text fields and areas: shown, but not editable.
226 pub read_only: bool,
227 /// Text fields: the text is hidden, and not in `value`.
228 pub password: bool,
229 /// Text fields: the text has an error (`Prop::Error`), which is the
230 /// description.
231 pub invalid: bool,
232 /// List rows: whether the row is selected.
233 pub selected: Option<bool>,
234 /// Tree items that open: whether the item is open.
235 pub expanded: Option<bool>,
236 /// Tree items: how deep, 1 at the top.
237 pub level: Option<usize>,
238 /// Tree items: their children are being read (`Prop::TreeLoading`).
239 pub busy: bool,
240 pub enabled: bool,
241 pub test_id: Option<String>,
242 /// In window coordinates.
243 pub frame: Rect,
244 pub children: Vec<A11yNode>,
245}
246
247impl A11yNode {
248 /// Depth-first iteration over this node and all descendants.
249 pub fn walk(&self) -> Vec<&A11yNode> {
250 // One list, filled from a stack: a list per subtree, appended to
251 // its parent's, copied each node once per level above it.
252 let mut out = Vec::new();
253 let mut stack = vec![self];
254 while let Some(node) = stack.pop() {
255 out.push(node);
256 stack.extend(node.children.iter().rev());
257 }
258 out
259 }
260}