using WPFluent.Hardware; using System.Windows.Media.Animation; namespace WPFluent.Animations; /// /// Provides tools for animation. /// /// /// TransitionAnimationProvider.ApplyTransition(MyFrameworkElement, Transition.FadeIn, 500); /// public static class TransitionAnimationProvider { private const double DecelerationRatio = 0.7D; private static void FadeInTransition(UIElement animatedUiElement, Duration duration) { var opacityDoubleAnimation = new DoubleAnimation { Duration = duration, DecelerationRatio = DecelerationRatio, From = 0.0, To = 1.0, }; animatedUiElement.BeginAnimation(UIElement.OpacityProperty, opacityDoubleAnimation); } private static void FadeInWithSlideTransition(UIElement animatedUiElement, Duration duration) { var translateDoubleAnimation = new DoubleAnimation { Duration = duration, DecelerationRatio = DecelerationRatio, From = 30, To = 0, }; if(animatedUiElement.RenderTransform is not TranslateTransform) { animatedUiElement.SetCurrentValue(UIElement.RenderTransformProperty, new TranslateTransform(0, 0)); } if(!animatedUiElement.RenderTransformOrigin.Equals(new Point(0.5, 0.5))) { animatedUiElement.SetCurrentValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5)); } animatedUiElement.RenderTransform.BeginAnimation(TranslateTransform.YProperty, translateDoubleAnimation); var opacityDoubleAnimation = new DoubleAnimation { Duration = duration, DecelerationRatio = DecelerationRatio, From = 0.0, To = 1.0, }; animatedUiElement.BeginAnimation(UIElement.OpacityProperty, opacityDoubleAnimation); } private static void SlideBottomTransition(UIElement animatedUiElement, Duration duration) { var translateDoubleAnimation = new DoubleAnimation { Duration = duration, DecelerationRatio = DecelerationRatio, From = 30, To = 0, }; if(animatedUiElement.RenderTransform is not TranslateTransform) { animatedUiElement.SetCurrentValue(UIElement.RenderTransformProperty, new TranslateTransform(0, 0)); } if(!animatedUiElement.RenderTransformOrigin.Equals(new Point(0.5, 0.5))) { animatedUiElement.SetCurrentValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5)); } animatedUiElement.RenderTransform.BeginAnimation(TranslateTransform.YProperty, translateDoubleAnimation); } private static void SlideLeftTransition(UIElement animatedUiElement, Duration duration) { var translateDoubleAnimation = new DoubleAnimation { Duration = duration, DecelerationRatio = DecelerationRatio, From = -50, To = 0, }; if(animatedUiElement.RenderTransform is not TranslateTransform) { animatedUiElement.SetCurrentValue(UIElement.RenderTransformProperty, new TranslateTransform(0, 0)); } if(!animatedUiElement.RenderTransformOrigin.Equals(new Point(0.5, 0.5))) { animatedUiElement.SetCurrentValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5)); } animatedUiElement.RenderTransform.BeginAnimation(TranslateTransform.XProperty, translateDoubleAnimation); } private static void SlideRightTransition(UIElement animatedUiElement, Duration duration) { var translateDoubleAnimation = new DoubleAnimation { Duration = duration, DecelerationRatio = DecelerationRatio, From = 50, To = 0, }; if(animatedUiElement.RenderTransform is not TranslateTransform) { animatedUiElement.SetCurrentValue(UIElement.RenderTransformProperty, new TranslateTransform(0, 0)); } if(!animatedUiElement.RenderTransformOrigin.Equals(new Point(0.5, 0.5))) { animatedUiElement.SetCurrentValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5)); } animatedUiElement.RenderTransform.BeginAnimation(TranslateTransform.XProperty, translateDoubleAnimation); } /// /// Attempts to apply an animation effect while adding content to the frame. /// /// Currently rendered element. /// Selected transition type. /// Transition duration. /// Returns if the transition was applied. Otherwise . public static bool ApplyTransition(object? element, Transition type, int duration) { if(type == Transition.None || !HardwareAcceleration.IsSupported(RenderingTier.PartialAcceleration) || element is not UIElement uiElement || duration < 10) { return false; } duration = duration > 10000 ? 10000 : duration; var timespanDuration = new Duration(TimeSpan.FromMilliseconds(duration)); switch(type) { case Transition.FadeIn: FadeInTransition(uiElement, timespanDuration); break; case Transition.FadeInWithSlide: FadeInWithSlideTransition(uiElement, timespanDuration); break; case Transition.SlideBottom: SlideBottomTransition(uiElement, timespanDuration); break; case Transition.SlideRight: SlideRightTransition(uiElement, timespanDuration); break; case Transition.SlideLeft: SlideLeftTransition(uiElement, timespanDuration); break; default: return false; } return true; } }