Просмотр исходного кода

Fix launcher hide behavior when clicking desktop.

The launcher now hides when the app resigns active state and when its window resigns key status, covering desktop clicks that previously left the window visible.

Made-with: Cursor
huzaifahayat12 3 месяцев назад
Родитель
Сommit
185a9551fe
1 измененных файлов с 24 добавлено и 1 удалено
  1. 24 1
      google_apps/AppDelegate.swift

+ 24 - 1
google_apps/AppDelegate.swift

@@ -11,6 +11,7 @@ import Cocoa
 class AppDelegate: NSObject, NSApplicationDelegate {
 class AppDelegate: NSObject, NSApplicationDelegate {
     private var statusItem: NSStatusItem?
     private var statusItem: NSStatusItem?
     private weak var launcherWindowReference: NSWindow?
     private weak var launcherWindowReference: NSWindow?
+    private var launcherWindowResignObserver: NSObjectProtocol?
 
 
     private var launcherWindow: NSWindow? {
     private var launcherWindow: NSWindow? {
         if let launcherWindowReference, launcherWindowReference.contentViewController is ViewController {
         if let launcherWindowReference, launcherWindowReference.contentViewController is ViewController {
@@ -151,6 +152,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
 
 
     private func configureMainWindowIfNeeded() {
     private func configureMainWindowIfNeeded() {
         guard let window = launcherWindow else { return }
         guard let window = launcherWindow else { return }
+        observeLauncherWindowResign(window)
         window.title = "My Apps"
         window.title = "My Apps"
         window.styleMask.insert(.titled)
         window.styleMask.insert(.titled)
         window.isOpaque = false
         window.isOpaque = false
@@ -178,6 +180,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
         }
         }
     }
     }
 
 
+    private func observeLauncherWindowResign(_ window: NSWindow) {
+        if let observer = launcherWindowResignObserver {
+            NotificationCenter.default.removeObserver(observer)
+            launcherWindowResignObserver = nil
+        }
+
+        launcherWindowResignObserver = NotificationCenter.default.addObserver(
+            forName: NSWindow.didResignKeyNotification,
+            object: window,
+            queue: .main
+        ) { [weak self] _ in
+            self?.hideMainWindow()
+        }
+    }
+
     private func applyRoundedCorners(to window: NSWindow) {
     private func applyRoundedCorners(to window: NSWindow) {
         let cornerRadius: CGFloat = 16
         let cornerRadius: CGFloat = 16
         window.hasShadow = true
         window.hasShadow = true
@@ -187,10 +204,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
     }
     }
 
 
     func applicationWillTerminate(_ aNotification: Notification) {
     func applicationWillTerminate(_ aNotification: Notification) {
+        if let observer = launcherWindowResignObserver {
+            NotificationCenter.default.removeObserver(observer)
+            launcherWindowResignObserver = nil
+        }
         // Insert code here to tear down your application
         // Insert code here to tear down your application
     }
     }
 
 
-    func applicationDidResignActive(_ notification: Notification) {}
+    func applicationDidResignActive(_ notification: Notification) {
+        hideMainWindow()
+    }
 
 
     func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
     func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
         showMainWindow()
         showMainWindow()