In Zterm I had been keeping some convenience functions for using Gtk+ in a separate file. Originally this was pretty small, but over time I’ve been adding to it and am now to the point where I think others might find it useful, or I might even want to re-use it myself. So I forked it off into a separate repo and have started expanding it a bit.
Some of this is just the boilerplate that you would need such as importing the headers and fixing a couple functions that
zig translate-c can’t currently cope with due to macro-abuse. But I’ve also been adding some nice things like wrappers that take a native Zig type as parameters or return native Zig types. For instance getting the state of a GtkToggleButton returns either 1 or 0 using the C api, but returns a native bool using the wrapper function.
I’ve been considering taking it further, and have a little prototype of the concept for the GtkBox widget, where I have a Box struct with a single member, which is the pointer to the GtkBox widget.
// Creating a GtkBox using the C api const box = gtk.gtk_box_new( @intToEnum(GtkOrientation, GTK_ORIENTATION_HORIZONTAL), 2, ); // Same, using the wrapper const zigbox = gtk.Box.new(gtk.Orientation.vertical, 3); // gtk.Orientation.vertical is a Zig enum that maps to the C equivalent, no need to do the @intToEnum // Packing a widget into the box (C api) gtk.gtk_box_pack_start(@ptrCast(*gtk.GtkBox, box), some_widget, 0, 1, 1); // bindings api zigbox.pack_start(some_widget, false, true, 1);
In addition to reducing boilerplate and the number of times it’s neccesary to cal @ptrCast and @intToEnum, I think it makes it way easier to track what the types actually are when you’re using real booleans and such, and less clutter leads to better readability.