Files
Garage-SDKs/garage_ui/macos/Classes/CursorLock.swift
T
ImBenjiandClaude Opus 5.5 b269201919 The Garage SDKs, in the open
garage_auth, garage_entitlements, garage_iap and garage_ui, moved out of
Garage-Services and Metro-Map-Maker into one public repo. MIT, one readme,
docs under docs/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013F4NWNvYcdeSgqbWMT1VQ7
2026-09-23 18:49:21 +01:00

65 lines
2.6 KiB
Swift

import Cocoa
import FlutterMacOS
// Hides the OS cursor and keeps it pinned in place for the duration of a
// drag, streaming raw pointer deltas back to Dart ourselves.
//
// Why not just let Flutters own pointer events drive this: Flutter computes
// PointerEvent.delta from the ABSOLUTE cursor position of consecutive
// events, not from raw hardware deltaX/deltaY. An earlier version tried to
// keep the cursor pinned by warping it back to an anchor point after every
// move while staying associated in between - that still let the OS visibly
// move the cursor between warps, and the warps themselves raced against
// real movement over the async Dart<->native round trip.
//
// The actual fix: CGAssociateMouseAndMouseCursorPosition(false) for the
// WHOLE gesture, not toggled per-event. WindowServer then simply stops
// moving the on-screen cursor at all in response to hardware input - no
// per-frame warping needed. mouseDragged events keep firing completely
// normally while disassociated, with correct deltaX/deltaY reflecting real
// HID movement (its only locationInWindow that freezes) - which is exactly
// why we read deltaX/deltaY here instead of position. Re-associating on
// unlock resumes normal 1:1 tracking from wherever the cursor was frozen,
// so it naturally reappears back where the drag started.
final class CursorLock {
static let shared = CursorLock()
private var locked = false
private var monitor: Any?
// strong on purpose - CursorLock.shared is a permanent singleton, theres
// no retain cycle risk, and a weak ref here previously meant the channel
// (whose only other strong reference was a local `let` in awakeFromNib)
// got deallocated the moment that function returned, silently breaking
// delta forwarding.
private var channel: FlutterMethodChannel?
func attach(channel: FlutterMethodChannel) {
self.channel = channel
}
func lock() {
guard !locked else { return }
locked = true
NSCursor.hide()
CGAssociateMouseAndMouseCursorPosition(0)
monitor = NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDragged]) { [weak self] event in
guard let self, self.locked else { return event }
self.channel?.invokeMethod("scrubDelta", arguments: ["dx": event.deltaX, "dy": event.deltaY])
// swallowed - Flutters own position for this event is frozen (and
// therefore meaningless) while disassociated, no point forwarding it.
return nil
}
}
func unlock() {
guard locked else { return }
locked = false
if let monitor {
NSEvent.removeMonitor(monitor)
}
monitor = nil
CGAssociateMouseAndMouseCursorPosition(1)
NSCursor.unhide()
}
}