diff --git a/cosmic-settings/src/pages/desktop/wallpaper/config.rs b/cosmic-settings/src/pages/desktop/wallpaper/config.rs index 29fa2e6..7ab6978 100644 --- a/cosmic-settings/src/pages/desktop/wallpaper/config.rs +++ b/cosmic-settings/src/pages/desktop/wallpaper/config.rs @@ -15,6 +15,7 @@ const CUSTOM_COLORS: &str = "custom-colors"; const CUSTOM_IMAGES: &str = "custom-images"; const RECENT_FOLDERS: &str = "recent-folders"; const BACKGROUNDS_DIR: &str = "backgrounds"; +const ROTATION_FREQUENCY: &str = "rotation-frequency"; #[derive(Debug, Default)] pub struct Config { @@ -24,6 +25,7 @@ pub struct Config { custom_colors: Vec, custom_images: Vec, recent_folders: VecDeque, + pub rotation_frequency: u64, } impl Config { @@ -94,6 +96,15 @@ impl Config { } } + // Get rotation frequency from cosmic-config. + if let Ok(frequency) = context.get::(ROTATION_FREQUENCY) { + // Set rotation frequency if it exists. + config.rotation_frequency = frequency; + } else { + // Set default value if it does not exists. + config.rotation_frequency = 300; + } + config.context = Some(context); config.state = Some(state); @@ -251,6 +262,21 @@ impl Config { Ok(()) } + /// Sets a new slideshow wallpaper rotation frequency + /// + /// # Errors + /// + /// Returns an error if the on-disk configuration could not be updated. + pub fn change_rotation_frequency( + &mut self, + frequency: u64, + ) -> Result<(), cosmic_config::Error> { + self.rotation_frequency = frequency; + self.update_rotation_frequency()?; + + Ok(()) + } + fn update( &self, key: &str, @@ -274,4 +300,8 @@ impl Config { fn update_recent_folders(&self) -> Result<(), cosmic_config::Error> { self.update(RECENT_FOLDERS, &self.recent_folders) } + + fn update_rotation_frequency(&self) -> Result<(), cosmic_config::Error> { + self.update(ROTATION_FREQUENCY, &self.rotation_frequency) + } } diff --git a/cosmic-settings/src/pages/desktop/wallpaper/mod.rs b/cosmic-settings/src/pages/desktop/wallpaper/mod.rs index 548ea7e..3d1fcbb 100644 --- a/cosmic-settings/src/pages/desktop/wallpaper/mod.rs +++ b/cosmic-settings/src/pages/desktop/wallpaper/mod.rs @@ -180,9 +180,6 @@ pub struct Page { /// Model for selecting between display outputs. outputs: SingleSelectModel, - /// Current value of the slideshow rotation frequency. - rotation_frequency: u64, - /// Model for available options for rotation frequencies. rotation_options: Vec, @@ -290,6 +287,9 @@ impl page::AutoBind for Page {} impl Default for Page { fn default() -> Self { + let config = Config::new(); + let selected_rotation = Self::get_selected_rotation(config.rotation_frequency); + let mut page = Page { entity: page::Entity::null(), on_enter_handle: None, @@ -326,10 +326,9 @@ impl Default for Page { }, wallpaper_service_config: wallpaper::Config::default(), color_model: ColorPickerModel::new(fl!("hex"), fl!("rgb"), None, Some(Color::WHITE)), - config: Config::new(), + config, fit_options: vec![fl!("fill"), fl!("fit-to-screen")], outputs: SingleSelectModel::default(), - rotation_frequency: 300, rotation_options: vec![ // FIX: fluent is inserting extra unicode characters in formatting fl!("x-minutes", number = 5) @@ -352,7 +351,7 @@ impl Default for Page { .replace('\u{2069}', ""), ], selected_fit: 0, - selected_rotation: 0, + selected_rotation, selection: Context::default(), update_config: None, }; @@ -364,6 +363,18 @@ impl Default for Page { } impl Page { + fn get_selected_rotation(rotation_frequency: u64) -> usize { + match rotation_frequency { + 0..=300 => MINUTES_5, + 301..=600 => MINUTES_10, + 601..=900 => MINUTES_15, + 901..=1800 => MINUTES_30, + 1801..=3600 => HOUR_1, + 3601..=7200 => HOUR_2, + _ => HOUR_2, + } + } + fn add_recent_folder(&mut self, folder: PathBuf) { if let Err(why) = self.config.add_recent_folder(folder) { tracing::error!(?why, "cannot add recent folder to config"); @@ -627,10 +638,10 @@ impl Page { } /// Changes the slideshow wallpaper rotation frequency - pub fn change_rotation_frequency(&mut self, option: usize) { + pub fn change_rotation_frequency(&mut self, option: usize) -> Result<(), cosmic_config::Error> { self.selected_rotation = option; - self.rotation_frequency = match self.selected_rotation { + let rotation_frequency = match self.selected_rotation { MINUTES_5 => 300, MINUTES_10 => 600, MINUTES_15 => 900, @@ -639,6 +650,8 @@ impl Page { HOUR_2 => 7200, _ => 10800, }; + self.config.change_rotation_frequency(rotation_frequency)?; + Ok(()) } /// Updates configuration for wallpaper image. @@ -659,7 +672,7 @@ impl Page { let entry = Entry::new(output, wallpaper::Source::Path(path)) .scaling_mode(scaling_mode) - .rotation_frequency(self.rotation_frequency); + .rotation_frequency(self.config.rotation_frequency); if let Some(old_entry) = old_entry { entry @@ -818,7 +831,11 @@ impl Page { return Task::none(); } - Message::RotationFrequency(pos) => self.change_rotation_frequency(pos), + Message::RotationFrequency(pos) => { + if let Err(err) = self.change_rotation_frequency(pos) { + tracing::warn!("Failed to save rotation frequency: {err}"); + } + } Message::SameWallpaper(value) => { self.wallpaper_service_config.same_on_all = value; @@ -1089,7 +1106,7 @@ impl Page { _ => self.selected_rotation = MINUTES_5, } - self.rotation_frequency = entry.rotation_frequency; + self.config.rotation_frequency = entry.rotation_frequency; self.cache_display_image(); }