niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·64a9351·Ivan Molodetskikh·2 years ago·1319 commits
| 1 | use std::time::Duration; |
| 2 | |
| 3 | use keyframe::functions::{EaseOutCubic, EaseOutQuad}; |
| 4 | use keyframe::EasingFunction; |
| 5 | use portable_atomic::{AtomicF64, Ordering}; |
| 6 | |
| 7 | use crate::utils::get_monotonic_time; |
| 8 | |
| 9 | mod spring; |
| 10 | pub use spring::{Spring, SpringParams}; |
| 11 | |
| 12 | pub static ANIMATION_SLOWDOWN: AtomicF64 = AtomicF64::new(1.); |
| 13 | |
| 14 | #[derive(Debug, Clone)] |
| 15 | pub struct Animation { |
| 16 | from: f64, |
| 17 | to: f64, |
| 18 | initial_velocity: f64, |
| 19 | is_off: bool, |
| 20 | duration: Duration, |
| 21 | /// Time until the animation first reaches `to`. |
| 22 | /// |
| 23 | /// Best effort; not always exactly precise. |
| 24 | clamped_duration: Duration, |
| 25 | start_time: Duration, |
| 26 | current_time: Duration, |
| 27 | kind: Kind, |
| 28 | } |
| 29 | |
| 30 | #[derive(Debug, Clone, Copy)] |
| 31 | enum Kind { |
| 32 | Easing { |
| 33 | curve: Curve, |
| 34 | }, |
| 35 | Spring(Spring), |
| 36 | Deceleration { |
| 37 | initial_velocity: f64, |
| 38 | deceleration_rate: f64, |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, Copy)] |
| 43 | pub enum Curve { |
| 44 | Linear, |
| 45 | EaseOutQuad, |
| 46 | EaseOutCubic, |
| 47 | EaseOutExpo, |
| 48 | } |
| 49 | |
| 50 | impl Animation { |
| 51 | pub fn new(from: f64, to: f64, initial_velocity: f64, config: niri_config::Animation) -> Self { |
| 52 | // Scale the velocity by slowdown to keep the touchpad gestures feeling right. |
| 53 | let initial_velocity = initial_velocity * ANIMATION_SLOWDOWN.load(Ordering::Relaxed); |
| 54 | |
| 55 | let mut rv = Self::ease(from, to, initial_velocity, 0, Curve::EaseOutCubic); |
| 56 | if config.off { |
| 57 | rv.is_off = true; |
| 58 | return rv; |
| 59 | } |
| 60 | |
| 61 | rv.replace_config(config); |
| 62 | rv |
| 63 | } |
| 64 | |
| 65 | pub fn replace_config(&mut self, config: niri_config::Animation) { |
| 66 | self.is_off = config.off; |
| 67 | if config.off { |
| 68 | self.duration = Duration::ZERO; |
| 69 | self.clamped_duration = Duration::ZERO; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | let start_time = self.start_time; |
| 74 | let current_time = self.current_time; |
| 75 | |
| 76 | match config.kind { |
| 77 | niri_config::AnimationKind::Spring(p) => { |
| 78 | let params = SpringParams::new(p.damping_ratio, f64::from(p.stiffness), p.epsilon); |
| 79 | |
| 80 | let spring = Spring { |
| 81 | from: self.from, |
| 82 | to: self.to, |
| 83 | initial_velocity: self.initial_velocity, |
| 84 | params, |
| 85 | }; |
| 86 | *self = Self::spring(spring); |
| 87 | } |
| 88 | niri_config::AnimationKind::Easing(p) => { |
| 89 | *self = Self::ease( |
| 90 | self.from, |
| 91 | self.to, |
| 92 | self.initial_velocity, |
| 93 | u64::from(p.duration_ms), |
| 94 | Curve::from(p.curve), |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | self.start_time = start_time; |
| 100 | self.current_time = current_time; |
| 101 | } |
| 102 | |
| 103 | /// Restarts the animation using the previous config. |
| 104 | pub fn restarted(&self, from: f64, to: f64, initial_velocity: f64) -> Self { |
| 105 | if self.is_off { |
| 106 | return self.clone(); |
| 107 | } |
| 108 | |
| 109 | // Scale the velocity by slowdown to keep the touchpad gestures feeling right. |
| 110 | let initial_velocity = initial_velocity * ANIMATION_SLOWDOWN.load(Ordering::Relaxed); |
| 111 | |
| 112 | match self.kind { |
| 113 | Kind::Easing { curve } => Self::ease( |
| 114 | from, |
| 115 | to, |
| 116 | initial_velocity, |
| 117 | self.duration.as_millis() as u64, |
| 118 | curve, |
| 119 | ), |
| 120 | Kind::Spring(spring) => { |
| 121 | let spring = Spring { |
| 122 | from: self.from, |
| 123 | to: self.to, |
| 124 | initial_velocity: self.initial_velocity, |
| 125 | params: spring.params, |
| 126 | }; |
| 127 | Self::spring(spring) |
| 128 | } |
| 129 | Kind::Deceleration { |
| 130 | initial_velocity, |
| 131 | deceleration_rate, |
| 132 | } => { |
| 133 | let threshold = 0.001; // FIXME |
| 134 | Self::decelerate(from, initial_velocity, deceleration_rate, threshold) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 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 | |
| 144 | let duration = Duration::from_millis(duration_ms); |
| 145 | let kind = Kind::Easing { curve }; |
| 146 | |
| 147 | Self { |
| 148 | from, |
| 149 | to, |
| 150 | initial_velocity, |
| 151 | is_off: false, |
| 152 | duration, |
| 153 | // Our current curves never overshoot. |
| 154 | clamped_duration: duration, |
| 155 | start_time: now, |
| 156 | current_time: now, |
| 157 | kind, |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | pub fn spring(spring: Spring) -> Self { |
| 162 | let _span = tracy_client::span!("Animation::spring"); |
| 163 | |
| 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 | |
| 168 | let duration = spring.duration(); |
| 169 | let clamped_duration = spring.clamped_duration().unwrap_or(duration); |
| 170 | let kind = Kind::Spring(spring); |
| 171 | |
| 172 | Self { |
| 173 | from: spring.from, |
| 174 | to: spring.to, |
| 175 | initial_velocity: spring.initial_velocity, |
| 176 | is_off: false, |
| 177 | duration, |
| 178 | clamped_duration, |
| 179 | start_time: now, |
| 180 | current_time: now, |
| 181 | kind, |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | pub fn decelerate( |
| 186 | from: f64, |
| 187 | initial_velocity: f64, |
| 188 | deceleration_rate: f64, |
| 189 | threshold: f64, |
| 190 | ) -> 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 | |
| 195 | let duration_s = if initial_velocity == 0. { |
| 196 | 0. |
| 197 | } else { |
| 198 | let coeff = 1000. * deceleration_rate.ln(); |
| 199 | (-coeff * threshold / initial_velocity.abs()).ln() / coeff |
| 200 | }; |
| 201 | let duration = Duration::from_secs_f64(duration_s); |
| 202 | |
| 203 | let to = from - initial_velocity / (1000. * deceleration_rate.ln()); |
| 204 | |
| 205 | let kind = Kind::Deceleration { |
| 206 | initial_velocity, |
| 207 | deceleration_rate, |
| 208 | }; |
| 209 | |
| 210 | Self { |
| 211 | from, |
| 212 | to, |
| 213 | initial_velocity, |
| 214 | is_off: false, |
| 215 | duration, |
| 216 | clamped_duration: duration, |
| 217 | start_time: now, |
| 218 | current_time: now, |
| 219 | kind, |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | pub fn set_current_time(&mut self, time: Duration) { |
| 224 | if self.duration.is_zero() { |
| 225 | self.current_time = time; |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | let end_time = self.start_time + self.duration; |
| 230 | if end_time <= self.current_time { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | let slowdown = ANIMATION_SLOWDOWN.load(Ordering::Relaxed); |
| 235 | if slowdown <= f64::EPSILON { |
| 236 | // Zero slowdown will cause the animation to end right away. |
| 237 | self.current_time = end_time; |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | // We can't change current_time (since the incoming time values are always real-time), so |
| 242 | // apply the slowdown by shifting the start time to compensate. |
| 243 | if self.current_time <= time { |
| 244 | let delta = time - self.current_time; |
| 245 | |
| 246 | let max_delta = end_time - self.current_time; |
| 247 | let min_slowdown = delta.as_secs_f64() / max_delta.as_secs_f64(); |
| 248 | if slowdown <= min_slowdown { |
| 249 | // Our slowdown value will cause the animation to end right away. |
| 250 | self.current_time = end_time; |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | let adjusted_delta = delta.div_f64(slowdown); |
| 255 | if adjusted_delta >= delta { |
| 256 | self.start_time -= adjusted_delta - delta; |
| 257 | } else { |
| 258 | self.start_time += delta - adjusted_delta; |
| 259 | } |
| 260 | } else { |
| 261 | let delta = self.current_time - time; |
| 262 | |
| 263 | let min_slowdown = delta.as_secs_f64() / self.current_time.as_secs_f64(); |
| 264 | if slowdown <= min_slowdown { |
| 265 | // Current time was about to jump to before the animation had started; let's just |
| 266 | // cancel the animation in this case. |
| 267 | self.current_time = end_time; |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | let adjusted_delta = delta.div_f64(slowdown); |
| 272 | if adjusted_delta >= delta { |
| 273 | self.start_time += adjusted_delta - delta; |
| 274 | } else { |
| 275 | self.start_time -= delta - adjusted_delta; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | self.current_time = time; |
| 280 | } |
| 281 | |
| 282 | pub fn is_done(&self) -> bool { |
| 283 | self.current_time >= self.start_time + self.duration |
| 284 | } |
| 285 | |
| 286 | pub fn is_clamped_done(&self) -> bool { |
| 287 | self.current_time >= self.start_time + self.clamped_duration |
| 288 | } |
| 289 | |
| 290 | pub fn value(&self) -> f64 { |
| 291 | if self.is_done() { |
| 292 | return self.to; |
| 293 | } |
| 294 | |
| 295 | let passed = self.current_time.saturating_sub(self.start_time); |
| 296 | |
| 297 | match self.kind { |
| 298 | Kind::Easing { curve } => { |
| 299 | let passed = passed.as_secs_f64(); |
| 300 | let total = self.duration.as_secs_f64(); |
| 301 | let x = (passed / total).clamp(0., 1.); |
| 302 | curve.y(x) * (self.to - self.from) + self.from |
| 303 | } |
| 304 | Kind::Spring(spring) => { |
| 305 | let value = spring.value_at(passed); |
| 306 | |
| 307 | // Protect against numerical instability. |
| 308 | let range = (self.to - self.from) * 10.; |
| 309 | let a = self.from - range; |
| 310 | let b = self.to + range; |
| 311 | if self.from <= self.to { |
| 312 | value.clamp(a, b) |
| 313 | } else { |
| 314 | value.clamp(b, a) |
| 315 | } |
| 316 | } |
| 317 | Kind::Deceleration { |
| 318 | initial_velocity, |
| 319 | deceleration_rate, |
| 320 | } => { |
| 321 | let passed = passed.as_secs_f64(); |
| 322 | let coeff = 1000. * deceleration_rate.ln(); |
| 323 | self.from + (deceleration_rate.powf(1000. * passed) - 1.) / coeff * initial_velocity |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /// Returns a value that stops at the target value after first reaching it. |
| 329 | /// |
| 330 | /// Best effort; not always exactly precise. |
| 331 | pub fn clamped_value(&self) -> f64 { |
| 332 | if self.is_clamped_done() { |
| 333 | return self.to; |
| 334 | } |
| 335 | |
| 336 | self.value() |
| 337 | } |
| 338 | |
| 339 | pub fn to(&self) -> f64 { |
| 340 | self.to |
| 341 | } |
| 342 | |
| 343 | #[cfg(test)] |
| 344 | pub fn from(&self) -> f64 { |
| 345 | self.from |
| 346 | } |
| 347 | |
| 348 | pub fn offset(&mut self, offset: f64) { |
| 349 | self.from += offset; |
| 350 | self.to += offset; |
| 351 | |
| 352 | if let Kind::Spring(spring) = &mut self.kind { |
| 353 | spring.from += offset; |
| 354 | spring.to += offset; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | impl Curve { |
| 360 | pub fn y(self, x: f64) -> f64 { |
| 361 | match self { |
| 362 | Curve::Linear => x, |
| 363 | Curve::EaseOutQuad => EaseOutQuad.y(x), |
| 364 | Curve::EaseOutCubic => EaseOutCubic.y(x), |
| 365 | Curve::EaseOutExpo => 1. - 2f64.powf(-10. * x), |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | impl From<niri_config::AnimationCurve> for Curve { |
| 371 | fn from(value: niri_config::AnimationCurve) -> Self { |
| 372 | match value { |
| 373 | niri_config::AnimationCurve::Linear => Curve::Linear, |
| 374 | niri_config::AnimationCurve::EaseOutQuad => Curve::EaseOutQuad, |
| 375 | niri_config::AnimationCurve::EaseOutCubic => Curve::EaseOutCubic, |
| 376 | niri_config::AnimationCurve::EaseOutExpo => Curve::EaseOutExpo, |
| 377 | } |
| 378 | } |
| 379 | } |