HACK: dynamic light/dark theme

This commit is contained in:
Danny Lin
2024-06-07 02:51:23 -07:00
parent 6789858d92
commit 9e71847c25

View File

@ -72,6 +72,8 @@ class AppDelegate: NSObject,
let updaterController: SPUStandardUpdaterController
let updaterDelegate: UpdaterDelegate = UpdaterDelegate()
private var appearanceObserver: NSKeyValueObservation? = nil
override init() {
terminalManager = TerminalManager(ghostty)
updaterController = SPUStandardUpdaterController(
@ -96,6 +98,8 @@ class AppDelegate: NSObject,
}
func applicationDidFinishLaunching(_ notification: Notification) {
updateTerminalTheme()
// System settings overrides
UserDefaults.standard.register(defaults: [
// Disable this so that repeated key events make it through to our terminal views.
@ -130,6 +134,16 @@ class AppDelegate: NSObject,
)
])
center.delegate = self
appearanceObserver = NSApp.observe(\.effectiveAppearance, options: [.new, .old]) { [weak self] app, change in
guard let self else { return }
if change.newValue?.name == change.oldValue?.name {
return
}
self.updateTerminalTheme()
self.reloadConfig(nil)
}
}
func applicationDidBecomeActive(_ notification: Notification) {
@ -394,6 +408,13 @@ class AppDelegate: NSObject,
}
}
private func updateTerminalTheme() {
let configDir = "\(NSHomeDirectory())/.config/ghostty"
let currentTheme = NSApp.effectiveAppearance.name == .darkAqua ? "dark" : "light"
unlink("\(configDir)/theme")
symlink("\(configDir)/theme-\(currentTheme)", "\(configDir)/theme")
}
/// Sync the appearance of our app with the theme specified in the config.
private func syncAppearance() {
guard let theme = ghostty.config.windowTheme else { return }