@@ -8,9 +8,11 @@ class TrackpadEvents {
88 static func observe( ) {
99 observe_ ( )
1010 TrackpadEvents . toggle ( Preferences . nextWindowGesture != . disabled)
11+ ScrollwheelEvents . observe ( )
1112 }
1213
1314 static func toggle( _ enabled: Bool ) {
15+ guard enabled != shouldBeEnabled else { return }
1416 shouldBeEnabled = enabled
1517 if let eventTap {
1618 CGEvent . tapEnable ( tap: eventTap, enable: enabled)
@@ -48,27 +50,28 @@ private let handleEvent: CGEventTapCallBack = { _, type, cgEvent, _ in
4850
4951private func touchEventHandler( _ cgEvent: CGEvent ) -> Bool {
5052 guard let nsEvent = GestureDetector . convertEvent ( cgEvent) else { return false }
51- // if the finger count doesn't match, we reset tracking data, and may trigger fingersUp
5253 let touches = nsEvent. allTouches ( )
53- if touches. count == 0 { return false } // sometimes the os sends events with no touches
54+ // sometimes the os sends events with no touches; we ignore these as they could break our gesture logic
55+ if touches. count == 0 { return false }
5456 let activeTouches = touches. filter { !$0. isResting && ( $0. phase == . began || $0. phase == . moved || $0. phase == . stationary) }
57+ // Logger.error("---", "activeTouches:", activeTouches.count, "all:", touches.map { $0.phase.readable })
5558 let requiredFingers = Preferences . nextWindowGesture. isThreeFinger ( ) ? 3 : 4
56- if ( !App. app. appIsBeingUsed && touches. count != requiredFingers) || ( App . app. appIsBeingUsed && touches. count < 2 ) {
59+ // not enough fingers are down
60+ if ( !App. app. appIsBeingUsed && activeTouches. count != requiredFingers) || ( App . app. appIsBeingUsed && activeTouches. count < 2 ) {
61+ DispatchQueue . main. async { ScrollwheelEvents . toggle ( false ) }
5762 TriggerSwipeDetector . reset ( )
5863 NavigationSwipeDetector . reset ( )
59- return GestureDetector . checkForFingersUp ( activeTouches, requiredFingers)
64+ return GestureDetector . checkForFingersUp ( activeTouches. count , requiredFingers)
6065 }
61- // when the native using 3-finger swipe to shift Space, macOS will block scrolling in the background
62- // We imitate this behavior by sending a synthetic scrollWheel event
63- GestureDetector . blockOngoingScrolling ( )
64- // trigger actions if conditions are met
66+ // enough fingers are down
6567 if App . app. appIsBeingUsed {
66- if !GestureDetector. updateStartPositions ( touches, & NavigationSwipeDetector. startPositions) {
67- if let r = NavigationSwipeDetector . check ( touches) { return r }
68+ DispatchQueue . main. async { ScrollwheelEvents . toggle ( true ) }
69+ if !GestureDetector. updateStartPositions ( activeTouches, & NavigationSwipeDetector. startPositions) {
70+ if let r = NavigationSwipeDetector . check ( activeTouches) { return r }
6871 }
6972 } else {
70- if !GestureDetector. updateStartPositions ( touches , & TriggerSwipeDetector. startPositions) {
71- if let r = TriggerSwipeDetector . check ( touches ) { return r }
73+ if !GestureDetector. updateStartPositions ( activeTouches , & TriggerSwipeDetector. startPositions) {
74+ if let r = TriggerSwipeDetector . check ( activeTouches ) { return r }
7275 }
7376 }
7477 return false
@@ -84,8 +87,8 @@ class GestureDetector {
8487 return nsEvent
8588 }
8689
87- static func checkForFingersUp( _ touches : Set < NSTouch > , _ requiredFingers: Int ) -> Bool {
88- if App . app. appIsBeingUsed && touches . count < requiredFingers && App . app. shortcutIndex == Preferences . gestureIndex
90+ static func checkForFingersUp( _ fingersDown : Int , _ requiredFingers: Int ) -> Bool {
91+ if App . app. appIsBeingUsed && fingersDown < requiredFingers && App . app. shortcutIndex == Preferences . gestureIndex
8992 && !App. app. forceDoNothingOnRelease && Preferences . shortcutStyle [ App . app. shortcutIndex] == . focusOnRelease {
9093 DispatchQueue . main. async { App . app. focusTarget ( ) }
9194 return true
@@ -152,7 +155,10 @@ class TriggerSwipeDetector {
152155 }
153156 }
154157 reset ( )
155- DispatchQueue . main. async { App . app. showUiOrCycleSelection ( Preferences . gestureIndex, false ) }
158+ DispatchQueue . main. async {
159+ ScrollwheelEvents . toggle ( true )
160+ App . app. showUiOrCycleSelection ( Preferences . gestureIndex, false )
161+ }
156162 return true
157163 }
158164 return nil
@@ -174,12 +180,12 @@ class NavigationSwipeDetector {
174180
175181 static var startPositions : [ String : NSPoint ] = [ : ]
176182
177- static func check( _ touches : Set < NSTouch > ) -> Bool ? {
178- let averageDistance = GestureDetector . computeAverageDistance ( touches , startPositions)
183+ static func check( _ activeTouches : Set < NSTouch > ) -> Bool ? {
184+ let averageDistance = GestureDetector . computeAverageDistance ( activeTouches , startPositions)
179185 let ( absX, absY) = ( abs ( averageDistance. x) , abs ( averageDistance. y) )
180186 let maxIsX = absX >= absY
181187 if ( maxIsX ? absX : absY) > MIN_SWIPE_DISTANCE {
182- maxIsX ? resetX ( touches ) : resetY ( touches )
188+ maxIsX ? resetX ( activeTouches ) : resetY ( activeTouches )
183189 let direction : Direction = maxIsX ? ( averageDistance. x < 0 ? . left : . right) : ( averageDistance. y < 0 ? . down : . up)
184190 DispatchQueue . main. async { App . app. cycleSelection ( direction, allowWrap: false ) }
185191 return true
0 commit comments