Skip to main content

waft_animation/
tween.rs

1//! Flutter counterpart: `animation/tween.dart`.
2
3use std::fmt::{self, Debug};
4use std::marker::PhantomData;
5use std::rc::Rc;
6
7use waft_embedder::{Color, Offset, Rect, Size};
8use waft_foundation::{App, Handle, Listener, Ref};
9
10use crate::animation::{
11    Animation, AnimationRef, AnimationStatus, AnimationStatusListener, AnyAnimation,
12};
13use crate::curves::Curve;
14
15/// An object that can produce a value of type `T` given an `Animation<double>`
16/// as input.
17///
18/// Typically, the values of the input animation are nominally in the range 0.0
19/// to 1.0. In principle, however, any value could be provided.
20///
21/// The main implementor of [`Animatable`] is [`Tween`].
22pub trait Animatable<T> {
23    /// Returns the value of the object at point `t`.
24    ///
25    /// The value of `t` is nominally a fraction in the range 0.0 to 1.0,
26    /// though in practice it may extend outside this range.
27    fn transform(&self, app: &App, t: f64) -> T;
28
29    /// The current value of this object for the given animation.
30    ///
31    /// This function is implemented by deferring to [`transform`]. Implementors
32    /// that want to provide custom behavior should override [`transform`], not
33    /// [`evaluate`].
34    ///
35    /// [`transform`]: Animatable::transform
36    /// [`evaluate`]: Animatable::evaluate
37    fn evaluate(&self, app: &App, animation: AnyAnimation<f64>) -> T
38    where
39        Self: Sized,
40    {
41        self.transform(app, animation.value(app))
42    }
43
44    /// Returns a new animation that is driven by the given animation but
45    /// that takes on values determined by this object. The new animation keeps
46    /// `parent`; whoever keeps the result keeps both.
47    #[track_caller]
48    fn animate(self, app: &mut App, parent: AnimationRef<f64>) -> AnimationRef<T>
49    where
50        Self: Sized + Clone + 'static,
51        T: 'static,
52    {
53        app.create(AnimatedEvaluation {
54            parent,
55            evaluatable: self,
56            _value: PhantomData,
57        })
58        .into_erased()
59    }
60
61    /// Returns a new [`Animatable`] whose value is determined by first
62    /// evaluating the given parent and then evaluating this object at the
63    /// result.
64    fn chain<P>(self, parent: P) -> ChainedEvaluation<P, Self>
65    where
66        Self: Sized + Clone,
67        P: Animatable<f64> + Clone,
68    {
69        ChainedEvaluation {
70            parent,
71            evaluatable: self,
72        }
73    }
74}
75
76/// Dart's `_CallbackAnimatable<T>`.
77#[derive(Clone)]
78pub struct CallbackAnimatable<T> {
79    callback: Rc<dyn Fn(f64) -> T>,
80}
81
82impl<T> CallbackAnimatable<T> {
83    /// Dart's `Animatable.fromCallback`.
84    pub fn from_callback(callback: impl Fn(f64) -> T + 'static) -> CallbackAnimatable<T> {
85        CallbackAnimatable {
86            callback: Rc::new(callback),
87        }
88    }
89}
90
91impl<T> Animatable<T> for CallbackAnimatable<T> {
92    fn transform(&self, _app: &App, t: f64) -> T {
93        (self.callback)(t)
94    }
95}
96
97impl<T> Debug for CallbackAnimatable<T> {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        f.write_str("CallbackAnimatable")
100    }
101}
102
103/// Dart's `_AnimatedEvaluation<T>` — the animation returned by
104/// [`Animatable::animate`].
105struct AnimatedEvaluation<T, A> {
106    parent: AnimationRef<f64>,
107    evaluatable: A,
108    _value: PhantomData<fn() -> T>,
109}
110
111impl<T: 'static, A: Animatable<T> + Clone + 'static> Animation<T> for AnimatedEvaluation<T, A> {
112    fn add_listener(self: Handle<Self>, app: &mut App, listener: Listener) {
113        app.get(self).parent.handle().add_listener(app, listener);
114    }
115
116    fn remove_listener(self: Handle<Self>, app: &mut App, listener: &Listener) {
117        app.get(self).parent.handle().remove_listener(app, listener);
118    }
119
120    fn add_status_listener(self: Handle<Self>, app: &mut App, listener: AnimationStatusListener) {
121        app.get(self)
122            .parent
123            .handle()
124            .add_status_listener(app, listener);
125    }
126
127    fn remove_status_listener(
128        self: Handle<Self>,
129        app: &mut App,
130        listener: &AnimationStatusListener,
131    ) {
132        app.get(self)
133            .parent
134            .handle()
135            .remove_status_listener(app, listener);
136    }
137
138    fn status(self: Handle<Self>, app: &App) -> AnimationStatus {
139        app.get(self).parent.status(app)
140    }
141
142    fn value(self: Handle<Self>, app: &App) -> T {
143        let parent = app.get(self).parent.handle();
144        let evaluatable = app.get(self).evaluatable.clone();
145        evaluatable.evaluate(app, parent)
146    }
147}
148
149/// Dart's `_ChainedEvaluation<T>`.
150#[derive(Clone)]
151pub struct ChainedEvaluation<P, C> {
152    parent: P,
153    evaluatable: C,
154}
155
156impl<P: Animatable<f64>, C: Animatable<T>, T> Animatable<T> for ChainedEvaluation<P, C> {
157    fn transform(&self, app: &App, t: f64) -> T {
158        self.evaluatable
159            .transform(app, self.parent.transform(app, t))
160    }
161}
162
163impl<P: Debug, C: Debug> Debug for ChainedEvaluation<P, C> {
164    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165        write!(f, "{:?}\u{27A9}{:?}", self.parent, self.evaluatable)
166    }
167}
168
169/// Types that [`Tween<T>`] can interpolate with `+`, `-`, and `*`.
170///
171/// Dart's `Tween.lerp` is `begin + (end - begin) * t` on `dynamic`, and its assert rejects a
172/// type that does not carry the three operators (`Rect` is the type it names: use
173/// [`RectTween`]). This trait is the set of types that do.
174pub trait TweenLerp: Clone {
175    /// Dart's `Tween.lerp`.
176    fn lerp(begin: &Self, end: &Self, t: f64) -> Self;
177}
178
179impl TweenLerp for f64 {
180    fn lerp(begin: &f64, end: &f64, t: f64) -> f64 {
181        begin + (end - begin) * t
182    }
183}
184
185impl TweenLerp for Offset {
186    fn lerp(begin: &Offset, end: &Offset, t: f64) -> Offset {
187        *begin + (*end - *begin) * t
188    }
189}
190
191impl TweenLerp for Size {
192    fn lerp(begin: &Size, end: &Size, t: f64) -> Size {
193        // Dart: `Size - Size` is an `Offset`, and `Size + Offset` is a `Size`.
194        *begin + (*end - *begin) * t
195    }
196}
197
198/// A linear interpolation between a beginning and ending value.
199///
200/// Tweens are mutable: [`set_begin`] / [`set_end`] after [`Animatable::animate`]
201/// is visible to the driven animation, as in Dart.
202///
203/// [`set_begin`]: Tween::set_begin
204/// [`set_end`]: Tween::set_end
205#[derive(Debug)]
206pub struct Tween<T> {
207    /// The value this variable has at the beginning of the animation.
208    pub begin: Option<T>,
209
210    /// The value this variable has at the end of the animation.
211    pub end: Option<T>,
212}
213
214impl<T: 'static> Tween<T> {
215    /// Creates a tween.
216    ///
217    /// The [`begin`] and [`end`] properties must be non-null before the tween is
218    /// first used, but the arguments can be null if the values are going to be
219    /// filled in later.
220    ///
221    /// [`begin`]: Tween::begin
222    /// [`end`]: Tween::end
223    #[track_caller]
224    pub fn new(app: &mut App, begin: Option<T>, end: Option<T>) -> Ref<Tween<T>> {
225        app.create(Tween { begin, end })
226    }
227
228    /// The value this variable has at the beginning of the animation.
229    pub fn begin(self: Handle<Self>, app: &App) -> Option<&T> {
230        app.get(self).begin.as_ref()
231    }
232
233    /// Dart's `begin` setter.
234    pub fn set_begin(self: Handle<Self>, app: &mut App, begin: Option<T>) {
235        app.get_mut(self).begin = begin;
236    }
237
238    /// The value this variable has at the end of the animation.
239    pub fn end(self: Handle<Self>, app: &App) -> Option<&T> {
240        app.get(self).end.as_ref()
241    }
242
243    /// Dart's `end` setter.
244    pub fn set_end(self: Handle<Self>, app: &mut App, end: Option<T>) {
245        app.get_mut(self).end = end;
246    }
247}
248
249fn tween_transform<T: Clone>(
250    begin: &Option<T>,
251    end: &Option<T>,
252    t: f64,
253    lerp: impl FnOnce(f64) -> T,
254) -> T {
255    if t == 0.0 {
256        return begin.clone().expect("Tween.begin must be set before use");
257    }
258    if t == 1.0 {
259        return end.clone().expect("Tween.end must be set before use");
260    }
261    lerp(t)
262}
263
264impl<T: TweenLerp + 'static> Tween<T> {
265    /// Returns the value this variable has at the given animation clock value.
266    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> T {
267        let tween = app.get(self);
268        let begin = tween
269            .begin
270            .as_ref()
271            .expect("Tween.begin must be set before use");
272        let end = tween
273            .end
274            .as_ref()
275            .expect("Tween.end must be set before use");
276        T::lerp(begin, end, t)
277    }
278}
279
280impl<T: TweenLerp + 'static> Animatable<T> for Ref<Tween<T>> {
281    fn transform(&self, app: &App, t: f64) -> T {
282        let this = self.handle();
283        let tween = app.get(this);
284        tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
285    }
286}
287
288/// A [`Tween`] that evaluates its [`parent`] in reverse.
289///
290/// [`parent`]: ReverseTween::parent
291pub struct ReverseTween<T: 'static> {
292    parent: Ref<Tween<T>>,
293}
294
295impl<T: TweenLerp + 'static> ReverseTween<T> {
296    /// Construct a [`Tween`] that evaluates its parent in reverse.
297    #[track_caller]
298    pub fn new(app: &mut App, parent: Ref<Tween<T>>) -> Ref<ReverseTween<T>> {
299        app.create(ReverseTween { parent })
300    }
301
302    /// This tween's value is the same as the parent's value evaluated in reverse.
303    pub fn parent(self: Handle<Self>, app: &App) -> Handle<Tween<T>> {
304        app.get(self).parent.handle()
305    }
306}
307
308impl<T: TweenLerp + 'static> Animatable<T> for Ref<ReverseTween<T>> {
309    fn transform(&self, app: &App, t: f64) -> T {
310        let this = self.handle();
311        this.parent(app).lerp(app, 1.0 - t)
312    }
313}
314
315/// An interpolation between two colors.
316pub struct ColorTween {
317    /// The value this variable has at the beginning of the animation.
318    pub begin: Option<Color>,
319
320    /// The value this variable has at the end of the animation.
321    pub end: Option<Color>,
322}
323
324impl ColorTween {
325    /// Creates a [`Color`] tween.
326    ///
327    /// The begin and end properties may be null; the null value is treated as
328    /// transparent.
329    #[track_caller]
330    pub fn new(app: &mut App, begin: Option<Color>, end: Option<Color>) -> Ref<ColorTween> {
331        app.create(ColorTween { begin, end })
332    }
333
334    /// The value this variable has at the beginning of the animation.
335    pub fn begin(self: Handle<Self>, app: &App) -> Option<Color> {
336        app.get(self).begin
337    }
338
339    /// Dart's `begin` setter.
340    pub fn set_begin(self: Handle<Self>, app: &mut App, begin: Option<Color>) {
341        app.get_mut(self).begin = begin;
342    }
343
344    /// The value this variable has at the end of the animation.
345    pub fn end(self: Handle<Self>, app: &App) -> Option<Color> {
346        app.get(self).end
347    }
348
349    /// Dart's `end` setter.
350    pub fn set_end(self: Handle<Self>, app: &mut App, end: Option<Color>) {
351        app.get_mut(self).end = end;
352    }
353
354    /// Returns the value this variable has at the given animation clock value.
355    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Color> {
356        let tween = app.get(self);
357        Color::lerp(tween.begin, tween.end, t)
358    }
359}
360
361impl Animatable<Option<Color>> for Ref<ColorTween> {
362    fn transform(&self, app: &App, t: f64) -> Option<Color> {
363        let this = self.handle();
364        if t == 0.0 {
365            return this.begin(app);
366        }
367        if t == 1.0 {
368            return this.end(app);
369        }
370        this.lerp(app, t)
371    }
372}
373
374/// An interpolation between two sizes.
375pub struct SizeTween {
376    /// The value this variable has at the beginning of the animation.
377    pub begin: Option<Size>,
378
379    /// The value this variable has at the end of the animation.
380    pub end: Option<Size>,
381}
382
383impl SizeTween {
384    /// Creates a [`Size`] tween.
385    #[track_caller]
386    pub fn new(app: &mut App, begin: Option<Size>, end: Option<Size>) -> Ref<SizeTween> {
387        app.create(SizeTween { begin, end })
388    }
389
390    /// Returns the value this variable has at the given animation clock value.
391    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Size> {
392        let tween = app.get(self);
393        Size::lerp(tween.begin, tween.end, t)
394    }
395}
396
397impl Animatable<Option<Size>> for Ref<SizeTween> {
398    fn transform(&self, app: &App, t: f64) -> Option<Size> {
399        let this = self.handle();
400        let tween = app.get(this);
401        if t == 0.0 {
402            return tween.begin;
403        }
404        if t == 1.0 {
405            return tween.end;
406        }
407        this.lerp(app, t)
408    }
409}
410
411/// An interpolation between two rectangles.
412pub struct RectTween {
413    /// The value this variable has at the beginning of the animation.
414    pub begin: Option<Rect>,
415
416    /// The value this variable has at the end of the animation.
417    pub end: Option<Rect>,
418}
419
420impl RectTween {
421    /// Creates a [`Rect`] tween.
422    #[track_caller]
423    pub fn new(app: &mut App, begin: Option<Rect>, end: Option<Rect>) -> Ref<RectTween> {
424        app.create(RectTween { begin, end })
425    }
426
427    /// Returns the value this variable has at the given animation clock value.
428    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Rect> {
429        let tween = app.get(self);
430        Rect::lerp(tween.begin, tween.end, t)
431    }
432}
433
434impl Animatable<Option<Rect>> for Ref<RectTween> {
435    fn transform(&self, app: &App, t: f64) -> Option<Rect> {
436        let this = self.handle();
437        let tween = app.get(this);
438        if t == 0.0 {
439            return tween.begin;
440        }
441        if t == 1.0 {
442            return tween.end;
443        }
444        this.lerp(app, t)
445    }
446}
447
448/// An interpolation between two integers that rounds.
449pub struct IntTween {
450    /// The value this variable has at the beginning of the animation.
451    pub begin: Option<i64>,
452
453    /// The value this variable has at the end of the animation.
454    pub end: Option<i64>,
455}
456
457impl IntTween {
458    /// Creates an int tween.
459    #[track_caller]
460    pub fn new(app: &mut App, begin: Option<i64>, end: Option<i64>) -> Ref<IntTween> {
461        app.create(IntTween { begin, end })
462    }
463
464    /// Returns the interpolated integer, rounded.
465    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> i64 {
466        let tween = app.get(self);
467        let begin = tween.begin.expect("IntTween.begin must be set before use");
468        let end = tween.end.expect("IntTween.end must be set before use");
469        (begin as f64 + (end - begin) as f64 * t).round() as i64
470    }
471}
472
473impl Animatable<i64> for Ref<IntTween> {
474    fn transform(&self, app: &App, t: f64) -> i64 {
475        let this = self.handle();
476        let tween = app.get(this);
477        tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
478    }
479}
480
481/// An interpolation between two integers that floors.
482pub struct StepTween {
483    /// The value this variable has at the beginning of the animation.
484    pub begin: Option<i64>,
485
486    /// The value this variable has at the end of the animation.
487    pub end: Option<i64>,
488}
489
490impl StepTween {
491    /// Creates an [`i64`] tween that floors.
492    #[track_caller]
493    pub fn new(app: &mut App, begin: Option<i64>, end: Option<i64>) -> Ref<StepTween> {
494        app.create(StepTween { begin, end })
495    }
496
497    /// Returns the interpolated integer, floored.
498    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> i64 {
499        let tween = app.get(self);
500        let begin = tween.begin.expect("StepTween.begin must be set before use");
501        let end = tween.end.expect("StepTween.end must be set before use");
502        (begin as f64 + (end - begin) as f64 * t).floor() as i64
503    }
504}
505
506impl Animatable<i64> for Ref<StepTween> {
507    fn transform(&self, app: &App, t: f64) -> i64 {
508        let this = self.handle();
509        let tween = app.get(this);
510        tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
511    }
512}
513
514/// A tween with a constant value.
515pub struct ConstantTween<T> {
516    // Dart: `class ConstantTween<T> extends Tween<T>` — the superclass's
517    // fields, with `begin` and `end` both set to the value.
518    tween: Tween<T>,
519}
520
521impl<T: Clone + 'static> ConstantTween<T> {
522    /// Create a tween whose begin and end values equal `value`.
523    #[track_caller]
524    pub fn new(app: &mut App, value: T) -> Ref<ConstantTween<T>> {
525        app.create(ConstantTween {
526            tween: Tween {
527                begin: Some(value.clone()),
528                end: Some(value),
529            },
530        })
531    }
532}
533
534impl<T: Clone + 'static> Animatable<T> for Ref<ConstantTween<T>> {
535    fn transform(&self, app: &App, _t: f64) -> T {
536        app.get(self)
537            .tween
538            .begin
539            .clone()
540            .expect("ConstantTween.begin must be set")
541    }
542}
543
544/// Transforms the value of the given animation by the given curve.
545#[derive(Debug)]
546pub struct CurveTween {
547    /// The curve to use when transforming the value of the animation.
548    pub curve: Rc<dyn Curve>,
549}
550
551impl CurveTween {
552    /// Creates a curve tween.
553    #[track_caller]
554    pub fn new(app: &mut App, curve: Rc<dyn Curve>) -> Ref<CurveTween> {
555        app.create(CurveTween { curve })
556    }
557
558    /// The curve to use when transforming the value of the animation.
559    pub fn curve(self: Handle<Self>, app: &App) -> Rc<dyn Curve> {
560        Rc::clone(&app.get(self).curve)
561    }
562
563    /// Dart's `curve` setter.
564    pub fn set_curve(self: Handle<Self>, app: &mut App, curve: Rc<dyn Curve>) {
565        app.get_mut(self).curve = curve;
566    }
567}
568
569impl Animatable<f64> for Ref<CurveTween> {
570    fn transform(&self, app: &App, t: f64) -> f64 {
571        let curve = &app.get(self).curve;
572        if t == 0.0 || t == 1.0 {
573            debug_assert_eq!(curve.transform(t).round(), t);
574            return t;
575        }
576        curve.transform(t)
577    }
578}
579
580/// Dart's `Tween<T>` held as a value rather than an arena object: for a tween whose ends
581/// are fixed once it is made, as a Dart `static final` or `const` tween's are, or one a build
582/// makes and drops.
583///
584/// A driven animation holds a copy, so nothing frees it and nothing can change its ends; a
585/// tween whose `begin` or `end` is written after it is driven is a [`Tween`]
586/// (`patterns/value-tweens.md` in the porting skill).
587#[derive(Clone, Copy, Debug)]
588pub struct ValueTween<T> {
589    /// The value this variable has at the beginning of the animation.
590    pub begin: T,
591
592    /// The value this variable has at the end of the animation.
593    pub end: T,
594}
595
596impl<T> ValueTween<T> {
597    /// Creates a tween from `begin` to `end`.
598    pub const fn new(begin: T, end: T) -> ValueTween<T> {
599        ValueTween { begin, end }
600    }
601}
602
603impl<T: TweenLerp> ValueTween<T> {
604    /// Returns the value this variable has at the given animation clock value.
605    pub fn lerp(&self, t: f64) -> T {
606        T::lerp(&self.begin, &self.end, t)
607    }
608}
609
610impl<T: TweenLerp> Animatable<T> for ValueTween<T> {
611    fn transform(&self, _app: &App, t: f64) -> T {
612        if t == 0.0 {
613            return self.begin.clone();
614        }
615        if t == 1.0 {
616            return self.end.clone();
617        }
618        self.lerp(t)
619    }
620}
621
622/// Dart's `ConstantTween<T>` held as a value, for the tweens [`ValueTween`] is for.
623#[derive(Clone, Copy, Debug)]
624pub struct ValueConstantTween<T> {
625    /// The value at every point of the animation: Dart's `begin` and `end`.
626    pub value: T,
627}
628
629impl<T> ValueConstantTween<T> {
630    /// Create a tween whose begin and end values equal `value`.
631    pub const fn new(value: T) -> ValueConstantTween<T> {
632        ValueConstantTween { value }
633    }
634}
635
636impl<T: Clone> ValueConstantTween<T> {
637    /// Dart's `ConstantTween.lerp`: the value, whatever `t` is.
638    pub fn lerp(&self, _t: f64) -> T {
639        self.value.clone()
640    }
641}
642
643impl<T: Clone> Animatable<T> for ValueConstantTween<T> {
644    fn transform(&self, _app: &App, t: f64) -> T {
645        self.lerp(t)
646    }
647}
648
649/// Dart's `CurveTween` held as a value, for the tweens [`ValueTween`] is for.
650#[derive(Clone, Debug)]
651pub struct ValueCurveTween {
652    /// The curve to use when transforming the value of the animation.
653    pub curve: Rc<dyn Curve>,
654}
655
656impl ValueCurveTween {
657    /// Creates a curve tween.
658    pub fn new(curve: Rc<dyn Curve>) -> ValueCurveTween {
659        ValueCurveTween { curve }
660    }
661}
662
663impl Animatable<f64> for ValueCurveTween {
664    fn transform(&self, _app: &App, t: f64) -> f64 {
665        if t == 0.0 || t == 1.0 {
666            debug_assert_eq!(self.curve.transform(t).round(), t);
667            return t;
668        }
669        self.curve.transform(t)
670    }
671}
672
673#[cfg(test)]
674mod tests {
675    use super::*;
676    use crate::animations::AlwaysStoppedAnimation;
677    use crate::curves::SawTooth;
678    use waft_foundation::AppCell;
679
680    #[test]
681    fn a_driven_tween_sees_end_assigned_after_animate() {
682        let cell = AppCell::new();
683        let mut app = cell.borrow_mut();
684        let tween = Tween::new(&mut app, Some(0.0), Some(1.0));
685        let parent: AnimationRef<f64> = app.create(AlwaysStoppedAnimation::new(1.0)).into_erased();
686        let driven = tween.clone().animate(&mut app, parent);
687
688        assert_eq!(driven.value(&app), 1.0);
689        tween.set_end(&mut app, Some(0.4));
690        assert_eq!(driven.value(&app), 0.4);
691    }
692
693    #[test]
694    fn lerp_walks_the_line() {
695        let cell = AppCell::new();
696        let mut app = cell.borrow_mut();
697        let tween = Tween::new(&mut app, Some(0.0), Some(10.0));
698        assert_eq!(tween.transform(&app, 0.0), 0.0);
699        assert_eq!(tween.transform(&app, 0.25), 2.5);
700        assert_eq!(tween.transform(&app, 1.0), 10.0);
701    }
702
703    #[test]
704    fn an_offset_tween_walks_both_axes() {
705        let cell = AppCell::new();
706        let mut app = cell.borrow_mut();
707        let tween = Tween::new(
708            &mut app,
709            Some(Offset::new(0.0, 10.0)),
710            Some(Offset::new(4.0, -10.0)),
711        );
712        assert_eq!(tween.transform(&app, 0.0), Offset::new(0.0, 10.0));
713        assert_eq!(tween.transform(&app, 0.25), Offset::new(1.0, 5.0));
714        assert_eq!(tween.transform(&app, 1.0), Offset::new(4.0, -10.0));
715    }
716
717    #[test]
718    fn a_size_tween_walks_both_dimensions() {
719        let cell = AppCell::new();
720        let mut app = cell.borrow_mut();
721        let tween = Tween::new(
722            &mut app,
723            Some(Size::new(10.0, 20.0)),
724            Some(Size::new(20.0, 0.0)),
725        );
726        assert_eq!(tween.transform(&app, 0.5), Size::new(15.0, 10.0));
727    }
728
729    #[test]
730    fn a_value_tween_walks_the_line_without_an_arena_entry() {
731        let cell = AppCell::new();
732        let app = cell.borrow();
733        let tween = ValueTween::new(Offset::new(0.0, 10.0), Offset::new(4.0, -10.0));
734        assert_eq!(tween.transform(&app, 0.0), Offset::new(0.0, 10.0));
735        assert_eq!(tween.transform(&app, 0.25), Offset::new(1.0, 5.0));
736        assert_eq!(tween.transform(&app, 1.0), Offset::new(4.0, -10.0));
737    }
738
739    #[test]
740    fn a_value_tween_chained_onto_a_value_curve_tween_reads_the_curved_input() {
741        let cell = AppCell::new();
742        let app = cell.borrow();
743        let tween =
744            ValueTween::new(0.0, 10.0).chain(ValueCurveTween::new(Rc::new(SawTooth::new(2))));
745        assert_eq!(tween.transform(&app, 0.25), 5.0);
746        assert_eq!(tween.transform(&app, 0.75), 5.0);
747        assert_eq!(ValueConstantTween::new(3.0).transform(&app, 0.5), 3.0);
748    }
749}