优化贴边隐藏动画

This commit is contained in:
liufei
2022-01-09 15:38:46 +08:00
parent b23a1d91c5
commit c3e46bc751

View File

@@ -4,8 +4,8 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Drawing; //添加引用 using System.Drawing;
using System.Windows.Forms;//添加引用 using System.Windows.Forms;
using MouseEventArgs = System.Windows.Input.MouseEventArgs; using MouseEventArgs = System.Windows.Input.MouseEventArgs;
using System.Windows; using System.Windows;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
@@ -14,32 +14,40 @@ using System.Windows.Media;
namespace GeekDesk.Util namespace GeekDesk.Util
{ {
enum HidePosition enum HideType
{ {
TOP = 1, TOP_SHOW = 1,
LEFT = 2, LEFT_SHOW = 2,
RIGHT = 3 RIGHT_SHOW = 3,
TOP_HIDE = 4,
LEFT_HIDE = 5,
RIGHT_HIDE = 6
} }
public class MarginHide public class MarginHide
{ {
readonly Window window;//定义使用该方法的窗体 private static Window window;//定义使用该方法的窗体
private readonly int hideTime = 80; private static readonly int hideTime = 200;
private static readonly int fadeHideTime = 180;
private static readonly int fadeShowTime = 200;
private static readonly int taskTime = 250;
private readonly int taskTime = 200; private static double showMarginWidth = 1;
private double showMarginWidth = 1; public static bool IS_RUN = false;
public static bool IS_HIDE = false;
#pragma warning disable CS0414 // 字段“MarginHide.isHide”已被赋值但从未使用过它的值 private static Timer timer = null;
private bool isHide;
#pragma warning restore CS0414 // 字段“MarginHide.isHide”已被赋值但从未使用过它的值
public Timer timer; public static void ReadyHide(Window window)
//构造函数,传入将要匹配的窗体
public MarginHide(Window window)
{ {
this.window = window; MarginHide.window = window;
if (timer != null) return;
timer = new Timer();//添加timer计时器隐藏功能
timer.Interval = taskTime;
timer.Tick += HideWindow;
timer.Start();
} }
@@ -47,7 +55,7 @@ namespace GeekDesk.Util
/// 窗体是否贴边 /// 窗体是否贴边
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public bool IsMargin() public static bool IsMargin()
{ {
double screenLeft = SystemParameters.VirtualScreenLeft; double screenLeft = SystemParameters.VirtualScreenLeft;
double screenTop = SystemParameters.VirtualScreenTop; double screenTop = SystemParameters.VirtualScreenTop;
@@ -67,9 +75,9 @@ namespace GeekDesk.Util
#region #region
private void TimerDealy(object o, EventArgs e) private static void HideWindow(object o, EventArgs e)
{ {
if (window.Visibility != Visibility.Visible) return; if (window.Visibility != Visibility.Visible || !IS_RUN) return;
double screenLeft = SystemParameters.VirtualScreenLeft; double screenLeft = SystemParameters.VirtualScreenLeft;
double screenTop = SystemParameters.VirtualScreenTop; double screenTop = SystemParameters.VirtualScreenTop;
@@ -87,47 +95,59 @@ namespace GeekDesk.Util
double mouseY = p.Y; double mouseY = p.Y;
//鼠标不在窗口上 //鼠标不在窗口上
if (mouseX < windowLeft || mouseX > windowLeft + windowWidth if ((mouseX < windowLeft || mouseX > windowLeft + windowWidth
|| mouseY < windowTop || mouseY > windowTop + windowHeight) || mouseY < windowTop || mouseY > windowTop + windowHeight) && !IS_HIDE)
{ {
//上方隐藏条件 //上方隐藏条件
if (windowTop <= screenTop) if (windowTop <= screenTop)
{ {
HideAnimation(windowTop, screenTop - windowHeight + showMarginWidth, Window.TopProperty); IS_HIDE = true;
isHide = true; FadeAnimation(1, 0);
HideAnimation(windowTop, screenTop - windowHeight + showMarginWidth, Window.TopProperty, HideType.TOP_HIDE);
return; return;
} }
//左侧隐藏条件 //左侧隐藏条件
if (windowLeft <= screenLeft) if (windowLeft <= screenLeft)
{ {
HideAnimation(windowLeft, screenLeft - windowWidth + showMarginWidth, Window.LeftProperty); IS_HIDE = true;
FadeAnimation(1, 0);
HideAnimation(windowLeft, screenLeft - windowWidth + showMarginWidth, Window.LeftProperty, HideType.LEFT_HIDE);
return; return;
} }
//右侧隐藏条件 //右侧隐藏条件
if (windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth) if (windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth)
{ {
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - showMarginWidth, Window.LeftProperty); IS_HIDE = true;
FadeAnimation(1, 0);
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - showMarginWidth, Window.LeftProperty, HideType.RIGHT_HIDE);
return; return;
} }
} else if (mouseX >= windowLeft && mouseX <= windowLeft + windowWidth }
&& mouseY >= windowTop && mouseY <= windowTop + windowHeight) else if (mouseX >= windowLeft && mouseX <= windowLeft + windowWidth
&& mouseY >= windowTop && mouseY <= windowTop + windowHeight && IS_HIDE)
{ {
//上方显示 //上方显示
if (windowTop <= screenTop - showMarginWidth) if (windowTop <= screenTop - showMarginWidth)
{ {
HideAnimation(windowTop, screenTop, Window.TopProperty); IS_HIDE = false;
HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW);
FadeAnimation(0, 1);
return; return;
} }
//左侧显示 //左侧显示
if (windowLeft <= screenLeft - showMarginWidth) if (windowLeft <= screenLeft - showMarginWidth)
{ {
HideAnimation(windowLeft, screenLeft, Window.LeftProperty); IS_HIDE = false;
HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW);
FadeAnimation(0, 1);
return; return;
} }
//右侧显示 //右侧显示
if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth) if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth)
{ {
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty); IS_HIDE = false;
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW);
FadeAnimation(0, 1);
return; return;
} }
} }
@@ -136,24 +156,17 @@ namespace GeekDesk.Util
#endregion #endregion
public void TimerSet() public static void StartHide()
{ {
if (timer != null) return; IS_RUN = true;
timer = new Timer();//添加timer计时器隐藏功能
#region
timer.Interval = taskTime;
timer.Tick += TimerDealy;
timer.Start();
#endregion
} }
public void TimerStop() public static void StopHide()
{ {
timer.Stop(); IS_RUN = false;
timer.Dispose();
timer = null;
//功能关闭 如果界面是隐藏状态 那么要显示界面 ↓ //功能关闭 如果界面是隐藏状态 那么要显示界面 ↓
if (IS_HIDE)
{
double screenLeft = SystemParameters.VirtualScreenLeft; double screenLeft = SystemParameters.VirtualScreenLeft;
double screenTop = SystemParameters.VirtualScreenTop; double screenTop = SystemParameters.VirtualScreenTop;
double screenWidth = SystemParameters.VirtualScreenWidth; double screenWidth = SystemParameters.VirtualScreenWidth;
@@ -166,28 +179,60 @@ namespace GeekDesk.Util
//左侧显示 //左侧显示
if (windowLeft <= screenLeft - showMarginWidth) if (windowLeft <= screenLeft - showMarginWidth)
{ {
HideAnimation(windowLeft, screenLeft, Window.LeftProperty); IS_HIDE = false;
FadeAnimation(0, 1);
HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW);
return; return;
} }
//上方显示 //上方显示
if (windowTop <= screenTop - showMarginWidth) if (windowTop <= screenTop - showMarginWidth)
{ {
HideAnimation(windowTop, screenTop, Window.TopProperty); IS_HIDE = false;
FadeAnimation(0, 1);
HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW);
return; return;
} }
//右侧显示 //右侧显示
if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth) if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth)
{ {
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty); IS_HIDE = false;
FadeAnimation(0, 1);
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW);
return; return;
} }
} }
}
private void HideAnimation(double from, double to, DependencyProperty property) private static void HideAnimation(double from, double to, DependencyProperty property, HideType hideType)
{ {
double toTemp = to;
double leftT = window.Width / 4 * 3;
double topT = window.Height / 4 * 3;
switch (hideType)
{
case HideType.LEFT_HIDE:
to += leftT;
break;
case HideType.LEFT_SHOW:
to -= leftT;
break;
case HideType.RIGHT_HIDE:
to -= leftT;
break;
case HideType.RIGHT_SHOW:
to += leftT;
break;
case HideType.TOP_HIDE:
to += topT;
break;
case HideType.TOP_SHOW:
to -= topT;
break;
}
DoubleAnimation da = new DoubleAnimation DoubleAnimation da = new DoubleAnimation
{ {
From = from, From = from,
@@ -196,11 +241,48 @@ namespace GeekDesk.Util
}; };
da.Completed += (s, e) => da.Completed += (s, e) =>
{ {
if ("Top".Equals(property.Name))
{
window.Top = toTemp;
}
else
{
window.Left = toTemp;
}
window.BeginAnimation(property, null); window.BeginAnimation(property, null);
}; };
Timeline.SetDesiredFrameRate(da, 60); Timeline.SetDesiredFrameRate(da, 60);
window.BeginAnimation(property, da); window.BeginAnimation(property, da);
} }
private static void FadeAnimation(double from, double to)
{
double time;
if (to == 0D)
{
time = fadeHideTime;
}
else
{
time = fadeShowTime;
}
DoubleAnimation opacityAnimation = new DoubleAnimation
{
From = from,
To = to,
Duration = new Duration(TimeSpan.FromMilliseconds(time))
};
opacityAnimation.Completed += (s, e) =>
{
//window.Opacity = to;
window.BeginAnimation(Window.OpacityProperty, null);
};
Timeline.SetDesiredFrameRate(opacityAnimation, 60);
window.BeginAnimation(Window.OpacityProperty, opacityAnimation);
}
} }
} }