macos: set background opacity/blur on window controller not surface

This commit is contained in:
Mitchell Hashimoto
2024-06-07 12:31:45 -07:00
parent 6dd88a1bb2
commit 25484d2ccc
5 changed files with 25 additions and 46 deletions

View File

@@ -166,6 +166,16 @@ class TerminalController: NSWindowController, NSWindowDelegate,
private func syncAppearance() {
guard let window = self.window as? TerminalWindow else { return }
// If our window is not visible, then delay this. This is possible specifically
// during state restoration but probably in other scenarios as well. To delay,
// we just loop directly on the dispatch queue. We have to delay because some
// APIs such as window blur have no effect unless the window is visible.
guard window.isVisible else {
// Weak window so that if the window changes or is destroyed we aren't holding a ref
DispatchQueue.main.async { [weak self] in self?.syncAppearance() }
return
}
// Set the font for the window and tab titles.
if let titleFontName = ghostty.config.windowTitleFontFamily {
window.titlebarFont = NSFont(name: titleFontName, size: NSFont.systemFontSize)
@@ -173,6 +183,18 @@ class TerminalController: NSWindowController, NSWindowDelegate,
window.titlebarFont = nil
}
// If we have window transparency then set it transparent. Otherwise set it opaque.
if (ghostty.config.backgroundOpacity < 1) {
window.isOpaque = false
window.hasShadow = false
window.backgroundColor = .clear
ghostty_set_window_background_blur(ghostty.app, Unmanaged.passUnretained(window).toOpaque())
} else {
window.isOpaque = true
window.hasShadow = true
window.backgroundColor = .windowBackgroundColor
}
guard window.hasStyledTabs else { return }
// The titlebar is always updated. We don't need to worry about opacity

View File

@@ -342,40 +342,6 @@ extension Ghostty {
// MARK: - NSView
override func viewDidMoveToWindow() {
// Set our background blur if requested
setWindowBackgroundBlur(window)
}
/// This function sets the window background to blur if it is configured on the surface.
private func setWindowBackgroundBlur(_ targetWindow: NSWindow?) {
// Surface must desire transparency
guard let surface = self.surface,
ghostty_surface_transparent(surface) else { return }
// Our target should always be our own view window
guard let target = targetWindow,
let window = self.window,
target == window else { return }
// If our window is not visible, then delay this. This is possible specifically
// during state restoration but probably in other scenarios as well. To delay,
// we just loop directly on the dispatch queue.
guard window.isVisible else {
// Weak window so that if the window changes or is destroyed we aren't holding a ref
DispatchQueue.main.async { [weak self, weak window] in self?.setWindowBackgroundBlur(window) }
return
}
// Set the window transparency settings
window.isOpaque = false
window.hasShadow = false
window.backgroundColor = .clear
// If we have a blur, set the blur
ghostty_set_window_background_blur(surface, Unmanaged.passUnretained(window).toOpaque())
}
override func becomeFirstResponder() -> Bool {
let result = super.becomeFirstResponder()
if (result) { focusDidChange(true) }