Skip to content

Commit 497da48

Browse files
fix(macos-helper): bound every accessibility read of the sharing indicator probe
AXUIElementSetMessagingTimeout applies only to the element it is set on, so the probe now sets it on each element it reads and stops at its own deadline.
1 parent 3cb0e86 commit 497da48

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,22 +561,32 @@ func frontWindow(
561561
/// `WindowSharingSessionButton`. That window is a system indicator, not the app's front window.
562562
private let windowSharingIndicatorButtonTitle = "WindowSharingSessionButton"
563563

564-
/// A hung app reads as no indicator rather than stalling a capture.
565-
private let sharingIndicatorReplyTimeoutSeconds: Float = 0.25
564+
/// A hung app reads as no indicator rather than stalling a capture. `AXUIElementSetMessagingTimeout`
565+
/// applies only to the element it is set on, so every element the probe reads gets it, and the
566+
/// probe stops at its own deadline.
567+
private let sharingIndicatorReplyTimeoutSeconds: Float = 0.1
568+
private let sharingIndicatorProbeSeconds = 0.5
566569

567570
private func isWindowSharingIndicator(pid: pid_t, frame: CGRect) -> Bool {
568-
let appElement = AXUIElementCreateApplication(pid)
569-
AXUIElementSetMessagingTimeout(appElement, sharingIndicatorReplyTimeoutSeconds)
571+
let deadline = Date().addingTimeInterval(sharingIndicatorProbeSeconds)
572+
func bounded(_ element: AXUIElement) -> AXUIElement? {
573+
guard Date() < deadline else { return nil }
574+
AXUIElementSetMessagingTimeout(element, sharingIndicatorReplyTimeoutSeconds)
575+
return element
576+
}
577+
guard let appElement = bounded(AXUIElementCreateApplication(pid)) else { return false }
570578
let window = windows(of: appElement).first { window in
571-
guard let rect = rectAttribute(window) else { return false }
579+
guard let window = bounded(window), let rect = rectAttribute(window) else { return false }
572580
return abs(rect.x - frame.minX) < 1 && abs(rect.y - frame.minY) < 1
573581
&& abs(rect.width - frame.width) < 1 && abs(rect.height - frame.height) < 1
574582
}
575-
return window.map(children(of:))?.contains { child in
576-
role(of: child) == "AXButton"
583+
guard let window else { return false }
584+
return children(of: window).contains { child in
585+
guard let child = bounded(child) else { return false }
586+
return role(of: child) == "AXButton"
577587
&& stringAttribute(child, attribute: kAXTitleAttribute as String)
578588
== windowSharingIndicatorButtonTitle
579-
} ?? false
589+
}
580590
}
581591

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

0 commit comments

Comments
 (0)