From cfe8ae5c09ee88e7d19e37d16aa20e1e787fb296 Mon Sep 17 00:00:00 2001 From: Jeremy Yao Date: Sat, 17 Oct 2026 22:20:54 -0400 Subject: [PATCH] Animation Effects: plainer Linux/BSD reader - The worker thread shares its latest answer with the UI as a plain Option behind a mutex, instead of a byte that needed encode and decode helpers. - Finding the user's config directory is its own small function. - In freedesktop.rs, the two dense one-liners in without_portal are split into named steps, and the parsers are pub(super) (ini_value private): only the animations module uses them. No change in behaviour. Co-Authored-By: Claude Opus 5.5 --- apps/photocraft/src/animations.rs | 40 ++++++++----------- apps/photocraft/src/animations/freedesktop.rs | 18 +++++---- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/apps/photocraft/src/animations.rs b/apps/photocraft/src/animations.rs index bae69370b0..b6067fd25d 100644 --- a/apps/photocraft/src/animations.rs +++ b/apps/photocraft/src/animations.rs @@ -62,13 +62,12 @@ mod platform { /// `gsettings`, so a worker thread does it and the UI reads its latest answer. #[cfg(all(unix, not(target_os = "macos")))] mod platform { - use std::sync::atomic::{AtomicU8, Ordering}; - use std::sync::{Arc, OnceLock, mpsc}; + use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError, mpsc}; use super::freedesktop; pub fn service() -> Option { - let latest = Arc::new(AtomicU8::new(encode(None))); + let latest = Arc::new(Mutex::new(None)); let wake: Arc> = Arc::new(OnceLock::new()); let (refresh, refreshes) = mpsc::channel::<()>(); let publisher = Publisher { latest: Arc::clone(&latest), wake: Arc::clone(&wake) }; @@ -83,7 +82,7 @@ mod platform { if focused && !was_focused { let _ = refresh.send(()); } - decode(latest.load(Ordering::Relaxed)) + *lock(&latest) })) } @@ -102,14 +101,14 @@ mod platform { /// The worker's side of the latest answer. A change wakes the UI. struct Publisher { - latest: Arc, + latest: Arc>>, wake: Arc>, } impl Publisher { fn publish(&self, animations: Option) { - let code = encode(animations); - if self.latest.swap(code, Ordering::Relaxed) != code + let previous = std::mem::replace(&mut *lock(&self.latest), animations); + if previous != animations && let Some(ctx) = self.wake.get() { ctx.request_repaint(); @@ -117,20 +116,9 @@ mod platform { } } - fn encode(animations: Option) -> u8 { - match animations { - None => 0, - Some(true) => 1, - Some(false) => 2, - } - } - - fn decode(code: u8) -> Option { - match code { - 1 => Some(true), - 2 => Some(false), - _ => None, - } + /// The latest answer; a panic elsewhere while holding it doesn't lose it. + fn lock(latest: &Mutex>) -> MutexGuard<'_, Option> { + latest.lock().unwrap_or_else(PoisonError::into_inner) } #[cfg(target_os = "linux")] @@ -230,12 +218,16 @@ mod platform { freedesktop::without_portal(&desktop, &config_file, &gsettings) } + /// `$XDG_CONFIG_HOME`, or `~/.config` when it isn't set. + fn config_home() -> Option { + let var = |key: &str| std::env::var_os(key).filter(|value| !value.is_empty()).map(PathBuf::from); + var("XDG_CONFIG_HOME").filter(|path| path.is_absolute()).or_else(|| Some(var("HOME")?.join(".config"))) + } + fn config_file(name: &str) -> Option { use std::io::Read as _; - let dir = |key: &str| std::env::var_os(key).filter(|v| !v.is_empty()).map(PathBuf::from); - let config_home = dir("XDG_CONFIG_HOME").filter(|path| path.is_absolute()).or_else(|| dir("HOME").map(|home| home.join(".config")))?; let mut text = String::new(); - std::fs::File::open(config_home.join(name)).ok()?.take(MAX_CONFIG_BYTES).read_to_string(&mut text).ok()?; + std::fs::File::open(config_home()?.join(name)).ok()?.take(MAX_CONFIG_BYTES).read_to_string(&mut text).ok()?; Some(text) } diff --git a/apps/photocraft/src/animations/freedesktop.rs b/apps/photocraft/src/animations/freedesktop.rs index 437ab5c42a..d5b4fbe626 100644 --- a/apps/photocraft/src/animations/freedesktop.rs +++ b/apps/photocraft/src/animations/freedesktop.rs @@ -4,7 +4,7 @@ //! animations are on, or `None` when the value doesn't parse (the next source is asked then). /// GSettings' and the portal's `true`/`false`, or GTK's `1`/`0`. -pub fn from_enabled(value: &str) -> Option { +pub(super) fn from_enabled(value: &str) -> Option { match value.trim().to_ascii_lowercase().as_str() { "true" | "1" => Some(true), "false" | "0" => Some(false), @@ -13,23 +13,26 @@ pub fn from_enabled(value: &str) -> Option { } /// KDE's animation speed factor: 0 turns animations off. -pub fn from_duration_factor(value: &str) -> Option { +pub(super) fn from_duration_factor(value: &str) -> Option { value.trim().parse::().ok().filter(|factor| factor.is_finite()).map(|factor| factor > 0.0) } /// The settings portal's `reduced-motion`: 1 asks for less motion; 0 and any value added later /// mean no preference. #[cfg(any(test, target_os = "linux"))] -pub fn from_reduced_motion(value: &str) -> Option { +pub(super) fn from_reduced_motion(value: &str) -> Option { value.trim().parse::().ok().map(|code| code != 1) } /// The desktop's setting when there is no settings portal to ask. `desktop` is /// `XDG_CURRENT_DESKTOP`, `config_file` reads a file in the user's config directory and /// `gsettings` runs `gsettings get `. -pub fn without_portal(desktop: &str, config_file: &dyn Fn(&str) -> Option, gsettings: &dyn Fn(&str, &str) -> Option) -> Option { +pub(super) fn without_portal(desktop: &str, config_file: &dyn Fn(&str) -> Option, gsettings: &dyn Fn(&str, &str) -> Option) -> Option { let running = |name: &str| desktop.split(':').any(|part| part.eq_ignore_ascii_case(name)); - let in_file = |file: &str, section: &str, key: &str, parse: fn(&str) -> Option| parse(ini_value(&config_file(file)?, section, key)?); + let in_file = |file: &str, section: &str, key: &str, parse: fn(&str) -> Option| { + let text = config_file(file)?; + parse(ini_value(&text, section, key)?) + }; // KDE Plasma keeps it in its own file. A kdeglobals left over from another desktop doesn't count. if running("KDE") @@ -45,11 +48,12 @@ pub fn without_portal(desktop: &str, config_file: &dyn Fn(&str) -> Option(text: &'a str, section: &str, key: &str) -> Option<&'a str> { +fn ini_value<'a>(text: &'a str, section: &str, key: &str) -> Option<&'a str> { let mut in_section = false; let mut found = None; for line in text.lines().map(str::trim) {