forge
Explore
Log in

Don't you dare go Hollow

Cubic-bezier curve for animation (#2059)

Browse files at this commit

1ffda91e0cc3938af1a9d4f1f1b6a87afa3c210fUnverifiedHoru and Ivan MolodetskikhParent: d9833fc
  • feat
    • cubic-bezier curve for animation (#2059)
Show full message
feat: cubic-bezier curve for animation (#2059)

* feat: bezier curve for animation

* fixes

---------
docs/wiki/Configuration:-Animations.mdmodified+13 -3
@@ -84,14 +84,24 @@ animations {
84 }84 }
85 ```85 ```
86 86
87-Currently, niri only supports four curves:87+Currently, niri only supports five curves.
88+You can get a feel for them on pages like [easings.net](https://easings.net/).
88 89
89 - `ease-out-quad` <sup>Since: 0.1.5</sup>90 - `ease-out-quad` <sup>Since: 0.1.5</sup>
90 - `ease-out-cubic`91 - `ease-out-cubic`
91 - `ease-out-expo`92 - `ease-out-expo`
92 - `linear` <sup>Since: 0.1.6</sup>93 - `linear` <sup>Since: 0.1.6</sup>
93-94+- `cubic-bezier` <sup>Since: next release</sup>
94-You can get a feel for them on pages like [easings.net](https://easings.net/).95+ A custom [cubic Bézier curve](https://www.w3.org/TR/css-easing-1/#cubic-bezier-easing-functions). You need to set 4 numbers defining the control points of the curve, for example:
96+ ```kdl
97+ animations {
98+ window-open {
99+ // Same as CSS cubic-bezier(0.05, 0.7, 0.1, 1)
100+ curve "cubic-bezier" 0.05 0.7 0.1 1
101+ }
102+ }
103+ ```
104+ You can tweak the cubic-bezier parameters on pages like [easings.co](https://easings.co?curve=0.05,0.7,0.1,1).
95 105
96 #### Spring106 #### Spring
97 107
niri-config/src/animations.rsmodified+90 -2
@@ -69,12 +69,13 @@ pub struct EasingParams {
69 pub curve: Curve,69 pub curve: Curve,
70 }70 }
71 71
72-#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq)]72+#[derive(Debug, Clone, Copy, PartialEq)]
73 pub enum Curve {73 pub enum Curve {
74 Linear,74 Linear,
75 EaseOutQuad,75 EaseOutQuad,
76 EaseOutCubic,76 EaseOutCubic,
77 EaseOutExpo,77 EaseOutExpo,
78+ CubicBezier(f64, f64, f64, f64),
78 }79 }
79 80
80 #[derive(Debug, Clone, Copy, PartialEq)]81 #[derive(Debug, Clone, Copy, PartialEq)]
@@ -540,7 +541,94 @@ impl Animation {
540 ));541 ));
541 }542 }
542 543
543- easing_params.curve = Some(parse_arg_node("curve", child, ctx)?);544+ let mut iter_args = child.arguments.iter();
545+ let val = iter_args.next().ok_or_else(|| {
546+ DecodeError::missing(child, "additional argument `curve` is required")
547+ })?;
548+ let animation_curve_string: String =
549+ knuffel::traits::DecodeScalar::decode(val, ctx)?;
550+
551+ let animation_curve = match animation_curve_string.as_str() {
552+ "linear" => Some(Curve::Linear),
553+ "ease-out-quad" => Some(Curve::EaseOutQuad),
554+ "ease-out-cubic" => Some(Curve::EaseOutCubic),
555+ "ease-out-expo" => Some(Curve::EaseOutExpo),
556+ "cubic-bezier" => {
557+ let val = iter_args.next().ok_or_else(|| {
558+ DecodeError::missing(
559+ child,
560+ "missing x1 coordinate for cubic Bézier curve control point",
561+ )
562+ })?;
563+ // the X axis represents time frame so it cannot be negative
564+ // or larger than 1
565+ let x1: FloatOrInt<0, 1> =
566+ knuffel::traits::DecodeScalar::decode(val, ctx)?;
567+ let val = iter_args.next().ok_or_else(|| {
568+ DecodeError::missing(
569+ child,
570+ "missing y1 coordinate for cubic Bézier curve control point",
571+ )
572+ })?;
573+ let y1: FloatOrInt<{ i32::MIN }, { i32::MAX }> =
574+ knuffel::traits::DecodeScalar::decode(val, ctx)?;
575+ let val = iter_args.next().ok_or_else(|| {
576+ DecodeError::missing(
577+ child,
578+ "missing x2 coordinate for cubic Bézier curve control point",
579+ )
580+ })?;
581+ let x2: FloatOrInt<0, 1> =
582+ knuffel::traits::DecodeScalar::decode(val, ctx)?;
583+ let val = iter_args.next().ok_or_else(|| {
584+ DecodeError::missing(
585+ child,
586+ "missing y2 coordinate for cubic Bézier curve control point",
587+ )
588+ })?;
589+ let y2: FloatOrInt<{ i32::MIN }, { i32::MAX }> =
590+ knuffel::traits::DecodeScalar::decode(val, ctx)?;
591+
592+ Some(Curve::CubicBezier(x1.0, y1.0, x2.0, y2.0))
593+ }
594+ unexpected_curve => {
595+ ctx.emit_error(DecodeError::unexpected(
596+ &val.literal,
597+ "argument",
598+ format!(
599+ "unexpected animation curve `{unexpected_curve}`. \
600+ Niri only supports five animation curves: \
601+ `ease-out-quad`, `ease-out-cubic`, `ease-out-expo`, `linear` and `cubic-bezier`."
602+ ),
603+ ));
604+
605+ None
606+ }
607+ };
608+
609+ if let Some(val) = iter_args.next() {
610+ ctx.emit_error(DecodeError::unexpected(
611+ &val.literal,
612+ "argument",
613+ "unexpected argument",
614+ ));
615+ }
616+ for name in child.properties.keys() {
617+ ctx.emit_error(DecodeError::unexpected(
618+ name,
619+ "property",
620+ format!("unexpected property `{}`", name.escape_default()),
621+ ));
622+ }
623+ for child in child.children() {
624+ ctx.emit_error(DecodeError::unexpected(
625+ child,
626+ "node",
627+ format!("unexpected node `{}`", child.node_name.escape_default()),
628+ ));
629+ }
630+
631+ easing_params.curve = animation_curve;
544 }632 }
545 name_str => {633 name_str => {
546 if !process_children(child, ctx)? {634 if !process_children(child, ctx)? {
niri-config/src/lib.rsmodified+10 -1
@@ -441,6 +441,10 @@ mod tests {
441 }441 }
442 442
443 window-open { off; }443 window-open { off; }
444+
445+ window-close {
446+ curve "cubic-bezier" 0.05 0.7 0.1 1
447+ }
444 }448 }
445 449
446 gestures {450 gestures {
@@ -1038,7 +1042,12 @@ mod tests {
1038 kind: Easing(1042 kind: Easing(
1039 EasingParams {1043 EasingParams {
1040 duration_ms: 150,1044 duration_ms: 150,
1041- curve: EaseOutQuad,1045+ curve: CubicBezier(
1046+ 0.05,
1047+ 0.7,
1048+ 0.1,
1049+ 1.0,
1050+ ),
1042 },1051 },
1043 ),1052 ),
1044 },1053 },
src/animation/bezier.rsadded+60 -0
@@ -0,0 +1,60 @@
1+use keyframe::EasingFunction;
2+
3+#[derive(Debug, Clone, Copy)]
4+pub struct CubicBezier {
5+ x1: f64,
6+ y1: f64,
7+ x2: f64,
8+ y2: f64,
9+}
10+
11+impl CubicBezier {
12+ pub fn new(x1: f64, y1: f64, x2: f64, y2: f64) -> Self {
13+ Self { x1, y1, x2, y2 }
14+ }
15+
16+ // Based on libadwaita (LGPL-2.1-or-later):
17+ // https://gitlab.gnome.org/GNOME/libadwaita/-/blob/1.7.6/src/adw-easing.c?ref_type=tags#L469-531
18+
19+ fn x_for_t(&self, t: f64) -> f64 {
20+ let omt = 1. - t;
21+ 3. * omt * omt * t * self.x1 + 3. * omt * t * t * self.x2 + t * t * t
22+ }
23+
24+ fn y_for_t(&self, t: f64) -> f64 {
25+ let omt = 1. - t;
26+ 3. * omt * omt * t * self.y1 + 3. * omt * t * t * self.y2 + t * t * t
27+ }
28+
29+ fn t_for_x(&self, x: f64) -> f64 {
30+ let mut min_t = 0.;
31+ let mut max_t = 1.;
32+
33+ for _ in 0..=30 {
34+ let guess_t = (min_t + max_t) / 2.;
35+ let guess_x = self.x_for_t(guess_t);
36+
37+ if x < guess_x {
38+ max_t = guess_t;
39+ } else {
40+ min_t = guess_t;
41+ }
42+ }
43+
44+ (min_t + max_t) / 2.
45+ }
46+}
47+
48+impl EasingFunction for CubicBezier {
49+ fn y(&self, x: f64) -> f64 {
50+ if x <= f64::EPSILON {
51+ return 0.;
52+ }
53+
54+ if 1. - f64::EPSILON <= x {
55+ return 1.;
56+ }
57+
58+ self.y_for_t(self.t_for_x(x))
59+ }
60+}
src/animation/mod.rsmodified+8 -0
@@ -3,6 +3,9 @@ use std::time::Duration;
3 use keyframe::functions::{EaseOutCubic, EaseOutQuad};3 use keyframe::functions::{EaseOutCubic, EaseOutQuad};
4 use keyframe::EasingFunction;4 use keyframe::EasingFunction;
5 5
6+mod bezier;
7+use bezier::CubicBezier;
8+
6 mod spring;9 mod spring;
7 pub use spring::{Spring, SpringParams};10 pub use spring::{Spring, SpringParams};
8 11
@@ -43,6 +46,7 @@ pub enum Curve {
43 EaseOutQuad,46 EaseOutQuad,
44 EaseOutCubic,47 EaseOutCubic,
45 EaseOutExpo,48 EaseOutExpo,
49+ CubicBezier(CubicBezier),
46 }50 }
47 51
48 impl Animation {52 impl Animation {
@@ -342,6 +346,7 @@ impl Curve {
342 Curve::EaseOutQuad => EaseOutQuad.y(x),346 Curve::EaseOutQuad => EaseOutQuad.y(x),
343 Curve::EaseOutCubic => EaseOutCubic.y(x),347 Curve::EaseOutCubic => EaseOutCubic.y(x),
344 Curve::EaseOutExpo => 1. - 2f64.powf(-10. * x),348 Curve::EaseOutExpo => 1. - 2f64.powf(-10. * x),
349+ Curve::CubicBezier(b) => b.y(x),
345 }350 }
346 }351 }
347 }352 }
@@ -353,6 +358,9 @@ impl From<niri_config::animations::Curve> for Curve {
353 niri_config::animations::Curve::EaseOutQuad => Curve::EaseOutQuad,358 niri_config::animations::Curve::EaseOutQuad => Curve::EaseOutQuad,
354 niri_config::animations::Curve::EaseOutCubic => Curve::EaseOutCubic,359 niri_config::animations::Curve::EaseOutCubic => Curve::EaseOutCubic,
355 niri_config::animations::Curve::EaseOutExpo => Curve::EaseOutExpo,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+ }
356 }364 }
357 }365 }
358 }366 }