qarmin / szyszka

Szyszka is fast and powerful file renamer
MIT License
835 stars 31 forks source link

Switch to event-driven model #13

Open mmstick opened 3 years ago

mmstick commented 3 years ago

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));
});
qarmin commented 3 years ago

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?

mmstick commented 3 years ago

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