diff --git a/crates/macos/src/tree/element_bounds.rs b/crates/macos/src/tree/element_bounds.rs index d8c05ff7dc..9873e8f5fe 100644 --- a/crates/macos/src/tree/element_bounds.rs +++ b/crates/macos/src/tree/element_bounds.rs @@ -100,11 +100,22 @@ pub(crate) fn ancestor_viewport( element: &AXElement, deadline: Deadline, ) -> Result, 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, 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 { @@ -134,10 +145,66 @@ pub(crate) fn ancestor_viewport( )) } +fn window_ancestor_bounds( + element: &AXElement, + instant: std::time::Instant, +) -> Result, 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( + start: &E, + mut parent: impl FnMut(&E) -> Result, AdapterError>, + mut role: impl FnMut(&E) -> Result, AdapterError>, +) -> Result, 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, i32>, ) -> Result, (&'static str, i32)> { @@ -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; diff --git a/crates/macos/src/tree/element_bounds_tests.rs b/crates/macos/src/tree/element_bounds_tests.rs new file mode 100644 index 0000000000..4bc11e23d2 --- /dev/null +++ b/crates/macos/src/tree/element_bounds_tests.rs @@ -0,0 +1,220 @@ +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, so the owning container is read from +/// `AXWindow` alone and never from a weaker attribute. +#[test] +fn the_owning_container_is_read_from_the_window_attribute_alone() { + 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); + } +} + +fn ancestry(roles: &[&'static str]) -> impl Fn(&usize) -> Result, AdapterError> { + let len = roles.len(); + move |index| Ok((index + 1 < len).then_some(index + 1)) +} + +#[test] +fn a_window_ancestor_stands_in_for_a_missing_window_attribute() { + let roles = [ + "AXPopUpButton", + "AXGroup", + "AXScrollArea", + "AXSplitGroup", + "AXWindow", + "AXApplication", + ]; + + let window = first_window_ancestor(&0, ancestry(&roles), |index| { + Ok(Some(roles[*index].to_owned())) + }) + .unwrap(); + + assert_eq!(window, Some(4)); +} + +#[test] +fn a_chain_that_reaches_the_application_has_no_window_ancestor() { + let roles = [ + "AXMenuItem", + "AXMenu", + "AXMenuBarItem", + "AXMenuBar", + "AXApplication", + ]; + + let window = first_window_ancestor(&0, ancestry(&roles), |index| { + Ok(Some(roles[*index].to_owned())) + }) + .unwrap(); + + assert_eq!(window, None); +} + +#[test] +fn a_popup_menu_item_inside_a_window_has_no_window_ancestor() { + let roles = [ + "AXMenuItem", + "AXMenu", + "AXPopUpButton", + "AXGroup", + "AXWindow", + ]; + + let window = first_window_ancestor(&0, ancestry(&roles), |index| { + Ok(Some(roles[*index].to_owned())) + }) + .unwrap(); + + assert_eq!(window, None); +} + +#[test] +fn an_ancestor_without_a_role_ends_the_search_unknown() { + let roles = ["AXPopUpButton", "AXGroup", "AXWindow"]; + + let window = first_window_ancestor(&0, ancestry(&roles), |index| { + Ok((*index != 1).then(|| roles[*index].to_owned())) + }) + .unwrap(); + + assert_eq!(window, None); +} + +#[test] +fn an_unbounded_ancestry_fails_instead_of_looping() { + let error = first_window_ancestor( + &0_usize, + |index| Ok(Some(index + 1)), + |_| Ok(Some("AXGroup".to_owned())), + ) + .unwrap_err(); + + assert_eq!(error.code, ErrorCode::AppUnresponsive); +} + +#[test] +fn an_own_surface_element_has_no_window_ancestor_even_as_the_start() { + for start in ["AXMenu", "AXPopover", "AXMenuItem"] { + let roles = [start, "AXPopUpButton", "AXGroup", "AXWindow"]; + + let window = first_window_ancestor(&0, ancestry(&roles), |index| { + Ok(Some(roles[*index].to_owned())) + }) + .unwrap(); + + assert_eq!(window, None, "{start}"); + } +} + +#[test] +fn a_control_inside_a_popover_has_no_window_ancestor() { + let roles = ["AXButton", "AXGroup", "AXPopover", "AXGroup", "AXWindow"]; + + let window = first_window_ancestor(&0, ancestry(&roles), |index| { + Ok(Some(roles[*index].to_owned())) + }) + .unwrap(); + + assert_eq!(window, None); +}