From 5357ce844332abe70dfe49ea1f07830901e12b18 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Thu, 29 May 2025 13:47:20 +0100 Subject: [PATCH] large rewrite of dpsettings to use an init and handle failed decodes make moveMouse() use dpsettings' values skyhigh moves faster skyhigh uses dpsettings' values extended NSScreen for safe access to main?.frame customising the width height of the mouse move rect should be possible? now just neeed a prefs windo --- DockPhobia.xcodeproj/project.pbxproj | 4 ++ DockPhobia/.DS_Store | Bin 6148 -> 6148 bytes DockPhobia/DPSettings.swift | 74 +++++++++++++++++++++++++-- DockPhobia/MouseTracker.swift | 24 ++++----- DockPhobia/SkyHigh.swift | 19 +++---- DockPhobia/Views/Preferences.swift | 8 +++ 6 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 DockPhobia/Views/Preferences.swift 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 6fce90f491344a6fc1b100ce10ea26c3a7928d3b..6a748ff3e10689153a69fb0f9434a427cb2c1b10 100644 GIT binary patch literal 6148 zcmeHKJx?1!5PcgQ$$%8QL5d=ZE2)t91v#9fqCp_hB?6x55s6h%AI?%SK4xqb7_ck2b9TDzS&pb4PPCRm$bS79Nb@2sT?7t$yJ2KtjQ|LK8Y`?CJC8~1Fn`lP|o z2L*xxL4i93!p?5TdLUczYc_41i)28IEtUr9p@lyJT87XC I1)f!b8^z|^ivR!s delta 146 zcmZoMXfc=|#>B)qu~2NHo}wr(0|Nsi1A_nqgC0W;Ljgk(Lq0>+#=_-{jJ%T#SS2QZ zVVOKRob4uK|71gUDYlxw2dd>iP7Y+3QH3y%IRf>wGK4W?GNdw;GZYtO7zQWj=N14( nfuLctAcrW+W_AvK4xo)d&Ufa?{34DVAUU9=ESn=l)-VGA;tnKo 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