diff --git a/QuickDrop/AppDelegate.swift b/QuickDrop/AppDelegate.swift index 1ad51a1..b9e989f 100644 --- a/QuickDrop/AppDelegate.swift +++ b/QuickDrop/AppDelegate.swift @@ -221,6 +221,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele @objc func openPlusScreen() { + // If window already exists and is visible, just bring it to the front + if let window = plusWindow, window.isVisible { + NSApp.activate(ignoringOtherApps: true) + window.makeKeyAndOrderFront(nil) + return + } + // Create the welcome screen SwiftUI view let plusView = PlusView(closeView: { log("Closing plus screen") @@ -228,26 +235,33 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele }) // Create an NSWindow to host the SwiftUI view - plusWindow = NSWindow( + let window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: plusViewWidth, height: plusViewHeight), styleMask: [.titled, .closable], backing: .buffered, defer: false ) - plusWindow?.styleMask.insert(.fullSizeContentView) - plusWindow?.titleVisibility = .hidden - plusWindow?.titlebarAppearsTransparent = true + window.styleMask.insert(.fullSizeContentView) + window.titleVisibility = .hidden + window.titlebarAppearsTransparent = true - plusWindow?.center() - plusWindow?.isReleasedWhenClosed = false - plusWindow?.setFrameAutosaveName("PlusScreen") - plusWindow?.contentView = NSHostingView(rootView: plusView) + window.center() + window.isReleasedWhenClosed = false + window.setFrameAutosaveName("PlusScreen") + window.contentView = NSHostingView(rootView: plusView) + + // Reset reference when closed by red button + NotificationCenter.default.addObserver(forName: NSWindow.willCloseNotification, object: window, queue: .main) { [weak self] _ in + self?.plusWindow = nil + } + + plusWindow = window // Ensure the window is always on top - NSApp.activate(ignoringOtherApps: true) // Brings the whole app to the front - plusWindow?.makeKeyAndOrderFront(nil) - plusWindow?.level = .normal + NSApp.activate(ignoringOtherApps: true) + window.makeKeyAndOrderFront(nil) + window.level = .normal } diff --git a/QuickDrop/View/LicenseWindow.swift b/QuickDrop/View/LicenseWindow.swift index fe20546..33802cf 100644 --- a/QuickDrop/View/LicenseWindow.swift +++ b/QuickDrop/View/LicenseWindow.swift @@ -31,7 +31,6 @@ struct LicensePage: View { func openLicenseWindow() -> NSWindow { - // Create a new NSWindow and retain it in the `newWindow` property let window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 500, height: 400), styleMask: [.titled, .closable, .resizable], @@ -44,10 +43,9 @@ func openLicenseWindow() -> NSWindow { window.contentView = NSHostingView(rootView: LicensePage()) // Ensure the window is always on top - NSApp.activate(ignoringOtherApps: true) // Brings the whole app to the front + NSApp.activate(ignoringOtherApps: true) window.makeKeyAndOrderFront(nil) window.level = .normal - // Retain the window to prevent deallocation return window }