From f0574cbf1a570f6530eb0349967ce5740dccf9ef Mon Sep 17 00:00:00 2001 From: artyom Date: Wed, 29 May 2019 02:10:03 +0300 Subject: [PATCH] Use PageTransition, fix possible bug with CrossFade --- .../TransitioningContentControl.cs | 82 +++---------------- src/Avalonia.Visuals/Animation/CrossFade.cs | 8 +- .../RoutedViewHostTest.cs | 3 +- .../TransitioningContentControlTest.cs | 3 +- .../ViewModelViewHostTest.cs | 3 +- 5 files changed, 19 insertions(+), 80 deletions(-) diff --git a/src/Avalonia.ReactiveUI/TransitioningContentControl.cs b/src/Avalonia.ReactiveUI/TransitioningContentControl.cs index db8f3c964c..68d69bc95f 100644 --- a/src/Avalonia.ReactiveUI/TransitioningContentControl.cs +++ b/src/Avalonia.ReactiveUI/TransitioningContentControl.cs @@ -14,18 +14,11 @@ namespace Avalonia.ReactiveUI public class TransitioningContentControl : ContentControl, IStyleable { /// - /// for the property. + /// for the property. /// - public static readonly AvaloniaProperty FadeInAnimationProperty = - AvaloniaProperty.Register(nameof(DefaultContent), - CreateOpacityAnimation(0d, 1d, TimeSpan.FromSeconds(0.25))); - - /// - /// for the property. - /// - public static readonly AvaloniaProperty FadeOutAnimationProperty = - AvaloniaProperty.Register(nameof(DefaultContent), - CreateOpacityAnimation(1d, 0d, TimeSpan.FromSeconds(0.25))); + public static readonly AvaloniaProperty PageTransitionProperty = + AvaloniaProperty.Register(nameof(DefaultContent), + new CrossFade(TimeSpan.FromSeconds(0.5))); /// /// for the property. @@ -34,23 +27,14 @@ namespace Avalonia.ReactiveUI AvaloniaProperty.Register(nameof(DefaultContent)); /// - /// Gets or sets the animation played when content appears. + /// Gets or sets the animation played when content appears and disappears. /// - public IAnimation FadeInAnimation + public IPageTransition PageTransition { - get => GetValue(FadeInAnimationProperty); - set => SetValue(FadeInAnimationProperty, value); + get => GetValue(PageTransitionProperty); + set => SetValue(PageTransitionProperty, value); } - /// - /// Gets or sets the animation played when content disappears. - /// - public IAnimation FadeOutAnimation - { - get => GetValue(FadeOutAnimationProperty); - set => SetValue(FadeOutAnimationProperty, value); - } - /// /// Gets or sets the content displayed whenever there is no page currently routed. /// @@ -81,53 +65,11 @@ namespace Avalonia.ReactiveUI /// New content to set. private async void UpdateContentWithTransition(object content) { - if (FadeOutAnimation != null) - await FadeOutAnimation.RunAsync(this, Clock); + if (PageTransition != null) + await PageTransition.Start(this, null, true); base.Content = content; - if (FadeInAnimation != null) - await FadeInAnimation.RunAsync(this, Clock); - } - - /// - /// Creates opacity animation for this routed view host. - /// - /// Opacity to start from. - /// Opacity to finish with. - /// Duration of the animation. - /// Animation object instance. - private static IAnimation CreateOpacityAnimation(double from, double to, TimeSpan duration) - { - return new Avalonia.Animation.Animation - { - Duration = duration, - Children = - { - new KeyFrame - { - Setters = - { - new Setter - { - Property = OpacityProperty, - Value = from - } - }, - Cue = new Cue(0d) - }, - new KeyFrame - { - Setters = - { - new Setter - { - Property = OpacityProperty, - Value = to - } - }, - Cue = new Cue(1d) - } - } - }; + if (PageTransition != null) + await PageTransition.Start(null, this, true); } } } \ No newline at end of file diff --git a/src/Avalonia.Visuals/Animation/CrossFade.cs b/src/Avalonia.Visuals/Animation/CrossFade.cs index 6b8cb8b755..614d828259 100644 --- a/src/Avalonia.Visuals/Animation/CrossFade.cs +++ b/src/Avalonia.Visuals/Animation/CrossFade.cs @@ -14,8 +14,8 @@ namespace Avalonia.Animation /// public class CrossFade : IPageTransition { - private Animation _fadeOutAnimation; - private Animation _fadeInAnimation; + private readonly Animation _fadeOutAnimation; + private readonly Animation _fadeInAnimation; /// /// Initializes a new instance of the class. @@ -61,10 +61,10 @@ namespace Avalonia.Animation new Setter { Property = Visual.OpacityProperty, - Value = 0d + Value = 1d } }, - Cue = new Cue(0d) + Cue = new Cue(1d) } } diff --git a/tests/Avalonia.ReactiveUI.UnitTests/RoutedViewHostTest.cs b/tests/Avalonia.ReactiveUI.UnitTests/RoutedViewHostTest.cs index e873c60e36..8c5b5083e8 100644 --- a/tests/Avalonia.ReactiveUI.UnitTests/RoutedViewHostTest.cs +++ b/tests/Avalonia.ReactiveUI.UnitTests/RoutedViewHostTest.cs @@ -62,8 +62,7 @@ namespace Avalonia.ReactiveUI.UnitTests { Router = screen.Router, DefaultContent = defaultContent, - FadeOutAnimation = null, - FadeInAnimation = null + PageTransition = null }; var root = new TestRoot diff --git a/tests/Avalonia.ReactiveUI.UnitTests/TransitioningContentControlTest.cs b/tests/Avalonia.ReactiveUI.UnitTests/TransitioningContentControlTest.cs index 5ffcede5dd..f09eea5ec5 100644 --- a/tests/Avalonia.ReactiveUI.UnitTests/TransitioningContentControlTest.cs +++ b/tests/Avalonia.ReactiveUI.UnitTests/TransitioningContentControlTest.cs @@ -28,8 +28,7 @@ namespace Avalonia.ReactiveUI.UnitTests { var target = new TransitioningContentControl { - FadeInAnimation = null, - FadeOutAnimation = null, + PageTransition = null, Template = GetTemplate(), Content = "Foo" }; diff --git a/tests/Avalonia.ReactiveUI.UnitTests/ViewModelViewHostTest.cs b/tests/Avalonia.ReactiveUI.UnitTests/ViewModelViewHostTest.cs index a21bf34ef5..8bed5adcd4 100644 --- a/tests/Avalonia.ReactiveUI.UnitTests/ViewModelViewHostTest.cs +++ b/tests/Avalonia.ReactiveUI.UnitTests/ViewModelViewHostTest.cs @@ -33,8 +33,7 @@ namespace Avalonia.ReactiveUI.UnitTests var host = new ViewModelViewHost { DefaultContent = defaultContent, - FadeOutAnimation = null, - FadeInAnimation = null + PageTransition = null }; var root = new TestRoot