Przeglądaj źródła

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 dzień temu
rodzic
commit
3ec827aee1
1 zmienionych plików z 35 dodań i 0 usunięć
  1. 35 0
      App for Indeed/AppDelegate.swift

+ 35 - 0
App for Indeed/AppDelegate.swift

@@ -13,6 +13,7 @@ enum AppWindowConfiguration {
13 13
 
14 14
     @MainActor
15 15
     static func apply(to window: NSWindow) {
16
+        MainWindowCloseBehavior.install(on: window)
16 17
         window.minSize = minimumContentSize
17 18
         window.isRestorable = false
18 19
         window.title = AppMarketingLinks.appDisplayName
@@ -102,5 +103,39 @@ class AppDelegate: NSObject, NSApplicationDelegate {
102 103
         return false
103 104
     }
104 105
 
106
+    /// Keep running in the Dock when the user closes the last window (standard single-window macOS apps).
107
+    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
108
+        false
109
+    }
110
+
111
+    /// Clicking the Dock icon after closing the window should bring it back.
112
+    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
113
+        guard !flag else { return true }
114
+        let window = AppWindowConfiguration.mainWindow(in: sender)
115
+            ?? sender.windows.first(where: { $0.canBecomeKey })
116
+        window?.makeKeyAndOrderFront(self)
117
+        sender.activate(ignoringOtherApps: true)
118
+        return true
119
+    }
120
+}
121
+
122
+// MARK: - Main window close (hide on red button, do not quit)
123
+
124
+@MainActor
125
+private enum MainWindowCloseBehavior {
126
+    private final class WindowDelegate: NSObject, NSWindowDelegate {
127
+        static let shared = WindowDelegate()
105 128
 
129
+        func windowShouldClose(_ sender: NSWindow) -> Bool {
130
+            sender.orderOut(nil)
131
+            return false
132
+        }
133
+    }
134
+
135
+    static func install(on window: NSWindow) {
136
+        window.isReleasedWhenClosed = false
137
+        if window.delegate !== WindowDelegate.shared {
138
+            window.delegate = WindowDelegate.shared
139
+        }
140
+    }
106 141
 }