diff options
Diffstat (limited to 'src/components/smart_summary.rs')
-rw-r--r-- | src/components/smart_summary.rs | 65 |
1 files changed, 44 insertions, 21 deletions
diff --git a/src/components/smart_summary.rs b/src/components/smart_summary.rs index b360d77..fbfc80b 100644 --- a/src/components/smart_summary.rs +++ b/src/components/smart_summary.rs @@ -1,5 +1,5 @@ use adw::prelude::*; -use relm4::{gtk, prelude::{AsyncComponent, AsyncComponentParts}, AsyncComponentSender}; +use relm4::{gtk, prelude::{Component, ComponentParts}, ComponentSender}; #[derive(Debug, Default)] pub(crate) struct SmartSummaryButton { @@ -13,8 +13,9 @@ pub(crate) enum Error { #[derive(Debug)] pub(crate) enum Input { - ButtonPressed, - Text(String) + #[doc(hidden)] ButtonPressed, + Text(String), + Cancel, } #[derive(Debug)] @@ -26,13 +27,13 @@ pub(crate) enum Output { Error(Error) } -#[relm4::component(pub(crate) async)] -impl AsyncComponent for SmartSummaryButton { +#[relm4::component(pub(crate))] +impl Component for SmartSummaryButton { type Input = Input; type Output = Output; type Init = (); - type CommandOutput = (); + type CommandOutput = Result<String, Error>; view! { #[root] @@ -52,44 +53,66 @@ impl AsyncComponent for SmartSummaryButton { } } - async fn init( + fn init( _init: Self::Init, root: Self::Root, - sender: AsyncComponentSender<Self> - ) -> AsyncComponentParts<Self> { + sender: ComponentSender<Self> + ) -> ComponentParts<Self> { let model = Self::default(); let widgets = view_output!(); - AsyncComponentParts { model, widgets } + ComponentParts { model, widgets } } - async fn update_with_view( + fn update( &mut self, - widgets: &mut Self::Widgets, msg: Self::Input, - sender: AsyncComponentSender<Self>, + sender: ComponentSender<Self>, _root: &Self::Root ) { match msg { + Input::Cancel => { + self.busy = false; + log::debug!("Parent component asked us to cancel."); + }, Input::ButtonPressed => if let Ok(()) = sender.output(Output::Start) { self.busy = true; log::debug!("Requesting text to summarize from parent component..."); - self.update_view(widgets, sender.clone()); }, Input::Text(text) => { log::debug!("Would generate summary for the following text:\n{}", text); - tokio::time::sleep(std::time::Duration::from_millis(450)).await; - for i in ["I'", "m ", "sorry,", " I", " am ", "unable", " to", " ", "write", " you ", "a summary.", " I", " am", " not ", "really ", "an ", "LLM."] { - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - let _ = sender.output(Output::Chunk(i.to_string())); - } + sender.command(|sender, shutdown| shutdown.register(async move { + tokio::time::sleep(std::time::Duration::from_millis(450)).await; + + for i in ["I'", "m ", "sorry,", " I", " am ", "unable", " to", " ", "write", " you ", "a summary.", " I", " am", " not ", "really ", "an ", "LLM."] { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + + if sender.send(Ok(i.to_string())).is_err() { + return + }; + } + + log::debug!("Done with the summary."); + let _ = sender.send(Ok(String::new())); + }).drop_on_shutdown()); + } + } + } - log::debug!("Done with the summary."); + fn update_cmd(&mut self, msg: Self::CommandOutput, sender: ComponentSender<Self>, _root: &Self::Root) { + match msg { + Ok(chunk) if chunk.is_empty() => { self.busy = false; let _ = sender.output(Output::Done); - self.update_view(widgets, sender.clone()); + }, + Err(err) => { + self.busy = false; + let _ = sender.output(Output::Error(err)); } + Ok(chunk) => { + let _ = sender.output(Output::Chunk(chunk)); + }, } } } |