Skip to content

Open source CapCut alternative.

TypeScript

Add accessibility action executor: grounded real-pointer click (overlay phase 4)

David Le0bda42d8cf12
diff --git a/apps/Donkey/Sources/DonkeyRuntime/AccessibilityActionExecutor.swift b/apps/Donkey/Sources/DonkeyRuntime/AccessibilityActionExecutor.swift
new file mode 100644
index 00000000..137f50ed
--- /dev/null
+++ b/apps/Donkey/Sources/DonkeyRuntime/AccessibilityActionExecutor.swift
@@ -0,0 +1,99 @@
+import CoreGraphics
+import DonkeyContracts
+import Foundation
+
+/// Low-level pointer input via CGEvent (move + left click at a screen point). Real input — callers
+/// must gate this behind explicit permission/guards.
+public enum MacPointerInput {
+ @discardableResult
+ public static func moveAndClick(at point: CGPoint) -> Bool {
+ guard let source = CGEventSource(stateID: .hidSystemState) else { return false }
+ CGEvent(mouseEventSource: source, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left)?
+ .post(tap: .cghidEventTap)
+ guard let down = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: point, mouseButton: .left),
+ let up = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: point, mouseButton: .left)
+ else {
+ return false
+ }
+ down.post(tap: .cghidEventTap)
+ up.post(tap: .cghidEventTap)
+ return true
+ }
+}
+
+/// Acts on an app by grounding a control in its accessibility tree and clicking it with the real
+/// pointer. This is the accessibility/UI execution path the strategy router falls back to for
+/// Electron / non-scriptable apps (and on AppleScript failure).
+///
+/// Safety: it performs real input only when `liveInputEnabled` is true AND the target window is
+/// frontmost (focus guard). Otherwise it grounds the control and reports what it *would* do without
+/// moving the pointer, so callers can preview / drive the overlay without acting.
+@MainActor
+public enum AccessibilityActionExecutor {
+ public struct Outcome: Sendable {
+ public var clicked: Bool
+ public var reason: String
+ public var screenPoint: CGPoint?
+ public var resolvedAppName: String?
+ public var controlID: String?
+
+ public init(
+ clicked: Bool,
+ reason: String,
+ screenPoint: CGPoint? = nil,
+ resolvedAppName: String? = nil,
+ controlID: String? = nil
+ ) {
+ self.clicked = clicked
+ self.reason = reason
+ self.screenPoint = screenPoint
+ self.resolvedAppName = resolvedAppName
+ self.controlID = controlID
+ }
+ }
+
+ public static func clickControl(
+ appName: String?,
+ bundleIdentifier: String?,
+ targetQuery: String,
+ liveInputEnabled: Bool,
+ windowResolver: MacWindowResolver = MacWindowResolver(),
+ click: (CGPoint) -> Bool = MacPointerInput.moveAndClick
+ ) -> Outcome {
+ guard let observation = AccessibilityObserver.observe(
+ appName: appName,
+ bundleIdentifier: bundleIdentifier,
+ resolver: windowResolver
+ ) else {
+ return Outcome(clicked: false, reason: "accessibilityObservationUnavailable")
+ }
+ guard let control = AccessibilityCursorPathBuilder.resolve(targetQuery, in: observation.controls),
+ let frame = control.frame,
+ frame.hasPositiveArea
+ else {
+ return Outcome(clicked: false, reason: "controlNotFound", resolvedAppName: observation.target.appName)
+ }
+
+ let point = clickPoint(for: frame)
+ // Focus guard: only drive real input into the frontmost window.
+ guard observation.target.isFrontmost else {
+ return Outcome(clicked: false, reason: "targetNotFrontmost", screenPoint: point, resolvedAppName: observation.target.appName, controlID: control.id)
+ }
+ guard liveInputEnabled else {
+ return Outcome(clicked: false, reason: "liveInputDisabled", screenPoint: point, resolvedAppName: observation.target.appName, controlID: control.id)
+ }
+ let didClick = click(point)
+ return Outcome(
+ clicked: didClick,
+ reason: didClick ? "ok" : "inputFailed",
+ screenPoint: point,
+ resolvedAppName: observation.target.appName,
+ controlID: control.id
+ )
+ }
+
+ /// Center of a screen-space control frame (top-left origin global coordinates, as CGEvent uses).
+ nonisolated static func clickPoint(for frame: WindowTargetBounds) -> CGPoint {
+ CGPoint(x: frame.x + frame.width / 2, y: frame.y + frame.height / 2)
+ }
+}
diff --git a/apps/Donkey/Sources/DonkeyRuntime/AccessibilityObserver.swift b/apps/Donkey/Sources/DonkeyRuntime/AccessibilityObserver.swift
new file mode 100644
index 00000000..40cc4e54
--- /dev/null
+++ b/apps/Donkey/Sources/DonkeyRuntime/AccessibilityObserver.swift
@@ -0,0 +1,65 @@
+@preconcurrency import ApplicationServices
+import DonkeyContracts
+import Foundation
+
+/// A resolved live accessibility observation of one app window: the window target and its controls.
+struct ResolvedAccessibilityObservation {
+ var target: MacWindowTargetCandidate
+ var controls: [LocalAppDiscoveredControl]
+}
+
+/// Captures a running app's accessibility tree and discovers its controls. Shared by the guidance
+/// overlay and the accessibility action executor so both ground against the same observation.
+@MainActor
+enum AccessibilityObserver {
+ static func observe(
+ appName: String?,
+ bundleIdentifier: String?,
+ resolver: MacWindowResolver = MacWindowResolver()
+ ) -> ResolvedAccessibilityObservation? {
+ guard AXIsProcessTrusted(),
+ let target = resolveTarget(appName: appName, bundleIdentifier: bundleIdentifier, resolver: resolver)
+ else {
+ return nil
+ }
+ let limits = MacAccessibilitySnapshotLimits(maxDepth: 8, maxChildrenPerNode: 120, maxTotalNodes: 1_200)
+ guard let tree = try? ApplicationServicesMacAccessibilitySnapshotCapturer().captureTree(
+ target: target,
+ limits: limits
+ ) else {
+ return nil
+ }
+ let snapshot = MacAccessibilitySnapshot(
+ target: target,
+ limits: limits,
+ root: tree.root,
+ totalNodeCount: tree.totalNodeCount,
+ isTreeTruncated: tree.isTreeTruncated
+ )
+ let index = LocalAppAccessibilityControlDiscovery().discover(in: snapshot)
+ return ResolvedAccessibilityObservation(target: target, controls: index.controls)
+ }
+
+ static func resolveTarget(
+ appName: String?,
+ bundleIdentifier: String?,
+ resolver: MacWindowResolver
+ ) -> MacWindowTargetCandidate? {
+ if appName != nil || bundleIdentifier != nil {
+ let candidates = resolver.enumerateCandidates()
+ if let match = candidates.first(where: { candidate in
+ if let bundleIdentifier, let candidateBundle = candidate.bundleIdentifier {
+ return candidateBundle.caseInsensitiveCompare(bundleIdentifier) == .orderedSame
+ }
+ if let appName, let candidateApp = candidate.appName {
+ return candidateApp.localizedCaseInsensitiveContains(appName)
+ || appName.localizedCaseInsensitiveContains(candidateApp)
+ }
+ return false
+ }) {
+ return match
+ }
+ }
+ return try? resolver.selectTarget()
+ }
+}
diff --git a/apps/Donkey/Sources/DonkeyRuntime/GuidanceOverlayFlow.swift b/apps/Donkey/Sources/DonkeyRuntime/GuidanceOverlayFlow.swift
index 2237e089..b04d816f 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/GuidanceOverlayFlow.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/GuidanceOverlayFlow.swift
@@ -37,33 +37,18 @@ public enum GuidanceOverlayFlow {
guard !targets.isEmpty else {
return Outcome(request: nil, reason: "noTargets", resolvedAppName: nil)
}
- guard let target = resolveTarget(
+ guard let observation = AccessibilityObserver.observe(
appName: appName,
bundleIdentifier: bundleIdentifier,
resolver: windowResolver
) else {
- return Outcome(request: nil, reason: "noWindowForApp", resolvedAppName: nil)
+ return Outcome(request: nil, reason: "accessibilityObservationUnavailable", resolvedAppName: nil)
}
-
- let limits = MacAccessibilitySnapshotLimits(maxDepth: 8, maxChildrenPerNode: 120, maxTotalNodes: 1_200)
- guard let tree = try? ApplicationServicesMacAccessibilitySnapshotCapturer().captureTree(
- target: target,
- limits: limits
- ) else {
- return Outcome(request: nil, reason: "accessibilityCaptureFailed", resolvedAppName: target.appName)
- }
- let snapshot = MacAccessibilitySnapshot(
- target: target,
- limits: limits,
- root: tree.root,
- totalNodeCount: tree.totalNodeCount,
- isTreeTruncated: tree.isTreeTruncated
- )
- let index = LocalAppAccessibilityControlDiscovery().discover(in: snapshot)
+ let target = observation.target
let steps = AccessibilityCursorPathBuilder.buildSteps(
targetApp: target.appName ?? appName ?? "",
- windowBounds: snapshot.target.bounds,
- controls: index.controls,
+ windowBounds: target.bounds,
+ controls: observation.controls,
targets: targets,
phaseID: "guidance"
)
@@ -82,27 +67,4 @@ public enum GuidanceOverlayFlow {
}
return Outcome(request: request, reason: "ok", resolvedAppName: target.appName)
}
-
- private static func resolveTarget(
- appName: String?,
- bundleIdentifier: String?,
- resolver: MacWindowResolver
- ) -> MacWindowTargetCandidate? {
- if appName != nil || bundleIdentifier != nil {
- let candidates = resolver.enumerateCandidates()
- if let match = candidates.first(where: { candidate in
- if let bundleIdentifier, let candidateBundle = candidate.bundleIdentifier {
- return candidateBundle.caseInsensitiveCompare(bundleIdentifier) == .orderedSame
- }
- if let appName, let candidateApp = candidate.appName {
- return candidateApp.localizedCaseInsensitiveContains(appName)
- || appName.localizedCaseInsensitiveContains(candidateApp)
- }
- return false
- }) {
- return match
- }
- }
- return try? resolver.selectTarget()
- }
}
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/AccessibilityActionExecutorTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/AccessibilityActionExecutorTests.swift
new file mode 100644
index 00000000..dc2340a3
--- /dev/null
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/AccessibilityActionExecutorTests.swift
@@ -0,0 +1,16 @@
+import CoreGraphics
+@testable import DonkeyRuntime
+import DonkeyContracts
+import Foundation
+import Testing
+
+@Suite
+struct AccessibilityActionExecutorTests {
+ @Test
+ func clickPointIsTheCenterOfTheControlFrame() {
+ let frame = WindowTargetBounds(x: 100, y: 200, width: 80, height: 40)
+ let point = AccessibilityActionExecutor.clickPoint(for: frame)
+ #expect(point.x == 140)
+ #expect(point.y == 220)
+ }
+}