Skip to content

Commit

Permalink
Use filtered nodes everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
DataTriny committed Dec 27, 2022
1 parent 3ca92fb commit a76296e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 45 deletions.
79 changes: 52 additions & 27 deletions platforms/unix/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,30 +144,60 @@ impl Adapter {
tree: &'a Arc<Tree>,
queue: Vec<QueuedEvent>,
}
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()) {
Expand All @@ -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 {
Expand Down
27 changes: 9 additions & 18 deletions platforms/unix/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -102,15 +99,6 @@ impl<'a> NodeWrapper<'a> {
self.node_state().id().into()
}

pub fn child_ids(
&self,
) -> impl DoubleEndedIterator<Item = NodeId>
+ ExactSizeIterator<Item = NodeId>
+ FusedIterator<Item = NodeId>
+ '_ {
self.node_state().child_ids()
}

pub fn role(&self) -> AtspiRole {
match self.node_state().role() {
Role::Alert => AtspiRole::Notification,
Expand Down Expand Up @@ -629,7 +617,7 @@ impl PlatformNode {

pub fn child_count(&self) -> fdo::Result<i32> {
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()))
})
}
Expand All @@ -640,18 +628,21 @@ impl PlatformNode {

pub fn child_at_index(&self, index: usize) -> fdo::Result<Option<ObjectRef>> {
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<Vec<ObjectRef>> {
self.resolve(|node| {
Ok(node
let children = node
.filtered_children(&filter)
.map(|child| child.id().into())
.collect())
.collect();
Ok(children)
})
}

Expand Down

0 comments on commit a76296e

Please sign in to comment.