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    TextField,
17    Checkbox,
18    Switch,
19    Image,
20    Slider,
21    List,
22    ListItem,
23    ScrollArea,
24}
25
26/// Overrides and additions to the semantics a widget derives on its own.
27#[derive(Clone, Debug, Default, PartialEq)]
28pub struct A11yProps {
29    pub role: Option<Role>,
30    pub label: Option<String>,
31    pub description: Option<String>,
32    /// Current value, as read out: "3 of 5", "50%".
33    pub value: Option<String>,
34    pub labelled_by: Option<NodeId>,
35    /// Remove this node and its subtree from the accessibility tree.
36    pub hidden: bool,
37}
38
39impl A11yProps {
40    pub fn new(role: Role) -> A11yProps {
41        A11yProps { role: Some(role), ..A11yProps::default() }
42    }
43
44    pub fn label(mut self, label: impl Into<String>) -> A11yProps {
45        self.label = Some(label.into());
46        self
47    }
48
49    pub fn description(mut self, description: impl Into<String>) -> A11yProps {
50        self.description = Some(description.into());
51        self
52    }
53
54    pub fn value(mut self, value: impl Into<String>) -> A11yProps {
55        self.value = Some(value.into());
56        self
57    }
58
59    pub fn is_empty(&self) -> bool {
60        *self == A11yProps::default()
61    }
62
63    /// These semantics, with every field `overrides` sets taking its place.
64    pub fn overridden_by(&self, overrides: &A11yProps) -> A11yProps {
65        A11yProps {
66            role: overrides.role.or(self.role),
67            label: overrides.label.clone().or_else(|| self.label.clone()),
68            description: overrides.description.clone().or_else(|| self.description.clone()),
69            value: overrides.value.clone().or_else(|| self.value.clone()),
70            labelled_by: overrides.labelled_by.or(self.labelled_by),
71            hidden: overrides.hidden || self.hidden,
72        }
73    }
74}
75
76/// Something assistive technology (or a test) can ask a control to do.
77#[derive(Clone, Debug, PartialEq)]
78pub enum A11yAction {
79    /// Press / click / toggle, whatever the control's primary action is.
80    Activate,
81    Focus,
82    SetValue(String),
83    Increment,
84    Decrement,
85    ScrollIntoView,
86}
87
88#[derive(Clone, Debug, PartialEq, Eq)]
89pub enum ActionError {
90    UnknownNode,
91    Disabled,
92    Unsupported,
93}
94
95impl std::fmt::Display for ActionError {
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        f.write_str(match self {
98            ActionError::UnknownNode => "the node does not exist",
99            ActionError::Disabled => "the control is disabled",
100            ActionError::Unsupported => "the control does not support this action",
101        })
102    }
103}
104
105/// A node of the computed accessibility tree.
106#[derive(Clone, Debug, PartialEq)]
107pub struct A11yNode {
108    pub id: NodeId,
109    pub role: Role,
110    pub name: Option<String>,
111    pub description: Option<String>,
112    pub value: Option<String>,
113    pub checked: Option<bool>,
114    pub enabled: bool,
115    pub test_id: Option<String>,
116    /// In window coordinates.
117    pub frame: Rect,
118    pub children: Vec<A11yNode>,
119}
120
121impl A11yNode {
122    /// Depth-first iteration over this node and all descendants.
123    pub fn walk(&self) -> Vec<&A11yNode> {
124        let mut out = vec![self];
125        for child in &self.children {
126            out.extend(child.walk());
127        }
128        out
129    }
130}