Skip to content

Commit

Permalink
Move ThemeAnimationHelper to Helper Folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Mar 19, 2023
1 parent 503c123 commit 3ab72ef
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 66 deletions.
1 change: 1 addition & 0 deletions src/Shared/HandyControlDemo_Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using HandyControl.Themes;
using HandyControl.Tools;
using HandyControl.Tools.Extension;
using HandyControl.Tools.Helper;
using HandyControlDemo.Data;
using HandyControlDemo.Properties.Langs;
using HandyControlDemo.Tools;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;

namespace HandyControl.Tools.Helper;
public static class ThemeAnimationHelper
{
public async static void AnimateTheme(UIElement element, SlideDirection slideDirection, double durationSeconds, double fromOpacity, double toOpacity)
{
if (element != null)
{
DoubleAnimation opacityAnimation = new DoubleAnimation();
opacityAnimation.From = fromOpacity;
opacityAnimation.To = toOpacity;
opacityAnimation.Duration = TimeSpan.FromSeconds(durationSeconds);

ThicknessAnimation slideAnimation = new ThicknessAnimation();
slideAnimation.Duration = TimeSpan.FromSeconds(durationSeconds);
slideAnimation.From = GetSlideFromThickness(element, slideDirection);
slideAnimation.To = new Thickness(0);

Storyboard storyboard = new Storyboard();
storyboard.Children.Add(opacityAnimation);
storyboard.Children.Add(slideAnimation);
Storyboard.SetTarget(opacityAnimation, element);
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(UIElement.OpacityProperty));
Storyboard.SetTarget(slideAnimation, element);
Storyboard.SetTargetProperty(slideAnimation, new PropertyPath(FrameworkElement.MarginProperty));

storyboard.Begin();
#if NET40
Thread.Sleep(TimeSpan.FromSeconds(durationSeconds));
#else
await Task.Delay(TimeSpan.FromSeconds(durationSeconds)); // Wait for the fade out animation to finish
#endif
}
}

public enum SlideDirection
{
Left,
Right,
Top,
Bottom
}

private static Thickness GetSlideFromThickness(UIElement element, SlideDirection slideDirection)
{
double elementWidth = element.RenderSize.Width;
double elementHeight = element.RenderSize.Height;

switch (slideDirection)
{
case SlideDirection.Left:
return new Thickness(-elementWidth, 0, elementWidth, 0);
case SlideDirection.Right:
return new Thickness(elementWidth, 0, -elementWidth, 0);
case SlideDirection.Top:
return new Thickness(0, -elementHeight, 0, elementHeight);
case SlideDirection.Bottom:
return new Thickness(0, elementHeight, 0, -elementHeight);
default:
return new Thickness();
}
}

}

0 comments on commit 3ab72ef

Please sign in to comment.