From 0cb934f6826bd829818938955d5471178d1714a5 Mon Sep 17 00:00:00 2001 From: Marc Espin Date: Sun, 6 Oct 2024 10:14:15 +0200 Subject: [PATCH] feat: Parent children based accessibility relations (#958) * feat: add attributes for most AccessKit properties * fix: update components * fix: tests * fix `NodeBuilder` unwrap assumption * fix: Proper support for keyboard navigation for Radio (#880) * fix: Proper incremental redraws for elements with outer or center borders * chore: torin changes * feat: Proper support for keyboard navigation with Radio * fix: Update tests * chore: Update tests * feat: Only focus focusable nodes * chore: Update tests * chore: Update tests * feat: add attributes for most AccessKit properties * fix: update components * fix: tests * revert components changes * fix accessibility nodes not being added to tree * feat: use inner text for `paragraph`/`label` names if none is provided * fix accessibility tests * fmt, lint * reduce out-of-scope changes * fmt * refactor: make `a11y_role` attribute kebab case * lint * fmt again * fix bad accessibility state merge * use `Role::parse` rather than `serde_json` * fix bad role in test * fix role parsing test * update or remove redundant roles from components * feat: Parent & children-based accessibility nodes relations --------- Co-authored-by: Tropical <42101043+Tropix126@users.noreply.github.com> --- crates/core/src/accessibility/mod.rs | 7 ++++-- crates/core/src/accessibility/tree.rs | 8 ++----- crates/core/src/dom/mutations_writer.rs | 6 ++--- crates/state/src/accessibility.rs | 32 ++++--------------------- 4 files changed, 13 insertions(+), 40 deletions(-) diff --git a/crates/core/src/accessibility/mod.rs b/crates/core/src/accessibility/mod.rs index 52f3cc600..e590e541e 100644 --- a/crates/core/src/accessibility/mod.rs +++ b/crates/core/src/accessibility/mod.rs @@ -4,6 +4,7 @@ use freya_native_core::{ real_dom::NodeImmutable, }; use freya_node_state::AccessibilityNodeState; +use itertools::Itertools; pub use tree::*; use crate::{ @@ -54,7 +55,9 @@ impl NodeAccessibility for DioxusNode<'_> { /// Collect all descendant accessibility node ids fn get_accessibility_children(&self) -> Vec { - let node_accessibility = &*self.get::().unwrap(); - node_accessibility.descencent_accessibility_ids.clone() + self.children() + .into_iter() + .filter_map(|child| child.get_accessibility_id()) + .collect_vec() } } diff --git a/crates/core/src/accessibility/tree.rs b/crates/core/src/accessibility/tree.rs index 565de7d68..f54c61f18 100644 --- a/crates/core/src/accessibility/tree.rs +++ b/crates/core/src/accessibility/tree.rs @@ -152,12 +152,8 @@ impl AccessibilityTree { // Mark the ancestors as modified for node_id in added_or_updated_ids.clone() { let node_ref = rdom.get(node_id).unwrap(); - let node_accessibility_state = node_ref.get::().unwrap(); - added_or_updated_ids.insert( - node_accessibility_state - .closest_accessibility_node_id - .unwrap_or(rdom.root_id()), - ); + let node_ref_parent = node_ref.parent_id().unwrap_or(rdom.root_id()); + added_or_updated_ids.insert(node_ref_parent); self.map .insert(node_ref.get_accessibility_id().unwrap(), node_id); } diff --git a/crates/core/src/dom/mutations_writer.rs b/crates/core/src/dom/mutations_writer.rs index 8db949c0b..aeb1d59be 100644 --- a/crates/core/src/dom/mutations_writer.rs +++ b/crates/core/src/dom/mutations_writer.rs @@ -17,7 +17,6 @@ use freya_native_core::{ NodeId, }; use freya_node_state::{ - AccessibilityNodeState, CursorState, CustomAttributeValues, LayerState, @@ -91,9 +90,8 @@ impl<'a> MutationsWriter<'a> { // Remove from the accessibility tree if node.get_accessibility_id().is_some() { - let node_accessibility_state = node.get::().unwrap(); - let closed_accessibility_node_id = node_accessibility_state - .closest_accessibility_node_id + let closed_accessibility_node_id = node + .parent_id() .unwrap_or(self.native_writer.rdom.root_id()); self.accessibility_dirty_nodes .remove(node.id(), closed_accessibility_node_id); diff --git a/crates/state/src/accessibility.rs b/crates/state/src/accessibility.rs index bcbdb0221..751ee14ca 100644 --- a/crates/state/src/accessibility.rs +++ b/crates/state/src/accessibility.rs @@ -51,8 +51,6 @@ use crate::{ #[derive(Clone, Debug, PartialEq, Default, Component)] pub struct AccessibilityNodeState { - pub closest_accessibility_node_id: Option, - pub descencent_accessibility_ids: Vec, pub node_id: NodeId, pub a11y_id: Option, pub a11y_auto_focus: bool, @@ -316,9 +314,9 @@ impl ParseAttribute for AccessibilityNodeState { #[partial_derive_state] impl State for AccessibilityNodeState { - type ParentDependencies = (Self,); + type ParentDependencies = (); - type ChildDependencies = (Self,); + type ChildDependencies = (); type NodeDependencies = (); @@ -399,8 +397,8 @@ impl State for AccessibilityNodeState { &mut self, node_view: NodeView, _node: ::ElementBorrowed<'a>, - parent: Option<::ElementBorrowed<'a>>, - children: Vec<::ElementBorrowed<'a>>, + _parent: Option<::ElementBorrowed<'a>>, + _children: Vec<::ElementBorrowed<'a>>, context: &SendAnyMap, ) -> bool { let root_id = context.get::().unwrap(); @@ -432,28 +430,6 @@ impl State for AccessibilityNodeState { } } - for (child,) in children { - if let Some(child_id) = child.a11y_id { - // Mark this child as descendent if it has an ID - accessibility.descencent_accessibility_ids.push(child_id) - } else { - // If it doesn't have an ID then use its descencent accessibility IDs - accessibility - .descencent_accessibility_ids - .extend(child.descencent_accessibility_ids.iter()); - } - } - - if let Some(parent) = parent { - // Mark the parent accessibility ID as the closest to this node or - // fallback to its closest ID. - accessibility.closest_accessibility_node_id = parent - .0 - .a11y_id - .map(|_| parent.0.node_id) - .or(parent.0.closest_accessibility_node_id); - } - let changed = &accessibility != self; let had_id = self.a11y_id.is_some();