forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

animations.rs190 lines, 5.8 KiBHistoryRawDownload
1use std::fmt::Write as _;
2use std::time::Duration;
3
4use insta::assert_snapshot;
5use niri_config::animations::{Curve, EasingParams, Kind};
6use niri_config::Config;
7use niri_ipc::SizeChange;
8use smithay::utils::{Point, Size};
9use wayland_client::protocol::wl_surface::WlSurface;
10
11use super::client::ClientId;
12use super::*;
13use crate::niri::Niri;
14
15fn format_tiles(niri: &Niri) -> String {
16 let mut buf = String::new();
17 let ws = niri.layout.active_workspace().unwrap();
18 let mut tiles: Vec<_> = ws.tiles_with_render_positions().collect();
19
20 // We sort by id since that gives us a consistent order (from first opened to last), but we
21 // don't print the id since it's nondeterministic (the id is a global counter across all
22 // running tests in the same binary).
23 tiles.sort_by_key(|(tile, _, _)| tile.window().id().get());
24 for (tile, pos, _visible) in tiles {
25 let Size { w, h, .. } = tile.animated_tile_size();
26 let Point { x, y, .. } = pos;
27 writeln!(&mut buf, "{w:>3.0} × {h:>3.0} at x:{x:>3.0} y:{y:>3.0}").unwrap();
28 }
29 buf
30}
31
32fn create_window(f: &mut Fixture, id: ClientId, w: u16, h: u16) -> WlSurface {
33 let window = f.client(id).create_window();
34 let surface = window.surface.clone();
35 window.commit();
36 f.roundtrip(id);
37
38 let window = f.client(id).window(&surface);
39 window.attach_new_buffer();
40 window.set_size(w, h);
41 window.ack_last_and_commit();
42 f.roundtrip(id);
43
44 surface
45}
46
47fn set_time(niri: &mut Niri, time: Duration) {
48 // This is a bit involved because we're dealing with an AdjustableClock that maintains its own
49 // internal current_time.
50
51 // First, reset current_time to zero by matching unadjusted time to it (at rate 0.0), then
52 // setting unadjusted time to zero at rate 1.0 (causing current_time to also go to zero).
53 let now = niri.clock.now();
54 niri.clock.set_unadjusted(now);
55 let _ = niri.clock.now();
56 niri.clock.set_unadjusted(Duration::ZERO);
57 niri.clock.set_rate(1.0);
58 let _ = niri.clock.now();
59
60 // Now, set the desired time at rate 1.0.
61 niri.clock.set_unadjusted(time);
62 let _ = niri.clock.now();
63
64 // Freeze the clock so that clear() inside the niri loop callback followed by some get()
65 // doesn't replace it with the monotonic time.
66 niri.clock.set_rate(0.0);
67}
68
69// Sets up a fixture with linear animations, a renderer, and an output.
70fn set_up() -> Fixture {
71 const LINEAR: Kind = Kind::Easing(EasingParams {
72 duration_ms: 1000,
73 curve: Curve::Linear,
74 });
75
76 let mut config = Config::default();
77 config.layout.gaps = 0.0;
78 config.animations.window_resize.anim.kind = LINEAR;
79 config.animations.window_movement.0.kind = LINEAR;
80
81 let mut f = Fixture::with_config(config);
82 f.niri_state().backend.headless().add_renderer().unwrap();
83 f.add_output(1, (1920, 1080));
84
85 f
86}
87
88fn set_up_two_in_column() -> (Fixture, ClientId, WlSurface, WlSurface) {
89 let mut f = set_up();
90
91 let id = f.add_client();
92
93 let surface1 = create_window(&mut f, id, 100, 100);
94 let surface2 = create_window(&mut f, id, 200, 200);
95 f.double_roundtrip(id);
96
97 let _ = f.client(id).window(&surface1).recent_configures();
98 let _ = f.client(id).window(&surface2).recent_configures();
99
100 // Consume into one column.
101 f.niri().layout.focus_left();
102 f.niri().layout.consume_into_column();
103 f.double_roundtrip(id);
104
105 // Commit for the column consume.
106 let window = f.client(id).window(&surface1);
107 window.ack_last_and_commit();
108
109 let window = f.client(id).window(&surface2);
110 window.ack_last_and_commit();
111
112 f.double_roundtrip(id);
113
114 set_time(f.niri(), Duration::ZERO);
115 f.niri_complete_animations();
116
117 (f, id, surface1, surface2)
118}
119
120#[test]
121fn egl_height_resize_animates_next_y() {
122 let (mut f, id, surface1, surface2) = set_up_two_in_column();
123
124 // Issue a resize.
125 f.niri()
126 .layout
127 .set_window_height(None, SizeChange::AdjustFixed(-50));
128 f.double_roundtrip(id);
129
130 // The top window shrinks in response, the bottom remains as is.
131 let window = f.client(id).window(&surface1);
132 window.set_size(100, 50);
133 window.ack_last_and_commit();
134 let window = f.client(id).window(&surface2);
135 window.ack_last_and_commit();
136
137 // This starts the resize animation for the top window and the Y move for the bottom.
138 f.roundtrip(id);
139
140 // No time had passed yet, so we're at the initial state.
141 assert_snapshot!(format_tiles(f.niri()), @r"
142 100 × 100 at x: 0 y: 0
143 200 × 200 at x: 0 y:100
144 ");
145
146 // Advance the time halfway.
147 set_time(f.niri(), Duration::from_millis(500));
148 f.niri().advance_animations();
149
150 // Top window is half-resized at 75 px tall, bottom window is at y=75 matching it.
151 assert_snapshot!(format_tiles(f.niri()), @r"
152 100 × 75 at x: 0 y: 0
153 200 × 200 at x: 0 y: 75
154 ");
155
156 // Advance the time to completion.
157 set_time(f.niri(), Duration::from_millis(1000));
158 f.niri().advance_animations();
159
160 // Final state at 50 px.
161 assert_snapshot!(format_tiles(f.niri()), @r"
162 100 × 50 at x: 0 y: 0
163 200 × 200 at x: 0 y: 50
164 ");
165}
166
167#[test]
168fn egl_clientside_height_change_doesnt_animate() {
169 let (mut f, id, surface1, _surface2) = set_up_two_in_column();
170
171 // The initial state.
172 assert_snapshot!(format_tiles(f.niri()), @r"
173 100 × 100 at x: 0 y: 0
174 200 × 200 at x: 0 y:100
175 ");
176
177 // The top window shrinks by itself, without a niri-issued resize.
178 let window = f.client(id).window(&surface1);
179 window.set_size(100, 50);
180 window.commit();
181
182 // This does not start any animations.
183 f.roundtrip(id);
184
185 // No time had passed yet, but we are at the final state right away.
186 assert_snapshot!(format_tiles(f.niri()), @r"
187 100 × 50 at x: 0 y: 0
188 200 × 200 at x: 0 y: 50
189 ");
190}