Skip to content

Commit e8688c3

Browse files
fix(macos-helper): refuse an unreadable window list during the menu wait
A secondary click whose menu poll never read the window list now fails with window-list-unreadable instead of context-menu-not-observed. ADR 0031 scopes the blocking AXShowMenu reply to the measured SwiftUI app.
1 parent 1c71c7d commit e8688c3

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func showMenuInBackground(
234234
let menusBefore = popUpMenuWindowNumbers(pid: pid, windowInfo: try requireWindowInfo())
235235
AXUIElementSetMessagingTimeout(owner, showMenuReplyTimeoutSeconds)
236236
let reply = AXUIElementPerformAction(owner, showMenuAction)
237-
guard waitForNewMenuWindow(pid: pid, excluding: menusBefore) else {
237+
guard try waitForNewMenuWindow(pid: pid, excluding: menusBefore) else {
238238
throw HelperError.commandFailed(
239239
"the element offered a context menu, but no menu opened",
240240
details: [
@@ -285,16 +285,20 @@ private func menuOwner(from hit: AXUIElement) -> AXUIElement? {
285285
return nil
286286
}
287287

288-
private func waitForNewMenuWindow(pid: pid_t, excluding before: Set<Int>) -> Bool {
288+
/// A window list that never became readable is a refusal, not an unobserved menu.
289+
private func waitForNewMenuWindow(pid: pid_t, excluding before: Set<Int>) throws -> Bool {
289290
let deadline = Date().addingTimeInterval(Double(menuWindowWaitMs) / 1000)
291+
var readAny = false
290292
repeat {
291-
if let info = appWindowInfo(),
292-
!popUpMenuWindowNumbers(pid: pid, windowInfo: info).subtracting(before).isEmpty
293-
{
294-
return true
293+
if let info = appWindowInfo() {
294+
readAny = true
295+
if !popUpMenuWindowNumbers(pid: pid, windowInfo: info).subtracting(before).isEmpty {
296+
return true
297+
}
295298
}
296299
Thread.sleep(forTimeInterval: Double(menuWindowPollMs) / 1000)
297300
} while Date() < deadline
301+
if !readAny { _ = try requireWindowInfo(nil) }
298302
return false
299303
}
300304

‎docs/adr/0031-macos-native-app-backend.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Pointer actions have no event fallback until an event path verifies its outcome.
9090
The helper performs `AXShowMenu` on the nearest element up the hit chain (at most 32 levels) that offers it.
9191
Unlike a press, the walk crosses containers, which can own a menu, as AppKit consults each superview.
9292
Parity with a real click outside SwiftUI is unmeasured; an `NSTableView` menu may report no clicked row.
93-
The app tracks the open menu and does not answer the action (`kAXErrorCannotComplete`).
93+
In the measured SwiftUI app, the app tracks the open menu and does not answer the action (`kAXErrorCannotComplete`).
9494
So the reply is abandoned after 0.5 s, and a new app-owned `kCGPopUpMenuWindowLevel` window within 1 s decides success.
9595
Otherwise the click fails with `context-menu-not-observed`.
9696
The open menu is in the owner's subtree, so a snapshot shows its items and a press picks one.

0 commit comments

Comments
 (0)