فهرست منبع

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 ماه پیش
والد
کامیت
2c0f350588
3فایلهای تغییر یافته به همراه51 افزوده شده و 3 حذف شده
  1. 32 2
      smart_printer/AppDelegate.swift
  2. 1 1
      smart_printer/Base.lproj/Main.storyboard
  3. 18 0
      smart_printer/MainWindowController.swift

+ 32 - 2
smart_printer/AppDelegate.swift

@@ -8,11 +8,28 @@ import Cocoa
 @main
 class AppDelegate: NSObject, NSApplicationDelegate {
 
+    private weak var mainWindowController: MainWindowController?
+
     func applicationDidFinishLaunching(_ notification: Notification) {
+        resolveMainWindowController()
         configureMainWindow()
         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 {
         false
     }
@@ -58,11 +75,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
     }
 
     private var mainWindow: NSWindow? {
-        NSApplication.shared.mainWindow
-            ?? NSApplication.shared.keyWindow
+        mainWindowController?.window
             ?? 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 {
         true
     }

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

@@ -682,7 +682,7 @@
         <!--Window Controller-->
         <scene sceneID="R2V-B0-nI4">
             <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">
                         <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="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
+    }
+}