aboutsummaryrefslogtreecommitdiff
path: root/niri-visual-tests/src
diff options
context:
space:
mode:
authorIvan Molodetskikh <yalterz@gmail.com>2024-11-24 09:41:43 +0300
committerIvan Molodetskikh <yalterz@gmail.com>2024-11-25 04:07:59 -0800
commit4c22c3285d8b10fbcef1c45a0788c3ddca03ec62 (patch)
tree506174fe9962a91598ac25d4d2dee1cdaa3d5292 /niri-visual-tests/src
parent93cee2994ab9ccf59a09f61d5b8acf6cd937d654 (diff)
downloadniri-4c22c3285d8b10fbcef1c45a0788c3ddca03ec62.tar.gz
niri-4c22c3285d8b10fbcef1c45a0788c3ddca03ec62.tar.bz2
niri-4c22c3285d8b10fbcef1c45a0788c3ddca03ec62.zip
Refactor animation timing to use lazy clocks
Diffstat (limited to 'niri-visual-tests/src')
-rw-r--r--niri-visual-tests/src/cases/gradient_angle.rs11
-rw-r--r--niri-visual-tests/src/cases/gradient_area.rs11
-rw-r--r--niri-visual-tests/src/cases/layout.rs15
-rw-r--r--niri-visual-tests/src/cases/tile.rs4
-rw-r--r--niri-visual-tests/src/main.rs14
-rw-r--r--niri-visual-tests/src/smithay_view.rs33
6 files changed, 46 insertions, 42 deletions
diff --git a/niri-visual-tests/src/cases/gradient_angle.rs b/niri-visual-tests/src/cases/gradient_angle.rs
index 64c953c4..39a2e4ef 100644
--- a/niri-visual-tests/src/cases/gradient_angle.rs
+++ b/niri-visual-tests/src/cases/gradient_angle.rs
@@ -1,8 +1,6 @@
use std::f32::consts::{FRAC_PI_2, PI};
-use std::sync::atomic::Ordering;
use std::time::Duration;
-use niri::animation::ANIMATION_SLOWDOWN;
use niri::render_helpers::border::BorderRenderElement;
use niri_config::{Color, CornerRadius, GradientInterpolation};
use smithay::backend::renderer::element::RenderElement;
@@ -31,20 +29,13 @@ impl TestCase for GradientAngle {
}
fn advance_animations(&mut self, current_time: Duration) {
- let mut delta = if self.prev_time.is_zero() {
+ let delta = if self.prev_time.is_zero() {
Duration::ZERO
} else {
current_time.saturating_sub(self.prev_time)
};
self.prev_time = current_time;
- let slowdown = ANIMATION_SLOWDOWN.load(Ordering::SeqCst);
- if slowdown == 0. {
- delta = Duration::ZERO
- } else {
- delta = delta.div_f64(slowdown);
- }
-
self.angle += delta.as_secs_f32() * PI;
if self.angle >= PI * 2. {
diff --git a/niri-visual-tests/src/cases/gradient_area.rs b/niri-visual-tests/src/cases/gradient_area.rs
index 2fa275f2..6f0cf4f3 100644
--- a/niri-visual-tests/src/cases/gradient_area.rs
+++ b/niri-visual-tests/src/cases/gradient_area.rs
@@ -1,8 +1,6 @@
use std::f32::consts::{FRAC_PI_4, PI};
-use std::sync::atomic::Ordering;
use std::time::Duration;
-use niri::animation::ANIMATION_SLOWDOWN;
use niri::layout::focus_ring::FocusRing;
use niri::render_helpers::border::BorderRenderElement;
use niri_config::{Color, CornerRadius, FloatOrInt, GradientInterpolation};
@@ -43,20 +41,13 @@ impl TestCase for GradientArea {
}
fn advance_animations(&mut self, current_time: Duration) {
- let mut delta = if self.prev_time.is_zero() {
+ let delta = if self.prev_time.is_zero() {
Duration::ZERO
} else {
current_time.saturating_sub(self.prev_time)
};
self.prev_time = current_time;
- let slowdown = ANIMATION_SLOWDOWN.load(Ordering::SeqCst);
- if slowdown == 0. {
- delta = Duration::ZERO
- } else {
- delta = delta.div_f64(slowdown);
- }
-
self.progress += delta.as_secs_f32() * PI;
if self.progress >= PI * 2. {
diff --git a/niri-visual-tests/src/cases/layout.rs b/niri-visual-tests/src/cases/layout.rs
index f979debb..0dbe004d 100644
--- a/niri-visual-tests/src/cases/layout.rs
+++ b/niri-visual-tests/src/cases/layout.rs
@@ -69,7 +69,7 @@ impl Layout {
let mut layout = niri::layout::Layout::with_options(clock.clone(), options);
layout.add_output(output.clone());
- let start_time = clock.now();
+ let start_time = clock.now_unadjusted();
Self {
output,
@@ -207,24 +207,25 @@ impl TestCase for Layout {
self.layout.are_animations_ongoing(Some(&self.output)) || !self.steps.is_empty()
}
- fn advance_animations(&mut self, current_time: Duration) {
+ fn advance_animations(&mut self, _current_time: Duration) {
+ let now_unadjusted = self.clock.now_unadjusted();
let run = self
.steps
.keys()
.copied()
- .filter(|delay| self.start_time + *delay <= current_time)
+ .filter(|delay| self.start_time + *delay <= now_unadjusted)
.collect::<Vec<_>>();
for delay in &run {
let now = self.start_time + *delay;
- self.clock.set_time_override(Some(now));
- self.layout.advance_animations(now);
+ self.clock.set_unadjusted(now);
+ self.layout.advance_animations();
let f = self.steps.remove(delay).unwrap();
f(self);
}
- self.clock.set_time_override(None);
- self.layout.advance_animations(current_time);
+ self.clock.set_unadjusted(now_unadjusted);
+ self.layout.advance_animations();
}
fn render(
diff --git a/niri-visual-tests/src/cases/tile.rs b/niri-visual-tests/src/cases/tile.rs
index e2637698..f50edf37 100644
--- a/niri-visual-tests/src/cases/tile.rs
+++ b/niri-visual-tests/src/cases/tile.rs
@@ -90,8 +90,8 @@ impl TestCase for Tile {
self.tile.are_animations_ongoing()
}
- fn advance_animations(&mut self, current_time: Duration) {
- self.tile.advance_animations(current_time);
+ fn advance_animations(&mut self, _current_time: Duration) {
+ self.tile.advance_animations();
}
fn render(
diff --git a/niri-visual-tests/src/main.rs b/niri-visual-tests/src/main.rs
index fa035c75..59563651 100644
--- a/niri-visual-tests/src/main.rs
+++ b/niri-visual-tests/src/main.rs
@@ -2,15 +2,11 @@
extern crate tracing;
use std::env;
-use std::sync::atomic::Ordering;
use adw::prelude::{AdwApplicationWindowExt, NavigationPageExt};
use cases::Args;
-use gtk::prelude::{
- AdjustmentExt, ApplicationExt, ApplicationExtManual, BoxExt, GtkWindowExt, WidgetExt,
-};
+use gtk::prelude::{ApplicationExt, ApplicationExtManual, BoxExt, GtkWindowExt, WidgetExt};
use gtk::{gdk, gio, glib};
-use niri::animation::ANIMATION_SLOWDOWN;
use smithay_view::SmithayView;
use tracing_subscriber::EnvFilter;
@@ -66,20 +62,23 @@ fn on_startup(_app: &adw::Application) {
fn build_ui(app: &adw::Application) {
let stack = gtk::Stack::new();
+ let anim_adjustment = gtk::Adjustment::new(1., 0., 10., 0.1, 0.5, 0.);
struct S {
stack: gtk::Stack,
+ anim_adjustment: gtk::Adjustment,
}
impl S {
fn add<T: TestCase + 'static>(&self, make: impl Fn(Args) -> T + 'static, title: &str) {
- let view = SmithayView::new(make);
+ let view = SmithayView::new(make, &self.anim_adjustment);
self.stack.add_titled(&view, None, title);
}
}
let s = S {
stack: stack.clone(),
+ anim_adjustment: anim_adjustment.clone(),
};
s.add(Window::freeform, "Freeform Window");
@@ -133,9 +132,6 @@ fn build_ui(app: &adw::Application) {
let content_headerbar = adw::HeaderBar::new();
- let anim_adjustment = gtk::Adjustment::new(1., 0., 10., 0.1, 0.5, 0.);
- anim_adjustment
- .connect_value_changed(|adj| ANIMATION_SLOWDOWN.store(adj.value(), Ordering::SeqCst));
let anim_scale = gtk::Scale::new(gtk::Orientation::Horizontal, Some(&anim_adjustment));
anim_scale.set_hexpand(true);
diff --git a/niri-visual-tests/src/smithay_view.rs b/niri-visual-tests/src/smithay_view.rs
index 68c5d67d..416729f0 100644
--- a/niri-visual-tests/src/smithay_view.rs
+++ b/niri-visual-tests/src/smithay_view.rs
@@ -1,4 +1,5 @@
use gtk::glib;
+use gtk::prelude::*;
use gtk::subclass::prelude::*;
use smithay::utils::Size;
@@ -7,13 +8,13 @@ use crate::cases::{Args, TestCase};
mod imp {
use std::cell::{Cell, OnceCell, RefCell};
use std::ptr::null;
+ use std::time::Duration;
use anyhow::{ensure, Context};
use gtk::gdk;
use gtk::prelude::*;
use niri::animation::Clock;
use niri::render_helpers::{resources, shaders};
- use niri::utils::get_monotonic_time;
use smithay::backend::egl::ffi::egl;
use smithay::backend::egl::EGLContext;
use smithay::backend::renderer::gles::GlesRenderer;
@@ -31,7 +32,7 @@ mod imp {
renderer: RefCell<Option<Result<GlesRenderer, ()>>>,
pub make_test_case: OnceCell<DynMakeTestCase>,
test_case: RefCell<Option<Box<dyn TestCase>>>,
- clock: RefCell<Clock>,
+ pub clock: RefCell<Clock>,
}
#[glib::object_subclass]
@@ -127,6 +128,10 @@ mod imp {
let size = self.size.get();
+ let frame_clock = self.obj().frame_clock().unwrap();
+ let time = Duration::from_micros(frame_clock.frame_time() as u64);
+ self.clock.borrow_mut().set_unadjusted(time);
+
// Create the test case if missing.
let mut case = self.test_case.borrow_mut();
let case = case.get_or_insert_with(|| {
@@ -138,7 +143,7 @@ mod imp {
make(args)
});
- case.advance_animations(get_monotonic_time());
+ case.advance_animations(self.clock.borrow_mut().now());
let rect: Rectangle<i32, Physical> = Rectangle::from_loc_and_size((0, 0), size);
@@ -238,13 +243,33 @@ glib::wrapper! {
}
impl SmithayView {
- pub fn new<T: TestCase + 'static>(make_test_case: impl Fn(Args) -> T + 'static) -> Self {
+ pub fn new<T: TestCase + 'static>(
+ make_test_case: impl Fn(Args) -> T + 'static,
+ anim_adjustment: &gtk::Adjustment,
+ ) -> Self {
let obj: Self = glib::Object::builder().build();
let make = move |args| Box::new(make_test_case(args)) as Box<dyn TestCase>;
let make_test_case = Box::new(make) as _;
let _ = obj.imp().make_test_case.set(make_test_case);
+ anim_adjustment.connect_value_changed({
+ let obj = obj.downgrade();
+ move |adj| {
+ if let Some(obj) = obj.upgrade() {
+ let mut clock = obj.imp().clock.borrow_mut();
+ let instantly = adj.value() == 0.0;
+ let rate = if instantly {
+ 1.0
+ } else {
+ 1.0 / adj.value().max(0.001)
+ };
+ clock.set_rate(rate);
+ clock.set_complete_instantly(instantly);
+ }
+ }
+ });
+
obj
}
}