mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-05-01 08:24:20 +02:00
refactor(animation): introduce Lerp trait
This commit is contained in:
@@ -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,
|
||||
|
||||
36
komorebi/src/animation/lerp.rs
Normal file
36
komorebi/src/animation/lerp.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use crate::core::Rect;
|
||||
use crate::AnimationStyle;
|
||||
|
||||
use super::style::apply_ease_func;
|
||||
|
||||
pub trait Lerp<T = Self> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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! {
|
||||
|
||||
@@ -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)?;
|
||||
|
||||
Reference in New Issue
Block a user