diff --git a/Sources/HelderUI/Components/PointerPolicy.swift b/Sources/HelderUI/Components/PointerPolicy.swift index da717b5..c904750 100644 --- a/Sources/HelderUI/Components/PointerPolicy.swift +++ b/Sources/HelderUI/Components/PointerPolicy.swift @@ -242,6 +242,22 @@ extension PointerPolicy { } } + private static var claimedClasses: Set = [] + + /// Gives the window's hosting view a `cursorUpdate:` that asks the policy. AppKit calls it directly on every display + /// cycle that moves a tracking area, outside any event the monitor sees, and the hosting view's own answer is the arrow. + static func claimCursorUpdates(of view: NSView) { + guard let viewClass = object_getClass(view), claimedClasses.insert(ObjectIdentifier(viewClass)).inserted else { return } + let block: @convention(block) (AnyObject, NSEvent) -> Void = { object, _ in + MainActor.assumeIsolated { + scheduleUpdate(in: (object as? NSView)?.window) + } + } + let selector = #selector(NSResponder.cursorUpdate(with:)) + let types = method_getTypeEncoding(class_getInstanceMethod(NSView.self, selector)!) + class_replaceMethod(viewClass, selector, imp_implementationWithBlock(block), types) + } + /// Runs `updateCursor` once, after the current event. Inside an event SwiftUI's hit test returns its root view, /// not the view under the mouse. It never resets the cursor rects: that would fire new cursor updates. static func scheduleUpdate(in window: NSWindow?) { diff --git a/Sources/HelderUI/Shell/WindowBridge.swift b/Sources/HelderUI/Shell/WindowBridge.swift index aab6cac..e9901c8 100644 --- a/Sources/HelderUI/Shell/WindowBridge.swift +++ b/Sources/HelderUI/Shell/WindowBridge.swift @@ -115,6 +115,7 @@ final class WindowCoordinator: NSObject { window.tabbingMode = .disallowed // A restored window would be a second project in this process. window.isRestorable = false + if let content = window.contentView { PointerPolicy.claimCursorUpdates(of: content) } } // MARK: Traffic lights