Skip to content

Commit cbb2548

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 b0240cf commit cbb2548

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
@@ -722,9 +722,10 @@ func requireWindowInfo(_ windowInfo: [[String: Any]]? = appWindowInfo()) throws
722722
/// `WindowSharingSessionButton`. That window is a system indicator, not the app's front window.
723723
private let windowSharingIndicatorButtonTitle = "WindowSharingSessionButton"
724724

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

730731
/// `AXUIElementSetMessagingTimeout` applies only to the element it is set on, so every element the
@@ -748,10 +749,11 @@ private func isWindowSharingIndicator(pid: pid_t, frame: CGRect) -> Bool {
748749
return isSameFrame(rect, frame)
749750
}
750751
guard let window else { return false }
751-
return isWindowSharingIndicator(
752-
children: children(of: window).lazy.compactMap(bounded).map { child in
753-
(role: role(of: child), title: stringAttribute(child, attribute: kAXTitleAttribute as String))
754-
})
752+
return children(of: window).contains { child in
753+
guard let child = bounded(child) else { return false }
754+
return isWindowSharingIndicatorButton(
755+
role: role(of: child), title: stringAttribute(child, attribute: kAXTitleAttribute as String))
756+
}
755757
}
756758

757759
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)