| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- //
- // 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) {
- MainWindowCloseBehavior.install(on: window)
- 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) {
- AppLanguageManager.shared.applyStoredPreferenceOnLaunch()
- 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
- }
- /// Keep running in the Dock when the user closes the last window (standard single-window macOS apps).
- func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
- false
- }
- /// Clicking the Dock icon after closing the window should bring it back.
- func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
- guard !flag else { return true }
- let window = AppWindowConfiguration.mainWindow(in: sender)
- ?? sender.windows.first(where: { $0.canBecomeKey })
- window?.makeKeyAndOrderFront(self)
- sender.activate(ignoringOtherApps: true)
- return true
- }
- }
- // MARK: - Main window close (hide on red button, do not quit)
- @MainActor
- private enum MainWindowCloseBehavior {
- private final class WindowDelegate: NSObject, NSWindowDelegate {
- static let shared = WindowDelegate()
- func windowShouldClose(_ sender: NSWindow) -> Bool {
- sender.orderOut(nil)
- return false
- }
- }
- static func install(on window: NSWindow) {
- window.isReleasedWhenClosed = false
- if window.delegate !== WindowDelegate.shared {
- window.delegate = WindowDelegate.shared
- }
- }
- }
|