Sfoglia il codice sorgente

Center the main window on launch and disable restored positioning.

Prevent macOS from restoring a stale window frame so the app opens centered on the visible screen.

Co-authored-by: Cursor <cursoragent@cursor.com>
AhtashamShahzad1 1 mese fa
parent
commit
38f13ab781

+ 40 - 5
smart_printer/AppDelegate.swift

@@ -9,13 +9,21 @@ import Cocoa
 class AppDelegate: NSObject, NSApplicationDelegate {
 
     func applicationDidFinishLaunching(_ notification: Notification) {
-        DispatchQueue.main.async {
-            self.configureMainWindow()
-        }
+        configureMainWindow()
+        NSApp.activate(ignoringOtherApps: true)
+    }
+
+    func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
+        false
     }
 
     private func configureMainWindow() {
-        guard let window = NSApplication.shared.windows.first else { return }
+        guard let window = mainWindow else {
+            DispatchQueue.main.async { [weak self] in
+                self?.configureMainWindow()
+            }
+            return
+        }
 
         window.title = "Smart Printer"
         window.titlebarAppearsTransparent = true
@@ -24,8 +32,35 @@ class AppDelegate: NSObject, NSApplicationDelegate {
         window.isMovableByWindowBackground = true
         window.backgroundColor = AppTheme.background
         window.setContentSize(NSSize(width: AppTheme.windowWidth, height: AppTheme.windowHeight))
-        window.center()
         window.minSize = NSSize(width: AppTheme.windowMinWidth, height: AppTheme.windowMinHeight)
+        window.isRestorable = false
+        centerWindowOnScreen(window)
+
+        DispatchQueue.main.async {
+            self.centerWindowOnScreen(window)
+            window.makeKeyAndOrderFront(nil)
+        }
+    }
+
+    private func centerWindowOnScreen(_ window: NSWindow) {
+        let screen = NSScreen.main ?? window.screen ?? NSScreen.screens.first
+        guard let visibleFrame = screen?.visibleFrame else {
+            window.center()
+            return
+        }
+
+        var frame = window.frame
+        frame.size.width = min(frame.width, visibleFrame.width)
+        frame.size.height = min(frame.height, visibleFrame.height)
+        frame.origin.x = visibleFrame.midX - frame.width / 2
+        frame.origin.y = visibleFrame.midY - frame.height / 2
+        window.setFrame(frame, display: true)
+    }
+
+    private var mainWindow: NSWindow? {
+        NSApplication.shared.mainWindow
+            ?? NSApplication.shared.keyWindow
+            ?? NSApplication.shared.windows.first(where: \.canBecomeMain)
     }
 
     func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {

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

@@ -685,7 +685,7 @@
                 <windowController id="B8D-0N-5wS" 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" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
+                        <windowPositionMask key="initialPositionMask" horizontalCenterStrut="YES" verticalCenterStrut="YES"/>
                         <rect key="contentRect" x="196" y="240" width="680" height="720"/>
                         <rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
                         <connections>