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
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import Cocoa
|
||||
import FlutterMacOS
|
||||
|
||||
// Entry point Flutter calls into via RegisterGeneratedPlugins - this is what
|
||||
// makes CursorLock and the eyedropper "just work" for any app that depends
|
||||
// on garage_ui, without that app having to hand-wire the channels itself in
|
||||
// its own MainFlutterWindow.swift. Used to be duplicated per-app, which is
|
||||
// exactly how it silently went stale (renamed channel on one side, not the
|
||||
// other) and why a brand new app (no Runner wiring at all) got a
|
||||
// MissingPluginException out of the box.
|
||||
public class GarageUiPlugin: NSObject, FlutterPlugin {
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let instance = GarageUiPlugin()
|
||||
|
||||
let cursorLockChannel = FlutterMethodChannel(
|
||||
name: "garage/cursor_lock",
|
||||
binaryMessenger: registrar.messenger
|
||||
)
|
||||
// CursorLock needs to hang on to the channel itself so it can push
|
||||
// scrubDelta calls back to dart - see its own header for why a plain
|
||||
// local var here isnt enough (it needs a home that outlives this fn)
|
||||
CursorLock.shared.attach(channel: cursorLockChannel)
|
||||
registrar.addMethodCallDelegate(instance, channel: cursorLockChannel)
|
||||
|
||||
let eyedropperChannel = FlutterMethodChannel(
|
||||
name: "garage/eyedropper",
|
||||
binaryMessenger: registrar.messenger
|
||||
)
|
||||
registrar.addMethodCallDelegate(instance, channel: eyedropperChannel)
|
||||
}
|
||||
|
||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||
switch call.method {
|
||||
case "lock":
|
||||
CursorLock.shared.lock()
|
||||
result(nil)
|
||||
case "unlock":
|
||||
CursorLock.shared.unlock()
|
||||
result(nil)
|
||||
case "isAvailable":
|
||||
if #available(macOS 10.15, *) {
|
||||
result(true)
|
||||
} else {
|
||||
result(false)
|
||||
}
|
||||
case "pick":
|
||||
if #available(macOS 10.15, *) {
|
||||
ScreenColorSampler.shared.show(result: result)
|
||||
} else {
|
||||
// dart reads nil as a cancel, which is close enough - it only ever
|
||||
// asks for a pick after isAvailable said yes anyway
|
||||
result(nil)
|
||||
}
|
||||
default:
|
||||
result(FlutterMethodNotImplemented)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import Cocoa
|
||||
import FlutterMacOS
|
||||
|
||||
// Screen wide colour picking via NSColorSampler - the same magnifier loupe
|
||||
// the system colour panel hands you when you click its eyedropper.
|
||||
//
|
||||
// Worth knowing: this runs out of process (WindowServer does the actual
|
||||
// grabbing), so unlike anything built on CGDisplayCreateImage it needs NO
|
||||
// screen recording permission and never triggers that prompt. It also draws
|
||||
// its own zoomed preview, which is why the dart side doesnt bother streaming
|
||||
// a live preview while this is up - theres nothing to add.
|
||||
//
|
||||
// Its 10.15+. Older systems just fall through to sampling the apps own
|
||||
// window in dart, so the button never disappears on anyone.
|
||||
@available(macOS 10.15, *)
|
||||
final class ScreenColorSampler {
|
||||
static let shared = ScreenColorSampler()
|
||||
|
||||
// held for the duration of the session. NSColorSampler is one shot per
|
||||
// instance and only one can be on screen at a time, so this doubles as the
|
||||
// "already sampling" flag.
|
||||
private var sampler: NSColorSampler?
|
||||
|
||||
func show(result: @escaping FlutterResult) {
|
||||
guard sampler == nil else {
|
||||
result(nil)
|
||||
return
|
||||
}
|
||||
|
||||
let sampler = NSColorSampler()
|
||||
self.sampler = sampler
|
||||
sampler.show { [weak self] color in
|
||||
self?.sampler = nil
|
||||
|
||||
// nil means escape / clicked away, thats a cancel not an error
|
||||
guard let color = color else {
|
||||
result(nil)
|
||||
return
|
||||
}
|
||||
// the sample comes back in whatever space the sampled display uses,
|
||||
// convert or the numbers wont match the hex the user sees anywhere else
|
||||
guard let srgb = color.usingColorSpace(.sRGB) else {
|
||||
result(nil)
|
||||
return
|
||||
}
|
||||
|
||||
func channel(_ value: CGFloat) -> Int {
|
||||
return Int((min(max(value, 0), 1) * 255).rounded())
|
||||
}
|
||||
|
||||
result([
|
||||
"r": channel(srgb.redComponent),
|
||||
"g": channel(srgb.greenComponent),
|
||||
"b": channel(srgb.blueComponent),
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user