From 44189d8382f63cdd42ef77e7007a4e73dbe369c2 Mon Sep 17 00:00:00 2001 From: thearturca Date: Sat, 28 Sep 2024 20:33:16 +0300 Subject: [PATCH] refactor(animation): move generation of `animation key` to `RenderDispatcher` --- komorebi/src/animation/animation.rs | 4 +-- komorebi/src/animation/render_dispatcher.rs | 1 + komorebi/src/window.rs | 34 ++++++++------------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/komorebi/src/animation/animation.rs b/komorebi/src/animation/animation.rs index 5a17e9ec..855bb65c 100644 --- a/komorebi/src/animation/animation.rs +++ b/komorebi/src/animation/animation.rs @@ -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()); diff --git a/komorebi/src/animation/render_dispatcher.rs b/komorebi/src/animation/render_dispatcher.rs index e7930624..9109f411 100644 --- a/komorebi/src/animation/render_dispatcher.rs +++ b/komorebi/src/animation/render_dispatcher.rs @@ -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<()>; diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 1382ad61..d0a995dc 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -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);