Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update controls #45

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
46 changes: 46 additions & 0 deletions src/CrissCross.WPF.UI/Animations/Transition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <auto-generated>
// This file has been borrowed from Wpf-UI.
// </auto-generated>

// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

namespace Wpf.Ui.Animations;

/// <summary>
/// Available types of transitions.
/// </summary>
public enum Transition
{
/// <summary>
/// None.
/// </summary>
None,

/// <summary>
/// Change opacity.
/// </summary>
FadeIn,

/// <summary>
/// Change opacity and slide from bottom.
/// </summary>
FadeInWithSlide,

/// <summary>
/// Slide from bottom.
/// </summary>
SlideBottom,

/// <summary>
/// Slide from the right side.
/// </summary>
SlideRight,

/// <summary>
/// Slide from the left side.
/// </summary>
SlideLeft,
}
227 changes: 227 additions & 0 deletions src/CrissCross.WPF.UI/Animations/TransitionAnimationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
// <auto-generated>
// This file has been borrowed from Wpf-UI.
// </auto-generated>

// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Windows.Media.Animation;
using Wpf.Ui.Hardware;

namespace Wpf.Ui.Animations;

/// <summary>
/// Provides tools for <see cref="FrameworkElement"/> animation.
/// </summary>
/// <example>
/// <code lang="csharp">
/// TransitionAnimationProvider.ApplyTransition(MyFrameworkElement, Transition.FadeIn, 500);
/// </code>
/// </example>
public static class TransitionAnimationProvider
{
private const double DecelerationRatio = 0.7D;

/// <summary>
/// Attempts to apply an animation effect while adding content to the frame.
/// </summary>
/// <param name="element">Currently rendered element.</param>
/// <param name="type">Selected transition type.</param>
/// <param name="duration">Transition duration.</param>
/// <returns>Returns <see langword="true"/> if the transition was applied. Otherwise <see langword="false"/>.</returns>
public static bool ApplyTransition(object element, Transition type, int duration)
{
if (type == Transition.None)
{
return false;
}

// Disable transitions for non-accelerated devices.
if (!HardwareAcceleration.IsSupported(RenderingTier.PartialAcceleration))
{
return false;
}

if (element is not UIElement uiElement)
{
return false;
}

if (duration < 10)
{
return false;
}

if (duration > 10000)
{
duration = 10000;
}

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;
}

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 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);
}

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);
}
}
Loading
Loading