1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Notifications.Wpf.Utils;
namespace Notifications.Wpf.Controls
{
[TemplatePart(Name = "PART_CloseButton", Type = typeof(Button))]
public class Notification : ContentControl
{
private TimeSpan _closingAnimationTime = TimeSpan.Zero;
public bool IsClosing { get; set; }
public static readonly RoutedEvent NotificationCloseInvokedEvent = EventManager.RegisterRoutedEvent(
"NotificationCloseInvoked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Notification));
public static readonly RoutedEvent NotificationClosedEvent = EventManager.RegisterRoutedEvent(
"NotificationClosed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Notification));
static Notification()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Notification),
new FrameworkPropertyMetadata(typeof(Notification)));
}
public event RoutedEventHandler NotificationCloseInvoked
{
add { AddHandler(NotificationCloseInvokedEvent, value); }
remove { RemoveHandler(NotificationCloseInvokedEvent, value); }
}
public event RoutedEventHandler NotificationClosed
{
add { AddHandler(NotificationClosedEvent, value); }
remove { RemoveHandler(NotificationClosedEvent, value); }
}
public static bool GetCloseOnClick(DependencyObject obj)
{
return (bool)obj.GetValue(CloseOnClickProperty);
}
public static void SetCloseOnClick(DependencyObject obj, bool value)
{
obj.SetValue(CloseOnClickProperty, value);
}
public static readonly DependencyProperty CloseOnClickProperty =
DependencyProperty.RegisterAttached("CloseOnClick", typeof(bool), typeof(Notification), new FrameworkPropertyMetadata(false,CloseOnClickChanged));
private static void CloseOnClickChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var button = dependencyObject as Button;
if (button == null)
{
return;
}
var value = (bool)dependencyPropertyChangedEventArgs.NewValue;
if (value)
{
button.Click += (sender, args) =>
{
var notification = VisualTreeHelperExtensions.GetParent<Notification>(button);
notification?.Close();
};
}
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var closeButton = GetTemplateChild("PART_CloseButton") as Button;
if (closeButton != null)
closeButton.Click += OnCloseButtonOnClick;
var storyboards = Template.Triggers.OfType<EventTrigger>().FirstOrDefault(t => t.RoutedEvent == NotificationCloseInvokedEvent)?.Actions.OfType<BeginStoryboard>().Select(a => a.Storyboard);
_closingAnimationTime = new TimeSpan(storyboards?.Max(s => Math.Min((s.Duration.HasTimeSpan ? s.Duration.TimeSpan + (s.BeginTime ?? TimeSpan.Zero) : TimeSpan.MaxValue).Ticks, s.Children.Select(ch => ch.Duration.TimeSpan + (s.BeginTime ?? TimeSpan.Zero)).Max().Ticks)) ?? 0);
}
private void OnCloseButtonOnClick(object sender, RoutedEventArgs args)
{
var button = sender as Button;
if (button == null) return;
button.Click -= OnCloseButtonOnClick;
Close();
}
//TODO: .NET40
public async void Close()
{
if (IsClosing)
{
return;
}
IsClosing = true;
RaiseEvent(new RoutedEventArgs(NotificationCloseInvokedEvent));
await Task.Delay(_closingAnimationTime);
RaiseEvent(new RoutedEventArgs(NotificationClosedEvent));
}
}
}
|