From 2bd15a7d832bb49c9a5a43eb9475dde1cb4696b3 Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 22 Aug 2024 18:30:14 +0300 Subject: SmartSummaryButton: un-asyncify and move summarization to a command What this command should do is construct a summarization request and return a future which would return chunks from the LLM. Perhaps this component will be asyncified in the future. --- src/lib.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 244c09f..8e73a33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use adw::prelude::*; use gtk::GridLayoutChild; -use relm4::{gtk, prelude::{AsyncComponent, AsyncComponentController, AsyncComponentParts, AsyncController}, AsyncComponentSender, RelmWidgetExt}; +use relm4::{gtk, prelude::{AsyncComponent, AsyncComponentController, AsyncComponentParts, AsyncController, ComponentController, Controller}, AsyncComponentSender, Component, RelmWidgetExt}; pub mod components { pub(crate) mod smart_summary; @@ -35,7 +35,7 @@ pub struct PostComposerModel { #[do_not_track] micropub: Arc, - #[do_not_track] smart_summary: AsyncController, + #[do_not_track] smart_summary: Controller, } impl PostComposerModel { @@ -342,18 +342,28 @@ impl AsyncComponent for PostComposerModel { match message { PostComposerInput::SmartSummary(components::SmartSummaryOutput::Start) => { - self.set_smart_summary_busy_guard( - Some(relm4::main_adw_application().mark_busy()) - ); - if self.smart_summary.sender().send(components::SmartSummaryInput::Text( - self.content_buffer.text( + widgets.content_textarea.set_sensitive(false); + if self.content_buffer.char_count() == 0 { + let _ = self.smart_summary.sender().send( + components::SmartSummaryInput::Cancel + ); + } else { + let text = self.content_buffer.text( &self.content_buffer.start_iter(), &self.content_buffer.end_iter(), false - ).into() - )).is_ok() { - self.summary_buffer.set_text(""); + ); + + self.set_smart_summary_busy_guard( + Some(relm4::main_adw_application().mark_busy()) + ); + if self.smart_summary.sender().send( + components::SmartSummaryInput::Text(text.into()) + ).is_ok() { + self.summary_buffer.set_text(""); + } } + widgets.content_textarea.set_sensitive(true); }, PostComposerInput::SmartSummary(components::SmartSummaryOutput::Chunk(text)) => { self.summary_buffer.insert_text(self.summary_buffer.length(), text); -- cgit 1.4.1