Toasts – Diff between revs 45 and 49

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 45 Rev 49
Line 9... Line 9...
9 using System.Diagnostics; 9 using System.Diagnostics;
10 using System.Drawing; 10 using System.Drawing;
11 using System.IO; 11 using System.IO;
12 using System.Linq; 12 using System.Linq;
13 using System.Media; 13 using System.Media;
-   14 using System.Net.Http;
14 using System.Runtime.InteropServices; 15 using System.Runtime.InteropServices;
15 using System.Threading; 16 using System.Threading;
16 using System.Threading.Tasks; 17 using System.Threading.Tasks;
17 using System.Windows.Forms; 18 using System.Windows.Forms;
18 using TheArtOfDev.HtmlRenderer.WinForms; 19 using TheArtOfDev.HtmlRenderer.WinForms;
Line 22... Line 23...
22 { 23 {
23 public partial class ToastForm : Form 24 public partial class ToastForm : Form
24 { 25 {
25 #region Public Fields and Properties 26 #region Public Fields and Properties
Line -... Line 27...
-   27  
-   28 public string UserAgent { get; set; } = string.Empty;
26   29  
Line 27... Line 30...
27 public bool EnableChime { get; set; } = true; 30 public bool EnableChime { get; set; } = true;
Line 28... Line 31...
28   31  
Line 40... Line 43...
40   43  
Line 41... Line 44...
41 public string ContentType { get; internal set; } = "text/plain"; 44 public string ContentType { get; internal set; } = "text/plain";
42   45  
-   46 public byte[] Chime
-   47 {
-   48 get
-   49 {
-   50 // offer the default built-in chime
-   51 if(_chime == null)
-   52 {
-   53 using (var memoryStream = new MemoryStream())
-   54 {
-   55 Resources.normal.CopyTo(memoryStream);
-   56  
-   57 memoryStream.Position = 0L;
-   58  
-   59 return memoryStream.ToArray();
-   60 }
43 public byte[] Chime 61 }
-   62  
44 { 63 return _chime;
45 get => _chime; 64 }
46 set 65 set
47 { 66 {
48 if (value is null) 67 if (value is null)
Line 52... Line 71...
52   71  
53 _chime = value; 72 _chime = value;
54 } 73 }
Line -... Line 74...
-   74 }
-   75  
-   76 public bool EnablePin { get; internal set; }
55 } 77 public Point PinPoint { get; internal set; }
56   78  
57 /// <summary> 79 /// <summary>
58 /// 80 ///
59 /// </summary> 81 /// </summary>
Line 75... Line 97...
75 set => imageBox.Image = value; 97 set => imageBox.Image = value;
76 } 98 }
Line 77... Line 99...
77   99  
Line 78... Line 100...
78 #endregion 100 #endregion
Line 79... Line 101...
79   101  
Line 80... Line 102...
80 #region Static Fields and Constants 102 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
Line 81... Line -...
81   -  
82 private static readonly object OpenNotificationsLock = new object(); -  
83   -  
84 private static readonly HashSet<ToastForm> OpenNotifications = new HashSet<ToastForm>(); -  
85   103  
-   104 private static readonly object OpenNotificationsLock = new object();
86 #endregion 105  
-   106 private static readonly HashSet<ToastForm> OpenNotifications = new HashSet<ToastForm>();
87   107  
-   108 private bool _toastDetached;
88 #region Private Fields and Properties 109  
Line -... Line 110...
-   110 private object _toastDetachedLock = new object();
-   111  
89   112 private bool _mouseDown;
-   113  
-   114 private Point _lastLocation;
Line 90... Line 115...
90 private bool _toastDetached; 115  
Line -... Line 116...
-   116 private static HttpClient httpClient = new HttpClient();
-   117  
-   118 private FormAnimator _formAnimator;
-   119  
-   120 private System.Timers.Timer _toastTimer;
-   121  
91 private object _toastDetachedLock = new object(); 122 private readonly int _screenWidth;
-   123  
92 private bool _mouseDown; 124 private readonly int _screenHeight;
93 private Point _lastLocation; 125  
94   126 private readonly TaskCompletionSource<object> _showFormTaskCompletionSource;
95 #endregion 127  
Line 111... Line 143...
111   143  
112 return baseParams; 144 return baseParams;
113 } 145 }
Line 114... Line 146...
114 } 146 }
115   -  
Line -... Line 147...
-   147  
116 public bool EnablePin { get; internal set; } 148 #endregion
117 public Point PinPoint { get; internal set; } 149  
118   -  
119 [DllImport("user32.dll", EntryPoint = "SetWindowPos")] 150 #region Natives
Line 120... Line 151...
120 public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); 151 [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
Line 121... Line 152...
121   152 public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
Line 129... Line 160...
129 // round rectangles 160 // round rectangles
130 //Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20)); 161 //Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
Line 131... Line 162...
131   162  
132 _screenWidth = Screen.PrimaryScreen.WorkingArea.Width; 163 _screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
133 _screenHeight = Screen.PrimaryScreen.WorkingArea.Height; -  
134   -  
135 _formAnimator = new FormAnimator(this, AnimationMethod, AnimationDirection, AnimationDuration); -  
136   -  
137 using (var memoryStream = new MemoryStream()) -  
138 { -  
139 Resources.normal.CopyTo(memoryStream); -  
140   -  
141 memoryStream.Position = 0L; -  
142   -  
143 Chime = memoryStream.ToArray(); -  
144 } -  
145   164 _screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
Line 146... Line -...
146 _showFormTaskCompletionSource = new TaskCompletionSource<object>(); -  
147   -  
148 _toastTimer = new System.Timers.Timer { Enabled = false, AutoReset = false, Interval = LingerTime }; 165 _showFormTaskCompletionSource = new TaskCompletionSource<object>();
Line 149... Line 166...
149 _toastTimer.Elapsed += ToastTimer_Elapsed; 166  
150 } 167 }
151   168  
Line 186... Line 203...
186 base.Dispose(disposing); 203 base.Dispose(disposing);
187 } 204 }
Line 188... Line 205...
188   205  
Line 189... Line -...
189 #endregion -  
190   -  
191 #region Private Delegates, Events, Enums, Properties, Indexers and Fields -  
192   -  
193 private readonly FormAnimator _formAnimator; -  
194   -  
195 private System.Timers.Timer _toastTimer; -  
196   -  
197 private readonly int _screenWidth; -  
198   -  
199 private readonly int _screenHeight; -  
200   -  
201 private readonly TaskCompletionSource<object> _showFormTaskCompletionSource; -  
202   -  
203 private byte[] _chime; -  
204   -  
205 #endregion 206 #endregion
Line 206... Line 207...
206   207  
207 #region Event Handlers 208 #region Event Handlers
208   209  
Line 249... Line 250...
249 } 250 }
250 } 251 }
Line 251... Line 252...
251   252  
252 private void ToastForm_Load(object sender, EventArgs e) 253 private void ToastForm_Load(object sender, EventArgs e)
-   254 {
-   255 _toastTimer = new System.Timers.Timer { Enabled = false, AutoReset = false, Interval = LingerTime };
-   256 _toastTimer.Elapsed += ToastTimer_Elapsed;
253 { 257  
254 if (EnableChime) 258 if (EnableChime)
255 { 259 {
256 Task.Factory.StartNew(() => 260 Task.Factory.StartNew(() =>
257 { 261 {
Line 416... Line 420...
416 { 420 {
417 Debug.WriteLine("Error on form load event."); 421 Debug.WriteLine("Error on form load event.");
418 } 422 }
419 } 423 }
Line -... Line 424...
-   424  
-   425 private async void HtmlPanel1_ImageLoad(object sender, TheArtOfDev.HtmlRenderer.Core.Entities.HtmlImageLoadEventArgs e)
-   426 {
-   427 if(!string.IsNullOrEmpty(UserAgent))
-   428 {
-   429 httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgent);
-   430 }
-   431  
-   432 try
-   433 {
-   434 using (var response = await httpClient.GetAsync(e.Src))
-   435 {
-   436 using (var responseStream = await response.Content.ReadAsStreamAsync())
-   437 {
-   438 var image = Image.FromStream(responseStream);
-   439  
-   440 e.Callback(image);
-   441 e.Handled = true;
-   442 }
-   443 }
-   444 }
-   445 catch
-   446 {
-   447 Debug.WriteLine("Error downloading image.");
-   448 }
-   449 }
420   450  
421 private void ToastForm_FormClosed(object sender, FormClosedEventArgs e) 451 private void ToastForm_FormClosed(object sender, FormClosedEventArgs e)
422 { 452 {
423 try 453 try
424 { 454 {
Line 433... Line 463...
433 } 463 }
434 } 464 }
Line 435... Line 465...
435   465  
436 private void ToastForm_Shown(object sender, EventArgs e) 466 private void ToastForm_Shown(object sender, EventArgs e)
-   467 {
-   468 _formAnimator = new FormAnimator(this, AnimationMethod, AnimationDirection, AnimationDuration);
437 { 469  
438 // Reverse animation direction for hiding. 470 // Reverse animation direction for hiding.
439 switch (_formAnimator.Direction) 471 switch (_formAnimator.Direction)
440 { 472 {
441 case FormAnimator.AnimationDirection.Up: 473 case FormAnimator.AnimationDirection.Up: