diff --git a/Cargo.lock b/Cargo.lock index 417830e..ca7982e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3540,6 +3540,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" name = "screenplay" version = "0.1.0" dependencies = [ + "async-std", "glib", "gtk4", ] diff --git a/screenplay/Cargo.toml b/screenplay/Cargo.toml index 04e9ab1..2d37843 100644 --- a/screenplay/Cargo.toml +++ b/screenplay/Cargo.toml @@ -7,5 +7,6 @@ license = "GPL-3.0-only" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-std = "1.13.0" glib = { version = "0.18" } gtk = { version = "0.7", package = "gtk4" } diff --git a/screenplay/src/lib.rs b/screenplay/src/lib.rs index 1f7fe1b..149608a 100644 --- a/screenplay/src/lib.rs +++ b/screenplay/src/lib.rs @@ -67,7 +67,7 @@ impl Screenplay { pub fn new(gtk_app: >k::Application, screens: Vec) -> Result { let window = gtk::ApplicationWindow::new(gtk_app); - let (sender, receiver) = gtk::glib::MainContext::channel(gtk::glib::Priority::DEFAULT); + let (sender, receiver) = async_std::channel::unbounded(); let layout = gtk::Box::builder() .orientation(gtk::Orientation::Horizontal) @@ -89,11 +89,15 @@ impl Screenplay { layout.append(&frame); listbox.connect_row_activated(move |_, row| { - match row.index() { - -1 => sender.send(Action::Deselect), - idx => sender.send(Action::SelectPage(idx as usize)), - } - .unwrap() + let sender = sender.clone(); + let row = row.clone(); + glib::spawn_future_local(async move { + match row.index() { + -1 => sender.send(Action::Deselect).await, + idx => sender.send(Action::SelectPage(idx as usize)).await, + } + .unwrap() + }); }); screens.iter().for_each(|Screen { title, .. }| { @@ -107,8 +111,14 @@ impl Screenplay { let storybook = Self { frame, screens }; { - let mut storybook = storybook.clone(); - receiver.attach(None, move |message| storybook.process_action(message)); + glib::spawn_future_local({ + let mut storybook = storybook.clone(); + async move { + while let Ok(msg) = receiver.recv().await { + storybook.process_action(msg); + } + } + }); } Ok(storybook)