Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
193 changes: 75 additions & 118 deletions crates/macos/src/tree/element_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,22 @@ pub(crate) fn ancestor_viewport(
element: &AXElement,
deadline: Deadline,
) -> Result<Option<Rect>, AdapterError> {
let mut viewport = owning_window_bounds(element, deadline)?;
if viewport.is_none() {
return Ok(None);
}
let instant = super::locator_deadline::from_operation(deadline)?;
if let Some(window) = owning_window_bounds(element, deadline)? {
return clip_by_ancestors(element, window, instant);
}
let Ok(Some(window)) = window_ancestor_bounds(element, instant) else {
return Ok(None);
};
Ok(clip_by_ancestors(element, window, instant).ok().flatten())
}

fn clip_by_ancestors(
element: &AXElement,
window: Rect,
instant: std::time::Instant,
) -> Result<Option<Rect>, AdapterError> {
let mut viewport = Some(window);
let mut current = super::surface_read::element(element, "AXParent", instant)?;
for _ in 0..super::properties::element::ABSOLUTE_MAX_DEPTH {
let Some(parent) = current else {
Expand Down Expand Up @@ -134,10 +145,66 @@ pub(crate) fn ancestor_viewport(
))
}

fn window_ancestor_bounds(
element: &AXElement,
instant: std::time::Instant,
) -> Result<Option<Rect>, AdapterError> {
let window = first_window_ancestor(
element,
|current| super::surface_read::element(current, "AXParent", instant),
|current| super::surface_read::string(current, "AXRole", instant),
)?;
match window {
Some(window) => read_bounds_with_deadline(&window, instant),
None => Ok(None),
}
}

/// Roles that draw on their own surface, outside the window their parent chain
/// passes through. Content at or below one of them has no window viewport,
/// because an open menu or popover would otherwise be judged offscreen.
const OWN_SURFACE_ROLES: [&str; 5] = [
"AXMenu",
"AXMenuItem",
"AXMenuBarItem",
"AXMenuBar",
"AXPopover",
];

/// Some controls, such as System Settings popup buttons, answer
/// `kAXErrorNoValue` for `AXWindow` although an `AXWindow` encloses them. The
/// parent chain still reaches that window. The search starts at the element
/// itself, stops without a window at the first own-surface role or at the
/// application, and a chain that cannot be read leaves the viewport unknown,
/// as it was before the fallback existed.
fn first_window_ancestor<E: Clone>(
start: &E,
mut parent: impl FnMut(&E) -> Result<Option<E>, AdapterError>,
mut role: impl FnMut(&E) -> Result<Option<String>, AdapterError>,
) -> Result<Option<E>, AdapterError> {
let mut current = Some(start.clone());
for _ in 0..super::properties::element::ABSOLUTE_MAX_DEPTH {
let Some(ancestor) = current else {
return Ok(None);
};
match role(&ancestor)?.as_deref() {
Some("AXWindow") => return Ok(Some(ancestor)),
Some(own) if OWN_SURFACE_ROLES.contains(&own) => return Ok(None),
Some("AXApplication") | None => return Ok(None),
Some(_) => current = parent(&ancestor)?,
}
}
Err(AdapterError::new(
ErrorCode::AppUnresponsive,
"Window ancestry exceeded its bounded depth",
))
}

/// The viewport an element is clipped by. `AXTopLevelUIElement` is not a
/// substitute: for menu content it resolves to the menu bar, a 29-point strip
/// that reports every open menu item as offscreen. An element with no window is
/// drawn on its own surface, so its clipping viewport is simply unknown.
/// that reports every open menu item as offscreen. Only `AXWindow` is read
/// here; an element that publishes none is handled by the parent-chain
/// fallback in `ancestor_viewport`.
fn first_owning_container(
mut read: impl FnMut(&'static str) -> Result<Option<AXElement>, i32>,
) -> Result<Option<AXElement>, (&'static str, i32)> {
Expand All @@ -161,115 +228,5 @@ fn read_error(attribute: &str, error: i32) -> AdapterError {
}

#[cfg(test)]
mod tests {
use super::*;

#[cfg(target_os = "macos")]
#[test]
fn native_bounds_decoder_rejects_invalid_geometry_before_live_or_snapshot_use() {
use core_graphics::geometry::{CGPoint, CGSize};
for (point, size) in [
(CGPoint::new(0.0, 33.0), CGSize::new(-1.0, 0.0)),
(CGPoint::new(0.0, 0.0), CGSize::new(10.0, -0.5)),
(CGPoint::new(f64::NAN, 0.0), CGSize::new(10.0, 10.0)),
(CGPoint::new(0.0, 0.0), CGSize::new(f64::INFINITY, 10.0)),
(CGPoint::new(0.0, 0.0), CGSize::new(f64::MAX, 10.0)),
] {
assert_eq!(rect_from_parts(point, size), None);
}
for size in [CGSize::new(0.0, 0.0), CGSize::new(20.0, 10.0)] {
let bounds = rect_from_parts(CGPoint::new(-20.0, 33.0), size).unwrap();
assert_eq!(bounds.x, -20.0);
assert_eq!(bounds.width, size.width);
assert_eq!(bounds.height, size.height);
}
}

fn rect(x: f64, width: f64) -> Rect {
Rect {
x,
y: 0.0,
width,
height: 100.0,
}
}

#[test]
fn only_clipping_containers_narrow_the_viewport() {
let window = Some(rect(0.0, 500.0));
assert_eq!(
clipped_viewport(window, "AXGroup", Some(rect(20.0, 10.0))),
window
);
assert_eq!(
clipped_viewport(window, "AXScrollArea", Some(rect(20.0, 100.0))),
Some(rect(20.0, 100.0))
);
}

#[test]
fn disjoint_viewports_remain_known_empty_through_descendants() {
let empty = clipped_viewport(
Some(rect(0.0, 100.0)),
"AXScrollArea",
Some(rect(200.0, 100.0)),
);
assert_eq!(
crate::tree::state_reader::offscreen(Some(rect(-50.0, 500.0)), empty),
Some(true)
);
let nested = clipped_viewport(empty, "AXScrollArea", Some(rect(-50.0, 500.0)));
assert_eq!(
crate::tree::state_reader::offscreen(Some(rect(-50.0, 500.0)), nested),
Some(true)
);
}

#[test]
fn missing_clipping_geometry_is_unknown_not_empty() {
assert_eq!(
clipped_viewport(Some(rect(0.0, 100.0)), "AXScrollArea", None),
None
);
assert_eq!(
clipped_viewport(None, "AXScrollArea", Some(rect(0.0, 100.0))),
None
);
}
/// A menu item's `AXTopLevelUIElement` is the menu bar, whose 29-point height
/// clips every open menu item. A window-less element therefore reports no
/// clipping viewport rather than a wrong one.
#[test]
fn a_window_less_element_reports_no_clipping_viewport() {
let mut attributes = Vec::new();
let container = first_owning_container(|attribute| {
attributes.push(attribute);
Ok(None)
})
.unwrap();

assert!(container.is_none());
assert_eq!(attributes, ["AXWindow"]);
}

#[test]
fn incomplete_window_read_never_falls_through_to_a_weaker_container() {
for error in [
accessibility_sys::kAXErrorCannotComplete,
accessibility_sys::kAXErrorInvalidUIElement,
] {
let calls = std::cell::Cell::new(0);
let result = first_owning_container(|_| {
calls.set(calls.get() + 1);
Err(error)
});

let failure = match result {
Err(failure) => failure,
Ok(_) => panic!("incomplete AXWindow read must fail"),
};
assert_eq!(failure, ("AXWindow", error));
assert_eq!(calls.get(), 1);
}
}
}
#[path = "element_bounds_tests.rs"]
mod tests;
Loading
Loading