From c858cd693c8c4d0221c6a303946e7a98ed0a4386 Mon Sep 17 00:00:00 2001 From: Adam Wolf Date: Tue, 14 Jan 2025 21:28:36 -0600 Subject: [PATCH] apprt/gtk: add toggle_background_opacity option --- include/ghostty.h | 1 + src/Surface.zig | 6 ++++++ src/apprt/action.zig | 4 ++++ src/apprt/glfw.zig | 1 + src/apprt/gtk/App.zig | 20 ++++++++++++++++++++ src/apprt/gtk/Window.zig | 11 +++++++++++ src/input/Binding.zig | 4 ++++ 7 files changed, 47 insertions(+) diff --git a/include/ghostty.h b/include/ghostty.h index 246fb9ed35..4ce91664a2 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -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, diff --git a/src/Surface.zig b/src/Surface.zig index 5a1d8c01d4..287ea4d360 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -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, diff --git a/src/apprt/action.zig b/src/apprt/action.zig index fe2039e523..a0a5994fec 100644 --- a/src/apprt/action.zig +++ b/src/apprt/action.zig @@ -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, @@ -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, diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index 686a70ddb6..6473ecd4e1 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -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, diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index f49d275de7..3dd59364af 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -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 @@ -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(), diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index 3c8c2c2e7d..121af08c32 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -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"); + } +} + /// Grabs focus on the currently selected tab. pub fn focusCurrentTab(self: *Window) void { const tab = self.notebook.currentTab() orelse return; diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 48725fb13e..8646ac8212 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -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. @@ -743,6 +746,7 @@ pub const Action = union(enum) { .toggle_maximize, .toggle_fullscreen, .toggle_window_decorations, + .toggle_background_opacity, .toggle_secure_input, .crash, => .surface,