diff --git a/native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift b/native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift index 3c74baca977..82ddfe10070 100644 --- a/native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift +++ b/native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift @@ -1186,8 +1186,10 @@ final class Provider { try requireTargetWindowFocused(snapshot, restoreWindowRequested: true) try Input.drag(from: start, to: end, targetWindow: snapshot) } else { - // Why: the mouse-down opens a popup or menu button's menu over the user before the drag moves. - let startElement = try fromRecord?.element ?? backgroundPointerTarget(at: start, snapshot: snapshot) + // Why: the mouse-down lands on whatever is under the start point, which can open a menu over the user. + guard let startElement = elementAtPoint(start, pid: snapshot.app.pid) ?? fromRecord?.element else { + throw unidentifiedBackgroundTarget + } try requireNoForegroundEffect(ForegroundEffect.ofPress(BackgroundInputTransport.pressTarget(startElement))) try withTransportErrors { try BackgroundInputTransport.drag(backgroundTarget(snapshot), from: start, to: end) @@ -1659,7 +1661,9 @@ private func clickPointer( try Input.click(at: point, button: button, count: count, modifiers: modifiers, targetWindow: snapshot) return } - let hitElement = try element ?? backgroundPointerTarget(at: point, snapshot: snapshot) + guard let hitElement = element ?? elementAtPoint(point, pid: snapshot.app.pid) else { + throw unidentifiedBackgroundTarget + } let hit = try backgroundClickHit(hitElement, snapshot: snapshot) // Why: an element click already checked its target before trying AXPress. if element == nil { @@ -1677,17 +1681,18 @@ private func clickPointer( } } -private func backgroundPointerTarget(at point: CGPoint, snapshot: Snapshot) throws -> AXUIElement { +private func elementAtPoint(_ point: CGPoint, pid: pid_t) -> AXUIElement? { var found: AXUIElement? guard Float(point.x).isFinite, Float(point.y).isFinite, - AXUIElementCopyElementAtPosition(AXUIElementCreateApplication(snapshot.app.pid), Float(point.x), Float(point.y), &found) == .success, - let found + AXUIElementCopyElementAtPosition(AXUIElementCreateApplication(pid), Float(point.x), Float(point.y), &found) == .success else { - throw ProviderError.coded("background_unavailable", "cannot identify the background click target; run get-app-state again or retry with --restore-window") + return nil } return found } +private let unidentifiedBackgroundTarget = ProviderError.coded("background_unavailable", "cannot identify the background click target; run get-app-state again or retry with --restore-window") + private func backgroundClickHit(_ element: AXUIElement, snapshot: Snapshot) throws -> BackgroundInputTransport.PointerHit { let recipient = pointerRecipient(of: element, targetPID: snapshot.app.pid) { WindowCapture.candidates(pid: snapshot.app.pid) diff --git a/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/ForegroundEffect.swift b/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/ForegroundEffect.swift index 49fbb57e4ce..988bf8fcad8 100644 --- a/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/ForegroundEffect.swift +++ b/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/ForegroundEffect.swift @@ -48,7 +48,8 @@ public enum ForegroundEffect: Equatable, Sendable { public static func ofAction(_ action: String, target chain: @autoclosure () -> [Node]) -> ForegroundEffect? { switch action { case "AXShowMenu": return .showsMenu - case "AXPress", "AXPick": return ofPress(chain()) + // Why: the same presses a click tries, so a secondary action can't run what a click refuses. + case "AXPress", "AXConfirm", "AXOpen", "AXPick": return ofPress(chain()) default: return nil } } diff --git a/native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/ForegroundEffectTests.swift b/native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/ForegroundEffectTests.swift index fb3cdc4bddd..3798e18b24b 100644 --- a/native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/ForegroundEffectTests.swift +++ b/native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/ForegroundEffectTests.swift @@ -32,5 +32,6 @@ final class ForegroundEffectTests: XCTestCase { XCTAssertNil(ForegroundEffect.ofClick(button: .left, control: false, target: plainButton)) XCTAssertEqual(ForegroundEffect.ofAction("AXShowMenu", target: plainButton), .showsMenu) XCTAssertNil(ForegroundEffect.ofAction("AXScrollToVisible", target: [Node(role: "AXPopUpButton")])) + XCTAssertEqual(ForegroundEffect.ofAction("AXConfirm", target: [Node(role: "AXButton", labels: ["Open\u{2026}"])]), .opensDialog) } }