Toasts – Diff between revs 53 and 55
?pathlinks?
Rev 53 | Rev 55 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Concurrent; |
2 | using System.Collections.Concurrent; |
|
3 | using System.ComponentModel; |
3 | using System.ComponentModel; |
|
4 | using System.Diagnostics; |
4 | using System.Diagnostics; |
|
5 | using System.Drawing; |
5 | using System.Drawing; |
|
- | 6 | using System.Linq; |
||
6 | using System.Runtime.InteropServices; |
7 | using System.Runtime.InteropServices; |
|
7 | using System.Threading; |
8 | using System.Threading; |
|
8 | using System.Threading.Tasks; |
9 | using System.Threading.Tasks; |
|
9 | using System.Threading.Tasks.Dataflow; |
10 | using System.Threading.Tasks.Dataflow; |
|
10 | using System.Windows.Forms; |
11 | using System.Windows.Forms; |
|
11 | using Markdig; |
12 | using Markdig; |
|
12 | using Markdig.Renderers; |
13 | using Markdig.Renderers; |
|
- | 14 | using Toasts.Utilities; |
||
13 | |
15 | |
|
14 | namespace Toasts |
16 | namespace Toasts |
|
15 | { |
17 | { |
|
16 | public class ToastDisplay : IDisposable |
18 | public class ToastDisplay : IDisposable |
|
17 | { |
19 | { |
|
18 | private readonly CancellationToken _cancellationToken; |
20 | private readonly CancellationToken _cancellationToken; |
|
19 | private readonly BufferBlock<ToastDisplayData> _toastFormBufferBlock; |
21 | private readonly BufferBlock<ToastDisplayData> _toastFormBufferBlock; |
|
20 | private ConcurrentBag<IDisposable> tplDataflowLinks; |
22 | private ConcurrentBag<IDisposable> tplDataflowLinks; |
|
21 | private readonly TransformBlock<ToastDisplayData, ToastDisplayData> _toastBodyTransformBlock; |
23 | private readonly TransformBlock<ToastDisplayData, ToastDisplayData> _toastBodyTransformBlock; |
|
22 | private readonly ActionBlock<ToastDisplayData> _toastFormActionBlock; |
24 | private readonly ActionBlock<ToastDisplayData> _toastFormActionBlock; |
|
23 | |
- | ||
- | 25 | private readonly ConcurrentDictionary<IntPtr, ToastForm> _toastForms; |
||
24 | private ToastDisplay() |
26 | private ToastDisplay() |
|
25 | { |
27 | { |
|
- | 28 | _toastForms = new ConcurrentDictionary<IntPtr, ToastForm>(); |
||
26 | tplDataflowLinks = new ConcurrentBag<IDisposable>(); |
29 | tplDataflowLinks = new ConcurrentBag<IDisposable>(); |
|
27 | } |
30 | } |
|
Line 28... | Line 31... | |||
28 | |
31 | |
|
29 | public ToastDisplay(CancellationToken cancellationToken) : this() |
32 | public ToastDisplay(CancellationToken cancellationToken) : this() |
|
Line 60... | Line 63... | |||
60 | { |
63 | { |
|
61 | disposable.Dispose(); |
64 | disposable.Dispose(); |
|
62 | } |
65 | } |
|
63 | } |
66 | } |
|
Line 64... | Line 67... | |||
64 | |
67 | |
|
65 | private static void DisplayForm(ToastDisplayData toastPayload) |
68 | private void DisplayForm(ToastDisplayData toastPayload) |
|
66 | { |
69 | { |
|
67 | var thread = new Thread(() => |
70 | var thread = new Thread(() => |
|
68 | { |
71 | { |
|
69 | try |
72 | try |
|
70 | { |
73 | { |
|
71 | var toastForm = new ToastForm(toastPayload.Title, toastPayload.Body) |
74 | var toastForm = new ToastForm(toastPayload.Title, toastPayload.Body) |
|
- | 75 | { |
||
72 | { |
76 | Proxy = toastPayload.Proxy, |
|
73 | EnableChime = toastPayload.EnableChime, |
77 | EnableChime = toastPayload.EnableChime, |
|
74 | Chime = toastPayload.Chime ?? toastPayload.Chime, |
78 | Chime = toastPayload.Chime ?? toastPayload.Chime, |
|
75 | LingerTime = toastPayload.LingerTime, |
79 | LingerTime = toastPayload.LingerTime, |
|
76 | Image = toastPayload.Image, |
80 | Image = toastPayload.Image, |
|
Line 86... | Line 90... | |||
86 | toastForm.FormClosing -= OnFormClosing; |
90 | toastForm.FormClosing -= OnFormClosing; |
|
87 | formClosingEvent.Set(); |
91 | formClosingEvent.Set(); |
|
88 | } |
92 | } |
|
89 | toastForm.FormClosing += OnFormClosing; |
93 | toastForm.FormClosing += OnFormClosing; |
|
Line -... | Line 94... | |||
- | 94 | |
||
90 | |
95 | _toastForms.TryAdd(toastForm.Handle, toastForm); |
|
Line 91... | Line 96... | |||
91 | Application.Run(toastForm); |
96 | Application.Run(toastForm); |
|
92 | |
97 | |
|
- | 98 | var timeout = toastPayload.EnablePin ? -1 : toastPayload.LingerTime; |
||
- | 99 | formClosingEvent.WaitOne(timeout); |
||
93 | var timeout = toastPayload.EnablePin ? -1 : toastPayload.LingerTime; |
100 | _toastForms.TryRemove(toastForm.Handle, out _); |
|
94 | formClosingEvent.WaitOne(timeout); |
101 | |
|
95 | toastForm.FormClosing -= OnFormClosing; |
102 | toastForm.FormClosing -= OnFormClosing; |
|
96 | toastForm.Close(); |
103 | toastForm.Close(); |
|
97 | if (!toastForm.IsDisposed) |
104 | if (!toastForm.IsDisposed) |
|
Line 132... | Line 139... | |||
132 | |
139 | |
|
133 | public async Task Queue(ToastDisplayData toastPayload) |
140 | public async Task Queue(ToastDisplayData toastPayload) |
|
134 | { |
141 | { |
|
135 | await _toastFormBufferBlock.SendAsync(toastPayload, _cancellationToken); |
142 | await _toastFormBufferBlock.SendAsync(toastPayload, _cancellationToken); |
|
- | 143 | } |
||
- | 144 | |
||
- | 145 | public void Clear() |
||
- | 146 | { |
||
- | 147 | foreach (var (handle, toastForm) in new ConcurrentDictionary<IntPtr, ToastForm>(_toastForms).Select(x => (x.Key, x.Value))) |
||
- | 148 | { |
||
- | 149 | _toastForms.TryRemove(handle, out _); |
||
- | 150 | toastForm.InvokeIfRequired(form => |
||
- | 151 | { |
||
- | 152 | form.Close(); |
||
- | 153 | }); |
||
- | 154 | } |
||
136 | } |
155 | } |
|
137 | } |
156 | } |
|
138 | } |
157 | } |