mmstick / gtkrs-tutorials

2021 Tutorial for GTK-rs
38 stars 4 forks source link

Update to GTK-rs 0.14 #1

Open EndilWayfare opened 3 years ago

EndilWayfare commented 3 years ago

The last code listing in Chapter 1.4 panics for me. gtk-3.0.lib: 3.24.28 glib-2.0.lib: 2.68.1 rustc: 0.53.0 gtk: 0.14.0 glib: 0.14.2

Downgrading to gtk = 0.9, the version used in Chapter 2 example source, resolves the panic.

On current gtk, changing

fn main() {
    ...
    // GLib has an executor in the background that will
    // asynchronously handle our events on this thread
    glib::MainContext::default().spawn_local(event_handler);
    ...
}

to something like

fn main() {
    ...
    // GLib has an executor in the background that will
    // asynchronously handle our events on this thread
    let ctx = glib::MainContext::default();
    // Must use `ctx_guard`; Thread's ownership of the loop is released on `Drop::drop()`.
    let ctx_guard = ctx.acquire().expect("Couldn't acquire ownership of main event loop");
    ctx.spawn_local(event_handler);
    ...
}

also solves the panic.

Just switching to gtk::Application paradigm doesn't work unless you call app.run() instead of gtk::main(). My best guess, without further digging, is that gtk::main used to acquire the default MainContext and somewhere between 0.9 and 0.14 it stopped doing that (presumably for SRP/flexibility purposes?).

mmstick commented 3 years ago

This is just a matter of breaking changes in the API since the tutorial was created. If you'd like to update it, you may.

EndilWayfare commented 3 years ago

Yeah, I figured something like that. I may draft a PR when I get a chance.

Thanks for the tutorial, by the way! In addition to the GTK event-driven design patterns, you introduced me to slotmap, which should be a very useful general dependency going forward.