refactor(macos): centralize background opacity toggling across controllers

This commit is contained in:
davidsanchez222
2026-04-05 12:35:29 -04:00
parent e5c31e8b37
commit 0e49204b95
2 changed files with 13 additions and 33 deletions

View File

@@ -767,7 +767,7 @@ class BaseTerminalController: NSWindowController,
tree: newTree,
position: notification.userInfo?[Notification.Name.ghosttySurfaceDragEndedNoTargetPointKey] as? NSPoint,
confirmUndo: false,
inheritBackgroundOpacityFrom: self as? TerminalController)
inheritBackgroundOpacity: isBackgroundOpaque)
}
// MARK: Local Events
@@ -991,11 +991,15 @@ class BaseTerminalController: NSWindowController,
// Do nothing if in fullscreen (transparency doesn't apply in fullscreen)
guard let window, !window.styleMask.contains(.fullScreen) else { return }
// Toggle between transparent and opaque
isBackgroundOpaque.toggle()
let newValue = !isBackgroundOpaque
let controllers = NSApplication.shared.windows.compactMap {
$0.windowController as? BaseTerminalController
}
// Update our appearance
syncAppearance()
for controller in controllers {
controller.isBackgroundOpaque = newValue
controller.syncAppearance()
}
}
/// Override this to resync any appearance related properties. This will be called automatically

View File

@@ -316,11 +316,11 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr
tree: SplitTree<Ghostty.SurfaceView>,
position: NSPoint? = nil,
confirmUndo: Bool = true,
inheritBackgroundOpacityFrom parentController: TerminalController? = nil
inheritBackgroundOpacity: Bool? = nil
) -> TerminalController {
let c = TerminalController.init(ghostty, withSurfaceTree: tree)
if let parentController {
c.isBackgroundOpaque = parentController.isBackgroundOpaque
if let inheritBackgroundOpacity {
c.isBackgroundOpaque = inheritBackgroundOpacity
}
// Calculate the target frame based on the tree's view bounds
@@ -369,7 +369,7 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr
_ = TerminalController.newWindow(
ghostty,
tree: tree,
inheritBackgroundOpacityFrom: parentController
inheritBackgroundOpacity: inheritBackgroundOpacity
)
}
}
@@ -503,30 +503,6 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr
return controller
}
override func toggleBackgroundOpacity() {
// Do nothing if config is already fully opaque
guard ghostty.config.backgroundOpacity < 1 else { return }
// Do nothing if in fullscreen (transparency doesn't apply in fullscreen)
guard let window, !window.styleMask.contains(.fullScreen) else { return }
let newValue = !isBackgroundOpaque
let controllers: [TerminalController]
if let tabGroup = window.tabGroup {
controllers = tabGroup.windows.compactMap {
$0.windowController as? TerminalController
}
} else {
controllers = [self]
}
for controller in controllers {
controller.isBackgroundOpaque = newValue
controller.syncAppearance()
}
}
// MARK: - Methods
@objc private func ghosttyConfigDidChange(_ notification: Notification) {