summary refs log tree commit diff
path: root/src/components/preferences.rs
blob: 9bbc313a33f9b19a1b60f3433bdc5712296f6ab6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use gettextrs::*;
use gio::prelude::*;
use adw::prelude::*;
use relm4::prelude::*;

pub struct Preferences {
    settings: gio::Settings,
}

#[relm4::component(pub)]
impl Component for Preferences {
    type CommandOutput = ();
    type Input = Option<gtk::Widget>;
    type Output = ();
    type Init = ();

    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(
        _: Self::Init,
        root: Self::Root,
        _sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        let model = Self {
            settings: gio::Settings::new(crate::APPLICATION_ID),
        };

        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()));
        }

        root.connect_closed(glib::clone!(
            #[strong(rename_to = settings)]
            model.settings,
            move |_| {
                settings.apply()
            }
        ));

        ComponentParts { model, widgets }
    }

    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>, root: &Self::Root) {
        root.present(msg.as_ref());
    }

    fn shutdown(&mut self, _: &mut Self::Widgets, _: relm4::Sender<()>) {
        self.settings.apply()
    }
}