refactor(kde): move the animation settings to settings, scoped guard for stacking

Plan step 10, S5 for KDE. The seven ANIMATION_* statics are gone. The
Animation, AnimationDuration, AnimationFps and AnimationStyle commands each
write settings().shared.animation with one update (a global value clears that
setting's per-prefix map, a prefix inserts one entry, as before); the
animation block of a load is one update through SharedDelta::apply (a global
value clears the map, a map replaces it, a block without fps has the default,
no block keeps everything); movement_animation and movement_style read one
snapshot (Animation::motion_for).

The seven commands that rearrange a stack (StackWindow, UnstackWindow,
StackAll, UnstackAll, CycleStack, CycleStackIndex, FocusStackWindow) used
ANIMATION_ENABLED_GLOBAL.swap(false) and store(restore), which overwrote a
reload or command that ran in between, and was skipped by an early return.
They now hold settings::animation_off(): Handle::scoped on the global enabled
flag, which puts the previous value back on drop only if the field still holds
false (a change made meanwhile wins, an early return still restores). Same
scope as before: the global flag only, not the per-animation map.

BEHAVIOUR: (1) one publish per load / command; (2) the restore no longer
clobbers a concurrent change; (3) an error return in a stack command now
restores the flag. Gate: KDE VM suites that stack windows (stacks, animation).

Verified: KDE cargo test --locked -p komorebi-linux --lib --tests (lite
env): 58 binaries, 623 passed, 0 failed; the existing tests animation.rs,
animation_config.rs (reload: global versus per-kind, fps default) and
animation_stack.rs (animation back on after each stack command) read through
settings(). VM suites not run.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent 28db46e4f1
commit 3c1339a5a4
7 files changed
+99 -153

No files matched your search

+12 -43
View File
@@ -10,14 +10,8 @@
use crate::core::animation::AnimationStyle;
use parking_lot::Mutex;
pub use prefix::AnimationPrefix;
pub use komorebi_config::animation::PerAnimationPrefixConfig;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::LazyLock;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicU64;
pub mod prefix;
@@ -27,52 +21,27 @@ pub const DEFAULT_ANIMATION_STYLE: AnimationStyle = AnimationStyle::Linear;
pub const DEFAULT_ANIMATION_DURATION: u64 = 250;
pub const DEFAULT_ANIMATION_FPS: u64 = 60;
pub static ANIMATION_STYLE_GLOBAL: LazyLock<Arc<Mutex<AnimationStyle>>> =
LazyLock::new(|| Arc::new(Mutex::new(DEFAULT_ANIMATION_STYLE)));
pub static ANIMATION_ENABLED_GLOBAL: LazyLock<Arc<AtomicBool>> =
LazyLock::new(|| Arc::new(AtomicBool::new(DEFAULT_ANIMATION_ENABLED)));
pub static ANIMATION_DURATION_GLOBAL: LazyLock<Arc<AtomicU64>> =
LazyLock::new(|| Arc::new(AtomicU64::new(DEFAULT_ANIMATION_DURATION)));
pub static ANIMATION_STYLE_PER_ANIMATION: LazyLock<
Arc<Mutex<HashMap<AnimationPrefix, AnimationStyle>>>,
> = LazyLock::new(|| Arc::new(Mutex::new(HashMap::new())));
pub static ANIMATION_ENABLED_PER_ANIMATION: LazyLock<Arc<Mutex<HashMap<AnimationPrefix, bool>>>> =
LazyLock::new(|| Arc::new(Mutex::new(HashMap::new())));
pub static ANIMATION_DURATION_PER_ANIMATION: LazyLock<Arc<Mutex<HashMap<AnimationPrefix, u64>>>> =
LazyLock::new(|| Arc::new(Mutex::new(HashMap::new())));
pub static ANIMATION_FPS: AtomicU64 = AtomicU64::new(DEFAULT_ANIMATION_FPS);
/// Returns how long window movement is animated for, in milliseconds, or `None` when it is not
/// animated.
///
/// The `movement` settings win over the global ones, for whether it is animated and for how long.
pub fn movement_animation() -> Option<u64> {
let enabled = ANIMATION_ENABLED_PER_ANIMATION
.lock()
.get(&AnimationPrefix::Movement)
.copied()
.unwrap_or_else(|| ANIMATION_ENABLED_GLOBAL.load(std::sync::atomic::Ordering::SeqCst));
enabled.then(|| {
ANIMATION_DURATION_PER_ANIMATION
.lock()
.get(&AnimationPrefix::Movement)
.copied()
.unwrap_or_else(|| ANIMATION_DURATION_GLOBAL.load(std::sync::atomic::Ordering::SeqCst))
})
crate::settings::settings()
.snapshot()
.shared
.animation
.motion_for(AnimationPrefix::Movement)
.map(|motion| motion.duration_ms)
}
/// Returns the easing of window movement: the `movement` style, else the global one.
pub fn movement_style() -> AnimationStyle {
ANIMATION_STYLE_PER_ANIMATION
.lock()
let snapshot = crate::settings::settings().snapshot();
let animation = &snapshot.shared.animation;
animation
.style_per_animation
.get(&AnimationPrefix::Movement)
.copied()
.unwrap_or_else(|| *ANIMATION_STYLE_GLOBAL.lock())
.unwrap_or(animation.style)
}
@@ -219,6 +219,7 @@
//! Once a message has been handled, every subscriber is sent a notification of it
//! ([`notify_subscribers`]).
use crate::settings::animation_off;
use crate::DATA_DIR;
use crate::FLOATING_APPLICATIONS;
use crate::IGNORE_IDENTIFIERS;
@@ -229,13 +230,6 @@ use crate::SESSION_FLOATING_APPLICATIONS;
use crate::SUBSCRIPTION_SOCKET_OPTIONS;
use crate::SUBSCRIPTION_SOCKETS;
use crate::WORKSPACE_MATCHING_RULES;
use crate::animation::ANIMATION_DURATION_GLOBAL;
use crate::animation::ANIMATION_DURATION_PER_ANIMATION;
use crate::animation::ANIMATION_ENABLED_GLOBAL;
use crate::animation::ANIMATION_ENABLED_PER_ANIMATION;
use crate::animation::ANIMATION_FPS;
use crate::animation::ANIMATION_STYLE_GLOBAL;
use crate::animation::ANIMATION_STYLE_PER_ANIMATION;
use crate::backend::backend;
use crate::border_manager;
use crate::core::ApplicationIdentifier;
@@ -1174,36 +1168,30 @@ impl WindowManager {
self.move_container_in_cycle_direction(direction)?;
}
SocketMessage::StackWindow(direction) => {
let restore = ANIMATION_ENABLED_GLOBAL.swap(false, Ordering::SeqCst);
let _animation_off = animation_off();
self.add_window_to_container(direction)?;
ANIMATION_ENABLED_GLOBAL.store(restore, Ordering::SeqCst);
}
SocketMessage::UnstackWindow => {
let restore = ANIMATION_ENABLED_GLOBAL.swap(false, Ordering::SeqCst);
let _animation_off = animation_off();
self.remove_window_from_container()?;
border_manager::destroy_all_borders()?;
ANIMATION_ENABLED_GLOBAL.store(restore, Ordering::SeqCst);
}
SocketMessage::StackAll => {
let restore = ANIMATION_ENABLED_GLOBAL.swap(false, Ordering::SeqCst);
let _animation_off = animation_off();
self.stack_all()?;
ANIMATION_ENABLED_GLOBAL.store(restore, Ordering::SeqCst);
}
SocketMessage::UnstackAll => {
let restore = ANIMATION_ENABLED_GLOBAL.swap(false, Ordering::SeqCst);
let _animation_off = animation_off();
self.unstack_all(true)?;
border_manager::destroy_all_borders()?;
ANIMATION_ENABLED_GLOBAL.store(restore, Ordering::SeqCst);
}
SocketMessage::CycleStack(direction) => {
let restore = ANIMATION_ENABLED_GLOBAL.swap(false, Ordering::SeqCst);
let _animation_off = animation_off();
self.cycle_container_window_in_direction(direction)?;
ANIMATION_ENABLED_GLOBAL.store(restore, Ordering::SeqCst);
}
SocketMessage::CycleStackIndex(direction) => {
let restore = ANIMATION_ENABLED_GLOBAL.swap(false, Ordering::SeqCst);
let _animation_off = animation_off();
self.cycle_container_window_index_in_direction(direction)?;
ANIMATION_ENABLED_GLOBAL.store(restore, Ordering::SeqCst);
}
SocketMessage::FocusStackWindow(idx) => {
// In case you are using this command on a bar on a monitor
@@ -1213,9 +1201,8 @@ impl WindowManager {
if let Some(monitor_idx) = self.monitor_idx_from_current_pos() {
self.focus_monitor(monitor_idx)?;
}
let restore = ANIMATION_ENABLED_GLOBAL.swap(false, Ordering::SeqCst);
let _animation_off = animation_off();
self.focus_container_window(idx)?;
ANIMATION_ENABLED_GLOBAL.store(restore, Ordering::SeqCst);
}
SocketMessage::CycleFocusMonitor(direction) => {
let monitor_idx = direction.next_idx(
@@ -2067,41 +2054,51 @@ impl WindowManager {
border_manager::BORDER_OFFSET.store(offset, Ordering::SeqCst);
border_manager::destroy_all_borders()?;
}
SocketMessage::Animation(enable, prefix) => match prefix {
Some(prefix) => {
ANIMATION_ENABLED_PER_ANIMATION
.lock()
.insert(prefix, enable);
}
None => {
ANIMATION_ENABLED_GLOBAL.store(enable, Ordering::SeqCst);
ANIMATION_ENABLED_PER_ANIMATION.lock().clear();
}
},
SocketMessage::AnimationDuration(duration, prefix) => match prefix {
Some(prefix) => {
ANIMATION_DURATION_PER_ANIMATION
.lock()
.insert(prefix, duration);
}
None => {
ANIMATION_DURATION_GLOBAL.store(duration, Ordering::SeqCst);
ANIMATION_DURATION_PER_ANIMATION.lock().clear();
}
},
SocketMessage::AnimationFps(fps) => {
ANIMATION_FPS.store(fps, Ordering::SeqCst);
SocketMessage::Animation(enable, prefix) => {
crate::settings::settings().update(|settings| {
let animation = &mut settings.shared.animation;
match prefix {
Some(prefix) => {
animation.enabled_per_animation.insert(prefix, enable);
}
None => {
animation.enabled = enable;
animation.enabled_per_animation.clear();
}
}
});
}
SocketMessage::AnimationDuration(duration, prefix) => {
crate::settings::settings().update(|settings| {
let animation = &mut settings.shared.animation;
match prefix {
Some(prefix) => {
animation.duration_per_animation.insert(prefix, duration);
}
None => {
animation.duration = duration;
animation.duration_per_animation.clear();
}
}
});
}
SocketMessage::AnimationFps(fps) => {
crate::settings::settings().update(|settings| settings.shared.animation.fps = fps);
}
SocketMessage::AnimationStyle(style, prefix) => {
crate::settings::settings().update(|settings| {
let animation = &mut settings.shared.animation;
match prefix {
Some(prefix) => {
animation.style_per_animation.insert(prefix, style);
}
None => {
animation.style = style;
animation.style_per_animation.clear();
}
}
});
}
SocketMessage::AnimationStyle(style, prefix) => match prefix {
Some(prefix) => {
ANIMATION_STYLE_PER_ANIMATION.lock().insert(prefix, style);
}
None => {
let mut animation_style = ANIMATION_STYLE_GLOBAL.lock();
*animation_style = style;
ANIMATION_STYLE_PER_ANIMATION.lock().clear();
}
},
SocketMessage::ReloadHotkeys => crate::kwin::hotkeys::reload()?,
SocketMessage::HotkeysState => {
let state = serde_json::to_string_pretty(&crate::kwin::hotkeys::state())?;
+17
View File
@@ -77,6 +77,23 @@ pub fn transparency_alpha() -> u8 {
settings().snapshot().shared.transparency.alpha
}
/// Turns the global animation off until the guard is dropped, then puts back what it was unless
/// something changed it meanwhile (a reload or a command wins). Per-animation values are not touched.
/// This replaces the `swap(false)` and `store(restore)` pair of the commands that rearrange a stack.
pub fn animation_off() -> komorebi_settings::Scoped<'static, KdeSettings, bool> {
settings().scoped(|settings| &mut settings.shared.animation.enabled, false)
}
/// `animation.fps`.
pub fn animation_fps() -> u64 {
settings().snapshot().shared.animation.fps
}
/// The global `animation.enabled`.
pub fn animation_enabled() -> bool {
settings().snapshot().shared.animation.enabled
}
#[cfg(test)]
mod tests {
use std::sync::atomic::Ordering;
+14 -45
View File
@@ -33,15 +33,6 @@ use crate::MANAGE_IDENTIFIERS;
use crate::REGEX_IDENTIFIERS;
use crate::TRANSPARENCY_BLACKLIST;
use crate::WORKSPACE_MATCHING_RULES;
use crate::animation::ANIMATION_DURATION_GLOBAL;
use crate::animation::ANIMATION_DURATION_PER_ANIMATION;
use crate::animation::ANIMATION_ENABLED_GLOBAL;
use crate::animation::ANIMATION_ENABLED_PER_ANIMATION;
use crate::animation::ANIMATION_FPS;
use crate::animation::ANIMATION_STYLE_GLOBAL;
use crate::animation::ANIMATION_STYLE_PER_ANIMATION;
use crate::animation::DEFAULT_ANIMATION_FPS;
use crate::animation::PerAnimationPrefixConfig;
use crate::backend::backend;
use crate::border_manager;
use crate::core::CrossBoundaryBehaviour;
@@ -72,6 +63,7 @@ use color_eyre::eyre;
use crossbeam_channel::Receiver;
use hotwatch::EventKind;
use hotwatch::Hotwatch;
use komorebi_settings::shared::AnimationDelta;
use komorebi_settings::shared::SharedDelta;
use komorebi_themes::colour::Colour;
use parking_lot::Mutex;
@@ -446,44 +438,21 @@ impl ApplyGlobals for StaticConfig {
// window::MINIMUM_WIDTH.store(width, Ordering::SeqCst);
// }
// the animation block, as one update: a global value clears its per-animation map, a map
// replaces the map, and a block without `fps` has the default (`SharedDelta::apply`)
if let Some(animations) = &self.animation {
match &animations.enabled {
PerAnimationPrefixConfig::Prefix(enabled) => {
ANIMATION_ENABLED_PER_ANIMATION.lock().clone_from(enabled);
crate::settings::settings().update(|settings| {
SharedDelta {
animation: Some(AnimationDelta {
enabled: animations.enabled.clone(),
style: animations.style.clone(),
duration: animations.duration.clone(),
fps: animations.fps,
}),
..SharedDelta::default()
}
PerAnimationPrefixConfig::Global(enabled) => {
ANIMATION_ENABLED_GLOBAL.store(*enabled, Ordering::SeqCst);
ANIMATION_ENABLED_PER_ANIMATION.lock().clear();
}
}
match &animations.style {
Some(PerAnimationPrefixConfig::Prefix(style)) => {
ANIMATION_STYLE_PER_ANIMATION.lock().clone_from(style);
}
Some(PerAnimationPrefixConfig::Global(style)) => {
let mut animation_style = ANIMATION_STYLE_GLOBAL.lock();
*animation_style = *style;
ANIMATION_STYLE_PER_ANIMATION.lock().clear();
}
None => {}
}
match &animations.duration {
Some(PerAnimationPrefixConfig::Prefix(duration)) => {
ANIMATION_DURATION_PER_ANIMATION.lock().clone_from(duration);
}
Some(PerAnimationPrefixConfig::Global(duration)) => {
ANIMATION_DURATION_GLOBAL.store(*duration, Ordering::SeqCst);
ANIMATION_DURATION_PER_ANIMATION.lock().clear();
}
None => {}
}
ANIMATION_FPS.store(
animations.fps.unwrap_or(DEFAULT_ANIMATION_FPS),
Ordering::SeqCst,
);
.apply(&mut settings.shared);
});
}
if let Some(border_width) = self.border_width {
+1 -3
View File
@@ -4,7 +4,6 @@
//! This file has one test because it changes the process-wide animation settings, which every
//! other test that asks for the look would see.
use komorebi::animation::ANIMATION_FPS;
use komorebi::animation::AnimationPrefix;
use komorebi::backend::AnimationLook;
use komorebi::backend::fake::Command;
@@ -14,7 +13,6 @@ use komorebi::core::SocketMessage;
use komorebi::core::animation::AnimationStyle;
use komorebi::window_manager::WindowManager;
use std::sync::Arc;
use std::sync::atomic::Ordering;
#[test]
fn the_animation_messages_set_the_animation_and_the_compositor_is_asked_for_it() {
@@ -86,6 +84,6 @@ fn the_animation_messages_set_the_animation_and_the_compositor_is_asked_for_it()
// the frame rate is kept, and asks nothing of the compositor
let asked = fake.commands().len();
send(&mut wm, SocketMessage::AnimationFps(30));
assert_eq!(ANIMATION_FPS.load(Ordering::SeqCst), 30);
assert_eq!(komorebi::settings::animation_fps(), 30);
assert_eq!(fake.commands().len(), asked);
}
@@ -6,7 +6,6 @@
//! other test that asks for the look would see.
use color_eyre::eyre;
use komorebi::animation::ANIMATION_FPS;
use komorebi::animation::movement_animation;
use komorebi::animation::movement_style;
use komorebi::backend::fake::FakeBackend;
@@ -19,7 +18,6 @@ use parking_lot::Mutex;
use std::os::unix::net::UnixListener;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::Ordering;
fn write_config(name: &str, json: &str) -> PathBuf {
let path = PathBuf::from(format!("/tmp/kmb-anim-{name}-{}.json", std::process::id()));
@@ -52,7 +50,7 @@ fn the_animation_key_of_the_file_is_the_animation_settings() {
// one value for every kind of animation
assert_eq!(movement_animation(), Some(120));
assert_eq!(movement_style(), AnimationStyle::EaseOutExpo);
assert_eq!(ANIMATION_FPS.load(Ordering::SeqCst), 30);
assert_eq!(komorebi::settings::animation_fps(), 30);
// a value for the kinds a file names wins over the global ones, which a global value takes away
let by_kind = write_config(
@@ -66,7 +64,7 @@ fn the_animation_key_of_the_file_is_the_animation_settings() {
assert_eq!(movement_animation(), None, "movement is off");
assert_eq!(movement_style(), AnimationStyle::EaseInOutSine);
assert_eq!(
ANIMATION_FPS.load(Ordering::SeqCst),
komorebi::settings::animation_fps(),
60,
"a file that leaves fps out has the default"
);
@@ -5,7 +5,6 @@
//! This file has one test because it changes the process-wide animation settings, which every
//! other test that asks for the look would see.
use komorebi::animation::ANIMATION_ENABLED_GLOBAL;
use komorebi::backend::AnimationLook;
use komorebi::backend::fake::Command;
use komorebi::backend::fake::FakeBackend;
@@ -19,7 +18,6 @@ use komorebi::window_manager_event::KWinNotification;
use komorebi::window_manager_event::SystemNotification;
use komorebi::window_manager_event::WindowManagerEvent;
use std::sync::Arc;
use std::sync::atomic::Ordering;
#[test]
fn the_animation_is_on_again_after_a_command_that_changes_a_stack() {
@@ -94,7 +92,7 @@ fn the_animation_is_on_again_after_a_command_that_changes_a_stack() {
fake.clear_commands();
send(&mut wm, message.clone());
assert!(
ANIMATION_ENABLED_GLOBAL.load(Ordering::SeqCst),
komorebi::settings::animation_enabled(),
"the animation is on again after {message:?}"
);
assert_eq!(