gtk: convert window actions to use zig-gobject

This commit is contained in:
Jeffrey C. Ollie
2025-02-24 22:07:16 -06:00
parent 92340f8fb0
commit d284146621

View File

@ -7,9 +7,15 @@ const Window = @This();
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const build_config = @import("../../build_config.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const assert = std.debug.assert; const assert = std.debug.assert;
const gio = @import("gio");
const glib = @import("glib");
const gobject = @import("gobject");
const gtk = @import("gtk");
const build_config = @import("../../build_config.zig");
const configpkg = @import("../../config.zig"); const configpkg = @import("../../config.zig");
const font = @import("../../font/main.zig"); const font = @import("../../font/main.zig");
const input = @import("../../input.zig"); const input = @import("../../input.zig");
@ -475,36 +481,38 @@ fn toggleCssClass(
/// menus and such. The menu is defined in App.zig but the action is defined /// menus and such. The menu is defined in App.zig but the action is defined
/// here. The string name binds them. /// here. The string name binds them.
fn initActions(self: *Window) void { fn initActions(self: *Window) void {
// FIXME: when rest of file is converted to gobject
const window: *gtk.Window = @ptrCast(@alignCast(self.window));
const action_map = gobject.ext.cast(gio.ActionMap, window).?;
const actions = .{ const actions = .{
.{ "about", &gtkActionAbout }, .{ "about", gtkActionAbout },
.{ "close", &gtkActionClose }, .{ "close", gtkActionClose },
.{ "new-window", &gtkActionNewWindow }, .{ "new-window", gtkActionNewWindow },
.{ "new-tab", &gtkActionNewTab }, .{ "new-tab", gtkActionNewTab },
.{ "close-tab", &gtkActionCloseTab }, .{ "close-tab", gtkActionCloseTab },
.{ "split-right", &gtkActionSplitRight }, .{ "split-right", gtkActionSplitRight },
.{ "split-down", &gtkActionSplitDown }, .{ "split-down", gtkActionSplitDown },
.{ "split-left", &gtkActionSplitLeft }, .{ "split-left", gtkActionSplitLeft },
.{ "split-up", &gtkActionSplitUp }, .{ "split-up", gtkActionSplitUp },
.{ "toggle-inspector", &gtkActionToggleInspector }, .{ "toggle-inspector", gtkActionToggleInspector },
.{ "copy", &gtkActionCopy }, .{ "copy", gtkActionCopy },
.{ "paste", &gtkActionPaste }, .{ "paste", gtkActionPaste },
.{ "reset", &gtkActionReset }, .{ "reset", gtkActionReset },
.{ "clear", &gtkActionClear }, .{ "clear", gtkActionClear },
.{ "prompt-title", &gtkActionPromptTitle }, .{ "prompt-title", gtkActionPromptTitle },
}; };
inline for (actions) |entry| { inline for (actions) |entry| {
const action = c.g_simple_action_new(entry[0], null); const action = gio.SimpleAction.new(entry[0], null);
defer c.g_object_unref(action); defer action.unref();
_ = c.g_signal_connect_data( _ = gio.SimpleAction.signals.activate.connect(
action, action,
"activate", *Window,
c.G_CALLBACK(entry[1]), entry[1],
self, self,
null, .{},
c.G_CONNECT_DEFAULT,
); );
c.g_action_map_add_action(@ptrCast(self.window), @ptrCast(action)); action_map.addAction(action.as(gio.Action));
} }
} }
@ -878,12 +886,10 @@ fn gtkKeyPressed(
} }
fn gtkActionAbout( fn gtkActionAbout(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const name = "Ghostty"; const name = "Ghostty";
const icon = "com.mitchellh.ghostty"; const icon = "com.mitchellh.ghostty";
const website = "https://ghostty.org"; const website = "https://ghostty.org";
@ -924,20 +930,18 @@ fn gtkActionAbout(
} }
fn gtkActionClose( fn gtkActionClose(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
c.gtk_window_destroy(self.window); c.gtk_window_destroy(self.window);
} }
fn gtkActionNewWindow( fn gtkActionNewWindow(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_window = {} }) catch |err| { _ = surface.performBindingAction(.{ .new_window = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -946,20 +950,19 @@ fn gtkActionNewWindow(
} }
fn gtkActionNewTab( fn gtkActionNewTab(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
// We can use undefined because the button is not used. // We can use undefined because the button is not used.
gtkTabNewClick(undefined, ud); gtkTabNewClick(undefined, self);
} }
fn gtkActionCloseTab( fn gtkActionCloseTab(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .close_tab = {} }) catch |err| { _ = surface.performBindingAction(.{ .close_tab = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -968,11 +971,10 @@ fn gtkActionCloseTab(
} }
fn gtkActionSplitRight( fn gtkActionSplitRight(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_split = .right }) catch |err| { _ = surface.performBindingAction(.{ .new_split = .right }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -981,11 +983,10 @@ fn gtkActionSplitRight(
} }
fn gtkActionSplitDown( fn gtkActionSplitDown(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_split = .down }) catch |err| { _ = surface.performBindingAction(.{ .new_split = .down }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -994,11 +995,10 @@ fn gtkActionSplitDown(
} }
fn gtkActionSplitLeft( fn gtkActionSplitLeft(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_split = .left }) catch |err| { _ = surface.performBindingAction(.{ .new_split = .left }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -1007,11 +1007,10 @@ fn gtkActionSplitLeft(
} }
fn gtkActionSplitUp( fn gtkActionSplitUp(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_split = .up }) catch |err| { _ = surface.performBindingAction(.{ .new_split = .up }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -1020,11 +1019,10 @@ fn gtkActionSplitUp(
} }
fn gtkActionToggleInspector( fn gtkActionToggleInspector(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .inspector = .toggle }) catch |err| { _ = surface.performBindingAction(.{ .inspector = .toggle }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -1033,11 +1031,10 @@ fn gtkActionToggleInspector(
} }
fn gtkActionCopy( fn gtkActionCopy(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .copy_to_clipboard = {} }) catch |err| { _ = surface.performBindingAction(.{ .copy_to_clipboard = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -1046,11 +1043,10 @@ fn gtkActionCopy(
} }
fn gtkActionPaste( fn gtkActionPaste(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .paste_from_clipboard = {} }) catch |err| { _ = surface.performBindingAction(.{ .paste_from_clipboard = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -1059,11 +1055,10 @@ fn gtkActionPaste(
} }
fn gtkActionReset( fn gtkActionReset(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .reset = {} }) catch |err| { _ = surface.performBindingAction(.{ .reset = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -1072,11 +1067,10 @@ fn gtkActionReset(
} }
fn gtkActionClear( fn gtkActionClear(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .clear_screen = {} }) catch |err| { _ = surface.performBindingAction(.{ .clear_screen = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
@ -1085,11 +1079,10 @@ fn gtkActionClear(
} }
fn gtkActionPromptTitle( fn gtkActionPromptTitle(
_: *c.GSimpleAction, _: *gio.SimpleAction,
_: *c.GVariant, _: ?*glib.Variant,
ud: ?*anyopaque, self: *Window,
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .prompt_surface_title = {} }) catch |err| { _ = surface.performBindingAction(.{ .prompt_surface_title = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});