Skip to content

Commit

Permalink
gtk: implement command palette
Browse files Browse the repository at this point in the history
  • Loading branch information
pluiedev committed Feb 17, 2025
1 parent da32534 commit 4b394d7
Show file tree
Hide file tree
Showing 10 changed files with 1,009 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/ghostty.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ typedef enum {
GHOSTTY_ACTION_COLOR_CHANGE,
GHOSTTY_ACTION_RELOAD_CONFIG,
GHOSTTY_ACTION_CONFIG_CHANGE,
GHOSTTY_ACTION_TOGGLE_COMMAND_PALETTE,
} ghostty_action_tag_e;

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

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

.toggle_secure_input => return try self.rt_app.performAction(
.{ .surface = self },
.secure_input,
Expand Down
4 changes: 4 additions & 0 deletions src/apprt/action.zig
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ pub const Action = union(Key) {
/// for changes.
config_change: ConfigChange,

/// Toggle the command palette
toggle_command_palette,

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

/// 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 @@ -222,6 +222,7 @@ pub const App = struct {
.close_tab,
.toggle_tab_overview,
.toggle_window_decorations,
.toggle_command_palette,
.toggle_quick_terminal,
.toggle_visibility,
.goto_tab,
Expand Down
24 changes: 23 additions & 1 deletion src/apprt/gtk/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ pub fn performAction(
.toggle_tab_overview => self.toggleTabOverview(target),
.toggle_split_zoom => self.toggleSplitZoom(target),
.toggle_window_decorations => self.toggleWindowDecorations(target),
.toggle_command_palette => return try self.toggleCommandPalette(target),
.quit_timer => self.quitTimer(value),

// Unimplemented
Expand Down Expand Up @@ -739,7 +740,7 @@ fn toggleWindowDecorations(
.surface => |v| {
const window = v.rt_surface.container.window() orelse {
log.info(
"toggleFullscreen invalid for container={s}",
"toggleWindowDecorations invalid for container={s}",
.{@tagName(v.rt_surface.container)},
);
return;
Expand All @@ -750,6 +751,27 @@ fn toggleWindowDecorations(
}
}

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

window.toggleCommandPalette();
return true;
},
}
}

fn quitTimer(self: *App, mode: apprt.action.QuitTimer) void {
switch (mode) {
.start => self.startQuitTimer(),
Expand Down
20 changes: 19 additions & 1 deletion src/apprt/gtk/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const adwaita = @import("adwaita.zig");
const gtk_key = @import("key.zig");
const TabView = @import("TabView.zig");
const HeaderBar = @import("headerbar.zig");
const CommandPalette = @import("command_palette.zig").CommandPalette;
const version = @import("version.zig");
const winproto = @import("winproto.zig");

Expand All @@ -49,6 +50,8 @@ context_menu: *c.GtkWidget,
/// The libadwaita widget for receiving toast send requests.
toast_overlay: *c.GtkWidget,

command_palette: ?*CommandPalette = null,

/// See adwTabOverviewOpen for why we have this.
adw_tab_overview_focus_timer: ?c.guint = null,

Expand Down Expand Up @@ -226,6 +229,15 @@ pub fn init(self: *Window, app: *App) !void {
);
c.gtk_box_append(@ptrCast(box), self.toast_overlay);

if (adwaita.versionAtLeast(1, 5, 0)) {
const command_palette = try CommandPalette.new(self);
// We manually reference the command palette here
// to prevent it from being destroyed when the dialog closes
command_palette.ref();
self.command_palette = command_palette;
}
errdefer if (self.command_palette) |palette| palette.unref();

// If we have a tab overview then we can set it on our notebook.
if (self.tab_overview) |tab_overview| {
if (!adwaita.versionAtLeast(1, 4, 0)) unreachable;
Expand Down Expand Up @@ -427,6 +439,7 @@ pub fn deinit(self: *Window) void {
c.gtk_widget_unparent(@ptrCast(self.context_menu));

self.winproto.deinit(self.app.core_app.alloc);
if (self.command_palette) |palette| palette.unref();

if (self.adw_tab_overview_focus_timer) |timer| {
_ = c.g_source_remove(timer);
Expand Down Expand Up @@ -544,6 +557,11 @@ pub fn toggleWindowDecorations(self: *Window) void {
self.updateConfig(&self.app.config) catch {};
}

/// Toggle the command palette for this window.
pub fn toggleCommandPalette(self: *Window) void {
if (self.command_palette) |palette| palette.present();
}

/// Grabs focus on the currently selected tab.
pub fn focusCurrentTab(self: *Window) void {
const tab = self.notebook.currentTab() orelse return;
Expand Down Expand Up @@ -1018,7 +1036,7 @@ fn gtkActionReset(
}

/// Returns the surface to use for an action.
fn actionSurface(self: *Window) ?*CoreSurface {
pub fn actionSurface(self: *Window) ?*CoreSurface {
const tab = self.notebook.currentTab() orelse return null;
const surface = tab.focus_child orelse return null;
return &surface.core_surface;
Expand Down
Loading

0 comments on commit 4b394d7

Please sign in to comment.