Ver código fonte

Add Window menu action to reopen the main window after close.

App Review flagged that closing the main window left the app running with no menu path to bring it back. Add Window → Show Main Window (⌘0), and harden reopen so the tracked window is ordered front again via openWindow plus AppKit restore instead of only activating the app.

Co-authored-by: Cursor <cursoragent@cursor.com>
Uzair Tahir 1 semana atrás
pai
commit
650a1b88bf

+ 37 - 8
App AI for Reddit/App/AppDelegate.swift

@@ -5,6 +5,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
     private var windowObservers: [NSObjectProtocol] = []
     private var shouldCenterMainWindowOnShow = true
     private var centeringDeadline: Date?
+    private weak var trackedMainWindow: NSWindow?
 
     func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
         false
@@ -31,6 +32,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
 
     func windowShouldClose(_ sender: NSWindow) -> Bool {
         if isMainAppWindow(sender) {
+            trackedMainWindow = sender
             shouldCenterMainWindowOnShow = true
             centeringDeadline = nil
         }
@@ -55,6 +57,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
 
     func configureMainWindows() {
         for window in NSApp.windows where isMainAppWindow(window) {
+            trackedMainWindow = window
             window.isReleasedWhenClosed = false
             window.isRestorable = false
             window.setFrameAutosaveName("")
@@ -130,21 +133,47 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
         ]
     }
 
-    private func showMainWindowIfNeeded() {
-        guard !NSApp.windows.contains(where: { isMainAppWindow($0) && $0.isVisible }) else { return }
-        showMainWindow()
-    }
+    func showMainWindow() {
+        configureMainWindows()
 
-    private func showMainWindow() {
-        guard let window = NSApp.windows.first(where: isMainAppWindow) else { return }
+        guard let window = resolvedMainWindow() else { return }
+
+        trackedMainWindow = window
         beginCenteringMainWindow()
+
+        if window.isMiniaturized {
+            window.deminiaturize(nil)
+        }
+
+        window.collectionBehavior.insert(.moveToActiveSpace)
+        window.orderFrontRegardless()
         window.makeKeyAndOrderFront(nil)
         NSApp.activate(ignoringOtherApps: true)
     }
 
+    private func showMainWindowIfNeeded() {
+        guard !NSApp.windows.contains(where: { isMainAppWindow($0) && $0.isVisible && !$0.isMiniaturized }) else {
+            return
+        }
+        showMainWindow()
+    }
+
+    private func resolvedMainWindow() -> NSWindow? {
+        if let trackedMainWindow, NSApp.windows.contains(trackedMainWindow) {
+            return trackedMainWindow
+        }
+
+        if let visible = NSApp.windows.first(where: { isMainAppWindow($0) && $0.isVisible }) {
+            return visible
+        }
+
+        return NSApp.windows.first(where: isMainAppWindow)
+    }
+
     private func isMainAppWindow(_ window: NSWindow) -> Bool {
-        guard window.canBecomeMain, !(window is NSPanel) else { return false }
+        if window === trackedMainWindow { return true }
+        guard !(window is NSPanel), window.level == .normal else { return false }
         if window.title == "Sign in to Reddit" { return false }
-        return window.level == .normal
+        return window.canBecomeKey || window.canBecomeMain
     }
 }

+ 15 - 0
App AI for Reddit/App/RedditAppApp.swift

@@ -30,6 +30,21 @@ struct RedditAppApp: App {
         .windowStyle(.hiddenTitleBar)
         .commands {
             CommandGroup(replacing: .newItem) {}
+            MainWindowReopenCommands()
+        }
+    }
+}
+
+private struct MainWindowReopenCommands: Commands {
+    @Environment(\.openWindow) private var openWindow
+
+    var body: some Commands {
+        CommandGroup(after: .windowList) {
+            Button("Show Main Window") {
+                openWindow(id: "main-window")
+                (NSApp.delegate as? AppDelegate)?.showMainWindow()
+            }
+            .keyboardShortcut("0", modifiers: .command)
         }
     }
 }