Skip to content

Commit 1e382ee

Browse files
committed
Fix transparency for the window, so it can be set externally
1 parent 76b865b commit 1e382ee

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

Sources/SwiftTerm/Mac/MacTerminalView.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,7 @@ open class TerminalView: NSView, NSUserInterfaceValidations, TerminalDelegate {
10851085
settingBg = true
10861086
_nativeBg = newValue
10871087
terminal.backgroundColor = newValue.getTerminalColor()
1088+
metalView?.layer?.isOpaque = newValue.cgColor.alpha >= 1.0
10881089
layer?.backgroundColor = metalView == nil
10891090
? effectiveNativeBackgroundColor.cgColor : NSColor.clear.cgColor
10901091
refreshCachedViewState()
@@ -1104,7 +1105,10 @@ open class TerminalView: NSView, NSUserInterfaceValidations, TerminalDelegate {
11041105
{
11051106
if settingBg { return }
11061107
settingBg = true
1107-
_nativeBg = newValue
1108+
// The terminal model stores RGB only. Keep the host opacity when its
1109+
// asynchronous color callback reconstructs an opaque native color.
1110+
let opacity = _nativeBg?.cgColor.alpha ?? 1.0
1111+
_nativeBg = newValue.withAlphaComponent(opacity)
11081112
refreshCachedViewState()
11091113
settingBg = false
11101114
}

Tests/SwiftTermTests/ProfileSupportTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ final class BackgroundOpacityTests {
230230
view.nativeBackgroundColor = NSColor (srgbRed: 0, green: 0, blue: 0, alpha: 0.85)
231231
#expect (abs (view.backgroundOpacity - 0.85) < 0.001)
232232
}
233+
234+
@Test func opacitySurvivesTheTerminalColorCallback () async {
235+
let view = TerminalView (frame: CGRect (x: 0, y: 0, width: 400, height: 300))
236+
view.nativeBackgroundColor = NSColor (srgbRed: 0.1, green: 0.2, blue: 0.3, alpha: 1)
237+
view.backgroundOpacity = 0.5
238+
239+
await withCheckedContinuation { continuation in
240+
DispatchQueue.main.async {
241+
continuation.resume()
242+
}
243+
}
244+
245+
#expect (abs (view.backgroundOpacity - 0.5) < 0.001)
246+
#expect (abs (view.nativeBackgroundColor.alphaComponent - 0.5) < 0.001)
247+
#expect (abs ((view.layer?.backgroundColor?.alpha ?? 1.0) - 0.5) < 0.001)
248+
let frameColor = FrameColor (view.effectiveNativeBackgroundColor, view: view)
249+
#expect (abs (frameColor.alpha - 0.5) < 0.001)
250+
}
233251
}
234252

235253
@MainActor

0 commit comments

Comments
 (0)