Skip to content

Commit

Permalink
Merge pull request #128 from itssali/Bug-Fixes
Browse files Browse the repository at this point in the history
Fixes [Feature Request] Make Mini player retain position when moved #124
  • Loading branch information
martinfekete10 authored Nov 20, 2024
2 parents cb0830a + 80b6fee commit 44746af
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
16 changes: 13 additions & 3 deletions Tuneful/Tuneful.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
// MARK: Mini player

@objc func setupMiniPlayer() {
let originalWindowPosition = miniPlayerWindow.frame.origin
let windowPosition = CGPoint(x: originalWindowPosition.x, y: originalWindowPosition.y + 10) // Not sure why, but everytime this function is called, window moves down a few pixels, thus this ugly workaround
let windowPosition = miniPlayerWindow.frame.origin

switch miniPlayerType {
case .full:
Expand Down Expand Up @@ -444,7 +443,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {

private func setupMiniPlayerWindow<Content: View>(size: NSSize, position: CGPoint, view: Content) {
DispatchQueue.main.async {
self.miniPlayerWindow.setFrame(NSRect(origin: position, size: size), display: true, animate: true)
// Calculate new position that maintains the same vertical alignment
let currentFrame = self.miniPlayerWindow.frame
let newPosition = NSPoint(
x: position.x,
y: position.y + (currentFrame.height - size.height) // Adjust Y position to maintain top alignment
)

self.miniPlayerWindow.setFrame(
NSRect(origin: newPosition, size: size),
display: true,
animate: true
)
}

let rootView = view.cornerRadius(15).environmentObject(self.playerManager)
Expand Down
36 changes: 35 additions & 1 deletion Tuneful/Windows/MiniPlayerWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import AppKit
class MiniPlayerWindow: NSWindow {

@AppStorage("miniPlayerType") var miniPlayerType: MiniPlayerType = .minimal
@AppStorage("windowPosition") var savedPosition: String = "10,0"

init() {
let userDefaults = UserDefaults.standard
let position = NSPoint.fromString(
userDefaults.string(forKey: "windowPosition") ?? "10,0"
) ?? NSPoint(x: 10, y: 0)

super.init(
contentRect: NSRect(x: 10, y: 0, width: 300, height: 145),
contentRect: NSRect(x: position.x, y: position.y, width: 300, height: 145),
styleMask: [.borderless],
backing: .buffered,
defer: false
Expand All @@ -26,6 +32,13 @@ class MiniPlayerWindow: NSWindow {
self.isReleasedWhenClosed = false
self.backgroundColor = NSColor.clear
self.hasShadow = true

NotificationCenter.default.addObserver(
self,
selector: #selector(windowDidMove),
name: NSWindow.didMoveNotification,
object: self
)
}

override func rightMouseDown(with event: NSEvent) {
Expand Down Expand Up @@ -70,4 +83,25 @@ class MiniPlayerWindow: NSWindow {
@objc func settings(_ sender: Any?) {
NSApplication.shared.sendAction(#selector(AppDelegate.openMiniPlayerAppearanceSettings), to: nil, from: nil)
}

@objc func windowDidMove(_ notification: Notification) {
let position = self.frame.origin
savedPosition = "\(position.x),\(position.y)"
}

deinit {
NotificationCenter.default.removeObserver(self)
}
}

extension NSPoint {
static func fromString(_ string: String) -> NSPoint? {
let components = string.split(separator: ",")
guard components.count == 2,
let x = Double(components[0]),
let y = Double(components[1]) else {
return nil
}
return NSPoint(x: x, y: y)
}
}

0 comments on commit 44746af

Please sign in to comment.