Declarative, state-driven views for GTK?

Hello! I’ve looked at GTK several times over the years, but I haven’t ever built anything beyond following some simple “getting started” tutorials. I’d like to change that. :slight_smile:

I’m wondering if there are any libraries that provide a declarative/reactive framework for working with the GTK API. Specifically, I’m thinking of the paradigm where a component (or the entire application) is treated as a pure function from “state” → “view,” and the application author isn’t responsible for imperatively updating view code (i.e. the framework manages updating the view when the state changes).

This approach was popularized on the web by the likes of React and The Elm Architecture, and it seems to be gaining traction in the world of mobile development with SwiftUI. I’m wondering if there’s anything similar for GTK. I found the gi-gtk-declarative Haskell project, but it doesn’t seem to have plans to update to GTK 4. Are there alternatives that I’m missing?

There’s relm4, which is an Elm-like architecture for Rust and GTK4, based on the excellent gtk-rs bindings.

In general, though, it’s perfectly possible to implement state-driven views for GTK4 without additional fancy syntax:

  • create GObject types that represent the state, and have readable properties that notify of changes
  • create GTK widgets that represent the view
  • use property bindings between the view widget and the model object, so that changes in the object are propagated to the widget
  • if you’re using list views, then you can also use expressions, which are nested, unidirectional bindings
  • you can also directly use the GObject::notify signal if you have more complex requirements

You can also express property bindings and expressions inside GtkBuilder UI definition files.

The GTK4 demo application that is included in GTK has various examples of this kind of programming model, and it’s definitely how we GTK developers recommend writing new applications.

1 Like

I knew there was something in the Rust world, I just couldn’t remember what it was. :smiley: Relm is exactly the sort of thing I was looking for, and I’m happy to see it’s been updated/rewritten for GTK 4.

The “property bindings” approach is also interesting. I’ll try to understand that better as I get started playing around with GTK. Thanks for the quick answer!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.