Skip to main content

cosmic_applet_avestruz/handlers/
settings.rs

1// SPDX-License-Identifier: MIT
2
3//! Settings message handlers for Avestruz.
4//!
5//! A setting changed here is written to COSMIC's config store and applied
6//! at once. One changed elsewhere (another instance, a hand in the config
7//! files) arrives through the config watch, and is applied the same.
8
9use crate::config::{Config, LogLevel, NOTE_TEXT_SIZE_MAX, NOTE_TEXT_SIZE_MIN};
10use cosmic::prelude::*;
11
12use crate::logging;
13use crate::messages::Message;
14use crate::state::{AppModel, Page};
15use crate::views::components::RowHeights;
16
17impl AppModel {
18    /// How much to log was chosen in the settings: keep it, and log that
19    /// much from now on, without a restart.
20    pub fn handle_set_log_level(&mut self, level: LogLevel) {
21        tracing::info!("Setting console log level to {}", level.as_filter_str());
22        self.config.log_level = level;
23        self.save_config("log level");
24        logging::set_console_level(level);
25    }
26
27    /// How large notes are set was chosen in the settings: keep it. The
28    /// rows are other heights from now on.
29    pub fn handle_set_note_text_size(&mut self, size: u16) {
30        let size = size.clamp(NOTE_TEXT_SIZE_MIN, NOTE_TEXT_SIZE_MAX);
31        if size == self.config.note_text_size {
32            return;
33        }
34        self.config.note_text_size = size;
35        self.save_config("note text size");
36        self.forget_row_heights();
37    }
38
39    /// How long the event store keeps other people's notes was chosen in
40    /// the settings: keep it, and have the store let go now of what is
41    /// older than that.
42    pub fn handle_set_keep_days(&mut self, days: u32) -> Task<cosmic::Action<Message>> {
43        if days == self.config.keep_days {
44            return Task::none();
45        }
46        self.config.keep_days = days;
47        self.save_config("days kept");
48        self.swept_at = None;
49        self.sweep_store()
50    }
51
52    /// Forget how tall the rows of every list measured: they are about to
53    /// be set in another size, and measure again as they come into view.
54    fn forget_row_heights(&mut self) {
55        self.following.heights = RowHeights::default();
56        for page in &mut self.pages {
57            match page {
58                Page::Thread(state) => state.heights = RowHeights::default(),
59                Page::Profile(state) => state.notes.heights = RowHeights::default(),
60                Page::Bookmarks(page) => page.notes.heights = RowHeights::default(),
61                Page::Search(page) => page.heights = RowHeights::default(),
62                Page::Notifications(page) => page.heights = RowHeights::default(),
63            }
64        }
65    }
66
67    /// The config changed on disk. A new log level is logged at from now
68    /// on, unless `RUST_LOG` says how much to log; notes are set in a new
69    /// size; the lists show the notes they are now to show; and the event
70    /// store lets go of what is older than it is now to keep.
71    pub fn handle_update_config(&mut self, config: Config) -> Task<cosmic::Action<Message>> {
72        if config.log_level != self.config.log_level && !logging::level_set_by_environment() {
73            logging::set_console_level(config.log_level);
74        }
75        let resized = config.note_text_size() != self.config.note_text_size();
76        let read_another_way = config.feed_mode != self.config.feed_mode;
77        let kept_another_while = config.keep_days != self.config.keep_days;
78        self.config = config;
79        self.suggest_reactions();
80        if resized {
81            self.forget_row_heights();
82        }
83        let shown = if read_another_way { self.show_feed_mode() } else { Task::none() };
84        let swept = if kept_another_while {
85            self.swept_at = None;
86            self.sweep_store()
87        } else {
88            Task::none()
89        };
90        Task::batch([shown, swept])
91    }
92}