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
This commit is contained in:
neon443
2025-05-29 13:47:20 +01:00
parent 853fc2a2db
commit 5357ce8443
6 changed files with 101 additions and 28 deletions

View File

@@ -14,6 +14,7 @@
A98C20C62DE614180008D61C /* DPSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = A98C20C52DE614180008D61C /* DPSettings.swift */; }; A98C20C62DE614180008D61C /* DPSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = A98C20C52DE614180008D61C /* DPSettings.swift */; };
A9C9AF812DE7776A0039D7A5 /* DockSide.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C9AF802DE777530039D7A5 /* DockSide.swift */; }; A9C9AF812DE7776A0039D7A5 /* DockSide.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C9AF802DE777530039D7A5 /* DockSide.swift */; };
A9C9AF832DE77CB70039D7A5 /* SkyHigh.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C9AF822DE77CB70039D7A5 /* SkyHigh.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 */ /* End PBXBuildFile section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
@@ -27,6 +28,7 @@
A98C20C52DE614180008D61C /* DPSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DPSettings.swift; sourceTree = "<group>"; }; A98C20C52DE614180008D61C /* DPSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DPSettings.swift; sourceTree = "<group>"; };
A9C9AF802DE777530039D7A5 /* DockSide.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DockSide.swift; sourceTree = "<group>"; }; A9C9AF802DE777530039D7A5 /* DockSide.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DockSide.swift; sourceTree = "<group>"; };
A9C9AF822DE77CB70039D7A5 /* SkyHigh.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkyHigh.swift; sourceTree = "<group>"; }; A9C9AF822DE77CB70039D7A5 /* SkyHigh.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkyHigh.swift; sourceTree = "<group>"; };
A9C9B0672DE888B20039D7A5 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@@ -52,6 +54,7 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
A94BEC0E2DE23E8500D4811D /* MainMenu.xib */, A94BEC0E2DE23E8500D4811D /* MainMenu.xib */,
A9C9B0672DE888B20039D7A5 /* Preferences.swift */,
); );
path = Views; path = Views;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -163,6 +166,7 @@
files = ( files = (
A98C20C62DE614180008D61C /* DPSettings.swift in Sources */, A98C20C62DE614180008D61C /* DPSettings.swift in Sources */,
A9C9AF832DE77CB70039D7A5 /* SkyHigh.swift in Sources */, A9C9AF832DE77CB70039D7A5 /* SkyHigh.swift in Sources */,
A9C9B0682DE888B20039D7A5 /* Preferences.swift in Sources */,
A966B4F82DE0852900C721A5 /* MouseTracker.swift in Sources */, A966B4F82DE0852900C721A5 /* MouseTracker.swift in Sources */,
A966B4F42DE0842500C721A5 /* AppDelegate.swift in Sources */, A966B4F42DE0842500C721A5 /* AppDelegate.swift in Sources */,
A9C9AF812DE7776A0039D7A5 /* DockSide.swift in Sources */, A9C9AF812DE7776A0039D7A5 /* DockSide.swift in Sources */,

BIN
DockPhobia/.DS_Store vendored
View File

Binary file not shown.

View File

@@ -8,10 +8,78 @@
import Foundation import Foundation
import AppKit 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 { struct DPSettings: Codable {
var dockMoves: Int = 0 var dockMoves: Int
var checkFullscreen: Bool = false var checkFullscreen: Bool
var moveMouseInstead: Bool = false 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 { class DPSettingsModel {

View File

@@ -23,22 +23,19 @@ class MouseTracker {
var dockHeight: CGFloat = 0 var dockHeight: CGFloat = 0
var settings: DPSettingsModel var settings: DPSettingsModel
var skyHigh = SkyHigh() var skyHigh: SkyHigh
init(settings: DPSettingsModel) { init(settings: DPSettingsModel) {
print(DockSide()) print(DockSide())
if let screen = NSScreen.main {
let rect = screen.frame self.screen = Screen(
self.screen = Screen( width: NSScreen.mainFrameWidth,
width: rect.width, height: NSScreen.mainFrameHeight
height: rect.height )
) print(self.screen)
print(self.screen)
} else {
fatalError("no screen wtf???")
}
self.settings = settings self.settings = settings
self.skyHigh = SkyHigh(settings: settings)
self.currentDockSide = .left self.currentDockSide = .left
moveDock(.bottom) moveDock(.bottom)
@@ -138,9 +135,9 @@ class MouseTracker {
} }
func moveMouse() { 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 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) let posY = CGFloat.random(in: rangeH)
CGDisplayMoveCursorToPoint(0, CGPoint(x: posX, y: posY)) CGDisplayMoveCursorToPoint(0, CGPoint(x: posX, y: posY))
} }
@@ -173,7 +170,6 @@ class MouseTracker {
} }
func getDockSize() { func getDockSize() {
guard let screen = NSScreen.main?.frame else { fatalError() }
guard let screenVisible = NSScreen.main?.visibleFrame else { fatalError() } guard let screenVisible = NSScreen.main?.visibleFrame else { fatalError() }
self.dockHeight = screen.height - screenVisible.height self.dockHeight = screen.height - screenVisible.height
} }

View File

@@ -12,16 +12,13 @@ class SkyHigh {
private var window: NSWindow private var window: NSWindow
private var x = 1 private var x = 1
private var timer: Timer? private var timer: Timer?
var settings: DPSettingsModel
init() { init(settings: DPSettingsModel) {
guard let screen = NSScreen.main?.frame else { fatalError() } self.settings = settings
self.window = NSWindow( self.window = NSWindow(
contentRect: CGRect( contentRect: settings.settings.mouseMoveRect,
x: screen.width*0.05,
y: screen.height*0.1,
width: screen.width*0.9,
height: screen.height*0.8
),
styleMask: .borderless, styleMask: .borderless,
backing: .buffered, backing: .buffered,
defer: false defer: false
@@ -31,12 +28,12 @@ class SkyHigh {
window.level = NSWindow.Level.statusBar + 1 window.level = NSWindow.Level.statusBar + 1
window.ignoresMouseEvents = true window.ignoresMouseEvents = true
window.hasShadow = true window.hasShadow = true
window.collectionBehavior = NSWindow.CollectionBehavior.canJoinAllSpaces.union(.stationary)
window.makeKeyAndOrderFront(nil) window.makeKeyAndOrderFront(nil)
window.setFrameOrigin(NSPoint(x: screen.width*0.05, y: screen.height*0.1))
} }
func move() { func move() {
x = 1 x = 5
timer?.invalidate() timer?.invalidate()
timer = Timer(timeInterval: 0.01, repeats: true) { [weak self] _ in timer = Timer(timeInterval: 0.01, repeats: true) { [weak self] _ in
guard let self = self else { return } guard let self = self else { return }
@@ -45,7 +42,7 @@ class SkyHigh {
return return
} }
self.window.setFrameOrigin(NSPoint(x: 1000-self.x, y: 1000-self.x)) self.window.setFrameOrigin(NSPoint(x: 1000-self.x, y: 1000-self.x))
self.x += 1 self.x += 5
} }
RunLoop.current.add(timer!, forMode: .common) RunLoop.current.add(timer!, forMode: .common)
} }

View File

@@ -0,0 +1,8 @@
//
// Preferences.swift
// DockPhobia
//
// Created by neon443 on 29/05/2025.
//
import Foundation