Skip to content

Commit c7205ac

Browse files
thymikeeclaude
andcommitted
fix(macos-helper): read a child's title only once it is a button
The recognizer now classifies one child, with the title as an autoclosure, so the probe again reads a child's title only after its role is AXButton, as it did before the recognizer became testable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4543a7f commit c7205ac

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

‎apple/macos-helper/Sources/AgentDeviceMacOSHelper/BackgroundInteraction.swift‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,10 @@ func frontWindow(
565565
/// `WindowSharingSessionButton`. That window is a system indicator, not the app's front window.
566566
private let windowSharingIndicatorButtonTitle = "WindowSharingSessionButton"
567567

568-
/// Whether a window with these accessibility children, as role and title, is the indicator.
569-
func isWindowSharingIndicator(children: some Sequence<(role: String, title: String?)>) -> Bool {
570-
children.contains { $0.role == "AXButton" && $0.title == windowSharingIndicatorButtonTitle }
568+
/// Whether a child of a window, by its accessibility role and title, marks the window as the
569+
/// indicator. The title is read only for a button.
570+
func isWindowSharingIndicatorButton(role: String, title: @autoclosure () -> String?) -> Bool {
571+
role == "AXButton" && title() == windowSharingIndicatorButtonTitle
571572
}
572573

573574
/// `AXUIElementSetMessagingTimeout` applies only to the element it is set on, so every element the
@@ -591,10 +592,11 @@ private func isWindowSharingIndicator(pid: pid_t, frame: CGRect) -> Bool {
591592
return isSameFrame(rect, frame)
592593
}
593594
guard let window else { return false }
594-
return isWindowSharingIndicator(
595-
children: children(of: window).lazy.compactMap(bounded).map { child in
596-
(role: role(of: child), title: stringAttribute(child, attribute: kAXTitleAttribute as String))
597-
})
595+
return children(of: window).contains { child in
596+
guard let child = bounded(child) else { return false }
597+
return isWindowSharingIndicatorButton(
598+
role: role(of: child), title: stringAttribute(child, attribute: kAXTitleAttribute as String))
599+
}
598600
}
599601

600602
func frontWindowFrame(pid: pid_t, windowInfo: [[String: Any]]? = appWindowInfo()) throws -> CGRect? {

‎apple/macos-helper/Tests/AgentDeviceMacOSHelperTests/AppWindowScreenshotTests.swift‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,23 @@ final class AppWindowScreenshotTests: XCTestCase {
5555
}
5656

5757
func testRecognizesTheSharingIndicatorOnlyByItsSessionButton() {
58-
XCTAssertTrue(isWindowSharingIndicator(children: [("AXButton", "WindowSharingSessionButton")]))
58+
XCTAssertTrue(isWindowSharingIndicatorButton(role: "AXButton", title: "WindowSharingSessionButton"))
5959

6060
// A small ordinary window: title-bar buttons, or the session title on a non-button.
61+
XCTAssertFalse(isWindowSharingIndicatorButton(role: "AXButton", title: nil))
62+
XCTAssertFalse(isWindowSharingIndicatorButton(role: "AXButton", title: "Close"))
6163
XCTAssertFalse(
62-
isWindowSharingIndicator(children: [
63-
(role: "AXButton", title: nil), (role: "AXButton", title: "Close"),
64-
(role: "AXStaticText", title: "Window"),
65-
]))
66-
XCTAssertFalse(isWindowSharingIndicator(children: [("AXStaticText", "WindowSharingSessionButton")]))
67-
XCTAssertFalse(isWindowSharingIndicator(children: []))
64+
isWindowSharingIndicatorButton(role: "AXStaticText", title: "WindowSharingSessionButton"))
65+
}
66+
67+
/// A child's title is read only once its role is a button.
68+
func testReadsNoTitleForAChildThatIsNotAButton() {
69+
var readTitle = false
70+
let isIndicator = isWindowSharingIndicatorButton(
71+
role: "AXGroup", title: { readTitle = true; return "WindowSharingSessionButton" }())
72+
73+
XCTAssertFalse(isIndicator)
74+
XCTAssertFalse(readTitle)
6875
}
6976

7077
/// The indicator check reads the app's accessibility tree; a lone window never needs it.

0 commit comments

Comments
 (0)