123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System;
- using System.ComponentModel;
- using System.Windows;
- using System.Windows.Threading;
- using HandyControl.Data;
- using HandyControl.Tools;
- namespace HandyControl.Controls;
- public sealed class Notification : System.Windows.Window
- {
- private const int WaitTime = 6;
- /// <summary>
- /// 计数
- /// </summary>
- private int _tickCount;
- /// <summary>
- /// 关闭计时器
- /// </summary>
- private DispatcherTimer _timerClose;
- private ShowAnimation ShowAnimation { get; set; }
- private bool _shouldBeClosed;
- public Notification()
- {
- WindowStyle = WindowStyle.None;
- AllowsTransparency = true;
- }
- public static Notification Show(object content, ShowAnimation showAnimation = ShowAnimation.None, bool staysOpen = false)
- {
- var notification = new Notification
- {
- Content = content,
- Opacity = 0,
- ShowAnimation = showAnimation
- };
- notification.Show();
- var desktopWorkingArea = SystemParameters.WorkArea;
- var leftMax = desktopWorkingArea.Width - notification.ActualWidth;
- var topMax = desktopWorkingArea.Height - notification.ActualHeight;
- switch (showAnimation)
- {
- case ShowAnimation.None:
- notification.Opacity = 1;
- notification.Left = leftMax;
- notification.Top = topMax;
- break;
- case ShowAnimation.HorizontalMove:
- notification.Opacity = 1;
- notification.Left = desktopWorkingArea.Width;
- notification.Top = topMax;
- notification.BeginAnimation(LeftProperty, AnimationHelper.CreateAnimation(leftMax));
- break;
- case ShowAnimation.VerticalMove:
- notification.Opacity = 1;
- notification.Left = leftMax;
- notification.Top = desktopWorkingArea.Height;
- notification.BeginAnimation(TopProperty, AnimationHelper.CreateAnimation(topMax));
- break;
- case ShowAnimation.Fade:
- notification.Left = leftMax;
- notification.Top = topMax;
- notification.BeginAnimation(OpacityProperty, AnimationHelper.CreateAnimation(1));
- break;
- default:
- notification.Opacity = 1;
- notification.Left = leftMax;
- notification.Top = topMax;
- break;
- }
- if (!staysOpen) notification.StartTimer();
- return notification;
- }
- protected override void OnClosing(CancelEventArgs e)
- {
- base.OnClosing(e);
- if (_shouldBeClosed)
- {
- return;
- }
- var desktopWorkingArea = SystemParameters.WorkArea;
- switch (ShowAnimation)
- {
- case ShowAnimation.None:
- break;
- case ShowAnimation.HorizontalMove:
- {
- var animation = AnimationHelper.CreateAnimation(desktopWorkingArea.Width);
- animation.Completed += Animation_Completed;
- BeginAnimation(LeftProperty, animation);
- e.Cancel = true;
- _shouldBeClosed = true;
- }
- break;
- case ShowAnimation.VerticalMove:
- {
- var animation = AnimationHelper.CreateAnimation(desktopWorkingArea.Height);
- animation.Completed += Animation_Completed;
- BeginAnimation(TopProperty, animation);
- e.Cancel = true;
- _shouldBeClosed = true;
- }
- break;
- case ShowAnimation.Fade:
- {
- var animation = AnimationHelper.CreateAnimation(0);
- animation.Completed += Animation_Completed;
- BeginAnimation(OpacityProperty, animation);
- e.Cancel = true;
- _shouldBeClosed = true;
- }
- break;
- }
- }
- private void Animation_Completed(object sender, EventArgs e) => Close();
- /// <summary>
- /// 开始计时器
- /// </summary>
- private void StartTimer()
- {
- _timerClose = new DispatcherTimer
- {
- Interval = TimeSpan.FromSeconds(1)
- };
- _timerClose.Tick += delegate
- {
- if (IsMouseOver)
- {
- _tickCount = 0;
- return;
- }
- _tickCount++;
- if (_tickCount >= WaitTime) Close();
- };
- _timerClose.Start();
- }
- }
|