szyszka icon indicating copy to clipboard operation
szyszka copied to clipboard

Switch to event-driven model

Open mmstick opened this issue 3 years ago • 2 comments

This will prevent the need to clone your widgets hundreds of times in multiple places, and eliminate the need for shared ownership of application state. Basic idea works as follows:

let (s, r) = async_channel::unbounded();

let mut app = App::new(s);

let event_handler = async move {
    while let Ok(event) = r.recv().await {
        match event {
            Event::ButtonClicked => app.do_task(),
        }
    }
};

glib::MainContext::default().spawn_local(event_handler);
fn block_on<F>(future: F) -> F::Output where F: Future {
    glib::MainContext::default().block_on(future)
}

button.connect_clicked(closure!(clone c, |_| {
    block_on(s.send(Event::ButtonClicked));
});

mmstick avatar May 13 '21 16:05 mmstick

For now I don't have in plans to rewrite this app or create a new one with futures, because I never programmed in such way and for now I don't have too much time to learn this.

But in the future I intend to make some major improvements to the program and may even use this style. Can you point to a GTK program (preferably a simple one) that uses futures?

qarmin avatar May 20 '21 14:05 qarmin

We use this approach extensively at System76 for GTK applications.

I have a small personal project using it here: https://github.com/mmstick/fontfinder

You can read more about it here: https://mmstick.github.io/gtkrs-tutorials

mmstick avatar May 20 '21 15:05 mmstick