Rust+GNOME Hackfest #5

Last week, I went to the fifth Rust+GNOME hackfest which was in Berlin again.

Generate builder for widgets

My goal for this hackfest was to fix this issue I opened nearly three years ago. The problem is that sometimes you want to create a widget or an object and set some properties at construction time. This might be needed when you want to set construct-only properties. For instance, you might want to create a webkit2gtk::WebView with a WebContext and a UserContentManager at the same time. That’s why a constructor was manually written for this use case.

Now, these manual implementations will become useless thanks to work I did during the Hackfest.

Now, whenever we tell gir to generate a specific builder, it will, so for the WebView, we’ll be able to do something like this:

let webview = WebViewBuilder::new()
    .user_content_manager(user_content_manager)
    .web_context(context)
    .build();

This PR allows generating builders for construct, construct-only and writable properties as well as the properties of any of the ancestors of the object.

Another good point of this work is that all of this is type-safe. If you look at the manual implementation more closely, you’ll see that it uses strings to specify which properties to set. That means the previous code was error-prone as there were no way to check at compile-time that the right type was used for the values.

A builder setter method looks like this:

pub fn text_lock(mut self, text_lock: &str) -> Self {
    self.text_lock = Some(text_lock.to_string());
    self
}

So the user must provide the correct type.

We will generate the builders on a case-by-case basis: for instance, the next version of webkit2gtk will have a WebViewBuilder.

Progress on relm

I also worked on relm to fix two long-standing issues: reduce the number of crates and remove a reference cycle.

Merge crates.

The relm project had a very big number of crates which was confusing for users: it used to have 9 crates. Now, the number is reduced to 3: relm itself, relm-derive which provides the procedural macros and relm-test to do GUI testing. The reason relm had such a big number of crates was because sometimes you might want to use it in a glib context without a graphical user interface. That’s what I did in a web browser extension. In this case, since there is no GUI, you don’t want to use all features of relm.

One remaining thing I need to do is to make some crates like gtk optional for this use case.

Reference cycle

Another issue I wanted to fix was the reference cycle issue. Since the EventStream type contains a Rc<T> and it was cloned in GTK+ callbacks without downgrading them to a Weak reference, this stream was never released.

This pull request now uses a weak reference which fixes the issue.

I also simplified the way the messages are processed which will also make the integration with other toolkits (like Qt) much simpler.

This pull request is not merged yet because there’s still an issue to fix, but it will be hopefully resolved soon.

Thanks

A big thank to Zeeshan Ali for organizing the Hackfest, a big thank to Kinvolk for the venue and a huge thank to the GNOME foundation for sponsoring my flight and accommodation to allow me to participate to this Hackfest.

GNOME Foundation