From 39d0c6f7cfac3e061e75dd0fcd5940302547bddb Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Thu, 29 Feb 2024 04:47:47 +0100 Subject: [PATCH] fix(wallpaper): get current slideshow image from cosmic-bg state --- Cargo.lock | 1 + app/Cargo.toml | 1 + app/src/pages/desktop/wallpaper/config.rs | 30 +++++++++++++++++++++ app/src/pages/desktop/wallpaper/mod.rs | 32 ++++++++++++++--------- pages/wallpapers/src/lib.rs | 4 --- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 72921c1..6eae321 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1142,6 +1142,7 @@ dependencies = [ "async-channel", "clap", "color-eyre", + "cosmic-bg-config", "cosmic-comp-config", "cosmic-panel-config", "cosmic-randr-shell", diff --git a/app/Cargo.toml b/app/Cargo.toml index d19a6b7..3df7430 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -9,6 +9,7 @@ rust-version = "1.65.0" apply = "0.3.0" async-channel = "2.1.1" color-eyre = "0.6.2" +cosmic-bg-config = { workspace = true } cosmic-randr-shell = { workspace = true } cosmic-settings-wallpaper = { path = "../pages/wallpapers" } cosmic-settings-page = { path = "../page" } diff --git a/app/src/pages/desktop/wallpaper/config.rs b/app/src/pages/desktop/wallpaper/config.rs index 25d4938..c36148e 100644 --- a/app/src/pages/desktop/wallpaper/config.rs +++ b/app/src/pages/desktop/wallpaper/config.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-only use cosmic::cosmic_config::{self, ConfigGet, ConfigSet}; +use cosmic_bg_config::Source; use cosmic_settings_wallpaper as wallpaper; use std::collections::VecDeque; use std::path::{Path, PathBuf}; @@ -16,6 +17,7 @@ const RECENT_FOLDERS: &str = "recent-folders"; #[derive(Debug, Default)] pub struct Config { context: Option, + state: Option, pub(super) current_folder: Option, custom_colors: Vec, custom_images: Vec, @@ -34,6 +36,15 @@ impl Config { } }; + let state = match cosmic_config::Config::new_state("com.system76.CosmicBackground", VERSION) + { + Ok(state) => state, + Err(why) => { + tracing::warn!(?why, "failed to get state"); + return Self::default(); + } + }; + // Get the active background folder from cosmic-config. if let Ok(path) = context.get::>(CURRENT_FOLDER) { // Set current folder if it exists. @@ -82,6 +93,7 @@ impl Config { } config.context = Some(context); + config.state = Some(state); config } @@ -151,6 +163,24 @@ impl Config { Ok(()) } + #[must_use] + pub fn current_image(&self, output: &str) -> Option { + let mut wallpapers = self + .state + .as_ref()? + .get::>("wallpapers") + .ok()? + .into_iter(); + + let wallpaper = if output == "all" { + wallpapers.next() + } else { + wallpapers.find(|(name, _path)| name == output) + }; + + wallpaper.map(|(_name, path)| path) + } + #[must_use] pub fn custom_images(&self) -> &[PathBuf] { &self.custom_images diff --git a/app/src/pages/desktop/wallpaper/mod.rs b/app/src/pages/desktop/wallpaper/mod.rs index 63eba0a..6ec711f 100644 --- a/app/src/pages/desktop/wallpaper/mod.rs +++ b/app/src/pages/desktop/wallpaper/mod.rs @@ -40,6 +40,7 @@ use cosmic::{ widget::{color_picker::ColorPickerUpdate, ColorPickerModel}, Element, }; +use cosmic_bg_config::Source; use cosmic_settings_page::Section; use cosmic_settings_page::{self as page, section}; use cosmic_settings_wallpaper::{self as wallpaper, Entry, ScalingMode}; @@ -321,10 +322,13 @@ impl Page { Choice::Slideshow => self .config_output() - .and_then(|output| { - let path = wallpaper::current_image(output).ok()?; - let id = self.wallpaper_id_from_path(&path)?; - Some(&self.selection.display_images[id]) + .and_then(|output| match self.config.current_image(output)? { + Source::Path(path) => { + let id = self.wallpaper_id_from_path(&path)?; + Some(&self.selection.display_images[id]) + } + + Source::Color(_color) => None, }) .or(self.selection.display_images.values().next()), @@ -770,16 +774,20 @@ impl Page { self.cache_display_image(); } else { if let Some(output) = self.config_output() { - if let Ok(path) = wallpaper::current_image(output) { - if let Some(entity) = self.wallpaper_id_from_path(&path) { - if let Some(entry) = - self.config_wallpaper_entry(output.to_owned(), path) - { - self.select_wallpaper(&entry, entity, false); - self.config_apply(); - return Command::none(); + match self.config.current_image(output) { + Some(Source::Path(path)) => { + if let Some(entity) = self.wallpaper_id_from_path(&path) { + if let Some(entry) = + self.config_wallpaper_entry(output.to_owned(), path) + { + self.select_wallpaper(&entry, entity, false); + self.config_apply(); + return Command::none(); + } } } + + _ => (), } } diff --git a/pages/wallpapers/src/lib.rs b/pages/wallpapers/src/lib.rs index 7d56442..008774f 100644 --- a/pages/wallpapers/src/lib.rs +++ b/pages/wallpapers/src/lib.rs @@ -43,10 +43,6 @@ pub const DEFAULT_COLORS: &[Color] = &[ }), ]; -pub fn current_image(output: &str) -> Result { - cosmic_bg_config::context()?.current_image(output) -} - pub async fn config() -> (Config, HashMap) { let mut displays = HashMap::new();