Skip to content

Commit 22f3e60

Browse files
authored
gtk: convert window actions to use zig-gobject (#5978)
2 parents 92340f8 + 3d08b1c commit 22f3e60

File tree

1 file changed

+78
-85
lines changed

1 file changed

+78
-85
lines changed

src/apprt/gtk/Window.zig

+78-85
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ const Window = @This();
77

88
const std = @import("std");
99
const builtin = @import("builtin");
10-
const build_config = @import("../../build_config.zig");
1110
const Allocator = std.mem.Allocator;
1211
const assert = std.debug.assert;
12+
13+
const gio = @import("gio");
14+
const glib = @import("glib");
15+
const gobject = @import("gobject");
16+
const gtk = @import("gtk");
17+
18+
const build_config = @import("../../build_config.zig");
1319
const configpkg = @import("../../config.zig");
1420
const font = @import("../../font/main.zig");
1521
const input = @import("../../input.zig");
@@ -475,36 +481,38 @@ fn toggleCssClass(
475481
/// menus and such. The menu is defined in App.zig but the action is defined
476482
/// here. The string name binds them.
477483
fn initActions(self: *Window) void {
484+
// FIXME: when rest of file is converted to gobject
485+
const window: *gtk.ApplicationWindow = @ptrCast(@alignCast(self.window));
486+
const action_map = window.as(gio.ActionMap);
478487
const actions = .{
479-
.{ "about", &gtkActionAbout },
480-
.{ "close", &gtkActionClose },
481-
.{ "new-window", &gtkActionNewWindow },
482-
.{ "new-tab", &gtkActionNewTab },
483-
.{ "close-tab", &gtkActionCloseTab },
484-
.{ "split-right", &gtkActionSplitRight },
485-
.{ "split-down", &gtkActionSplitDown },
486-
.{ "split-left", &gtkActionSplitLeft },
487-
.{ "split-up", &gtkActionSplitUp },
488-
.{ "toggle-inspector", &gtkActionToggleInspector },
489-
.{ "copy", &gtkActionCopy },
490-
.{ "paste", &gtkActionPaste },
491-
.{ "reset", &gtkActionReset },
492-
.{ "clear", &gtkActionClear },
493-
.{ "prompt-title", &gtkActionPromptTitle },
488+
.{ "about", gtkActionAbout },
489+
.{ "close", gtkActionClose },
490+
.{ "new-window", gtkActionNewWindow },
491+
.{ "new-tab", gtkActionNewTab },
492+
.{ "close-tab", gtkActionCloseTab },
493+
.{ "split-right", gtkActionSplitRight },
494+
.{ "split-down", gtkActionSplitDown },
495+
.{ "split-left", gtkActionSplitLeft },
496+
.{ "split-up", gtkActionSplitUp },
497+
.{ "toggle-inspector", gtkActionToggleInspector },
498+
.{ "copy", gtkActionCopy },
499+
.{ "paste", gtkActionPaste },
500+
.{ "reset", gtkActionReset },
501+
.{ "clear", gtkActionClear },
502+
.{ "prompt-title", gtkActionPromptTitle },
494503
};
495504

496505
inline for (actions) |entry| {
497-
const action = c.g_simple_action_new(entry[0], null);
498-
defer c.g_object_unref(action);
499-
_ = c.g_signal_connect_data(
506+
const action = gio.SimpleAction.new(entry[0], null);
507+
defer action.unref();
508+
_ = gio.SimpleAction.signals.activate.connect(
500509
action,
501-
"activate",
502-
c.G_CALLBACK(entry[1]),
510+
*Window,
511+
entry[1],
503512
self,
504-
null,
505-
c.G_CONNECT_DEFAULT,
513+
.{},
506514
);
507-
c.g_action_map_add_action(@ptrCast(self.window), @ptrCast(action));
515+
action_map.addAction(action.as(gio.Action));
508516
}
509517
}
510518

@@ -878,12 +886,10 @@ fn gtkKeyPressed(
878886
}
879887

880888
fn gtkActionAbout(
881-
_: *c.GSimpleAction,
882-
_: *c.GVariant,
883-
ud: ?*anyopaque,
889+
_: *gio.SimpleAction,
890+
_: ?*glib.Variant,
891+
self: *Window,
884892
) callconv(.C) void {
885-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
886-
887893
const name = "Ghostty";
888894
const icon = "com.mitchellh.ghostty";
889895
const website = "https://ghostty.org";
@@ -924,20 +930,18 @@ fn gtkActionAbout(
924930
}
925931

926932
fn gtkActionClose(
927-
_: *c.GSimpleAction,
928-
_: *c.GVariant,
929-
ud: ?*anyopaque,
933+
_: *gio.SimpleAction,
934+
_: ?*glib.Variant,
935+
self: *Window,
930936
) callconv(.C) void {
931-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
932937
c.gtk_window_destroy(self.window);
933938
}
934939

935940
fn gtkActionNewWindow(
936-
_: *c.GSimpleAction,
937-
_: *c.GVariant,
938-
ud: ?*anyopaque,
941+
_: *gio.SimpleAction,
942+
_: ?*glib.Variant,
943+
self: *Window,
939944
) callconv(.C) void {
940-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
941945
const surface = self.actionSurface() orelse return;
942946
_ = surface.performBindingAction(.{ .new_window = {} }) catch |err| {
943947
log.warn("error performing binding action error={}", .{err});
@@ -946,20 +950,19 @@ fn gtkActionNewWindow(
946950
}
947951

948952
fn gtkActionNewTab(
949-
_: *c.GSimpleAction,
950-
_: *c.GVariant,
951-
ud: ?*anyopaque,
953+
_: *gio.SimpleAction,
954+
_: ?*glib.Variant,
955+
self: *Window,
952956
) callconv(.C) void {
953957
// We can use undefined because the button is not used.
954-
gtkTabNewClick(undefined, ud);
958+
gtkTabNewClick(undefined, self);
955959
}
956960

957961
fn gtkActionCloseTab(
958-
_: *c.GSimpleAction,
959-
_: *c.GVariant,
960-
ud: ?*anyopaque,
962+
_: *gio.SimpleAction,
963+
_: ?*glib.Variant,
964+
self: *Window,
961965
) callconv(.C) void {
962-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
963966
const surface = self.actionSurface() orelse return;
964967
_ = surface.performBindingAction(.{ .close_tab = {} }) catch |err| {
965968
log.warn("error performing binding action error={}", .{err});
@@ -968,11 +971,10 @@ fn gtkActionCloseTab(
968971
}
969972

970973
fn gtkActionSplitRight(
971-
_: *c.GSimpleAction,
972-
_: *c.GVariant,
973-
ud: ?*anyopaque,
974+
_: *gio.SimpleAction,
975+
_: ?*glib.Variant,
976+
self: *Window,
974977
) callconv(.C) void {
975-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
976978
const surface = self.actionSurface() orelse return;
977979
_ = surface.performBindingAction(.{ .new_split = .right }) catch |err| {
978980
log.warn("error performing binding action error={}", .{err});
@@ -981,11 +983,10 @@ fn gtkActionSplitRight(
981983
}
982984

983985
fn gtkActionSplitDown(
984-
_: *c.GSimpleAction,
985-
_: *c.GVariant,
986-
ud: ?*anyopaque,
986+
_: *gio.SimpleAction,
987+
_: ?*glib.Variant,
988+
self: *Window,
987989
) callconv(.C) void {
988-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
989990
const surface = self.actionSurface() orelse return;
990991
_ = surface.performBindingAction(.{ .new_split = .down }) catch |err| {
991992
log.warn("error performing binding action error={}", .{err});
@@ -994,11 +995,10 @@ fn gtkActionSplitDown(
994995
}
995996

996997
fn gtkActionSplitLeft(
997-
_: *c.GSimpleAction,
998-
_: *c.GVariant,
999-
ud: ?*anyopaque,
998+
_: *gio.SimpleAction,
999+
_: ?*glib.Variant,
1000+
self: *Window,
10001001
) callconv(.C) void {
1001-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
10021002
const surface = self.actionSurface() orelse return;
10031003
_ = surface.performBindingAction(.{ .new_split = .left }) catch |err| {
10041004
log.warn("error performing binding action error={}", .{err});
@@ -1007,11 +1007,10 @@ fn gtkActionSplitLeft(
10071007
}
10081008

10091009
fn gtkActionSplitUp(
1010-
_: *c.GSimpleAction,
1011-
_: *c.GVariant,
1012-
ud: ?*anyopaque,
1010+
_: *gio.SimpleAction,
1011+
_: ?*glib.Variant,
1012+
self: *Window,
10131013
) callconv(.C) void {
1014-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
10151014
const surface = self.actionSurface() orelse return;
10161015
_ = surface.performBindingAction(.{ .new_split = .up }) catch |err| {
10171016
log.warn("error performing binding action error={}", .{err});
@@ -1020,11 +1019,10 @@ fn gtkActionSplitUp(
10201019
}
10211020

10221021
fn gtkActionToggleInspector(
1023-
_: *c.GSimpleAction,
1024-
_: *c.GVariant,
1025-
ud: ?*anyopaque,
1022+
_: *gio.SimpleAction,
1023+
_: ?*glib.Variant,
1024+
self: *Window,
10261025
) callconv(.C) void {
1027-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
10281026
const surface = self.actionSurface() orelse return;
10291027
_ = surface.performBindingAction(.{ .inspector = .toggle }) catch |err| {
10301028
log.warn("error performing binding action error={}", .{err});
@@ -1033,11 +1031,10 @@ fn gtkActionToggleInspector(
10331031
}
10341032

10351033
fn gtkActionCopy(
1036-
_: *c.GSimpleAction,
1037-
_: *c.GVariant,
1038-
ud: ?*anyopaque,
1034+
_: *gio.SimpleAction,
1035+
_: ?*glib.Variant,
1036+
self: *Window,
10391037
) callconv(.C) void {
1040-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
10411038
const surface = self.actionSurface() orelse return;
10421039
_ = surface.performBindingAction(.{ .copy_to_clipboard = {} }) catch |err| {
10431040
log.warn("error performing binding action error={}", .{err});
@@ -1046,11 +1043,10 @@ fn gtkActionCopy(
10461043
}
10471044

10481045
fn gtkActionPaste(
1049-
_: *c.GSimpleAction,
1050-
_: *c.GVariant,
1051-
ud: ?*anyopaque,
1046+
_: *gio.SimpleAction,
1047+
_: ?*glib.Variant,
1048+
self: *Window,
10521049
) callconv(.C) void {
1053-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
10541050
const surface = self.actionSurface() orelse return;
10551051
_ = surface.performBindingAction(.{ .paste_from_clipboard = {} }) catch |err| {
10561052
log.warn("error performing binding action error={}", .{err});
@@ -1059,11 +1055,10 @@ fn gtkActionPaste(
10591055
}
10601056

10611057
fn gtkActionReset(
1062-
_: *c.GSimpleAction,
1063-
_: *c.GVariant,
1064-
ud: ?*anyopaque,
1058+
_: *gio.SimpleAction,
1059+
_: ?*glib.Variant,
1060+
self: *Window,
10651061
) callconv(.C) void {
1066-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
10671062
const surface = self.actionSurface() orelse return;
10681063
_ = surface.performBindingAction(.{ .reset = {} }) catch |err| {
10691064
log.warn("error performing binding action error={}", .{err});
@@ -1072,11 +1067,10 @@ fn gtkActionReset(
10721067
}
10731068

10741069
fn gtkActionClear(
1075-
_: *c.GSimpleAction,
1076-
_: *c.GVariant,
1077-
ud: ?*anyopaque,
1070+
_: *gio.SimpleAction,
1071+
_: ?*glib.Variant,
1072+
self: *Window,
10781073
) callconv(.C) void {
1079-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
10801074
const surface = self.actionSurface() orelse return;
10811075
_ = surface.performBindingAction(.{ .clear_screen = {} }) catch |err| {
10821076
log.warn("error performing binding action error={}", .{err});
@@ -1085,11 +1079,10 @@ fn gtkActionClear(
10851079
}
10861080

10871081
fn gtkActionPromptTitle(
1088-
_: *c.GSimpleAction,
1089-
_: *c.GVariant,
1090-
ud: ?*anyopaque,
1082+
_: *gio.SimpleAction,
1083+
_: ?*glib.Variant,
1084+
self: *Window,
10911085
) callconv(.C) void {
1092-
const self: *Window = @ptrCast(@alignCast(ud orelse return));
10931086
const surface = self.actionSurface() orelse return;
10941087
_ = surface.performBindingAction(.{ .prompt_surface_title = {} }) catch |err| {
10951088
log.warn("error performing binding action error={}", .{err});

0 commit comments

Comments
 (0)