Pārlūkot izejas kodu

Hide the main window on close instead of quitting the app.

Match standard macOS behavior: the red close button keeps the app in the Dock, and clicking the Dock icon restores the window.

Co-authored-by: Cursor <cursoragent@cursor.com>
AhtashamShahzad1 1 mēnesi atpakaļ
vecāks
revīzija
3ec827aee1
1 mainītis faili ar 35 papildinājumiem un 0 dzēšanām
  1. 35 0
      App for Indeed/AppDelegate.swift

+ 35 - 0
App for Indeed/AppDelegate.swift

@@ -13,6 +13,7 @@ enum AppWindowConfiguration {
 
     @MainActor
     static func apply(to window: NSWindow) {
+        MainWindowCloseBehavior.install(on: window)
         window.minSize = minimumContentSize
         window.isRestorable = false
         window.title = AppMarketingLinks.appDisplayName
@@ -102,5 +103,39 @@ class AppDelegate: NSObject, NSApplicationDelegate {
         return false
     }
 
+    /// Keep running in the Dock when the user closes the last window (standard single-window macOS apps).
+    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
+        false
+    }
+
+    /// Clicking the Dock icon after closing the window should bring it back.
+    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
+        guard !flag else { return true }
+        let window = AppWindowConfiguration.mainWindow(in: sender)
+            ?? sender.windows.first(where: { $0.canBecomeKey })
+        window?.makeKeyAndOrderFront(self)
+        sender.activate(ignoringOtherApps: true)
+        return true
+    }
+}
+
+// MARK: - Main window close (hide on red button, do not quit)
+
+@MainActor
+private enum MainWindowCloseBehavior {
+    private final class WindowDelegate: NSObject, NSWindowDelegate {
+        static let shared = WindowDelegate()
 
+        func windowShouldClose(_ sender: NSWindow) -> Bool {
+            sender.orderOut(nil)
+            return false
+        }
+    }
+
+    static func install(on window: NSWindow) {
+        window.isReleasedWhenClosed = false
+        if window.delegate !== WindowDelegate.shared {
+            window.delegate = WindowDelegate.shared
+        }
+    }
 }