Files
downterm/Downterm/Downterm/AppDelegate.swift
2026-02-27 11:57:09 +11:00

70 lines
2.1 KiB
Swift

import AppKit
import Combine
/// Application delegate that bootstraps the notch overlay system.
@MainActor
class AppDelegate: NSObject, NSApplicationDelegate {
private var cancellables = Set<AnyCancellable>()
func applicationDidFinishLaunching(_ notification: Notification) {
NotchSettings.registerDefaults()
NSApp.setActivationPolicy(.accessory)
ScreenManager.shared.start()
observeDisplayPreference()
observeSizePreferences()
observeFontSizeChanges()
}
func applicationWillTerminate(_ notification: Notification) {
ScreenManager.shared.stop()
}
// MARK: - Preference observers
/// Only rebuild windows when the display-count preference changes.
private func observeDisplayPreference() {
UserDefaults.standard.publisher(for: \.showOnAllDisplays)
.removeDuplicates()
.dropFirst()
.sink { _ in
ScreenManager.shared.rebuildWindows()
}
.store(in: &cancellables)
}
/// Reposition (not rebuild) when any sizing preference changes.
private func observeSizePreferences() {
NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)
.debounce(for: .milliseconds(300), scheduler: RunLoop.main)
.sink { _ in
ScreenManager.shared.repositionWindows()
}
.store(in: &cancellables)
}
/// Live-update terminal font size across all sessions.
private func observeFontSizeChanges() {
UserDefaults.standard.publisher(for: \.terminalFontSize)
.removeDuplicates()
.sink { newSize in
guard newSize > 0 else { return }
TerminalManager.shared.updateAllFontSizes(CGFloat(newSize))
}
.store(in: &cancellables)
}
}
// MARK: - KVO key paths
private extension UserDefaults {
@objc var terminalFontSize: Double {
double(forKey: NotchSettings.Keys.terminalFontSize)
}
@objc var showOnAllDisplays: Bool {
bool(forKey: NotchSettings.Keys.showOnAllDisplays)
}
}