1
1
import ForceSimulation
2
2
import SwiftUI
3
3
4
- public enum GraphDragState {
5
- case node( AnyHashable )
6
- case background( start : SIMD2 < Double > )
4
+ public enum GraphDragState < NodeID : Hashable > {
5
+ case node( NodeID )
6
+ case background( SIMD2 < Double > )
7
7
}
8
8
9
9
#if !os(tvOS)
10
10
11
11
@usableFromInline
12
- struct GraphDragModifier : ViewModifier {
12
+ struct GraphDragModifier < NodeID : Hashable > : ViewModifier {
13
13
14
14
@inlinable
15
15
public var dragGesture : some Gesture {
@@ -28,18 +28,18 @@ struct GraphDragModifier: ViewModifier {
28
28
29
29
@inlinable
30
30
@State
31
- public var dragState : GraphDragState ?
31
+ public var dragState : GraphDragState < NodeID > ?
32
32
33
33
@usableFromInline
34
34
let graphProxy : GraphProxy
35
35
36
36
@usableFromInline
37
- let action : ( ( GraphDragState ? ) -> Void ) ?
37
+ let action : ( ( GraphDragState < NodeID > ? ) -> Void ) ?
38
38
39
39
@inlinable
40
40
init (
41
41
graphProxy: GraphProxy ,
42
- action: ( ( GraphDragState ? ) -> Void ) ? = nil
42
+ action: ( ( GraphDragState < NodeID > ? ) -> Void ) ? = nil
43
43
) {
44
44
self . graphProxy = graphProxy
45
45
self . action = action
@@ -62,7 +62,7 @@ struct GraphDragModifier: ViewModifier {
62
62
case . background( let start) :
63
63
let delta = value. location. simd - start
64
64
graphProxy. modelTransform. translate += delta
65
- dragState = . background( start : value. location. simd)
65
+ dragState = . background( value. location. simd)
66
66
case . none:
67
67
break
68
68
}
@@ -79,11 +79,11 @@ struct GraphDragModifier: ViewModifier {
79
79
value: DragGesture . Value
80
80
) {
81
81
if dragState == nil {
82
- if let nodeID = graphProxy. node ( at: value. startLocation) {
82
+ if let nodeID = graphProxy. node ( of : NodeID . self , at: value. startLocation) {
83
83
dragState = . node( nodeID)
84
84
graphProxy. setNodeFixation ( nodeID: nodeID, fixation: value. startLocation)
85
85
} else {
86
- dragState = . background( start : value. location. simd)
86
+ dragState = . background( value. location. simd)
87
87
}
88
88
} else {
89
89
switch dragState {
@@ -92,7 +92,7 @@ struct GraphDragModifier: ViewModifier {
92
92
case . background( let start) :
93
93
let delta = value. location. simd - start
94
94
graphProxy. modelTransform. translate += delta
95
- dragState = . background( start : value. location. simd)
95
+ dragState = . background( value. location. simd)
96
96
case . none:
97
97
break
98
98
}
@@ -105,10 +105,17 @@ struct GraphDragModifier: ViewModifier {
105
105
}
106
106
107
107
extension View {
108
+
109
+ /// Attach a drag gesture to an overlay or a background view created with ``SwiftUICore/View/graphOverlay(alignment:content:)``.
110
+ /// - Parameters:
111
+ /// - proxy: The graph proxy that provides the graph context.
112
+ /// - type: The type of the node ID. The drag gesture will look for the node ID of this type.
113
+ /// - action: The action to perform when the drag gesture changes.
108
114
@inlinable
109
- public func withGraphDragGesture(
115
+ public func withGraphDragGesture< NodeID > (
110
116
_ proxy: GraphProxy ,
111
- action: ( ( GraphDragState ? ) -> Void ) ? = nil
117
+ of type: NodeID . Type ,
118
+ action: ( ( GraphDragState < NodeID > ? ) -> Void ) ? = nil
112
119
) -> some View {
113
120
self . modifier ( GraphDragModifier ( graphProxy: proxy, action: action) )
114
121
}
0 commit comments