From d3ac6b72c225f830a4e91d2e1197e6dcb5c68430 Mon Sep 17 00:00:00 2001 From: thearturca Date: Tue, 17 Sep 2024 22:12:42 +0300 Subject: [PATCH] refactor(animation): reduce mutex call on `ANIMATION_STYLE` --- komorebi/src/animation/animation.rs | 15 ++++++++------- komorebi/src/animation/style.rs | 5 +---- komorebi/src/window.rs | 6 ++++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/komorebi/src/animation/animation.rs b/komorebi/src/animation/animation.rs index 39e33571..12b96c34 100644 --- a/komorebi/src/animation/animation.rs +++ b/komorebi/src/animation/animation.rs @@ -13,6 +13,7 @@ use super::style::apply_ease_func; use super::ANIMATION_DURATION; use super::ANIMATION_FPS; use super::ANIMATION_MANAGER; +use crate::AnimationStyle; #[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, JsonSchema, PartialEq)] pub struct Animation { @@ -53,19 +54,19 @@ impl Animation { } #[allow(clippy::cast_possible_truncation)] - pub fn lerp(start: i32, end: i32, t: f64) -> i32 { - let time = apply_ease_func(t); + pub fn lerp(start: i32, end: i32, t: f64, style: AnimationStyle) -> i32 { + let time = apply_ease_func(t, style); f64::from(end - start) .mul_add(time, f64::from(start)) .round() as i32 } - pub fn lerp_rect(start_rect: &Rect, end_rect: &Rect, t: f64) -> Rect { + pub fn lerp_rect(start_rect: &Rect, end_rect: &Rect, t: f64, style: AnimationStyle) -> Rect { Rect { - left: Self::lerp(start_rect.left, end_rect.left, t), - top: Self::lerp(start_rect.top, end_rect.top, t), - right: Self::lerp(start_rect.right, end_rect.right, t), - bottom: Self::lerp(start_rect.bottom, end_rect.bottom, t), + left: Self::lerp(start_rect.left, end_rect.left, t, style), + top: Self::lerp(start_rect.top, end_rect.top, t, style), + right: Self::lerp(start_rect.right, end_rect.right, t, style), + bottom: Self::lerp(start_rect.bottom, end_rect.bottom, t, style), } } diff --git a/komorebi/src/animation/style.rs b/komorebi/src/animation/style.rs index 7ba4c75a..31bbdef3 100644 --- a/komorebi/src/animation/style.rs +++ b/komorebi/src/animation/style.rs @@ -1,4 +1,3 @@ -use super::ANIMATION_STYLE; use crate::core::AnimationStyle; use std::f64::consts::PI; @@ -356,9 +355,7 @@ impl Ease for EaseInOutBounce { } } -pub fn apply_ease_func(t: f64) -> f64 { - let style = *ANIMATION_STYLE.lock(); - +pub fn apply_ease_func(t: f64, style: AnimationStyle) -> f64 { match style { AnimationStyle::Linear => Linear::evaluate(t), AnimationStyle::EaseInSine => EaseInSine::evaluate(t), diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 6b956648..deb16621 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -1,6 +1,7 @@ use crate::animation::ANIMATIONS_IN_PROGRESS; use crate::animation::ANIMATION_DURATION; use crate::animation::ANIMATION_ENABLED; +use crate::animation::ANIMATION_STYLE; use crate::border_manager; use crate::com::SetCloak; use crate::focus_manager; @@ -203,8 +204,9 @@ impl Window { pub fn animate_position(&self, start_rect: &Rect, target_rect: &Rect, top: bool) -> Result<()> { let start_rect = *start_rect; let target_rect = *target_rect; - let duration = Duration::from_millis(ANIMATION_DURATION.load(Ordering::SeqCst)); let mut animation = self.animation; + let duration = Duration::from_millis(ANIMATION_DURATION.load(Ordering::SeqCst)); + let style = *ANIMATION_STYLE.lock(); border_manager::BORDER_TEMPORARILY_DISABLED.store(true, Ordering::SeqCst); border_manager::send_notification(Some(self.hwnd)); @@ -216,7 +218,7 @@ impl Window { std::thread::spawn(move || { animation.animate(duration, |progress: f64| { - let new_rect = Animation::lerp_rect(&start_rect, &target_rect, progress); + let new_rect = Animation::lerp_rect(&start_rect, &target_rect, progress, style); if progress == 1.0 { WindowsApi::position_window(hwnd, &new_rect, top)?;