| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- //
- // AppDelegate.swift
- // App for Indeed
- //
- // Created by Mql Mac 2 on 11/05/2026.
- //
- import Cocoa
- enum AppWindowConfiguration {
- static let defaultContentSize = NSSize(width: 1120, height: 800)
- static let minimumContentSize = NSSize(width: 1120, height: 800)
- @MainActor
- static func apply(to window: NSWindow) {
- window.minSize = minimumContentSize
- window.isRestorable = false
- window.title = AppMarketingLinks.appDisplayName
- window.styleMask.insert(.fullSizeContentView)
- window.titlebarAppearsTransparent = true
- window.titleVisibility = .hidden
- window.isMovableByWindowBackground = true
- // Same as dashboard chrome — avoids a halo outside the frame with fullSizeContentView.
- window.backgroundColor = AppAppearanceManager.shared.windowChromeColor
- let targetContent = NSRect(origin: .zero, size: defaultContentSize)
- let targetFrame = window.frameRect(forContentRect: targetContent)
- var frame = targetFrame
- let current = window.frame
- frame.origin.x = current.midX - frame.width / 2
- frame.origin.y = current.midY - frame.height / 2
- window.setFrame(frame, display: false, animate: false)
- window.setContentSize(defaultContentSize)
- }
- @MainActor
- static func mainWindow(in app: NSApplication = .shared) -> NSWindow? {
- app.mainWindow
- ?? app.keyWindow
- ?? app.windows.first(where: { $0.isVisible && $0.canBecomeKey })
- }
- }
- @main
- class AppDelegate: NSObject, NSApplicationDelegate {
- /// Avoids hammering StoreKit when `didBecomeActive` fires in quick succession (e.g. after system sheets).
- private var lastSubscriptionRefreshAt: Date?
- func applicationWillFinishLaunching(_ notification: Notification) {
- AppAppearanceManager.shared.apply()
- }
- func applicationDidFinishLaunching(_ aNotification: Notification) {
- AppRatingCoordinator.shared.start()
- NotificationCenter.default.addObserver(
- forName: NSApplication.didBecomeActiveNotification,
- object: nil,
- queue: .main
- ) { [weak self] _ in
- Task { @MainActor in
- guard let self else { return }
- let now = Date()
- if let last = self.lastSubscriptionRefreshAt, now.timeIntervalSince(last) < 2.5 {
- return
- }
- self.lastSubscriptionRefreshAt = now
- await SubscriptionStore.shared.refreshEntitlements(deep: true)
- }
- }
- // Initial StoreKit refresh runs on `LoadingViewController` before the dashboard is shown.
- NSApp.activate(ignoringOtherApps: true)
- applyDefaultWindowSize()
- }
- @MainActor
- private func applyDefaultWindowSize() {
- DispatchQueue.main.async { [weak self] in
- guard let self, let window = AppWindowConfiguration.mainWindow() else { return }
- AppWindowConfiguration.apply(to: window)
- window.center()
- window.makeKeyAndOrderFront(nil)
- // Layout can run after the first pass; enforce default size once more.
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in
- guard self != nil, let window = AppWindowConfiguration.mainWindow() else { return }
- AppWindowConfiguration.apply(to: window)
- window.center()
- }
- }
- }
- func applicationWillTerminate(_ aNotification: Notification) {
- // Insert code here to tear down your application
- }
- func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
- // Opt out until real `NSWindowRestoration` is implemented (avoids null className restore logs).
- return false
- }
- }
|