mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 01:18:43 +00:00
Animation Effects: plainer Linux/BSD reader
- The worker thread shares its latest answer with the UI as a plain Option<bool> 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 <noreply@anthropic.com>
This commit is contained in:
1 parent
0bdac25172
commit
cfe8ae5c09
2 files changed
+27
-31
No files matched your search
@@ -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<photocraft_ui_egui::SystemAnimationsFn> {
|
||||
let latest = Arc::new(AtomicU8::new(encode(None)));
|
||||
let latest = Arc::new(Mutex::new(None));
|
||||
let wake: Arc<OnceLock<egui::Context>> = 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<AtomicU8>,
|
||||
latest: Arc<Mutex<Option<bool>>>,
|
||||
wake: Arc<OnceLock<egui::Context>>,
|
||||
}
|
||||
|
||||
impl Publisher {
|
||||
fn publish(&self, animations: Option<bool>) {
|
||||
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<bool>) -> u8 {
|
||||
match animations {
|
||||
None => 0,
|
||||
Some(true) => 1,
|
||||
Some(false) => 2,
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(code: u8) -> Option<bool> {
|
||||
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<Option<bool>>) -> MutexGuard<'_, Option<bool>> {
|
||||
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<PathBuf> {
|
||||
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<String> {
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<bool> {
|
||||
pub(super) fn from_enabled(value: &str) -> Option<bool> {
|
||||
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<bool> {
|
||||
}
|
||||
|
||||
/// KDE's animation speed factor: 0 turns animations off.
|
||||
pub fn from_duration_factor(value: &str) -> Option<bool> {
|
||||
pub(super) fn from_duration_factor(value: &str) -> Option<bool> {
|
||||
value.trim().parse::<f64>().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<bool> {
|
||||
pub(super) fn from_reduced_motion(value: &str) -> Option<bool> {
|
||||
value.trim().parse::<u32>().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 <schema> <key>`.
|
||||
pub fn without_portal(desktop: &str, config_file: &dyn Fn(&str) -> Option<String>, gsettings: &dyn Fn(&str, &str) -> Option<String>) -> Option<bool> {
|
||||
pub(super) fn without_portal(desktop: &str, config_file: &dyn Fn(&str) -> Option<String>, gsettings: &dyn Fn(&str, &str) -> Option<String>) -> Option<bool> {
|
||||
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<bool>| parse(ini_value(&config_file(file)?, section, key)?);
|
||||
let in_file = |file: &str, section: &str, key: &str, parse: fn(&str) -> Option<bool>| {
|
||||
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<String
|
||||
}
|
||||
// `true` may only be the schema's default on a desktop that doesn't use it, so GTK's own
|
||||
// file comes first: it is what a bare window manager's user writes, and what KDE keeps in step.
|
||||
["gtk-4.0/settings.ini", "gtk-3.0/settings.ini"].iter().find_map(|file| in_file(file, "Settings", "gtk-enable-animations", from_enabled)).or(gnome)
|
||||
let gtk = ["gtk-4.0/settings.ini", "gtk-3.0/settings.ini"].iter().find_map(|file| in_file(file, "Settings", "gtk-enable-animations", from_enabled));
|
||||
gtk.or(gnome)
|
||||
}
|
||||
|
||||
/// The last value of `key` in `[section]` of an INI-style file (KDE's config, GTK's settings).
|
||||
pub fn ini_value<'a>(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) {
|
||||
|
||||
Reference in new issue
Block a user