diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2024-11-24 09:41:43 +0300 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2024-11-25 04:07:59 -0800 |
| commit | 4c22c3285d8b10fbcef1c45a0788c3ddca03ec62 (patch) | |
| tree | 506174fe9962a91598ac25d4d2dee1cdaa3d5292 /src/ui | |
| parent | 93cee2994ab9ccf59a09f61d5b8acf6cd937d654 (diff) | |
| download | niri-4c22c3285d8b10fbcef1c45a0788c3ddca03ec62.tar.gz niri-4c22c3285d8b10fbcef1c45a0788c3ddca03ec62.tar.bz2 niri-4c22c3285d8b10fbcef1c45a0788c3ddca03ec62.zip | |
Refactor animation timing to use lazy clocks
Diffstat (limited to 'src/ui')
| -rw-r--r-- | src/ui/config_error_notification.rs | 10 | ||||
| -rw-r--r-- | src/ui/screen_transition.rs | 40 | ||||
| -rw-r--r-- | src/ui/screenshot_ui.rs | 11 |
3 files changed, 29 insertions, 32 deletions
diff --git a/src/ui/config_error_notification.rs b/src/ui/config_error_notification.rs index af4f67e3..bd46aa7d 100644 --- a/src/ui/config_error_notification.rs +++ b/src/ui/config_error_notification.rs @@ -60,7 +60,7 @@ impl ConfigErrorNotification { fn animation(&self, from: f64, to: f64) -> Animation { let c = self.config.borrow(); Animation::new( - self.clock.now(), + self.clock.clone(), from, to, 0., @@ -96,11 +96,10 @@ impl ConfigErrorNotification { self.state = State::Hiding(self.animation(1., 0.)); } - pub fn advance_animations(&mut self, target_presentation_time: Duration) { + pub fn advance_animations(&mut self) { match &mut self.state { State::Hidden => (), State::Showing(anim) => { - anim.set_current_time(target_presentation_time); if anim.is_done() { let duration = if self.created_path.is_some() { // Make this quite a bit longer because it comes with a monitor modeset @@ -110,16 +109,15 @@ impl ConfigErrorNotification { } else { Duration::from_secs(4) }; - self.state = State::Shown(target_presentation_time + duration); + self.state = State::Shown(self.clock.now() + duration); } } State::Shown(deadline) => { - if target_presentation_time >= *deadline { + if self.clock.now() >= *deadline { self.hide(); } } State::Hiding(anim) => { - anim.set_current_time(target_presentation_time); if anim.is_clamped_done() { self.state = State::Hidden; } diff --git a/src/ui/screen_transition.rs b/src/ui/screen_transition.rs index 3b5c2159..7d26f85f 100644 --- a/src/ui/screen_transition.rs +++ b/src/ui/screen_transition.rs @@ -4,6 +4,7 @@ use smithay::backend::renderer::element::Kind; use smithay::backend::renderer::gles::GlesTexture; use smithay::utils::{Scale, Transform}; +use crate::animation::Clock; use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; use crate::render_helpers::RenderTarget; @@ -17,31 +18,25 @@ pub struct ScreenTransition { from_texture: [TextureBuffer<GlesTexture>; 3], /// Monotonic time when to start the crossfade. start_at: Duration, - /// Current crossfade alpha. - alpha: f32, + /// Clock to drive animations. + clock: Clock, } impl ScreenTransition { - pub fn new(from_texture: [TextureBuffer<GlesTexture>; 3], start_at: Duration) -> Self { + pub fn new( + from_texture: [TextureBuffer<GlesTexture>; 3], + delay: Duration, + clock: Clock, + ) -> Self { Self { from_texture, - start_at, - alpha: 1., - } - } - - pub fn advance_animations(&mut self, current_time: Duration) { - if self.start_at + DURATION <= current_time { - self.alpha = 0.; - } else if self.start_at <= current_time { - self.alpha = 1. - (current_time - self.start_at).as_secs_f32() / DURATION.as_secs_f32(); - } else { - self.alpha = 1.; + start_at: clock.now_unadjusted() + delay, + clock, } } pub fn is_done(&self) -> bool { - self.alpha == 0. + self.start_at + DURATION <= self.clock.now_unadjusted() } pub fn update_render_elements(&mut self, scale: Scale<f64>, transform: Transform) { @@ -53,6 +48,17 @@ impl ScreenTransition { } pub fn render(&self, target: RenderTarget) -> PrimaryGpuTextureRenderElement { + // Screen transition ignores animation slowdown. + let now = self.clock.now_unadjusted(); + + let alpha = if self.start_at + DURATION <= now { + 0. + } else if self.start_at <= now { + 1. - (now - self.start_at).as_secs_f32() / DURATION.as_secs_f32() + } else { + 1. + }; + let idx = match target { RenderTarget::Output => 0, RenderTarget::Screencast => 1, @@ -62,7 +68,7 @@ impl ScreenTransition { PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( self.from_texture[idx].clone(), (0., 0.), - self.alpha, + alpha, None, None, Kind::Unspecified, diff --git a/src/ui/screenshot_ui.rs b/src/ui/screenshot_ui.rs index cde9bdac..f7d1bd1a 100644 --- a/src/ui/screenshot_ui.rs +++ b/src/ui/screenshot_ui.rs @@ -3,7 +3,6 @@ use std::cmp::{max, min}; use std::collections::HashMap; use std::iter::zip; use std::rc::Rc; -use std::time::Duration; use anyhow::Context; use arrayvec::ArrayVec; @@ -185,7 +184,7 @@ impl ScreenshotUi { let open_anim = { let c = config.borrow(); - Animation::new(clock.now(), 0., 1., 0., c.animations.screenshot_ui_open.0) + Animation::new(clock.clone(), 0., 1., 0., c.animations.screenshot_ui_open.0) }; *self = Self::Open { @@ -238,13 +237,7 @@ impl ScreenshotUi { matches!(self, ScreenshotUi::Open { .. }) } - pub fn advance_animations(&mut self, current_time: Duration) { - let Self::Open { open_anim, .. } = self else { - return; - }; - - open_anim.set_current_time(current_time); - } + pub fn advance_animations(&mut self) {} pub fn are_animations_ongoing(&self) -> bool { let Self::Open { open_anim, .. } = self else { |
