Skip to content

Commit

Permalink
feat: Parent children based accessibility relations (#958)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
marc2332 and Tropix126 authored Oct 6, 2024
1 parent 56e11cf commit 0cb934f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 40 deletions.
7 changes: 5 additions & 2 deletions crates/core/src/accessibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use freya_native_core::{
real_dom::NodeImmutable,
};
use freya_node_state::AccessibilityNodeState;
use itertools::Itertools;
pub use tree::*;

use crate::{
Expand Down Expand Up @@ -54,7 +55,9 @@ impl NodeAccessibility for DioxusNode<'_> {

/// Collect all descendant accessibility node ids
fn get_accessibility_children(&self) -> Vec<AccessibilityId> {
let node_accessibility = &*self.get::<AccessibilityNodeState>().unwrap();
node_accessibility.descencent_accessibility_ids.clone()
self.children()
.into_iter()
.filter_map(|child| child.get_accessibility_id())
.collect_vec()
}
}
8 changes: 2 additions & 6 deletions crates/core/src/accessibility/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<AccessibilityNodeState>().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);
}
Expand Down
6 changes: 2 additions & 4 deletions crates/core/src/dom/mutations_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use freya_native_core::{
NodeId,
};
use freya_node_state::{
AccessibilityNodeState,
CursorState,
CustomAttributeValues,
LayerState,
Expand Down Expand Up @@ -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::<AccessibilityNodeState>().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);
Expand Down
32 changes: 4 additions & 28 deletions crates/state/src/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ use crate::{

#[derive(Clone, Debug, PartialEq, Default, Component)]
pub struct AccessibilityNodeState {
pub closest_accessibility_node_id: Option<NodeId>,
pub descencent_accessibility_ids: Vec<AccessibilityId>,
pub node_id: NodeId,
pub a11y_id: Option<AccessibilityId>,
pub a11y_auto_focus: bool,
Expand Down Expand Up @@ -316,9 +314,9 @@ impl ParseAttribute for AccessibilityNodeState {

#[partial_derive_state]
impl State<CustomAttributeValues> for AccessibilityNodeState {
type ParentDependencies = (Self,);
type ParentDependencies = ();

type ChildDependencies = (Self,);
type ChildDependencies = ();

type NodeDependencies = ();

Expand Down Expand Up @@ -399,8 +397,8 @@ impl State<CustomAttributeValues> for AccessibilityNodeState {
&mut self,
node_view: NodeView<CustomAttributeValues>,
_node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
_parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
_children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
context: &SendAnyMap,
) -> bool {
let root_id = context.get::<NodeId>().unwrap();
Expand Down Expand Up @@ -432,28 +430,6 @@ impl State<CustomAttributeValues> 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();

Expand Down

0 comments on commit 0cb934f

Please sign in to comment.