Bladeren bron

Merge pull request #6029 from MarchingCube/brush-transition

Transitions for solid color brushes.
Dariusz Komosiński 4 jaren geleden
bovenliggende
commit
257e7ed3f3

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

@@ -141,6 +141,39 @@
       <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>
+            <BrushTransition 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>
+
+      <Style Selector="Border.Rect11">
+        <Setter Property="Transitions">
+          <Transitions>
+            <BrushTransition Property="Background" Duration="0:0:0.5" />
+          </Transitions>
+        </Setter>
+        <Setter Property="Background" Value="Red" />
+      </Style>
+
+      <Style Selector="Border.Rect11:pointerover">
+        <Setter Property="Background" >
+          <LinearGradientBrush StartPoint="0%,0%" EndPoint="100%,100%">
+            <LinearGradientBrush.GradientStops>
+              <GradientStop Offset="0" Color="Red"/>
+              <GradientStop Offset="1" Color="Blue"/>
+            </LinearGradientBrush.GradientStops>
+          </LinearGradientBrush>
+        </Setter>
+      </Style>
     </Styles>
   </UserControl.Styles>
 
@@ -166,6 +199,9 @@
 
         <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" />
+        <Border Classes="Test Rect11" />
       </WrapPanel>
     </StackPanel>
   </Grid>

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

@@ -0,0 +1,59 @@
+using System;
+using Avalonia.Animation.Animators;
+using Avalonia.Animation.Easings;
+using Avalonia.Media;
+
+#nullable enable
+
+namespace Avalonia.Animation
+{
+    /// <summary>
+    /// Transition class that handles <see cref="AvaloniaProperty"/> with <see cref="IBrush"/> type.
+    /// Only values of <see cref="ISolidColorBrush"/> will transition correctly at the moment.
+    /// </summary>
+    public class BrushTransition : Transition<IBrush?>
+    {
+        private static readonly ISolidColorBrushAnimator s_animator = new ISolidColorBrushAnimator();
+
+        public override IObservable<IBrush?> DoTransition(IObservable<double> progress, IBrush? oldValue, IBrush? newValue)
+        {
+            var oldSolidColorBrush = TryGetSolidColorBrush(oldValue);
+            var newSolidColorBrush = TryGetSolidColorBrush(newValue);
+
+            if (oldSolidColorBrush != null && newSolidColorBrush != null)
+            {
+                return new AnimatorTransitionObservable<ISolidColorBrush, ISolidColorBrushAnimator>(
+                    s_animator, progress, Easing, oldSolidColorBrush, newSolidColorBrush);
+            }
+
+            return new IncompatibleTransitionObservable(progress, Easing, oldValue, newValue);
+        }
+
+        private static ISolidColorBrush? TryGetSolidColorBrush(IBrush? brush)
+        {
+            if (brush is null)
+            {
+                return Brushes.Transparent;
+            }
+
+            return brush as ISolidColorBrush;
+        }
+
+        private class IncompatibleTransitionObservable : TransitionObservableBase<IBrush?>
+        {
+            private readonly IBrush? _from;
+            private readonly IBrush? _to;
+
+            public IncompatibleTransitionObservable(IObservable<double> progress, Easing easing, IBrush? from, IBrush? to) : base(progress, easing)
+            {
+                _from = from;
+                _to = to;
+            }
+
+            protected override IBrush? ProduceValue(double progress)
+            {
+                return progress < 0.5 ? _from : _to;
+            }
+        }
+    }
+}