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 option #5086

Open
wants to merge 1 commit 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
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
20 changes: 20 additions & 0 deletions src/apprt/gtk/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ pub fn performAction(
.toggle_tab_overview => self.toggleTabOverview(target),
.toggle_split_zoom => self.toggleSplitZoom(target),
.toggle_window_decorations => self.toggleWindowDecorations(target),
.toggle_background_opacity => self.toggleBackgroundOpacity(target),
.quit_timer => self.quitTimer(value),

// Unimplemented
Expand Down Expand Up @@ -791,6 +792,25 @@ fn toggleWindowDecorations(
}
}

fn toggleBackgroundOpacity(
_: *App,
target: apprt.Target,
) void {
switch (target) {
.app => {},
.surface => |v| {
const window = v.rt_surface.container.window() orelse {
log.info(
"toggleBackgroundOpacity invalid for container={s}",
.{@tagName(v.rt_surface.container)},
);
return;
};
window.toggleBackgroundOpacity();
},
}
}

fn quitTimer(self: *App, mode: apprt.action.QuitTimer) void {
switch (mode) {
.start => self.startQuitTimer(),
Expand Down
11 changes: 11 additions & 0 deletions src/apprt/gtk/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,17 @@ pub fn toggleWindowDecorations(self: *Window) void {
self.headerbar.setVisible(new_decorated);
}

/// Toggle the background opacity for this window.
pub fn toggleBackgroundOpacity(self: *Window) void {
if (self.app.config.@"background-opacity" >= 1) return;

if (c.gtk_widget_has_css_class(@ptrCast(self.window), "background") == 1) {
c.gtk_widget_remove_css_class(@ptrCast(self.window), "background");
} else {
c.gtk_widget_add_css_class(@ptrCast(self.window), "background");
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually update the config for the OpenGL renderer, so the actual terminal screen will still render as if they're transparent (and likely have wrong colors)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, didn't even think about that. Let me take a look at that as well

/// Grabs focus on the currently selected tab.
pub fn focusCurrentTab(self: *Window) void {
const tab = self.notebook.currentTab() orelse return;
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