-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGestureMapper.swift
More file actions
276 lines (232 loc) · 10.4 KB
/
Copy pathGestureMapper.swift
File metadata and controls
276 lines (232 loc) · 10.4 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import CoreGraphics
import Foundation
final class GestureMapper {
private let config: AppConfig
private let logger: DebugLogger
private let liveEventDebug: Bool
private let mxMasterRegistry: MXMasterDeviceRegistry
private let scrollTransformer = ScrollTransformer()
private var gestureState = GestureState()
var onGestureActivityChanged: ((Bool) -> Void)?
init(config: AppConfig, logger: DebugLogger, liveEventDebug: Bool, mxMasterRegistry: MXMasterDeviceRegistry) {
self.config = config
self.logger = logger
self.liveEventDebug = liveEventDebug
self.mxMasterRegistry = mxMasterRegistry
}
var eventTypes: [CGEventType] {
if liveEventDebug {
return [
.leftMouseDown,
.leftMouseUp,
.rightMouseDown,
.rightMouseUp,
.otherMouseDown,
.otherMouseUp,
.mouseMoved,
.leftMouseDragged,
.rightMouseDragged,
.otherMouseDragged,
.scrollWheel
]
}
return primaryEventTypes + movementEventTypes
}
var primaryEventTypes: [CGEventType] {
var types: [CGEventType] = []
if config.buttons.gesture != nil || config.buttons.back != nil || config.buttons.forward != nil {
types.append(contentsOf: [.otherMouseDown, .otherMouseUp])
}
if config.scrollTransformMode != .disabled {
types.append(.scrollWheel)
}
return types
}
var movementEventTypes: [CGEventType] {
config.buttons.gesture == nil ? [] : [.mouseMoved, .otherMouseDragged]
}
func handle(type: CGEventType, event: CGEvent) -> Bool {
let suppressed: Bool
switch type {
case .leftMouseDown, .rightMouseDown, .otherMouseDown:
suppressed = handleButtonDown(event: event)
case .leftMouseUp, .rightMouseUp, .otherMouseUp:
suppressed = handleButtonUp(event: event)
case .mouseMoved, .leftMouseDragged, .rightMouseDragged, .otherMouseDragged:
suppressed = handleMovement(type: type, event: event)
case .scrollWheel:
suppressed = handleScroll(event: event)
default:
suppressed = false
}
if liveEventDebug {
logEvent(type: type, event: event, suppressed: suppressed)
}
return suppressed
}
func cancelGestureForEventTapInterruption() {
guard gestureState.isActive else {
return
}
gestureState.end()
onGestureActivityChanged?(false)
}
#if DEBUG
var gestureStateForTesting: GestureState {
gestureState
}
#endif
private func handleButtonDown(event: CGEvent) -> Bool {
let button = Int(event.getIntegerValueField(.mouseEventButtonNumber))
if gestureState.isActive {
_ = cancelGestureIfStale(event: event)
}
switch GestureState.buttonDownDecision(button: button, buttons: config.buttons, gestureActive: gestureState.isActive) {
case .beginGesture:
gestureState.begin(at: event.location, timestamp: event.timestamp)
onGestureActivityChanged?(true)
logger.log("gesture start button=\(button) startLocation=\(event.location.compactDescription)")
return true
case .suppressSideButton:
logger.log("side button suppressed during gesture hold button=\(button)")
return true
case .commandBracket(let left):
logger.log("\(left ? "back" : "forward") button=\(button) -> cmd+\(left ? "[" : "]")")
KeySender.sendCommandBracket(left: left)
return true
case .passThrough:
return false
}
}
private func handleButtonUp(event: CGEvent) -> Bool {
let button = Int(event.getIntegerValueField(.mouseEventButtonNumber))
let staleGestureCancelled = gestureState.isActive && cancelGestureIfStale(event: event)
if matches(button, config.buttons.gesture) {
if !staleGestureCancelled {
logger.log("gesture end button=\(button) final accumulatedDx=\(format(gestureState.accumulatedDx)) accumulatedDy=\(format(gestureState.accumulatedDy)) accumulatedScrollX=\(format(gestureState.accumulatedScrollX)) accumulatedScrollY=\(format(gestureState.accumulatedScrollY))")
gestureState.end()
onGestureActivityChanged?(false)
}
return true
}
if matchesSideButton(button) {
return true
}
return false
}
private func handleMovement(type: CGEventType, event: CGEvent) -> Bool {
guard gestureState.isActive else {
return false
}
guard !cancelGestureIfStale(event: event) else {
return false
}
let currentLocation = event.location
let previousLocation = gestureState.lastLocation ?? currentLocation
let stepDx = Double(currentLocation.x - previousLocation.x)
let rawStepDy = Double(currentLocation.y - previousLocation.y)
let stepDy = config.invertY ? -rawStepDy : rawStepDy
gestureState.addMovement(to: currentLocation, invertY: config.invertY)
logger.log("gesture move type=\(type.label) stepDx=\(format(stepDx)) stepDy=\(format(stepDy)) accumulatedDx=\(format(gestureState.accumulatedDx)) accumulatedDy=\(format(gestureState.accumulatedDy))")
evaluateGesture(source: "pointer")
return true
}
private func handleScroll(event: CGEvent) -> Bool {
guard gestureState.isActive else {
transformInactiveScroll(event: event)
return false
}
guard !cancelGestureIfStale(event: event) else {
transformInactiveScroll(event: event)
return false
}
let axis1 = Double(event.getIntegerValueField(.scrollWheelEventDeltaAxis1))
let axis2 = Double(event.getIntegerValueField(.scrollWheelEventDeltaAxis2))
gestureState.addScroll(axis1: axis1, axis2: axis2)
logger.log("gesture scroll axis1=\(format(axis1)) axis2=\(format(axis2)) accumulatedScrollX=\(format(gestureState.accumulatedScrollX)) accumulatedScrollY=\(format(gestureState.accumulatedScrollY))")
evaluateGesture(source: "scroll")
return false
}
private func cancelGestureIfStale(event: CGEvent) -> Bool {
guard gestureState.isStale(at: event.timestamp) else {
return false
}
logger.log("stale gesture cancelled")
gestureState.end()
onGestureActivityChanged?(false)
return true
}
private func transformInactiveScroll(event: CGEvent) {
if let result = scrollTransformer.apply(
to: event,
config: config,
mxMasterRegistry: mxMasterRegistry,
captureDebug: liveEventDebug
) {
logger.log(result.debugDescription(prefix: "scroll transform"))
}
}
private func evaluateGesture(source: String) {
guard let action = gestureState.consumeAction(
threshold: config.threshold,
downAction: config.downAction,
invertDesktopDirection: config.invertDesktopGestureDirection
) else {
return
}
switch action {
case .controlArrow(let direction):
logger.log("gesture action=ctrl+\(direction.label) source=\(source) invertDesktopGestureDirection=\(config.invertDesktopGestureDirection) accumulatedDx=\(format(gestureState.accumulatedDx)) accumulatedDy=\(format(gestureState.accumulatedDy)) accumulatedScrollX=\(format(gestureState.accumulatedScrollX)) accumulatedScrollY=\(format(gestureState.accumulatedScrollY))")
KeySender.sendControlArrow(direction, backend: config.actionBackend, logger: logger)
case .disabledDown:
logger.log("gesture down source=\(source) disabled accumulatedDx=\(format(gestureState.accumulatedDx)) accumulatedDy=\(format(gestureState.accumulatedDy)) accumulatedScrollX=\(format(gestureState.accumulatedScrollX)) accumulatedScrollY=\(format(gestureState.accumulatedScrollY))")
}
}
private func logEvent(type: CGEventType, event: CGEvent, suppressed: Bool) {
var fields = [
"event type=\(type.label)",
"locationX=\(format(Double(event.location.x)))",
"locationY=\(format(Double(event.location.y)))",
"gestureActive=\(gestureState.isActive)",
"gestureConsumed=\(gestureState.isConsumed)",
"suppressed=\(suppressed)"
]
if type.isMouseButtonEvent {
let button = event.getIntegerValueField(.mouseEventButtonNumber)
fields.append("button=\(button)")
}
if type.isMouseMoveEvent {
let deltaX = event.getIntegerValueField(.mouseEventDeltaX)
let deltaY = event.getIntegerValueField(.mouseEventDeltaY)
fields.append("deltaX=\(deltaX)")
fields.append("deltaY=\(deltaY)")
fields.append("accumulatedDx=\(format(gestureState.accumulatedDx))")
fields.append("accumulatedDy=\(format(gestureState.accumulatedDy))")
}
if type == .scrollWheel {
let axis1 = event.getIntegerValueField(.scrollWheelEventDeltaAxis1)
let axis2 = event.getIntegerValueField(.scrollWheelEventDeltaAxis2)
let pointAxis1 = event.getIntegerValueField(.scrollWheelEventPointDeltaAxis1)
let pointAxis2 = event.getIntegerValueField(.scrollWheelEventPointDeltaAxis2)
fields.append("scrollAxis1=\(axis1)")
fields.append("scrollAxis2=\(axis2)")
fields.append("scrollPointAxis1=\(pointAxis1)")
fields.append("scrollPointAxis2=\(pointAxis2)")
fields.append("accumulatedScrollX=\(format(gestureState.accumulatedScrollX))")
fields.append("accumulatedScrollY=\(format(gestureState.accumulatedScrollY))")
}
logger.log(fields.joined(separator: " "))
}
private func format(_ value: Double) -> String {
String(format: "%.1f", value)
}
private func matches(_ button: Int, _ configuredButton: Int?) -> Bool {
guard let configuredButton else {
return false
}
return button == configuredButton
}
private func matchesSideButton(_ button: Int) -> Bool {
matches(button, config.buttons.back) || matches(button, config.buttons.forward)
}
}