Skip to main content

cosmic_applet_avestruz/views/
settings.rs

1// SPDX-License-Identifier: MIT
2
3//! The settings view for Avestruz: who is signed in, signing out, how large
4//! notes are set, how long they are kept, and how much is logged.
5
6use cosmic::Element;
7use cosmic::iced::widget::text::Wrapping;
8use cosmic::iced::{Alignment, Length};
9use cosmic::widget::{self, button, container, text};
10
11use crate::config::{KEEP_DAYS_CHOICES, LogLevel, NOTE_TEXT_SIZE_MAX, NOTE_TEXT_SIZE_MIN};
12use crate::fl;
13use crate::messages::Message;
14use crate::nostr::account::Account;
15use crate::nostr::models::{display_name, short_npub};
16use crate::state::AppModel;
17use crate::views::components::avatar;
18
19/// How wide the account's picture is in the settings: as in a note's row.
20const ACCOUNT_AVATAR_SIZE: f32 = 36.0;
21
22impl AppModel {
23    /// The line above the settings: the way back, and what this is.
24    fn view_settings_header(&self) -> Element<'_, Message> {
25        let back = button::icon(widget::icon::from_name("go-previous-symbolic"))
26            .tooltip(fl!("settings-back"))
27            .on_press(Message::CloseSettings);
28        widget::Row::new()
29            .push(back)
30            .push(text(fl!("settings-title")).size(16).font(cosmic::font::bold()))
31            .spacing(8)
32            .align_y(Alignment::Center)
33            .padding([4, 12])
34            .into()
35    }
36
37    /// Who is signed in, how, and the way out.
38    fn view_settings_account(&self, account: &Account) -> Element<'_, Message> {
39        let name = display_name(&self.profiles, &account.public_key);
40        let picture = self.profiles.get(&account.public_key).and_then(|profile| profile.picture.as_deref());
41        let picture = avatar(&self.images, picture, &account.public_key, &name, ACCOUNT_AVATAR_SIZE);
42
43        let key = if account.can_sign {
44            short_npub(&account.public_key)
45        } else {
46            format!("{} · {}", short_npub(&account.public_key), fl!("read-only"))
47        };
48        // The name is cut rather than wrapped when it doesn't fit.
49        let who = widget::Column::new()
50            .push(container(text(name).size(13).font(cosmic::font::bold()).wrapping(Wrapping::None)).clip(true))
51            .push(text(key).size(11).wrapping(Wrapping::None))
52            .spacing(2)
53            .width(Length::Fill);
54
55        widget::Row::new()
56            .push(picture)
57            .push(who)
58            .push(button::destructive(fl!("sign-out")).on_press(Message::SignOut))
59            .spacing(8)
60            .align_y(Alignment::Center)
61            .into()
62    }
63
64    /// How large notes are set: the size, a step at a time, and a line set
65    /// in it, since the notes themselves are behind this view.
66    fn view_settings_text_size(&self) -> Element<'_, Message> {
67        let size = self.config.note_text_size();
68        let stepper = widget::spin_button(
69            size.to_string(),
70            fl!("settings-text-size"),
71            size,
72            1,
73            NOTE_TEXT_SIZE_MIN,
74            NOTE_TEXT_SIZE_MAX,
75            Message::SetNoteTextSize,
76        );
77        let chooser = widget::Row::new()
78            .push(text(fl!("settings-text-size-description")).size(11).wrapping(Wrapping::WordOrGlyph).width(Length::Fill))
79            .push(stepper)
80            .spacing(8)
81            .align_y(Alignment::Center);
82        widget::Column::new()
83            .push(text(fl!("settings-text-size")).size(14))
84            .push(chooser)
85            .push(text(fl!("settings-text-size-sample")).size(size).wrapping(Wrapping::WordOrGlyph))
86            .spacing(8)
87            .into()
88    }
89
90    /// For how long the event store keeps other people's notes. A number
91    /// of days the config file says that is none of the choices shows as
92    /// none chosen.
93    fn view_settings_keeping(&self) -> Element<'_, Message> {
94        let choices: Vec<String> = KEEP_DAYS_CHOICES
95            .iter()
96            .map(|days| match days {
97                0 => fl!("settings-keep-forever"),
98                days => fl!("settings-keep-days", days = days),
99            })
100            .collect();
101        let chosen = KEEP_DAYS_CHOICES.iter().position(|days| *days == self.config.keep_days);
102        widget::Column::new()
103            .push(text(fl!("settings-keep")).size(14))
104            .push(
105                widget::dropdown(choices, chosen, |index| {
106                    Message::SetKeepDays(KEEP_DAYS_CHOICES.get(index).copied().unwrap_or_default())
107                })
108                .width(Length::Fill),
109            )
110            .push(text(fl!("settings-keep-description")).size(11).wrapping(Wrapping::WordOrGlyph))
111            .spacing(8)
112            .into()
113    }
114
115    /// How much is logged: an applet has no command line to say so on.
116    fn view_settings_logging(&self) -> Element<'_, Message> {
117        let chosen = LogLevel::ALL.iter().position(|level| *level == self.config.log_level);
118        widget::Column::new()
119            .push(text(fl!("settings-logging")).size(14))
120            .push(
121                widget::dropdown(&LogLevel::ALL, chosen, |index| {
122                    Message::SetLogLevel(LogLevel::ALL.get(index).copied().unwrap_or_default())
123                })
124                .width(Length::Fill),
125            )
126            .push(text(fl!("settings-logging-description")).size(11).wrapping(Wrapping::WordOrGlyph))
127            .spacing(8)
128            .into()
129    }
130
131    /// Render the settings view.
132    pub fn view_settings(&self) -> Element<'_, Message> {
133        let mut account = widget::Column::new().push(text(fl!("settings-account")).size(14)).spacing(8);
134        // There is nobody to sign out until the account is known.
135        if let Some(signed_in) = &self.account {
136            account = account.push(self.view_settings_account(signed_in));
137        }
138        let body = widget::Column::new()
139            .push(account)
140            .push(self.view_settings_text_size())
141            .push(self.view_settings_keeping())
142            .push(self.view_settings_logging())
143            .spacing(16);
144
145        // In a scroller: on a small screen there is more of it than room.
146        widget::Column::new()
147            .push(self.view_settings_header())
148            .push(widget::divider::horizontal::default())
149            .push(widget::scrollable(container(body).padding([8, 12]).width(Length::Fill)))
150            .into()
151    }
152}