From 406509dc549cb58d788a3dbd5572f28008c84546 Mon Sep 17 00:00:00 2001 From: Vika Date: Wed, 4 Sep 2024 22:15:32 +0300 Subject: Make LLM enhancements optional --- src/components/post_editor.rs | 45 ++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'src/components/post_editor.rs') diff --git a/src/components/post_editor.rs b/src/components/post_editor.rs index f2268ad..c42b06a 100644 --- a/src/components/post_editor.rs +++ b/src/components/post_editor.rs @@ -1,11 +1,12 @@ use gettextrs::*; -use crate::components; use crate::components::tag_pill::*; use adw::prelude::*; use glib::translate::IntoGlib; use gtk::GridLayoutChild; -use relm4::{gtk, prelude::{ComponentController, Controller, DynamicIndex}, factory::FactoryVecDeque, Component, ComponentParts, ComponentSender, RelmWidgetExt}; +use relm4::{factory::FactoryVecDeque, gtk, prelude::{Controller, DynamicIndex}, Component, ComponentParts, ComponentSender, RelmWidgetExt}; +#[cfg(feature = "smart-summary")] +use relm4::prelude::ComponentController; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, glib::Enum)] #[enum_type(name = "MicropubVisibility")] @@ -87,7 +88,8 @@ pub(crate) struct PostEditor { #[do_not_track] wide_layout: gtk::GridLayout, - #[do_not_track] smart_summary: Controller, + #[cfg(feature = "smart-summary")] + #[do_not_track] smart_summary: Controller, _err: std::marker::PhantomData } @@ -104,7 +106,8 @@ impl PostEditor { #[allow(private_interfaces)] // intentional #[allow(clippy::manual_non_exhaustive)] // false positive pub enum Input { - #[doc(hidden)] SmartSummary(components::smart_summary::Output), + #[cfg(feature = "smart-summary")] + #[doc(hidden)] SmartSummary(crate::components::smart_summary::Output), #[doc(hidden)] VisibilitySelected(Visibility), #[doc(hidden)] AddTagFromBuffer, #[doc(hidden)] RemoveTag(DynamicIndex), @@ -115,7 +118,10 @@ pub enum Input { #[relm4::component(pub)] impl Component for PostEditor { - type Init = (::Init, Option); + #[cfg(feature = "smart-summary")] + type Init = (soup::Session, Option); + #[cfg(not(feature = "smart-summary"))] + type Init = Option; type Output = Option; type Input = Input; type CommandOutput = (); @@ -169,8 +175,6 @@ impl Component for Post #[track = "model.busy_changed()"] set_sensitive: !model.busy(), }, - - model.smart_summary.widget(), }, #[name = "tag_label"] @@ -331,10 +335,13 @@ impl Component for Post } fn init( - (http, init): Self::Init, + init: Self::Init, root: Self::Root, sender: ComponentSender ) -> ComponentParts { + #[cfg(feature = "smart-summary")] + let (http, init) = init; + let mut model = Self { smart_summary_busy_guard: None, sending: false, @@ -359,7 +366,8 @@ impl Component for Post wide_layout: gtk::GridLayout::new(), - smart_summary: components::SmartSummaryButton::builder() + #[cfg(feature = "smart-summary")] + smart_summary: crate::components::SmartSummaryButton::builder() .launch(http) .forward(sender.input_sender(), Input::SmartSummary), @@ -371,6 +379,9 @@ impl Component for Post let widgets = view_output!(); + #[cfg(feature = "smart-summary")] + widgets.summary_field.append(model.smart_summary.widget()); + widgets.visibility_selector.set_expression(Some( gtk::ClosureExpression::new::( [] as [gtk::Expression; 0], @@ -459,11 +470,12 @@ impl Component for Post fn update_with_view(&mut self, widgets: &mut Self::Widgets, msg: Self::Input, sender: ComponentSender, root: &Self::Root) { self.reset(); match msg { - Input::SmartSummary(components::SmartSummaryOutput::Start) => { + #[cfg(feature = "smart-summary")] + Input::SmartSummary(crate::components::SmartSummaryOutput::Start) => { widgets.content_textarea.set_sensitive(false); if self.content_buffer.char_count() == 0 { let _ = self.smart_summary.sender().send( - components::SmartSummaryInput::Cancel + crate::components::SmartSummaryInput::Cancel ); } else { let text = self.content_buffer.text( @@ -476,20 +488,23 @@ impl Component for Post Some(relm4::main_adw_application().mark_busy()) ); if self.smart_summary.sender().send( - components::SmartSummaryInput::Text(text.into()) + crate::components::SmartSummaryInput::Text(text.into()) ).is_ok() { self.summary_buffer.set_text(""); } } widgets.content_textarea.set_sensitive(true); }, - Input::SmartSummary(components::SmartSummaryOutput::Chunk(text)) => { + #[cfg(feature = "smart-summary")] + Input::SmartSummary(crate::components::SmartSummaryOutput::Chunk(text)) => { self.summary_buffer.insert_text(self.summary_buffer.length(), text); }, - Input::SmartSummary(components::SmartSummaryOutput::Done) => { + #[cfg(feature = "smart-summary")] + Input::SmartSummary(crate::components::SmartSummaryOutput::Done) => { self.set_smart_summary_busy_guard(None); }, - Input::SmartSummary(components::SmartSummaryOutput::Error(err)) => { + #[cfg(feature = "smart-summary")] + Input::SmartSummary(crate::components::SmartSummaryOutput::Error(err)) => { self.set_smart_summary_busy_guard(None); let toast = adw::Toast::new(&gettext!("Smart Summary error: {}", err)); -- cgit 1.4.1