feat: remember launcher position across restarts
This commit is contained in:
@@ -16,6 +16,7 @@ final class LauncherWindowController: NSWindowController, NSWindowDelegate, NSSe
|
||||
private var isLaunching = false
|
||||
private var previousApplication: NSRunningApplication?
|
||||
private var alignmentGuides: LauncherAlignmentGuides?
|
||||
private static let positionKey = "launcherPosition"
|
||||
|
||||
init(history: LaunchHistory = LaunchHistory()) {
|
||||
self.history = history
|
||||
@@ -40,7 +41,12 @@ final class LauncherWindowController: NSWindowController, NSWindowDelegate, NSSe
|
||||
window.delegate = self
|
||||
window.navigateSelection = { [weak self] offset in self?.moveSelection(by: offset) }
|
||||
window.dismiss = { [weak self] in self?.dismiss() }
|
||||
window.dragEnded = { [weak self] in self?.alignmentGuides?.end(snapping: self?.window) }
|
||||
window.dragEnded = { [weak self] in
|
||||
guard let self else { return }
|
||||
self.alignmentGuides?.end(snapping: self.window)
|
||||
self.savePosition()
|
||||
}
|
||||
restorePosition()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
@@ -53,6 +59,7 @@ final class LauncherWindowController: NSWindowController, NSWindowDelegate, NSSe
|
||||
previousApplication = frontmost
|
||||
}
|
||||
searchField.stringValue = ""
|
||||
restorePosition()
|
||||
updateResults()
|
||||
window?.deminiaturize(nil)
|
||||
showWindow(nil)
|
||||
@@ -84,6 +91,34 @@ final class LauncherWindowController: NSWindowController, NSWindowDelegate, NSSe
|
||||
}
|
||||
}
|
||||
|
||||
private func savePosition() {
|
||||
guard let window, let screen = window.screen, let identifier = displayIdentifier(screen) else { return }
|
||||
UserDefaults.standard.set([
|
||||
"display": identifier, "x": window.frame.minX, "y": window.frame.minY
|
||||
], forKey: Self.positionKey)
|
||||
}
|
||||
|
||||
private func restorePosition() {
|
||||
guard let window else { return }
|
||||
let saved = UserDefaults.standard.dictionary(forKey: Self.positionKey)
|
||||
let savedScreen = NSScreen.screens.first { displayIdentifier($0) == saved?["display"] as? String }
|
||||
guard let screen = savedScreen ?? NSScreen.main ?? NSScreen.screens.first else { return }
|
||||
var origin: CGPoint?
|
||||
if savedScreen != nil, let x = saved?["x"] as? Double, let y = saved?["y"] as? Double {
|
||||
origin = CGPoint(x: x, y: y)
|
||||
}
|
||||
let frame = LauncherAlignment.restoredFrame(
|
||||
savedOrigin: origin, windowSize: window.frame.size, visibleFrame: screen.visibleFrame
|
||||
)
|
||||
if window.frame.origin != frame.origin { window.setFrameOrigin(frame.origin) }
|
||||
}
|
||||
|
||||
private func displayIdentifier(_ screen: NSScreen) -> String? {
|
||||
guard let number = screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? NSNumber,
|
||||
let uuid = CGDisplayCreateUUIDFromDisplayID(number.uint32Value)?.takeRetainedValue() else { return nil }
|
||||
return CFUUIDCreateString(nil, uuid) as String
|
||||
}
|
||||
|
||||
func windowWillMove(_ notification: Notification) {
|
||||
guard let window else { return }
|
||||
let guides = alignmentGuides ?? LauncherAlignmentGuides()
|
||||
|
||||
Reference in New Issue
Block a user