Skip to main content

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.
75    Tab,
76}
77
78/// Overrides and additions to the semantics a widget derives on its own.
79#[derive(Clone, Debug, Default, PartialEq)]
80pub struct A11yProps {
81    pub role: Option<Role>,
82    pub label: Option<String>,
83    pub description: Option<String>,
84    /// Current value, as read out: "3 of 5", "50%".
85    pub value: Option<String>,
86    pub labelled_by: Option<NodeId>,
87    /// Remove this node and its subtree from the accessibility tree.
88    pub hidden: bool,
89}
90
91impl A11yProps {
92    pub fn new(role: Role) -> A11yProps {
93        A11yProps { role: Some(role), ..A11yProps::default() }
94    }
95
96    pub fn label(mut self, label: impl Into<String>) -> A11yProps {
97        self.label = Some(label.into());
98        self
99    }
100
101    pub fn description(mut self, description: impl Into<String>) -> A11yProps {
102        self.description = Some(description.into());
103        self
104    }
105
106    pub fn value(mut self, value: impl Into<String>) -> A11yProps {
107        self.value = Some(value.into());
108        self
109    }
110
111    pub fn is_empty(&self) -> bool {
112        *self == A11yProps::default()
113    }
114
115    /// These semantics, with every field `overrides` sets taking its place.
116    pub fn overridden_by(&self, overrides: &A11yProps) -> A11yProps {
117        A11yProps {
118            role: overrides.role.or(self.role),
119            label: overrides.label.clone().or_else(|| self.label.clone()),
120            description: overrides.description.clone().or_else(|| self.description.clone()),
121            value: overrides.value.clone().or_else(|| self.value.clone()),
122            labelled_by: overrides.labelled_by.or(self.labelled_by),
123            hidden: overrides.hidden || self.hidden,
124        }
125    }
126}
127
128/// Something assistive technology (or a test) can ask a control to do.
129#[derive(Clone, Debug, PartialEq)]
130pub enum A11yAction {
131    /// Press / click / toggle, whatever the control's primary action is.
132    Activate,
133    Focus,
134    /// Replace a text field's text, choose the `Select` or `RadioGroup`
135    /// option with this text, or move a `Slider` or `NumberInput` to this number.
136    SetValue(String),
137    /// Step an adjustable control (a slider, a spin button) up or down.
138    Increment,
139    Decrement,
140    ScrollIntoView,
141    /// Select a `List` row or a `Table` row (on a row or one of its cells;
142    /// in a single-selection list, instead of the selected one; in a
143    /// multiple-selection list, as the only one).
144    Select,
145    /// Press the header of a `Table`'s column, by its index, as assistive
146    /// technology presses one: the table sorts by that column, or the
147    /// other way round, as the platform does. Only sortable columns.
148    PressHeader(usize),
149    /// Choose the item of the node's context menu with this id, as
150    /// assistive technology does once it has shown the menu. The menu
151    /// itself never opens.
152    ContextMenuItem(u32),
153    /// Choose the item of a `MenuButton`'s menu with this id, as
154    /// assistive technology does once the button has shown it. The menu
155    /// itself never opens. (`Activate` would open it, so a menu button
156    /// doesn't take it.)
157    MenuItem(u32),
158}
159
160#[derive(Clone, Debug, PartialEq, Eq)]
161pub enum ActionError {
162    UnknownNode,
163    Disabled,
164    /// A read-only text field can't be edited.
165    ReadOnly,
166    Unsupported,
167}
168
169impl std::fmt::Display for ActionError {
170    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171        f.write_str(match self {
172            ActionError::UnknownNode => "the node does not exist",
173            ActionError::Disabled => "the control is disabled",
174            ActionError::ReadOnly => "the control is read-only",
175            ActionError::Unsupported => "the control does not support this action",
176        })
177    }
178}
179
180/// A node of the computed accessibility tree.
181#[derive(Clone, Debug, PartialEq)]
182pub struct A11yNode {
183    pub id: NodeId,
184    pub role: Role,
185    pub name: Option<String>,
186    pub description: Option<String>,
187    pub value: Option<String>,
188    pub checked: Option<bool>,
189    /// Checkboxes: in the mixed state, whatever `checked` says.
190    pub mixed: bool,
191    /// Text fields and areas: shown, but not editable.
192    pub read_only: bool,
193    /// Text fields: the text is hidden, and not in `value`.
194    pub password: bool,
195    /// List rows: whether the row is selected.
196    pub selected: Option<bool>,
197    pub enabled: bool,
198    pub test_id: Option<String>,
199    /// In window coordinates.
200    pub frame: Rect,
201    pub children: Vec<A11yNode>,
202}
203
204impl A11yNode {
205    /// Depth-first iteration over this node and all descendants.
206    pub fn walk(&self) -> Vec<&A11yNode> {
207        // One list, filled from a stack: a list per subtree, appended to
208        // its parent's, copied each node once per level above it.
209        let mut out = Vec::new();
210        let mut stack = vec![self];
211        while let Some(node) = stack.pop() {
212            out.push(node);
213            stack.extend(node.children.iter().rev());
214        }
215        out
216    }
217}