Selaa lähdekoodia

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 12 tuntia sitten
vanhempi
commit
38f13ab781
2 muutettua tiedostoa jossa 41 lisäystä ja 6 poistoa
  1. 40 5
      smart_printer/AppDelegate.swift
  2. 1 1
      smart_printer/Base.lproj/Main.storyboard

+ 40 - 5
smart_printer/AppDelegate.swift

@@ -9,13 +9,21 @@ import Cocoa
9 9
 class AppDelegate: NSObject, NSApplicationDelegate {
10 10
 
11 11
     func applicationDidFinishLaunching(_ notification: Notification) {
12
-        DispatchQueue.main.async {
13
-            self.configureMainWindow()
14
-        }
12
+        configureMainWindow()
13
+        NSApp.activate(ignoringOtherApps: true)
14
+    }
15
+
16
+    func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
17
+        false
15 18
     }
16 19
 
17 20
     private func configureMainWindow() {
18
-        guard let window = NSApplication.shared.windows.first else { return }
21
+        guard let window = mainWindow else {
22
+            DispatchQueue.main.async { [weak self] in
23
+                self?.configureMainWindow()
24
+            }
25
+            return
26
+        }
19 27
 
20 28
         window.title = "Smart Printer"
21 29
         window.titlebarAppearsTransparent = true
@@ -24,8 +32,35 @@ class AppDelegate: NSObject, NSApplicationDelegate {
24 32
         window.isMovableByWindowBackground = true
25 33
         window.backgroundColor = AppTheme.background
26 34
         window.setContentSize(NSSize(width: AppTheme.windowWidth, height: AppTheme.windowHeight))
27
-        window.center()
28 35
         window.minSize = NSSize(width: AppTheme.windowMinWidth, height: AppTheme.windowMinHeight)
36
+        window.isRestorable = false
37
+        centerWindowOnScreen(window)
38
+
39
+        DispatchQueue.main.async {
40
+            self.centerWindowOnScreen(window)
41
+            window.makeKeyAndOrderFront(nil)
42
+        }
43
+    }
44
+
45
+    private func centerWindowOnScreen(_ window: NSWindow) {
46
+        let screen = NSScreen.main ?? window.screen ?? NSScreen.screens.first
47
+        guard let visibleFrame = screen?.visibleFrame else {
48
+            window.center()
49
+            return
50
+        }
51
+
52
+        var frame = window.frame
53
+        frame.size.width = min(frame.width, visibleFrame.width)
54
+        frame.size.height = min(frame.height, visibleFrame.height)
55
+        frame.origin.x = visibleFrame.midX - frame.width / 2
56
+        frame.origin.y = visibleFrame.midY - frame.height / 2
57
+        window.setFrame(frame, display: true)
58
+    }
59
+
60
+    private var mainWindow: NSWindow? {
61
+        NSApplication.shared.mainWindow
62
+            ?? NSApplication.shared.keyWindow
63
+            ?? NSApplication.shared.windows.first(where: \.canBecomeMain)
29 64
     }
30 65
 
31 66
     func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {

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

@@ -685,7 +685,7 @@
685 685
                 <windowController id="B8D-0N-5wS" sceneMemberID="viewController">
686 686
                     <window key="window" title="Smart Printer" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="YES" animationBehavior="default" id="IQv-IB-iLA">
687 687
                         <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
688
-                        <windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
688
+                        <windowPositionMask key="initialPositionMask" horizontalCenterStrut="YES" verticalCenterStrut="YES"/>
689 689
                         <rect key="contentRect" x="196" y="240" width="680" height="720"/>
690 690
                         <rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
691 691
                         <connections>