@@ -20,45 +20,49 @@ struct ContentView: View {
2020}
2121
2222struct DraggableView : NSViewRepresentable {
23+ class Coordinator : NSObject {
24+ @objc func handlePanGesture( _ gesture: NSPanGestureRecognizer ) {
25+ guard let window = gesture. view? . window, let event = NSApp . currentEvent else { return }
26+
27+ switch gesture. state {
28+ case . began, . changed:
29+ window. performDrag ( with: event)
30+ default :
31+ break
32+ }
33+ }
34+ }
35+
2336 func makeNSView( context: Context ) -> NSView {
24- let view = DragView ( )
37+ let view = NSView ( )
2538 view. wantsLayer = true
2639 view. layer? . backgroundColor = . clear
27-
40+
2841 // Ensure the view is above others and can receive mouse events
2942 view. translatesAutoresizingMaskIntoConstraints = false
3043 view. layer? . zPosition = 999
44+
45+ let panGesture = NSPanGestureRecognizer (
46+ target: context. coordinator,
47+ action: #selector( Coordinator . handlePanGesture ( _: ) )
48+ )
49+ panGesture. allowedTouchTypes = [ . direct]
50+ view. addGestureRecognizer ( panGesture)
51+
3152 return view
3253 }
33-
54+
3455 func updateNSView( _ nsView: NSView , context: Context ) { }
35- }
3656
37- class DragView : NSView {
38- // Allow dragging the window from this view
39- override var mouseDownCanMoveWindow : Bool { true }
40-
41- override var allowsVibrancy : Bool { true }
42-
43- override func hitTest( _ point: NSPoint ) -> NSView ? {
44- if let currentEvent = NSApplication . shared. currentEvent,
45- currentEvent. type == . leftMouseDown ||
46- ( currentEvent. type == . leftMouseDragged && NSEvent . pressedMouseButtons == 1 ) {
47- return self
48- }
49- // Pass through all other events
50- return nil
51- }
52-
53- override func mouseDown( with event: NSEvent ) {
54- window? . performDrag ( with: event)
57+ func makeCoordinator( ) -> Coordinator {
58+ Coordinator ( )
5559 }
5660}
5761
5862struct VisualEffectView : NSViewRepresentable {
5963 let material : NSVisualEffectView . Material
6064 let blendingMode : NSVisualEffectView . BlendingMode
61-
65+
6266 func makeNSView( context: Context ) -> NSVisualEffectView {
6367 let visualEffectView = NSVisualEffectView ( )
6468 visualEffectView. material = material
0 commit comments