Skip to content

Commit

Permalink
gtk: add separate close_window apprt action
Browse files Browse the repository at this point in the history
For *some* reason we have a binding for close_window but it merely closes
the surface and not the entire window. That is not only misleading but
also just wrong. Now we make a separate apprt action for close_window
that would make it show a close confirmation prompt identical to as if
the user had clicked the (X) button on the window titlebar.
  • Loading branch information
pluiedev committed Mar 6, 2025
1 parent b4bfdb2 commit 9ed7672
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/ghostty.h
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ typedef enum {
GHOSTTY_ACTION_COLOR_CHANGE,
GHOSTTY_ACTION_RELOAD_CONFIG,
GHOSTTY_ACTION_CONFIG_CHANGE,
GHOSTTY_ACTION_CLOSE_WINDOW,
} ghostty_action_tag_e;

typedef union {
Expand Down
6 changes: 5 additions & 1 deletion src/Surface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4292,7 +4292,11 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool

.close_surface => self.close(),

.close_window => self.app.closeSurface(self),
.close_window => return try self.rt_app.performAction(
.{ .surface = self },
.close_window,
{},
),

.crash => |location| switch (location) {
.main => @panic("crash binding action, crashing intentionally"),
Expand Down
4 changes: 4 additions & 0 deletions src/apprt/action.zig
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ pub const Action = union(Key) {
/// for changes.
config_change: ConfigChange,

/// Closes the currently focused window.
close_window,

/// Sync with: ghostty_action_tag_e
pub const Key = enum(c_int) {
quit,
Expand Down Expand Up @@ -283,6 +286,7 @@ pub const Action = union(Key) {
color_change,
reload_config,
config_change,
close_window,
};

/// Sync with: ghostty_action_u
Expand Down
1 change: 1 addition & 0 deletions src/apprt/glfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ pub const App = struct {
.toggle_split_zoom,
.present_terminal,
.close_all_windows,
.close_window,
.close_tab,
.toggle_tab_overview,
.toggle_window_decorations,
Expand Down
21 changes: 17 additions & 4 deletions src/apprt/gtk/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,11 @@ pub fn performAction(
.app => null,
.surface => |v| v,
}),
.close_window => return try self.closeWindow(target),
.toggle_maximize => self.toggleMaximize(target),
.toggle_fullscreen => self.toggleFullscreen(target, value),
.new_tab => try self.newTab(target),
.close_tab => try self.closeTab(target),
.close_tab => return try self.closeTab(target),
.goto_tab => return self.gotoTab(target, value),
.move_tab => self.moveTab(target, value),
.new_split => try self.newSplit(target, value),
Expand Down Expand Up @@ -549,19 +550,20 @@ fn newTab(_: *App, target: apprt.Target) !void {
}
}

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

tab.closeWithConfirmation();
return true;
},
}
}
Expand Down Expand Up @@ -1426,6 +1428,17 @@ fn setSecureInput(_: *App, target: apprt.Target, value: apprt.action.SecureInput
}
}

fn closeWindow(_: *App, target: apprt.action.Target) !bool {
switch (target) {
.app => return false,
.surface => |v| {
const window = v.rt_surface.container.window() orelse return false;
window.closeWithConfirmation();
return true;
},
}
}

fn quit(self: *App) void {
// If we're already not running, do nothing.
if (!self.running) return;
Expand Down

0 comments on commit 9ed7672

Please sign in to comment.