macos: sync NSAppearance for app based on window-theme setting

This commit is contained in:
Mitchell Hashimoto
2023-09-20 21:54:50 -07:00
parent 8963c3b299
commit 3ad39dd5e4
2 changed files with 30 additions and 0 deletions

View File

@ -187,6 +187,9 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp
// Config could change keybindings, so update our menu // Config could change keybindings, so update our menu
syncMenuShortcuts() syncMenuShortcuts()
// Config could change window appearance
syncAppearance()
// If we have configuration errors, we need to show them. // If we have configuration errors, we need to show them.
let c = ConfigurationErrorsController.sharedInstance let c = ConfigurationErrorsController.sharedInstance
c.model.errors = state.configErrors() 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 //MARK: - Dock Menu
private func reloadDockMenu() { private func reloadDockMenu() {

View File

@ -71,6 +71,16 @@ extension Ghostty {
_ = ghostty_config_get(config, &v, key, UInt(key.count)) _ = ghostty_config_get(config, &v, key, UInt(key.count))
return v; return v;
} }
/// The window theme as a string.
var windowTheme: String? {
guard let config = self.config else { return nil }
var v: UnsafePointer<Int8>? = 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() { init() {
// Initialize ghostty global state. This happens once per process. // Initialize ghostty global state. This happens once per process.