From 9e71847c250b3a4061b59995679b08e7c734d6fd Mon Sep 17 00:00:00 2001 From: Danny Lin Date: Fri, 7 Jun 2024 02:51:23 -0700 Subject: [PATCH] HACK: dynamic light/dark theme --- macos/Sources/App/macOS/AppDelegate.swift | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index 8b6b064a9..4ebb87a0c 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -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 }