Winify – Blame information for rev 15
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
11 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Drawing; |
||
5 | using System.Linq; |
||
6 | using System.Threading; |
||
7 | using System.Threading.Tasks; |
||
8 | using System.Windows.Forms; |
||
9 | using Microsoft.Win32; |
||
10 | using Winify.Gotify; |
||
11 | |||
12 | namespace Winify |
||
13 | { |
||
14 | public class NotificationManager : IDisposable |
||
15 | { |
||
16 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
17 | |||
18 | private readonly TaskScheduler _uiScheduler; |
||
19 | |||
20 | private List<NotificationForm> _notificationList; |
||
21 | |||
22 | private Rectangle _screenWorkingArea; |
||
23 | |||
24 | #endregion |
||
25 | |||
26 | #region Constructors, Destructors and Finalizers |
||
27 | |||
28 | public NotificationManager(TaskScheduler uiScheduler) : this() |
||
29 | { |
||
30 | _uiScheduler = uiScheduler; |
||
31 | } |
||
32 | |||
33 | private NotificationManager() |
||
34 | { |
||
35 | UpdateScreenResolution(); |
||
36 | |||
37 | SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged; |
||
38 | } |
||
39 | |||
40 | public void Dispose() |
||
41 | { |
||
42 | SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged; |
||
43 | } |
||
44 | |||
45 | #endregion |
||
46 | |||
47 | #region Event Handlers |
||
48 | |||
49 | private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) |
||
50 | { |
||
51 | UpdateScreenResolution(); |
||
52 | } |
||
53 | |||
54 | private void NotificationForm_Closing(object sender, CancelEventArgs e) |
||
55 | { |
||
56 | var notificationForm = (NotificationForm) sender; |
||
57 | notificationForm.Closing += NotificationForm_Closing; |
||
58 | _notificationList[notificationForm.Index] = null; |
||
59 | } |
||
60 | |||
61 | #endregion |
||
62 | |||
63 | #region Public Methods |
||
64 | |||
15 | office | 65 | public void ShowNotification(GotifyNotificationEventArgs e, Announcements.Announcements notifications) |
11 | office | 66 | { |
67 | Task.Factory.StartNew(() => |
||
68 | { |
||
69 | var i = _notificationList.FindIndex(item => item == null); |
||
70 | if (i == -1) |
||
71 | { |
||
72 | return; |
||
73 | } |
||
74 | |||
15 | office | 75 | var settings = |
76 | notifications.Announcement.FirstOrDefault( |
||
77 | notification => notification.AppId == e.Notification.AppId); |
||
11 | office | 78 | |
15 | office | 79 | NotificationForm notificationForm; |
80 | switch (settings == null) |
||
81 | { |
||
82 | case true: |
||
83 | notificationForm = |
||
84 | new NotificationForm(i, e.Image, $"{e.Notification.Title} ({e.Notification.AppId})", |
||
85 | e.Notification.Message, 5000); |
||
86 | |||
87 | break; |
||
88 | default: |
||
89 | notificationForm = |
||
90 | new NotificationForm(i, e.Image, $"{e.Notification.Title} ({e.Notification.AppId})", |
||
91 | e.Notification.Message, settings.LingerTime, settings.Speak); |
||
92 | |||
93 | break; |
||
94 | } |
||
95 | |||
11 | office | 96 | _notificationList[i] = notificationForm; |
97 | |||
98 | var notificationX = _screenWorkingArea.Right - notificationForm.Width; |
||
99 | var notificationY = _screenWorkingArea.Bottom - (i + 1) * notificationForm.Height; |
||
100 | notificationForm.UpdateLocation(notificationX, notificationY); |
||
101 | |||
102 | notificationForm.Closing += NotificationForm_Closing; |
||
103 | notificationForm.Show(); |
||
104 | }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler); |
||
105 | } |
||
106 | |||
107 | #endregion |
||
15 | office | 108 | |
109 | #region Private Methods |
||
110 | |||
111 | private void UpdateScreenResolution() |
||
112 | { |
||
113 | _notificationList = new List<NotificationForm>(); |
||
114 | |||
115 | _screenWorkingArea = Screen.GetWorkingArea(new Point(0, 0)); |
||
116 | |||
117 | using (var notificationForm = new NotificationForm()) |
||
118 | { |
||
119 | var items = _screenWorkingArea.Bottom / notificationForm.Height; |
||
120 | foreach (var _ in Enumerable.Range(0, items)) |
||
121 | { |
||
122 | _notificationList.Add(null); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | |||
127 | #endregion |
||
11 | office | 128 | } |
129 | } |