mirror of
https://github.com/neon443/DockPhobia.git
synced 2026-03-11 14:56:16 +00:00
fix the dock move triggers
maybe random dock movements in the future?
This commit is contained in:
@@ -86,10 +86,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
|
|
||||||
@objc func didTapStart() {
|
@objc func didTapStart() {
|
||||||
if mouseTracker.running {
|
if mouseTracker.running {
|
||||||
mouseTracker.removeMonitor()
|
mouseTracker.stop()
|
||||||
changeMenuIcon(running: false)
|
changeMenuIcon(running: false)
|
||||||
} else {
|
} else {
|
||||||
mouseTracker.addMonitor()
|
mouseTracker.start()
|
||||||
changeMenuIcon(running: true)
|
changeMenuIcon(running: true)
|
||||||
}
|
}
|
||||||
setupMenus()
|
setupMenus()
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ struct Screen {
|
|||||||
|
|
||||||
enum DockSide: Int, RawRepresentable {
|
enum DockSide: Int, RawRepresentable {
|
||||||
case left
|
case left
|
||||||
case right
|
|
||||||
case bottom
|
case bottom
|
||||||
|
case right
|
||||||
|
|
||||||
public typealias RawValue = String
|
public typealias RawValue = String
|
||||||
|
|
||||||
@@ -31,6 +31,12 @@ enum DockSide: Int, RawRepresentable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Random Dock Side
|
||||||
|
/// - will return a random Dock Side when calling DockSide()
|
||||||
|
public init() {
|
||||||
|
self = DockSide(rawValue: Int.random(in: 1...3))!
|
||||||
|
}
|
||||||
|
|
||||||
public init?(rawValue: String) {
|
public init?(rawValue: String) {
|
||||||
switch rawValue {
|
switch rawValue {
|
||||||
case "left":
|
case "left":
|
||||||
@@ -43,6 +49,19 @@ enum DockSide: Int, RawRepresentable {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public init?(rawValue: Int) {
|
||||||
|
switch rawValue {
|
||||||
|
case 1:
|
||||||
|
self = .left
|
||||||
|
case 2:
|
||||||
|
self = .bottom
|
||||||
|
case 3:
|
||||||
|
self = .right
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MouseTracker {
|
class MouseTracker {
|
||||||
@@ -52,7 +71,10 @@ class MouseTracker {
|
|||||||
|
|
||||||
var running: Bool = false
|
var running: Bool = false
|
||||||
|
|
||||||
|
var currentDockSide: DockSide
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
print(DockSide())
|
||||||
if let screen = NSScreen.main {
|
if let screen = NSScreen.main {
|
||||||
let rect = screen.frame
|
let rect = screen.frame
|
||||||
self.screen = Screen(
|
self.screen = Screen(
|
||||||
@@ -63,6 +85,9 @@ class MouseTracker {
|
|||||||
} else {
|
} else {
|
||||||
fatalError("no screen wtf???")
|
fatalError("no screen wtf???")
|
||||||
}
|
}
|
||||||
|
self.currentDockSide = .bottom
|
||||||
|
moveDock(.bottom)
|
||||||
|
start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkMouse(_ event: NSEvent) {
|
func checkMouse(_ event: NSEvent) {
|
||||||
@@ -71,15 +96,9 @@ class MouseTracker {
|
|||||||
#if DEBUG
|
#if DEBUG
|
||||||
print(location)
|
print(location)
|
||||||
#endif
|
#endif
|
||||||
if location.y > 1000 {
|
|
||||||
if location.x < screen.width/2 {
|
switch currentDockSide {
|
||||||
moveDock(.right)
|
case .left:
|
||||||
return
|
|
||||||
} else {
|
|
||||||
moveDock(.left)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if location.x < 100 {
|
if location.x < 100 {
|
||||||
if location.y < screen.height/2 {
|
if location.y < screen.height/2 {
|
||||||
moveDock(.bottom)
|
moveDock(.bottom)
|
||||||
@@ -89,6 +108,17 @@ class MouseTracker {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case .bottom:
|
||||||
|
if location.y > 1000 {
|
||||||
|
if location.x < screen.width/2 {
|
||||||
|
moveDock(.right)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
moveDock(.left)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case .right:
|
||||||
if location.x > 1600 {
|
if location.x > 1600 {
|
||||||
if location.y < screen.height/2 {
|
if location.y < screen.height/2 {
|
||||||
moveDock(.bottom)
|
moveDock(.bottom)
|
||||||
@@ -99,14 +129,15 @@ class MouseTracker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func addMonitor() {
|
func start() {
|
||||||
self.monitor = NSEvent.addGlobalMonitorForEvents(matching: .mouseMoved, handler: checkMouse)
|
self.monitor = NSEvent.addGlobalMonitorForEvents(matching: .mouseMoved, handler: checkMouse)
|
||||||
self.running = true
|
self.running = true
|
||||||
print("started tracking")
|
print("started tracking")
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeMonitor() {
|
func stop() {
|
||||||
if let monitor = monitor {
|
if let monitor = monitor {
|
||||||
NSEvent.removeMonitor(monitor)
|
NSEvent.removeMonitor(monitor)
|
||||||
self.running = false
|
self.running = false
|
||||||
@@ -124,9 +155,11 @@ class MouseTracker {
|
|||||||
end tell
|
end tell
|
||||||
"""
|
"""
|
||||||
applescript(script)
|
applescript(script)
|
||||||
|
currentDockSide = toSide
|
||||||
}
|
}
|
||||||
|
|
||||||
func applescript(_ script: String) {
|
@discardableResult
|
||||||
|
func applescript(_ script: String) -> String? {
|
||||||
var error: NSDictionary?
|
var error: NSDictionary?
|
||||||
if let scriptObject = NSAppleScript(source: script) {
|
if let scriptObject = NSAppleScript(source: script) {
|
||||||
scriptObject.executeAndReturnError(&error)
|
scriptObject.executeAndReturnError(&error)
|
||||||
@@ -134,6 +167,7 @@ class MouseTracker {
|
|||||||
print(error as Any)
|
print(error as Any)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user