1use crate::*;
102
103pub(crate) mod debug;
104
105use crate::{App, Bounds, FocusId, Pixels, SharedString, Window};
106use accesskit::{Action, NodeId, TreeUpdate};
107use collections::{FxHashMap, FxHashSet};
108use smallvec::SmallVec;
109use std::hash::{Hash, Hasher};
110use std::sync::{
111 Arc,
112 atomic::{AtomicBool, Ordering},
113};
114
115pub(crate) const ROOT_NODE_ID: NodeId = NodeId(0);
117
118pub(crate) type A11yActionListener =
120 Box<dyn FnMut(Option<&accesskit::ActionData>, &mut Window, &mut App) + 'static>;
121
122pub(crate) struct A11y {
127 force_disabled: bool,
131 active_flag: Arc<AtomicBool>,
136 active_this_frame: bool,
148 pub(crate) nodes: A11yNodeBuilder,
149 pub(crate) focus_ids: FxHashMap<NodeId, FocusId>,
150 pub(crate) node_bounds: FxHashMap<NodeId, Bounds<Pixels>>,
151 pub(crate) action_listeners: FxHashMap<NodeId, Vec<(Action, A11yActionListener)>>,
152 window_title: Option<SharedString>,
155 last_focus_without_node: Option<FocusId>,
158 debug: debug::A11yDebug,
161 #[cfg(debug_assertions)]
163 pub(crate) view_type_names: FxHashMap<EntityId, &'static str>,
164}
165
166impl A11y {
167 pub(crate) fn new(
168 active_flag: Arc<AtomicBool>,
169 force_disabled: bool,
170 window_title: Option<SharedString>,
171 ) -> Self {
172 Self {
173 force_disabled,
174 active_flag,
175 active_this_frame: false,
176 nodes: A11yNodeBuilder::new(),
177 focus_ids: FxHashMap::default(),
178 node_bounds: FxHashMap::default(),
179 action_listeners: FxHashMap::default(),
180 window_title,
181 last_focus_without_node: None,
182 debug: debug::A11yDebug::default(),
183 #[cfg(debug_assertions)]
184 view_type_names: FxHashMap::default(),
185 }
186 }
187
188 pub(crate) fn note_focus_without_node(&mut self, focus_id: FocusId, reason: &str) {
194 if self.last_focus_without_node != Some(focus_id) {
195 self.last_focus_without_node = Some(focus_id);
196 log::info!(
197 "a11y: focused element ({focus_id:?}) has no accessibility node \
198 ({reason}); assistive technology will announce the whole window \
199 instead. Give it both an `.id(...)` and a `.role(...)` to expose it."
200 );
201 }
202 }
203
204 pub(crate) fn set_window_title(&mut self, title: impl Into<SharedString>) {
205 self.window_title = Some(title.into());
206 }
207
208 pub(crate) fn sync_active_flag(&mut self) {
213 self.active_this_frame = !self.force_disabled && self.active_flag.load(Ordering::SeqCst);
214 }
215
216 pub(crate) fn is_active(&self) -> bool {
217 self.active_this_frame
218 }
219
220 pub(crate) fn set_focusable(&mut self, node_id: NodeId, focus_id: FocusId) {
221 self.focus_ids.insert(node_id, focus_id);
222 }
223
224 pub(crate) fn set_focus(&mut self, node_id: NodeId) {
229 if !self.focus_ids.contains_key(&node_id) {
231 if cfg!(debug_assertions) {
232 panic!("set_focus called for a node that was not registered with set_focusable");
233 } else {
234 log::warn!(
235 "a11y: set_focus called for a node that was not registered with \
236 set_focusable ({node_id:?})"
237 );
238 }
239 }
240 if self.nodes.has_node(node_id) {
241 self.last_focus_without_node = None;
244 self.nodes.set_focus(node_id);
245 } else {
246 if let Some(focus_id) = self.focus_ids.get(&node_id).copied() {
249 self.note_focus_without_node(focus_id, "it has an id but no role");
250 }
251 }
252 }
253
254 pub(crate) fn set_active_descendant(&mut self, node_id: NodeId) {
255 if self.nodes.node_is_focused(node_id) {
258 if cfg!(debug_assertions) {
259 panic!("set_active_descendant called on the focused node");
260 } else {
261 log::warn!("a11y: set_active_descendant called on the focused node ({node_id:?})");
262 }
263 return;
264 }
265 if self.nodes.has_node(node_id) && self.nodes.focus_is_ancestor_of_current() {
266 self.nodes.set_active_descendant(node_id);
267 }
268 }
269
270 pub(crate) fn begin_frame(&mut self) {
272 self.focus_ids.clear();
273 self.node_bounds.clear();
274 self.action_listeners.clear();
275 self.nodes.begin_frame(self.window_title.as_ref());
276 }
277
278 pub(crate) fn end_frame(&mut self, frame: debug::FrameDebugInfo) -> TreeUpdate {
280 let update = self.nodes.finalize();
281 self.debug.capture(
282 &update,
283 self.nodes.focus,
284 self.nodes.active_descendant,
285 self.window_title.as_ref(),
286 frame,
287 );
288 #[cfg(debug_assertions)]
289 self.debug.capture_node_info(&self.nodes.node_info);
290 update
291 }
292
293 pub(crate) fn debug_tree_json(&self) -> Option<String> {
294 self.debug.to_json()
295 }
296}
297
298pub struct A11ySubtreeBuilder<'a> {
301 parent_id: NodeId,
302 nodes: &'a mut A11yNodeBuilder,
303 #[cfg(debug_assertions)]
306 creator: debug::NodeCreator,
307}
308
309impl<'a> A11ySubtreeBuilder<'a> {
310 pub(crate) fn new(parent_id: NodeId, nodes: &'a mut A11yNodeBuilder) -> Self {
311 Self {
312 parent_id,
313 nodes,
314 #[cfg(debug_assertions)]
315 creator: debug::NodeCreator::default(),
316 }
317 }
318
319 #[cfg(debug_assertions)]
320 pub(crate) fn with_creator(mut self, creator: debug::NodeCreator) -> Self {
321 self.creator = creator;
322 self
323 }
324
325 pub fn synthetic_node_id(&self, key: impl Hash) -> NodeId {
332 let mut hasher = std::hash::DefaultHasher::default();
333 self.parent_id.0.hash(&mut hasher);
334 key.hash(&mut hasher);
335 NodeId(hasher.finish())
336 }
337
338 pub fn push_child(&mut self, id: NodeId, node: accesskit::Node) -> bool {
343 let pushed = self.nodes.push_leaf(id, node);
344 #[cfg(debug_assertions)]
345 if pushed {
346 self.nodes.record_node_info(
347 id,
348 debug::NodeDebugInfo {
349 synthetic: true,
350 view: self.creator.view,
351 element_id: self.creator.element_id.clone(),
352 source_location: self.creator.source_location,
353 },
354 );
355 }
356 pushed
357 }
358
359 pub fn parent_node(&mut self) -> &mut accesskit::Node {
361 self.nodes
362 .current_node_mut()
363 .expect("A11ySubtreeBuilder exists only while its element's node is on the stack")
364 }
365}
366
367pub(crate) struct A11yNodeBuilder {
368 ids_stack: SmallVec<[NodeId; 16]>,
369 nodes_stack: SmallVec<[accesskit::Node; 16]>,
370 all_nodes: Vec<(NodeId, accesskit::Node)>,
373 seen_ids: FxHashSet<NodeId>,
374 focus: Option<NodeId>,
377 active_descendant: Option<NodeId>,
382 #[cfg(debug_assertions)]
383 node_info: FxHashMap<NodeId, debug::NodeDebugInfo>,
384}
385
386impl A11yNodeBuilder {
387 fn new() -> Self {
388 Self {
389 ids_stack: SmallVec::new(),
390 nodes_stack: SmallVec::new(),
391 all_nodes: Vec::new(),
392 seen_ids: FxHashSet::default(),
393 focus: None,
394 active_descendant: None,
395 #[cfg(debug_assertions)]
396 node_info: FxHashMap::default(),
397 }
398 }
399
400 #[cfg(debug_assertions)]
402 pub(crate) fn record_node_info(&mut self, id: NodeId, info: debug::NodeDebugInfo) {
403 self.node_info.insert(id, info);
404 }
405
406 #[must_use]
407 fn can_push(&mut self, id: NodeId) -> bool {
408 debug_assert!(!self.ids_stack.is_empty(), "node pushed before push_root");
409
410 if !self.seen_ids.insert(id) {
411 debug_assert!(
412 false,
413 "Duplicate a11y node id: {id:?}. In a release build, this node would be silently discarded from the a11y tree."
414 );
415 return false;
416 }
417
418 true
419 }
420
421 pub(crate) fn push(&mut self, id: NodeId, node: accesskit::Node) -> bool {
426 if !self.can_push(id) {
427 return false;
428 }
429
430 if let Some(parent) = self.nodes_stack.last_mut() {
431 parent.push_child(id);
432 }
433 self.ids_stack.push(id);
434 self.nodes_stack.push(node);
435 true
436 }
437
438 pub(crate) fn push_leaf(&mut self, id: NodeId, node: accesskit::Node) -> bool {
444 if !self.can_push(id) {
445 return false;
446 }
447
448 if let Some(parent) = self.nodes_stack.last_mut() {
449 parent.push_child(id);
450 }
451 self.all_nodes.push((id, node));
452 true
453 }
454
455 pub(crate) fn current_node_mut(&mut self) -> Option<&mut accesskit::Node> {
456 self.nodes_stack.last_mut()
457 }
458
459 pub(crate) fn pop(&mut self) {
462 debug_assert!(self.ids_stack.len() > 1, "pop would remove the root node");
463
464 if let (Some(id), Some(node)) = (self.ids_stack.pop(), self.nodes_stack.pop()) {
465 self.all_nodes.push((id, node));
466 }
467 }
468
469 fn begin_frame(&mut self, window_title: Option<&SharedString>) {
471 self.all_nodes.clear();
472 self.ids_stack.clear();
473 self.nodes_stack.clear();
474 self.seen_ids.clear();
475 #[cfg(debug_assertions)]
476 self.node_info.clear();
477 let mut root_node = accesskit::Node::new(accesskit::Role::Window);
478 if let Some(title) = window_title {
479 root_node.set_label(title.to_string());
480 }
481
482 self.ids_stack.push(ROOT_NODE_ID);
483 self.nodes_stack.push(root_node);
484 self.focus = None;
485 self.active_descendant = None;
486 }
487
488 pub(crate) fn has_node(&self, id: NodeId) -> bool {
490 id == ROOT_NODE_ID || self.seen_ids.contains(&id)
491 }
492
493 pub(crate) fn node_is_focused(&self, id: NodeId) -> bool {
495 self.focus == Some(id)
496 }
497
498 pub(crate) fn focus_is_ancestor_of_current(&self) -> bool {
499 let Some(focus) = self.focus else {
500 return false;
501 };
502
503 let ancestor_count = self.ids_stack.len().saturating_sub(1);
506 self.ids_stack[..ancestor_count].contains(&focus)
507 }
508
509 pub(crate) fn set_active_descendant(&mut self, id: NodeId) {
510 if self
511 .active_descendant
512 .is_some_and(|existing| existing != id)
513 {
514 if cfg!(debug_assertions) {
515 panic!("active descendant claimed by multiple nodes in one frame");
516 } else {
517 log::warn!(
518 "a11y: multiple nodes claimed the active descendant this frame; \
519 using last-wins ({id:?})"
520 );
521 }
522 }
523 self.active_descendant = Some(id);
524 }
525
526 pub(crate) fn set_focus(&mut self, id: NodeId) {
527 if self.focus.is_some() {
528 if cfg!(debug_assertions) {
529 panic!("set_focus called more than once in a single frame");
530 } else {
531 log::warn!(
532 "a11y: set_focus called more than once in a single frame; \
533 using last-wins ({id:?})"
534 );
535 }
536 }
537 self.focus = Some(id);
538 }
539
540 fn finalize(&mut self) -> TreeUpdate {
541 debug_assert_eq!(self.ids_stack.len(), 1);
543 debug_assert_eq!(self.ids_stack[0], ROOT_NODE_ID);
544
545 if self.ids_stack.len() != 1 {
546 log::error!(
547 "a11y: Stack imbalance at end of frame: expected 1 (root), got {}. \
548 Some elements may have pushed without popping.",
549 self.ids_stack.len()
550 );
551 }
552
553 while !self.ids_stack.is_empty() {
555 if let (Some(id), Some(node)) = (self.ids_stack.pop(), self.nodes_stack.pop()) {
556 self.all_nodes.push((id, node));
557 }
558 }
559
560 let focus = match self.active_descendant {
561 Some(id) if self.has_node(id) => id,
562 Some(id) => {
563 if cfg!(debug_assertions) {
564 panic!("active_descendant set to {id:?}, which is not in the tree");
565 } else {
566 log::warn!("active_descendant set to {id:?}, which is not in the tree");
567 self.focus.unwrap_or(ROOT_NODE_ID)
568 }
569 }
570
571 _ => self.focus.unwrap_or(ROOT_NODE_ID),
572 };
573
574 let nodes = std::mem::take(&mut self.all_nodes);
575 let update = TreeUpdate {
576 nodes,
577 tree: Some(accesskit::Tree::new(ROOT_NODE_ID)),
578 tree_id: accesskit::TreeId::ROOT,
579 focus,
580 };
581
582 Self::repair_tree_update(update)
583 }
584
585 fn repair_tree_update(mut update: TreeUpdate) -> TreeUpdate {
588 let node_ids: FxHashSet<NodeId> = update.nodes.iter().map(|(id, _)| *id).collect();
589
590 if !node_ids.contains(&update.focus) {
592 log::error!(
593 "a11y: Focused node {:?} is not in the tree ({} nodes). \
594 Falling back to root. This is a bug in the a11y tree builder.",
595 update.focus,
596 update.nodes.len()
597 );
598 update.focus = ROOT_NODE_ID;
599 }
600
601 for (id, node) in &mut update.nodes {
603 let has_invalid_child = node
604 .children()
605 .iter()
606 .any(|child_id| !node_ids.contains(child_id));
607 if has_invalid_child {
608 let children = node.children();
609 let invalid_count = children
610 .iter()
611 .filter(|child_id| !node_ids.contains(child_id))
612 .count();
613 log::error!(
614 "a11y: Node {:?} references {} children not present in the tree. \
615 Stripping invalid child references.",
616 id,
617 invalid_count
618 );
619 let valid: Vec<NodeId> = children
620 .iter()
621 .copied()
622 .filter(|child_id| node_ids.contains(child_id))
623 .collect();
624 node.set_children(valid);
625 }
626 }
627
628 update
629 }
630}
631
632#[cfg(test)]
633mod tests {
634 use super::{A11y, A11yNodeBuilder, ROOT_NODE_ID};
637 use crate::FocusId;
638 use accesskit::{NodeId, Role};
639 use std::sync::{Arc, atomic::AtomicBool};
640
641 fn test_node() -> accesskit::Node {
642 accesskit::Node::new(Role::GenericContainer)
643 }
644
645 fn new_builder() -> A11yNodeBuilder {
646 let mut builder = A11yNodeBuilder::new();
647 builder.begin_frame(None);
648 builder
649 }
650
651 fn new_a11y() -> A11y {
652 let mut a11y = A11y::new(Arc::new(AtomicBool::new(true)), false, None);
653 a11y.begin_frame();
654 a11y
655 }
656
657 #[test]
658 fn active_descendant_honored_when_container_focused() {
659 let mut builder = new_builder();
660 let container = NodeId(1);
661 let item = NodeId(2);
662
663 assert!(builder.push(container, test_node()));
664 builder.set_focus(container);
665 assert!(builder.push(item, test_node()));
666
667 assert!(builder.focus_is_ancestor_of_current());
670 builder.set_active_descendant(item);
671
672 builder.pop(); builder.pop(); let update = builder.finalize();
675 assert_eq!(update.focus, item);
676 }
677
678 #[test]
679 fn active_descendant_honored_for_deep_descendant() {
680 let mut builder = new_builder();
681 let container = NodeId(1);
682 let group = NodeId(2);
683 let item = NodeId(3);
684
685 assert!(builder.push(container, test_node()));
686 builder.set_focus(container);
687 assert!(builder.push(group, test_node()));
688 assert!(builder.push(item, test_node()));
689
690 assert!(builder.focus_is_ancestor_of_current());
693 builder.set_active_descendant(item);
694
695 builder.pop(); builder.pop(); builder.pop(); let update = builder.finalize();
699 assert_eq!(update.focus, item);
700 }
701
702 #[test]
703 fn active_descendant_ignored_when_focus_in_other_subtree() {
704 let mut builder = new_builder();
705 let focused_container = NodeId(1);
706 let focused_leaf = NodeId(2);
707 let other_container = NodeId(3);
708 let other_item = NodeId(4);
709
710 assert!(builder.push(focused_container, test_node()));
712 assert!(builder.push(focused_leaf, test_node()));
713 builder.set_focus(focused_leaf);
714 builder.pop(); builder.pop(); assert!(builder.push(other_container, test_node()));
720 assert!(builder.push(other_item, test_node()));
721 assert!(!builder.focus_is_ancestor_of_current());
722 builder.pop(); builder.pop(); let update = builder.finalize();
726 assert_eq!(update.focus, focused_leaf);
727 }
728
729 #[test]
730 fn active_descendant_ignored_when_nothing_focused() {
731 let mut builder = new_builder();
732 let container = NodeId(1);
733 let item = NodeId(2);
734
735 assert!(builder.push(container, test_node()));
736 assert!(builder.push(item, test_node()));
737
738 assert!(!builder.focus_is_ancestor_of_current());
741 builder.pop();
742 builder.pop();
743
744 let update = builder.finalize();
745 assert_eq!(update.focus, ROOT_NODE_ID);
746 }
747
748 #[test]
749 fn regular_focus_used_when_no_active_descendant() {
750 let mut builder = new_builder();
751 let focused = NodeId(1);
752
753 assert!(builder.push(focused, test_node()));
754 builder.set_focus(focused);
755 builder.pop();
756
757 let update = builder.finalize();
758 assert_eq!(update.focus, focused);
759 }
760
761 #[test]
762 fn focus_is_ancestor_excludes_self_and_non_ancestors() {
763 let mut builder = new_builder();
764 let container = NodeId(1);
765 let item = NodeId(2);
766
767 assert!(builder.push(container, test_node()));
768 builder.set_focus(container);
769
770 assert!(!builder.focus_is_ancestor_of_current());
773
774 assert!(builder.push(item, test_node()));
775 assert!(builder.focus_is_ancestor_of_current());
777
778 builder.pop();
779 builder.pop();
780 }
781
782 #[test]
785 #[cfg_attr(
786 debug_assertions,
787 should_panic(expected = "active descendant claimed by multiple nodes")
788 )]
789 fn multiple_active_descendant_claims_panic_in_debug() {
790 let mut builder = new_builder();
791 builder.set_active_descendant(NodeId(1));
792 builder.set_active_descendant(NodeId(2));
793 }
794
795 #[test]
798 #[cfg_attr(
799 debug_assertions,
800 should_panic(expected = "set_focus called more than once")
801 )]
802 fn setting_focus_twice_panics_in_debug() {
803 let mut builder = new_builder();
804 builder.set_focus(NodeId(1));
805 builder.set_focus(NodeId(2));
806 }
807
808 #[test]
811 #[cfg_attr(
812 debug_assertions,
813 should_panic(expected = "was not registered with set_focusable")
814 )]
815 fn set_focus_without_set_focusable() {
816 let mut a11y = new_a11y();
817 let node = NodeId(1);
818 assert!(a11y.nodes.push(node, test_node()));
819 a11y.set_focus(node);
821 }
822
823 #[test]
826 #[cfg_attr(debug_assertions, should_panic(expected = "on the focused node"))]
827 fn set_active_descendant_on_focused_node() {
828 let mut a11y = new_a11y();
829 let node = NodeId(1);
830 assert!(a11y.nodes.push(node, test_node()));
831 a11y.set_focusable(node, FocusId::default());
832 a11y.set_focus(node);
833 a11y.set_active_descendant(node);
834 }
835
836 #[test]
840 #[cfg_attr(
841 debug_assertions,
842 should_panic(expected = "active descendant claimed by multiple nodes")
843 )]
844 fn two_siblings_claiming_active_descendant() {
845 let mut a11y = new_a11y();
846 let container = NodeId(1);
847 let first = NodeId(2);
848 let second = NodeId(3);
849
850 assert!(a11y.nodes.push(container, test_node()));
851 a11y.set_focusable(container, FocusId::default());
852 a11y.set_focus(container);
853
854 assert!(a11y.nodes.push(first, test_node()));
855 a11y.set_active_descendant(first);
856 a11y.nodes.pop(); assert!(a11y.nodes.push(second, test_node()));
859 a11y.set_active_descendant(second);
860 a11y.nodes.pop(); a11y.nodes.pop(); }
864
865 #[test]
868 fn active_descendant_in_unfocused_subtree_keeps_real_focus() {
869 let mut a11y = new_a11y();
870 let a = NodeId(1);
871 let b = NodeId(2);
872 let c = NodeId(3);
873
874 assert!(a11y.nodes.push(a, test_node()));
875 a11y.set_focusable(a, FocusId::default());
876 a11y.set_focus(a);
877 a11y.nodes.pop(); assert!(a11y.nodes.push(b, test_node()));
880 assert!(a11y.nodes.push(c, test_node()));
881 a11y.set_active_descendant(c);
882 a11y.nodes.pop(); a11y.nodes.pop(); let update = a11y.end_frame(Default::default());
886 assert_eq!(update.focus, a);
887 }
888}