-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerControl.xaml.cs
More file actions
599 lines (535 loc) · 21.3 KB
/
TimerControl.xaml.cs
File metadata and controls
599 lines (535 loc) · 21.3 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
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.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Path = System.IO.Path;
using System.Xml;
using Wpf.Ui.Controls;
using MessageBox = System.Windows.MessageBox;
using System.Drawing;
using Color = System.Drawing.Color;
using ColorConverter = System.Drawing.ColorConverter;
using System.Windows.Automation.Peers;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text.RegularExpressions;
namespace SimpleUsefulTimer
{
public partial class TimerControl : INotifyPropertyChanged
{
public static TimerControl Self;
public static TimerView timerView;
public bool shouldListenForHotkeys = true; //Controller that child windows can use
private Properties.Settings s = Properties.Settings.Default;
public static DispatcherTimer dispatcherTimer = new DispatcherTimer();
public static Stopwatch stopWatch;
public ClockControl ?clockControlPanel = null;
private bool _is_background_default, _is_background_transparent = false;
public bool IsBackgroundDefault { get { return _is_background_default; } set { _is_background_default = value; OnPropertyChanged("IsBackgroundDefault"); } }
public bool IsBackgroundTransparent { get { return _is_background_transparent; } set { _is_background_transparent = value; OnPropertyChanged("IsBackgroundTransparent"); } }
public bool IsBackgroundCustom { get { if (IsBackgroundDefault == false && IsBackgroundTransparent == false) return true; else return false; } }
private bool _useCustomForeground = false;
public bool UseCustomForeground { get { return _useCustomForeground; }set { _useCustomForeground = value;OnPropertyChanged("UseCustomForeground"); } }
private Brush _foreground= Brushes.Transparent;
public Brush MainTimerForeground { get { return _foreground; } set { _foreground = value; OnPropertyChanged("MainTimerForeground"); } }
private bool _useForegroundGradient = false;
public bool UseForegroundGradient { get { return _useForegroundGradient; } set { _useForegroundGradient = value; OnPropertyChanged("UseForegroundGradient"); } }
private Brush _foregroundGradient = Brushes.Transparent;
public Brush ForegroundGradient { get { return _foregroundGradient; } set { _foregroundGradient = value; OnPropertyChanged("ForegroundGradient"); } }
private Boolean _hex_color_input = false;
private bool didWarnUserAboutConfigWindow = false;
private int _msFieldAlignment;
public int MsFieldAlignment { get { return _msFieldAlignment; } set { _msFieldAlignment = value; OnPropertyChanged("MsFieldAlignment"); } }
public int TimerOffsetMs { get { return timerOffsetMs; } set { timerOffsetMs = value; OnPropertyChanged("TimerOffsetMs"); } }
private int timerOffsetMs = 0;
private int _showSystemTime;
public int ShouldShowSystemTime { get { return _showSystemTime; } set { _showSystemTime = value; OnPropertyChanged("ShowSystemTime"); } }
public Boolean HexColorInput
{
get { return _hex_color_input; }
set
{
_hex_color_input = value;
OnPropertyChanged("HexColorInput");
}
}
private string _timer = "";
public string Timer
{
get
{
return _timer;
}
set
{
_timer = value;
OnPropertyChanged("Timer");
}
}
private string _timerMs = "";
public string TimerMs
{
get
{
return _timerMs;
}
set
{
_timerMs = value;
OnPropertyChanged("TimerMs");
}
}
private int _fontSize;
public int TimerFontSize
{
get
{
return _fontSize;
}
set
{
_fontSize = value;
OnPropertyChanged("TimerFontSize");
OnPropertyChanged("AdjustedTimerFontSize");
}
}
public int AdjustedTimerFontSize
{
get
{
return TimerFontSize - 15;
}
}
private string _customFont = "";
public string TimerCustomFont
{
get
{
return _customFont;
}
set
{
_customFont = value;
OnPropertyChanged("TimerCustomFont");
}
}
private Brush _background = Brushes.White;
public Brush MainTimerBackground
{
get => _background;
set
{
_background = value;
OnPropertyChanged("MainTimerBackground");
}
}
public Key _toggleTimerHotkey;
public Key _resetTimerHotkey;
public bool respondToHotkeys = true;
public HotkeyResponderLoop hotkeyLoop;
public List<string> KeyList
{
get { return Enum.GetNames(typeof(Key)).ToList(); }
}
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public void SaveConfig()
{
var s = Properties.Settings.Default;
s.Save();
s.Reload();
}
public TimerControl()
{
InitializeComponent();
this.Visibility = Visibility.Hidden;
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
Self = this;
timerView = new TimerView(ref Self);
stopWatch = new Stopwatch();
//stopWatch.Start();
dispatcherTimer.Start();
Timer = "Initializing...";
HexColorInput = false;
timerView.Show();
_resetTimerHotkey = KeyInterop.KeyFromVirtualKey(s.ResetTimerHotkey == 0 ? s.DefaultResetTimerHotkey : s.ResetTimerHotkey);
_toggleTimerHotkey = KeyInterop.KeyFromVirtualKey(s.ToggleTimerHotkey == 0 ? s.DefaultToggleTimerHotkey : s.ToggleTimerHotkey);
hotkeyLoop = new HotkeyResponderLoop(ref Self);
ShouldShowSystemTime = s.EnableSystemClock;
clockControlPanel = new ClockControl(ref Self);
ShouldEnableSystemClock(ShouldShowSystemTime);
UpdateLayout();
}
public static void SetWindowEnabledOrNot(bool enableWindow)
{
Self.IsEnabled = enableWindow;
}
private void dispatcherTimer_Tick(object? sender, EventArgs e)
{
TimeSpan offset = TimeSpan.FromMilliseconds(stopWatch.Elapsed.TotalMilliseconds + TimerOffsetMs);
switch (offset.TotalMinutes)
{
case var expression when (offset.TotalMinutes < 1):
Timer = offset.ToString("ss").TrimStart('0');
if (offset.TotalMilliseconds < 1000)
Timer = Timer.Insert(0,"0");
break;
case var expression when (offset.TotalMinutes >= 1 && offset.TotalMinutes < 60):
Timer = offset.ToString(@"mm\:ss").TrimStart('0');
break;
case var expression when (offset.TotalMinutes >= 60 && offset.TotalDays < 1):
Timer = offset.ToString(@"hh\:mm\:ss").TrimStart('0');
break;
case var expression when (offset.TotalMinutes >= 60 && offset.TotalDays > 0):
Timer = $"{offset.Days}:{offset:hh\\:mm\\:ss}";
break;
default:
break;
}
TimerMs = offset.ToString(@"\.ff");
}
public void ShouldEnableSystemClock(int should)
{
if (should == 0)
{
ClockControl.getClockInstance().Hide();
clockControlPanel?.Hide();
}
else
{
ClockControl.getClockInstance().Visibility = Visibility.Visible;
}
SystemTimeControl.SelectedIndex = ShouldShowSystemTime;
}
private void SystemTimeControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ShouldEnableSystemClock(ShouldShowSystemTime);
}
private void BackgroundHexColorInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (BackgroundHexColorInput.Text.Length != 6)
{
return;
}
var clean = "";
if (BackgroundHexColorInput.Text.Length == 6)
clean = $"#{BackgroundHexColorInput.Text}";
else
clean = $"{BackgroundHexColorInput.Text}";
try
{
var color = ColorTranslator.FromHtml(clean);
MainTimerBackground = ConvertColorToBrush(color);
s.TimerBackground = color;
SaveConfig();
return;
}
catch (Exception)
{
MainTimerBackground = ConvertColorToBrush(s.DefaultTimerBackground);
return;
}
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
if (e.Source is RadioButton)
{
RadioButton radio = (RadioButton)e.Source;
if (radio.GroupName == "background")
{
switch (radio.Content)
{
case "Default":
{
ResetTimerBackground();
s.TimerBackground = s.DefaultTimerBackground;
break;
}
case "Transparent":
{
MainTimerBackground = Brushes.Transparent;
s.TimerBackground = Color.Transparent;
break;
}
case "Hex":
{
// Textbox update event will save hex code only if its valid
break;
}
default:
break;
}
}
SaveConfig();
}
}
public static string ColorToHex(System.Drawing.Color color)
{
return $"{color.R:X2}{color.G:X2}{color.B:X2}";
}
public static Brush ConvertColorToBrush(Color color)
{
return (new SolidColorBrush(System.Windows.Media.Color.FromRgb(color.R, color.G, color.B)));
}
public static System.Windows.Media.Color ConvertDrawingColorToWinMediaColor(System.Drawing.Color color)
{
var winMediaColor = new System.Windows.Media.Color();
winMediaColor.R = color.R;
winMediaColor.G = color.G;
winMediaColor.B = color.B;
winMediaColor.A = color.A;
return winMediaColor;
}
public static void ToggleTimer()
{
if (stopWatch.IsRunning)
{
stopWatch.Stop();
}
else if (!stopWatch.IsRunning)
{
stopWatch.Start();
}
}
public static void ResetTimer()
{
stopWatch.Stop();
stopWatch.Reset();
}
private void ResetTimerBackground()
{
MainTimerBackground = ConvertColorToBrush(s.DefaultTimerBackground);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
}
private void Window_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
s.Save();
Visibility = Visibility.Hidden;
UpdateLayout();
}
private void EnableCustomForeground_Checked(object sender, RoutedEventArgs e)
{
if (ForegroundHexColorInput.Text == null)
return;
ForegroundHexColorInput.Text = (s.TimerForeground.IsEmpty ? ColorToHex(s.DefaultTimerForeground) : ColorToHex(s.TimerForeground));
MainTimerForeground = ConvertColorToBrush(s.TimerForeground);
s.UseCustomTimerForeground = true;
s.Save();
}
private void UseCustomForegroundCheck_Unchecked(object sender, RoutedEventArgs e)
{
MainTimerForeground = ConvertColorToBrush(s.DefaultTimerForeground);
UseCustomForegroundGradientCheck.IsChecked = false;
s.UseCustomTimerForeground = false;
s.Save();
}
private void ForegroundHexColorInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (ForegroundHexColorInput.Text.Length != 6)
{
return;
}
var clean = "";
if (ForegroundHexColorInput.Text.Length == 6)
clean = $"#{ForegroundHexColorInput.Text}";
else
clean = $"{ForegroundHexColorInput.Text}";
try
{
var color = ColorTranslator.FromHtml(clean);
MainTimerForeground = ConvertColorToBrush(color);
s.TimerForeground = color;
UseForegroundGradient = false;
SaveConfig();
return;
}
catch (Exception)
{
MainTimerForeground = ConvertColorToBrush(s.DefaultTimerForeground);
return;
}
}
private void UseCustomForegroundGradientCheck_Checked(object sender, RoutedEventArgs e)
{
MainTimerForeground = (s.TimerForeground.IsEmpty ? ConvertColorToBrush(s.DefaultTimerForeground) : GetGradientFrom(s.TimerForeground, s.TimerForegroundGradient));
GradientHexColorInput.Text = (s.TimerForegroundGradient.IsEmpty ? ColorToHex( s.DefaultForegroundGradient): ColorToHex(s.TimerForegroundGradient));
UseForegroundGradient = true;
s.UseTimerForegroundGradient = true;
SaveConfig();
}
private void UseCustomForegroundGradientCheck_Unchecked(object sender, RoutedEventArgs e)
{
MainTimerForeground = (s.UseCustomTimerForeground ? ConvertColorToBrush(s.TimerForeground): ConvertColorToBrush(s.DefaultTimerForeground));
UseForegroundGradient = false;
s.UseTimerForegroundGradient = false;
SaveConfig();
}
private void ForegroundGradientHexColorInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (GradientHexColorInput.Text.Length != 6)
{
return;
}
if (UseCustomForegroundGradientCheck.IsChecked == false)
{
return;
}
var clean = "";
if (GradientHexColorInput.Text.Length == 6)
clean = $"#{GradientHexColorInput.Text}";
else
clean = $"{GradientHexColorInput.Text}";
try
{
var color = ColorTranslator.FromHtml(clean);
var gradient = GetGradientFrom(s.TimerForeground, color);
MainTimerForeground = gradient;
s.TimerForegroundGradient = color;
SaveConfig();
return;
}
catch (Exception)
{
MainTimerForeground = ConvertColorToBrush(s.DefaultTimerForeground);
return;
}
}
public static LinearGradientBrush GetGradientFrom(Color color1, Color color2)
{
ColorConverter cc = new ColorConverter();
LinearGradientBrush gradient = new LinearGradientBrush();
GradientStopCollection gradientStops = new GradientStopCollection();
GradientStop gradientStopOne = new GradientStop();
GradientStop gradientStopTwo = new GradientStop();
gradientStopOne.Color = ConvertDrawingColorToWinMediaColor(color1);
gradientStopOne.Offset = 0.4;
gradientStopTwo.Color = ConvertDrawingColorToWinMediaColor(color2);
gradientStopTwo.Offset = 0.6;
gradientStops.Add(gradientStopOne);
gradientStops.Add(gradientStopTwo);
gradient.GradientStops = gradientStops;
return gradient;
}
private void ResetWelcomeMessage_Click(object sender, RoutedEventArgs e)
{
s.DidReadWelcomeMessage = false;
s.Save();
}
private void FontChangeDialogueOpener_Click(object sender, RoutedEventArgs e)
{
var fontDialogue = new FontPicker(ref Self);
fontDialogue.ShowDialog();
}
private void MsFieldControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MsFieldAlignment = MsFieldControl.SelectedIndex;
}
private void ResetSettings_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Reset();
Properties.Settings.Default.EnableSystemClock = 0;
timerView.Close();
}
private void HotkeyManagerControlButton_Click(object sender, RoutedEventArgs e)
{
var hcWindow = new Hotkeys(ref Self);
hcWindow.ShowDialog();
}
private void SetCustomTime_Click(object sender, RoutedEventArgs e)
{
string pattern = @"^((?<hour>\d+):(?<min>[0-5]?\d):(?<sec>[0-5]?\d))|((?<min>[0-5]?\d):(?<sec>[0-5]?\d))|((?<hour>\d+):(?<min>[0-5]?\d))$";
Regex regex = new Regex(pattern);
shouldListenForHotkeys = false;
string input = Microsoft.VisualBasic.Interaction.InputBox("Please ensure time is in HH:MM:SS or MM:SS format (1:12:01 or 12:01 or 0:12 for example) ", "Enter desired time", "0:00");
shouldListenForHotkeys = true;
Match match = regex.Match(input);
int hour = 0, minute = 0, second = 0;
if (match.Success)
{
if (match.Groups["hour"].Value.Length > 0)
hour = int.Parse(match.Groups["hour"].Value);
if (match.Groups["min"].Value.Length > 0)
minute = int.Parse(match.Groups["min"].Value);
if (match.Groups["sec"].Value.Length > 0)
second = match.Groups["sec"].Success ? int.Parse(match.Groups["sec"].Value) : 0;
TimerOffsetMs = ((hour * 3600) + (minute * 60) + (second)) * 1000;
}
else
{
TimerOffsetMs = 0;
}
}
private void NumberBox_ValueChanged(object sender, RoutedEventArgs e)
{
s.TimerFontSize = (FontSizeNumberBox.Text != "" ? Int32.Parse(FontSizeNumberBox.Text): s.DefaultTimerFontSize);
s.Save();
}
}
public class IntToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is not int) return Visibility.Hidden;
if ((int)value == 3) return Visibility.Hidden;
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Visibility visibility)
{
return visibility == Visibility.Visible;
}
return false;
}
}
public class IntToVerticalAlignmentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
switch ((int)value)
{
case 3: break;
case 0: return VerticalAlignment.Top;
case 1: return VerticalAlignment.Center;
case 2: return VerticalAlignment.Bottom;
}
return VerticalAlignment.Stretch;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
switch ((VerticalAlignment)value)
{
case VerticalAlignment.Top: return 0;
case VerticalAlignment.Center: return 1;
case VerticalAlignment.Bottom: return 2;
case VerticalAlignment.Stretch: break;
}
return 3;
}
}
}