From 17ad77b5b0b7b7ead22bcfda11c9250d064d408d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 12 Jun 2025 21:33:40 -0700 Subject: [PATCH] macos: fix background color of terminal window to match surface --- .../Window Styles/TerminalWindow.swift | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/macos/Sources/Features/Terminal/Window Styles/TerminalWindow.swift b/macos/Sources/Features/Terminal/Window Styles/TerminalWindow.swift index d9b98695e..a1bb1d86d 100644 --- a/macos/Sources/Features/Terminal/Window Styles/TerminalWindow.swift +++ b/macos/Sources/Features/Terminal/Window Styles/TerminalWindow.swift @@ -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) } }