forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

mod.rs366 lines, 10.0 KiBHistoryRawDownload
1use std::time::Duration;
2
3use keyframe::functions::{EaseOutCubic, EaseOutQuad};
4use keyframe::EasingFunction;
5
6mod bezier;
7use bezier::CubicBezier;
8
9mod spring;
10pub use spring::{Spring, SpringParams};
11
12mod clock;
13pub use clock::Clock;
14
15#[derive(Debug, Clone)]
16pub struct Animation {
17 from: f64,
18 to: f64,
19 initial_velocity: f64,
20 is_off: bool,
21 duration: Duration,
22 /// Time until the animation first reaches `to`.
23 ///
24 /// Best effort; not always exactly precise.
25 clamped_duration: Duration,
26 start_time: Duration,
27 clock: Clock,
28 kind: Kind,
29}
30
31#[derive(Debug, Clone, Copy)]
32enum Kind {
33 Easing {
34 curve: Curve,
35 },
36 Spring(Spring),
37 Deceleration {
38 initial_velocity: f64,
39 deceleration_rate: f64,
40 },
41}
42
43#[derive(Debug, Clone, Copy)]
44pub enum Curve {
45 Linear,
46 EaseOutQuad,
47 EaseOutCubic,
48 EaseOutExpo,
49 CubicBezier(CubicBezier),
50}
51
52impl Animation {
53 pub fn new(
54 clock: Clock,
55 from: f64,
56 to: f64,
57 initial_velocity: f64,
58 config: niri_config::Animation,
59 ) -> Self {
60 // Scale the velocity by rate to keep the touchpad gestures feeling right.
61 let initial_velocity = initial_velocity / clock.rate().max(0.001);
62
63 let mut rv = Self::ease(clock, from, to, initial_velocity, 0, Curve::EaseOutCubic);
64 if config.off {
65 rv.is_off = true;
66 return rv;
67 }
68
69 rv.replace_config(config);
70 rv
71 }
72
73 pub fn replace_config(&mut self, config: niri_config::Animation) {
74 self.is_off = config.off;
75 if config.off {
76 self.duration = Duration::ZERO;
77 self.clamped_duration = Duration::ZERO;
78 return;
79 }
80
81 let start_time = self.start_time;
82
83 match config.kind {
84 niri_config::animations::Kind::Spring(p) => {
85 let params = SpringParams::new(p.damping_ratio, f64::from(p.stiffness), p.epsilon);
86
87 let spring = Spring {
88 from: self.from,
89 to: self.to,
90 initial_velocity: self.initial_velocity,
91 params,
92 };
93 *self = Self::spring(self.clock.clone(), spring);
94 }
95 niri_config::animations::Kind::Easing(p) => {
96 *self = Self::ease(
97 self.clock.clone(),
98 self.from,
99 self.to,
100 self.initial_velocity,
101 u64::from(p.duration_ms),
102 Curve::from(p.curve),
103 );
104 }
105 }
106
107 self.start_time = start_time;
108 }
109
110 /// Restarts the animation using the previous config.
111 pub fn restarted(&self, from: f64, to: f64, initial_velocity: f64) -> Self {
112 if self.is_off {
113 return self.clone();
114 }
115
116 // Scale the velocity by rate to keep the touchpad gestures feeling right.
117 let initial_velocity = initial_velocity / self.clock.rate().max(0.001);
118
119 match self.kind {
120 Kind::Easing { curve } => Self::ease(
121 self.clock.clone(),
122 from,
123 to,
124 initial_velocity,
125 self.duration.as_millis() as u64,
126 curve,
127 ),
128 Kind::Spring(spring) => {
129 let spring = Spring {
130 from,
131 to,
132 initial_velocity: self.initial_velocity,
133 params: spring.params,
134 };
135 Self::spring(self.clock.clone(), spring)
136 }
137 Kind::Deceleration {
138 initial_velocity,
139 deceleration_rate,
140 } => {
141 let threshold = 0.001; // FIXME
142 Self::decelerate(
143 self.clock.clone(),
144 from,
145 initial_velocity,
146 deceleration_rate,
147 threshold,
148 )
149 }
150 }
151 }
152
153 pub fn ease(
154 clock: Clock,
155 from: f64,
156 to: f64,
157 initial_velocity: f64,
158 duration_ms: u64,
159 curve: Curve,
160 ) -> Self {
161 let duration = Duration::from_millis(duration_ms);
162 let kind = Kind::Easing { curve };
163
164 Self {
165 from,
166 to,
167 initial_velocity,
168 is_off: false,
169 duration,
170 // Our current curves never overshoot.
171 clamped_duration: duration,
172 start_time: clock.now(),
173 clock,
174 kind,
175 }
176 }
177
178 pub fn spring(clock: Clock, spring: Spring) -> Self {
179 let _span = tracy_client::span!("Animation::spring");
180
181 let duration = spring.duration();
182 let clamped_duration = spring.clamped_duration().unwrap_or(duration);
183 let kind = Kind::Spring(spring);
184
185 Self {
186 from: spring.from,
187 to: spring.to,
188 initial_velocity: spring.initial_velocity,
189 is_off: false,
190 duration,
191 clamped_duration,
192 start_time: clock.now(),
193 clock,
194 kind,
195 }
196 }
197
198 pub fn decelerate(
199 clock: Clock,
200 from: f64,
201 initial_velocity: f64,
202 deceleration_rate: f64,
203 threshold: f64,
204 ) -> Self {
205 let duration_s = if initial_velocity == 0. {
206 0.
207 } else {
208 let coeff = 1000. * deceleration_rate.ln();
209 (-coeff * threshold / initial_velocity.abs()).ln() / coeff
210 };
211 let duration = Duration::from_secs_f64(duration_s);
212
213 let to = from - initial_velocity / (1000. * deceleration_rate.ln());
214
215 let kind = Kind::Deceleration {
216 initial_velocity,
217 deceleration_rate,
218 };
219
220 Self {
221 from,
222 to,
223 initial_velocity,
224 is_off: false,
225 duration,
226 clamped_duration: duration,
227 start_time: clock.now(),
228 clock,
229 kind,
230 }
231 }
232
233 pub fn is_done(&self) -> bool {
234 if self.clock.should_complete_instantly() {
235 return true;
236 }
237
238 self.clock.now() >= self.start_time + self.duration
239 }
240
241 pub fn is_clamped_done(&self) -> bool {
242 if self.clock.should_complete_instantly() {
243 return true;
244 }
245
246 self.clock.now() >= self.start_time + self.clamped_duration
247 }
248
249 pub fn value_at(&self, at: Duration) -> f64 {
250 if at <= self.start_time {
251 // Return from when at == start_time so that when the animations are off, the behavior
252 // within a single event loop cycle (i.e. no time had passed since the start of an
253 // animation) matches the behavior when the animations are on.
254 return self.from;
255 } else if self.start_time + self.duration <= at {
256 return self.to;
257 }
258
259 if self.clock.should_complete_instantly() {
260 return self.to;
261 }
262
263 let passed = at.saturating_sub(self.start_time);
264
265 match self.kind {
266 Kind::Easing { curve } => {
267 let passed = passed.as_secs_f64();
268 let total = self.duration.as_secs_f64();
269 let x = (passed / total).clamp(0., 1.);
270 curve.y(x) * (self.to - self.from) + self.from
271 }
272 Kind::Spring(spring) => {
273 let value = spring.value_at(passed);
274
275 // Protect against numerical instability.
276 let range = (self.to - self.from) * 10.;
277 let a = self.from - range;
278 let b = self.to + range;
279 if self.from <= self.to {
280 value.clamp(a, b)
281 } else {
282 value.clamp(b, a)
283 }
284 }
285 Kind::Deceleration {
286 initial_velocity,
287 deceleration_rate,
288 } => {
289 let passed = passed.as_secs_f64();
290 let coeff = 1000. * deceleration_rate.ln();
291 self.from + (deceleration_rate.powf(1000. * passed) - 1.) / coeff * initial_velocity
292 }
293 }
294 }
295
296 pub fn value(&self) -> f64 {
297 self.value_at(self.clock.now())
298 }
299
300 /// Returns a value that stops at the target value after first reaching it.
301 ///
302 /// Best effort; not always exactly precise.
303 pub fn clamped_value(&self) -> f64 {
304 if self.is_clamped_done() {
305 return self.to;
306 }
307
308 self.value()
309 }
310
311 pub fn to(&self) -> f64 {
312 self.to
313 }
314
315 pub fn from(&self) -> f64 {
316 self.from
317 }
318
319 pub fn start_time(&self) -> Duration {
320 self.start_time
321 }
322
323 pub fn end_time(&self) -> Duration {
324 self.start_time + self.duration
325 }
326
327 pub fn duration(&self) -> Duration {
328 self.duration
329 }
330
331 pub fn offset(&mut self, offset: f64) {
332 self.from += offset;
333 self.to += offset;
334
335 if let Kind::Spring(spring) = &mut self.kind {
336 spring.from += offset;
337 spring.to += offset;
338 }
339 }
340}
341
342impl Curve {
343 pub fn y(self, x: f64) -> f64 {
344 match self {
345 Curve::Linear => x,
346 Curve::EaseOutQuad => EaseOutQuad.y(x),
347 Curve::EaseOutCubic => EaseOutCubic.y(x),
348 Curve::EaseOutExpo => 1. - 2f64.powf(-10. * x),
349 Curve::CubicBezier(b) => b.y(x),
350 }
351 }
352}
353
354impl From<niri_config::animations::Curve> for Curve {
355 fn from(value: niri_config::animations::Curve) -> Self {
356 match value {
357 niri_config::animations::Curve::Linear => Curve::Linear,
358 niri_config::animations::Curve::EaseOutQuad => Curve::EaseOutQuad,
359 niri_config::animations::Curve::EaseOutCubic => Curve::EaseOutCubic,
360 niri_config::animations::Curve::EaseOutExpo => Curve::EaseOutExpo,
361 niri_config::animations::Curve::CubicBezier(x1, y1, x2, y2) => {
362 Curve::CubicBezier(CubicBezier::new(x1, y1, x2, y2))
363 }
364 }
365 }
366}