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