-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
409 lines (372 loc) · 16.5 KB
/
MainWindow.xaml.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Diagnostics;
using System.Reflection;
using Forms = System.Windows.Forms;
using System.Media;
using System.IO;
namespace ClearClock
{
/// <summary>
/// TODO: Add user's selections to settings manager.
/// </summary>
public partial class MainWindow : Window
{
#region [Props]
DispatcherTimer timer = null;
Forms.NotifyIcon notifyIcon;
SoundPlayer tick1 = null;
SoundPlayer tick2 = null;
const int MENU1 = 0;
const int MENU2 = 1;
const int MENU3 = 2;
const int MENU4 = 3;
const int MENU5 = 4;
int cleanCounter = 0;
double screen_bottom = SystemParameters.WorkArea.Bottom;
double screen_top = SystemParameters.WorkArea.Top;
double screen_left = SystemParameters.WorkArea.Left;
double screen_right = SystemParameters.WorkArea.Right;
bool hourNotify = false;
bool tickSound = false;
bool smoothMode = false;
int currentHour = 0;
#endregion
/// <summary>
/// Default Constructor
/// </summary>
public MainWindow()
{
InitializeComponent();
tick1 = new SoundPlayer(ClearClock.Properties.Resources.tick1q);
tick2 = new SoundPlayer(ClearClock.Properties.Resources.tick2q);
UpdateClockAngles();
currentHour = DateTime.Now.Hour;
if (timer == null)
{
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer.Tick += Timer_Tick;
timer.Start();
this.Topmost = true;
}
}
#region [Events]
/// <summary>
/// <see cref="DispatcherTimer"/> event.
/// </summary>
void Timer_Tick(object sender, EventArgs e)
{
UpdateClockAngles();
if (notifyIcon != null)
notifyIcon.Text = DateTime.Now.ToLongDateString();
if ((currentHour != DateTime.Now.Hour) && hourNotify)
{
currentHour = DateTime.Now.Hour;
if (notifyIcon != null)
notifyIcon.ShowBalloonTip(5000, "Time Notice", $"The time is now {DateTime.Now.ToLongTimeString()}", Forms.ToolTipIcon.Info);
}
if (++cleanCounter > 10000)
{
cleanCounter = 0;
GC.Collect();
// Reinforce our topmost setting
if (notifyIcon.ContextMenuStrip.Items[MENU1].Image == ClearClock.Properties.Resources.red_x)
this.Topmost = false;
else
{
this.Topmost = true;
// This can steal the focus when the user is engaged in a different application.
//Application.Current.Dispatcher.Invoke(() => this.Activate());
}
}
if (tickSound && tick1 != null && tick2 != null && smoothMode == false)
{
if (cleanCounter % 2 == 0)
tick1.PlaySync();
else
tick2.PlaySync();
}
}
/// <summary>
/// <see cref="Window"/> event.
/// </summary>
void Window_Loaded(object sender, RoutedEventArgs e)
{
this.ShowInTaskbar = false; //hide in TaskBar
try
{
notifyIcon = new Forms.NotifyIcon();
notifyIcon.Icon = ClearClock.Properties.Resources.clock_bold1;
notifyIcon.Text = Assembly.GetExecutingAssembly().GetName().Name;
notifyIcon.Click += NotifyIcon_Click;
notifyIcon.ContextMenuStrip = new Forms.ContextMenuStrip();
notifyIcon.ContextMenuStrip.Items.Add("Stay on top", ClearClock.Properties.Resources.green_check, OnTopmostClicked); // MENU1
notifyIcon.ContextMenuStrip.Items.Add("Smooth seconds", ClearClock.Properties.Resources.red_x, OnSmoothClicked); // MENU2
notifyIcon.ContextMenuStrip.Items.Add("Hour notifications", ClearClock.Properties.Resources.red_x, OnNotifyClicked); // MENU3
notifyIcon.ContextMenuStrip.Items.Add("Play tick sound", ClearClock.Properties.Resources.red_x, OnSoundClicked); // MENU4
notifyIcon.ContextMenuStrip.Items.Add("Exit application", ClearClock.Properties.Resources.exit, OnExitClicked); // MENU5
notifyIcon.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show($"NotifyIcon creation error: {ex.Message}", "ClearClock", MessageBoxButton.OK, MessageBoxImage.Warning);
}
#region [Restore user's desired location]
if (SettingsManager.WindowWidth != -1)
{
this.Top = SettingsManager.WindowTop;
this.Left = SettingsManager.WindowLeft;
this.Width = SettingsManager.WindowWidth;
this.Height = SettingsManager.WindowHeight;
var maxWidth = ScreenInformation.GetMonitorCount() * SystemParameters.WorkArea.Width;
var maxHeight = ScreenInformation.GetMonitorCount() * SystemParameters.WorkArea.Height;
Debug.WriteLine($"[INFO] MaxWidth:{maxWidth} MaxHeight:{maxHeight}");
if ((SettingsManager.WindowTop + SettingsManager.WindowHeight) > maxHeight)
this.Top = screen_bottom - this.Height;
if ((SettingsManager.WindowLeft + SettingsManager.WindowWidth) > maxWidth)
this.Left = screen_right - this.Width;
#region [Update menu settings]
this.Topmost = SettingsManager.StayOnTop;
notifyIcon.ContextMenuStrip.Items[MENU1].Image = SettingsManager.StayOnTop ? ClearClock.Properties.Resources.green_check : ClearClock.Properties.Resources.red_x;
smoothMode = SettingsManager.SmoothSeconds;
notifyIcon.ContextMenuStrip.Items[MENU2].Image = SettingsManager.SmoothSeconds ? ClearClock.Properties.Resources.green_check : ClearClock.Properties.Resources.red_x;
timer.Interval = SettingsManager.SmoothSeconds ? new TimeSpan(0, 0, 0, 0, 36) : new TimeSpan(0, 0, 0, 0, 1000);
hourNotify = SettingsManager.Notifications;
notifyIcon.ContextMenuStrip.Items[MENU3].Image = SettingsManager.Notifications ? ClearClock.Properties.Resources.green_check : ClearClock.Properties.Resources.red_x;
tickSound = SettingsManager.Sound;
notifyIcon.ContextMenuStrip.Items[MENU4].Image = SettingsManager.Sound ? ClearClock.Properties.Resources.green_check : ClearClock.Properties.Resources.red_x;
#endregion
}
else // The window params are defaulted to -1, meaning we have not created/loaded a config file yet.
{
// Move to bottom-right of screen work area.
if (this.Left < (screen_right - this.Width))
this.Left = screen_right - this.Width;
if (this.Top < (screen_bottom - this.Height))
this.Top = screen_bottom - this.Height;
}
#endregion
}
/// <summary>
/// <see cref="Window"/> event.
/// </summary>
void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (notifyIcon != null)
{
notifyIcon.Visible = false;
notifyIcon.Icon.Dispose();
notifyIcon.Dispose();
notifyIcon = null;
}
if (timer != null)
{
timer.Stop();
timer = null;
}
if (this.WindowState == WindowState.Normal)
{
SettingsManager.WindowLeft = this.Left;
SettingsManager.WindowTop = this.Top;
SettingsManager.WindowHeight = this.Height;
SettingsManager.WindowWidth = this.Width;
}
SettingsManager.WindowState = (int)this.WindowState;
SettingsManager.Save(SettingsManager.AppSettings, SettingsManager.Location, SettingsManager.Version);
}
/// <summary>
/// <see cref="Forms.NotifyIcon"/> event.
/// </summary>
void NotifyIcon_Click(object sender, EventArgs e)
{
//Application.Current.MainWindow.WindowState = WindowState.Normal;
if (e is Forms.MouseEventArgs)
{
if ((e as Forms.MouseEventArgs).Button == Forms.MouseButtons.Left)
{
//if (this.Topmost)
// this.Topmost = false;
//else
// this.Topmost = true;
}
else if ((e as Forms.MouseEventArgs).Button == Forms.MouseButtons.Right)
{
//Application.Current.Shutdown();
}
}
}
/// <summary>
/// <see cref="Forms.NotifyIcon"/> event.
/// </summary>
void OnTopmostClicked(object sender, EventArgs e)
{
if (this.Topmost)
{
this.Topmost = SettingsManager.StayOnTop = false;
notifyIcon.ContextMenuStrip.Items[MENU1].Image = ClearClock.Properties.Resources.red_x;
}
else
{
this.Topmost = SettingsManager.StayOnTop = true;
notifyIcon.ContextMenuStrip.Items[MENU1].Image = ClearClock.Properties.Resources.green_check;
}
}
/// <summary>
/// <see cref="Forms.NotifyIcon"/> event.
/// </summary>
void OnSmoothClicked(object sender, EventArgs e)
{
if (timer.Interval == new TimeSpan(0, 0, 0, 0, 36))
{
smoothMode = SettingsManager.SmoothSeconds = false;
timer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
notifyIcon.ContextMenuStrip.Items[MENU2].Image = ClearClock.Properties.Resources.red_x;
}
else
{
smoothMode = SettingsManager.SmoothSeconds = true;
timer.Interval = new TimeSpan(0, 0, 0, 0, 36);
notifyIcon.ContextMenuStrip.Items[MENU2].Image = ClearClock.Properties.Resources.green_check;
}
}
/// <summary>
/// <see cref="Forms.NotifyIcon"/> event.
/// </summary>
void OnNotifyClicked(object sender, EventArgs e)
{
if (hourNotify)
{
hourNotify = SettingsManager.Notifications = false;
notifyIcon.ContextMenuStrip.Items[MENU3].Image = ClearClock.Properties.Resources.red_x;
}
else
{
hourNotify = SettingsManager.Notifications = true;
notifyIcon.ContextMenuStrip.Items[MENU3].Image = ClearClock.Properties.Resources.green_check;
}
}
/// <summary>
/// <see cref="Forms.NotifyIcon"/> event.
/// </summary>
void OnSoundClicked(object sender, EventArgs e)
{
if (tickSound)
{
tickSound = SettingsManager.Sound = false;
notifyIcon.ContextMenuStrip.Items[MENU4].Image = ClearClock.Properties.Resources.red_x;
}
else
{
tickSound = SettingsManager.Sound = true;
notifyIcon.ContextMenuStrip.Items[MENU4].Image = ClearClock.Properties.Resources.green_check;
}
}
/// <summary>
/// <see cref="Window"/> event.
/// </summary>
void OnExitClicked(object sender, EventArgs e) => Application.Current.Shutdown();
/// <summary>
/// I've replaced this logic with the Window overrides for
/// OnMouseLeftButtonDown and OnMouseRightButtonUp.
/// </summary>
void MainGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
else if (e.RightButton == MouseButtonState.Pressed)
{
e.Handled = true;
Application.Current.Shutdown();
}
}
/// <summary>
/// Handle dragging the clock face.
/// </summary>
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
this.DragMove();
}
/// <summary>
/// Provides an alternate way to exit.
/// </summary>
protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
{
base.OnMouseRightButtonUp(e);
Application.Current.Shutdown();
}
#endregion
#region [Helpers]
/// <summary>
/// Updates the <see cref="Transform"/> for the <see cref="UIElement"/>.
/// </summary>
void UpdateClockAngles()
{
/*
float M_PI = 3.14159265358979323846
float M_PI_2 = 1.57079632679489661923
float secondsAsRadians = (float)DateTime.Now.Second / 60.0 * 2.0 * M_PI - M_PI_2;
float minutesAsRadians = (float)DateTime.Now.Minute / 60.0 * 2.0 * M_PI - M_PI_2;
float hoursAsRadians = (float)DateTime.Now.Hour / 12.0 * 2.0 * M_PI - M_PI_2;
*/
double hours = DateTime.Now.Hour * 2.0;
double minutes = DateTime.Now.Minute * 1.0;
minutes += DateTime.Now.Second / 60.0; // add more precision for smooth movement
//Debug.WriteLine($"[INFO] minutes => {minutes}");
hours += minutes / 60.0; // add more precision for smooth movement
//Debug.WriteLine($"[INFO] hours => {hours}");
double seconds = (DateTime.Now.Second * 1.0) + (DateTime.Now.Millisecond / 1000.0);
//double millisec = Math.Round(DateTime.Now.Millisecond / 1000.0, 6, MidpointRounding.AwayFromZero);
PART_HourLine.RenderTransform = new RotateTransform((hours / 24.0) * 360.0, 0.1, 0.1);
PART_MinuteLine.RenderTransform = new RotateTransform((minutes / 60.0) * 360.0, 0.1, 0.1);
PART_SecondLine.RenderTransform = new RotateTransform((seconds / 60.0) * 360.0, 0.1, 0.1);
}
/// <summary>
/// Uses <see cref="Dispatcher.Invoke(Action, DispatcherPriority)"/> for thread safety.
/// </summary>
public void RunOnUIThread(Action action)
{
// The Application.Current may be null when closing the
// window while a background thread is still running.
if (action == null || Application.Current == null)
return;
Dispatcher dispatcher = Application.Current.Dispatcher;
if (dispatcher.CheckAccess())
action();
else
dispatcher.Invoke(DispatcherPriority.Normal, (Delegate)(action));
}
/// <summary>
/// Uses <see cref="Dispatcher.BeginInvoke(Delegate, DispatcherPriority, object[])"/> for thread safety.
/// </summary>
public void RunOnUIThreadAsync(Action action)
{
// The Application.Current may be null when closing the
// window while a background thread is still running.
if (action == null || Application.Current == null)
return;
Dispatcher dispatcher = Application.Current.Dispatcher;
if (dispatcher.CheckAccess())
action();
else
dispatcher.BeginInvoke(DispatcherPriority.Normal, (Delegate)(action));
}
#endregion
}
}