fix(wallpaper): save rotation_frequency setting to disk
This commit is contained in:
parent
eec172cdae
commit
2924fea3ac
2 changed files with 58 additions and 11 deletions
|
|
@ -15,6 +15,7 @@ const CUSTOM_COLORS: &str = "custom-colors";
|
||||||
const CUSTOM_IMAGES: &str = "custom-images";
|
const CUSTOM_IMAGES: &str = "custom-images";
|
||||||
const RECENT_FOLDERS: &str = "recent-folders";
|
const RECENT_FOLDERS: &str = "recent-folders";
|
||||||
const BACKGROUNDS_DIR: &str = "backgrounds";
|
const BACKGROUNDS_DIR: &str = "backgrounds";
|
||||||
|
const ROTATION_FREQUENCY: &str = "rotation-frequency";
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
|
@ -24,6 +25,7 @@ pub struct Config {
|
||||||
custom_colors: Vec<wallpaper::Color>,
|
custom_colors: Vec<wallpaper::Color>,
|
||||||
custom_images: Vec<PathBuf>,
|
custom_images: Vec<PathBuf>,
|
||||||
recent_folders: VecDeque<PathBuf>,
|
recent_folders: VecDeque<PathBuf>,
|
||||||
|
pub rotation_frequency: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
|
@ -94,6 +96,15 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get rotation frequency from cosmic-config.
|
||||||
|
if let Ok(frequency) = context.get::<u64>(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.context = Some(context);
|
||||||
config.state = Some(state);
|
config.state = Some(state);
|
||||||
|
|
||||||
|
|
@ -251,6 +262,21 @@ impl Config {
|
||||||
Ok(())
|
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<V: serde::Serialize>(
|
fn update<V: serde::Serialize>(
|
||||||
&self,
|
&self,
|
||||||
key: &str,
|
key: &str,
|
||||||
|
|
@ -274,4 +300,8 @@ impl Config {
|
||||||
fn update_recent_folders(&self) -> Result<(), cosmic_config::Error> {
|
fn update_recent_folders(&self) -> Result<(), cosmic_config::Error> {
|
||||||
self.update(RECENT_FOLDERS, &self.recent_folders)
|
self.update(RECENT_FOLDERS, &self.recent_folders)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_rotation_frequency(&self) -> Result<(), cosmic_config::Error> {
|
||||||
|
self.update(ROTATION_FREQUENCY, &self.rotation_frequency)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -180,9 +180,6 @@ pub struct Page {
|
||||||
/// Model for selecting between display outputs.
|
/// Model for selecting between display outputs.
|
||||||
outputs: SingleSelectModel,
|
outputs: SingleSelectModel,
|
||||||
|
|
||||||
/// Current value of the slideshow rotation frequency.
|
|
||||||
rotation_frequency: u64,
|
|
||||||
|
|
||||||
/// Model for available options for rotation frequencies.
|
/// Model for available options for rotation frequencies.
|
||||||
rotation_options: Vec<String>,
|
rotation_options: Vec<String>,
|
||||||
|
|
||||||
|
|
@ -290,6 +287,9 @@ impl page::AutoBind<crate::pages::Message> for Page {}
|
||||||
|
|
||||||
impl Default for Page {
|
impl Default for Page {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
let config = Config::new();
|
||||||
|
let selected_rotation = Self::get_selected_rotation(config.rotation_frequency);
|
||||||
|
|
||||||
let mut page = Page {
|
let mut page = Page {
|
||||||
entity: page::Entity::null(),
|
entity: page::Entity::null(),
|
||||||
on_enter_handle: None,
|
on_enter_handle: None,
|
||||||
|
|
@ -326,10 +326,9 @@ impl Default for Page {
|
||||||
},
|
},
|
||||||
wallpaper_service_config: wallpaper::Config::default(),
|
wallpaper_service_config: wallpaper::Config::default(),
|
||||||
color_model: ColorPickerModel::new(fl!("hex"), fl!("rgb"), None, Some(Color::WHITE)),
|
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")],
|
fit_options: vec![fl!("fill"), fl!("fit-to-screen")],
|
||||||
outputs: SingleSelectModel::default(),
|
outputs: SingleSelectModel::default(),
|
||||||
rotation_frequency: 300,
|
|
||||||
rotation_options: vec![
|
rotation_options: vec![
|
||||||
// FIX: fluent is inserting extra unicode characters in formatting
|
// FIX: fluent is inserting extra unicode characters in formatting
|
||||||
fl!("x-minutes", number = 5)
|
fl!("x-minutes", number = 5)
|
||||||
|
|
@ -352,7 +351,7 @@ impl Default for Page {
|
||||||
.replace('\u{2069}', ""),
|
.replace('\u{2069}', ""),
|
||||||
],
|
],
|
||||||
selected_fit: 0,
|
selected_fit: 0,
|
||||||
selected_rotation: 0,
|
selected_rotation,
|
||||||
selection: Context::default(),
|
selection: Context::default(),
|
||||||
update_config: None,
|
update_config: None,
|
||||||
};
|
};
|
||||||
|
|
@ -364,6 +363,18 @@ impl Default for Page {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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) {
|
fn add_recent_folder(&mut self, folder: PathBuf) {
|
||||||
if let Err(why) = self.config.add_recent_folder(folder) {
|
if let Err(why) = self.config.add_recent_folder(folder) {
|
||||||
tracing::error!(?why, "cannot add recent folder to config");
|
tracing::error!(?why, "cannot add recent folder to config");
|
||||||
|
|
@ -627,10 +638,10 @@ impl Page {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Changes the slideshow wallpaper rotation frequency
|
/// 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.selected_rotation = option;
|
||||||
|
|
||||||
self.rotation_frequency = match self.selected_rotation {
|
let rotation_frequency = match self.selected_rotation {
|
||||||
MINUTES_5 => 300,
|
MINUTES_5 => 300,
|
||||||
MINUTES_10 => 600,
|
MINUTES_10 => 600,
|
||||||
MINUTES_15 => 900,
|
MINUTES_15 => 900,
|
||||||
|
|
@ -639,6 +650,8 @@ impl Page {
|
||||||
HOUR_2 => 7200,
|
HOUR_2 => 7200,
|
||||||
_ => 10800,
|
_ => 10800,
|
||||||
};
|
};
|
||||||
|
self.config.change_rotation_frequency(rotation_frequency)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates configuration for wallpaper image.
|
/// Updates configuration for wallpaper image.
|
||||||
|
|
@ -659,7 +672,7 @@ impl Page {
|
||||||
|
|
||||||
let entry = Entry::new(output, wallpaper::Source::Path(path))
|
let entry = Entry::new(output, wallpaper::Source::Path(path))
|
||||||
.scaling_mode(scaling_mode)
|
.scaling_mode(scaling_mode)
|
||||||
.rotation_frequency(self.rotation_frequency);
|
.rotation_frequency(self.config.rotation_frequency);
|
||||||
|
|
||||||
if let Some(old_entry) = old_entry {
|
if let Some(old_entry) = old_entry {
|
||||||
entry
|
entry
|
||||||
|
|
@ -818,7 +831,11 @@ impl Page {
|
||||||
return Task::none();
|
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) => {
|
Message::SameWallpaper(value) => {
|
||||||
self.wallpaper_service_config.same_on_all = value;
|
self.wallpaper_service_config.same_on_all = value;
|
||||||
|
|
@ -1089,7 +1106,7 @@ impl Page {
|
||||||
_ => self.selected_rotation = MINUTES_5,
|
_ => self.selected_rotation = MINUTES_5,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.rotation_frequency = entry.rotation_frequency;
|
self.config.rotation_frequency = entry.rotation_frequency;
|
||||||
|
|
||||||
self.cache_display_image();
|
self.cache_display_image();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue