|
| 1 | +import AppKit |
| 2 | +import QuartzCore |
| 3 | + |
| 4 | +/// A drawn pointer that shows where a background action lands while the user keeps the real |
| 5 | +/// one. The helper exits after each command, so the last point is persisted and the next |
| 6 | +/// action glides from it; a stale point restarts from the user's own cursor. |
| 7 | +final class GhostCursor { |
| 8 | + private static let size = CGSize(width: 64, height: 64) |
| 9 | + /// Where the arrow's tip sits inside the panel, from its top-left corner; it leaves room for |
| 10 | + /// the pulse ring's full radius. |
| 11 | + private static let tipOffset = CGPoint(x: 22, y: 22) |
| 12 | + private static let staleAfter: TimeInterval = 30 |
| 13 | + private static let positionURL = URL(fileURLWithPath: NSTemporaryDirectory()) |
| 14 | + .appendingPathComponent("agent-device-ghost-cursor.json") |
| 15 | + |
| 16 | + private let panel: NSPanel |
| 17 | + private let view: GhostCursorView |
| 18 | + private let primaryScreenHeight: CGFloat |
| 19 | + private var position: CGPoint |
| 20 | + |
| 21 | + /// Nil when no display is attached; the action still runs, just unseen. |
| 22 | + static func show() -> GhostCursor? { |
| 23 | + _ = NSApplication.shared |
| 24 | + NSApp.setActivationPolicy(.accessory) |
| 25 | + guard let primary = NSScreen.screens.first else { return nil } |
| 26 | + return GhostCursor(primaryScreenHeight: primary.frame.height) |
| 27 | + } |
| 28 | + |
| 29 | + private init(primaryScreenHeight: CGFloat) { |
| 30 | + self.primaryScreenHeight = primaryScreenHeight |
| 31 | + view = GhostCursorView(frame: CGRect(origin: .zero, size: Self.size), tip: Self.tipOffset) |
| 32 | + panel = NSPanel( |
| 33 | + contentRect: CGRect(origin: .zero, size: Self.size), |
| 34 | + styleMask: [.borderless, .nonactivatingPanel], |
| 35 | + backing: .buffered, |
| 36 | + defer: false |
| 37 | + ) |
| 38 | + panel.isOpaque = false |
| 39 | + panel.backgroundColor = .clear |
| 40 | + panel.hasShadow = false |
| 41 | + panel.ignoresMouseEvents = true |
| 42 | + panel.level = .screenSaver |
| 43 | + panel.collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle, .fullScreenAuxiliary] |
| 44 | + panel.contentView = view |
| 45 | + position = |
| 46 | + Self.restoredPosition(primaryScreenHeight: primaryScreenHeight) |
| 47 | + ?? Self.userCursorPosition(primaryScreenHeight: primaryScreenHeight) |
| 48 | + place(at: position) |
| 49 | + panel.orderFrontRegardless() |
| 50 | + flush(for: 0.016) |
| 51 | + } |
| 52 | + |
| 53 | + /// Glides to a point in global top-left coordinates, the space AX and CGEvent share. |
| 54 | + func move(to target: CGPoint, duration: TimeInterval = 0.22) { |
| 55 | + let start = position |
| 56 | + let steps = max(1, Int(duration / 0.016)) |
| 57 | + for step in 1...steps { |
| 58 | + let t = Double(step) / Double(steps) |
| 59 | + let eased = t < 0.5 ? 2 * t * t : 1 - pow(-2 * t + 2, 2) / 2 |
| 60 | + place( |
| 61 | + at: CGPoint( |
| 62 | + x: start.x + (target.x - start.x) * eased, |
| 63 | + y: start.y + (target.y - start.y) * eased |
| 64 | + ) |
| 65 | + ) |
| 66 | + flush(for: 0.016) |
| 67 | + } |
| 68 | + position = target |
| 69 | + } |
| 70 | + |
| 71 | + /// A ring at the tip, marking the moment the action was delivered. |
| 72 | + func pulse(duration: TimeInterval = 0.18) { |
| 73 | + let steps = max(1, Int(duration / 0.016)) |
| 74 | + for step in 1...steps { |
| 75 | + view.ringProgress = CGFloat(step) / CGFloat(steps) |
| 76 | + view.display() |
| 77 | + flush(for: 0.016) |
| 78 | + } |
| 79 | + view.ringProgress = nil |
| 80 | + view.display() |
| 81 | + } |
| 82 | + |
| 83 | + /// Hides the cursor; only a delivered action's point becomes the next action's start. |
| 84 | + func finish(delivered: Bool, linger: TimeInterval = 0.25) { |
| 85 | + flush(for: linger) |
| 86 | + panel.orderOut(nil) |
| 87 | + if delivered { persist(position) } |
| 88 | + } |
| 89 | + |
| 90 | + private func place(at tip: CGPoint) { |
| 91 | + panel.setFrameOrigin( |
| 92 | + CGPoint( |
| 93 | + x: tip.x - Self.tipOffset.x, |
| 94 | + y: primaryScreenHeight - tip.y - Self.size.height + Self.tipOffset.y |
| 95 | + ) |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + private func flush(for interval: TimeInterval) { |
| 100 | + CATransaction.flush() |
| 101 | + RunLoop.current.run(until: Date(timeIntervalSinceNow: interval)) |
| 102 | + } |
| 103 | + |
| 104 | + private static func userCursorPosition(primaryScreenHeight: CGFloat) -> CGPoint { |
| 105 | + let location = NSEvent.mouseLocation |
| 106 | + return CGPoint(x: location.x, y: primaryScreenHeight - location.y) |
| 107 | + } |
| 108 | + |
| 109 | + /// The last delivered point, unless it is stale or no display shows it any more. |
| 110 | + private static func restoredPosition(primaryScreenHeight: CGFloat) -> CGPoint? { |
| 111 | + guard let attributes = try? FileManager.default.attributesOfItem(atPath: positionURL.path), |
| 112 | + let modified = attributes[.modificationDate] as? Date, |
| 113 | + Date().timeIntervalSince(modified) < staleAfter, |
| 114 | + let data = try? Data(contentsOf: positionURL), |
| 115 | + let stored = try? JSONDecoder().decode([Double].self, from: data), |
| 116 | + stored.count == 2 |
| 117 | + else { |
| 118 | + return nil |
| 119 | + } |
| 120 | + let point = CGPoint(x: stored[0], y: stored[1]) |
| 121 | + let appKitPoint = CGPoint(x: point.x, y: primaryScreenHeight - point.y) |
| 122 | + return NSScreen.screens.contains { $0.frame.contains(appKitPoint) } ? point : nil |
| 123 | + } |
| 124 | + |
| 125 | + private func persist(_ point: CGPoint) { |
| 126 | + guard let data = try? JSONEncoder().encode([Double(point.x), Double(point.y)]) else { return } |
| 127 | + try? data.write(to: Self.positionURL, options: .atomic) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +private final class GhostCursorView: NSView { |
| 132 | + private let tip: CGPoint |
| 133 | + var ringProgress: CGFloat? |
| 134 | + |
| 135 | + init(frame: CGRect, tip: CGPoint) { |
| 136 | + self.tip = tip |
| 137 | + super.init(frame: frame) |
| 138 | + } |
| 139 | + |
| 140 | + required init?(coder: NSCoder) { |
| 141 | + nil |
| 142 | + } |
| 143 | + |
| 144 | + override var isFlipped: Bool { true } |
| 145 | + |
| 146 | + override func draw(_ dirtyRect: NSRect) { |
| 147 | + NSColor.clear.setFill() |
| 148 | + dirtyRect.fill() |
| 149 | + if let progress = ringProgress { |
| 150 | + let radius = 6 + 14 * progress |
| 151 | + let ring = NSBezierPath( |
| 152 | + ovalIn: CGRect(x: tip.x - radius, y: tip.y - radius, width: radius * 2, height: radius * 2) |
| 153 | + ) |
| 154 | + ring.lineWidth = 3 |
| 155 | + NSColor.systemPurple.withAlphaComponent(0.85 * (1 - progress)).setStroke() |
| 156 | + ring.stroke() |
| 157 | + } |
| 158 | + let arrow = NSBezierPath() |
| 159 | + arrow.move(to: tip) |
| 160 | + arrow.line(to: CGPoint(x: tip.x, y: tip.y + 22)) |
| 161 | + arrow.line(to: CGPoint(x: tip.x + 6, y: tip.y + 17)) |
| 162 | + arrow.line(to: CGPoint(x: tip.x + 10, y: tip.y + 26)) |
| 163 | + arrow.line(to: CGPoint(x: tip.x + 14, y: tip.y + 24)) |
| 164 | + arrow.line(to: CGPoint(x: tip.x + 10, y: tip.y + 15)) |
| 165 | + arrow.line(to: CGPoint(x: tip.x + 17, y: tip.y + 15)) |
| 166 | + arrow.close() |
| 167 | + arrow.lineJoinStyle = .round |
| 168 | + let shadow = NSShadow() |
| 169 | + shadow.shadowBlurRadius = 3 |
| 170 | + shadow.shadowOffset = CGSize(width: 0, height: -1) |
| 171 | + shadow.shadowColor = NSColor.black.withAlphaComponent(0.35) |
| 172 | + NSGraphicsContext.saveGraphicsState() |
| 173 | + shadow.set() |
| 174 | + NSColor.systemPurple.setFill() |
| 175 | + arrow.fill() |
| 176 | + NSGraphicsContext.restoreGraphicsState() |
| 177 | + arrow.lineWidth = 1.5 |
| 178 | + NSColor.white.setStroke() |
| 179 | + arrow.stroke() |
| 180 | + } |
| 181 | +} |
0 commit comments