forge
Explore
Log in

Don't you dare go Hollow

Refactor animations to take explicit current time

Browse files at this commit

93cee2994ab9ccf59a09f61d5b8acf6cd937d654Ivan MolodetskikhParent: 9c7e8d0
niri-visual-tests/src/cases/gradient_angle.rsmodified+3 -3
@@ -7,9 +7,9 @@ use niri::render_helpers::border::BorderRenderElement;
77 use niri_config::{Color, CornerRadius, GradientInterpolation};
88 use smithay::backend::renderer::element::RenderElement;
99 use smithay::backend::renderer::gles::GlesRenderer;
10-use smithay::utils::{Logical, Physical, Rectangle, Size};
10+use smithay::utils::{Physical, Rectangle, Size};
1111
12-use super::TestCase;
12+use super::{Args, TestCase};
1313
1414 pub struct GradientAngle {
1515 angle: f32,
@@ -17,7 +17,7 @@ pub struct GradientAngle {
1717 }
1818
1919 impl GradientAngle {
20- pub fn new(_size: Size<i32, Logical>) -> Self {
20+ pub fn new(_args: Args) -> Self {
2121 Self {
2222 angle: 0.,
2323 prev_time: Duration::ZERO,
niri-visual-tests/src/cases/gradient_area.rsmodified+3 -3
@@ -8,9 +8,9 @@ use niri::render_helpers::border::BorderRenderElement;
88 use niri_config::{Color, CornerRadius, FloatOrInt, GradientInterpolation};
99 use smithay::backend::renderer::element::RenderElement;
1010 use smithay::backend::renderer::gles::GlesRenderer;
11-use smithay::utils::{Logical, Physical, Point, Rectangle, Size};
11+use smithay::utils::{Physical, Point, Rectangle, Size};
1212
13-use super::TestCase;
13+use super::{Args, TestCase};
1414
1515 pub struct GradientArea {
1616 progress: f32,
@@ -19,7 +19,7 @@ pub struct GradientArea {
1919 }
2020
2121 impl GradientArea {
22- pub fn new(_size: Size<i32, Logical>) -> Self {
22+ pub fn new(_args: Args) -> Self {
2323 let border = FocusRing::new(niri_config::FocusRing {
2424 off: false,
2525 width: FloatOrInt(1.),
niri-visual-tests/src/cases/gradient_oklab.rsmodified+3 -3
@@ -4,16 +4,16 @@ use niri_config::{
44 };
55 use smithay::backend::renderer::element::RenderElement;
66 use smithay::backend::renderer::gles::GlesRenderer;
7-use smithay::utils::{Logical, Physical, Rectangle, Size};
7+use smithay::utils::{Physical, Rectangle, Size};
88
9-use super::TestCase;
9+use super::{Args, TestCase};
1010
1111 pub struct GradientOklab {
1212 gradient_format: GradientInterpolation,
1313 }
1414
1515 impl GradientOklab {
16- pub fn new(_size: Size<i32, Logical>) -> Self {
16+ pub fn new(_args: Args) -> Self {
1717 Self {
1818 gradient_format: GradientInterpolation {
1919 color_space: GradientColorSpace::Oklab,
niri-visual-tests/src/cases/gradient_oklab_alpha.rsmodified+3 -3
@@ -2,16 +2,16 @@ use niri::render_helpers::border::BorderRenderElement;
22 use niri_config::{Color, CornerRadius, GradientColorSpace, GradientInterpolation};
33 use smithay::backend::renderer::element::RenderElement;
44 use smithay::backend::renderer::gles::GlesRenderer;
5-use smithay::utils::{Logical, Physical, Rectangle, Size};
5+use smithay::utils::{Physical, Rectangle, Size};
66
7-use super::TestCase;
7+use super::{Args, TestCase};
88
99 pub struct GradientOklabAlpha {
1010 gradient_format: GradientInterpolation,
1111 }
1212
1313 impl GradientOklabAlpha {
14- pub fn new(_size: Size<i32, Logical>) -> Self {
14+ pub fn new(_args: Args) -> Self {
1515 Self {
1616 gradient_format: GradientInterpolation {
1717 color_space: GradientColorSpace::Oklab,
niri-visual-tests/src/cases/gradient_oklch_alpha.rsmodified+3 -3
@@ -4,16 +4,16 @@ use niri_config::{
44 };
55 use smithay::backend::renderer::element::RenderElement;
66 use smithay::backend::renderer::gles::GlesRenderer;
7-use smithay::utils::{Logical, Physical, Rectangle, Size};
7+use smithay::utils::{Physical, Rectangle, Size};
88
9-use super::TestCase;
9+use super::{Args, TestCase};
1010
1111 pub struct GradientOklchAlpha {
1212 gradient_format: GradientInterpolation,
1313 }
1414
1515 impl GradientOklchAlpha {
16- pub fn new(_size: Size<i32, Logical>) -> Self {
16+ pub fn new(_args: Args) -> Self {
1717 Self {
1818 gradient_format: GradientInterpolation {
1919 color_space: GradientColorSpace::Oklch,
niri-visual-tests/src/cases/gradient_oklch_decreasing.rsmodified+3 -3
@@ -4,16 +4,16 @@ use niri_config::{
44 };
55 use smithay::backend::renderer::element::RenderElement;
66 use smithay::backend::renderer::gles::GlesRenderer;
7-use smithay::utils::{Logical, Physical, Rectangle, Size};
7+use smithay::utils::{Physical, Rectangle, Size};
88
9-use super::TestCase;
9+use super::{Args, TestCase};
1010
1111 pub struct GradientOklchDecreasing {
1212 gradient_format: GradientInterpolation,
1313 }
1414
1515 impl GradientOklchDecreasing {
16- pub fn new(_size: Size<i32, Logical>) -> Self {
16+ pub fn new(_args: Args) -> Self {
1717 Self {
1818 gradient_format: GradientInterpolation {
1919 color_space: GradientColorSpace::Oklch,
niri-visual-tests/src/cases/gradient_oklch_increasing.rsmodified+3 -3
@@ -4,16 +4,16 @@ use niri_config::{
44 };
55 use smithay::backend::renderer::element::RenderElement;
66 use smithay::backend::renderer::gles::GlesRenderer;
7-use smithay::utils::{Logical, Physical, Rectangle, Size};
7+use smithay::utils::{Physical, Rectangle, Size};
88
9-use super::TestCase;
9+use super::{Args, TestCase};
1010
1111 pub struct GradientOklchIncreasing {
1212 gradient_format: GradientInterpolation,
1313 }
1414
1515 impl GradientOklchIncreasing {
16- pub fn new(_size: Size<i32, Logical>) -> Self {
16+ pub fn new(_args: Args) -> Self {
1717 Self {
1818 gradient_format: GradientInterpolation {
1919 color_space: GradientColorSpace::Oklch,
niri-visual-tests/src/cases/gradient_oklch_longer.rsmodified+3 -3
@@ -4,16 +4,16 @@ use niri_config::{
44 };
55 use smithay::backend::renderer::element::RenderElement;
66 use smithay::backend::renderer::gles::GlesRenderer;
7-use smithay::utils::{Logical, Physical, Rectangle, Size};
7+use smithay::utils::{Physical, Rectangle, Size};
88
9-use super::TestCase;
9+use super::{Args, TestCase};
1010
1111 pub struct GradientOklchLonger {
1212 gradient_format: GradientInterpolation,
1313 }
1414
1515 impl GradientOklchLonger {
16- pub fn new(_size: Size<i32, Logical>) -> Self {
16+ pub fn new(_args: Args) -> Self {
1717 Self {
1818 gradient_format: GradientInterpolation {
1919 color_space: GradientColorSpace::Oklch,
niri-visual-tests/src/cases/gradient_oklch_shorter.rsmodified+3 -3
@@ -4,16 +4,16 @@ use niri_config::{
44 };
55 use smithay::backend::renderer::element::RenderElement;
66 use smithay::backend::renderer::gles::GlesRenderer;
7-use smithay::utils::{Logical, Physical, Rectangle, Size};
7+use smithay::utils::{Physical, Rectangle, Size};
88
9-use super::TestCase;
9+use super::{Args, TestCase};
1010
1111 pub struct GradientOklchShorter {
1212 gradient_format: GradientInterpolation,
1313 }
1414
1515 impl GradientOklchShorter {
16- pub fn new(_size: Size<i32, Logical>) -> Self {
16+ pub fn new(_args: Args) -> Self {
1717 Self {
1818 gradient_format: GradientInterpolation {
1919 color_space: GradientColorSpace::Oklch,
niri-visual-tests/src/cases/gradient_srgb.rsmodified+3 -3
@@ -4,16 +4,16 @@ use niri_config::{
44 };
55 use smithay::backend::renderer::element::RenderElement;
66 use smithay::backend::renderer::gles::GlesRenderer;
7-use smithay::utils::{Logical, Physical, Rectangle, Size};
7+use smithay::utils::{Physical, Rectangle, Size};
88
9-use super::TestCase;
9+use super::{Args, TestCase};
1010
1111 pub struct GradientSrgb {
1212 gradient_format: GradientInterpolation,
1313 }
1414
1515 impl GradientSrgb {
16- pub fn new(_size: Size<i32, Logical>) -> Self {
16+ pub fn new(_args: Args) -> Self {
1717 Self {
1818 gradient_format: GradientInterpolation {
1919 color_space: GradientColorSpace::Srgb,
niri-visual-tests/src/cases/gradient_srgb_alpha.rsmodified+3 -3
@@ -2,16 +2,16 @@ use niri::render_helpers::border::BorderRenderElement;
22 use niri_config::{Color, CornerRadius, GradientColorSpace, GradientInterpolation};
33 use smithay::backend::renderer::element::RenderElement;
44 use smithay::backend::renderer::gles::GlesRenderer;
5-use smithay::utils::{Logical, Physical, Rectangle, Size};
5+use smithay::utils::{Physical, Rectangle, Size};
66
7-use super::TestCase;
7+use super::{Args, TestCase};
88
99 pub struct GradientSrgbAlpha {
1010 gradient_format: GradientInterpolation,
1111 }
1212
1313 impl GradientSrgbAlpha {
14- pub fn new(_size: Size<i32, Logical>) -> Self {
14+ pub fn new(_args: Args) -> Self {
1515 Self {
1616 gradient_format: GradientInterpolation {
1717 color_space: GradientColorSpace::Srgb,
niri-visual-tests/src/cases/gradient_srgblinear.rsmodified+3 -3
@@ -4,16 +4,16 @@ use niri_config::{
44 };
55 use smithay::backend::renderer::element::RenderElement;
66 use smithay::backend::renderer::gles::GlesRenderer;
7-use smithay::utils::{Logical, Physical, Rectangle, Size};
7+use smithay::utils::{Physical, Rectangle, Size};
88
9-use super::TestCase;
9+use super::{Args, TestCase};
1010
1111 pub struct GradientSrgbLinear {
1212 gradient_format: GradientInterpolation,
1313 }
1414
1515 impl GradientSrgbLinear {
16- pub fn new(_size: Size<i32, Logical>) -> Self {
16+ pub fn new(_args: Args) -> Self {
1717 Self {
1818 gradient_format: GradientInterpolation {
1919 color_space: GradientColorSpace::SrgbLinear,
niri-visual-tests/src/cases/gradient_srgblinear_alpha.rsmodified+3 -3
@@ -2,16 +2,16 @@ use niri::render_helpers::border::BorderRenderElement;
22 use niri_config::{Color, CornerRadius, GradientColorSpace, GradientInterpolation};
33 use smithay::backend::renderer::element::RenderElement;
44 use smithay::backend::renderer::gles::GlesRenderer;
5-use smithay::utils::{Logical, Physical, Rectangle, Size};
5+use smithay::utils::{Physical, Rectangle, Size};
66
7-use super::TestCase;
7+use super::{Args, TestCase};
88
99 pub struct GradientSrgbLinearAlpha {
1010 gradient_format: GradientInterpolation,
1111 }
1212
1313 impl GradientSrgbLinearAlpha {
14- pub fn new(_size: Size<i32, Logical>) -> Self {
14+ pub fn new(_args: Args) -> Self {
1515 Self {
1616 gradient_format: GradientInterpolation {
1717 color_space: GradientColorSpace::SrgbLinear,
niri-visual-tests/src/cases/layout.rsmodified+30 -22
@@ -1,18 +1,18 @@
11 use std::collections::HashMap;
22 use std::time::Duration;
33
4+use niri::animation::Clock;
45 use niri::layout::workspace::ColumnWidth;
56 use niri::layout::{LayoutElement as _, Options};
67 use niri::render_helpers::RenderTarget;
7-use niri::utils::get_monotonic_time;
88 use niri_config::{Color, FloatOrInt, OutputName};
99 use smithay::backend::renderer::element::RenderElement;
1010 use smithay::backend::renderer::gles::GlesRenderer;
1111 use smithay::desktop::layer_map_for_output;
1212 use smithay::output::{Mode, Output, PhysicalProperties, Subpixel};
13-use smithay::utils::{Logical, Physical, Size};
13+use smithay::utils::{Physical, Size};
1414
15-use super::TestCase;
15+use super::{Args, TestCase};
1616 use crate::test_window::TestWindow;
1717
1818 type DynStepFn = Box<dyn FnOnce(&mut Layout)>;
@@ -20,13 +20,16 @@ type DynStepFn = Box<dyn FnOnce(&mut Layout)>;
2020 pub struct Layout {
2121 output: Output,
2222 windows: Vec<TestWindow>,
23+ clock: Clock,
2324 layout: niri::layout::Layout<TestWindow>,
2425 start_time: Duration,
2526 steps: HashMap<Duration, DynStepFn>,
2627 }
2728
2829 impl Layout {
29- pub fn new(size: Size<i32, Logical>) -> Self {
30+ pub fn new(args: Args) -> Self {
31+ let Args { size, clock } = args;
32+
3033 let output = Output::new(
3134 String::new(),
3235 PhysicalProperties {
@@ -63,20 +66,23 @@ impl Layout {
6366 },
6467 ..Default::default()
6568 };
66- let mut layout = niri::layout::Layout::with_options(options);
69+ let mut layout = niri::layout::Layout::with_options(clock.clone(), options);
6770 layout.add_output(output.clone());
6871
72+ let start_time = clock.now();
73+
6974 Self {
7075 output,
7176 windows: Vec::new(),
77+ clock,
7278 layout,
73- start_time: get_monotonic_time(),
79+ start_time,
7480 steps: HashMap::new(),
7581 }
7682 }
7783
78- pub fn open_in_between(size: Size<i32, Logical>) -> Self {
79- let mut rv = Self::new(size);
84+ pub fn open_in_between(args: Args) -> Self {
85+ let mut rv = Self::new(args);
8086
8187 rv.add_window(TestWindow::freeform(0), Some(ColumnWidth::Proportion(0.3)));
8288 rv.add_window(TestWindow::freeform(1), Some(ColumnWidth::Proportion(0.3)));
@@ -91,8 +97,8 @@ impl Layout {
9197 rv
9298 }
9399
94- pub fn open_multiple_quickly(size: Size<i32, Logical>) -> Self {
95- let mut rv = Self::new(size);
100+ pub fn open_multiple_quickly(args: Args) -> Self {
101+ let mut rv = Self::new(args);
96102
97103 for delay in [100, 200, 300] {
98104 rv.add_step(delay, move |l| {
@@ -105,8 +111,8 @@ impl Layout {
105111 rv
106112 }
107113
108- pub fn open_multiple_quickly_big(size: Size<i32, Logical>) -> Self {
109- let mut rv = Self::new(size);
114+ pub fn open_multiple_quickly_big(args: Args) -> Self {
115+ let mut rv = Self::new(args);
110116
111117 for delay in [100, 200, 300] {
112118 rv.add_step(delay, move |l| {
@@ -119,8 +125,8 @@ impl Layout {
119125 rv
120126 }
121127
122- pub fn open_to_the_left(size: Size<i32, Logical>) -> Self {
123- let mut rv = Self::new(size);
128+ pub fn open_to_the_left(args: Args) -> Self {
129+ let mut rv = Self::new(args);
124130
125131 rv.add_window(TestWindow::freeform(0), Some(ColumnWidth::Proportion(0.3)));
126132 rv.add_window(TestWindow::freeform(1), Some(ColumnWidth::Proportion(0.3)));
@@ -135,8 +141,8 @@ impl Layout {
135141 rv
136142 }
137143
138- pub fn open_to_the_left_big(size: Size<i32, Logical>) -> Self {
139- let mut rv = Self::new(size);
144+ pub fn open_to_the_left_big(args: Args) -> Self {
145+ let mut rv = Self::new(args);
140146
141147 rv.add_window(TestWindow::freeform(0), Some(ColumnWidth::Proportion(0.3)));
142148 rv.add_window(TestWindow::freeform(1), Some(ColumnWidth::Proportion(0.8)));
@@ -201,21 +207,23 @@ impl TestCase for Layout {
201207 self.layout.are_animations_ongoing(Some(&self.output)) || !self.steps.is_empty()
202208 }
203209
204- fn advance_animations(&mut self, mut current_time: Duration) {
210+ fn advance_animations(&mut self, current_time: Duration) {
205211 let run = self
206212 .steps
207213 .keys()
208214 .copied()
209215 .filter(|delay| self.start_time + *delay <= current_time)
210216 .collect::<Vec<_>>();
211- for key in &run {
212- let f = self.steps.remove(key).unwrap();
217+ for delay in &run {
218+ let now = self.start_time + *delay;
219+ self.clock.set_time_override(Some(now));
220+ self.layout.advance_animations(now);
221+
222+ let f = self.steps.remove(delay).unwrap();
213223 f(self);
214224 }
215- if !run.is_empty() {
216- current_time = get_monotonic_time();
217- }
218225
226+ self.clock.set_time_override(None);
219227 self.layout.advance_animations(current_time);
220228 }
221229
niri-visual-tests/src/cases/mod.rsmodified+7 -1
@@ -1,8 +1,9 @@
11 use std::time::Duration;
22
3+use niri::animation::Clock;
34 use smithay::backend::renderer::element::RenderElement;
45 use smithay::backend::renderer::gles::GlesRenderer;
5-use smithay::utils::{Physical, Size};
6+use smithay::utils::{Logical, Physical, Size};
67
78 pub mod gradient_angle;
89 pub mod gradient_area;
@@ -21,6 +22,11 @@ pub mod layout;
2122 pub mod tile;
2223 pub mod window;
2324
25+pub struct Args {
26+ pub size: Size<i32, Logical>,
27+ pub clock: Clock,
28+}
29+
2430 pub trait TestCase {
2531 fn resize(&mut self, _width: i32, _height: i32) {}
2632 fn are_animations_ongoing(&self) -> bool {
niri-visual-tests/src/cases/tile.rsmodified+22 -25
@@ -6,9 +6,9 @@ use niri::render_helpers::RenderTarget;
66 use niri_config::{Color, FloatOrInt};
77 use smithay::backend::renderer::element::RenderElement;
88 use smithay::backend::renderer::gles::GlesRenderer;
9-use smithay::utils::{Logical, Physical, Point, Rectangle, Scale, Size};
9+use smithay::utils::{Physical, Point, Rectangle, Scale, Size};
1010
11-use super::TestCase;
11+use super::{Args, TestCase};
1212 use crate::test_window::TestWindow;
1313
1414 pub struct Tile {
@@ -17,53 +17,44 @@ pub struct Tile {
1717 }
1818
1919 impl Tile {
20- pub fn freeform(size: Size<i32, Logical>) -> Self {
20+ pub fn freeform(args: Args) -> Self {
2121 let window = TestWindow::freeform(0);
22- let mut rv = Self::with_window(window);
23- rv.tile.request_tile_size(size.to_f64(), false, None);
24- rv.window.communicate();
25- rv
22+ Self::with_window(args, window)
2623 }
2724
28- pub fn fixed_size(size: Size<i32, Logical>) -> Self {
25+ pub fn fixed_size(args: Args) -> Self {
2926 let window = TestWindow::fixed_size(0);
30- let mut rv = Self::with_window(window);
31- rv.tile.request_tile_size(size.to_f64(), false, None);
32- rv.window.communicate();
33- rv
27+ Self::with_window(args, window)
3428 }
3529
36- pub fn fixed_size_with_csd_shadow(size: Size<i32, Logical>) -> Self {
30+ pub fn fixed_size_with_csd_shadow(args: Args) -> Self {
3731 let window = TestWindow::fixed_size(0);
3832 window.set_csd_shadow_width(64);
39- let mut rv = Self::with_window(window);
40- rv.tile.request_tile_size(size.to_f64(), false, None);
41- rv.window.communicate();
42- rv
33+ Self::with_window(args, window)
4334 }
4435
45- pub fn freeform_open(size: Size<i32, Logical>) -> Self {
46- let mut rv = Self::freeform(size);
36+ pub fn freeform_open(args: Args) -> Self {
37+ let mut rv = Self::freeform(args);
4738 rv.window.set_color([0.1, 0.1, 0.1, 1.]);
4839 rv.tile.start_open_animation();
4940 rv
5041 }
5142
52- pub fn fixed_size_open(size: Size<i32, Logical>) -> Self {
53- let mut rv = Self::fixed_size(size);
43+ pub fn fixed_size_open(args: Args) -> Self {
44+ let mut rv = Self::fixed_size(args);
5445 rv.window.set_color([0.1, 0.1, 0.1, 1.]);
5546 rv.tile.start_open_animation();
5647 rv
5748 }
5849
59- pub fn fixed_size_with_csd_shadow_open(size: Size<i32, Logical>) -> Self {
60- let mut rv = Self::fixed_size_with_csd_shadow(size);
50+ pub fn fixed_size_with_csd_shadow_open(args: Args) -> Self {
51+ let mut rv = Self::fixed_size_with_csd_shadow(args);
6152 rv.window.set_color([0.1, 0.1, 0.1, 1.]);
6253 rv.tile.start_open_animation();
6354 rv
6455 }
6556
66- pub fn with_window(window: TestWindow) -> Self {
57+ pub fn with_window(args: Args, window: TestWindow) -> Self {
6758 let options = Options {
6859 focus_ring: niri_config::FocusRing {
6960 off: true,
@@ -77,7 +68,13 @@ impl Tile {
7768 },
7869 ..Default::default()
7970 };
80- let tile = niri::layout::tile::Tile::new(window.clone(), 1., Rc::new(options));
71+
72+ let mut tile =
73+ niri::layout::tile::Tile::new(window.clone(), 1., args.clock, Rc::new(options));
74+
75+ tile.request_tile_size(args.size.to_f64(), false, None);
76+ window.communicate();
77+
8178 Self { window, tile }
8279 }
8380 }
niri-visual-tests/src/cases/window.rsmodified+8 -8
@@ -2,9 +2,9 @@ use niri::layout::LayoutElement;
22 use niri::render_helpers::RenderTarget;
33 use smithay::backend::renderer::element::RenderElement;
44 use smithay::backend::renderer::gles::GlesRenderer;
5-use smithay::utils::{Logical, Physical, Point, Scale, Size};
5+use smithay::utils::{Physical, Point, Scale, Size};
66
7-use super::TestCase;
7+use super::{Args, TestCase};
88 use crate::test_window::TestWindow;
99
1010 pub struct Window {
@@ -12,24 +12,24 @@ pub struct Window {
1212 }
1313
1414 impl Window {
15- pub fn freeform(size: Size<i32, Logical>) -> Self {
15+ pub fn freeform(args: Args) -> Self {
1616 let mut window = TestWindow::freeform(0);
17- window.request_size(size, false, None);
17+ window.request_size(args.size, false, None);
1818 window.communicate();
1919 Self { window }
2020 }
2121
22- pub fn fixed_size(size: Size<i32, Logical>) -> Self {
22+ pub fn fixed_size(args: Args) -> Self {
2323 let mut window = TestWindow::fixed_size(0);
24- window.request_size(size, false, None);
24+ window.request_size(args.size, false, None);
2525 window.communicate();
2626 Self { window }
2727 }
2828
29- pub fn fixed_size_with_csd_shadow(size: Size<i32, Logical>) -> Self {
29+ pub fn fixed_size_with_csd_shadow(args: Args) -> Self {
3030 let mut window = TestWindow::fixed_size(0);
3131 window.set_csd_shadow_width(64);
32- window.request_size(size, false, None);
32+ window.request_size(args.size, false, None);
3333 window.communicate();
3434 Self { window }
3535 }
niri-visual-tests/src/main.rsmodified+2 -6
@@ -5,12 +5,12 @@ use std::env;
55 use std::sync::atomic::Ordering;
66
77 use adw::prelude::{AdwApplicationWindowExt, NavigationPageExt};
8+use cases::Args;
89 use gtk::prelude::{
910 AdjustmentExt, ApplicationExt, ApplicationExtManual, BoxExt, GtkWindowExt, WidgetExt,
1011 };
1112 use gtk::{gdk, gio, glib};
1213 use niri::animation::ANIMATION_SLOWDOWN;
13-use smithay::utils::{Logical, Size};
1414 use smithay_view::SmithayView;
1515 use tracing_subscriber::EnvFilter;
1616
@@ -72,11 +72,7 @@ fn build_ui(app: &adw::Application) {
7272 }
7373
7474 impl S {
75- fn add<T: TestCase + 'static>(
76- &self,
77- make: impl Fn(Size<i32, Logical>) -> T + 'static,
78- title: &str,
79- ) {
75+ fn add<T: TestCase + 'static>(&self, make: impl Fn(Args) -> T + 'static, title: &str) {
8076 let view = SmithayView::new(make);
8177 self.stack.add_titled(&view, None, title);
8278 }
niri-visual-tests/src/smithay_view.rsmodified+12 -8
@@ -1,8 +1,8 @@
11 use gtk::glib;
22 use gtk::subclass::prelude::*;
3-use smithay::utils::{Logical, Size};
3+use smithay::utils::Size;
44
5-use crate::cases::TestCase;
5+use crate::cases::{Args, TestCase};
66
77 mod imp {
88 use std::cell::{Cell, OnceCell, RefCell};
@@ -11,6 +11,7 @@ mod imp {
1111 use anyhow::{ensure, Context};
1212 use gtk::gdk;
1313 use gtk::prelude::*;
14+ use niri::animation::Clock;
1415 use niri::render_helpers::{resources, shaders};
1516 use niri::utils::get_monotonic_time;
1617 use smithay::backend::egl::ffi::egl;
@@ -21,7 +22,7 @@ mod imp {
2122
2223 use super::*;
2324
24- type DynMakeTestCase = Box<dyn Fn(Size<i32, Logical>) -> Box<dyn TestCase>>;
25+ type DynMakeTestCase = Box<dyn Fn(Args) -> Box<dyn TestCase>>;
2526
2627 #[derive(Default)]
2728 pub struct SmithayView {
@@ -30,6 +31,7 @@ mod imp {
3031 renderer: RefCell<Option<Result<GlesRenderer, ()>>>,
3132 pub make_test_case: OnceCell<DynMakeTestCase>,
3233 test_case: RefCell<Option<Box<dyn TestCase>>>,
34+ clock: RefCell<Clock>,
3335 }
3436
3537 #[glib::object_subclass]
@@ -129,7 +131,11 @@ mod imp {
129131 let mut case = self.test_case.borrow_mut();
130132 let case = case.get_or_insert_with(|| {
131133 let make = self.make_test_case.get().unwrap();
132- make(Size::from(size))
134+ let args = Args {
135+ size: Size::from(size),
136+ clock: self.clock.borrow().clone(),
137+ };
138+ make(args)
133139 });
134140
135141 case.advance_animations(get_monotonic_time());
@@ -232,12 +238,10 @@ glib::wrapper! {
232238 }
233239
234240 impl SmithayView {
235- pub fn new<T: TestCase + 'static>(
236- make_test_case: impl Fn(Size<i32, Logical>) -> T + 'static,
237- ) -> Self {
241+ pub fn new<T: TestCase + 'static>(make_test_case: impl Fn(Args) -> T + 'static) -> Self {
238242 let obj: Self = glib::Object::builder().build();
239243
240- let make = move |size| Box::new(make_test_case(size)) as Box<dyn TestCase>;
244+ let make = move |args| Box::new(make_test_case(args)) as Box<dyn TestCase>;
241245 let make_test_case = Box::new(make) as _;
242246 let _ = obj.imp().make_test_case.set(make_test_case);
243247
src/animation/clock.rsadded+41 -0
@@ -0,0 +1,41 @@
1+use std::cell::Cell;
2+use std::rc::Rc;
3+use std::time::Duration;
4+
5+use crate::utils::get_monotonic_time;
6+
7+/// Clock that can have its time value overridden.
8+///
9+/// Can be cloned to share the same clock.
10+#[derive(Debug, Default, Clone)]
11+pub struct Clock {
12+ time_override: Rc<Cell<Option<Duration>>>,
13+}
14+
15+impl Clock {
16+ /// Creates a new [`Clock`] with time override in place.
17+ pub fn with_override(time: Duration) -> Self {
18+ Self {
19+ time_override: Rc::new(Cell::new(Some(time))),
20+ }
21+ }
22+
23+ /// Sets the current time override.
24+ pub fn set_time_override(&mut self, time: Option<Duration>) {
25+ self.time_override.set(time);
26+ }
27+
28+ /// Gets the current time.
29+ #[inline]
30+ pub fn now(&self) -> Duration {
31+ self.time_override.get().unwrap_or_else(get_monotonic_time)
32+ }
33+}
34+
35+impl PartialEq for Clock {
36+ fn eq(&self, other: &Self) -> bool {
37+ Rc::ptr_eq(&self.time_override, &other.time_override)
38+ }
39+}
40+
41+impl Eq for Clock {}
src/animation/mod.rsmodified+52 -28
@@ -4,11 +4,12 @@ use keyframe::functions::{EaseOutCubic, EaseOutQuad};
44 use keyframe::EasingFunction;
55 use portable_atomic::{AtomicF64, Ordering};
66
7-use crate::utils::get_monotonic_time;
8-
97 mod spring;
108 pub use spring::{Spring, SpringParams};
119
10+mod clock;
11+pub use clock::Clock;
12+
1213 pub static ANIMATION_SLOWDOWN: AtomicF64 = AtomicF64::new(1.);
1314
1415 #[derive(Debug, Clone)]
@@ -48,11 +49,24 @@ pub enum Curve {
4849 }
4950
5051 impl Animation {
51- pub fn new(from: f64, to: f64, initial_velocity: f64, config: niri_config::Animation) -> Self {
52+ pub fn new(
53+ current_time: Duration,
54+ from: f64,
55+ to: f64,
56+ initial_velocity: f64,
57+ config: niri_config::Animation,
58+ ) -> Self {
5259 // Scale the velocity by slowdown to keep the touchpad gestures feeling right.
5360 let initial_velocity = initial_velocity * ANIMATION_SLOWDOWN.load(Ordering::Relaxed);
5461
55- let mut rv = Self::ease(from, to, initial_velocity, 0, Curve::EaseOutCubic);
62+ let mut rv = Self::ease(
63+ current_time,
64+ from,
65+ to,
66+ initial_velocity,
67+ 0,
68+ Curve::EaseOutCubic,
69+ );
5670 if config.off {
5771 rv.is_off = true;
5872 return rv;
@@ -83,10 +97,11 @@ impl Animation {
8397 initial_velocity: self.initial_velocity,
8498 params,
8599 };
86- *self = Self::spring(spring);
100+ *self = Self::spring(current_time, spring);
87101 }
88102 niri_config::AnimationKind::Easing(p) => {
89103 *self = Self::ease(
104+ current_time,
90105 self.from,
91106 self.to,
92107 self.initial_velocity,
@@ -101,7 +116,13 @@ impl Animation {
101116 }
102117
103118 /// Restarts the animation using the previous config.
104- pub fn restarted(&self, from: f64, to: f64, initial_velocity: f64) -> Self {
119+ pub fn restarted(
120+ &self,
121+ current_time: Duration,
122+ from: f64,
123+ to: f64,
124+ initial_velocity: f64,
125+ ) -> Self {
105126 if self.is_off {
106127 return self.clone();
107128 }
@@ -111,6 +132,7 @@ impl Animation {
111132
112133 match self.kind {
113134 Kind::Easing { curve } => Self::ease(
135+ current_time,
114136 from,
115137 to,
116138 initial_velocity,
@@ -124,23 +146,32 @@ impl Animation {
124146 initial_velocity: self.initial_velocity,
125147 params: spring.params,
126148 };
127- Self::spring(spring)
149+ Self::spring(current_time, spring)
128150 }
129151 Kind::Deceleration {
130152 initial_velocity,
131153 deceleration_rate,
132154 } => {
133155 let threshold = 0.001; // FIXME
134- Self::decelerate(from, initial_velocity, deceleration_rate, threshold)
156+ Self::decelerate(
157+ current_time,
158+ from,
159+ initial_velocity,
160+ deceleration_rate,
161+ threshold,
162+ )
135163 }
136164 }
137165 }
138166
139- pub fn ease(from: f64, to: f64, initial_velocity: f64, duration_ms: u64, curve: Curve) -> Self {
140- // FIXME: ideally we shouldn't use current time here because animations started within the
141- // same frame cycle should have the same start time to be synchronized.
142- let now = get_monotonic_time();
143-
167+ pub fn ease(
168+ current_time: Duration,
169+ from: f64,
170+ to: f64,
171+ initial_velocity: f64,
172+ duration_ms: u64,
173+ curve: Curve,
174+ ) -> Self {
144175 let duration = Duration::from_millis(duration_ms);
145176 let kind = Kind::Easing { curve };
146177
@@ -152,19 +183,15 @@ impl Animation {
152183 duration,
153184 // Our current curves never overshoot.
154185 clamped_duration: duration,
155- start_time: now,
156- current_time: now,
186+ start_time: current_time,
187+ current_time,
157188 kind,
158189 }
159190 }
160191
161- pub fn spring(spring: Spring) -> Self {
192+ pub fn spring(current_time: Duration, spring: Spring) -> Self {
162193 let _span = tracy_client::span!("Animation::spring");
163194
164- // FIXME: ideally we shouldn't use current time here because animations started within the
165- // same frame cycle should have the same start time to be synchronized.
166- let now = get_monotonic_time();
167-
168195 let duration = spring.duration();
169196 let clamped_duration = spring.clamped_duration().unwrap_or(duration);
170197 let kind = Kind::Spring(spring);
@@ -176,22 +203,19 @@ impl Animation {
176203 is_off: false,
177204 duration,
178205 clamped_duration,
179- start_time: now,
180- current_time: now,
206+ start_time: current_time,
207+ current_time,
181208 kind,
182209 }
183210 }
184211
185212 pub fn decelerate(
213+ current_time: Duration,
186214 from: f64,
187215 initial_velocity: f64,
188216 deceleration_rate: f64,
189217 threshold: f64,
190218 ) -> Self {
191- // FIXME: ideally we shouldn't use current time here because animations started within the
192- // same frame cycle should have the same start time to be synchronized.
193- let now = get_monotonic_time();
194-
195219 let duration_s = if initial_velocity == 0. {
196220 0.
197221 } else {
@@ -214,8 +238,8 @@ impl Animation {
214238 is_off: false,
215239 duration,
216240 clamped_duration: duration,
217- start_time: now,
218- current_time: now,
241+ start_time: current_time,
242+ current_time,
219243 kind,
220244 }
221245 }
src/input/mod.rsmodified+2 -1
@@ -3024,6 +3024,7 @@ pub fn mods_with_finger_scroll_binds(comp_mod: CompositorMod, binds: &Binds) ->
30243024 #[cfg(test)]
30253025 mod tests {
30263026 use super::*;
3027+ use crate::animation::Clock;
30273028
30283029 #[test]
30293030 fn bindings_suppress_keys() {
@@ -3042,7 +3043,7 @@ mod tests {
30423043 let comp_mod = CompositorMod::Super;
30433044 let mut suppressed_keys = HashSet::new();
30443045
3045- let screenshot_ui = ScreenshotUi::new(Default::default());
3046+ let screenshot_ui = ScreenshotUi::new(Clock::default(), Default::default());
30463047 let disable_power_key_handling = false;
30473048
30483049 // The key_code we pick is arbitrary, the only thing
src/layout/closing_window.rsmodified+1 -2
@@ -142,8 +142,7 @@ impl ClosingWindow {
142142 match &mut self.anim_state {
143143 AnimationState::Waiting { blocker, anim } => {
144144 if blocker.state() != BlockerState::Pending {
145- let mut anim = anim.restarted(0., 1., 0.);
146- anim.set_current_time(current_time);
145+ let anim = anim.restarted(current_time, 0., 1., 0.);
147146 self.anim_state = AnimationState::Animating(anim);
148147 }
149148 }
src/layout/mod.rsmodified+104 -16
@@ -52,6 +52,7 @@ use workspace::WorkspaceId;
5252 pub use self::monitor::MonitorRenderElement;
5353 use self::monitor::{Monitor, WorkspaceSwitch};
5454 use self::workspace::{compute_working_area, Column, ColumnWidth, InsertHint, OutputId, Workspace};
55+use crate::animation::Clock;
5556 use crate::layout::workspace::InsertPosition;
5657 use crate::niri_render_elements;
5758 use crate::render_helpers::renderer::NiriRenderer;
@@ -216,6 +217,8 @@ pub struct Layout<W: LayoutElement> {
216217 last_active_workspace_id: HashMap<String, WorkspaceId>,
217218 /// Ongoing interactive move.
218219 interactive_move: Option<InteractiveMoveState<W>>,
220+ /// Clock for driving animations.
221+ clock: Clock,
219222 /// Configurable properties of the layout.
220223 options: Rc<Options>,
221224 }
@@ -433,27 +436,30 @@ impl Options {
433436 }
434437
435438 impl<W: LayoutElement> Layout<W> {
436- pub fn new(config: &Config) -> Self {
437- Self::with_options_and_workspaces(config, Options::from_config(config))
439+ pub fn new(clock: Clock, config: &Config) -> Self {
440+ Self::with_options_and_workspaces(clock, config, Options::from_config(config))
438441 }
439442
440- pub fn with_options(options: Options) -> Self {
443+ pub fn with_options(clock: Clock, options: Options) -> Self {
441444 Self {
442445 monitor_set: MonitorSet::NoOutputs { workspaces: vec![] },
443446 is_active: true,
444447 last_active_workspace_id: HashMap::new(),
445448 interactive_move: None,
449+ clock,
446450 options: Rc::new(options),
447451 }
448452 }
449453
450- fn with_options_and_workspaces(config: &Config, options: Options) -> Self {
454+ fn with_options_and_workspaces(clock: Clock, config: &Config, options: Options) -> Self {
451455 let opts = Rc::new(options);
452456
453457 let workspaces = config
454458 .workspaces
455459 .iter()
456- .map(|ws| Workspace::new_with_config_no_outputs(Some(ws.clone()), opts.clone()))
460+ .map(|ws| {
461+ Workspace::new_with_config_no_outputs(Some(ws.clone()), clock.clone(), opts.clone())
462+ })
457463 .collect();
458464
459465 Self {
@@ -461,6 +467,7 @@ impl<W: LayoutElement> Layout<W> {
461467 is_active: true,
462468 last_active_workspace_id: HashMap::new(),
463469 interactive_move: None,
470+ clock,
464471 options: opts,
465472 }
466473 }
@@ -522,13 +529,18 @@ impl<W: LayoutElement> Layout<W> {
522529 }
523530
524531 // Make sure there's always an empty workspace.
525- workspaces.push(Workspace::new(output.clone(), self.options.clone()));
532+ workspaces.push(Workspace::new(
533+ output.clone(),
534+ self.clock.clone(),
535+ self.options.clone(),
536+ ));
526537
527538 for ws in &mut workspaces {
528539 ws.set_output(Some(output.clone()));
529540 }
530541
531- let mut monitor = Monitor::new(output, workspaces, self.options.clone());
542+ let mut monitor =
543+ Monitor::new(output, workspaces, self.clock.clone(), self.options.clone());
532544 monitor.active_workspace_idx = active_workspace_idx.unwrap_or(0);
533545 monitors.push(monitor);
534546
@@ -540,7 +552,11 @@ impl<W: LayoutElement> Layout<W> {
540552 }
541553 MonitorSet::NoOutputs { mut workspaces } => {
542554 // We know there are no empty workspaces there, so add one.
543- workspaces.push(Workspace::new(output.clone(), self.options.clone()));
555+ workspaces.push(Workspace::new(
556+ output.clone(),
557+ self.clock.clone(),
558+ self.options.clone(),
559+ ));
544560
545561 let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());
546562 let mut active_workspace_idx = 0;
@@ -553,7 +569,8 @@ impl<W: LayoutElement> Layout<W> {
553569 }
554570 }
555571
556- let mut monitor = Monitor::new(output, workspaces, self.options.clone());
572+ let mut monitor =
573+ Monitor::new(output, workspaces, self.clock.clone(), self.options.clone());
557574 monitor.active_workspace_idx = active_workspace_idx;
558575
559576 MonitorSet::Normal {
@@ -785,7 +802,10 @@ impl<W: LayoutElement> Layout<W> {
785802 let ws = if let Some(ws) = workspaces.get_mut(0) {
786803 ws
787804 } else {
788- workspaces.push(Workspace::new_no_outputs(self.options.clone()));
805+ workspaces.push(Workspace::new_no_outputs(
806+ self.clock.clone(),
807+ self.options.clone(),
808+ ));
789809 &mut workspaces[0]
790810 };
791811 ws.add_window(None, window, true, width, is_full_width);
@@ -1992,6 +2012,8 @@ impl<W: LayoutElement> Layout<W> {
19922012 move_win_id = Some(window_id.clone());
19932013 }
19942014 InteractiveMoveState::Moving(move_) => {
2015+ assert_eq!(self.clock, move_.tile.clock);
2016+
19952017 let scale = move_.output.current_scale().fractional_scale();
19962018 let options = Options::clone(&self.options).adjusted_for_scale(scale);
19972019 assert_eq!(
@@ -2026,6 +2048,8 @@ impl<W: LayoutElement> Layout<W> {
20262048 "with no outputs there cannot be empty unnamed workspaces"
20272049 );
20282050
2051+ assert_eq!(self.clock, workspace.clock);
2052+
20292053 assert_eq!(
20302054 workspace.base_options, self.options,
20312055 "workspace base options must be synchronized with layout"
@@ -2070,6 +2094,7 @@ impl<W: LayoutElement> Layout<W> {
20702094 );
20712095 assert!(monitor.active_workspace_idx < monitor.workspaces.len());
20722096
2097+ assert_eq!(self.clock, monitor.clock);
20732098 assert_eq!(
20742099 monitor.options, self.options,
20752100 "monitor options must be synchronized with layout"
@@ -2135,6 +2160,8 @@ impl<W: LayoutElement> Layout<W> {
21352160 // exists.
21362161
21372162 for workspace in &monitor.workspaces {
2163+ assert_eq!(self.clock, workspace.clock);
2164+
21382165 assert_eq!(
21392166 workspace.base_options, self.options,
21402167 "workspace options must be synchronized with layout"
@@ -2326,6 +2353,7 @@ impl<W: LayoutElement> Layout<W> {
23262353 return;
23272354 }
23282355
2356+ let clock = self.clock.clone();
23292357 let options = self.options.clone();
23302358
23312359 match &mut self.monitor_set {
@@ -2349,6 +2377,7 @@ impl<W: LayoutElement> Layout<W> {
23492377 let ws = Workspace::new_with_config(
23502378 mon.output.clone(),
23512379 Some(ws_config.clone()),
2380+ clock,
23522381 options,
23532382 );
23542383 mon.workspaces.insert(0, ws);
@@ -2357,7 +2386,8 @@ impl<W: LayoutElement> Layout<W> {
23572386 mon.clean_up_workspaces();
23582387 }
23592388 MonitorSet::NoOutputs { workspaces } => {
2360- let ws = Workspace::new_with_config_no_outputs(Some(ws_config.clone()), options);
2389+ let ws =
2390+ Workspace::new_with_config_no_outputs(Some(ws_config.clone()), clock, options);
23612391 workspaces.insert(0, ws);
23622392 }
23632393 }
@@ -3161,7 +3191,10 @@ impl<W: LayoutElement> Layout<W> {
31613191 let ws = if let Some(ws) = workspaces.get_mut(0) {
31623192 ws
31633193 } else {
3164- workspaces.push(Workspace::new_no_outputs(self.options.clone()));
3194+ workspaces.push(Workspace::new_no_outputs(
3195+ self.clock.clone(),
3196+ self.options.clone(),
3197+ ));
31653198 &mut workspaces[0]
31663199 };
31673200
@@ -3620,7 +3653,7 @@ mod tests {
36203653
36213654 impl<W: LayoutElement> Default for Layout<W> {
36223655 fn default() -> Self {
3623- Self::with_options(Default::default())
3656+ Self::with_options(Clock::with_override(Duration::ZERO), Default::default())
36243657 }
36253658 }
36263659
@@ -3854,6 +3887,16 @@ mod tests {
38543887 prop_oneof![Just(1.), Just(1.5), Just(2.),]
38553888 }
38563889
3890+ fn arbitrary_msec_delta() -> impl Strategy<Value = i32> {
3891+ prop_oneof![
3892+ 1 => Just(-1000),
3893+ 2 => Just(-10),
3894+ 1 => Just(0),
3895+ 2 => Just(10),
3896+ 6 => Just(1000),
3897+ ]
3898+ }
3899+
38573900 #[derive(Debug, Clone, Copy, Arbitrary)]
38583901 enum Op {
38593902 AddOutput(#[proptest(strategy = "1..=5usize")] usize),
@@ -3997,6 +4040,10 @@ mod tests {
39974040 Refresh {
39984041 is_active: bool,
39994042 },
4043+ AdvanceAnimations {
4044+ #[proptest(strategy = "arbitrary_msec_delta()")]
4045+ msec_delta: i32,
4046+ },
40004047 MoveWorkspaceToOutput(#[proptest(strategy = "1..=5u8")] u8),
40014048 ViewOffsetGestureBegin {
40024049 #[proptest(strategy = "1..=5usize")]
@@ -4505,6 +4552,16 @@ mod tests {
45054552 Op::Refresh { is_active } => {
45064553 layout.refresh(is_active);
45074554 }
4555+ Op::AdvanceAnimations { msec_delta } => {
4556+ let mut now = layout.clock.now();
4557+ if msec_delta >= 0 {
4558+ now = now.saturating_add(Duration::from_millis(msec_delta as u64));
4559+ } else {
4560+ now = now.saturating_sub(Duration::from_millis(-msec_delta as u64));
4561+ }
4562+ layout.clock.set_time_override(Some(now));
4563+ layout.advance_animations(now);
4564+ }
45084565 Op::MoveWorkspaceToOutput(id) => {
45094566 let name = format!("output{id}");
45104567 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
@@ -4617,7 +4674,7 @@ mod tests {
46174674
46184675 #[track_caller]
46194676 fn check_ops_with_options(options: Options, ops: &[Op]) {
4620- let mut layout = Layout::with_options(options);
4677+ let mut layout = Layout::with_options(Clock::with_override(Duration::ZERO), options);
46214678
46224679 for op in ops {
46234680 op.apply(&mut layout);
@@ -5440,7 +5497,7 @@ mod tests {
54405497 config.layout.border.off = false;
54415498 config.layout.border.width = FloatOrInt(2.);
54425499
5443- let mut layout = Layout::new(&config);
5500+ let mut layout = Layout::new(Clock::default(), &config);
54445501
54455502 Op::AddWindow {
54465503 id: 1,
@@ -5460,7 +5517,7 @@ mod tests {
54605517 let mut config = Config::default();
54615518 config.layout.preset_window_heights = vec![PresetSize::Fixed(1), PresetSize::Fixed(2)];
54625519
5463- let mut layout = Layout::new(&config);
5520+ let mut layout = Layout::new(Clock::default(), &config);
54645521
54655522 let ops = [
54665523 Op::AddOutput(1),
@@ -5752,6 +5809,37 @@ mod tests {
57525809 check_ops(&ops);
57535810 }
57545811
5812+ #[test]
5813+ fn interactive_move_onto_last_workspace() {
5814+ let ops = [
5815+ Op::AddOutput(1),
5816+ Op::AddWindow {
5817+ id: 0,
5818+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5819+ min_max_size: Default::default(),
5820+ },
5821+ Op::InteractiveMoveBegin {
5822+ window: 0,
5823+ output_idx: 1,
5824+ px: 0.,
5825+ py: 0.,
5826+ },
5827+ Op::InteractiveMoveUpdate {
5828+ window: 0,
5829+ dx: 1000.,
5830+ dy: 0.,
5831+ output_idx: 1,
5832+ px: 0.,
5833+ py: 0.,
5834+ },
5835+ Op::FocusWorkspaceDown,
5836+ Op::AdvanceAnimations { msec_delta: 1000 },
5837+ Op::InteractiveMoveEnd { window: 0 },
5838+ ];
5839+
5840+ check_ops(&ops);
5841+ }
5842+
57555843 #[test]
57565844 fn output_active_workspace_is_preserved() {
57575845 let ops = [
src/layout/monitor.rsmodified+17 -3
@@ -15,7 +15,7 @@ use super::workspace::{
1515 WorkspaceRenderElement,
1616 };
1717 use super::{LayoutElement, Options};
18-use crate::animation::Animation;
18+use crate::animation::{Animation, Clock};
1919 use crate::input::swipe_tracker::SwipeTracker;
2020 use crate::render_helpers::renderer::NiriRenderer;
2121 use crate::render_helpers::RenderTarget;
@@ -45,6 +45,8 @@ pub struct Monitor<W: LayoutElement> {
4545 pub(super) previous_workspace_id: Option<WorkspaceId>,
4646 /// In-progress switch between workspaces.
4747 pub(super) workspace_switch: Option<WorkspaceSwitch>,
48+ /// Clock for driving animations.
49+ pub(super) clock: Clock,
4850 /// Configurable properties of the layout.
4951 pub(super) options: Rc<Options>,
5052 }
@@ -94,7 +96,12 @@ impl WorkspaceSwitch {
9496 }
9597
9698 impl<W: LayoutElement> Monitor<W> {
97- pub fn new(output: Output, workspaces: Vec<Workspace<W>>, options: Rc<Options>) -> Self {
99+ pub fn new(
100+ output: Output,
101+ workspaces: Vec<Workspace<W>>,
102+ clock: Clock,
103+ options: Rc<Options>,
104+ ) -> Self {
98105 Self {
99106 output_name: output.name(),
100107 output,
@@ -102,6 +109,7 @@ impl<W: LayoutElement> Monitor<W> {
102109 active_workspace_idx: 0,
103110 previous_workspace_id: None,
104111 workspace_switch: None,
112+ clock,
105113 options,
106114 }
107115 }
@@ -151,7 +159,11 @@ impl<W: LayoutElement> Monitor<W> {
151159 }
152160
153161 pub fn add_workspace_bottom(&mut self) {
154- let ws = Workspace::new(self.output.clone(), self.options.clone());
162+ let ws = Workspace::new(
163+ self.output.clone(),
164+ self.clock.clone(),
165+ self.options.clone(),
166+ );
155167 self.workspaces.push(ws);
156168 }
157169
@@ -172,6 +184,7 @@ impl<W: LayoutElement> Monitor<W> {
172184 self.active_workspace_idx = idx;
173185
174186 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
187+ self.clock.now(),
175188 current_idx,
176189 idx as f64,
177190 0.,
@@ -1099,6 +1112,7 @@ impl<W: LayoutElement> Monitor<W> {
10991112
11001113 self.active_workspace_idx = new_idx;
11011114 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
1115+ self.clock.now(),
11021116 gesture.current_idx,
11031117 new_idx as f64,
11041118 velocity,
src/layout/tile.rsmodified+18 -7
@@ -13,7 +13,7 @@ use super::{
1313 LayoutElement, LayoutElementRenderElement, LayoutElementRenderSnapshot, Options,
1414 RESIZE_ANIMATION_THRESHOLD,
1515 };
16-use crate::animation::Animation;
16+use crate::animation::{Animation, Clock};
1717 use crate::niri_render_elements;
1818 use crate::render_helpers::border::BorderRenderElement;
1919 use crate::render_helpers::clipped_surface::{ClippedSurfaceRenderElement, RoundedCornerDamage};
@@ -76,6 +76,9 @@ pub struct Tile<W: LayoutElement> {
7676 /// Scale of the output the tile is on (and rounds its sizes to).
7777 scale: f64,
7878
79+ /// Clock for driving animations.
80+ pub(super) clock: Clock,
81+
7982 /// Configurable properties of the layout.
8083 pub(super) options: Rc<Options>,
8184 }
@@ -110,7 +113,7 @@ struct MoveAnimation {
110113 }
111114
112115 impl<W: LayoutElement> Tile<W> {
113- pub fn new(window: W, scale: f64, options: Rc<Options>) -> Self {
116+ pub fn new(window: W, scale: f64, clock: Clock, options: Rc<Options>) -> Self {
114117 let rules = window.rules();
115118 let border_config = rules.border.resolve_against(options.border);
116119 let focus_ring_config = rules.focus_ring.resolve_against(options.focus_ring.into());
@@ -130,6 +133,7 @@ impl<W: LayoutElement> Tile<W> {
130133 unmap_snapshot: None,
131134 rounded_corner_damage: Default::default(),
132135 scale,
136+ clock,
133137 options,
134138 }
135139 }
@@ -180,7 +184,13 @@ impl<W: LayoutElement> Tile<W> {
180184 let change = self.window.size().to_f64().to_point() - size_from.to_point();
181185 let change = f64::max(change.x.abs(), change.y.abs());
182186 if change > RESIZE_ANIMATION_THRESHOLD {
183- let anim = Animation::new(0., 1., 0., self.options.animations.window_resize.anim);
187+ let anim = Animation::new(
188+ self.clock.now(),
189+ 0.,
190+ 1.,
191+ 0.,
192+ self.options.animations.window_resize.anim,
193+ );
184194 self.resize_animation = Some(ResizeAnimation {
185195 anim,
186196 size_from,
@@ -316,6 +326,7 @@ impl<W: LayoutElement> Tile<W> {
316326
317327 pub fn start_open_animation(&mut self) {
318328 self.open_animation = Some(OpenAnimation::new(Animation::new(
329+ self.clock.now(),
319330 0.,
320331 1.,
321332 0.,
@@ -342,8 +353,8 @@ impl<W: LayoutElement> Tile<W> {
342353 // Preserve the previous config if ongoing.
343354 let anim = self.move_x_animation.take().map(|move_| move_.anim);
344355 let anim = anim
345- .map(|anim| anim.restarted(1., 0., 0.))
346- .unwrap_or_else(|| Animation::new(1., 0., 0., config));
356+ .map(|anim| anim.restarted(self.clock.now(), 1., 0., 0.))
357+ .unwrap_or_else(|| Animation::new(self.clock.now(), 1., 0., 0., config));
347358
348359 self.move_x_animation = Some(MoveAnimation {
349360 anim,
@@ -361,8 +372,8 @@ impl<W: LayoutElement> Tile<W> {
361372 // Preserve the previous config if ongoing.
362373 let anim = self.move_y_animation.take().map(|move_| move_.anim);
363374 let anim = anim
364- .map(|anim| anim.restarted(1., 0., 0.))
365- .unwrap_or_else(|| Animation::new(1., 0., 0., config));
375+ .map(|anim| anim.restarted(self.clock.now(), 1., 0., 0.))
376+ .unwrap_or_else(|| Animation::new(self.clock.now(), 1., 0., 0., config));
366377
367378 self.move_y_animation = Some(MoveAnimation {
368379 anim,
src/layout/workspace.rsmodified+35 -11
@@ -19,7 +19,7 @@ use super::closing_window::{ClosingWindow, ClosingWindowRenderElement};
1919 use super::insert_hint_element::{InsertHintElement, InsertHintRenderElement};
2020 use super::tile::{Tile, TileRenderElement, TileRenderSnapshot};
2121 use super::{ConfigureIntent, InteractiveResizeData, LayoutElement, Options, RemovedTile};
22-use crate::animation::Animation;
22+use crate::animation::{Animation, Clock};
2323 use crate::input::swipe_tracker::SwipeTracker;
2424 use crate::niri_render_elements;
2525 use crate::render_helpers::renderer::NiriRenderer;
@@ -113,6 +113,9 @@ pub struct Workspace<W: LayoutElement> {
113113 /// Insert hint element for rendering.
114114 insert_hint_element: InsertHintElement,
115115
116+ /// Clock for driving animations.
117+ pub(super) clock: Clock,
118+
116119 /// Configurable properties of the layout as received from the parent monitor.
117120 pub(super) base_options: Rc<Options>,
118121
@@ -293,6 +296,9 @@ pub struct Column<W: LayoutElement> {
293296 /// Scale of the output the column is on (and rounds its sizes to).
294297 scale: f64,
295298
299+ /// Clock for driving animations.
300+ clock: Clock,
301+
296302 /// Configurable properties of the layout.
297303 options: Rc<Options>,
298304 }
@@ -403,13 +409,14 @@ impl TileData {
403409 }
404410
405411 impl<W: LayoutElement> Workspace<W> {
406- pub fn new(output: Output, options: Rc<Options>) -> Self {
407- Self::new_with_config(output, None, options)
412+ pub fn new(output: Output, clock: Clock, options: Rc<Options>) -> Self {
413+ Self::new_with_config(output, None, clock, options)
408414 }
409415
410416 pub fn new_with_config(
411417 output: Output,
412418 config: Option<WorkspaceConfig>,
419+ clock: Clock,
413420 base_options: Rc<Options>,
414421 ) -> Self {
415422 let original_output = config
@@ -442,6 +449,7 @@ impl<W: LayoutElement> Workspace<W> {
442449 closing_windows: vec![],
443450 insert_hint: None,
444451 insert_hint_element: InsertHintElement::new(options.insert_hint),
452+ clock,
445453 base_options,
446454 options,
447455 name: config.map(|c| c.name.0),
@@ -451,6 +459,7 @@ impl<W: LayoutElement> Workspace<W> {
451459
452460 pub fn new_with_config_no_outputs(
453461 config: Option<WorkspaceConfig>,
462+ clock: Clock,
454463 base_options: Rc<Options>,
455464 ) -> Self {
456465 let original_output = OutputId(
@@ -482,6 +491,7 @@ impl<W: LayoutElement> Workspace<W> {
482491 closing_windows: vec![],
483492 insert_hint: None,
484493 insert_hint_element: InsertHintElement::new(options.insert_hint),
494+ clock,
485495 base_options,
486496 options,
487497 name: config.map(|c| c.name.0),
@@ -489,8 +499,8 @@ impl<W: LayoutElement> Workspace<W> {
489499 }
490500 }
491501
492- pub fn new_no_outputs(options: Rc<Options>) -> Self {
493- Self::new_with_config_no_outputs(None, options)
502+ pub fn new_no_outputs(clock: Clock, options: Rc<Options>) -> Self {
503+ Self::new_with_config_no_outputs(None, clock, options)
494504 }
495505
496506 pub fn id(&self) -> WorkspaceId {
@@ -941,6 +951,7 @@ impl<W: LayoutElement> Workspace<W> {
941951
942952 // FIXME: also compute and use current velocity.
943953 self.view_offset_adj = Some(ViewOffsetAdjustment::Animation(Animation::new(
954+ self.clock.now(),
944955 self.view_offset,
945956 new_view_offset,
946957 0.,
@@ -1100,7 +1111,12 @@ impl<W: LayoutElement> Workspace<W> {
11001111 width: ColumnWidth,
11011112 is_full_width: bool,
11021113 ) {
1103- let tile = Tile::new(window, self.scale.fractional_scale(), self.options.clone());
1114+ let tile = Tile::new(
1115+ window,
1116+ self.scale.fractional_scale(),
1117+ self.clock.clone(),
1118+ self.options.clone(),
1119+ );
11041120 self.add_tile(col_idx, tile, activate, width, is_full_width, None);
11051121 }
11061122
@@ -1118,7 +1134,6 @@ impl<W: LayoutElement> Workspace<W> {
11181134 self.view_size,
11191135 self.working_area,
11201136 self.scale.fractional_scale(),
1121- self.options.clone(),
11221137 width,
11231138 is_full_width,
11241139 true,
@@ -1682,7 +1697,13 @@ impl<W: LayoutElement> Workspace<W> {
16821697 ) {
16831698 let output_scale = Scale::from(self.scale.fractional_scale());
16841699
1685- let anim = Animation::new(0., 1., 0., self.options.animations.window_close.anim);
1700+ let anim = Animation::new(
1701+ self.clock.now(),
1702+ 0.,
1703+ 1.,
1704+ 0.,
1705+ self.options.animations.window_close.anim,
1706+ );
16861707
16871708 let blocker = if self.options.disable_transactions {
16881709 TransactionBlocker::completed()
@@ -1725,6 +1746,7 @@ impl<W: LayoutElement> Workspace<W> {
17251746
17261747 for (column, data) in zip(&self.columns, &self.data) {
17271748 assert!(Rc::ptr_eq(&self.options, &column.options));
1749+ assert_eq!(self.clock, column.clock);
17281750 assert_eq!(self.scale.fractional_scale(), column.scale);
17291751 column.verify_invariants();
17301752
@@ -2619,7 +2641,6 @@ impl<W: LayoutElement> Workspace<W> {
26192641 self.view_size,
26202642 self.working_area,
26212643 self.scale.fractional_scale(),
2622- self.options.clone(),
26232644 removed.width,
26242645 removed.is_full_width,
26252646 false,
@@ -2969,6 +2990,7 @@ impl<W: LayoutElement> Workspace<W> {
29692990 let target_view_offset = target_snap.view_pos - new_col_x;
29702991
29712992 self.view_offset_adj = Some(ViewOffsetAdjustment::Animation(Animation::new(
2993+ self.clock.now(),
29722994 current_view_offset + delta,
29732995 target_view_offset,
29742996 velocity,
@@ -3174,7 +3196,6 @@ impl<W: LayoutElement> Column<W> {
31743196 view_size: Size<f64, Logical>,
31753197 working_area: Rectangle<f64, Logical>,
31763198 scale: f64,
3177- options: Rc<Options>,
31783199 width: ColumnWidth,
31793200 is_full_width: bool,
31803201 animate_resize: bool,
@@ -3190,7 +3211,8 @@ impl<W: LayoutElement> Column<W> {
31903211 view_size,
31913212 working_area,
31923213 scale,
3193- options,
3214+ clock: tile.clock.clone(),
3215+ options: tile.options.clone(),
31943216 };
31953217
31963218 let is_pending_fullscreen = tile.window().is_pending_fullscreen();
@@ -3313,6 +3335,7 @@ impl<W: LayoutElement> Column<W> {
33133335 let current_offset = self.move_animation.as_ref().map_or(0., Animation::value);
33143336
33153337 self.move_animation = Some(Animation::new(
3338+ self.clock.now(),
33163339 from_x_offset + current_offset,
33173340 0.,
33183341 0.,
@@ -3716,6 +3739,7 @@ impl<W: LayoutElement> Column<W> {
37163739 let mut total_min_height = 0.;
37173740 for (tile, data) in zip(&self.tiles, &self.data) {
37183741 assert!(Rc::ptr_eq(&self.options, &tile.options));
3742+ assert_eq!(self.clock, tile.clock);
37193743 assert_eq!(self.scale, tile.scale());
37203744 assert_eq!(self.is_fullscreen, tile.window().is_pending_fullscreen());
37213745
src/niri.rsmodified+12 -3
@@ -100,6 +100,7 @@ use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState;
100100 use smithay::wayland::xdg_activation::XdgActivationState;
101101 use smithay::wayland::xdg_foreign::XdgForeignState;
102102
103+use crate::animation::Clock;
103104 use crate::backend::tty::SurfaceDmabufFeedback;
104105 use crate::backend::{Backend, RenderResult, Tty, Winit};
105106 use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor};
@@ -179,6 +180,9 @@ pub struct Niri {
179180 /// Whether the at-startup=true window rules are active.
180181 pub is_at_startup: bool,
181182
183+ /// Clock for driving animations.
184+ pub clock: Clock,
185+
182186 // Each workspace corresponds to a Space. Each workspace generally has one Output mapped to it,
183187 // however it may have none (when there are no outputs connected) or multiple (when mirroring).
184188 pub layout: Layout<Mapped>,
@@ -1671,7 +1675,8 @@ impl Niri {
16711675 let config_ = config.borrow();
16721676 let config_file_output_config = config_.outputs.clone();
16731677
1674- let layout = Layout::new(&config_);
1678+ let clock = Clock::default();
1679+ let layout = Layout::new(clock.clone(), &config_);
16751680
16761681 let (blocker_cleared_tx, blocker_cleared_rx) = mpsc::channel();
16771682
@@ -1799,8 +1804,8 @@ impl Niri {
17991804 let mods_with_finger_scroll_binds =
18001805 mods_with_finger_scroll_binds(backend.mod_key(), &config_.binds);
18011806
1802- let screenshot_ui = ScreenshotUi::new(config.clone());
1803- let config_error_notification = ConfigErrorNotification::new(config.clone());
1807+ let screenshot_ui = ScreenshotUi::new(clock.clone(), config.clone());
1808+ let config_error_notification = ConfigErrorNotification::new(clock.clone(), config.clone());
18041809
18051810 let mut hotkey_overlay = HotkeyOverlay::new(config.clone(), backend.mod_key());
18061811 if !config_.hotkey_overlay.skip_at_startup {
@@ -1895,6 +1900,7 @@ impl Niri {
18951900 display_handle,
18961901 start_time: Instant::now(),
18971902 is_at_startup: true,
1903+ clock,
18981904
18991905 layout,
19001906 global_space: Space::default(),
@@ -3044,6 +3050,7 @@ impl Niri {
30443050
30453051 for state in self.output_state.values_mut() {
30463052 if let Some(transition) = &mut state.screen_transition {
3053+ // Screen transition uses real time so that it's not affected by animation slowdown.
30473054 transition.advance_animations(target_time);
30483055 if transition.is_done() {
30493056 state.screen_transition = None;
@@ -4819,6 +4826,8 @@ impl Niri {
48194826 let delay = delay_ms.map_or(screen_transition::DELAY, |d| {
48204827 Duration::from_millis(u64::from(d))
48214828 });
4829+
4830+ // Screen transition uses real time so that it's not affected by animation slowdown.
48224831 let start_at = get_monotonic_time() + delay;
48234832 for (output, from_texture) in textures {
48244833 let state = self.output_state.get_mut(&output).unwrap();
src/ui/config_error_notification.rsmodified+11 -3
@@ -14,7 +14,7 @@ use smithay::output::Output;
1414 use smithay::reexports::gbm::Format as Fourcc;
1515 use smithay::utils::{Point, Transform};
1616
17-use crate::animation::Animation;
17+use crate::animation::{Animation, Clock};
1818 use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
1919 use crate::render_helpers::renderer::NiriRenderer;
2020 use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement};
@@ -35,6 +35,7 @@ pub struct ConfigErrorNotification {
3535 // notification.
3636 created_path: Option<PathBuf>,
3737
38+ clock: Clock,
3839 config: Rc<RefCell<Config>>,
3940 }
4041
@@ -46,18 +47,25 @@ enum State {
4647 }
4748
4849 impl ConfigErrorNotification {
49- pub fn new(config: Rc<RefCell<Config>>) -> Self {
50+ pub fn new(clock: Clock, config: Rc<RefCell<Config>>) -> Self {
5051 Self {
5152 state: State::Hidden,
5253 buffers: RefCell::new(HashMap::new()),
5354 created_path: None,
55+ clock,
5456 config,
5557 }
5658 }
5759
5860 fn animation(&self, from: f64, to: f64) -> Animation {
5961 let c = self.config.borrow();
60- Animation::new(from, to, 0., c.animations.config_notification_open_close.0)
62+ Animation::new(
63+ self.clock.now(),
64+ from,
65+ to,
66+ 0.,
67+ c.animations.config_notification_open_close.0,
68+ )
6169 }
6270
6371 pub fn show_created(&mut self, created_path: PathBuf) {
src/ui/screenshot_ui.rsmodified+13 -4
@@ -20,7 +20,7 @@ use smithay::input::keyboard::{Keysym, ModifiersState};
2020 use smithay::output::{Output, WeakOutput};
2121 use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform};
2222
23-use crate::animation::Animation;
23+use crate::animation::{Animation, Clock};
2424 use crate::niri_render_elements;
2525 use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
2626 use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};
@@ -49,6 +49,7 @@ const TEXT_SHOW_P: &str =
4949 pub enum ScreenshotUi {
5050 Closed {
5151 last_selection: Option<(WeakOutput, Rectangle<i32, Physical>)>,
52+ clock: Clock,
5253 config: Rc<RefCell<Config>>,
5354 },
5455 Open {
@@ -57,6 +58,7 @@ pub enum ScreenshotUi {
5758 mouse_down: bool,
5859 show_pointer: bool,
5960 open_anim: Animation,
61+ clock: Clock,
6062 config: Rc<RefCell<Config>>,
6163 },
6264 }
@@ -86,9 +88,10 @@ niri_render_elements! {
8688 }
8789
8890 impl ScreenshotUi {
89- pub fn new(config: Rc<RefCell<Config>>) -> Self {
91+ pub fn new(clock: Clock, config: Rc<RefCell<Config>>) -> Self {
9092 Self::Closed {
9193 last_selection: None,
94+ clock,
9295 config,
9396 }
9497 }
@@ -106,6 +109,7 @@ impl ScreenshotUi {
106109
107110 let Self::Closed {
108111 last_selection,
112+ clock,
109113 config,
110114 } = self
111115 else {
@@ -181,7 +185,7 @@ impl ScreenshotUi {
181185
182186 let open_anim = {
183187 let c = config.borrow();
184- Animation::new(0., 1., 0., c.animations.screenshot_ui_open.0)
188+ Animation::new(clock.now(), 0., 1., 0., c.animations.screenshot_ui_open.0)
185189 };
186190
187191 *self = Self::Open {
@@ -190,6 +194,7 @@ impl ScreenshotUi {
190194 mouse_down: false,
191195 show_pointer: true,
192196 open_anim,
197+ clock: clock.clone(),
193198 config: config.clone(),
194199 };
195200
@@ -200,7 +205,10 @@ impl ScreenshotUi {
200205
201206 pub fn close(&mut self) -> bool {
202207 let Self::Open {
203- selection, config, ..
208+ selection,
209+ clock,
210+ config,
211+ ..
204212 } = self
205213 else {
206214 return false;
@@ -213,6 +221,7 @@ impl ScreenshotUi {
213221
214222 *self = Self::Closed {
215223 last_selection,
224+ clock: clock.clone(),
216225 config: config.clone(),
217226 };
218227