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