macos: fix background color of terminal window to match surface

pull/7588/head
Mitchell Hashimoto 2025-06-12 21:33:40 -07:00
parent 9d9c451b0a
commit 17ad77b5b0
No known key found for this signature in database
GPG Key ID: 523D5DC389D273BC
1 changed files with 11 additions and 10 deletions

View File

@ -322,22 +322,23 @@ class TerminalWindow: NSWindow {
/// change the alpha channel again manually.
var preferredBackgroundColor: NSColor? {
if let terminalController, !terminalController.surfaceTree.isEmpty {
let surface: Ghostty.SurfaceView?
// If our focused surface borders the top then we prefer its background color
if let focusedSurface = terminalController.focusedSurface,
let treeRoot = terminalController.surfaceTree.root,
let focusedNode = treeRoot.node(view: focusedSurface),
treeRoot.spatial().doesBorder(side: .up, from: focusedNode),
let backgroundcolor = focusedSurface.backgroundColor {
let alpha = focusedSurface.derivedConfig.backgroundOpacity.clamped(to: 0.001...1)
return NSColor(backgroundcolor).withAlphaComponent(alpha)
treeRoot.spatial().doesBorder(side: .up, from: focusedNode) {
surface = focusedSurface
} else {
// If it doesn't border the top, we use the top-left leaf
surface = terminalController.surfaceTree.root?.leftmostLeaf()
}
// Doesn't border the top or we don't have a focused surface, so
// we try to match the top-left surface.
let topLeftSurface = terminalController.surfaceTree.root?.leftmostLeaf()
if let topLeftBgColor = topLeftSurface?.backgroundColor {
let alpha = topLeftSurface?.derivedConfig.backgroundOpacity.clamped(to: 0.001...1) ?? 1
return NSColor(topLeftBgColor).withAlphaComponent(alpha)
if let surface {
let backgroundColor = surface.backgroundColor ?? surface.derivedConfig.backgroundColor
let alpha = surface.derivedConfig.backgroundOpacity.clamped(to: 0.001...1)
return NSColor(backgroundColor).withAlphaComponent(alpha)
}
}