import AppKit import SwiftUI /// An AppKit drag surface for the non-activating panel. SwiftUI's hosting view consumes mouse /// events, and `performDrag` is unreliable while the app is inactive, so the view moves the /// window itself from the screen-space mouse delta. struct WindowDragArea: NSViewRepresentable { func makeNSView(context: Context) -> NSView { DragView() } func updateNSView(_ nsView: NSView, context: Context) {} } private final class DragView: NSView { private var startMouse = NSPoint.zero private var startOrigin = NSPoint.zero override var mouseDownCanMoveWindow: Bool { false } override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true } override func mouseDown(with event: NSEvent) { guard let window else { return } startMouse = NSEvent.mouseLocation startOrigin = window.frame.origin NSCursor.closedHand.push() } override func mouseDragged(with event: NSEvent) { guard let window else { return } let mouse = NSEvent.mouseLocation window.setFrameOrigin( NSPoint( x: startOrigin.x + mouse.x - startMouse.x, y: startOrigin.y + mouse.y - startMouse.y ) ) } override func mouseUp(with event: NSEvent) { NSCursor.pop() } override func resetCursorRects() { addCursorRect(bounds, cursor: .openHand) } }