add Notification

This commit is contained in:
NaBian committed 2019-08-16 00:30:15 +08:00
1 parent 436f5d076b
commit a4e80e288d
30 files changed
+440 -78

No files matched your search

@@ -0,0 +1,155 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
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();
}
}
}
@@ -0,0 +1,10 @@
namespace HandyControl.Data
{
public enum ShowAnimation
{
None,
HorizontalMove,
verticalMove,
Fade
}
}
@@ -24,6 +24,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Magnifier.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\ButtonGroup.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Card.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Notification.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Sprite.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\ToggleBlock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Panel\HoneycombPanel.cs" />
@@ -31,6 +32,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Slider\RangeSlider\RangeThumb.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Slider\RangeSlider\TwoWayRangeBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\RunningBlock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\ShowAnimation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Commands\CloseWindowCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Commands\OpenLinkCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Converter\BorderCircularClipConverter.cs" />
@@ -688,6 +690,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Themes\Styles\Notification.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Themes\Styles\NumericUpDown.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -0,0 +1,22 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="clr-namespace:HandyControl.Controls">
<Style TargetType="controls:Notification">
<Setter Property="controls:WindowAttach.IgnoreAltF4" Value="True"/>
<Setter Property="controls:WindowAttach.ShowInTaskManager" Value="False"/>
<Setter Property="SizeToContent" Value="WidthAndHeight"/>
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ShowActivated" Value="False"/>
<Setter Property="ShowInTaskbar" Value="False"/>
<Setter Property="Topmost" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Notification">
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
@@ -72,6 +72,7 @@
<ResourceDictionary Source="ToggleBlock.xaml"/>
<ResourceDictionary Source="RunningBlock.xaml"/>
<ResourceDictionary Source="Sprite.xaml"/>
<ResourceDictionary Source="Notification.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>