Korero – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.IO;
6 using System.Linq;
7 using System.Threading;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 using Microsoft.Win32;
11  
12 namespace Korero.Notifications
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  
65 public void UpdateScreenResolution()
66 {
67 _notificationList = new List<NotificationForm>();
68  
69 _screenWorkingArea = Screen.GetWorkingArea(new Point(0, 0));
70  
71 using (var notificationForm = new NotificationForm())
72 {
73 var items = _screenWorkingArea.Bottom / notificationForm.Height;
74 foreach (var _ in Enumerable.Range(0, items))
75 {
76 _notificationList.Add(null);
77 }
78 }
79 }
80  
81 public void ShowNotification(string title, string message, int timeout)
82 {
83 Task.Factory.StartNew(() =>
84 {
85 var i = _notificationList.FindIndex(item => item == null);
86 if (i == -1)
87 {
88 return;
89 }
90  
91 var notificationForm =
92 new NotificationForm(i, title, message, timeout);
93  
94 _notificationList[i] = notificationForm;
95  
96 var notificationX = _screenWorkingArea.Right - notificationForm.Width;
97 var notificationY = _screenWorkingArea.Bottom - (i + 1) * notificationForm.Height;
98 notificationForm.UpdateLocation(notificationX, notificationY);
99  
100 notificationForm.Closing += NotificationForm_Closing;
101 notificationForm.Show();
102 }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler);
103 }
104  
105 public void ShowNotification(string title, string message, UnmanagedMemoryStream sound, int timeout)
106 {
107 Task.Factory.StartNew(() =>
108 {
109 var i = _notificationList.FindIndex(item => item == null);
110 if (i == -1)
111 {
112 return;
113 }
114  
115 var notificationForm =
116 new NotificationForm(i, title, message, timeout, sound);
117  
118 _notificationList[i] = notificationForm;
119  
120 var notificationX = _screenWorkingArea.Right - notificationForm.Width;
121 var notificationY = _screenWorkingArea.Bottom - (i + 1) * notificationForm.Height;
122 notificationForm.UpdateLocation(notificationX, notificationY);
123  
124 notificationForm.Closing += NotificationForm_Closing;
125 notificationForm.Show();
126 }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler);
127 }
128  
129 public void ShowNotification(string title, string message, UnmanagedMemoryStream sound, int timeout,
130 Action action)
131 {
132 Task.Factory.StartNew(() =>
133 {
134 var i = _notificationList.FindIndex(item => item == null);
135 if (i == -1)
136 {
137 return;
138 }
139  
140 var notificationForm =
141 new NotificationForm(i, title, message, timeout, sound, action);
142  
143 _notificationList[i] = notificationForm;
144  
145 var notificationX = _screenWorkingArea.Right - notificationForm.Width;
146 var notificationY = _screenWorkingArea.Bottom - (i + 1) * notificationForm.Height;
147 notificationForm.UpdateLocation(notificationX, notificationY);
148  
149 notificationForm.Closing += NotificationForm_Closing;
150 notificationForm.Show();
151 }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler);
152 }
153  
154 #endregion
155 }
156 }