1use crate::NodeId;
5use crate::geometry::Rect;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum Role {
9 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#[derive(Clone, Debug, Default, PartialEq)]
28pub struct A11yProps {
29 pub role: Option<Role>,
30 pub label: Option<String>,
31 pub description: Option<String>,
32 pub value: Option<String>,
34 pub labelled_by: Option<NodeId>,
35 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 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#[derive(Clone, Debug, PartialEq)]
78pub enum A11yAction {
79 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#[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 pub frame: Rect,
118 pub children: Vec<A11yNode>,
119}
120
121impl A11yNode {
122 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}