Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toggle_background_opacity action for macOS #5170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/ghostty.h
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ typedef enum {
GHOSTTY_ACTION_TOGGLE_FULLSCREEN,
GHOSTTY_ACTION_TOGGLE_TAB_OVERVIEW,
GHOSTTY_ACTION_TOGGLE_WINDOW_DECORATIONS,
GHOSTTY_ACTION_TOGGLE_BACKGROUND_OPACITY,
GHOSTTY_ACTION_TOGGLE_QUICK_TERMINAL,
GHOSTTY_ACTION_TOGGLE_VISIBILITY,
GHOSTTY_ACTION_MOVE_TAB,
Expand Down
23 changes: 22 additions & 1 deletion macos/Sources/Ghostty/Ghostty.App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ extension Ghostty {
deinit {
// This will force the didSet callbacks to run which free.
self.app = nil

#if os(macOS)
NotificationCenter.default.removeObserver(self)
#endif
Expand Down Expand Up @@ -529,6 +529,9 @@ extension Ghostty {
case GHOSTTY_ACTION_COLOR_CHANGE:
colorChange(app, target: target, change: action.action.color_change)

case GHOSTTY_ACTION_TOGGLE_BACKGROUND_OPACITY:
toggleBackgroundOpacity(app, target: target)

case GHOSTTY_ACTION_CLOSE_ALL_WINDOWS:
fallthrough
case GHOSTTY_ACTION_TOGGLE_TAB_OVERVIEW:
Expand Down Expand Up @@ -1291,6 +1294,24 @@ extension Ghostty {
}
}

private static func toggleBackgroundOpacity(_ app: ghostty_app_t, target: ghostty_target_s) {
switch (target.tag) {
case GHOSTTY_TARGET_APP:
Ghostty.logger.warning("toggle_background_opacity does nothing with an app target")
return

case GHOSTTY_TARGET_SURFACE:
guard let surface = target.target.surface,
let surfaceView = self.surfaceView(from: surface) else { return }
NotificationCenter.default.post(
name: .ghosttyBackgroundOpacityDidToggle,
object: surfaceView
)

default:
assertionFailure()
}
}

// MARK: User Notifications

Expand Down
3 changes: 3 additions & 0 deletions macos/Sources/Ghostty/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ extension Notification.Name {
static let ghosttyColorDidChange = Notification.Name("com.mitchellh.ghostty.ghosttyColorDidChange")
static let GhosttyColorChangeKey = ghosttyColorDidChange.rawValue

/// Background opacity toggle event
static let ghosttyBackgroundOpacityDidToggle = Notification.Name("com.mitchellh.ghostty.backgroundOpacityDidToggle")

/// Goto tab. Has tab index in the userinfo.
static let ghosttyMoveTab = Notification.Name("com.mitchellh.ghostty.moveTab")
static let GhosttyMoveTabKey = ghosttyMoveTab.rawValue
Expand Down
28 changes: 28 additions & 0 deletions macos/Sources/Ghostty/SurfaceView_AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ extension Ghostty {
selector: #selector(windowDidChangeScreen),
name: NSWindow.didChangeScreenNotification,
object: nil)
center.addObserver(
self,
selector: #selector(ghosttyBackgroundOpacityDidToggle),
name: .ghosttyBackgroundOpacityDidToggle,
object: self)

// Listen for local events that we need to know of outside of
// single surface handlers.
Expand Down Expand Up @@ -490,6 +495,29 @@ extension Ghostty {
}
}

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

// Don't toggle transparency if opacity is 1+ or in fullscreen mode
if self.derivedConfig.backgroundOpacity >= 1 || window.styleMask.contains(.fullScreen) {
return
}

// Toggle opacity state
window.isOpaque = !window.isOpaque

if window.isOpaque {
window.backgroundColor = NSColor(self.derivedConfig.backgroundColor)
} else {
// 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())
}
}

// MARK: - NSView

override func becomeFirstResponder() -> Bool {
Expand Down
6 changes: 6 additions & 0 deletions src/Surface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4199,6 +4199,12 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
{},
),

.toggle_background_opacity => try self.rt_app.performAction(
.{ .surface = self },
.toggle_background_opacity,
{},
),

.toggle_tab_overview => try self.rt_app.performAction(
.{ .surface = self },
.toggle_tab_overview,
Expand Down
4 changes: 4 additions & 0 deletions src/apprt/action.zig
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub const Action = union(Key) {
/// Toggle whether window directions are shown.
toggle_window_decorations,

/// Toggle the background opacity of the target terminal.
toggle_background_opacity,

/// Toggle the quick terminal in or out.
toggle_quick_terminal,

Expand Down Expand Up @@ -238,6 +241,7 @@ pub const Action = union(Key) {
toggle_fullscreen,
toggle_tab_overview,
toggle_window_decorations,
toggle_background_opacity,
toggle_quick_terminal,
toggle_visibility,
move_tab,
Expand Down
1 change: 1 addition & 0 deletions src/apprt/glfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pub const App = struct {
.close_tab,
.toggle_tab_overview,
.toggle_window_decorations,
.toggle_background_opacity,
.toggle_quick_terminal,
.toggle_visibility,
.goto_tab,
Expand Down
4 changes: 4 additions & 0 deletions src/input/Binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ pub const Action = union(enum) {
/// Toggle window decorations on and off. This only works on Linux.
toggle_window_decorations: void,

/// Toggle background opacity of window.
toggle_background_opacity: void,

/// Toggle secure input mode on or off. This is used to prevent apps
/// that monitor input from seeing what you type. This is useful for
/// entering passwords or other sensitive information.
Expand Down Expand Up @@ -743,6 +746,7 @@ pub const Action = union(enum) {
.toggle_maximize,
.toggle_fullscreen,
.toggle_window_decorations,
.toggle_background_opacity,
.toggle_secure_input,
.crash,
=> .surface,
Expand Down