ghostty/macos/Sources/Helpers/NSAppearance+Extension.swift
Mitchell Hashimoto 9d6b9d78d8 macos: set NSAppearance on windowDidLoad
Fixes #3072

Previously, when `window-theme = auto`, the appearance was delayed
enough on the DispatchQueue that the window was already visible. This
would result in the window appearing with the wrong appearance before
switching to the correct one.

For annoying reasons, we can't set the NSApplication.shared.appearance
in `applicationDidFinishLaunching` because it results in a deadlock with
AppKit.

This commit moves to set the `NSWindow.appearance` in `windowDidLoad`
(and any config sync) to ensure that the appearance is set before the
window is visible.

This is probably the right solution anyways because this allows windows
with different background colors to each have their own distinct
appearance.
2024-12-22 19:33:01 -08:00

32 lines
826 B
Swift

import Cocoa
extension NSAppearance {
/// Returns true if the appearance is some kind of dark.
var isDark: Bool {
return name.rawValue.lowercased().contains("dark")
}
/// Initialize a desired NSAppearance for the Ghostty configuration.
convenience init?(ghosttyConfig config: Ghostty.Config) {
guard let theme = config.windowTheme else { return nil }
switch (theme) {
case "dark":
self.init(named: .darkAqua)
case "light":
self.init(named: .aqua)
case "auto":
let color = OSColor(config.backgroundColor)
if color.isLightColor {
self.init(named: .aqua)
} else {
self.init(named: .darkAqua)
}
default:
return nil
}
}
}