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 1 månad sedan
förälder
incheckning
2c0f350588

+ 32 - 2
smart_printer/AppDelegate.swift

@@ -8,11 +8,28 @@ import Cocoa
 @main
 @main
 class AppDelegate: NSObject, NSApplicationDelegate {
 class AppDelegate: NSObject, NSApplicationDelegate {
 
 
+    private weak var mainWindowController: MainWindowController?
+
     func applicationDidFinishLaunching(_ notification: Notification) {
     func applicationDidFinishLaunching(_ notification: Notification) {
+        resolveMainWindowController()
         configureMainWindow()
         configureMainWindow()
         NSApp.activate(ignoringOtherApps: true)
         NSApp.activate(ignoringOtherApps: true)
     }
     }
 
 
+    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
+        false
+    }
+
+    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
+        if !flag {
+            resolveMainWindowController()
+            mainWindowController?.showWindow(self)
+            mainWindowController?.window?.makeKeyAndOrderFront(self)
+            NSApp.activate(ignoringOtherApps: true)
+        }
+        return true
+    }
+
     func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
     func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
         false
         false
     }
     }
@@ -58,11 +75,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
     }
     }
 
 
     private var mainWindow: NSWindow? {
     private var mainWindow: NSWindow? {
-        NSApplication.shared.mainWindow
-            ?? NSApplication.shared.keyWindow
+        mainWindowController?.window
             ?? NSApplication.shared.windows.first(where: \.canBecomeMain)
             ?? NSApplication.shared.windows.first(where: \.canBecomeMain)
     }
     }
 
 
+    private func resolveMainWindowController() {
+        guard mainWindowController == nil else { return }
+
+        mainWindowController = NSApplication.shared.windows
+            .compactMap { $0.windowController as? MainWindowController }
+            .first
+
+        if mainWindowController == nil {
+            DispatchQueue.main.async { [weak self] in
+                self?.resolveMainWindowController()
+            }
+        }
+    }
+
     func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
     func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
         true
         true
     }
     }

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

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

+ 18 - 0
smart_printer/MainWindowController.swift

@@ -0,0 +1,18 @@
+//
+//  MainWindowController.swift
+//  smart_printer
+//
+
+import Cocoa
+
+final class MainWindowController: NSWindowController, NSWindowDelegate {
+    override func windowDidLoad() {
+        super.windowDidLoad()
+        window?.delegate = self
+    }
+
+    @objc func windowShouldClose(_ sender: NSWindow) -> Bool {
+        sender.orderOut(nil)
+        return false
+    }
+}