-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseTrackingModifier.swift
More file actions
85 lines (72 loc) · 1.63 KB
/
MouseTrackingModifier.swift
File metadata and controls
85 lines (72 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import SwiftUI
public struct MouseState
{
public var leftDown : Bool
public var middleDown : Bool
public var rightDown : Bool
public var position : CGPoint // in view space
}
public struct MouseScrollEvent
{
public var scrollDelta : CGFloat
public var position : CGPoint // in view space
}
public struct MouseTrackingModifier : ViewModifier
{
var onMouseStateChanged : (MouseState)->MouseEventResponse
var onMouseScroll : (MouseScrollEvent)->MouseEventResponse
public func body(content: Content) -> some View
{
content
.overlay
{
MouseTrackingView(onMouseStateChanged: onMouseStateChanged, onMouseScroll: onMouseScroll)
}
}
}
public extension View
{
func mouseTracking(_ onMouseStateChanged:@escaping (MouseState)->MouseEventResponse,onScroll:@escaping (MouseScrollEvent)->MouseEventResponse) -> some View
{
modifier(MouseTrackingModifier(onMouseStateChanged: onMouseStateChanged, onMouseScroll: onScroll))
}
func mouseTracking(_ onMouseStateChanged:@escaping (MouseState)->MouseEventResponse) -> some View
{
mouseTracking( onMouseStateChanged, onScroll:{_ in return .InheritedBehaviour } )
}
}
internal struct MouseTestView : View
{
@State var debug : String = "Hello"
@State var scroll : CGFloat = 0.0
var body: some View
{
Rectangle()
.fill(.blue)
.frame(width:300,height:300)
.overlay
{
VStack
{
Text("Scroll: \(scroll)")
Text(debug)
//.foregroundStyle(.white) // macos14
}
}
.mouseTracking
{
debug = "\($0)"
print($0)
return .InheritedBehaviour
}
onScroll:
{
scroll += $0.scrollDelta
return .EventHandled
}
}
}
#Preview
{
MouseTestView()
}