gtk: introduce Zig bindings for GTK/GObject (#5560)

`zig-gobject` is a set of GObject bindings that allow us to write
GTK-facing code in Zig instead of getting hands dirty with C. It's been
tested and refined in real-life applications and several GTK
contributors agree that it is a marked improvement over using the C API
directly, such as allowing method call syntax and avoiding many manual
`@ptrCast`s.

This PR doesn't actually contain any changes to our preexisting GTK code
— the migration process is intended to begin in chunks, firstly in
self-contained components (e.g. the header bar, overlays, etc.), and
then full-scale migration can begin when we remove non-Adwaita GTK
builds for 1.2. (After all, why port code that you'll remove later
either way?)
This commit is contained in:
Mitchell Hashimoto
2025-02-11 07:19:59 -08:00
committed by GitHub
5 changed files with 36 additions and 3 deletions

View File

@ -41,6 +41,10 @@
.url = "git+https://github.com/natecraddock/zf/?ref=main#ed99ca18b02dda052e20ba467e90b623c04690dd", .url = "git+https://github.com/natecraddock/zf/?ref=main#ed99ca18b02dda052e20ba467e90b623c04690dd",
.hash = "1220edc3b8d8bedbb50555947987e5e8e2f93871ca3c8e8d4cc8f1377c15b5dd35e8", .hash = "1220edc3b8d8bedbb50555947987e5e8e2f93871ca3c8e8d4cc8f1377c15b5dd35e8",
}, },
.gobject = .{
.url = "https://github.com/ianprime0509/zig-gobject/releases/download/v0.2.2/bindings-gnome47.tar.zst",
.hash = "12208d70ee791d7ef7e16e1c3c9c1127b57f1ed066a24f87d57fc9f730c5dc394b9d",
},
// C libs // C libs
.cimgui = .{ .path = "./pkg/cimgui" }, .cimgui = .{ .path = "./pkg/cimgui" },

View File

@ -30,6 +30,7 @@
glib, glib,
glslang, glslang,
gtk4, gtk4,
gobject-introspection,
libadwaita, libadwaita,
adwaita-icon-theme, adwaita-icon-theme,
hicolor-icon-theme, hicolor-icon-theme,
@ -83,6 +84,7 @@
libadwaita libadwaita
gtk4 gtk4
glib glib
gobject-introspection
wayland wayland
]; ];
in in
@ -157,6 +159,7 @@ in
libadwaita libadwaita
gtk4 gtk4
glib glib
gobject-introspection
wayland wayland
wayland-scanner wayland-scanner
wayland-protocols wayland-protocols

View File

@ -12,6 +12,7 @@
libGL, libGL,
glib, glib,
gtk4, gtk4,
gobject-introspection,
libadwaita, libadwaita,
wrapGAppsHook4, wrapGAppsHook4,
gsettings-desktop-schemas, gsettings-desktop-schemas,
@ -124,6 +125,7 @@ in
pandoc pandoc
pkg-config pkg-config
zig_hook zig_hook
gobject-introspection
wrapGAppsHook4 wrapGAppsHook4
] ]
++ lib.optionals enableWayland [ ++ lib.optionals enableWayland [

View File

@ -1,3 +1,3 @@
# This file is auto-generated! check build-support/check-zig-cache-hash.sh for # This file is auto-generated! check build-support/check-zig-cache-hash.sh for
# more details. # more details.
"sha256-I7uuv0MkaW3gWAw6NHci+II42OfM7NdtKh2Npw2pTis=" "sha256-+Ag900R3lDV7iEeRFGe2HWJDtquW3I9GFvHGle+U3k0="

View File

@ -430,9 +430,32 @@ pub fn add(
}, },
.gtk => { .gtk => {
const gobject = b.dependency("gobject", .{
.target = target,
.optimize = optimize,
});
const gobject_imports = .{
.{ "gobject", "gobject2" },
.{ "glib", "glib2" },
.{ "gtk", "gtk4" },
.{ "gdk", "gdk4" },
};
inline for (gobject_imports) |import| {
const name, const module = import;
step.root_module.addImport(name, gobject.module(module));
}
step.linkSystemLibrary2("gtk4", dynamic_link_opts); step.linkSystemLibrary2("gtk4", dynamic_link_opts);
if (self.config.adwaita) step.linkSystemLibrary2("libadwaita-1", dynamic_link_opts);
if (self.config.x11) step.linkSystemLibrary2("X11", dynamic_link_opts); if (self.config.adwaita) {
step.linkSystemLibrary2("libadwaita-1", dynamic_link_opts);
step.root_module.addImport("adw", gobject.module("adw1"));
}
if (self.config.x11) {
step.linkSystemLibrary2("X11", dynamic_link_opts);
step.root_module.addImport("gdk_x11", gobject.module("gdkx114"));
}
if (self.config.wayland) { if (self.config.wayland) {
const scanner = Scanner.create(b.dependency("zig_wayland", .{}), .{ const scanner = Scanner.create(b.dependency("zig_wayland", .{}), .{
@ -460,6 +483,7 @@ pub fn add(
scanner.generate("org_kde_kwin_server_decoration_manager", 1); scanner.generate("org_kde_kwin_server_decoration_manager", 1);
step.root_module.addImport("wayland", wayland); step.root_module.addImport("wayland", wayland);
step.root_module.addImport("gdk_wayland", gobject.module("gdkwayland4"));
step.linkSystemLibrary2("wayland-client", dynamic_link_opts); step.linkSystemLibrary2("wayland-client", dynamic_link_opts);
} }