summary refs log tree commit diff
path: root/src/components/preferences.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/preferences.rs')
-rw-r--r--src/components/preferences.rs137
1 files changed, 94 insertions, 43 deletions
diff --git a/src/components/preferences.rs b/src/components/preferences.rs
index 9bbc313..fbf406d 100644
--- a/src/components/preferences.rs
+++ b/src/components/preferences.rs
@@ -1,4 +1,3 @@
-use gettextrs::*;
 use gio::prelude::*;
 use adw::prelude::*;
 use relm4::prelude::*;
@@ -7,38 +6,100 @@ pub struct Preferences {
     settings: gio::Settings,
 }
 
-#[relm4::component(pub)]
+#[cfg(feature = "smart-summary")]
+#[allow(dead_code)]
+struct LanguageModelPreferencesWidgets {
+    page: adw::PreferencesPage,
+
+    general_group: adw::PreferencesGroup,
+    llm_endpoint: adw::EntryRow,
+
+    smart_summary_group: adw::PreferencesGroup,
+    smart_summary_model: adw::EntryRow,
+    smart_summary_system_prompt: adw::EntryRow,
+    smart_summary_prompt_prefix: adw::EntryRow,
+    smart_summary_prompt_suffix: adw::EntryRow,
+}
+
+#[cfg(feature = "smart-summary")]
+impl LanguageModelPreferencesWidgets {
+    fn new(settings: &gio::Settings) -> Self {
+        use gettextrs::*;
+
+        let page = adw::PreferencesPage::builder()
+            .title(gettext("Language Models"))
+            .description(gettext("Settings for the language model integrations."))
+            .icon_name("magic-wand")
+            .build();
+
+        let general_group = adw::PreferencesGroup::builder()
+            .title(gettext("General"))
+            .build();
+        let llm_endpoint = adw::EntryRow::new();
+        general_group.add(&llm_endpoint);
+        page.add(&general_group);
+
+        let smart_summary_group = adw::PreferencesGroup::builder()
+            .title(gettext("Smart Summary"))
+            .build();
+        let smart_summary_model = adw::EntryRow::new();
+        let smart_summary_system_prompt = adw::EntryRow::new();
+        let smart_summary_prompt_prefix = adw::EntryRow::new();
+        let smart_summary_prompt_suffix = adw::EntryRow::new();
+        smart_summary_group.add(&smart_summary_model);
+        smart_summary_group.add(&smart_summary_system_prompt);
+        smart_summary_group.add(&smart_summary_prompt_prefix);
+        smart_summary_group.add(&smart_summary_prompt_suffix);
+        page.add(&smart_summary_group);
+
+        let widgets = Self {
+            page,
+
+            general_group,
+            llm_endpoint,
+
+            smart_summary_group,
+            smart_summary_model,
+            smart_summary_system_prompt,
+            smart_summary_prompt_prefix,
+            smart_summary_prompt_suffix
+        };
+
+        let schema = settings.settings_schema().unwrap();
+
+        for (row, key) in [
+            (&widgets.llm_endpoint, "llm-endpoint"),
+            (&widgets.smart_summary_model, "smart-summary-model"),
+            (&widgets.smart_summary_system_prompt, "smart-summary-system-prompt"),
+            (&widgets.smart_summary_prompt_prefix, "smart-summary-prompt-prefix"),
+            (&widgets.smart_summary_prompt_suffix, "smart-summary-prompt-suffix"),
+        ] {
+            settings.bind(key, row, "text")
+                .get()
+                .set()
+                .build();
+            row.set_title(&gettext(schema.key(key).summary().unwrap()));
+        }
+
+        widgets
+    }
+}
+
+pub struct PreferencesWidgets {
+    #[cfg(feature = "smart-summary")]
+    llm: LanguageModelPreferencesWidgets
+}
+
 impl Component for Preferences {
     type CommandOutput = ();
     type Input = Option<gtk::Widget>;
     type Output = ();
     type Init = ();
+    type Root = adw::PreferencesDialog;
+    type Widgets = PreferencesWidgets;
 
-    view! {
-        #[root]
-        adw::PreferencesDialog {
-            add = &adw::PreferencesPage {
-                set_title: &gettext("Language Models"),
-                set_description: &gettext("Settings for the language model integrations."),
-                set_icon_name: Some("magic-wand"),
-
-                adw::PreferencesGroup {
-                    set_title: &gettext("General"),
-
-                    #[name = "llm_endpoint"]
-                    adw::EntryRow {},
-                },
-
-                adw::PreferencesGroup {
-                    set_title: &gettext("Smart Summary"),
-
-                    #[name = "smart_summary_model"] adw::EntryRow {},
-                    #[name = "smart_summary_system_prompt"] adw::EntryRow {},
-                    #[name = "smart_summary_prompt_prefix"] adw::EntryRow {},
-                    #[name = "smart_summary_prompt_suffix"] adw::EntryRow {},
-                }
-            }
-        }
+    fn init_root() -> Self::Root {
+        adw::PreferencesDialog::new()
     }
 
     fn init(
@@ -51,23 +112,13 @@ impl Component for Preferences {
         };
 
         model.settings.delay();
-        let schema = model.settings.settings_schema().unwrap();
 
-        let widgets = view_output!();
-
-        for (row, key) in [
-            (&widgets.llm_endpoint, "llm-endpoint"),
-            (&widgets.smart_summary_model, "smart-summary-model"),
-            (&widgets.smart_summary_system_prompt, "smart-summary-system-prompt"),
-            (&widgets.smart_summary_prompt_prefix, "smart-summary-prompt-prefix"),
-            (&widgets.smart_summary_prompt_suffix, "smart-summary-prompt-suffix"),
-        ] {
-            model.settings.bind(key, row, "text")
-                .get()
-                .set()
-                .build();
-            row.set_title(&gettext(schema.key(key).summary().unwrap()));
-        }
+        let widgets = PreferencesWidgets {
+            #[cfg(feature = "smart-summary")]
+            llm: LanguageModelPreferencesWidgets::new(&model.settings),
+        };
+        #[cfg(feature = "smart-summary")]
+        root.add(&widgets.llm.page);
 
         root.connect_closed(glib::clone!(
             #[strong(rename_to = settings)]