diff --git a/komorebi/src/animation/animation.rs b/komorebi/src/animation/animation.rs index 12b96c34..a32beda7 100644 --- a/komorebi/src/animation/animation.rs +++ b/komorebi/src/animation/animation.rs @@ -53,23 +53,6 @@ impl Animation { latest_cancel_idx == cancel_idx } - #[allow(clippy::cast_possible_truncation)] - 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, style: AnimationStyle) -> Rect { - Rect { - 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), - } - } - #[allow(clippy::cast_precision_loss)] pub fn animate( &mut self, diff --git a/komorebi/src/animation/lerp.rs b/komorebi/src/animation/lerp.rs new file mode 100644 index 00000000..46afd89f --- /dev/null +++ b/komorebi/src/animation/lerp.rs @@ -0,0 +1,36 @@ +use crate::core::Rect; +use crate::AnimationStyle; + +use super::style::apply_ease_func; + +pub trait Lerp { + fn lerp(self, end: T, time: f64, style: AnimationStyle) -> T; +} + +impl Lerp for i32 { + #[allow(clippy::cast_possible_truncation)] + fn lerp(self, end: i32, time: f64, style: AnimationStyle) -> i32 { + let time = apply_ease_func(time, style); + + f64::from(end - self).mul_add(time, f64::from(self)).round() as i32 + } +} + +impl Lerp for f64 { + fn lerp(self, end: f64, time: f64, style: AnimationStyle) -> f64 { + let time = apply_ease_func(time, style); + + (end - self).mul_add(time, self) + } +} + +impl Lerp for Rect { + fn lerp(self, end: Rect, time: f64, style: AnimationStyle) -> Rect { + Rect { + left: self.left.lerp(end.left, time, style), + top: self.top.lerp(end.top, time, style), + right: self.right.lerp(end.right, time, style), + bottom: self.bottom.lerp(end.bottom, time, style), + } + } +} diff --git a/komorebi/src/animation/mod.rs b/komorebi/src/animation/mod.rs index 149c8847..5c986fcd 100644 --- a/komorebi/src/animation/mod.rs +++ b/komorebi/src/animation/mod.rs @@ -12,6 +12,7 @@ use parking_lot::Mutex; pub mod animation; pub use animation::Animation; pub mod animation_manager; +pub mod lerp; pub mod style; lazy_static! { diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index deb16621..e6c23ecd 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -1,3 +1,4 @@ +use crate::animation::lerp::Lerp; use crate::animation::ANIMATIONS_IN_PROGRESS; use crate::animation::ANIMATION_DURATION; use crate::animation::ANIMATION_ENABLED; @@ -218,7 +219,7 @@ impl Window { std::thread::spawn(move || { animation.animate(duration, |progress: f64| { - let new_rect = Animation::lerp_rect(&start_rect, &target_rect, progress, style); + let new_rect = start_rect.lerp(target_rect, progress, style); if progress == 1.0 { WindowsApi::position_window(hwnd, &new_rect, top)?;