Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions adapters/ios/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::{ffi::c_void, ptr::null_mut};
use crate::{
context::{ActionHandlerNoMut, ActionHandlerWrapper, Context},
event::{EventGenerator, QueuedEvents, layout_event, screen_changed_event},
filters::filter,
filters::{element_at_point, filter},
node::PlatformNode,
util::from_cg_point,
};
Expand Down Expand Up @@ -495,7 +495,9 @@ impl Adapter {
let Some(point) = from_cg_point(&view, &root, point) else {
return null_mut();
};
let node = root.node_at_point(point, &filter).unwrap_or(root);
let Some(node) = element_at_point(root, point) else {
return null_mut();
};
match context.get_or_create_platform_node(node.id()) {
Some(platform_node) => Retained::autorelease_return(platform_node) as *mut _,
None => null_mut(),
Expand Down
58 changes: 57 additions & 1 deletion adapters/ios/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.

use accesskit::Point;
use accesskit_consumer::{FilterResult, NodeRef};
use std::iter::successors;

pub(crate) use accesskit_consumer::common_filter as filter;

Expand Down Expand Up @@ -51,10 +53,20 @@ pub(crate) fn filter_for_is_accessibility_element(node: &NodeRef) -> FilterResul
FilterResult::Include
}

/// Finds the accessibility element at a point, if any. Hit testing must only
/// return accessibility elements: VoiceOver reads whatever it is given, then
/// moves its cursor back to a real element.
pub(crate) fn element_at_point<'a>(root: NodeRef<'a>, point: Point) -> Option<NodeRef<'a>> {
let hit = root.node_at_point(point, &filter).unwrap_or(root);
successors(Some(hit), |node| node.filtered_parent(&filter))
.filter(|node| filter_for_is_accessibility_element(node) == FilterResult::Include)
.last()
}

#[cfg(test)]
mod tests {
use super::*;
use accesskit::{Action, Node, NodeId, Role, TreeId, TreeInfo, TreeUpdate};
use accesskit::{Action, Node, NodeId, Rect, Role, TreeId, TreeInfo, TreeUpdate};

const ROOT_ID: NodeId = NodeId(0);
const CHILD_1_ID: NodeId = NodeId(1);
Expand Down Expand Up @@ -127,4 +139,48 @@ mod tests {
FilterResult::Include,
);
}

const CONTAINER_ID: NodeId = NodeId(1);
const BUTTON_ID: NodeId = NodeId(2);
const LABEL_ID: NodeId = NodeId(3);

// A container whose button covers only its right half, and whose button
// gets its label from a child.
fn hit_test(point: Point) -> Option<NodeId> {
let mut root = Node::new(Role::Window);
root.set_bounds(Rect::new(0.0, 0.0, 200.0, 200.0));
root.set_children(vec![CONTAINER_ID]);
let mut container = Node::new(Role::Group);
container.set_bounds(Rect::new(0.0, 0.0, 200.0, 100.0));
container.set_children(vec![BUTTON_ID]);
let mut button = Node::new(Role::Button);
button.add_action(Action::Click);
button.set_bounds(Rect::new(100.0, 0.0, 200.0, 100.0));
button.set_children(vec![LABEL_ID]);
let mut label = Node::new(Role::Label);
label.set_value("OK");
label.set_bounds(Rect::new(120.0, 20.0, 180.0, 80.0));
let tree = build_tree(vec![
(ROOT_ID, root),
(CONTAINER_ID, container),
(BUTTON_ID, button),
(LABEL_ID, label),
]);
element_at_point(tree.state().root(), point).map(|node| node.locate().0)
}

#[test]
fn hit_test_skips_containers() {
assert_eq!(hit_test(Point::new(50.0, 50.0)), None);
}

#[test]
fn hit_test_finds_leaf() {
assert_eq!(hit_test(Point::new(110.0, 50.0)), Some(BUTTON_ID));
}

#[test]
fn hit_test_skips_children_of_leaf() {
assert_eq!(hit_test(Point::new(150.0, 50.0)), Some(BUTTON_ID));
}
}
Loading