8
8
import Foundation
9
9
import Cocoa
10
10
import AppKit
11
+ import SwiftUI
11
12
12
13
class AppDelegate : NSObject , NSApplicationDelegate {
13
14
private var flagsMonitor : Any ?
15
+ private var permissionsService = PermissionsService . shared
14
16
15
17
func applicationDidFinishLaunching( _ notification: Notification ) {
16
- let options : NSDictionary = [ kAXTrustedCheckOptionPrompt. takeRetainedValue ( ) as NSString : true ]
17
- let enabled = AXIsProcessTrustedWithOptions ( options)
18
- if !enabled {
19
- CGEvent ( scrollWheelEvent2Source: nil , units: . pixel, wheelCount: 2 , wheel1: 0 , wheel2: 0 , wheel3: 0 ) ? . post ( tap: CGEventTapLocation . cghidEventTap)
20
- UserDefaults . standard. setValue ( true , forKey: " needsPermission " )
21
- return
22
- } else {
23
- UserDefaults . standard. setValue ( false , forKey: " needsPermission " )
24
- }
25
-
18
+ PermissionsService . acquireAccessibilityPrivileges ( )
19
+ permissionsService. pollAccessibilityPrivileges ( onTrusted: self . scrollMonitor)
20
+ }
21
+
22
+ func scrollMonitor( ) {
26
23
var mouseMonitor : Any ?
27
24
var initialPos : CGPoint ?
28
25
var lastPos : CGPoint ?
29
26
var lastDelta : CGPoint ?
27
+ var stillPressed = true
30
28
flagsMonitor = NSEvent . addGlobalMonitorForEvents ( matching: . flagsChanged) { event in
31
29
if let mouseMonitor = mouseMonitor {
32
30
NSEvent . removeMonitor ( mouseMonitor)
@@ -36,55 +34,67 @@ class AppDelegate: NSObject, NSApplicationDelegate {
36
34
var all = self . allModifiers ( )
37
35
all. remove ( self . getModifierFlag ( ) )
38
36
if event. modifierFlags. contains ( self . getModifierFlag ( ) ) && event. modifierFlags. intersection ( all) . isEmpty {
39
- let scrollSpeedAny = UserDefaults . standard
40
- . object ( forKey: " scrollSpeed " )
41
- let scrollSpeed = scrollSpeedAny != nil ? scrollSpeedAny as! CGFloat : 20.0
37
+ stillPressed = true
42
38
43
- let naturalScrollingAny = UserDefaults . standard. object ( forKey: " naturalScrolling " )
44
- let naturalScrolling = naturalScrollingAny != nil ? naturalScrollingAny as! Bool : true
45
- let xEnabledAny = UserDefaults . standard. object ( forKey: " xEnabled " )
46
- let xEnabled = xEnabledAny != nil ? xEnabledAny as! Bool : true
47
- let yEnabledAny = UserDefaults . standard. object ( forKey: " yEnabled " )
48
- let yEnabled = yEnabledAny != nil ? yEnabledAny as! Bool : true
39
+ let triggerDelayAny = UserDefaults . standard
40
+ . object ( forKey: " triggerDelay " )
41
+ let triggerDelay = triggerDelayAny != nil ? triggerDelayAny as! Double : 0.0
49
42
50
- initialPos = CGEvent ( source: nil ) !. location
51
- mouseMonitor = NSEvent . addGlobalMonitorForEvents ( matching: . mouseMoved) { _ in
52
- let pos = CGEvent ( source: nil ) !. location
53
- if let lastPos = lastPos {
54
- let delta = CGPoint ( x: lastPos. x - pos. x, y: lastPos. y - pos. y)
55
- if delta == CGPoint ( ) {
56
- return
57
- }
58
- var scroll = true
59
- if let lastDelta = lastDelta {
60
- if delta == CGPoint ( x: - lastDelta. x, y: - lastDelta. y) {
61
- scroll = false
62
- }
63
- }
64
- if scroll {
65
- var x : Int32 = 0
66
- var y : Int32 = 0
67
-
68
- if xEnabled {
69
- x = Int32 ( delta. x * scrollSpeed)
43
+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + ( triggerDelay / 1000.0 ) ) {
44
+ if ( stillPressed == false ) {
45
+ return
46
+ }
47
+ let scrollSpeedAny = UserDefaults . standard
48
+ . object ( forKey: " scrollSpeed " )
49
+ let scrollSpeed = scrollSpeedAny != nil ? scrollSpeedAny as! CGFloat : 20.0
50
+
51
+ let naturalScrollingAny = UserDefaults . standard. object ( forKey: " naturalScrolling " )
52
+ let naturalScrolling = naturalScrollingAny != nil ? naturalScrollingAny as! Bool : true
53
+ let xEnabledAny = UserDefaults . standard. object ( forKey: " xEnabled " )
54
+ let xEnabled = xEnabledAny != nil ? xEnabledAny as! Bool : true
55
+ let yEnabledAny = UserDefaults . standard. object ( forKey: " yEnabled " )
56
+ let yEnabled = yEnabledAny != nil ? yEnabledAny as! Bool : true
57
+
58
+ initialPos = CGEvent ( source: nil ) !. location
59
+ mouseMonitor = NSEvent . addGlobalMonitorForEvents ( matching: . mouseMoved) { _ in
60
+ let pos = CGEvent ( source: nil ) !. location
61
+ if let lastPos = lastPos {
62
+ let delta = CGPoint ( x: lastPos. x - pos. x, y: lastPos. y - pos. y)
63
+ if delta == CGPoint ( ) {
64
+ return
70
65
}
71
- if yEnabled {
72
- y = Int32 ( delta. y * scrollSpeed)
66
+ var scroll = true
67
+ if let lastDelta = lastDelta {
68
+ if delta == CGPoint ( x: - lastDelta. x, y: - lastDelta. y) {
69
+ scroll = false
70
+ }
73
71
}
74
-
75
- if naturalScrolling {
76
- x = - x
77
- y = - y
72
+ if scroll {
73
+ var x : Int32 = 0
74
+ var y : Int32 = 0
75
+
76
+ if xEnabled {
77
+ x = Int32 ( delta. x * scrollSpeed)
78
+ }
79
+ if yEnabled {
80
+ y = Int32 ( delta. y * scrollSpeed)
81
+ }
82
+
83
+ if naturalScrolling {
84
+ x = - x
85
+ y = - y
86
+ }
87
+
88
+ let scrollEvent = CGEvent ( scrollWheelEvent2Source: nil , units: . pixel, wheelCount: 2 , wheel1: y, wheel2: x, wheel3: 0 )
89
+ scrollEvent? . post ( tap: CGEventTapLocation . cghidEventTap)
78
90
}
79
-
80
- let scrollEvent = CGEvent ( scrollWheelEvent2Source: nil , units: . pixel, wheelCount: 2 , wheel1: y, wheel2: x, wheel3: 0 )
81
- scrollEvent? . post ( tap: CGEventTapLocation . cghidEventTap)
91
+ lastDelta= delta
82
92
}
83
- lastDelta = delta
93
+ lastPos = pos
84
94
}
85
- lastPos = pos
86
95
}
87
96
} else {
97
+ stillPressed = false
88
98
if initialPos != nil && lastPos != nil {
89
99
let mouseEvent = CGEvent ( mouseEventSource: nil , mouseType: CGEventType . mouseMoved, mouseCursorPosition: initialPos!, mouseButton: CGMouseButton . left)
90
100
mouseEvent? . post ( tap: CGEventTapLocation . cghidEventTap)
0 commit comments