From 58c23945478c460dc20345944e069c3e006e0193 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Mon, 10 Dec 2018 01:00:18 +0800 Subject: [PATCH] Fix SCBAnimator to finally make color animations work. --- .../Animators/SolidColorBrushAnimator.cs | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs b/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs index ce2bf41fd6..43cc9d5476 100644 --- a/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs +++ b/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs @@ -14,56 +14,55 @@ namespace Avalonia.Animation.Animators { ColorAnimator colorAnimator; - void InitializeColorAnimator() - { - colorAnimator = new ColorAnimator(); - - foreach (AnimatorKeyFrame keyframe in this) - { - colorAnimator.Add(keyframe); - } - } - public override IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable match, Action onComplete) { var ctrl = (Visual)control; foreach (var keyframe in this) { + // Return if the keyframe value is not a SolidColorBrush if (keyframe.Value as ISolidColorBrush == null) + { return Disposable.Empty; + } + + // Preprocess values to Color if the xaml parser converts them to ISCB + if (keyframe.Value.GetType() == typeof(ImmutableSolidColorBrush)) + { + keyframe.Value = ((ImmutableSolidColorBrush)keyframe.Value).Color; + } } - if (control.GetValue(Property) == null) - control.SetValue(Property, new SolidColorBrush(Colors.Transparent)); + // Make sure that the target property has SCB instead of the immutable one nor null. var targetVal = control.GetValue(Property); - if (typeof(ISolidColorBrush).IsAssignableFrom(targetVal.GetType())) - { - if (colorAnimator == null) - InitializeColorAnimator(); + SolidColorBrush targetSCB = null; - SolidColorBrush finalTarget = new SolidColorBrush(); + if (targetVal == null) + targetSCB = new SolidColorBrush(Colors.Transparent); + else if (typeof(ISolidColorBrush).IsAssignableFrom(targetVal.GetType())) + targetSCB = new SolidColorBrush(((ISolidColorBrush)targetVal).Color); + else + return Disposable.Empty; - if (targetVal.GetType() == typeof(ImmutableSolidColorBrush)) - { - var k = (ImmutableSolidColorBrush)targetVal; - finalTarget.Color = k.Color; - } - else + control.SetValue(Property, targetSCB); + + if (colorAnimator == null) + { + colorAnimator = new ColorAnimator(); + + foreach (AnimatorKeyFrame keyframe in this) { - finalTarget = targetVal as SolidColorBrush; + colorAnimator.Add(keyframe); } colorAnimator.Property = SolidColorBrush.ColorProperty; - - return colorAnimator.Apply(animation, finalTarget, clock ?? control.Clock, match, onComplete); } - return Disposable.Empty; + return colorAnimator.Apply(animation, targetSCB, clock ?? control.Clock, match, onComplete); } public override SolidColorBrush Interpolate(double p, SolidColorBrush o, SolidColorBrush n) => null; } -} \ No newline at end of file +}