Skip to content

Commit

Permalink
Optimize background opacity toggle implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
liby committed Jan 17, 2025
1 parent 839bfff commit 354bd2f
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions macos/Sources/Ghostty/SurfaceView_AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -496,26 +496,25 @@ extension Ghostty {
}

@objc private func ghosttyBackgroundOpacityDidToggle() {
guard self.derivedConfig.backgroundOpacity < 1 else { return }
guard let window = self.window as? TerminalWindow else { return }

// Toggle the window's background opacity
if let window = self.window as? TerminalWindow {
let newOpaque = !window.isOpaque
window.isOpaque = newOpaque
// Don't toggle transparency if opacity is 1+ or in fullscreen mode
if self.derivedConfig.backgroundOpacity >= 1 || window.styleMask.contains(.fullScreen) {
return
}

// Update the window background color based on opacity state
if newOpaque {
window.backgroundColor = NSColor(self.derivedConfig.backgroundColor)
} else {
// Use a very small alpha component to match Terminal.app's look
window.backgroundColor = .white.withAlphaComponent(0.001)
// Apply background blur
if let app = (NSApplication.shared.delegate as? AppDelegate)?.ghostty.app {
ghostty_set_window_background_blur(app, Unmanaged.passUnretained(window).toOpaque())
}
}
// Toggle opacity state
window.isOpaque = !window.isOpaque

if window.isOpaque {
window.backgroundColor = NSColor(self.derivedConfig.backgroundColor)
} else {
Ghostty.logger.warning("toggle background opacity: no terminal window found")
// This is weird, but we don't use ".clear" because this creates a look that
// matches Terminal.app much more closer. This lets users transition from
// Terminal.app more easily.
window.backgroundColor = .white.withAlphaComponent(0.001)
guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return }
ghostty_set_window_background_blur(appDelegate.ghostty.app, Unmanaged.passUnretained(window).toOpaque())
}
}

Expand Down

0 comments on commit 354bd2f

Please sign in to comment.