From 3ad39dd5e4a4fe70b990fd45554da649eddb4375 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 20 Sep 2023 21:54:50 -0700 Subject: [PATCH] macos: sync NSAppearance for app based on window-theme setting --- macos/Sources/AppDelegate.swift | 20 ++++++++++++++++++++ macos/Sources/Ghostty/AppState.swift | 10 ++++++++++ 2 files changed, 30 insertions(+) diff --git a/macos/Sources/AppDelegate.swift b/macos/Sources/AppDelegate.swift index ebf728d83..e4a3e939c 100644 --- a/macos/Sources/AppDelegate.swift +++ b/macos/Sources/AppDelegate.swift @@ -187,6 +187,9 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp // Config could change keybindings, so update our menu syncMenuShortcuts() + // Config could change window appearance + syncAppearance() + // If we have configuration errors, we need to show them. let c = ConfigurationErrorsController.sharedInstance c.model.errors = state.configErrors() @@ -197,6 +200,23 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp } } + /// Sync the appearance of our app with the theme specified in the config. + private func syncAppearance() { + guard let theme = ghostty.windowTheme else { return } + switch (theme) { + case "dark": + let appearance = NSAppearance(named: .darkAqua) + NSApplication.shared.appearance = appearance + + case "light": + let appearance = NSAppearance(named: .aqua) + NSApplication.shared.appearance = appearance + + default: + NSApplication.shared.appearance = nil + } + } + //MARK: - Dock Menu private func reloadDockMenu() { diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index fe6ee42ae..9e7674a25 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -71,6 +71,16 @@ extension Ghostty { _ = ghostty_config_get(config, &v, key, UInt(key.count)) return v; } + + /// The window theme as a string. + var windowTheme: String? { + guard let config = self.config else { return nil } + var v: UnsafePointer? = nil + let key = "window-theme" + guard ghostty_config_get(config, &v, key, UInt(key.count)) else { return nil } + guard let ptr = v else { return nil } + return String(cString: ptr) + } init() { // Initialize ghostty global state. This happens once per process.