Bläddra i källkod

Hide the window on close instead of quitting the app.

Match standard macOS behavior by keeping Smart Printer in the Dock and reopening the window when the Dock icon is clicked.

Co-authored-by: Cursor <cursoragent@cursor.com>
AhtashamShahzad1 15 timmar sedan
förälder
incheckning
2c0f350588

+ 32 - 2
smart_printer/AppDelegate.swift

@@ -8,11 +8,28 @@ import Cocoa
8 8
 @main
9 9
 class AppDelegate: NSObject, NSApplicationDelegate {
10 10
 
11
+    private weak var mainWindowController: MainWindowController?
12
+
11 13
     func applicationDidFinishLaunching(_ notification: Notification) {
14
+        resolveMainWindowController()
12 15
         configureMainWindow()
13 16
         NSApp.activate(ignoringOtherApps: true)
14 17
     }
15 18
 
19
+    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
20
+        false
21
+    }
22
+
23
+    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
24
+        if !flag {
25
+            resolveMainWindowController()
26
+            mainWindowController?.showWindow(self)
27
+            mainWindowController?.window?.makeKeyAndOrderFront(self)
28
+            NSApp.activate(ignoringOtherApps: true)
29
+        }
30
+        return true
31
+    }
32
+
16 33
     func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
17 34
         false
18 35
     }
@@ -58,11 +75,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
58 75
     }
59 76
 
60 77
     private var mainWindow: NSWindow? {
61
-        NSApplication.shared.mainWindow
62
-            ?? NSApplication.shared.keyWindow
78
+        mainWindowController?.window
63 79
             ?? NSApplication.shared.windows.first(where: \.canBecomeMain)
64 80
     }
65 81
 
82
+    private func resolveMainWindowController() {
83
+        guard mainWindowController == nil else { return }
84
+
85
+        mainWindowController = NSApplication.shared.windows
86
+            .compactMap { $0.windowController as? MainWindowController }
87
+            .first
88
+
89
+        if mainWindowController == nil {
90
+            DispatchQueue.main.async { [weak self] in
91
+                self?.resolveMainWindowController()
92
+            }
93
+        }
94
+    }
95
+
66 96
     func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
67 97
         true
68 98
     }

+ 1 - 1
smart_printer/Base.lproj/Main.storyboard

@@ -682,7 +682,7 @@
682 682
         <!--Window Controller-->
683 683
         <scene sceneID="R2V-B0-nI4">
684 684
             <objects>
685
-                <windowController id="B8D-0N-5wS" sceneMemberID="viewController">
685
+                <windowController id="B8D-0N-5wS" customClass="MainWindowController" customModuleProvider="target" sceneMemberID="viewController">
686 686
                     <window key="window" title="Smart Printer" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="YES" animationBehavior="default" id="IQv-IB-iLA">
687 687
                         <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
688 688
                         <windowPositionMask key="initialPositionMask" horizontalCenterStrut="YES" verticalCenterStrut="YES"/>

+ 18 - 0
smart_printer/MainWindowController.swift

@@ -0,0 +1,18 @@
1
+//
2
+//  MainWindowController.swift
3
+//  smart_printer
4
+//
5
+
6
+import Cocoa
7
+
8
+final class MainWindowController: NSWindowController, NSWindowDelegate {
9
+    override func windowDidLoad() {
10
+        super.windowDidLoad()
11
+        window?.delegate = self
12
+    }
13
+
14
+    @objc func windowShouldClose(_ sender: NSWindow) -> Bool {
15
+        sender.orderOut(nil)
16
+        return false
17
+    }
18
+}