refactor(animation): move generation of animation key to RenderDispatcher

This commit is contained in:
thearturca
2024-09-28 20:33:16 +03:00
parent 2400d757fe
commit 44189d8382
3 changed files with 15 additions and 24 deletions

View File

@@ -56,11 +56,11 @@ impl Animation {
#[allow(clippy::cast_precision_loss)]
pub fn animate(
animation_key: String,
duration: Duration,
render_dispatcher: (impl RenderDispatcher + Send + 'static),
duration: Duration,
) -> Result<()> {
std::thread::spawn(move || {
let animation_key = render_dispatcher.get_animation_key();
if ANIMATION_MANAGER.lock().in_progress(animation_key.as_str()) {
let should_animate = Self::cancel(animation_key.as_str());

View File

@@ -1,6 +1,7 @@
use color_eyre::Result;
pub trait RenderDispatcher {
fn get_animation_key(&self) -> String;
fn pre_render(&self) -> Result<()>;
fn render(&self, delta: f64) -> Result<()>;
fn post_render(&self) -> Result<()>;

View File

@@ -2,7 +2,6 @@ use crate::animation::lerp::Lerp;
use crate::animation::prefix::new_animation_key;
use crate::animation::prefix::AnimationPrefix;
use crate::animation::RenderDispatcher;
// use crate::animation::renderer::Renderer;
use crate::animation::ANIMATION_DURATION;
use crate::animation::ANIMATION_ENABLED;
use crate::animation::ANIMATION_MANAGER;
@@ -185,6 +184,10 @@ impl WindowMoveRenderDispatcher {
}
impl RenderDispatcher for WindowMoveRenderDispatcher {
fn get_animation_key(&self) -> String {
new_animation_key(self.prefix, self.hwnd.to_string())
}
fn pre_render(&self) -> Result<()> {
border_manager::BORDER_TEMPORARILY_DISABLED.store(true, Ordering::SeqCst);
border_manager::send_notification(Some(self.hwnd));
@@ -232,14 +235,12 @@ struct WindowTransparencyRenderDispatcher {
target_opacity: u8,
style: AnimationStyle,
is_opaque: bool,
is_transparent: bool,
}
impl WindowTransparencyRenderDispatcher {
pub fn new(
hwnd: isize,
is_opaque: bool,
is_transparent: bool,
start_opacity: u8,
target_opacity: u8,
style: AnimationStyle,
@@ -251,15 +252,18 @@ impl WindowTransparencyRenderDispatcher {
target_opacity,
style,
is_opaque,
is_transparent,
}
}
}
impl RenderDispatcher for WindowTransparencyRenderDispatcher {
fn get_animation_key(&self) -> String {
new_animation_key(self.prefix, self.hwnd.to_string())
}
fn pre_render(&self) -> Result<()> {
//transparent
if self.is_transparent {
if !self.is_opaque {
let window = Window::from(self.hwnd);
let mut ex_style = window.ex_style()?;
ex_style.insert(ExtendedWindowStyle::LAYERED);
@@ -349,11 +353,7 @@ impl Window {
let render_dispatcher =
WindowMoveRenderDispatcher::new(self.hwnd, window_rect, *layout, top, style);
Animation::animate(
new_animation_key(render_dispatcher.prefix, self.hwnd.to_string()),
duration,
render_dispatcher,
)
Animation::animate(render_dispatcher, duration)
} else {
WindowsApi::position_window(self.hwnd, layout, top)
}
@@ -469,17 +469,12 @@ impl Window {
let render_dispatcher = WindowTransparencyRenderDispatcher::new(
self.hwnd,
false,
true,
WindowsApi::get_transparent(self.hwnd).unwrap_or(255),
transparency_manager::TRANSPARENCY_ALPHA.load_consume(),
style,
);
Animation::animate(
new_animation_key(render_dispatcher.prefix, self.hwnd.to_string()),
duration,
render_dispatcher,
)
Animation::animate(render_dispatcher, duration)
} else {
let mut ex_style = self.ex_style()?;
ex_style.insert(ExtendedWindowStyle::LAYERED);
@@ -499,18 +494,13 @@ impl Window {
let render_dispatcher = WindowTransparencyRenderDispatcher::new(
self.hwnd,
true,
false,
WindowsApi::get_transparent(self.hwnd)
.unwrap_or(transparency_manager::TRANSPARENCY_ALPHA.load_consume()),
255,
style,
);
Animation::animate(
new_animation_key(render_dispatcher.prefix, self.hwnd.to_string()),
duration,
render_dispatcher,
)
Animation::animate(render_dispatcher, duration)
} else {
let mut ex_style = self.ex_style()?;
ex_style.remove(ExtendedWindowStyle::LAYERED);