Gtk-rs Is Pretty Neat

I’m sure by now you’ve seen the gtk-rs bindings which keep getting better

Recently I put together a little demo1 taking advantage of gtk-rs to bind a small GLib/C library providing a simple object, extend that object in rust and ultimatly consume it in GJS2.

The name ‘gtk-rs’, like ‘gtk-doc’, is a bit of a misdirect. Whilst the project’s main goal is Gtk bindings it’s really a system for building rust crates wrapping GLib-style libraries via gobject-introspection. Much of the work is done by the gir3 4 tool which both generates the low-level C ffi and automates most of the high-level binding – with the notable exception of subclass support.

Fortunately, we can look at the Gtk4 bindings for help, I found layout_manager.rs to be a useful example – one gotcha to watch out for is vfuncs, especially signals, as they don’t necessarily have a default implementation

Unfortunatly gtk-rs doesn’t have any magic for generating introspection data (yet?) so we follow rsvg’s example and manually build C-ABI wrappers along with a hand-written header to use with g-ir-scannerhandle.rs is a useful resource when doing this.

Happy multi-language hacking 🙂


Footnotes

1
This was orignally an minimal reproducer, turns out I was just being silly
2
Of course it doesn’t have to be GJS, PyGObject etc should work as well
3
You could think of it as a high-level (and specialised) version of bindgen
4
The name is a tad confusing and may change

Thanks to Bilal, Jordan, Chris, Sebastian, and others for their tips/fixes and/or rubber ducking as well as the wider gtk-rs/g-i communities who build this tooling

One thought on “Gtk-rs Is Pretty Neat

  1. This is exactly the example I’ve been hoping someone would write! (But it never got high enough on my to-do list to do all the work to figure out how I would do it myself.) Thank you so much!

Leave a Reply

Your email address will not be published. Required fields are marked *