diff --git a/komorebi-for-mac/komorebi/src/animation/mod.rs b/komorebi-for-mac/komorebi/src/animation/mod.rs index 1213a6d6..5be7b245 100644 --- a/komorebi-for-mac/komorebi/src/animation/mod.rs +++ b/komorebi-for-mac/komorebi/src/animation/mod.rs @@ -1,13 +1,7 @@ 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; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use std::time::Duration; @@ -35,27 +29,6 @@ pub const DEFAULT_ANIMATION_FPS: u64 = 60; /// How many operations have asked for animations to be skipped for as long as they run static ANIMATIONS_SUSPENDED: AtomicUsize = AtomicUsize::new(0); -pub static ANIMATION_STYLE_GLOBAL: LazyLock>> = - LazyLock::new(|| Arc::new(Mutex::new(DEFAULT_ANIMATION_STYLE))); - -pub static ANIMATION_ENABLED_GLOBAL: LazyLock> = - LazyLock::new(|| Arc::new(AtomicBool::new(DEFAULT_ANIMATION_ENABLED))); - -pub static ANIMATION_DURATION_GLOBAL: LazyLock> = - LazyLock::new(|| Arc::new(AtomicU64::new(DEFAULT_ANIMATION_DURATION))); - -pub static ANIMATION_STYLE_PER_ANIMATION: LazyLock< - Arc>>, -> = LazyLock::new(|| Arc::new(Mutex::new(HashMap::new()))); - -pub static ANIMATION_ENABLED_PER_ANIMATION: LazyLock>>> = - LazyLock::new(|| Arc::new(Mutex::new(HashMap::new()))); - -pub static ANIMATION_DURATION_PER_ANIMATION: LazyLock>>> = - LazyLock::new(|| Arc::new(Mutex::new(HashMap::new()))); - -pub static ANIMATION_FPS: AtomicU64 = AtomicU64::new(DEFAULT_ANIMATION_FPS); - /// Skips animations for as long as the guard is held /// /// Some operations, such as stacking windows, rearrange a workspace in a way that animating would @@ -81,29 +54,16 @@ pub fn motion_for(prefix: AnimationPrefix) -> Option { return None; } - let enabled = ANIMATION_ENABLED_PER_ANIMATION - .lock() - .get(&prefix) - .copied() - .unwrap_or_else(|| ANIMATION_ENABLED_GLOBAL.load(Ordering::SeqCst)); - - if !enabled { - return None; - } + // one snapshot: enabled, duration and style are read from the same configuration + let motion = crate::settings::settings() + .snapshot() + .shared + .animation + .motion_for(prefix)?; Some(MotionSpec { - duration: Duration::from_millis( - ANIMATION_DURATION_PER_ANIMATION - .lock() - .get(&prefix) - .copied() - .unwrap_or_else(|| ANIMATION_DURATION_GLOBAL.load(Ordering::SeqCst)), - ), - style: ANIMATION_STYLE_PER_ANIMATION - .lock() - .get(&prefix) - .copied() - .unwrap_or_else(|| *ANIMATION_STYLE_GLOBAL.lock()), + duration: Duration::from_millis(motion.duration_ms), + style: motion.style, }) } @@ -114,7 +74,7 @@ pub fn workspace_transitions_enabled() -> bool { /// How long one frame takes, given the configured frames per second pub fn frame_period() -> Duration { - let fps = ANIMATION_FPS.load(Ordering::Relaxed).clamp(1, 240); + let fps = crate::settings::animation_fps().clamp(1, 240); Duration::from_secs_f64(1.0 / fps as f64) } diff --git a/komorebi-for-mac/komorebi/src/process_command.rs b/komorebi-for-mac/komorebi/src/process_command.rs index 3d01c6eb..05286068 100644 --- a/komorebi-for-mac/komorebi/src/process_command.rs +++ b/komorebi-for-mac/komorebi/src/process_command.rs @@ -9,13 +9,6 @@ use crate::SUBSCRIPTION_SOCKET_OPTIONS; use crate::SUBSCRIPTION_SOCKETS; use crate::WORKSPACE_MATCHING_RULES; use crate::animation; -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::application::Application; use crate::border_manager; use crate::cf_array_as; @@ -1808,41 +1801,51 @@ impl WindowManager { SocketMessage::Theme(ref theme) => { theme_manager::send_notification(*theme.clone()); } - 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(); - } - }, // Messages of other platforms: answered, and nothing else happens SocketMessage::AddSubscriberPipe(..) | SocketMessage::AltFocusHack(..) diff --git a/komorebi-for-mac/komorebi/src/settings.rs b/komorebi-for-mac/komorebi/src/settings.rs index 1c1f9213..f949c7e6 100644 --- a/komorebi-for-mac/komorebi/src/settings.rs +++ b/komorebi-for-mac/komorebi/src/settings.rs @@ -67,6 +67,11 @@ pub fn display_index_preferences() -> HashMap { .clone() } +/// `animation.fps`. +pub fn animation_fps() -> u64 { + settings().snapshot().shared.animation.fps +} + #[cfg(test)] mod tests { use std::sync::atomic::Ordering; diff --git a/komorebi-for-mac/komorebi/src/static_config.rs b/komorebi-for-mac/komorebi/src/static_config.rs index 018cf428..ffbd9716 100644 --- a/komorebi-for-mac/komorebi/src/static_config.rs +++ b/komorebi-for-mac/komorebi/src/static_config.rs @@ -1,3 +1,4 @@ +use komorebi_settings::shared::AnimationDelta; use komorebi_settings::shared::SharedDelta; use crate::CoreFoundationRunLoop; use crate::DATA_DIR; @@ -10,15 +11,6 @@ use crate::REGEX_IDENTIFIERS; use crate::TABBED_APPLICATIONS; use crate::TITLELESS_APPLICATIONS; 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::border_manager; use crate::core::ApplicationIdentifier; use crate::core::CrossBoundaryBehaviour; @@ -421,44 +413,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 { diff --git a/komorebi-for-mac/komorebi/src/window_manager.rs b/komorebi-for-mac/komorebi/src/window_manager.rs index 2f3f0471..1d6ff564 100644 --- a/komorebi-for-mac/komorebi/src/window_manager.rs +++ b/komorebi-for-mac/komorebi/src/window_manager.rs @@ -4033,18 +4033,26 @@ mod tests { impl AnimationsOn { fn new() -> Self { - animation::ANIMATION_ENABLED_PER_ANIMATION - .lock() - .insert(AnimationPrefix::Movement, true); + crate::settings::settings().update(|settings| { + settings + .shared + .animation + .enabled_per_animation + .insert(AnimationPrefix::Movement, true); + }); Self } } impl Drop for AnimationsOn { fn drop(&mut self) { - animation::ANIMATION_ENABLED_PER_ANIMATION - .lock() - .remove(&AnimationPrefix::Movement); + crate::settings::settings().update(|settings| { + settings + .shared + .animation + .enabled_per_animation + .remove(&AnimationPrefix::Movement); + }); } }