Browse Source

Transitions for solid color brushes.

Dariusz Komosinski 4 years ago
parent
commit
5365b2fc95

+ 15 - 0
samples/RenderDemo/Pages/TransitionsPage.xaml

@@ -141,6 +141,19 @@
       <Style Selector="Border.Shadow:pointerover">
         <Setter Property="BoxShadow" Value="inset 30 30 20 30 Green, 20 40 20 10 Red"/>
       </Style>
+
+      <Style Selector="Border.Rect10">
+        <Setter Property="Transitions">
+          <Transitions>
+            <ISolidColorBrushTransition Property="Background" Duration="0:0:0.5" />
+          </Transitions>
+        </Setter>
+        <Setter Property="Background" Value="Red" />
+      </Style>
+
+      <Style Selector="Border.Rect10:pointerover">
+        <Setter Property="Background" Value="Orange" />
+      </Style>
     </Styles>
   </UserControl.Styles>
 
@@ -166,6 +179,8 @@
 
         <Border Classes="Test Shadow" CornerRadius="10" Child="{x:Null}" />
         <Border Classes="Test Shadow" CornerRadius="0 30 60 0" Child="{x:Null}" />
+
+        <Border Classes="Test Rect10" />
       </WrapPanel>
     </StackPanel>
   </Grid>

+ 29 - 0
src/Avalonia.Visuals/Animation/Transitions/BrushTransition.cs

@@ -0,0 +1,29 @@
+using System;
+using Avalonia.Animation.Animators;
+using Avalonia.Media;
+
+namespace Avalonia.Animation
+{
+    /// <summary>
+    /// Transition class that handles <see cref="AvaloniaProperty"/> with <see cref="IBrush"/> type.
+    /// Only values of <see cref="ISolidColorBrush"/> will correctly transition.
+    /// </summary>
+    public class ISolidColorBrushTransition : Transition<IBrush>
+    {
+        private static readonly ISolidColorBrushAnimator s_animator = new ISolidColorBrushAnimator();
+
+        public override IObservable<IBrush> DoTransition(IObservable<double> progress, IBrush oldValue, IBrush newValue)
+        {
+            var oldSolidBrush = AsImmutable(oldValue);
+            var newSolidBrush = AsImmutable(newValue);
+
+            return new AnimatorTransitionObservable<ISolidColorBrush, ISolidColorBrushAnimator>(
+                s_animator, progress, Easing, oldSolidBrush, newSolidBrush);
+        }
+
+        private static ISolidColorBrush AsImmutable(IBrush brush)
+        {
+            return (ISolidColorBrush)(brush as ISolidColorBrush)?.ToImmutable();
+        }
+    }
+}