From a76296e9c4d061818b655c57745831c9ba2193cb Mon Sep 17 00:00:00 2001 From: DataTriny Date: Tue, 27 Dec 2022 19:02:07 +0100 Subject: [PATCH] Use filtered nodes everywhere --- platforms/unix/src/adapter.rs | 79 +++++++++++++++++++++++------------ platforms/unix/src/node.rs | 27 ++++-------- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/platforms/unix/src/adapter.rs b/platforms/unix/src/adapter.rs index 74bf4353c..9be1b2f01 100644 --- a/platforms/unix/src/adapter.rs +++ b/platforms/unix/src/adapter.rs @@ -11,11 +11,11 @@ use crate::{ }, Bus, ObjectId, ACCESSIBLE_PATH_PREFIX, }, - node::{filter, NodeWrapper, PlatformNode, PlatformRootNode}, + node::{filter, filter_detached, NodeWrapper, PlatformNode, PlatformRootNode}, util::{AppContext, WindowBounds}, }; use accesskit::{kurbo::Rect, ActionHandler, NodeId, Role, TreeUpdate}; -use accesskit_consumer::{DetachedNode, Node, Tree, TreeChangeHandler, TreeState}; +use accesskit_consumer::{DetachedNode, FilterResult, Node, Tree, TreeChangeHandler, TreeState}; use atspi::{Interface, InterfaceSet, State}; use parking_lot::RwLock; use std::sync::Arc; @@ -144,30 +144,60 @@ impl Adapter { tree: &'a Arc, queue: Vec, } - impl TreeChangeHandler for Handler<'_> { - fn node_added(&mut self, node: &Node) { + impl Handler<'_> { + fn add_node(&mut self, node: &Node) { let interfaces = NodeWrapper::Node(node).interfaces(); self.adapter .register_interfaces(self.tree, node.id(), interfaces) .unwrap(); } - fn node_updated(&mut self, old_node: &DetachedNode, new_node: &Node) { - let old_wrapper = NodeWrapper::DetachedNode(old_node); - let new_wrapper = NodeWrapper::Node(new_node); - let old_interfaces = old_wrapper.interfaces(); - let new_interfaces = new_wrapper.interfaces(); - let kept_interfaces = old_interfaces & new_interfaces; - self.adapter - .unregister_interfaces(&new_wrapper.id(), old_interfaces ^ kept_interfaces) - .unwrap(); + fn remove_node(&mut self, node: &DetachedNode) { + let node = NodeWrapper::DetachedNode(node); + self.queue.push(QueuedEvent::Object { + target: node.id(), + event: ObjectEvent::StateChanged(State::Defunct, true), + }); self.adapter - .register_interfaces(self.tree, new_node.id(), new_interfaces ^ kept_interfaces) + .unregister_interfaces(&node.id(), node.interfaces()) .unwrap(); - new_wrapper.enqueue_changes( - &self.adapter.root_window_bounds.read(), - &mut self.queue, - &old_wrapper, - ); + } + } + impl TreeChangeHandler for Handler<'_> { + fn node_added(&mut self, node: &Node) { + if filter(node) == FilterResult::Include { + self.add_node(node); + } + } + fn node_updated(&mut self, old_node: &DetachedNode, new_node: &Node) { + let filter_old = filter_detached(old_node); + let filter_new = filter(new_node); + if filter_old == FilterResult::ExcludeNode && filter_old == filter_new { + } else if filter_old == FilterResult::ExcludeNode { + self.add_node(new_node); + } else if filter_new == FilterResult::ExcludeNode { + self.remove_node(old_node); + } else { + let old_wrapper = NodeWrapper::DetachedNode(old_node); + let new_wrapper = NodeWrapper::Node(new_node); + let old_interfaces = old_wrapper.interfaces(); + let new_interfaces = new_wrapper.interfaces(); + let kept_interfaces = old_interfaces & new_interfaces; + self.adapter + .unregister_interfaces(&new_wrapper.id(), old_interfaces ^ kept_interfaces) + .unwrap(); + self.adapter + .register_interfaces( + self.tree, + new_node.id(), + new_interfaces ^ kept_interfaces, + ) + .unwrap(); + new_wrapper.enqueue_changes( + &self.adapter.root_window_bounds.read(), + &mut self.queue, + &old_wrapper, + ); + } } fn focus_moved(&mut self, old_node: Option<&DetachedNode>, new_node: Option<&Node>) { if let Some(root_window) = root_window(&self.tree.read()) { @@ -193,14 +223,9 @@ impl Adapter { } } fn node_removed(&mut self, node: &DetachedNode, _: &TreeState) { - let node = NodeWrapper::DetachedNode(node); - self.queue.push(QueuedEvent::Object { - target: node.id(), - event: ObjectEvent::StateChanged(State::Defunct, true), - }); - self.adapter - .unregister_interfaces(&node.id(), node.interfaces()) - .unwrap(); + if filter_detached(node) != FilterResult::ExcludeNode { + self.remove_node(node); + } } } let mut handler = Handler { diff --git a/platforms/unix/src/node.rs b/platforms/unix/src/node.rs index 0ebf1b981..6d6eb6a0f 100644 --- a/platforms/unix/src/node.rs +++ b/platforms/unix/src/node.rs @@ -25,10 +25,7 @@ use atspi::{ StateSet, }; use parking_lot::RwLock; -use std::{ - iter::FusedIterator, - sync::{Arc, Weak}, -}; +use std::sync::{Arc, Weak}; use zbus::fdo; fn filter_common(node: &NodeState) -> FilterResult { @@ -102,15 +99,6 @@ impl<'a> NodeWrapper<'a> { self.node_state().id().into() } - pub fn child_ids( - &self, - ) -> impl DoubleEndedIterator - + ExactSizeIterator - + FusedIterator - + '_ { - self.node_state().child_ids() - } - pub fn role(&self) -> AtspiRole { match self.node_state().role() { Role::Alert => AtspiRole::Notification, @@ -629,7 +617,7 @@ impl PlatformNode { pub fn child_count(&self) -> fdo::Result { self.resolve(|node| { - i32::try_from(node.state().child_ids().count()) + i32::try_from(node.filtered_children(&filter).count()) .map_err(|_| fdo::Error::Failed("Too many children.".into())) }) } @@ -640,18 +628,21 @@ impl PlatformNode { pub fn child_at_index(&self, index: usize) -> fdo::Result> { self.resolve(|node| { - let wrapper = NodeWrapper::Node(&node); - let child = wrapper.child_ids().nth(index).map(ObjectRef::from); + let child = node + .filtered_children(&filter) + .nth(index) + .map(|child| child.id().into()); Ok(child) }) } pub fn children(&self) -> fdo::Result> { self.resolve(|node| { - Ok(node + let children = node .filtered_children(&filter) .map(|child| child.id().into()) - .collect()) + .collect(); + Ok(children) }) }