Parcourir la source

Harden status-bar reopen activation for launcher window.

Resolve the launcher window by controller identity and reassert app/window activation during status-bar reopen so the app reliably returns to front after switching to other apps.

Made-with: Cursor
huzaifahayat12 il y a 3 mois
Parent
commit
2375854a40
1 fichiers modifiés avec 12 ajouts et 2 suppressions
  1. 12 2
      google_apps/AppDelegate.swift

+ 12 - 2
google_apps/AppDelegate.swift

@@ -13,10 +13,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
     private weak var launcherWindowReference: NSWindow?
 
     private var launcherWindow: NSWindow? {
-        if let launcherWindowReference {
+        if let launcherWindowReference, launcherWindowReference.contentViewController is ViewController {
             return launcherWindowReference
         }
-        let resolved = NSApp.windows.first
+        let resolved =
+            NSApp.windows.first(where: { $0.contentViewController is ViewController }) ??
+            NSApp.orderedWindows.first(where: { $0.contentViewController is ViewController }) ??
+            NSApp.windows.first ??
+            NSApp.orderedWindows.first
         launcherWindowReference = resolved
         return resolved
     }
@@ -130,6 +134,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
     private func showMainWindow() {
         configureMainWindowIfNeeded()
         NSApp.setActivationPolicy(.regular)
+        NSRunningApplication.current.activate(options: [.activateAllWindows, .activateIgnoringOtherApps])
         NSApp.activate(ignoringOtherApps: true)
         guard let window = launcherWindow else { return }
         if window.isMiniaturized {
@@ -137,6 +142,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
         }
         window.orderFrontRegardless()
         window.makeKeyAndOrderFront(nil)
+        // Re-assert key/front on next runloop; status-bar activations can race app focus handoff.
+        DispatchQueue.main.async {
+            window.orderFrontRegardless()
+            window.makeKeyAndOrderFront(nil)
+        }
     }
 
     private func configureMainWindowIfNeeded() {