From d743a5039aa995058daf800404047cc52bade19f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6ttger?= Date: Tue, 3 Jun 2025 20:56:01 +0200 Subject: [PATCH] Fix welcome screen opened multiple times if app icon clicked multiple times --- QuickDrop/AppDelegate.swift | 38 +++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/QuickDrop/AppDelegate.swift b/QuickDrop/AppDelegate.swift index 4307ac6..b022112 100644 --- a/QuickDrop/AppDelegate.swift +++ b/QuickDrop/AppDelegate.swift @@ -15,7 +15,7 @@ import SwiftUI import UserNotifications @main -class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate, MainAppDelegate { +class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate, NSWindowDelegate, MainAppDelegate { private var statusItem: NSStatusItem? private var activeIncomingTransfers: [String: TransferInfo] = [:] @@ -118,6 +118,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele } + func windowWillClose(_ notification: Notification) { + if let window = notification.object as? NSWindow, window == welcomeWindow { + log("Welcome window closed") + welcomeWindow = nil + NSApp.setActivationPolicy(.accessory) + } + } + + func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows _: Bool) -> Bool { openWelcomeScreen() statusItem?.isVisible = true @@ -156,12 +165,25 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele @objc func openWelcomeScreen() { - let welcomeView = WelcomeScreen(openPlusScreen: openPlusScreen, - openAppAdvertisementView: { self.openSheetView(type: .downloadAndroidApp) }, - openCableTransmissionView: { self.openSheetView(type: .downloadCableConnectionApp) }, - checkForNetworkIssues: performDeviceToDeviceCheck) - + NSApp.setActivationPolicy(.regular) + + if let window = welcomeWindow { + if window.isMiniaturized { + window.deminiaturize(nil) + } + NSApp.activate(ignoringOtherApps: true) + window.makeKeyAndOrderFront(nil) + return + } + + let welcomeView = WelcomeScreen( + openPlusScreen: openPlusScreen, + openAppAdvertisementView: { self.openSheetView(type: .downloadAndroidApp) }, + openCableTransmissionView: { self.openSheetView(type: .downloadCableConnectionApp) }, + checkForNetworkIssues: performDeviceToDeviceCheck + ) + welcomeWindow = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 400, height: 280), styleMask: [.titled, .closable], @@ -177,9 +199,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele welcomeWindow?.isReleasedWhenClosed = false welcomeWindow?.setFrameAutosaveName("WelcomeScreen") welcomeWindow?.contentView = NSHostingView(rootView: welcomeView) + welcomeWindow?.delegate = self - // Ensure the window is always on top - NSApp.activate(ignoringOtherApps: true) // Brings the whole app to the front + NSApp.activate(ignoringOtherApps: true) welcomeWindow?.makeKeyAndOrderFront(nil) welcomeWindow?.level = .normal }