gtkrs-tutorials icon indicating copy to clipboard operation
gtkrs-tutorials copied to clipboard

Update to GTK-rs 0.14

Open EndilWayfare opened this issue 4 years ago • 2 comments

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?).

EndilWayfare avatar Jul 16 '21 20:07 EndilWayfare

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.

mmstick avatar Jul 18 '21 17:07 mmstick

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.

EndilWayfare avatar Jul 19 '21 20:07 EndilWayfare