-
Notifications
You must be signed in to change notification settings - Fork 56
/
AKPlugin.swift
186 lines (167 loc) · 6.15 KB
/
AKPlugin.swift
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
//
// MacPlugin.swift
// AKInterface
//
// Created by Isaac Marovitz on 13/09/2022.
//
import AppKit
import CoreGraphics
import Foundation
class AKPlugin: NSObject, Plugin {
required override init() {
}
var screenCount: Int {
NSScreen.screens.count
}
var mousePoint: CGPoint {
NSApplication.shared.windows.first?.mouseLocationOutsideOfEventStream ?? CGPoint()
}
var windowFrame: CGRect {
NSApplication.shared.windows.first?.frame ?? CGRect()
}
var isMainScreenEqualToFirst: Bool {
return NSScreen.main == NSScreen.screens.first
}
var mainScreenFrame: CGRect {
return NSScreen.main!.frame as CGRect
}
var isFullscreen: Bool {
NSApplication.shared.windows.first!.styleMask.contains(.fullScreen)
}
var cmdPressed: Bool = false
var cursorHideLevel = 0
func hideCursor() {
NSCursor.hide()
cursorHideLevel += 1
CGAssociateMouseAndMouseCursorPosition(0)
warpCursor()
}
func warpCursor() {
guard let firstScreen = NSScreen.screens.first else {return}
let frame = windowFrame
// Convert from NS coordinates to CG coordinates
CGWarpMouseCursorPosition(CGPoint(x: frame.midX, y: firstScreen.frame.height - frame.midY))
}
func unhideCursor() {
NSCursor.unhide()
cursorHideLevel -= 1
if cursorHideLevel <= 0 {
CGAssociateMouseAndMouseCursorPosition(1)
}
}
func terminateApplication() {
NSApplication.shared.terminate(self)
}
private var modifierFlag: UInt = 0
// swiftlint:disable:next function_body_length
func setupKeyboard(keyboard: @escaping (UInt16, Bool, Bool, Bool) -> Bool,
swapMode: @escaping () -> Bool) {
func checkCmd(modifier: NSEvent.ModifierFlags) -> Bool {
if modifier.contains(.command) {
self.cmdPressed = true
return true
} else if self.cmdPressed {
self.cmdPressed = false
}
return false
}
NSEvent.addLocalMonitorForEvents(matching: .keyDown, handler: { event in
if checkCmd(modifier: event.modifierFlags) {
return event
}
let consumed = keyboard(event.keyCode, true, event.isARepeat,
event.modifierFlags.contains(.control))
if consumed {
return nil
}
return event
})
NSEvent.addLocalMonitorForEvents(matching: .keyUp, handler: { event in
if checkCmd(modifier: event.modifierFlags) {
return event
}
let consumed = keyboard(event.keyCode, false, false,
event.modifierFlags.contains(.control))
if consumed {
return nil
}
return event
})
NSEvent.addLocalMonitorForEvents(matching: .flagsChanged, handler: { event in
if checkCmd(modifier: event.modifierFlags) {
return event
}
let pressed = self.modifierFlag < event.modifierFlags.rawValue
let changed = self.modifierFlag ^ event.modifierFlags.rawValue
self.modifierFlag = event.modifierFlags.rawValue
let changedFlags = NSEvent.ModifierFlags(rawValue: changed)
if pressed && changedFlags.contains(.option) {
if swapMode() {
return nil
}
return event
}
let consumed = keyboard(event.keyCode, pressed, false,
event.modifierFlags.contains(.control))
if consumed {
return nil
}
return event
})
}
func setupMouseMoved(_ mouseMoved: @escaping (CGFloat, CGFloat) -> Bool) {
let mask: NSEvent.EventTypeMask = [.leftMouseDragged, .otherMouseDragged, .rightMouseDragged]
NSEvent.addLocalMonitorForEvents(matching: mask, handler: { event in
let consumed = mouseMoved(event.deltaX, event.deltaY)
if consumed {
return nil
}
return event
})
// transpass mouse moved event when no button pressed, for traffic light button to light up
NSEvent.addLocalMonitorForEvents(matching: .mouseMoved, handler: { event in
_ = mouseMoved(event.deltaX, event.deltaY)
return event
})
}
func setupMouseButton(left: Bool, right: Bool, _ consumed: @escaping (Int, Bool) -> Bool) {
let downType: NSEvent.EventTypeMask = left ? .leftMouseDown : right ? .rightMouseDown : .otherMouseDown
let upType: NSEvent.EventTypeMask = left ? .leftMouseUp : right ? .rightMouseUp : .otherMouseUp
NSEvent.addLocalMonitorForEvents(matching: downType, handler: { event in
// For traffic light buttons when fullscreen
if event.window != NSApplication.shared.windows.first! {
return event
}
if consumed(event.buttonNumber, true) {
return nil
}
return event
})
NSEvent.addLocalMonitorForEvents(matching: upType, handler: { event in
if consumed(event.buttonNumber, false) {
return nil
}
return event
})
}
func setupScrollWheel(_ onMoved: @escaping (CGFloat, CGFloat) -> Bool) {
NSEvent.addLocalMonitorForEvents(matching: NSEvent.EventTypeMask.scrollWheel, handler: { event in
var deltaX = event.scrollingDeltaX, deltaY = event.scrollingDeltaY
if !event.hasPreciseScrollingDeltas {
deltaX *= 16
deltaY *= 16
}
let consumed = onMoved(deltaX, deltaY)
if consumed {
return nil
}
return event
})
}
func urlForApplicationWithBundleIdentifier(_ value: String) -> URL? {
NSWorkspace.shared.urlForApplication(withBundleIdentifier: value)
}
func setMenuBarVisible(_ visible: Bool) {
NSMenu.setMenuBarVisible(visible)
}
}