diff --git a/DockPhobia.xcodeproj/project.pbxproj b/DockPhobia.xcodeproj/project.pbxproj index bb73c21..2f352fa 100644 --- a/DockPhobia.xcodeproj/project.pbxproj +++ b/DockPhobia.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ A98C20C62DE614180008D61C /* DPSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = A98C20C52DE614180008D61C /* DPSettings.swift */; }; A9C9AF812DE7776A0039D7A5 /* DockSide.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C9AF802DE777530039D7A5 /* DockSide.swift */; }; A9C9AF832DE77CB70039D7A5 /* SkyHigh.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C9AF822DE77CB70039D7A5 /* SkyHigh.swift */; }; + A9C9B0682DE888B20039D7A5 /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C9B0672DE888B20039D7A5 /* Preferences.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -27,6 +28,7 @@ A98C20C52DE614180008D61C /* DPSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DPSettings.swift; sourceTree = ""; }; A9C9AF802DE777530039D7A5 /* DockSide.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DockSide.swift; sourceTree = ""; }; A9C9AF822DE77CB70039D7A5 /* SkyHigh.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkyHigh.swift; sourceTree = ""; }; + A9C9B0672DE888B20039D7A5 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -52,6 +54,7 @@ isa = PBXGroup; children = ( A94BEC0E2DE23E8500D4811D /* MainMenu.xib */, + A9C9B0672DE888B20039D7A5 /* Preferences.swift */, ); path = Views; sourceTree = ""; @@ -163,6 +166,7 @@ files = ( A98C20C62DE614180008D61C /* DPSettings.swift in Sources */, A9C9AF832DE77CB70039D7A5 /* SkyHigh.swift in Sources */, + A9C9B0682DE888B20039D7A5 /* Preferences.swift in Sources */, A966B4F82DE0852900C721A5 /* MouseTracker.swift in Sources */, A966B4F42DE0842500C721A5 /* AppDelegate.swift in Sources */, A9C9AF812DE7776A0039D7A5 /* DockSide.swift in Sources */, diff --git a/DockPhobia/.DS_Store b/DockPhobia/.DS_Store index 6fce90f..6a748ff 100644 Binary files a/DockPhobia/.DS_Store and b/DockPhobia/.DS_Store differ diff --git a/DockPhobia/DPSettings.swift b/DockPhobia/DPSettings.swift index 5b461b7..389cfe6 100644 --- a/DockPhobia/DPSettings.swift +++ b/DockPhobia/DPSettings.swift @@ -8,10 +8,78 @@ import Foundation import AppKit +extension NSScreen { + static var mainFrame: CGRect { + main?.frame ?? CGRect(x: 0, y: 0, width: 1920, height: 1080) + } + static var mainFrameWidth: CGFloat { + main?.frame.width ?? 1920 + } + static var mainFrameHeight: CGFloat { + main?.frame.height ?? 1080 + } +} + struct DPSettings: Codable { - var dockMoves: Int = 0 - var checkFullscreen: Bool = false - var moveMouseInstead: Bool = false + var dockMoves: Int + var checkFullscreen: Bool + var moveMouseInstead: Bool + + var mouseInsetLeading: CGFloat { + NSScreen.mainFrameWidth*insetHorizontal + } + var mouseInsetBottom: CGFloat { + NSScreen.mainFrameHeight*insetVertical + } + var mouseInsetTop: CGFloat { + NSScreen.mainFrameHeight*(1-(2*insetVertical)) + } + var mouseInsetTrailing: CGFloat { + NSScreen.mainFrameWidth*(1-(2*insetHorizontal)) + } + + var insetHorizontal: CGFloat + var insetVertical: CGFloat + + var mouseMoveRect: CGRect { + return CGRect( + x: mouseInsetLeading, + y: mouseInsetBottom, + width: mouseInsetTrailing, + height: mouseInsetTop + ) + } + + init( + dockMoves: Int = 0, + checkFullscreen: Bool = false, + moveMouseInstead: Bool = false, + insetHorizontal: CGFloat = 0.05, + insetVertical: CGFloat = 0.1 + ) { + self.dockMoves = dockMoves + self.checkFullscreen = checkFullscreen + self.moveMouseInstead = moveMouseInstead + self.insetHorizontal = insetHorizontal + self.insetVertical = insetVertical + } + + init(from decoder: any Decoder) throws { + let defaults = DPSettings() + let container = try decoder.container(keyedBy: CodingKeys.self) + dockMoves = try container.decodeIfPresent(Int.self, forKey: .dockMoves) + ?? defaults.dockMoves + + checkFullscreen = try container.decodeIfPresent(Bool.self, forKey: .checkFullscreen) + ?? defaults.checkFullscreen + moveMouseInstead = try container.decodeIfPresent(Bool.self, forKey: .moveMouseInstead) + ?? defaults.moveMouseInstead + + insetHorizontal = try container.decodeIfPresent(CGFloat.self, forKey: .insetHorizontal) + ?? defaults.insetHorizontal + insetVertical = try container.decodeIfPresent(CGFloat.self, forKey: .insetVertical) + ?? defaults.insetVertical + } } class DPSettingsModel { diff --git a/DockPhobia/MouseTracker.swift b/DockPhobia/MouseTracker.swift index 612b39e..2bec440 100644 --- a/DockPhobia/MouseTracker.swift +++ b/DockPhobia/MouseTracker.swift @@ -23,22 +23,19 @@ class MouseTracker { var dockHeight: CGFloat = 0 var settings: DPSettingsModel - var skyHigh = SkyHigh() + var skyHigh: SkyHigh init(settings: DPSettingsModel) { print(DockSide()) - if let screen = NSScreen.main { - let rect = screen.frame - self.screen = Screen( - width: rect.width, - height: rect.height - ) - print(self.screen) - } else { - fatalError("no screen wtf???") - } + + self.screen = Screen( + width: NSScreen.mainFrameWidth, + height: NSScreen.mainFrameHeight + ) + print(self.screen) self.settings = settings + self.skyHigh = SkyHigh(settings: settings) self.currentDockSide = .left moveDock(.bottom) @@ -138,9 +135,9 @@ class MouseTracker { } func moveMouse() { - let rangeW = screen.width*0.1...screen.width*0.9 + let rangeW = settings.settings.mouseInsetLeading...settings.settings.mouseInsetTrailing let posX = CGFloat.random(in: rangeW) - let rangeH = screen.height*0.1...screen.height*0.9 + let rangeH = settings.settings.mouseInsetBottom...settings.settings.mouseInsetTop let posY = CGFloat.random(in: rangeH) CGDisplayMoveCursorToPoint(0, CGPoint(x: posX, y: posY)) } @@ -173,7 +170,6 @@ class MouseTracker { } func getDockSize() { - guard let screen = NSScreen.main?.frame else { fatalError() } guard let screenVisible = NSScreen.main?.visibleFrame else { fatalError() } self.dockHeight = screen.height - screenVisible.height } diff --git a/DockPhobia/SkyHigh.swift b/DockPhobia/SkyHigh.swift index 9db894a..b47a6ee 100644 --- a/DockPhobia/SkyHigh.swift +++ b/DockPhobia/SkyHigh.swift @@ -12,16 +12,13 @@ class SkyHigh { private var window: NSWindow private var x = 1 private var timer: Timer? + var settings: DPSettingsModel - init() { - guard let screen = NSScreen.main?.frame else { fatalError() } + init(settings: DPSettingsModel) { + self.settings = settings + self.window = NSWindow( - contentRect: CGRect( - x: screen.width*0.05, - y: screen.height*0.1, - width: screen.width*0.9, - height: screen.height*0.8 - ), + contentRect: settings.settings.mouseMoveRect, styleMask: .borderless, backing: .buffered, defer: false @@ -31,12 +28,12 @@ class SkyHigh { window.level = NSWindow.Level.statusBar + 1 window.ignoresMouseEvents = true window.hasShadow = true + window.collectionBehavior = NSWindow.CollectionBehavior.canJoinAllSpaces.union(.stationary) window.makeKeyAndOrderFront(nil) - window.setFrameOrigin(NSPoint(x: screen.width*0.05, y: screen.height*0.1)) } func move() { - x = 1 + x = 5 timer?.invalidate() timer = Timer(timeInterval: 0.01, repeats: true) { [weak self] _ in guard let self = self else { return } @@ -45,7 +42,7 @@ class SkyHigh { return } self.window.setFrameOrigin(NSPoint(x: 1000-self.x, y: 1000-self.x)) - self.x += 1 + self.x += 5 } RunLoop.current.add(timer!, forMode: .common) } diff --git a/DockPhobia/Views/Preferences.swift b/DockPhobia/Views/Preferences.swift new file mode 100644 index 0000000..48b6eb6 --- /dev/null +++ b/DockPhobia/Views/Preferences.swift @@ -0,0 +1,8 @@ +// +// Preferences.swift +// DockPhobia +// +// Created by neon443 on 29/05/2025. +// + +import Foundation