-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathanimation.rs
More file actions
419 lines (373 loc) · 14.4 KB
/
animation.rs
File metadata and controls
419 lines (373 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use std::time::{Duration, Instant};
use objc2_core_foundation::{CGPoint, CGRect, CGSize};
use tracing::{debug, trace};
use super::TransactionId;
use crate::actor::app::{AppThreadHandle, Request, WindowId, pid_t};
use crate::actor::reactor::Reactor;
use crate::common::collections::HashMap;
use crate::common::config::AnimationEasing;
use crate::sys::geometry::{Round, SameAs};
use crate::sys::power;
use crate::sys::screen::SpaceId;
use crate::sys::timer::Timer;
use crate::sys::window_server::WindowServerId;
#[derive(Debug)]
pub struct Animation<'a> {
//start: CFAbsoluteTime,
//interval: CFTimeInterval,
start: Instant,
interval: Duration,
frames: u32,
windows: Vec<(
&'a AppThreadHandle,
WindowId,
CGRect,
CGRect,
bool,
TransactionId,
)>,
}
impl<'a> Animation<'a> {
pub fn new(fps: f64, duration: f64, _: AnimationEasing) -> Self {
let interval = Duration::from_secs_f64(1.0 / fps);
// let now = unsafe { CFAbsoluteTimeGetCurrent() };
let now = Instant::now();
Animation {
start: now, // + interval, // not necessary, provide one extra frame to get things going
interval,
frames: (duration * fps).round() as u32,
windows: vec![],
}
}
pub fn add_window(
&mut self,
handle: &'a AppThreadHandle,
wid: WindowId,
start: CGRect,
finish: CGRect,
is_focus: bool,
txid: TransactionId,
) {
self.windows.push((handle, wid, start, finish, is_focus, txid))
}
pub fn run(self) {
if self.windows.is_empty() {
return;
}
for &(handle, wid, from, to, is_focus, txid) in &self.windows {
_ = handle.send(Request::BeginWindowAnimation(wid));
// Resize new windows immediately.
if is_focus {
let frame = CGRect {
origin: from.origin,
size: to.size,
};
_ = handle.send(Request::SetWindowFrame(wid, frame, txid, true));
}
}
let mut next_frames = Vec::with_capacity(self.windows.len());
for frame in 1..=self.frames {
let t: f64 = f64::from(frame) / f64::from(self.frames);
next_frames.clear();
for (_, _, from, to, _, _) in &self.windows {
next_frames.push(get_frame(*from, *to, t));
}
let deadline = self.start + frame * self.interval;
let duration = deadline - Instant::now();
if duration < Duration::ZERO {
continue;
}
Timer::sleep(duration);
for (&(handle, wid, _, to, _, txid), rect) in self.windows.iter().zip(&next_frames) {
let mut rect = *rect;
// Actually don't animate size, too slow. Resize halfway through
// and then set the size again at the end, in case it got
// clipped during the animation.
if frame * 2 == self.frames || frame == self.frames {
rect.size = to.size;
_ = handle.send(Request::SetWindowFrame(wid, rect, txid, true));
} else {
_ = handle.send(Request::SetWindowPos(wid, rect.origin, txid, true));
}
}
}
for &(handle, wid, ..) in &self.windows {
_ = handle.send(Request::EndWindowAnimation(wid));
}
}
#[allow(dead_code)]
pub fn skip_to_end(self) {
for &(handle, wid, _from, to, _, txid) in &self.windows {
_ = handle.send(Request::SetWindowFrame(wid, to, txid, true));
}
}
}
fn get_frame(a: CGRect, b: CGRect, t: f64) -> CGRect {
let s = ease(t);
CGRect {
origin: CGPoint {
x: blend(a.origin.x, b.origin.x, s),
y: blend(a.origin.y, b.origin.y, s),
},
size: CGSize {
width: blend(a.size.width, b.size.width, s),
height: blend(a.size.height, b.size.height, s),
},
}
}
// https://notes.yvt.jp/Graphics/Easing-Functions/
fn ease(t: f64) -> f64 {
if t < 0.5 {
(1.0 - f64::sqrt(1.0 - f64::powi(2.0 * t, 2))) / 2.0
} else {
(f64::sqrt(1.0 - f64::powi(-2.0 * t + 2.0, 2)) + 1.0) / 2.0
}
}
fn blend(a: f64, b: f64, s: f64) -> f64 { (1.0 - s) * a + s * b }
pub struct AnimationManager;
impl AnimationManager {
pub fn animate_layout(
reactor: &mut Reactor,
space: SpaceId,
layout: &[(WindowId, CGRect)],
is_resize: bool,
skip_wid: Option<WindowId>,
) -> bool {
let Some(active_ws) = reactor.layout_manager.layout_engine.active_workspace(space) else {
return false;
};
let mut anim = Animation::new(
reactor.config.settings.animation_fps,
reactor.config.settings.animation_duration,
reactor.config.settings.animation_easing.clone(),
);
let mut animated_count = 0;
let mut animated_wids_wsids: Vec<u32> = Vec::new();
let mut any_frame_changed = false;
let mut synced_native_tab_wids = Vec::new();
for &(wid, target_frame) in layout {
// Skip applying layout frames and animations for the window currently being dragged.
if skip_wid == Some(wid) {
trace!(
?wid,
"Skipping animated layout update for window currently being dragged"
);
continue;
}
let target_frame = target_frame.round();
let (current_frame, window_server_id, txid) =
match reactor.window_manager.windows.get_mut(&wid) {
Some(window) => {
let current_frame = window.frame_monotonic;
if target_frame.same_as(current_frame) {
continue;
}
let Some(wsid) = window.info.sys_id else {
trace!(
?wid,
?current_frame,
?target_frame,
"Skipping animation for window without window server id"
);
continue;
};
any_frame_changed = true;
let txid = reactor.transaction_manager.generate_next_txid(wsid);
(current_frame, Some(wsid), txid)
}
None => {
debug!(?wid, "Skipping - window no longer exists");
continue;
}
};
let Some(app_state) = reactor.app_manager.apps.get(&wid.pid) else {
debug!(?wid, "Skipping for window - app no longer exists");
continue;
};
let is_active = reactor
.layout_manager
.layout_engine
.virtual_workspace_manager()
.workspace_for_window(space, wid)
.map_or(false, |ws| ws == active_ws);
if is_active {
trace!(?wid, ?current_frame, ?target_frame, "Animating visible window");
animated_wids_wsids.push(wid.idx.into());
anim.add_window(&app_state.handle, wid, current_frame, target_frame, false, txid);
animated_count += 1;
if let Some(wsid) = window_server_id {
reactor.transaction_manager.update_txid_entries([(wsid, txid, target_frame)]);
}
} else {
trace!(
?wid,
?current_frame,
?target_frame,
"Direct positioning hidden window"
);
if let Some(wsid) = window_server_id {
reactor.transaction_manager.update_txid_entries([(wsid, txid, target_frame)]);
}
if let Err(e) =
app_state.handle.send(Request::SetWindowFrame(wid, target_frame, txid, true))
{
debug!(?wid, ?e, "Failed to send frame request for hidden window");
continue;
}
}
if let Some(window) = reactor.window_manager.windows.get_mut(&wid) {
window.frame_monotonic = target_frame;
}
synced_native_tab_wids.push(wid);
}
if animated_count > 0 {
let low_power = power::is_low_power_mode_enabled();
let layout_animate = reactor
.layout_manager
.layout_engine
.layout_specific_animate_settings(space)
.unwrap_or(reactor.config.settings.animate);
if is_resize || !layout_animate || low_power {
anim.skip_to_end();
} else {
anim.run();
}
}
for wid in synced_native_tab_wids {
reactor.handle_native_tab_frame_changed(wid, true);
}
any_frame_changed
}
pub fn instant_layout(
reactor: &mut Reactor,
layout: &[(WindowId, CGRect)],
skip_wid: Option<WindowId>,
) -> bool {
let mut per_app: HashMap<pid_t, Vec<(WindowId, CGRect)>> = HashMap::default();
let mut any_frame_changed = false;
for &(wid, target_frame) in layout {
// Skip applying a layout frame for the window currently being dragged.
if skip_wid == Some(wid) {
trace!(?wid, "Skipping layout update for window currently being dragged");
continue;
}
let Some(window) = reactor.window_manager.windows.get_mut(&wid) else {
debug!(?wid, "Skipping layout - window no longer exists");
continue;
};
let target_frame = target_frame.round();
let current_frame = window.frame_monotonic;
if target_frame.same_as(current_frame) {
continue;
}
if window.info.sys_id.is_none() {
trace!(
?wid,
?current_frame,
?target_frame,
"Skipping instant layout for window without window server id"
);
continue;
}
any_frame_changed = true;
trace!(
?wid,
?current_frame,
?target_frame,
"Instant workspace positioning"
);
per_app.entry(wid.pid).or_default().push((wid, target_frame));
}
for (pid, frames) in per_app.into_iter() {
if frames.is_empty() {
continue;
}
let Some(app_state) = reactor.app_manager.apps.get(&pid) else {
debug!(?pid, "Skipping layout update for app - app no longer exists");
continue;
};
let handle = app_state.handle.clone();
let (first_wid, first_target) = frames[0];
let mut txid = TransactionId::default();
let mut has_txid = false;
let mut txid_entries: Vec<(WindowServerId, TransactionId, CGRect)> = Vec::new();
if let Some(window) = reactor.window_manager.windows.get_mut(&first_wid) {
if let Some(wsid) = window.info.sys_id {
txid = reactor.transaction_manager.generate_next_txid(wsid);
has_txid = true;
txid_entries.push((wsid, txid, first_target));
}
}
if has_txid {
for (wid, frame) in frames.iter().skip(1) {
if let Some(w) = reactor.window_manager.windows.get_mut(wid) {
if let Some(wsid) = w.info.sys_id {
reactor.transaction_manager.set_last_sent_txid(wsid, txid);
txid_entries.push((wsid, txid, *frame));
}
}
}
reactor.transaction_manager.update_txid_entries(txid_entries);
}
let frames_to_send = frames.clone();
if let Err(e) = handle.send(Request::SetBatchWindowFrame(frames_to_send, txid)) {
debug!(
?pid,
?e,
"Failed to send batch frame request - app may have quit"
);
continue;
}
for (wid, target_frame) in &frames {
if let Some(window) = reactor.window_manager.windows.get_mut(wid) {
window.frame_monotonic = *target_frame;
}
reactor.handle_native_tab_frame_changed(*wid, true);
}
}
any_frame_changed
}
}
#[cfg(test)]
mod tests {
use objc2_core_foundation::{CGPoint, CGRect, CGSize};
use super::AnimationManager;
use crate::actor::reactor::testing::{Apps, make_window, screen_params_event};
use crate::actor::reactor::{Reactor, WindowId};
use crate::layout_engine::LayoutEngine;
use crate::sys::screen::SpaceId;
#[test]
fn layout_application_skips_windows_without_window_server_ids() {
let mut apps = Apps::new();
let mut reactor = Reactor::new_for_test(LayoutEngine::new(
&crate::common::config::VirtualWorkspaceSettings::default(),
&crate::common::config::LayoutSettings::default(),
None,
));
let space = SpaceId::new(90);
reactor.handle_event(screen_params_event(
vec![CGRect::new(CGPoint::new(0., 0.), CGSize::new(1000., 1000.))],
vec![Some(space)],
vec![],
));
reactor.handle_events(apps.make_app(1, vec![make_window(1)]));
apps.simulate_until_quiet(&mut reactor);
let _ = apps.requests();
let wid = WindowId::new(1, 1);
let target = CGRect::new(CGPoint::new(300., 50.), CGSize::new(400., 700.));
reactor.window_manager.windows.get_mut(&wid).unwrap().info.sys_id = None;
assert!(!AnimationManager::animate_layout(
&mut reactor,
space,
&[(wid, target)],
false,
None,
));
assert!(!AnimationManager::instant_layout(
&mut reactor,
&[(wid, target)],
None,
));
assert!(apps.requests().is_empty());
}
}