From d17af2630a1a2936c458ecd0843f48af8e449fba Mon Sep 17 00:00:00 2001 From: joeVenner Date: Sun, 1 Nov 2026 16:28:20 +0400 Subject: [PATCH 1/4] fix(macos): find the window of a control that omits AXWindow System Settings popup buttons answer kAXErrorNoValue for AXWindow even though an AXWindow encloses them. ancestor_viewport then had no viewport, the live offscreen state stayed unknown, and because a popup button never reports AXExpanded the actionability gate waited out its whole 5 s budget. select, click and expand on these popups failed with TIMEOUT and a "target may be busy" suggestion. When AXWindow has no value, the viewport now starts from the first AXWindow in the parent chain. A chain that reaches the application without a window, as open menu content does, still has no viewport. --- crates/macos/src/tree/element_bounds.rs | 114 +++++++++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-) diff --git a/crates/macos/src/tree/element_bounds.rs b/crates/macos/src/tree/element_bounds.rs index d8c05ff7dc..1d4a34f438 100644 --- a/crates/macos/src/tree/element_bounds.rs +++ b/crates/macos/src/tree/element_bounds.rs @@ -100,11 +100,14 @@ pub(crate) fn ancestor_viewport( element: &AXElement, deadline: Deadline, ) -> Result, AdapterError> { - let mut viewport = owning_window_bounds(element, deadline)?; + let instant = super::locator_deadline::from_operation(deadline)?; + let mut viewport = match owning_window_bounds(element, deadline)? { + Some(bounds) => Some(bounds), + None => window_ancestor_bounds(element, instant)?, + }; if viewport.is_none() { return Ok(None); } - let instant = super::locator_deadline::from_operation(deadline)?; 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,6 +137,47 @@ pub(crate) fn ancestor_viewport( )) } +fn window_ancestor_bounds( + element: &AXElement, + instant: std::time::Instant, +) -> Result, AdapterError> { + let window = first_window_ancestor( + element.clone(), + |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), + } +} + +/// Some controls, such as System Settings popup buttons, answer +/// `kAXErrorNoValue` for `AXWindow` although an `AXWindow` encloses them. The +/// parent chain still reaches that window; a chain that ends at the +/// application without one, as menu content does, has no window. +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 = parent(&start)?; + 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("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 @@ -272,4 +316,70 @@ mod tests { 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 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); + } } From d7857dfb3be3eac2e54b3a1b5d2a120ab292b5e5 Mon Sep 17 00:00:00 2001 From: joeVenner Date: Sun, 1 Nov 2026 16:42:59 +0400 Subject: [PATCH 2/4] fix(macos): keep menu content out of the window-ancestor fallback An open popup menu hangs off its popup button, so its parent chain passes through the enclosing AXWindow. The window-ancestor fallback would have given menu items that window's viewport, and an item drawn outside the window would read as offscreen. The search now stops at the first menu role, so menu content keeps no viewport, as it had before the fallback. Measured on a System Settings popup: select takes about 2.9 s with and without this guard, and changes the value in both builds. --- crates/macos/src/tree/element_bounds.rs | 27 ++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/macos/src/tree/element_bounds.rs b/crates/macos/src/tree/element_bounds.rs index 1d4a34f438..0bcbc77e1f 100644 --- a/crates/macos/src/tree/element_bounds.rs +++ b/crates/macos/src/tree/element_bounds.rs @@ -154,8 +154,10 @@ fn window_ancestor_bounds( /// Some controls, such as System Settings popup buttons, answer /// `kAXErrorNoValue` for `AXWindow` although an `AXWindow` encloses them. The -/// parent chain still reaches that window; a chain that ends at the -/// application without one, as menu content does, has no window. +/// parent chain still reaches that window. Menu content has no window even +/// when its chain passes through one, because an open menu draws outside the +/// window and would be judged offscreen; the search stops at the first menu +/// role, and a chain that ends at the application has no window either. fn first_window_ancestor( start: E, mut parent: impl FnMut(&E) -> Result, AdapterError>, @@ -168,7 +170,8 @@ fn first_window_ancestor( }; match role(&ancestor)?.as_deref() { Some("AXWindow") => return Ok(Some(ancestor)), - Some("AXApplication") | None => return Ok(None), + Some("AXApplication" | "AXMenu" | "AXMenuItem" | "AXMenuBarItem" | "AXMenuBar") + | None => return Ok(None), Some(_) => current = parent(&ancestor)?, } } @@ -359,6 +362,24 @@ mod tests { 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"]; From 4ef31c29389ed157aa6dcc8d093ada8148fdfefb Mon Sep 17 00:00:00 2001 From: flash2do Date: Sat, 7 Nov 2026 15:15:38 -0800 Subject: [PATCH 3/4] test(macos): move element_bounds tests beside the module The carried window-ancestor fallback put element_bounds.rs at 406 lines, over the 400-line cap; its inline tests now live in element_bounds_tests.rs as the layout rule asks. --- crates/macos/src/tree/element_bounds.rs | 198 +----------------- crates/macos/src/tree/element_bounds_tests.rs | 194 +++++++++++++++++ 2 files changed, 196 insertions(+), 196 deletions(-) create mode 100644 crates/macos/src/tree/element_bounds_tests.rs diff --git a/crates/macos/src/tree/element_bounds.rs b/crates/macos/src/tree/element_bounds.rs index 0bcbc77e1f..d79a0d8a2a 100644 --- a/crates/macos/src/tree/element_bounds.rs +++ b/crates/macos/src/tree/element_bounds.rs @@ -208,199 +208,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); - } - } - - 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); - } -} +#[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..215cb52799 --- /dev/null +++ b/crates/macos/src/tree/element_bounds_tests.rs @@ -0,0 +1,194 @@ +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); + } +} + +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); +} From f98f65f496ed36ea0de75e1713000fa53ca992a4 Mon Sep 17 00:00:00 2001 From: flash2do Date: Sat, 7 Nov 2026 15:33:02 -0800 Subject: [PATCH 4/4] fix(macos): keep the window-ancestor fallback from judging own-surface content or failing reads Review of the carried fallback found three gaps. A menu or popover used as the start element still walked up to its host window and read as offscreen; a popover's content was not stopped at all; and an unreadable parent chain on the fallback now failed state reads that used to report an unknown viewport. The search now starts at the element itself, stops at AXPopover as well as the menu roles, and any fallback read failure leaves the viewport unknown as before. The primary AXWindow path and its clipping walk are unchanged. --- crates/macos/src/tree/element_bounds.rs | 56 +++++++++++++------ crates/macos/src/tree/element_bounds_tests.rs | 42 +++++++++++--- 2 files changed, 72 insertions(+), 26 deletions(-) diff --git a/crates/macos/src/tree/element_bounds.rs b/crates/macos/src/tree/element_bounds.rs index d79a0d8a2a..9873e8f5fe 100644 --- a/crates/macos/src/tree/element_bounds.rs +++ b/crates/macos/src/tree/element_bounds.rs @@ -101,13 +101,21 @@ pub(crate) fn ancestor_viewport( deadline: Deadline, ) -> Result, AdapterError> { let instant = super::locator_deadline::from_operation(deadline)?; - let mut viewport = match owning_window_bounds(element, deadline)? { - Some(bounds) => Some(bounds), - None => window_ancestor_bounds(element, instant)?, - }; - if viewport.is_none() { - return Ok(None); + 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 { @@ -142,7 +150,7 @@ fn window_ancestor_bounds( instant: std::time::Instant, ) -> Result, AdapterError> { let window = first_window_ancestor( - element.clone(), + element, |current| super::surface_read::element(current, "AXParent", instant), |current| super::surface_read::string(current, "AXRole", instant), )?; @@ -152,26 +160,37 @@ fn window_ancestor_bounds( } } +/// 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. Menu content has no window even -/// when its chain passes through one, because an open menu draws outside the -/// window and would be judged offscreen; the search stops at the first menu -/// role, and a chain that ends at the application has no window either. -fn first_window_ancestor( - start: E, +/// 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 = parent(&start)?; + 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("AXApplication" | "AXMenu" | "AXMenuItem" | "AXMenuBarItem" | "AXMenuBar") - | None => return Ok(None), + Some(own) if OWN_SURFACE_ROLES.contains(&own) => return Ok(None), + Some("AXApplication") | None => return Ok(None), Some(_) => current = parent(&ancestor)?, } } @@ -183,8 +202,9 @@ fn first_window_ancestor( /// 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)> { diff --git a/crates/macos/src/tree/element_bounds_tests.rs b/crates/macos/src/tree/element_bounds_tests.rs index 215cb52799..4bc11e23d2 100644 --- a/crates/macos/src/tree/element_bounds_tests.rs +++ b/crates/macos/src/tree/element_bounds_tests.rs @@ -73,10 +73,10 @@ fn missing_clipping_geometry_is_unknown_not_empty() { ); } /// 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. +/// clips every open menu item, so the owning container is read from +/// `AXWindow` alone and never from a weaker attribute. #[test] -fn a_window_less_element_reports_no_clipping_viewport() { +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); @@ -125,7 +125,7 @@ fn a_window_ancestor_stands_in_for_a_missing_window_attribute() { "AXApplication", ]; - let window = first_window_ancestor(0, ancestry(&roles), |index| { + let window = first_window_ancestor(&0, ancestry(&roles), |index| { Ok(Some(roles[*index].to_owned())) }) .unwrap(); @@ -143,7 +143,7 @@ fn a_chain_that_reaches_the_application_has_no_window_ancestor() { "AXApplication", ]; - let window = first_window_ancestor(0, ancestry(&roles), |index| { + let window = first_window_ancestor(&0, ancestry(&roles), |index| { Ok(Some(roles[*index].to_owned())) }) .unwrap(); @@ -161,7 +161,7 @@ fn a_popup_menu_item_inside_a_window_has_no_window_ancestor() { "AXWindow", ]; - let window = first_window_ancestor(0, ancestry(&roles), |index| { + let window = first_window_ancestor(&0, ancestry(&roles), |index| { Ok(Some(roles[*index].to_owned())) }) .unwrap(); @@ -173,7 +173,7 @@ fn a_popup_menu_item_inside_a_window_has_no_window_ancestor() { fn an_ancestor_without_a_role_ends_the_search_unknown() { let roles = ["AXPopUpButton", "AXGroup", "AXWindow"]; - let window = first_window_ancestor(0, ancestry(&roles), |index| { + let window = first_window_ancestor(&0, ancestry(&roles), |index| { Ok((*index != 1).then(|| roles[*index].to_owned())) }) .unwrap(); @@ -184,7 +184,7 @@ fn an_ancestor_without_a_role_ends_the_search_unknown() { #[test] fn an_unbounded_ancestry_fails_instead_of_looping() { let error = first_window_ancestor( - 0_usize, + &0_usize, |index| Ok(Some(index + 1)), |_| Ok(Some("AXGroup".to_owned())), ) @@ -192,3 +192,29 @@ fn an_unbounded_ancestry_fails_instead_of_looping() { 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); +}