Parcourir la Source

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 il y a 11 heures
Parent
commit
2c0f350588

+ 32 - 2
smart_printer/AppDelegate.swift

@@ -8,11 +8,28 @@ import Cocoa
8
 @main
8
 @main
9
 class AppDelegate: NSObject, NSApplicationDelegate {
9
 class AppDelegate: NSObject, NSApplicationDelegate {
10
 
10
 
11
+    private weak var mainWindowController: MainWindowController?
12
+
11
     func applicationDidFinishLaunching(_ notification: Notification) {
13
     func applicationDidFinishLaunching(_ notification: Notification) {
14
+        resolveMainWindowController()
12
         configureMainWindow()
15
         configureMainWindow()
13
         NSApp.activate(ignoringOtherApps: true)
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
     func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
33
     func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
17
         false
34
         false
18
     }
35
     }
@@ -58,11 +75,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
58
     }
75
     }
59
 
76
 
60
     private var mainWindow: NSWindow? {
77
     private var mainWindow: NSWindow? {
61
-        NSApplication.shared.mainWindow
62
-            ?? NSApplication.shared.keyWindow
78
+        mainWindowController?.window
63
             ?? NSApplication.shared.windows.first(where: \.canBecomeMain)
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
     func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
96
     func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
67
         true
97
         true
68
     }
98
     }

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

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