1use crate::widgets::{WidgetKind, WidgetSpec};
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub enum AccessibleRole {
23 Window,
24 Container,
25 Dialog,
26 Alert,
27 Label,
28 Button,
29 CheckBox,
30 RadioButton,
31 Slider,
32 SpinBox,
33 ProgressBar,
34 Separator,
35 Group,
36 TabList,
37 Image,
38 DatePicker,
39 Link,
40 TextInput,
41 TextArea,
42 ComboBox,
43 List,
44 Tree,
45 Table,
46 SplitPane,
47 Canvas,
48 Toolbar,
49}
50
51impl AccessibleRole {
52 pub fn for_kind(kind: WidgetKind) -> Self {
57 match kind {
58 WidgetKind::Window => Self::Window,
59 WidgetKind::Container => Self::Container,
60 WidgetKind::Label => Self::Label,
61 WidgetKind::Button => Self::Button,
62 #[cfg(feature = "widgets-choice")]
63 WidgetKind::RadioButton => Self::RadioButton,
64 #[cfg(feature = "widgets-range")]
65 WidgetKind::Slider => Self::Slider,
66 #[cfg(feature = "widgets-range")]
67 WidgetKind::SpinBox => Self::SpinBox,
68 #[cfg(feature = "widgets-containers")]
69 WidgetKind::Separator => Self::Separator,
70 #[cfg(feature = "widgets-containers")]
71 WidgetKind::GroupBox => Self::Group,
72 #[cfg(feature = "widgets-containers")]
73 WidgetKind::Tabs => Self::TabList,
74 #[cfg(feature = "widgets-media")]
75 WidgetKind::Image => Self::Image,
76 #[cfg(feature = "widgets-pickers")]
77 WidgetKind::DatePicker => Self::DatePicker,
78 #[cfg(feature = "widgets-text")]
79 WidgetKind::LinkLabel => Self::Link,
80 #[cfg(feature = "widgets-data")]
81 WidgetKind::ListView => Self::List,
82 #[cfg(feature = "widgets-data")]
83 WidgetKind::TreeView => Self::Tree,
84 #[cfg(feature = "widgets-containers")]
85 WidgetKind::SplitPane => Self::SplitPane,
86 #[cfg(feature = "widgets-pickers")]
87 WidgetKind::FileChooserDialog => Self::Dialog,
88 #[cfg(feature = "widgets-containers")]
89 WidgetKind::AlertDialog => Self::Alert,
90 #[cfg(feature = "widgets-text")]
91 WidgetKind::TextInput => Self::TextInput,
92 #[cfg(feature = "widgets-text")]
93 WidgetKind::TextArea => Self::TextArea,
94 #[cfg(feature = "widgets-canvas")]
95 WidgetKind::VulkanCanvas | WidgetKind::MetalCanvas => Self::Canvas,
96 #[cfg(feature = "widgets-choice")]
97 WidgetKind::Checkbox => Self::CheckBox,
98 #[cfg(feature = "widgets-choice")]
99 WidgetKind::ComboBox => Self::ComboBox,
100 #[cfg(feature = "widgets-range")]
101 WidgetKind::ProgressBar => Self::ProgressBar,
102 #[cfg(feature = "widgets-data")]
103 WidgetKind::DataList => Self::List,
104 #[cfg(feature = "widgets-data")]
105 WidgetKind::DataGrid => Self::Table,
106 #[cfg(feature = "widgets-canvas")]
107 WidgetKind::Canvas => Self::Canvas,
108 #[cfg(feature = "widgets-pickers")]
109 WidgetKind::ColorDialog => Self::Dialog,
110 #[cfg(feature = "widgets-pickers")]
111 WidgetKind::FontDialog => Self::Dialog,
112 WidgetKind::Toolbar => Self::Toolbar,
113 #[cfg(feature = "widgets-pickers")]
114 WidgetKind::Calendar => Self::DatePicker,
115 #[cfg(feature = "widgets-text")]
116 WidgetKind::RichText => Self::TextArea,
117 #[cfg(feature = "widgets-choice")]
118 WidgetKind::MultiSelect => Self::List,
119 }
120 }
121
122 pub fn as_str(self) -> &'static str {
125 match self {
126 Self::Window => "window",
127 Self::Container => "container",
128 Self::Dialog => "dialog",
129 Self::Alert => "alert",
130 Self::Label => "label",
131 Self::Button => "button",
132 Self::CheckBox => "checkbox",
133 Self::RadioButton => "radio",
134 Self::Slider => "slider",
135 Self::SpinBox => "spinbox",
136 Self::ProgressBar => "progressbar",
137 Self::Separator => "separator",
138 Self::Group => "group",
139 Self::TabList => "tablist",
140 Self::Image => "image",
141 Self::DatePicker => "datepicker",
142 Self::Link => "link",
143 Self::TextInput => "textinput",
144 Self::TextArea => "textarea",
145 Self::ComboBox => "combobox",
146 Self::List => "list",
147 Self::Tree => "tree",
148 Self::Table => "table",
149 Self::SplitPane => "splitpane",
150 Self::Canvas => "canvas",
151 Self::Toolbar => "toolbar",
152 }
153 }
154
155 pub fn default_actions(self) -> &'static [AccessibleAction] {
157 use AccessibleAction::*;
158 const PRESS: [AccessibleAction; 1] = [Press];
159 const TOGGLE_PRESS: [AccessibleAction; 2] = [Toggle, Press];
160 const STEP: [AccessibleAction; 2] = [Increment, Decrement];
161 const OPEN: [AccessibleAction; 1] = [Open];
162 const SELECT: [AccessibleAction; 1] = [Select];
163 const TREE: [AccessibleAction; 3] = [Select, Expand, Collapse];
164 const EMPTY: [AccessibleAction; 0] = [];
165
166 match self {
167 Self::Button | Self::Link => &PRESS,
168 Self::CheckBox => &TOGGLE_PRESS,
169 Self::RadioButton => &PRESS,
170 Self::Slider | Self::SpinBox => &STEP,
171 Self::ComboBox => &OPEN,
172 Self::List | Self::Table => &SELECT,
173 Self::Tree => &TREE,
174 _ => &EMPTY,
175 }
176 }
177}
178
179#[derive(Debug, Clone, Copy, PartialEq, Eq)]
181pub enum AccessibleAction {
182 Press,
183 Toggle,
184 Increment,
185 Decrement,
186 Expand,
187 Collapse,
188 Open,
189 Select,
190}
191
192impl AccessibleAction {
193 pub fn as_str(self) -> &'static str {
195 match self {
196 Self::Press => "press",
197 Self::Toggle => "toggle",
198 Self::Increment => "increment",
199 Self::Decrement => "decrement",
200 Self::Expand => "expand",
201 Self::Collapse => "collapse",
202 Self::Open => "open",
203 Self::Select => "select",
204 }
205 }
206}
207
208#[derive(Debug, Clone, PartialEq, Eq)]
210pub struct AccessibilitySpec {
211 pub name: Option<String>,
215 pub description: Option<String>,
218 pub role: AccessibleRole,
220}
221
222impl AccessibilitySpec {
223 pub fn for_kind(kind: WidgetKind) -> Self {
225 Self {
226 name: None,
227 description: None,
228 role: AccessibleRole::for_kind(kind),
229 }
230 }
231}
232
233#[derive(Debug, Clone, Copy, PartialEq, Eq)]
238pub struct AccessibilityState {
239 pub enabled: bool,
242 pub checked: Option<bool>,
245 pub selected: bool,
247}
248
249impl AccessibilityState {
250 pub fn of(spec: &WidgetSpec) -> Self {
252 Self {
253 enabled: spec.enabled.unwrap_or(true),
254 checked: spec.checked,
255 selected: spec.selected_index.is_some() || !spec.selected_indices.is_empty(),
256 }
257 }
258}
259
260#[cfg(test)]
261mod tests {
262 use super::*;
263 use crate::widgets::{abstractable_widget_types, WidgetId};
264
265 #[test]
266 fn every_widget_kind_maps_to_the_expected_role() {
267 let expected = [
268 (WidgetKind::Window, AccessibleRole::Window),
269 (WidgetKind::Container, AccessibleRole::Container),
270 (WidgetKind::Label, AccessibleRole::Label),
271 (WidgetKind::Button, AccessibleRole::Button),
272 #[cfg(feature = "widgets-choice")]
273 (WidgetKind::RadioButton, AccessibleRole::RadioButton),
274 #[cfg(feature = "widgets-range")]
275 (WidgetKind::Slider, AccessibleRole::Slider),
276 #[cfg(feature = "widgets-range")]
277 (WidgetKind::SpinBox, AccessibleRole::SpinBox),
278 #[cfg(feature = "widgets-containers")]
279 (WidgetKind::Separator, AccessibleRole::Separator),
280 #[cfg(feature = "widgets-containers")]
281 (WidgetKind::GroupBox, AccessibleRole::Group),
282 #[cfg(feature = "widgets-containers")]
283 (WidgetKind::Tabs, AccessibleRole::TabList),
284 #[cfg(feature = "widgets-media")]
285 (WidgetKind::Image, AccessibleRole::Image),
286 #[cfg(feature = "widgets-pickers")]
287 (WidgetKind::DatePicker, AccessibleRole::DatePicker),
288 #[cfg(feature = "widgets-text")]
289 (WidgetKind::LinkLabel, AccessibleRole::Link),
290 #[cfg(feature = "widgets-data")]
291 (WidgetKind::ListView, AccessibleRole::List),
292 #[cfg(feature = "widgets-data")]
293 (WidgetKind::TreeView, AccessibleRole::Tree),
294 #[cfg(feature = "widgets-containers")]
295 (WidgetKind::SplitPane, AccessibleRole::SplitPane),
296 #[cfg(feature = "widgets-pickers")]
297 (WidgetKind::FileChooserDialog, AccessibleRole::Dialog),
298 #[cfg(feature = "widgets-containers")]
299 (WidgetKind::AlertDialog, AccessibleRole::Alert),
300 #[cfg(feature = "widgets-text")]
301 (WidgetKind::TextInput, AccessibleRole::TextInput),
302 #[cfg(feature = "widgets-text")]
303 (WidgetKind::TextArea, AccessibleRole::TextArea),
304 #[cfg(feature = "widgets-canvas")]
305 (WidgetKind::VulkanCanvas, AccessibleRole::Canvas),
306 #[cfg(feature = "widgets-choice")]
307 (WidgetKind::Checkbox, AccessibleRole::CheckBox),
308 #[cfg(feature = "widgets-choice")]
309 (WidgetKind::ComboBox, AccessibleRole::ComboBox),
310 #[cfg(feature = "widgets-range")]
311 (WidgetKind::ProgressBar, AccessibleRole::ProgressBar),
312 #[cfg(feature = "widgets-data")]
313 (WidgetKind::DataList, AccessibleRole::List),
314 #[cfg(feature = "widgets-data")]
315 (WidgetKind::DataGrid, AccessibleRole::Table),
316 #[cfg(feature = "widgets-canvas")]
317 (WidgetKind::Canvas, AccessibleRole::Canvas),
318 #[cfg(feature = "widgets-pickers")]
319 (WidgetKind::ColorDialog, AccessibleRole::Dialog),
320 #[cfg(feature = "widgets-pickers")]
321 (WidgetKind::FontDialog, AccessibleRole::Dialog),
322 (WidgetKind::Toolbar, AccessibleRole::Toolbar),
323 #[cfg(feature = "widgets-pickers")]
324 (WidgetKind::Calendar, AccessibleRole::DatePicker),
325 #[cfg(feature = "widgets-choice")]
326 (WidgetKind::MultiSelect, AccessibleRole::List),
327 ];
328
329 assert_eq!(expected.len(), abstractable_widget_types().len());
331 for (kind, role) in expected {
332 assert_eq!(AccessibleRole::for_kind(kind), role, "role for {kind:?}");
333 }
334 }
335
336 #[test]
337 fn role_identifiers_are_stable_and_unique() {
338 let roles = [
339 AccessibleRole::Window,
340 AccessibleRole::Container,
341 AccessibleRole::Dialog,
342 AccessibleRole::Alert,
343 AccessibleRole::Label,
344 AccessibleRole::Button,
345 AccessibleRole::CheckBox,
346 AccessibleRole::RadioButton,
347 AccessibleRole::Slider,
348 AccessibleRole::SpinBox,
349 AccessibleRole::ProgressBar,
350 AccessibleRole::Separator,
351 AccessibleRole::Group,
352 AccessibleRole::TabList,
353 AccessibleRole::Image,
354 AccessibleRole::DatePicker,
355 AccessibleRole::Link,
356 AccessibleRole::TextInput,
357 AccessibleRole::TextArea,
358 AccessibleRole::ComboBox,
359 AccessibleRole::List,
360 AccessibleRole::Tree,
361 AccessibleRole::Table,
362 AccessibleRole::SplitPane,
363 AccessibleRole::Canvas,
364 AccessibleRole::Toolbar,
365 ];
366
367 let mut seen = std::collections::HashSet::new();
368 for role in roles {
369 let id = role.as_str();
370 assert!(!id.is_empty(), "empty role id for {role:?}");
371 assert!(seen.insert(id), "duplicate role id {id}");
372 }
373 }
374
375 #[test]
376 fn default_actions_follow_role_semantics() {
377 assert_eq!(
378 AccessibleRole::Button.default_actions(),
379 [AccessibleAction::Press]
380 );
381 assert_eq!(
382 AccessibleRole::CheckBox.default_actions(),
383 [AccessibleAction::Toggle, AccessibleAction::Press]
384 );
385 assert_eq!(
386 AccessibleRole::Slider.default_actions(),
387 [AccessibleAction::Increment, AccessibleAction::Decrement]
388 );
389 assert_eq!(
390 AccessibleRole::Tree.default_actions(),
391 [
392 AccessibleAction::Select,
393 AccessibleAction::Expand,
394 AccessibleAction::Collapse
395 ]
396 );
397 assert!(AccessibleRole::Label.default_actions().is_empty());
398 assert!(AccessibleRole::Window.default_actions().is_empty());
399 }
400
401 #[test]
402 fn state_projection_reflects_widget_spec() {
403 let mut spec = WidgetSpec::new(WidgetId(1), WidgetKind::Button);
404 let state = AccessibilityState::of(&spec);
405 assert!(state.enabled);
406 assert_eq!(state.checked, None);
407 assert!(!state.selected);
408
409 spec.enabled = Some(false);
410 spec.checked = Some(true);
411 spec.selected_indices = vec![2];
412 let state = AccessibilityState::of(&spec);
413 assert!(!state.enabled);
414 assert_eq!(state.checked, Some(true));
415 assert!(state.selected);
416 }
417
418 #[test]
419 #[cfg(feature = "widgets-data")]
420 fn accessibility_spec_defaults_track_the_widget_kind() {
421 let spec = AccessibilitySpec::for_kind(WidgetKind::DataGrid);
422 assert_eq!(spec.role, AccessibleRole::Table);
423 assert_eq!(spec.name, None);
424 assert_eq!(spec.description, None);
425 }
426}