config: Extract animations
Browse files at this commitda5e33775cbb7a4e64189360b4eb00eab6dbb1b3Parent: 1e3ab8a2 changed files +671 -664
- niri-config/src/animations.rs +667 -0
- niri-config/src/lib.rs +4 -664
niri-config/src/animations.rsadded+667 -0
| @@ -0,0 +1,667 @@ | ||
| 1 | +use knuffel::errors::DecodeError; | |
| 2 | +use knuffel::Decode as _; | |
| 3 | + | |
| 4 | +use crate::{expect_only_children, parse_arg_node, FloatOrInt}; | |
| 5 | + | |
| 6 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq)] | |
| 7 | +pub struct Animations { | |
| 8 | + #[knuffel(child)] | |
| 9 | + pub off: bool, | |
| 10 | + #[knuffel(child, unwrap(argument), default = FloatOrInt(1.))] | |
| 11 | + pub slowdown: FloatOrInt<0, { i32::MAX }>, | |
| 12 | + #[knuffel(child, default)] | |
| 13 | + pub workspace_switch: WorkspaceSwitchAnim, | |
| 14 | + #[knuffel(child, default)] | |
| 15 | + pub window_open: WindowOpenAnim, | |
| 16 | + #[knuffel(child, default)] | |
| 17 | + pub window_close: WindowCloseAnim, | |
| 18 | + #[knuffel(child, default)] | |
| 19 | + pub horizontal_view_movement: HorizontalViewMovementAnim, | |
| 20 | + #[knuffel(child, default)] | |
| 21 | + pub window_movement: WindowMovementAnim, | |
| 22 | + #[knuffel(child, default)] | |
| 23 | + pub window_resize: WindowResizeAnim, | |
| 24 | + #[knuffel(child, default)] | |
| 25 | + pub config_notification_open_close: ConfigNotificationOpenCloseAnim, | |
| 26 | + #[knuffel(child, default)] | |
| 27 | + pub exit_confirmation_open_close: ExitConfirmationOpenCloseAnim, | |
| 28 | + #[knuffel(child, default)] | |
| 29 | + pub screenshot_ui_open: ScreenshotUiOpenAnim, | |
| 30 | + #[knuffel(child, default)] | |
| 31 | + pub overview_open_close: OverviewOpenCloseAnim, | |
| 32 | +} | |
| 33 | + | |
| 34 | +impl Default for Animations { | |
| 35 | + fn default() -> Self { | |
| 36 | + Self { | |
| 37 | + off: false, | |
| 38 | + slowdown: FloatOrInt(1.), | |
| 39 | + workspace_switch: Default::default(), | |
| 40 | + horizontal_view_movement: Default::default(), | |
| 41 | + window_movement: Default::default(), | |
| 42 | + window_open: Default::default(), | |
| 43 | + window_close: Default::default(), | |
| 44 | + window_resize: Default::default(), | |
| 45 | + config_notification_open_close: Default::default(), | |
| 46 | + exit_confirmation_open_close: Default::default(), | |
| 47 | + screenshot_ui_open: Default::default(), | |
| 48 | + overview_open_close: Default::default(), | |
| 49 | + } | |
| 50 | + } | |
| 51 | +} | |
| 52 | + | |
| 53 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 54 | +pub struct Animation { | |
| 55 | + pub off: bool, | |
| 56 | + pub kind: AnimationKind, | |
| 57 | +} | |
| 58 | + | |
| 59 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 60 | +pub enum AnimationKind { | |
| 61 | + Easing(EasingParams), | |
| 62 | + Spring(SpringParams), | |
| 63 | +} | |
| 64 | + | |
| 65 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 66 | +pub struct EasingParams { | |
| 67 | + pub duration_ms: u32, | |
| 68 | + pub curve: AnimationCurve, | |
| 69 | +} | |
| 70 | + | |
| 71 | +#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq)] | |
| 72 | +pub enum AnimationCurve { | |
| 73 | + Linear, | |
| 74 | + EaseOutQuad, | |
| 75 | + EaseOutCubic, | |
| 76 | + EaseOutExpo, | |
| 77 | +} | |
| 78 | + | |
| 79 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 80 | +pub struct SpringParams { | |
| 81 | + pub damping_ratio: f64, | |
| 82 | + pub stiffness: u32, | |
| 83 | + pub epsilon: f64, | |
| 84 | +} | |
| 85 | + | |
| 86 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 87 | +pub struct WorkspaceSwitchAnim(pub Animation); | |
| 88 | + | |
| 89 | +impl Default for WorkspaceSwitchAnim { | |
| 90 | + fn default() -> Self { | |
| 91 | + Self(Animation { | |
| 92 | + off: false, | |
| 93 | + kind: AnimationKind::Spring(SpringParams { | |
| 94 | + damping_ratio: 1., | |
| 95 | + stiffness: 1000, | |
| 96 | + epsilon: 0.0001, | |
| 97 | + }), | |
| 98 | + }) | |
| 99 | + } | |
| 100 | +} | |
| 101 | + | |
| 102 | +#[derive(Debug, Clone, PartialEq)] | |
| 103 | +pub struct WindowOpenAnim { | |
| 104 | + pub anim: Animation, | |
| 105 | + pub custom_shader: Option<String>, | |
| 106 | +} | |
| 107 | + | |
| 108 | +impl Default for WindowOpenAnim { | |
| 109 | + fn default() -> Self { | |
| 110 | + Self { | |
| 111 | + anim: Animation { | |
| 112 | + off: false, | |
| 113 | + kind: AnimationKind::Easing(EasingParams { | |
| 114 | + duration_ms: 150, | |
| 115 | + curve: AnimationCurve::EaseOutExpo, | |
| 116 | + }), | |
| 117 | + }, | |
| 118 | + custom_shader: None, | |
| 119 | + } | |
| 120 | + } | |
| 121 | +} | |
| 122 | + | |
| 123 | +#[derive(Debug, Clone, PartialEq)] | |
| 124 | +pub struct WindowCloseAnim { | |
| 125 | + pub anim: Animation, | |
| 126 | + pub custom_shader: Option<String>, | |
| 127 | +} | |
| 128 | + | |
| 129 | +impl Default for WindowCloseAnim { | |
| 130 | + fn default() -> Self { | |
| 131 | + Self { | |
| 132 | + anim: Animation { | |
| 133 | + off: false, | |
| 134 | + kind: AnimationKind::Easing(EasingParams { | |
| 135 | + duration_ms: 150, | |
| 136 | + curve: AnimationCurve::EaseOutQuad, | |
| 137 | + }), | |
| 138 | + }, | |
| 139 | + custom_shader: None, | |
| 140 | + } | |
| 141 | + } | |
| 142 | +} | |
| 143 | + | |
| 144 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 145 | +pub struct HorizontalViewMovementAnim(pub Animation); | |
| 146 | + | |
| 147 | +impl Default for HorizontalViewMovementAnim { | |
| 148 | + fn default() -> Self { | |
| 149 | + Self(Animation { | |
| 150 | + off: false, | |
| 151 | + kind: AnimationKind::Spring(SpringParams { | |
| 152 | + damping_ratio: 1., | |
| 153 | + stiffness: 800, | |
| 154 | + epsilon: 0.0001, | |
| 155 | + }), | |
| 156 | + }) | |
| 157 | + } | |
| 158 | +} | |
| 159 | + | |
| 160 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 161 | +pub struct WindowMovementAnim(pub Animation); | |
| 162 | + | |
| 163 | +impl Default for WindowMovementAnim { | |
| 164 | + fn default() -> Self { | |
| 165 | + Self(Animation { | |
| 166 | + off: false, | |
| 167 | + kind: AnimationKind::Spring(SpringParams { | |
| 168 | + damping_ratio: 1., | |
| 169 | + stiffness: 800, | |
| 170 | + epsilon: 0.0001, | |
| 171 | + }), | |
| 172 | + }) | |
| 173 | + } | |
| 174 | +} | |
| 175 | + | |
| 176 | +#[derive(Debug, Clone, PartialEq)] | |
| 177 | +pub struct WindowResizeAnim { | |
| 178 | + pub anim: Animation, | |
| 179 | + pub custom_shader: Option<String>, | |
| 180 | +} | |
| 181 | + | |
| 182 | +impl Default for WindowResizeAnim { | |
| 183 | + fn default() -> Self { | |
| 184 | + Self { | |
| 185 | + anim: Animation { | |
| 186 | + off: false, | |
| 187 | + kind: AnimationKind::Spring(SpringParams { | |
| 188 | + damping_ratio: 1., | |
| 189 | + stiffness: 800, | |
| 190 | + epsilon: 0.0001, | |
| 191 | + }), | |
| 192 | + }, | |
| 193 | + custom_shader: None, | |
| 194 | + } | |
| 195 | + } | |
| 196 | +} | |
| 197 | + | |
| 198 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 199 | +pub struct ConfigNotificationOpenCloseAnim(pub Animation); | |
| 200 | + | |
| 201 | +impl Default for ConfigNotificationOpenCloseAnim { | |
| 202 | + fn default() -> Self { | |
| 203 | + Self(Animation { | |
| 204 | + off: false, | |
| 205 | + kind: AnimationKind::Spring(SpringParams { | |
| 206 | + damping_ratio: 0.6, | |
| 207 | + stiffness: 1000, | |
| 208 | + epsilon: 0.001, | |
| 209 | + }), | |
| 210 | + }) | |
| 211 | + } | |
| 212 | +} | |
| 213 | + | |
| 214 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 215 | +pub struct ExitConfirmationOpenCloseAnim(pub Animation); | |
| 216 | + | |
| 217 | +impl Default for ExitConfirmationOpenCloseAnim { | |
| 218 | + fn default() -> Self { | |
| 219 | + Self(Animation { | |
| 220 | + off: false, | |
| 221 | + kind: AnimationKind::Spring(SpringParams { | |
| 222 | + damping_ratio: 0.6, | |
| 223 | + stiffness: 500, | |
| 224 | + epsilon: 0.01, | |
| 225 | + }), | |
| 226 | + }) | |
| 227 | + } | |
| 228 | +} | |
| 229 | + | |
| 230 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 231 | +pub struct ScreenshotUiOpenAnim(pub Animation); | |
| 232 | + | |
| 233 | +impl Default for ScreenshotUiOpenAnim { | |
| 234 | + fn default() -> Self { | |
| 235 | + Self(Animation { | |
| 236 | + off: false, | |
| 237 | + kind: AnimationKind::Easing(EasingParams { | |
| 238 | + duration_ms: 200, | |
| 239 | + curve: AnimationCurve::EaseOutQuad, | |
| 240 | + }), | |
| 241 | + }) | |
| 242 | + } | |
| 243 | +} | |
| 244 | + | |
| 245 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 246 | +pub struct OverviewOpenCloseAnim(pub Animation); | |
| 247 | + | |
| 248 | +impl Default for OverviewOpenCloseAnim { | |
| 249 | + fn default() -> Self { | |
| 250 | + Self(Animation { | |
| 251 | + off: false, | |
| 252 | + kind: AnimationKind::Spring(SpringParams { | |
| 253 | + damping_ratio: 1., | |
| 254 | + stiffness: 800, | |
| 255 | + epsilon: 0.0001, | |
| 256 | + }), | |
| 257 | + }) | |
| 258 | + } | |
| 259 | +} | |
| 260 | + | |
| 261 | +impl<S> knuffel::Decode<S> for WorkspaceSwitchAnim | |
| 262 | +where | |
| 263 | + S: knuffel::traits::ErrorSpan, | |
| 264 | +{ | |
| 265 | + fn decode_node( | |
| 266 | + node: &knuffel::ast::SpannedNode<S>, | |
| 267 | + ctx: &mut knuffel::decode::Context<S>, | |
| 268 | + ) -> Result<Self, DecodeError<S>> { | |
| 269 | + let default = Self::default().0; | |
| 270 | + Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 271 | + Ok(false) | |
| 272 | + })?)) | |
| 273 | + } | |
| 274 | +} | |
| 275 | + | |
| 276 | +impl<S> knuffel::Decode<S> for HorizontalViewMovementAnim | |
| 277 | +where | |
| 278 | + S: knuffel::traits::ErrorSpan, | |
| 279 | +{ | |
| 280 | + fn decode_node( | |
| 281 | + node: &knuffel::ast::SpannedNode<S>, | |
| 282 | + ctx: &mut knuffel::decode::Context<S>, | |
| 283 | + ) -> Result<Self, DecodeError<S>> { | |
| 284 | + let default = Self::default().0; | |
| 285 | + Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 286 | + Ok(false) | |
| 287 | + })?)) | |
| 288 | + } | |
| 289 | +} | |
| 290 | + | |
| 291 | +impl<S> knuffel::Decode<S> for WindowMovementAnim | |
| 292 | +where | |
| 293 | + S: knuffel::traits::ErrorSpan, | |
| 294 | +{ | |
| 295 | + fn decode_node( | |
| 296 | + node: &knuffel::ast::SpannedNode<S>, | |
| 297 | + ctx: &mut knuffel::decode::Context<S>, | |
| 298 | + ) -> Result<Self, DecodeError<S>> { | |
| 299 | + let default = Self::default().0; | |
| 300 | + Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 301 | + Ok(false) | |
| 302 | + })?)) | |
| 303 | + } | |
| 304 | +} | |
| 305 | + | |
| 306 | +impl<S> knuffel::Decode<S> for WindowOpenAnim | |
| 307 | +where | |
| 308 | + S: knuffel::traits::ErrorSpan, | |
| 309 | +{ | |
| 310 | + fn decode_node( | |
| 311 | + node: &knuffel::ast::SpannedNode<S>, | |
| 312 | + ctx: &mut knuffel::decode::Context<S>, | |
| 313 | + ) -> Result<Self, DecodeError<S>> { | |
| 314 | + let default = Self::default().anim; | |
| 315 | + let mut custom_shader = None; | |
| 316 | + let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | |
| 317 | + if &**child.node_name == "custom-shader" { | |
| 318 | + custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 319 | + Ok(true) | |
| 320 | + } else { | |
| 321 | + Ok(false) | |
| 322 | + } | |
| 323 | + })?; | |
| 324 | + | |
| 325 | + Ok(Self { | |
| 326 | + anim, | |
| 327 | + custom_shader, | |
| 328 | + }) | |
| 329 | + } | |
| 330 | +} | |
| 331 | + | |
| 332 | +impl<S> knuffel::Decode<S> for WindowCloseAnim | |
| 333 | +where | |
| 334 | + S: knuffel::traits::ErrorSpan, | |
| 335 | +{ | |
| 336 | + fn decode_node( | |
| 337 | + node: &knuffel::ast::SpannedNode<S>, | |
| 338 | + ctx: &mut knuffel::decode::Context<S>, | |
| 339 | + ) -> Result<Self, DecodeError<S>> { | |
| 340 | + let default = Self::default().anim; | |
| 341 | + let mut custom_shader = None; | |
| 342 | + let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | |
| 343 | + if &**child.node_name == "custom-shader" { | |
| 344 | + custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 345 | + Ok(true) | |
| 346 | + } else { | |
| 347 | + Ok(false) | |
| 348 | + } | |
| 349 | + })?; | |
| 350 | + | |
| 351 | + Ok(Self { | |
| 352 | + anim, | |
| 353 | + custom_shader, | |
| 354 | + }) | |
| 355 | + } | |
| 356 | +} | |
| 357 | + | |
| 358 | +impl<S> knuffel::Decode<S> for WindowResizeAnim | |
| 359 | +where | |
| 360 | + S: knuffel::traits::ErrorSpan, | |
| 361 | +{ | |
| 362 | + fn decode_node( | |
| 363 | + node: &knuffel::ast::SpannedNode<S>, | |
| 364 | + ctx: &mut knuffel::decode::Context<S>, | |
| 365 | + ) -> Result<Self, DecodeError<S>> { | |
| 366 | + let default = Self::default().anim; | |
| 367 | + let mut custom_shader = None; | |
| 368 | + let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | |
| 369 | + if &**child.node_name == "custom-shader" { | |
| 370 | + custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 371 | + Ok(true) | |
| 372 | + } else { | |
| 373 | + Ok(false) | |
| 374 | + } | |
| 375 | + })?; | |
| 376 | + | |
| 377 | + Ok(Self { | |
| 378 | + anim, | |
| 379 | + custom_shader, | |
| 380 | + }) | |
| 381 | + } | |
| 382 | +} | |
| 383 | + | |
| 384 | +impl<S> knuffel::Decode<S> for ConfigNotificationOpenCloseAnim | |
| 385 | +where | |
| 386 | + S: knuffel::traits::ErrorSpan, | |
| 387 | +{ | |
| 388 | + fn decode_node( | |
| 389 | + node: &knuffel::ast::SpannedNode<S>, | |
| 390 | + ctx: &mut knuffel::decode::Context<S>, | |
| 391 | + ) -> Result<Self, DecodeError<S>> { | |
| 392 | + let default = Self::default().0; | |
| 393 | + Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 394 | + Ok(false) | |
| 395 | + })?)) | |
| 396 | + } | |
| 397 | +} | |
| 398 | + | |
| 399 | +impl<S> knuffel::Decode<S> for ExitConfirmationOpenCloseAnim | |
| 400 | +where | |
| 401 | + S: knuffel::traits::ErrorSpan, | |
| 402 | +{ | |
| 403 | + fn decode_node( | |
| 404 | + node: &knuffel::ast::SpannedNode<S>, | |
| 405 | + ctx: &mut knuffel::decode::Context<S>, | |
| 406 | + ) -> Result<Self, DecodeError<S>> { | |
| 407 | + let default = Self::default().0; | |
| 408 | + Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 409 | + Ok(false) | |
| 410 | + })?)) | |
| 411 | + } | |
| 412 | +} | |
| 413 | + | |
| 414 | +impl<S> knuffel::Decode<S> for ScreenshotUiOpenAnim | |
| 415 | +where | |
| 416 | + S: knuffel::traits::ErrorSpan, | |
| 417 | +{ | |
| 418 | + fn decode_node( | |
| 419 | + node: &knuffel::ast::SpannedNode<S>, | |
| 420 | + ctx: &mut knuffel::decode::Context<S>, | |
| 421 | + ) -> Result<Self, DecodeError<S>> { | |
| 422 | + let default = Self::default().0; | |
| 423 | + Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 424 | + Ok(false) | |
| 425 | + })?)) | |
| 426 | + } | |
| 427 | +} | |
| 428 | + | |
| 429 | +impl<S> knuffel::Decode<S> for OverviewOpenCloseAnim | |
| 430 | +where | |
| 431 | + S: knuffel::traits::ErrorSpan, | |
| 432 | +{ | |
| 433 | + fn decode_node( | |
| 434 | + node: &knuffel::ast::SpannedNode<S>, | |
| 435 | + ctx: &mut knuffel::decode::Context<S>, | |
| 436 | + ) -> Result<Self, DecodeError<S>> { | |
| 437 | + let default = Self::default().0; | |
| 438 | + Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 439 | + Ok(false) | |
| 440 | + })?)) | |
| 441 | + } | |
| 442 | +} | |
| 443 | + | |
| 444 | +impl Animation { | |
| 445 | + pub fn new_off() -> Self { | |
| 446 | + Self { | |
| 447 | + off: true, | |
| 448 | + kind: AnimationKind::Easing(EasingParams { | |
| 449 | + duration_ms: 0, | |
| 450 | + curve: AnimationCurve::Linear, | |
| 451 | + }), | |
| 452 | + } | |
| 453 | + } | |
| 454 | + | |
| 455 | + fn decode_node<S: knuffel::traits::ErrorSpan>( | |
| 456 | + node: &knuffel::ast::SpannedNode<S>, | |
| 457 | + ctx: &mut knuffel::decode::Context<S>, | |
| 458 | + default: Self, | |
| 459 | + mut process_children: impl FnMut( | |
| 460 | + &knuffel::ast::SpannedNode<S>, | |
| 461 | + &mut knuffel::decode::Context<S>, | |
| 462 | + ) -> Result<bool, DecodeError<S>>, | |
| 463 | + ) -> Result<Self, DecodeError<S>> { | |
| 464 | + #[derive(Default, PartialEq)] | |
| 465 | + struct OptionalEasingParams { | |
| 466 | + duration_ms: Option<u32>, | |
| 467 | + curve: Option<AnimationCurve>, | |
| 468 | + } | |
| 469 | + | |
| 470 | + expect_only_children(node, ctx); | |
| 471 | + | |
| 472 | + let mut off = false; | |
| 473 | + let mut easing_params = OptionalEasingParams::default(); | |
| 474 | + let mut spring_params = None; | |
| 475 | + | |
| 476 | + for child in node.children() { | |
| 477 | + match &**child.node_name { | |
| 478 | + "off" => { | |
| 479 | + knuffel::decode::check_flag_node(child, ctx); | |
| 480 | + if off { | |
| 481 | + ctx.emit_error(DecodeError::unexpected( | |
| 482 | + &child.node_name, | |
| 483 | + "node", | |
| 484 | + "duplicate node `off`, single node expected", | |
| 485 | + )); | |
| 486 | + } else { | |
| 487 | + off = true; | |
| 488 | + } | |
| 489 | + } | |
| 490 | + "spring" => { | |
| 491 | + if easing_params != OptionalEasingParams::default() { | |
| 492 | + ctx.emit_error(DecodeError::unexpected( | |
| 493 | + child, | |
| 494 | + "node", | |
| 495 | + "cannot set both spring and easing parameters at once", | |
| 496 | + )); | |
| 497 | + } | |
| 498 | + if spring_params.is_some() { | |
| 499 | + ctx.emit_error(DecodeError::unexpected( | |
| 500 | + &child.node_name, | |
| 501 | + "node", | |
| 502 | + "duplicate node `spring`, single node expected", | |
| 503 | + )); | |
| 504 | + } | |
| 505 | + | |
| 506 | + spring_params = Some(SpringParams::decode_node(child, ctx)?); | |
| 507 | + } | |
| 508 | + "duration-ms" => { | |
| 509 | + if spring_params.is_some() { | |
| 510 | + ctx.emit_error(DecodeError::unexpected( | |
| 511 | + child, | |
| 512 | + "node", | |
| 513 | + "cannot set both spring and easing parameters at once", | |
| 514 | + )); | |
| 515 | + } | |
| 516 | + if easing_params.duration_ms.is_some() { | |
| 517 | + ctx.emit_error(DecodeError::unexpected( | |
| 518 | + &child.node_name, | |
| 519 | + "node", | |
| 520 | + "duplicate node `duration-ms`, single node expected", | |
| 521 | + )); | |
| 522 | + } | |
| 523 | + | |
| 524 | + easing_params.duration_ms = Some(parse_arg_node("duration-ms", child, ctx)?); | |
| 525 | + } | |
| 526 | + "curve" => { | |
| 527 | + if spring_params.is_some() { | |
| 528 | + ctx.emit_error(DecodeError::unexpected( | |
| 529 | + child, | |
| 530 | + "node", | |
| 531 | + "cannot set both spring and easing parameters at once", | |
| 532 | + )); | |
| 533 | + } | |
| 534 | + if easing_params.curve.is_some() { | |
| 535 | + ctx.emit_error(DecodeError::unexpected( | |
| 536 | + &child.node_name, | |
| 537 | + "node", | |
| 538 | + "duplicate node `curve`, single node expected", | |
| 539 | + )); | |
| 540 | + } | |
| 541 | + | |
| 542 | + easing_params.curve = Some(parse_arg_node("curve", child, ctx)?); | |
| 543 | + } | |
| 544 | + name_str => { | |
| 545 | + if !process_children(child, ctx)? { | |
| 546 | + ctx.emit_error(DecodeError::unexpected( | |
| 547 | + child, | |
| 548 | + "node", | |
| 549 | + format!("unexpected node `{}`", name_str.escape_default()), | |
| 550 | + )); | |
| 551 | + } | |
| 552 | + } | |
| 553 | + } | |
| 554 | + } | |
| 555 | + | |
| 556 | + let kind = if let Some(spring_params) = spring_params { | |
| 557 | + // Configured spring. | |
| 558 | + AnimationKind::Spring(spring_params) | |
| 559 | + } else if easing_params == OptionalEasingParams::default() { | |
| 560 | + // Did not configure anything. | |
| 561 | + default.kind | |
| 562 | + } else { | |
| 563 | + // Configured easing. | |
| 564 | + let default = if let AnimationKind::Easing(easing) = default.kind { | |
| 565 | + easing | |
| 566 | + } else { | |
| 567 | + // Generic fallback values for when the default animation is spring, but the user | |
| 568 | + // configured an easing animation. | |
| 569 | + EasingParams { | |
| 570 | + duration_ms: 250, | |
| 571 | + curve: AnimationCurve::EaseOutCubic, | |
| 572 | + } | |
| 573 | + }; | |
| 574 | + | |
| 575 | + AnimationKind::Easing(EasingParams { | |
| 576 | + duration_ms: easing_params.duration_ms.unwrap_or(default.duration_ms), | |
| 577 | + curve: easing_params.curve.unwrap_or(default.curve), | |
| 578 | + }) | |
| 579 | + }; | |
| 580 | + | |
| 581 | + Ok(Self { off, kind }) | |
| 582 | + } | |
| 583 | +} | |
| 584 | + | |
| 585 | +impl<S> knuffel::Decode<S> for SpringParams | |
| 586 | +where | |
| 587 | + S: knuffel::traits::ErrorSpan, | |
| 588 | +{ | |
| 589 | + fn decode_node( | |
| 590 | + node: &knuffel::ast::SpannedNode<S>, | |
| 591 | + ctx: &mut knuffel::decode::Context<S>, | |
| 592 | + ) -> Result<Self, DecodeError<S>> { | |
| 593 | + if let Some(type_name) = &node.type_name { | |
| 594 | + ctx.emit_error(DecodeError::unexpected( | |
| 595 | + type_name, | |
| 596 | + "type name", | |
| 597 | + "no type name expected for this node", | |
| 598 | + )); | |
| 599 | + } | |
| 600 | + if let Some(val) = node.arguments.first() { | |
| 601 | + ctx.emit_error(DecodeError::unexpected( | |
| 602 | + &val.literal, | |
| 603 | + "argument", | |
| 604 | + "unexpected argument", | |
| 605 | + )); | |
| 606 | + } | |
| 607 | + for child in node.children() { | |
| 608 | + ctx.emit_error(DecodeError::unexpected( | |
| 609 | + child, | |
| 610 | + "node", | |
| 611 | + format!("unexpected node `{}`", child.node_name.escape_default()), | |
| 612 | + )); | |
| 613 | + } | |
| 614 | + | |
| 615 | + let mut damping_ratio = None; | |
| 616 | + let mut stiffness = None; | |
| 617 | + let mut epsilon = None; | |
| 618 | + for (name, val) in &node.properties { | |
| 619 | + match &***name { | |
| 620 | + "damping-ratio" => { | |
| 621 | + damping_ratio = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?); | |
| 622 | + } | |
| 623 | + "stiffness" => { | |
| 624 | + stiffness = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?); | |
| 625 | + } | |
| 626 | + "epsilon" => { | |
| 627 | + epsilon = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?); | |
| 628 | + } | |
| 629 | + name_str => { | |
| 630 | + ctx.emit_error(DecodeError::unexpected( | |
| 631 | + name, | |
| 632 | + "property", | |
| 633 | + format!("unexpected property `{}`", name_str.escape_default()), | |
| 634 | + )); | |
| 635 | + } | |
| 636 | + } | |
| 637 | + } | |
| 638 | + let damping_ratio = damping_ratio | |
| 639 | + .ok_or_else(|| DecodeError::missing(node, "property `damping-ratio` is required"))?; | |
| 640 | + let stiffness = stiffness | |
| 641 | + .ok_or_else(|| DecodeError::missing(node, "property `stiffness` is required"))?; | |
| 642 | + let epsilon = | |
| 643 | + epsilon.ok_or_else(|| DecodeError::missing(node, "property `epsilon` is required"))?; | |
| 644 | + | |
| 645 | + if !(0.1..=10.).contains(&damping_ratio) { | |
| 646 | + ctx.emit_error(DecodeError::conversion( | |
| 647 | + node, | |
| 648 | + "damping-ratio must be between 0.1 and 10.0", | |
| 649 | + )); | |
| 650 | + } | |
| 651 | + if stiffness < 1 { | |
| 652 | + ctx.emit_error(DecodeError::conversion(node, "stiffness must be >= 1")); | |
| 653 | + } | |
| 654 | + if !(0.00001..=0.1).contains(&epsilon) { | |
| 655 | + ctx.emit_error(DecodeError::conversion( | |
| 656 | + node, | |
| 657 | + "epsilon must be between 0.00001 and 0.1", | |
| 658 | + )); | |
| 659 | + } | |
| 660 | + | |
| 661 | + Ok(SpringParams { | |
| 662 | + damping_ratio, | |
| 663 | + stiffness, | |
| 664 | + epsilon, | |
| 665 | + }) | |
| 666 | + } | |
| 667 | +} |
niri-config/src/lib.rsmodified+4 -664
| @@ -12,7 +12,6 @@ use std::time::Duration; | ||
| 12 | 12 | |
| 13 | 13 | use bitflags::bitflags; |
| 14 | 14 | use knuffel::errors::DecodeError; |
| 15 | -use knuffel::Decode as _; | |
| 16 | 15 | use miette::{miette, Context, IntoDiagnostic}; |
| 17 | 16 | use niri_ipc::{ |
| 18 | 17 | ColumnDisplay, ConfiguredMode, LayoutSwitchTarget, PositionChange, SizeChange, Transform, |
| @@ -27,10 +26,14 @@ use smithay::reexports::input; | ||
| 27 | 26 | pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.25, 0.25, 0.25, 1.]); |
| 28 | 27 | pub const DEFAULT_BACKDROP_COLOR: Color = Color::from_array_unpremul([0.15, 0.15, 0.15, 1.]); |
| 29 | 28 | |
| 29 | +pub mod animations; | |
| 30 | 30 | pub mod layer_rule; |
| 31 | 31 | pub mod utils; |
| 32 | 32 | pub mod window_rule; |
| 33 | 33 | |
| 34 | +pub use crate::animations::{ | |
| 35 | + Animation, AnimationCurve, AnimationKind, Animations, EasingParams, SpringParams, | |
| 36 | +}; | |
| 34 | 37 | pub use crate::layer_rule::LayerRule; |
| 35 | 38 | pub use crate::window_rule::{FloatingPosition, RelativeTo, WindowRule}; |
| 36 | 39 | |
| @@ -1089,261 +1092,6 @@ pub struct Clipboard { | ||
| 1089 | 1092 | pub disable_primary: bool, |
| 1090 | 1093 | } |
| 1091 | 1094 | |
| 1092 | -#[derive(knuffel::Decode, Debug, Clone, PartialEq)] | |
| 1093 | -pub struct Animations { | |
| 1094 | - #[knuffel(child)] | |
| 1095 | - pub off: bool, | |
| 1096 | - #[knuffel(child, unwrap(argument), default = FloatOrInt(1.))] | |
| 1097 | - pub slowdown: FloatOrInt<0, { i32::MAX }>, | |
| 1098 | - #[knuffel(child, default)] | |
| 1099 | - pub workspace_switch: WorkspaceSwitchAnim, | |
| 1100 | - #[knuffel(child, default)] | |
| 1101 | - pub window_open: WindowOpenAnim, | |
| 1102 | - #[knuffel(child, default)] | |
| 1103 | - pub window_close: WindowCloseAnim, | |
| 1104 | - #[knuffel(child, default)] | |
| 1105 | - pub horizontal_view_movement: HorizontalViewMovementAnim, | |
| 1106 | - #[knuffel(child, default)] | |
| 1107 | - pub window_movement: WindowMovementAnim, | |
| 1108 | - #[knuffel(child, default)] | |
| 1109 | - pub window_resize: WindowResizeAnim, | |
| 1110 | - #[knuffel(child, default)] | |
| 1111 | - pub config_notification_open_close: ConfigNotificationOpenCloseAnim, | |
| 1112 | - #[knuffel(child, default)] | |
| 1113 | - pub exit_confirmation_open_close: ExitConfirmationOpenCloseAnim, | |
| 1114 | - #[knuffel(child, default)] | |
| 1115 | - pub screenshot_ui_open: ScreenshotUiOpenAnim, | |
| 1116 | - #[knuffel(child, default)] | |
| 1117 | - pub overview_open_close: OverviewOpenCloseAnim, | |
| 1118 | -} | |
| 1119 | - | |
| 1120 | -impl Default for Animations { | |
| 1121 | - fn default() -> Self { | |
| 1122 | - Self { | |
| 1123 | - off: false, | |
| 1124 | - slowdown: FloatOrInt(1.), | |
| 1125 | - workspace_switch: Default::default(), | |
| 1126 | - horizontal_view_movement: Default::default(), | |
| 1127 | - window_movement: Default::default(), | |
| 1128 | - window_open: Default::default(), | |
| 1129 | - window_close: Default::default(), | |
| 1130 | - window_resize: Default::default(), | |
| 1131 | - config_notification_open_close: Default::default(), | |
| 1132 | - exit_confirmation_open_close: Default::default(), | |
| 1133 | - screenshot_ui_open: Default::default(), | |
| 1134 | - overview_open_close: Default::default(), | |
| 1135 | - } | |
| 1136 | - } | |
| 1137 | -} | |
| 1138 | - | |
| 1139 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1140 | -pub struct WorkspaceSwitchAnim(pub Animation); | |
| 1141 | - | |
| 1142 | -impl Default for WorkspaceSwitchAnim { | |
| 1143 | - fn default() -> Self { | |
| 1144 | - Self(Animation { | |
| 1145 | - off: false, | |
| 1146 | - kind: AnimationKind::Spring(SpringParams { | |
| 1147 | - damping_ratio: 1., | |
| 1148 | - stiffness: 1000, | |
| 1149 | - epsilon: 0.0001, | |
| 1150 | - }), | |
| 1151 | - }) | |
| 1152 | - } | |
| 1153 | -} | |
| 1154 | - | |
| 1155 | -#[derive(Debug, Clone, PartialEq)] | |
| 1156 | -pub struct WindowOpenAnim { | |
| 1157 | - pub anim: Animation, | |
| 1158 | - pub custom_shader: Option<String>, | |
| 1159 | -} | |
| 1160 | - | |
| 1161 | -impl Default for WindowOpenAnim { | |
| 1162 | - fn default() -> Self { | |
| 1163 | - Self { | |
| 1164 | - anim: Animation { | |
| 1165 | - off: false, | |
| 1166 | - kind: AnimationKind::Easing(EasingParams { | |
| 1167 | - duration_ms: 150, | |
| 1168 | - curve: AnimationCurve::EaseOutExpo, | |
| 1169 | - }), | |
| 1170 | - }, | |
| 1171 | - custom_shader: None, | |
| 1172 | - } | |
| 1173 | - } | |
| 1174 | -} | |
| 1175 | - | |
| 1176 | -#[derive(Debug, Clone, PartialEq)] | |
| 1177 | -pub struct WindowCloseAnim { | |
| 1178 | - pub anim: Animation, | |
| 1179 | - pub custom_shader: Option<String>, | |
| 1180 | -} | |
| 1181 | - | |
| 1182 | -impl Default for WindowCloseAnim { | |
| 1183 | - fn default() -> Self { | |
| 1184 | - Self { | |
| 1185 | - anim: Animation { | |
| 1186 | - off: false, | |
| 1187 | - kind: AnimationKind::Easing(EasingParams { | |
| 1188 | - duration_ms: 150, | |
| 1189 | - curve: AnimationCurve::EaseOutQuad, | |
| 1190 | - }), | |
| 1191 | - }, | |
| 1192 | - custom_shader: None, | |
| 1193 | - } | |
| 1194 | - } | |
| 1195 | -} | |
| 1196 | - | |
| 1197 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1198 | -pub struct HorizontalViewMovementAnim(pub Animation); | |
| 1199 | - | |
| 1200 | -impl Default for HorizontalViewMovementAnim { | |
| 1201 | - fn default() -> Self { | |
| 1202 | - Self(Animation { | |
| 1203 | - off: false, | |
| 1204 | - kind: AnimationKind::Spring(SpringParams { | |
| 1205 | - damping_ratio: 1., | |
| 1206 | - stiffness: 800, | |
| 1207 | - epsilon: 0.0001, | |
| 1208 | - }), | |
| 1209 | - }) | |
| 1210 | - } | |
| 1211 | -} | |
| 1212 | - | |
| 1213 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1214 | -pub struct WindowMovementAnim(pub Animation); | |
| 1215 | - | |
| 1216 | -impl Default for WindowMovementAnim { | |
| 1217 | - fn default() -> Self { | |
| 1218 | - Self(Animation { | |
| 1219 | - off: false, | |
| 1220 | - kind: AnimationKind::Spring(SpringParams { | |
| 1221 | - damping_ratio: 1., | |
| 1222 | - stiffness: 800, | |
| 1223 | - epsilon: 0.0001, | |
| 1224 | - }), | |
| 1225 | - }) | |
| 1226 | - } | |
| 1227 | -} | |
| 1228 | - | |
| 1229 | -#[derive(Debug, Clone, PartialEq)] | |
| 1230 | -pub struct WindowResizeAnim { | |
| 1231 | - pub anim: Animation, | |
| 1232 | - pub custom_shader: Option<String>, | |
| 1233 | -} | |
| 1234 | - | |
| 1235 | -impl Default for WindowResizeAnim { | |
| 1236 | - fn default() -> Self { | |
| 1237 | - Self { | |
| 1238 | - anim: Animation { | |
| 1239 | - off: false, | |
| 1240 | - kind: AnimationKind::Spring(SpringParams { | |
| 1241 | - damping_ratio: 1., | |
| 1242 | - stiffness: 800, | |
| 1243 | - epsilon: 0.0001, | |
| 1244 | - }), | |
| 1245 | - }, | |
| 1246 | - custom_shader: None, | |
| 1247 | - } | |
| 1248 | - } | |
| 1249 | -} | |
| 1250 | - | |
| 1251 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1252 | -pub struct ConfigNotificationOpenCloseAnim(pub Animation); | |
| 1253 | - | |
| 1254 | -impl Default for ConfigNotificationOpenCloseAnim { | |
| 1255 | - fn default() -> Self { | |
| 1256 | - Self(Animation { | |
| 1257 | - off: false, | |
| 1258 | - kind: AnimationKind::Spring(SpringParams { | |
| 1259 | - damping_ratio: 0.6, | |
| 1260 | - stiffness: 1000, | |
| 1261 | - epsilon: 0.001, | |
| 1262 | - }), | |
| 1263 | - }) | |
| 1264 | - } | |
| 1265 | -} | |
| 1266 | - | |
| 1267 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1268 | -pub struct ExitConfirmationOpenCloseAnim(pub Animation); | |
| 1269 | - | |
| 1270 | -impl Default for ExitConfirmationOpenCloseAnim { | |
| 1271 | - fn default() -> Self { | |
| 1272 | - Self(Animation { | |
| 1273 | - off: false, | |
| 1274 | - kind: AnimationKind::Spring(SpringParams { | |
| 1275 | - damping_ratio: 0.6, | |
| 1276 | - stiffness: 500, | |
| 1277 | - epsilon: 0.01, | |
| 1278 | - }), | |
| 1279 | - }) | |
| 1280 | - } | |
| 1281 | -} | |
| 1282 | - | |
| 1283 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1284 | -pub struct ScreenshotUiOpenAnim(pub Animation); | |
| 1285 | - | |
| 1286 | -impl Default for ScreenshotUiOpenAnim { | |
| 1287 | - fn default() -> Self { | |
| 1288 | - Self(Animation { | |
| 1289 | - off: false, | |
| 1290 | - kind: AnimationKind::Easing(EasingParams { | |
| 1291 | - duration_ms: 200, | |
| 1292 | - curve: AnimationCurve::EaseOutQuad, | |
| 1293 | - }), | |
| 1294 | - }) | |
| 1295 | - } | |
| 1296 | -} | |
| 1297 | - | |
| 1298 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1299 | -pub struct OverviewOpenCloseAnim(pub Animation); | |
| 1300 | - | |
| 1301 | -impl Default for OverviewOpenCloseAnim { | |
| 1302 | - fn default() -> Self { | |
| 1303 | - Self(Animation { | |
| 1304 | - off: false, | |
| 1305 | - kind: AnimationKind::Spring(SpringParams { | |
| 1306 | - damping_ratio: 1., | |
| 1307 | - stiffness: 800, | |
| 1308 | - epsilon: 0.0001, | |
| 1309 | - }), | |
| 1310 | - }) | |
| 1311 | - } | |
| 1312 | -} | |
| 1313 | - | |
| 1314 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1315 | -pub struct Animation { | |
| 1316 | - pub off: bool, | |
| 1317 | - pub kind: AnimationKind, | |
| 1318 | -} | |
| 1319 | - | |
| 1320 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1321 | -pub enum AnimationKind { | |
| 1322 | - Easing(EasingParams), | |
| 1323 | - Spring(SpringParams), | |
| 1324 | -} | |
| 1325 | - | |
| 1326 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1327 | -pub struct EasingParams { | |
| 1328 | - pub duration_ms: u32, | |
| 1329 | - pub curve: AnimationCurve, | |
| 1330 | -} | |
| 1331 | - | |
| 1332 | -#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq)] | |
| 1333 | -pub enum AnimationCurve { | |
| 1334 | - Linear, | |
| 1335 | - EaseOutQuad, | |
| 1336 | - EaseOutCubic, | |
| 1337 | - EaseOutExpo, | |
| 1338 | -} | |
| 1339 | - | |
| 1340 | -#[derive(Debug, Clone, Copy, PartialEq)] | |
| 1341 | -pub struct SpringParams { | |
| 1342 | - pub damping_ratio: f64, | |
| 1343 | - pub stiffness: u32, | |
| 1344 | - pub epsilon: f64, | |
| 1345 | -} | |
| 1346 | - | |
| 1347 | 1095 | #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)] |
| 1348 | 1096 | pub struct Gestures { |
| 1349 | 1097 | #[knuffel(child, default)] |
| @@ -3061,51 +2809,6 @@ fn parse_arg_node<S: knuffel::traits::ErrorSpan, T: knuffel::traits::DecodeScala | ||
| 3061 | 2809 | Ok(value) |
| 3062 | 2810 | } |
| 3063 | 2811 | |
| 3064 | -impl<S> knuffel::Decode<S> for WorkspaceSwitchAnim | |
| 3065 | -where | |
| 3066 | - S: knuffel::traits::ErrorSpan, | |
| 3067 | -{ | |
| 3068 | - fn decode_node( | |
| 3069 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3070 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3071 | - ) -> Result<Self, DecodeError<S>> { | |
| 3072 | - let default = Self::default().0; | |
| 3073 | - Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 3074 | - Ok(false) | |
| 3075 | - })?)) | |
| 3076 | - } | |
| 3077 | -} | |
| 3078 | - | |
| 3079 | -impl<S> knuffel::Decode<S> for HorizontalViewMovementAnim | |
| 3080 | -where | |
| 3081 | - S: knuffel::traits::ErrorSpan, | |
| 3082 | -{ | |
| 3083 | - fn decode_node( | |
| 3084 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3085 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3086 | - ) -> Result<Self, DecodeError<S>> { | |
| 3087 | - let default = Self::default().0; | |
| 3088 | - Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 3089 | - Ok(false) | |
| 3090 | - })?)) | |
| 3091 | - } | |
| 3092 | -} | |
| 3093 | - | |
| 3094 | -impl<S> knuffel::Decode<S> for WindowMovementAnim | |
| 3095 | -where | |
| 3096 | - S: knuffel::traits::ErrorSpan, | |
| 3097 | -{ | |
| 3098 | - fn decode_node( | |
| 3099 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3100 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3101 | - ) -> Result<Self, DecodeError<S>> { | |
| 3102 | - let default = Self::default().0; | |
| 3103 | - Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 3104 | - Ok(false) | |
| 3105 | - })?)) | |
| 3106 | - } | |
| 3107 | -} | |
| 3108 | - | |
| 3109 | 2812 | impl<S: knuffel::traits::ErrorSpan> knuffel::DecodeScalar<S> for WorkspaceName { |
| 3110 | 2813 | fn type_check( |
| 3111 | 2814 | type_name: &Option<knuffel::span::Spanned<knuffel::ast::TypeName, S>>, |
| @@ -3157,369 +2860,6 @@ impl<S: knuffel::traits::ErrorSpan> knuffel::DecodeScalar<S> for WorkspaceName { | ||
| 3157 | 2860 | } |
| 3158 | 2861 | } |
| 3159 | 2862 | |
| 3160 | -impl<S> knuffel::Decode<S> for WindowOpenAnim | |
| 3161 | -where | |
| 3162 | - S: knuffel::traits::ErrorSpan, | |
| 3163 | -{ | |
| 3164 | - fn decode_node( | |
| 3165 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3166 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3167 | - ) -> Result<Self, DecodeError<S>> { | |
| 3168 | - let default = Self::default().anim; | |
| 3169 | - let mut custom_shader = None; | |
| 3170 | - let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | |
| 3171 | - if &**child.node_name == "custom-shader" { | |
| 3172 | - custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 3173 | - Ok(true) | |
| 3174 | - } else { | |
| 3175 | - Ok(false) | |
| 3176 | - } | |
| 3177 | - })?; | |
| 3178 | - | |
| 3179 | - Ok(Self { | |
| 3180 | - anim, | |
| 3181 | - custom_shader, | |
| 3182 | - }) | |
| 3183 | - } | |
| 3184 | -} | |
| 3185 | - | |
| 3186 | -impl<S> knuffel::Decode<S> for WindowCloseAnim | |
| 3187 | -where | |
| 3188 | - S: knuffel::traits::ErrorSpan, | |
| 3189 | -{ | |
| 3190 | - fn decode_node( | |
| 3191 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3192 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3193 | - ) -> Result<Self, DecodeError<S>> { | |
| 3194 | - let default = Self::default().anim; | |
| 3195 | - let mut custom_shader = None; | |
| 3196 | - let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | |
| 3197 | - if &**child.node_name == "custom-shader" { | |
| 3198 | - custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 3199 | - Ok(true) | |
| 3200 | - } else { | |
| 3201 | - Ok(false) | |
| 3202 | - } | |
| 3203 | - })?; | |
| 3204 | - | |
| 3205 | - Ok(Self { | |
| 3206 | - anim, | |
| 3207 | - custom_shader, | |
| 3208 | - }) | |
| 3209 | - } | |
| 3210 | -} | |
| 3211 | - | |
| 3212 | -impl<S> knuffel::Decode<S> for WindowResizeAnim | |
| 3213 | -where | |
| 3214 | - S: knuffel::traits::ErrorSpan, | |
| 3215 | -{ | |
| 3216 | - fn decode_node( | |
| 3217 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3218 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3219 | - ) -> Result<Self, DecodeError<S>> { | |
| 3220 | - let default = Self::default().anim; | |
| 3221 | - let mut custom_shader = None; | |
| 3222 | - let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | |
| 3223 | - if &**child.node_name == "custom-shader" { | |
| 3224 | - custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 3225 | - Ok(true) | |
| 3226 | - } else { | |
| 3227 | - Ok(false) | |
| 3228 | - } | |
| 3229 | - })?; | |
| 3230 | - | |
| 3231 | - Ok(Self { | |
| 3232 | - anim, | |
| 3233 | - custom_shader, | |
| 3234 | - }) | |
| 3235 | - } | |
| 3236 | -} | |
| 3237 | - | |
| 3238 | -impl<S> knuffel::Decode<S> for ConfigNotificationOpenCloseAnim | |
| 3239 | -where | |
| 3240 | - S: knuffel::traits::ErrorSpan, | |
| 3241 | -{ | |
| 3242 | - fn decode_node( | |
| 3243 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3244 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3245 | - ) -> Result<Self, DecodeError<S>> { | |
| 3246 | - let default = Self::default().0; | |
| 3247 | - Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 3248 | - Ok(false) | |
| 3249 | - })?)) | |
| 3250 | - } | |
| 3251 | -} | |
| 3252 | - | |
| 3253 | -impl<S> knuffel::Decode<S> for ExitConfirmationOpenCloseAnim | |
| 3254 | -where | |
| 3255 | - S: knuffel::traits::ErrorSpan, | |
| 3256 | -{ | |
| 3257 | - fn decode_node( | |
| 3258 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3259 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3260 | - ) -> Result<Self, DecodeError<S>> { | |
| 3261 | - let default = Self::default().0; | |
| 3262 | - Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 3263 | - Ok(false) | |
| 3264 | - })?)) | |
| 3265 | - } | |
| 3266 | -} | |
| 3267 | - | |
| 3268 | -impl<S> knuffel::Decode<S> for ScreenshotUiOpenAnim | |
| 3269 | -where | |
| 3270 | - S: knuffel::traits::ErrorSpan, | |
| 3271 | -{ | |
| 3272 | - fn decode_node( | |
| 3273 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3274 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3275 | - ) -> Result<Self, DecodeError<S>> { | |
| 3276 | - let default = Self::default().0; | |
| 3277 | - Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 3278 | - Ok(false) | |
| 3279 | - })?)) | |
| 3280 | - } | |
| 3281 | -} | |
| 3282 | - | |
| 3283 | -impl<S> knuffel::Decode<S> for OverviewOpenCloseAnim | |
| 3284 | -where | |
| 3285 | - S: knuffel::traits::ErrorSpan, | |
| 3286 | -{ | |
| 3287 | - fn decode_node( | |
| 3288 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3289 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3290 | - ) -> Result<Self, DecodeError<S>> { | |
| 3291 | - let default = Self::default().0; | |
| 3292 | - Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | |
| 3293 | - Ok(false) | |
| 3294 | - })?)) | |
| 3295 | - } | |
| 3296 | -} | |
| 3297 | - | |
| 3298 | -impl Animation { | |
| 3299 | - pub fn new_off() -> Self { | |
| 3300 | - Self { | |
| 3301 | - off: true, | |
| 3302 | - kind: AnimationKind::Easing(EasingParams { | |
| 3303 | - duration_ms: 0, | |
| 3304 | - curve: AnimationCurve::Linear, | |
| 3305 | - }), | |
| 3306 | - } | |
| 3307 | - } | |
| 3308 | - | |
| 3309 | - fn decode_node<S: knuffel::traits::ErrorSpan>( | |
| 3310 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3311 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3312 | - default: Self, | |
| 3313 | - mut process_children: impl FnMut( | |
| 3314 | - &knuffel::ast::SpannedNode<S>, | |
| 3315 | - &mut knuffel::decode::Context<S>, | |
| 3316 | - ) -> Result<bool, DecodeError<S>>, | |
| 3317 | - ) -> Result<Self, DecodeError<S>> { | |
| 3318 | - #[derive(Default, PartialEq)] | |
| 3319 | - struct OptionalEasingParams { | |
| 3320 | - duration_ms: Option<u32>, | |
| 3321 | - curve: Option<AnimationCurve>, | |
| 3322 | - } | |
| 3323 | - | |
| 3324 | - expect_only_children(node, ctx); | |
| 3325 | - | |
| 3326 | - let mut off = false; | |
| 3327 | - let mut easing_params = OptionalEasingParams::default(); | |
| 3328 | - let mut spring_params = None; | |
| 3329 | - | |
| 3330 | - for child in node.children() { | |
| 3331 | - match &**child.node_name { | |
| 3332 | - "off" => { | |
| 3333 | - knuffel::decode::check_flag_node(child, ctx); | |
| 3334 | - if off { | |
| 3335 | - ctx.emit_error(DecodeError::unexpected( | |
| 3336 | - &child.node_name, | |
| 3337 | - "node", | |
| 3338 | - "duplicate node `off`, single node expected", | |
| 3339 | - )); | |
| 3340 | - } else { | |
| 3341 | - off = true; | |
| 3342 | - } | |
| 3343 | - } | |
| 3344 | - "spring" => { | |
| 3345 | - if easing_params != OptionalEasingParams::default() { | |
| 3346 | - ctx.emit_error(DecodeError::unexpected( | |
| 3347 | - child, | |
| 3348 | - "node", | |
| 3349 | - "cannot set both spring and easing parameters at once", | |
| 3350 | - )); | |
| 3351 | - } | |
| 3352 | - if spring_params.is_some() { | |
| 3353 | - ctx.emit_error(DecodeError::unexpected( | |
| 3354 | - &child.node_name, | |
| 3355 | - "node", | |
| 3356 | - "duplicate node `spring`, single node expected", | |
| 3357 | - )); | |
| 3358 | - } | |
| 3359 | - | |
| 3360 | - spring_params = Some(SpringParams::decode_node(child, ctx)?); | |
| 3361 | - } | |
| 3362 | - "duration-ms" => { | |
| 3363 | - if spring_params.is_some() { | |
| 3364 | - ctx.emit_error(DecodeError::unexpected( | |
| 3365 | - child, | |
| 3366 | - "node", | |
| 3367 | - "cannot set both spring and easing parameters at once", | |
| 3368 | - )); | |
| 3369 | - } | |
| 3370 | - if easing_params.duration_ms.is_some() { | |
| 3371 | - ctx.emit_error(DecodeError::unexpected( | |
| 3372 | - &child.node_name, | |
| 3373 | - "node", | |
| 3374 | - "duplicate node `duration-ms`, single node expected", | |
| 3375 | - )); | |
| 3376 | - } | |
| 3377 | - | |
| 3378 | - easing_params.duration_ms = Some(parse_arg_node("duration-ms", child, ctx)?); | |
| 3379 | - } | |
| 3380 | - "curve" => { | |
| 3381 | - if spring_params.is_some() { | |
| 3382 | - ctx.emit_error(DecodeError::unexpected( | |
| 3383 | - child, | |
| 3384 | - "node", | |
| 3385 | - "cannot set both spring and easing parameters at once", | |
| 3386 | - )); | |
| 3387 | - } | |
| 3388 | - if easing_params.curve.is_some() { | |
| 3389 | - ctx.emit_error(DecodeError::unexpected( | |
| 3390 | - &child.node_name, | |
| 3391 | - "node", | |
| 3392 | - "duplicate node `curve`, single node expected", | |
| 3393 | - )); | |
| 3394 | - } | |
| 3395 | - | |
| 3396 | - easing_params.curve = Some(parse_arg_node("curve", child, ctx)?); | |
| 3397 | - } | |
| 3398 | - name_str => { | |
| 3399 | - if !process_children(child, ctx)? { | |
| 3400 | - ctx.emit_error(DecodeError::unexpected( | |
| 3401 | - child, | |
| 3402 | - "node", | |
| 3403 | - format!("unexpected node `{}`", name_str.escape_default()), | |
| 3404 | - )); | |
| 3405 | - } | |
| 3406 | - } | |
| 3407 | - } | |
| 3408 | - } | |
| 3409 | - | |
| 3410 | - let kind = if let Some(spring_params) = spring_params { | |
| 3411 | - // Configured spring. | |
| 3412 | - AnimationKind::Spring(spring_params) | |
| 3413 | - } else if easing_params == OptionalEasingParams::default() { | |
| 3414 | - // Did not configure anything. | |
| 3415 | - default.kind | |
| 3416 | - } else { | |
| 3417 | - // Configured easing. | |
| 3418 | - let default = if let AnimationKind::Easing(easing) = default.kind { | |
| 3419 | - easing | |
| 3420 | - } else { | |
| 3421 | - // Generic fallback values for when the default animation is spring, but the user | |
| 3422 | - // configured an easing animation. | |
| 3423 | - EasingParams { | |
| 3424 | - duration_ms: 250, | |
| 3425 | - curve: AnimationCurve::EaseOutCubic, | |
| 3426 | - } | |
| 3427 | - }; | |
| 3428 | - | |
| 3429 | - AnimationKind::Easing(EasingParams { | |
| 3430 | - duration_ms: easing_params.duration_ms.unwrap_or(default.duration_ms), | |
| 3431 | - curve: easing_params.curve.unwrap_or(default.curve), | |
| 3432 | - }) | |
| 3433 | - }; | |
| 3434 | - | |
| 3435 | - Ok(Self { off, kind }) | |
| 3436 | - } | |
| 3437 | -} | |
| 3438 | - | |
| 3439 | -impl<S> knuffel::Decode<S> for SpringParams | |
| 3440 | -where | |
| 3441 | - S: knuffel::traits::ErrorSpan, | |
| 3442 | -{ | |
| 3443 | - fn decode_node( | |
| 3444 | - node: &knuffel::ast::SpannedNode<S>, | |
| 3445 | - ctx: &mut knuffel::decode::Context<S>, | |
| 3446 | - ) -> Result<Self, DecodeError<S>> { | |
| 3447 | - if let Some(type_name) = &node.type_name { | |
| 3448 | - ctx.emit_error(DecodeError::unexpected( | |
| 3449 | - type_name, | |
| 3450 | - "type name", | |
| 3451 | - "no type name expected for this node", | |
| 3452 | - )); | |
| 3453 | - } | |
| 3454 | - if let Some(val) = node.arguments.first() { | |
| 3455 | - ctx.emit_error(DecodeError::unexpected( | |
| 3456 | - &val.literal, | |
| 3457 | - "argument", | |
| 3458 | - "unexpected argument", | |
| 3459 | - )); | |
| 3460 | - } | |
| 3461 | - for child in node.children() { | |
| 3462 | - ctx.emit_error(DecodeError::unexpected( | |
| 3463 | - child, | |
| 3464 | - "node", | |
| 3465 | - format!("unexpected node `{}`", child.node_name.escape_default()), | |
| 3466 | - )); | |
| 3467 | - } | |
| 3468 | - | |
| 3469 | - let mut damping_ratio = None; | |
| 3470 | - let mut stiffness = None; | |
| 3471 | - let mut epsilon = None; | |
| 3472 | - for (name, val) in &node.properties { | |
| 3473 | - match &***name { | |
| 3474 | - "damping-ratio" => { | |
| 3475 | - damping_ratio = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?); | |
| 3476 | - } | |
| 3477 | - "stiffness" => { | |
| 3478 | - stiffness = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?); | |
| 3479 | - } | |
| 3480 | - "epsilon" => { | |
| 3481 | - epsilon = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?); | |
| 3482 | - } | |
| 3483 | - name_str => { | |
| 3484 | - ctx.emit_error(DecodeError::unexpected( | |
| 3485 | - name, | |
| 3486 | - "property", | |
| 3487 | - format!("unexpected property `{}`", name_str.escape_default()), | |
| 3488 | - )); | |
| 3489 | - } | |
| 3490 | - } | |
| 3491 | - } | |
| 3492 | - let damping_ratio = damping_ratio | |
| 3493 | - .ok_or_else(|| DecodeError::missing(node, "property `damping-ratio` is required"))?; | |
| 3494 | - let stiffness = stiffness | |
| 3495 | - .ok_or_else(|| DecodeError::missing(node, "property `stiffness` is required"))?; | |
| 3496 | - let epsilon = | |
| 3497 | - epsilon.ok_or_else(|| DecodeError::missing(node, "property `epsilon` is required"))?; | |
| 3498 | - | |
| 3499 | - if !(0.1..=10.).contains(&damping_ratio) { | |
| 3500 | - ctx.emit_error(DecodeError::conversion( | |
| 3501 | - node, | |
| 3502 | - "damping-ratio must be between 0.1 and 10.0", | |
| 3503 | - )); | |
| 3504 | - } | |
| 3505 | - if stiffness < 1 { | |
| 3506 | - ctx.emit_error(DecodeError::conversion(node, "stiffness must be >= 1")); | |
| 3507 | - } | |
| 3508 | - if !(0.00001..=0.1).contains(&epsilon) { | |
| 3509 | - ctx.emit_error(DecodeError::conversion( | |
| 3510 | - node, | |
| 3511 | - "epsilon must be between 0.00001 and 0.1", | |
| 3512 | - )); | |
| 3513 | - } | |
| 3514 | - | |
| 3515 | - Ok(SpringParams { | |
| 3516 | - damping_ratio, | |
| 3517 | - stiffness, | |
| 3518 | - epsilon, | |
| 3519 | - }) | |
| 3520 | - } | |
| 3521 | -} | |
| 3522 | - | |
| 3523 | 2863 | impl<S> knuffel::Decode<S> for CornerRadius |
| 3524 | 2864 | where |
| 3525 | 2865 | S: knuffel::traits::ErrorSpan, |