723c0b9cdc
实现工具栏配置页面,允许用户调整工具栏按钮的顺序和可见性 包含主工具栏、画布控制和尾部按钮三个区域的配置 支持恢复默认布局功能
389 lines
14 KiB
C#
389 lines
14 KiB
C#
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls.Primitives;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Animation;
|
|
|
|
namespace Ink_Canvas.Helpers
|
|
{
|
|
internal class AnimationsHelper
|
|
{
|
|
private static UIElement ResolveAnimationTarget(UIElement element)
|
|
{
|
|
return element;
|
|
}
|
|
|
|
public static void ShowWithFadeIn(UIElement element, double duration = 0.15)
|
|
{
|
|
if (element.Visibility == Visibility.Visible) return;
|
|
|
|
if (element == null)
|
|
throw new ArgumentNullException(nameof(element));
|
|
|
|
var sb = new Storyboard();
|
|
|
|
// 渐变动画
|
|
var fadeInAnimation = new DoubleAnimation
|
|
{
|
|
From = 0.5,
|
|
To = 1,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
|
|
|
|
sb.Children.Add(fadeInAnimation);
|
|
|
|
element.Visibility = Visibility.Visible;
|
|
|
|
sb.Begin((FrameworkElement)element);
|
|
}
|
|
|
|
public static void ShowWithSlideFromBottomAndFade(UIElement element, double duration = 0.15)
|
|
{
|
|
try
|
|
{
|
|
if (element == null)
|
|
throw new ArgumentNullException(nameof(element));
|
|
|
|
if (element.Visibility == Visibility.Visible) return;
|
|
|
|
element.Visibility = Visibility.Visible;
|
|
|
|
var target = ResolveAnimationTarget(element);
|
|
|
|
var sb = new Storyboard();
|
|
|
|
var fadeInAnimation = new DoubleAnimation
|
|
{
|
|
From = 0.5,
|
|
To = 1,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
fadeInAnimation.EasingFunction = new CubicEase();
|
|
|
|
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
|
|
|
|
var slideAnimation = new DoubleAnimation
|
|
{
|
|
From = 10,
|
|
To = 0,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
|
|
|
|
slideAnimation.EasingFunction = new CubicEase();
|
|
|
|
sb.Children.Add(fadeInAnimation);
|
|
sb.Children.Add(slideAnimation);
|
|
|
|
target.RenderTransform = new TranslateTransform();
|
|
|
|
sb.Begin((FrameworkElement)target);
|
|
}
|
|
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
|
|
}
|
|
|
|
public static void ShowWithSlideFromLeftAndFade(UIElement element, double duration = 0.25)
|
|
{
|
|
try
|
|
{
|
|
if (element.Visibility == Visibility.Visible) return;
|
|
|
|
if (element == null)
|
|
throw new ArgumentNullException(nameof(element));
|
|
|
|
var sb = new Storyboard();
|
|
|
|
// 渐变动画
|
|
var fadeInAnimation = new DoubleAnimation
|
|
{
|
|
From = 0.5,
|
|
To = 1,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
|
|
|
|
// 滑动动画
|
|
var slideAnimation = new DoubleAnimation
|
|
{
|
|
From = element.RenderTransform.Value.OffsetX - 20, // 滑动距离
|
|
To = 0,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
|
|
|
|
sb.Children.Add(fadeInAnimation);
|
|
sb.Children.Add(slideAnimation);
|
|
|
|
element.Visibility = Visibility.Visible;
|
|
element.RenderTransform = new TranslateTransform();
|
|
|
|
sb.Begin((FrameworkElement)element);
|
|
}
|
|
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
|
|
}
|
|
|
|
public static void ShowWithScaleFromLeft(UIElement element, double duration = 0.2)
|
|
{
|
|
try
|
|
{
|
|
if (element.Visibility == Visibility.Visible) return;
|
|
|
|
if (element == null)
|
|
throw new ArgumentNullException(nameof(element));
|
|
|
|
var sb = new Storyboard();
|
|
|
|
// 水平方向的缩放动画
|
|
var scaleXAnimation = new DoubleAnimation
|
|
{
|
|
From = 0,
|
|
To = 1,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));
|
|
|
|
// 垂直方向的缩放动画
|
|
var scaleYAnimation = new DoubleAnimation
|
|
{
|
|
From = 0,
|
|
To = 1,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
scaleYAnimation.EasingFunction = new CubicEase();
|
|
scaleXAnimation.EasingFunction = new CubicEase();
|
|
Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)"));
|
|
|
|
sb.Children.Add(scaleXAnimation);
|
|
sb.Children.Add(scaleYAnimation);
|
|
|
|
element.Visibility = Visibility.Visible;
|
|
element.RenderTransformOrigin = new Point(0, 0.5); // 左侧中心点为基准
|
|
element.RenderTransform = new ScaleTransform(0, 0);
|
|
|
|
sb.Begin((FrameworkElement)element);
|
|
}
|
|
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
|
|
}
|
|
|
|
public static void ShowWithScaleFromRight(UIElement element, double duration = 0.2)
|
|
{
|
|
try
|
|
{
|
|
if (element.Visibility == Visibility.Visible) return;
|
|
|
|
if (element == null)
|
|
throw new ArgumentNullException(nameof(element));
|
|
|
|
var sb = new Storyboard();
|
|
|
|
// 水平方向的缩放动画
|
|
var scaleXAnimation = new DoubleAnimation
|
|
{
|
|
From = 0,
|
|
To = 1,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));
|
|
|
|
// 垂直方向的缩放动画
|
|
var scaleYAnimation = new DoubleAnimation
|
|
{
|
|
From = 0,
|
|
To = 1,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)"));
|
|
|
|
scaleYAnimation.EasingFunction = new CubicEase();
|
|
scaleXAnimation.EasingFunction = new CubicEase();
|
|
|
|
sb.Children.Add(scaleXAnimation);
|
|
sb.Children.Add(scaleYAnimation);
|
|
|
|
element.Visibility = Visibility.Visible;
|
|
element.RenderTransformOrigin = new Point(1, 0.5); // 右侧中心点为基准
|
|
element.RenderTransform = new ScaleTransform(0, 0);
|
|
|
|
sb.Begin((FrameworkElement)element);
|
|
}
|
|
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
|
|
}
|
|
|
|
public static void HideWithSlideAndFade(UIElement element, double duration = 0.15)
|
|
{
|
|
try
|
|
{
|
|
if (element == null)
|
|
throw new ArgumentNullException(nameof(element));
|
|
|
|
if (element.Visibility == Visibility.Collapsed) return;
|
|
|
|
var target = ResolveAnimationTarget(element);
|
|
|
|
var sb = new Storyboard();
|
|
|
|
var fadeOutAnimation = new DoubleAnimation
|
|
{
|
|
From = 1,
|
|
To = 0,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
fadeOutAnimation.EasingFunction = new CubicEase();
|
|
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(UIElement.OpacityProperty));
|
|
|
|
var slideAnimation = new DoubleAnimation
|
|
{
|
|
From = 0,
|
|
To = 10,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
slideAnimation.EasingFunction = new CubicEase();
|
|
|
|
Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
|
|
|
|
sb.Children.Add(fadeOutAnimation);
|
|
sb.Children.Add(slideAnimation);
|
|
|
|
sb.Completed += (s, e) =>
|
|
{
|
|
element.Visibility = Visibility.Collapsed;
|
|
};
|
|
|
|
target.RenderTransform = new TranslateTransform();
|
|
sb.Begin((FrameworkElement)target);
|
|
}
|
|
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
|
|
}
|
|
|
|
public static void HideWithFadeOut(UIElement element, double duration = 0.15)
|
|
{
|
|
if (element.Visibility == Visibility.Collapsed) return;
|
|
|
|
if (element == null)
|
|
throw new ArgumentNullException(nameof(element));
|
|
|
|
var sb = new Storyboard();
|
|
|
|
var fadeOutAnimation = new DoubleAnimation
|
|
{
|
|
From = 1,
|
|
To = 0,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(UIElement.OpacityProperty));
|
|
|
|
sb.Children.Add(fadeOutAnimation);
|
|
|
|
sb.Completed += (s, e) =>
|
|
{
|
|
element.Visibility = Visibility.Collapsed;
|
|
};
|
|
|
|
sb.Begin((FrameworkElement)element);
|
|
}
|
|
|
|
public static void ShowPopupWithSlideAndFade(Popup popup, double duration = 0.15)
|
|
{
|
|
try
|
|
{
|
|
if (popup == null)
|
|
throw new ArgumentNullException(nameof(popup));
|
|
|
|
if (popup.IsOpen) return;
|
|
|
|
var child = popup.Child as FrameworkElement;
|
|
if (child == null)
|
|
{
|
|
popup.IsOpen = true;
|
|
return;
|
|
}
|
|
|
|
child.Opacity = 0.5;
|
|
child.RenderTransform = new TranslateTransform(0, 10);
|
|
|
|
popup.IsOpen = true;
|
|
|
|
var sb = new Storyboard();
|
|
|
|
var fadeInAnimation = new DoubleAnimation
|
|
{
|
|
From = 0.5,
|
|
To = 1,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
fadeInAnimation.EasingFunction = new CubicEase();
|
|
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
|
|
|
|
var slideAnimation = new DoubleAnimation
|
|
{
|
|
From = 10,
|
|
To = 0,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
slideAnimation.EasingFunction = new CubicEase();
|
|
Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
|
|
|
|
sb.Children.Add(fadeInAnimation);
|
|
sb.Children.Add(slideAnimation);
|
|
|
|
sb.Begin(child);
|
|
}
|
|
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
|
|
}
|
|
|
|
public static void HidePopupWithSlideAndFade(Popup popup, double duration = 0.15)
|
|
{
|
|
try
|
|
{
|
|
if (popup == null)
|
|
throw new ArgumentNullException(nameof(popup));
|
|
|
|
if (!popup.IsOpen) return;
|
|
|
|
var child = popup.Child as FrameworkElement;
|
|
if (child == null)
|
|
{
|
|
popup.IsOpen = false;
|
|
return;
|
|
}
|
|
|
|
var sb = new Storyboard();
|
|
|
|
var fadeOutAnimation = new DoubleAnimation
|
|
{
|
|
From = 1,
|
|
To = 0,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
fadeOutAnimation.EasingFunction = new CubicEase();
|
|
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(UIElement.OpacityProperty));
|
|
|
|
var slideAnimation = new DoubleAnimation
|
|
{
|
|
From = 0,
|
|
To = 10,
|
|
Duration = TimeSpan.FromSeconds(duration)
|
|
};
|
|
slideAnimation.EasingFunction = new CubicEase();
|
|
Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
|
|
|
|
sb.Children.Add(fadeOutAnimation);
|
|
sb.Children.Add(slideAnimation);
|
|
|
|
sb.Completed += (s, e) =>
|
|
{
|
|
popup.IsOpen = false;
|
|
child.Opacity = 1;
|
|
child.RenderTransform = new TranslateTransform();
|
|
};
|
|
|
|
child.RenderTransform = new TranslateTransform();
|
|
sb.Begin(child);
|
|
}
|
|
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
|
|
}
|
|
|
|
}
|
|
}
|