From 13eb3655714558ed3d66f34b83e1d6ee136b6ccf Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Mon, 10 Dec 2018 11:54:38 +0800 Subject: [PATCH] Fix SCBA. --- .../Animators/SolidColorBrushAnimator.cs | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs b/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs index 518d3d2ca7..c3de578959 100644 --- a/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs +++ b/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs @@ -13,53 +13,62 @@ namespace Avalonia.Animation.Animators { ColorAnimator colorAnimator; + void InitializeColorAnimator() + { + colorAnimator = new ColorAnimator(); + + foreach (AnimatorKeyFrame keyframe in this) + { + colorAnimator.Add(keyframe); + } + + colorAnimator.Property = SolidColorBrush.ColorProperty; + } + 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 + // Preprocess keyframe values to Color if the xaml parser converts them to ISCB. if (keyframe.Value.GetType() == typeof(ImmutableSolidColorBrush)) { keyframe.Value = ((ImmutableSolidColorBrush)keyframe.Value).Color; } } - // Make sure that the target property has SCB instead of the immutable one nor null. + // Add SCB if the target prop is empty. + if (control.GetValue(Property) == null) + control.SetValue(Property, new SolidColorBrush(Colors.Transparent)); var targetVal = control.GetValue(Property); - SolidColorBrush targetSCB = null; - - 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; - - control.SetValue(Property, targetSCB); - - if (colorAnimator == null) + // Continue if target prop is not empty & is a SolidColorBrush derivative. + if (typeof(ISolidColorBrush).IsAssignableFrom(targetVal.GetType())) { - colorAnimator = new ColorAnimator(); + if (colorAnimator == null) + InitializeColorAnimator(); + + SolidColorBrush finalTarget; - foreach (AnimatorKeyFrame keyframe in this) + // If it's ISCB, change it back to SCB. + if (targetVal.GetType() == typeof(ImmutableSolidColorBrush)) { - colorAnimator.Add(keyframe); + var col = (ImmutableSolidColorBrush)targetVal; + targetVal = new SolidColorBrush(col.Color); + control.SetValue(Property, targetVal); } - colorAnimator.Property = SolidColorBrush.ColorProperty; + finalTarget = targetVal as SolidColorBrush; + + return colorAnimator.Apply(animation, finalTarget, clock ?? control.Clock, match, onComplete); } - return colorAnimator.Apply(animation, targetSCB, clock ?? control.Clock, match, onComplete); + return Disposable.Empty; } public override SolidColorBrush Interpolate(double p, SolidColorBrush o, SolidColorBrush n) => null;