Explorar o código

Center launcher window on startup

Ensure the main window consistently opens centered on cold launch for a better first-run experience.

Co-authored-by: Cursor <cursoragent@cursor.com>
huzaifahayat12 hai 2 meses
pai
achega
7605f4c3b8
Modificáronse 1 ficheiros con 23 adicións e 3 borrados
  1. 23 3
      google_apps/AppDelegate.swift

+ 23 - 3
google_apps/AppDelegate.swift

@@ -34,7 +34,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
 
     func applicationDidFinishLaunching(_ aNotification: Notification) {
         applyBundledAppIcon()
-        configureMainWindowIfNeeded()
+        configureMainWindowIfNeeded(centerOnLaunch: true)
         setupStatusBarItem()
         StatusBarAppIconsController.shared.start()
         presentLauncherAndPaywallOnLaunch()
@@ -272,7 +272,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
     }
 
     private func showMainWindow() {
-        configureMainWindowIfNeeded()
+        configureMainWindowIfNeeded(centerOnLaunch: false)
         NSApp.setActivationPolicy(.regular)
         NSRunningApplication.current.activate(options: [.activateAllWindows, .activateIgnoringOtherApps])
         NSApp.activate(ignoringOtherApps: true)
@@ -289,7 +289,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
         }
     }
 
-    private func configureMainWindowIfNeeded() {
+    private func configureMainWindowIfNeeded(centerOnLaunch: Bool = false) {
         guard let window = launcherWindow else { return }
         observeLauncherWindowResign(window)
         window.title = "My Apps"
@@ -317,6 +317,26 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
             )
             window.setFrame(targetFrame, display: true)
         }
+
+        if centerOnLaunch {
+            centerWindowOnCurrentOrPrimaryScreen(window)
+        }
+    }
+
+    private func centerWindowOnCurrentOrPrimaryScreen(_ window: NSWindow) {
+        // Prefer the screen the window is currently on; fall back to the primary screen.
+        let screen = window.screen ?? NSScreen.main
+        guard let visibleFrame = screen?.visibleFrame else { return }
+
+        let frameSize = window.frame.size
+        let unclampedX = visibleFrame.midX - (frameSize.width / 2)
+        let unclampedY = visibleFrame.midY - (frameSize.height / 2)
+
+        // Clamp so we don't move the window completely off-screen on small displays.
+        let clampedX = min(max(unclampedX, visibleFrame.minX), visibleFrame.maxX - frameSize.width)
+        let clampedY = min(max(unclampedY, visibleFrame.minY), visibleFrame.maxY - frameSize.height)
+
+        window.setFrame(NSRect(origin: NSPoint(x: clampedX, y: clampedY), size: frameSize), display: false)
     }
 
     private func observeLauncherWindowResign(_ window: NSWindow) {