-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
1107 lines (942 loc) · 43.2 KB
/
MainForm.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
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* *******************************************************************************************************************
* Application: PomodoroTimer
*
* Autor: Daniel Liedke
*
* Copyright © Daniel Liedke 2025
* Usage and reproduction in any manner whatsoever without the written permission of Daniel Liedke is strictly forbidden.
*
* Purpose: Main form to control the pomodoro timer
*
* *******************************************************************************************************************/
using System;
using System.Drawing;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Windows.Automation;
using System.Runtime.InteropServices;
using System.Linq;
namespace PomodoroTimer
{
public enum TimerStatus
{
Task,
Meeting,
Break,
LongBreak,
Lunch
}
public partial class MainForm : Form
{
#region Class Variables / Constructor
private System.ComponentModel.IContainer components;
private int _taskDuration;
private int _breakDuration;
private bool _fullScreenBreak;
private NotifyIcon _notifyIcon;
private System.Windows.Forms.Timer _timer;
private new ContextMenuStrip ContextMenu;
private ToolStripMenuItem pauseToolStripMenuItem;
private ToolStripSeparator toolStripMenuItemSeparator1;
private ToolStripMenuItem goToTaskToolStripMenuItem;
private ToolStripMenuItem goToBreakToolStripMenuItem;
private ToolStripMenuItem goToMeetingToolStripMenuItem;
private ToolStripMenuItem goToLunchToolStripMenuItem;
private ToolStripMenuItem goToLongBreakToolStripMenuItem;
private ToolStripSeparator toolStripMenuItemSeparator3;
private ToolStripMenuItem configureToolStripMenuItem;
private ToolStripSeparator toolStripMenuItemSeparator4;
private ToolStripMenuItem totalBreaksTimeToolStripMenuItem;
private ToolStripMenuItem totalTasksTimeToolStripMenuItem;
private ToolStripMenuItem totalMeetingTimeToolStripMenuItem;
private ToolStripMenuItem totalLunchTimeToolStripMenuItem;
private ToolStripMenuItem totalLongBreakTimeToolStripMenuItem;
private ToolStripMenuItem totalWorkTimeToolStripMenuItem;
private ToolStripMenuItem totalRestTimeToolStripMenuItem;
private ToolStripMenuItem totalTimeToolStripMenuItem;
private ToolStripMenuItem totalBreaksCountToolStripMenuItem;
private ToolStripSeparator toolStripMenuItemSeparator2;
private ToolStripMenuItem exitToolStripMenuItem;
private ToolTip toolTip;
bool _hideToolTipTimer = false;
private ToolTipForm _toolTipForm1 = new ToolTipForm();
private ToolTipForm _toolTipForm2 = new ToolTipForm();
private int _totalBreakTime;
private int _totalMeetingTime;
private int _totalLunchTime;
private int _totalLongBreakTime;
private int _totalBreaksCount;
private Label lblText;
private int _totalTaskTime;
private System.Drawing.Size _originalToolTipFormSize;
private TimerStatus _previousStatus = TimerStatus.Task;
private int _remainingTime;
private ToolStripSeparator toolStripMenuItem1;
public int RemainingTime
{
get { return _remainingTime; }
set { _remainingTime = value; }
}
private TimerStatus _currentStatus;
public TimerStatus CurrentStatus
{
get { return _currentStatus; }
set { _currentStatus = value; }
}
public MainForm()
{
InitializeComponent();
_originalToolTipFormSize = _toolTipForm1.Size;
// If no location is set, put _toolTipForm in center of screen
if (Properties.Settings.Default.ToolTipFormLocation == null ||
(Properties.Settings.Default.ToolTipFormLocation.X == 0 &&
Properties.Settings.Default.ToolTipFormLocation.Y == 0))
{
_toolTipForm1.StartPosition = FormStartPosition.Manual;
_toolTipForm1.Location = new System.Drawing.Point(
Screen.PrimaryScreen.Bounds.Width / 2 - _toolTipForm1.Width / 2,
Screen.PrimaryScreen.Bounds.Height / 2 - _toolTipForm1.Height / 2);
}
else
{
_toolTipForm1.Location = Properties.Settings.Default.ToolTipFormLocation;
}
LoadSettings();
LoadMetrics();
InitializeTimer();
// Help tooltip when initializing
_notifyIcon.Text = "Left click to hide/show timer\r\nALT+F12 or Right click show menu";
// Add app version to the exit menu item
Version version = Assembly.GetExecutingAssembly().GetName().Version;
string versionString = $"Pomodoro Timer v{version.Major}.{version.Minor}";
exitToolStripMenuItem.Text += $" ({versionString})";
// Register ALT+F12 hotkey
RegisterHotKey(this.Handle, HOTKEY_ID_F12, 0x0001 /* MOD_ALT */, (uint)Keys.F12);
}
#endregion
#region Timer Control for Task/Break/Meeting
private void InitializeTimer()
{
_timer?.Stop();
_timer = new System.Windows.Forms.Timer
{
Interval = 1000
};
_timer.Tick += Timer_Tick;
_remainingTime = _taskDuration;
_currentStatus = TimerStatus.Task;
UpdateDisplay();
UpdateFullScreenStatus();
StartTimer();
}
private void StartTimer()
{
_timer.Start();
pauseToolStripMenuItem.Text = "Pause timer (&0)";
}
private void StopTimer()
{
_timer.Stop();
pauseToolStripMenuItem.Text = "Continue timer (&0)";
}
private void Timer_Tick(object sender, EventArgs e)
{
// Hide this loading window
if (WindowState != FormWindowState.Minimized)
{
WindowState = FormWindowState.Minimized;
this.Hide();
}
// Decrement the remaining time if not in meeting status
if (_currentStatus != TimerStatus.Meeting)
{
_remainingTime--;
}
else
{
_remainingTime++;
}
// Update the display
UpdateDisplay();
// Update the total task, break or meeting time
switch (_currentStatus)
{
case TimerStatus.Task:
_totalTaskTime++;
break;
case TimerStatus.Break:
_totalBreakTime++;
break;
case TimerStatus.Meeting:
_totalMeetingTime++;
break;
case TimerStatus.Lunch:
_totalLunchTime++;
break;
case TimerStatus.LongBreak:
_totalLongBreakTime++;
break;
}
// Increment _totalBreaksCount when switching from a break, lunch break, or long break to a different status
if ((_previousStatus == TimerStatus.Break || _previousStatus == TimerStatus.Lunch || _previousStatus == TimerStatus.LongBreak) &&
(_currentStatus != TimerStatus.Break && _currentStatus != TimerStatus.Lunch && _currentStatus != TimerStatus.LongBreak))
{
_totalBreaksCount++;
}
_previousStatus = _currentStatus;
// Play a melody if the remaining time is 10 seconds
if (_remainingTime == 10 && _currentStatus != TimerStatus.Meeting)
{
PlayMelody(_currentStatus == TimerStatus.Task);
}
// Switch to the other time if the remaining time is 0
if (_remainingTime == 0 && _currentStatus != TimerStatus.Meeting)
{
if (_currentStatus == TimerStatus.Task)
{
SwitchToBreak();
}
else
{
SwitchToTask();
}
// Update the total times
UpdateTotalTimes();
}
}
public void SwitchToTask()
{
PauseContinueTimer(pause: false);
TimerStatus oldStatus = _currentStatus;
_currentStatus = TimerStatus.Task;
_remainingTime = _taskDuration;
UpdateDisplay();
UpdateFullScreenStatus();
if (oldStatus != TimerStatus.Meeting &&
oldStatus != TimerStatus.Task)
{
SetTeamStatus("Busy");
}
}
private void SwitchToBreak()
{
PauseContinueTimer(pause: false);
SetTeamStatus("AwayBRB");
_currentStatus = TimerStatus.Break;
_remainingTime = _breakDuration;
UpdateDisplay();
UpdateFullScreenStatus();
}
private void SwitchToMeeting()
{
PauseContinueTimer(pause: false);
_currentStatus = TimerStatus.Meeting;
_remainingTime = 0;
UpdateDisplay();
UpdateFullScreenStatus();
}
private void SwitchToLunch()
{
PauseContinueTimer(pause: false);
SetTeamStatus("Away");
_currentStatus = TimerStatus.Lunch;
_remainingTime = 0;
UpdateDisplay();
UpdateFullScreenStatus();
}
private void SwitchToLongBreak()
{
PauseContinueTimer(pause: false);
SetTeamStatus("Away");
_currentStatus = TimerStatus.LongBreak;
_remainingTime = 0;
UpdateDisplay();
UpdateFullScreenStatus();
}
private void UpdateDisplay()
{
int hours = Math.Abs(_remainingTime) / 3600;
int minutes = (Math.Abs(_remainingTime) % 3600) / 60;
int seconds = Math.Abs(_remainingTime) % 60;
string time;
// Show time in 00:00:00 format
// With hours as optional
if (_currentStatus == TimerStatus.Meeting || _currentStatus == TimerStatus.Lunch)
{
time = $"{hours:00}:{minutes:00}:{seconds:00}";
// Increase _toolTipForm.Size by 13.5%
_toolTipForm1.Size = new System.Drawing.Size((int)(_originalToolTipFormSize.Width * 1.35), _originalToolTipFormSize.Height);
}
else if (hours > 0)
{
time = $"{hours:00}:{minutes:00}:{seconds:00}";
// Increase _toolTipForm.Size by 12%
_toolTipForm1.Size = new System.Drawing.Size((int)(_originalToolTipFormSize.Width * 1.2), _originalToolTipFormSize.Height);
}
else
{
time = $"{minutes:00}:{seconds:00}";
_toolTipForm1.Size = _originalToolTipFormSize;
}
// Get task, break, meeting, or lunch string
string statusText = Enum.GetName(typeof(TimerStatus), _currentStatus);
if (statusText == "LongBreak")
{
statusText = "Long Break";
}
// Create text with task, break, meeting, or lunch and time
string notificationText = $"{statusText} - {time}";
// Update tooltip text if required
if (_hideToolTipTimer)
{
_notifyIcon.Text = notificationText;
}
// Update times for break, long break, task, meeting, lunch, and total
UpdateTotalTimes();
// Update big tooltip text
ShowToolTip(notificationText);
}
private void UpdateFullScreenStatus()
{
// Change icon
if (_currentStatus == TimerStatus.Task || _currentStatus == TimerStatus.Meeting || _currentStatus == TimerStatus.Lunch)
{
_notifyIcon.Icon = new System.Drawing.Icon(GetType().Assembly.GetManifestResourceStream("PomodoroTimer.Resources.greenball.ico"));
}
else
{
_notifyIcon.Icon = new System.Drawing.Icon(GetType().Assembly.GetManifestResourceStream("PomodoroTimer.Resources.redball.ico"));
}
// Show break as full screen or regular break
// Full screen lunch and long break
if ((_currentStatus == TimerStatus.Break && Properties.Settings.Default.BreakFullScreen) ||
(_currentStatus == TimerStatus.Lunch || _currentStatus == TimerStatus.LongBreak))
{
Screen currentScreen = Screen.FromPoint(_toolTipForm1.Location);
Screen otherScreen = Screen.AllScreens.FirstOrDefault(s => s.Primary != currentScreen.Primary);
_toolTipForm1.SetFullScreen(true, _currentStatus);
if (otherScreen != null)
{
_toolTipForm2.SetFullScreen(true, _currentStatus, otherScreen);
_toolTipForm2.Visible = true;
}
else
{
_toolTipForm2.Visible = false;
}
}
else
{
_toolTipForm1.SetFullScreen(false, _currentStatus);
_toolTipForm2.SetFullScreen(false, _currentStatus);
_toolTipForm2.Visible = false;
}
}
private void UpdateTotalTimes()
{
// Update Break/Task/Meeting/Total times
totalBreaksTimeToolStripMenuItem.Text = $"Total Break Time: {TimeSpan.FromSeconds(_totalBreakTime):hh\\:mm\\:ss}";
totalTasksTimeToolStripMenuItem.Text = $"Total Task Time: {TimeSpan.FromSeconds(_totalTaskTime):hh\\:mm\\:ss}";
totalMeetingTimeToolStripMenuItem.Text = $"Total Meeting Time: {TimeSpan.FromSeconds(_totalMeetingTime):hh\\:mm\\:ss}";
totalLunchTimeToolStripMenuItem.Text = $"Total Lunch Time: {TimeSpan.FromSeconds(_totalLunchTime):hh\\:mm\\:ss}";
totalLongBreakTimeToolStripMenuItem.Text = $"Total Long Break Time: {TimeSpan.FromSeconds(_totalLongBreakTime):hh\\:mm\\:ss}";
// Calculate total work time (task + meeting)
int totalWorkTime = _totalTaskTime + _totalMeetingTime;
totalWorkTimeToolStripMenuItem.Text = $"Total Work Time: {TimeSpan.FromSeconds(totalWorkTime):hh\\:mm\\:ss}";
// Calculate total rest time (break + long break + lunch)
int totalRestTime = _totalBreakTime + _totalLongBreakTime + _totalLunchTime;
totalRestTimeToolStripMenuItem.Text = $"Total Rest Time: {TimeSpan.FromSeconds(totalRestTime):hh\\:mm\\:ss}";
// Calculate total time today (work + rest)
int totalTime = totalWorkTime + totalRestTime;
totalTimeToolStripMenuItem.Text = $"Total Time Today: {TimeSpan.FromSeconds(totalTime):hh\\:mm\\:ss}";
// Update total breaks count
totalBreaksCountToolStripMenuItem.Text = $"Total Breaks Today: {_totalBreaksCount}";
}
#endregion
#region Hide/Show Timer and Notifications
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
// If right click, show context menu
if (e.Button == MouseButtons.Right)
{
// Delay to avoid menu closing automatically sometimes
System.Threading.Tasks.Task.Delay(100).ContinueWith(_ =>
{
Invoke(new Action(() =>
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(_notifyIcon, null);
}));
});
_notifyIcon.Text = "";
}
// If left click, hide/show timer
if (e.Button == MouseButtons.Left)
{
_hideToolTipTimer = !_hideToolTipTimer;
if (!_hideToolTipTimer)
{
_notifyIcon.Text = "";
}
}
}
private void ShowToolTip(string message)
{
_toolTipForm1.SetToolTip(message);
if (_toolTipForm2.Visible)
{
_toolTipForm2.SetToolTip(message);
}
if (!_hideToolTipTimer)
{
_toolTipForm1.Show();
}
else
{
_toolTipForm1.Hide();
}
}
public void PlayMelody(bool isTask)
{
Task.Run(() =>
{
int tempo = 300;
int[] frequencies;
int[] durations;
if (isTask)
{
// Task sound
frequencies = new int[] { 523, 523 };
durations = new int[] { tempo, tempo };
}
else
{
// Break sound
frequencies = new int[] { 1047, 1047 };
durations = new int[] { tempo, tempo };
}
for (int i = 0; i < frequencies.Length; i++)
{
Console.Beep(frequencies[i], durations[i]);
System.Threading.Thread.Sleep(tempo / 4);
}
});
}
#endregion
#region Context Menus
private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
{
PauseContinueTimer(_timer.Enabled);
}
public void PauseContinueTimer(bool pause)
{
if (pause)
{
StopTimer();
pauseToolStripMenuItem.Text = "Continue timer (&0)";
}
else
{
StartTimer();
pauseToolStripMenuItem.Text = "Pause timer (&0)";
}
}
private void goToBreakToolStripMenuItem_Click(object sender, EventArgs e)
{
SwitchToBreak();
}
private void goToTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
SwitchToTask();
}
private void goToMeetingToolStripMenuItem_Click(object sender, EventArgs e)
{
SwitchToMeeting();
}
private void goToLunchToolStripMenuItem_Click(object sender, EventArgs e)
{
SwitchToLunch();
}
private void goToLongBreakToolStripMenuItem_Click(object sender, EventArgs e)
{
SwitchToLongBreak();
}
private void totalTasksTimeToolStripMenuItem_Click(object sender, EventArgs e)
{
goToTaskToolStripMenuItem_Click(sender, e);
}
private void totalMeetingTimeToolStripMenuItem_Click(object sender, EventArgs e)
{
goToMeetingToolStripMenuItem_Click((object)sender, e);
}
private void totalBreaksTimeToolStripMenuItem_Click(object sender, EventArgs e)
{
goToBreakToolStripMenuItem_Click((object)sender, e);
}
private void totalLongBreakTimeToolStripMenuItem_Click(object sender, EventArgs e)
{
goToLongBreakToolStripMenuItem_Click((object)sender, e);
}
private void totalLunchTimeToolStripMenuItem_Click(object sender, EventArgs e)
{
goToLunchToolStripMenuItem_Click((object)sender, e);
}
private void configureToolStripMenuItem_Click(object sender, EventArgs e)
{
// Open configuration form
ConfigurationForm configForm = new ConfigurationForm
{
FullScreenBreak = _fullScreenBreak,
TaskDuration = _taskDuration,
BreakDuration = _breakDuration
};
// Save configuration if saved button was clicked
if (configForm.ShowDialog() == DialogResult.OK)
{
_taskDuration = configForm.TaskDuration;
_breakDuration = configForm.BreakDuration;
_fullScreenBreak = configForm.FullScreenBreak;
SaveSettings();
InitializeTimer();
}
}
#endregion
#region Teams Status Automation
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
private static Point GetTeamsTrayIconPosition()
{
AutomationElement teamsIcon = GetTeamsTrayIcon();
if (teamsIcon != null)
{
System.Windows.Rect iconRectWin = teamsIcon.Current.BoundingRectangle;
Rectangle iconRect = new Rectangle((int)iconRectWin.Left, (int)iconRectWin.Top, (int)iconRectWin.Right - (int)iconRectWin.Left, (int)iconRectWin.Bottom - (int)iconRectWin.Top);
Point iconCenter = new Point(iconRect.Left + (iconRect.Width / 2), iconRect.Top + (iconRect.Height / 2));
return new Point(iconCenter.X, iconCenter.Y);
}
else
{
return new Point(0, 0);
}
}
private static AutomationElement GetTeamsTrayIcon()
{
// Note: name for automation elements can be found with
// Inspect.exe tool from Windows SDK
// Sample path: C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\inspect.exe
// Get desktop
AutomationElement desktop = AutomationElement.RootElement;
// Get tray icons
AutomationElement trayIcons = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Shell_TrayWnd"));
if (trayIcons != null)
{
// Try to find the Teams icon in the tray with name "Microsoft Teams"
AutomationElement teamsIcon = trayIcons.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Microsoft Teams"));
if (teamsIcon == null)
{
// Try to find the Teams icon in the tray with name "Microsoft Teams | Dell Technologies"
teamsIcon = trayIcons.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Microsoft Teams | Dell Technologies"));
}
if (teamsIcon == null)
{
// Try to find the Teams icon in the tray with name "Microsoft Teams Microsoft Teams | Dell Technologies"
teamsIcon = trayIcons.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Microsoft Teams Microsoft Teams | Dell Technologies"));
}
return teamsIcon;
}
return null;
}
public static void SetTeamStatus(string status)
{
// Check if the "MS Teams" process is running
Process[] processes = Process.GetProcessesByName("ms-teams");
if (processes.Length == 0)
{
// "MS Teams" process is not running, so return
return;
}
Thread.Sleep(500);
Point teamIconPosition = GetTeamsTrayIconPosition();
// Could not find msteams tray icon position
if (teamIconPosition.X == 0)
return;
// Right-click in Teams tray icon
SetCursorPos(teamIconPosition.X, teamIconPosition.Y);
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
// Select My Status menu
Thread.Sleep(1000);
Point myStatusMenuPosition = GetMyStatusMenuPosition(teamIconPosition);
SetCursorPos(myStatusMenuPosition.X, myStatusMenuPosition.Y);
Thread.Sleep(1000);
Point statusOptionPosition = GetStatusOptionPosition(status, teamIconPosition);
SetCursorPos(statusOptionPosition.X, statusOptionPosition.Y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Thread.Sleep(1000);
// Place cursor in the center of screen
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
int centerX = screenWidth / 2;
int centerY = screenHeight / 2;
SetCursorPos(centerX, centerY);
}
private static Point GetMyStatusMenuPosition(Point teamIconPosition)
{
// Calculate the position of the "My Status" menu based on the team icon position
int myStatusMenuX = teamIconPosition.X - 205;
int myStatusMenuY = teamIconPosition.Y - 110;
return new Point(myStatusMenuX, myStatusMenuY);
}
private static Point GetStatusOptionPosition(string status, Point teamIconPosition)
{
int statusOptionX = teamIconPosition.X + 152;
int statusOptionY;
if (status.Equals("Away", StringComparison.OrdinalIgnoreCase))
{
// Set as status "Away"
statusOptionY = teamIconPosition.Y - 175;
}
else if (status.Equals("AwayBRB", StringComparison.OrdinalIgnoreCase))
{
// Set as status "Away - I will be right back"
statusOptionY = teamIconPosition.Y - 215;
}
else if (status.Equals("Busy", StringComparison.OrdinalIgnoreCase))
{
// Set as status "Busy"
statusOptionY = teamIconPosition.Y - 277;
}
else
{
// Default status option position (e.g., "Available")
statusOptionY = teamIconPosition.Y - 307;
}
return new Point(statusOptionX, statusOptionY);
}
#endregion
#region Global Key Handlers
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private const int HOTKEY_ID_F12 = 12;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312) // WM_HOTKEY
{
int hotkeyId = m.WParam.ToInt32();
if (hotkeyId == HOTKEY_ID_F12)
{
ShowContextMenu();
}
}
}
private void ShowContextMenu()
{
// Close if required
if (ContextMenu.Visible)
{
ContextMenu.Hide();
}
else
{
// Open if required
Point mousePosition = Control.MousePosition;
ContextMenu.Show(mousePosition);
}
}
#endregion
#region Load/Save settings
private void LoadSettings()
{
_taskDuration = int.Parse(Properties.Settings.Default.TaskDuration);
_breakDuration = int.Parse(Properties.Settings.Default.BreakDuration);
_fullScreenBreak = Properties.Settings.Default.BreakFullScreen;
}
private void SaveSettings()
{
Properties.Settings.Default.TaskDuration = _taskDuration.ToString();
Properties.Settings.Default.BreakDuration = _breakDuration.ToString();
Properties.Settings.Default.BreakFullScreen = _fullScreenBreak;
Properties.Settings.Default.Save();
}
#endregion
#region Save/Load Metrics
private void SaveMetrics()
{
MetricsReportManager.SaveMetricsReport(DateTime.Today, _totalTaskTime, _totalMeetingTime, _totalBreakTime, _totalLongBreakTime, _totalLunchTime, _totalBreaksCount);
}
private void LoadMetrics()
{
(_totalTaskTime, _totalMeetingTime, _totalBreakTime, _totalLongBreakTime, _totalLunchTime, _totalBreaksCount, _) = MetricsReportManager.LoadMetricsReport(DateTime.Today);
}
#endregion
#region Closing/Exiting
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
// Unregister F12 hotkey global handler
UnregisterHotKey(this.Handle, HOTKEY_ID_F12);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveMetrics();
SaveSettings();
Application.Exit();
}
#endregion
#region Design
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this._notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
this.ContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
this.pauseToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItemSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.goToTaskToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.goToMeetingToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.goToBreakToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.goToLongBreakToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.goToLunchToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItemSeparator3 = new System.Windows.Forms.ToolStripSeparator();
this.configureToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItemSeparator4 = new System.Windows.Forms.ToolStripSeparator();
this.totalTasksTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.totalMeetingTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.totalBreaksTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.totalLongBreakTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.totalLunchTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator();
this.totalWorkTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.totalRestTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.totalTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.totalBreaksCountToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItemSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this._timer = new System.Windows.Forms.Timer(this.components);
this.toolTip = new System.Windows.Forms.ToolTip(this.components);
this.lblText = new System.Windows.Forms.Label();
this.ContextMenu.SuspendLayout();
this.SuspendLayout();
//
// _notifyIcon
//
this._notifyIcon.ContextMenuStrip = this.ContextMenu;
this._notifyIcon.Text = "notifyIcon1";
this._notifyIcon.Visible = true;
this._notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseClick);
//
// ContextMenu
//
this.ContextMenu.ImageScalingSize = new System.Drawing.Size(40, 40);
this.ContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.pauseToolStripMenuItem,
this.toolStripMenuItemSeparator2,
this.goToTaskToolStripMenuItem,
this.goToMeetingToolStripMenuItem,
this.goToBreakToolStripMenuItem,
this.goToLongBreakToolStripMenuItem,
this.goToLunchToolStripMenuItem,
this.toolStripMenuItemSeparator3,
this.configureToolStripMenuItem,
this.toolStripMenuItemSeparator4,
this.totalTasksTimeToolStripMenuItem,
this.totalMeetingTimeToolStripMenuItem,
this.totalBreaksTimeToolStripMenuItem,
this.totalLongBreakTimeToolStripMenuItem,
this.totalLunchTimeToolStripMenuItem,
this.toolStripMenuItem1,
this.totalWorkTimeToolStripMenuItem,
this.totalRestTimeToolStripMenuItem,
this.totalTimeToolStripMenuItem,
this.totalBreaksCountToolStripMenuItem,
this.toolStripMenuItemSeparator1,
this.exitToolStripMenuItem});
this.ContextMenu.Name = "ContextMenu";
this.ContextMenu.Size = new System.Drawing.Size(514, 905);
//
// pauseToolStripMenuItem
//
this.pauseToolStripMenuItem.Name = "pauseToolStripMenuItem";
this.pauseToolStripMenuItem.Size = new System.Drawing.Size(513, 48);
this.pauseToolStripMenuItem.Text = "Pause timer (&0)";
this.pauseToolStripMenuItem.Click += new System.EventHandler(this.pauseToolStripMenuItem_Click);
//
// toolStripMenuItemSeparator2
//
this.toolStripMenuItemSeparator2.Name = "toolStripMenuItemSeparator2";
this.toolStripMenuItemSeparator2.Size = new System.Drawing.Size(510, 6);
//
// goToTaskToolStripMenuItem
//
this.goToTaskToolStripMenuItem.Name = "goToTaskToolStripMenuItem";
this.goToTaskToolStripMenuItem.Size = new System.Drawing.Size(513, 48);
this.goToTaskToolStripMenuItem.Text = "Start Task (&1)";
this.goToTaskToolStripMenuItem.Click += new System.EventHandler(this.goToTaskToolStripMenuItem_Click);
//
// goToMeetingToolStripMenuItem
//
this.goToMeetingToolStripMenuItem.Name = "goToMeetingToolStripMenuItem";
this.goToMeetingToolStripMenuItem.Size = new System.Drawing.Size(513, 48);
this.goToMeetingToolStripMenuItem.Text = "Start Meeting (&2)";
this.goToMeetingToolStripMenuItem.Click += new System.EventHandler(this.goToMeetingToolStripMenuItem_Click);
//
// goToBreakToolStripMenuItem
//
this.goToBreakToolStripMenuItem.Name = "goToBreakToolStripMenuItem";
this.goToBreakToolStripMenuItem.Size = new System.Drawing.Size(513, 48);
this.goToBreakToolStripMenuItem.Text = "Start Break (&3)";
this.goToBreakToolStripMenuItem.Click += new System.EventHandler(this.goToBreakToolStripMenuItem_Click);
//
// goToLongBreakToolStripMenuItem
//
this.goToLongBreakToolStripMenuItem.Name = "goToLongBreakToolStripMenuItem";
this.goToLongBreakToolStripMenuItem.Size = new System.Drawing.Size(513, 48);
this.goToLongBreakToolStripMenuItem.Text = "Start Long Break (&4)";
this.goToLongBreakToolStripMenuItem.Click += new System.EventHandler(this.goToLongBreakToolStripMenuItem_Click);
//
// goToLunchToolStripMenuItem
//
this.goToLunchToolStripMenuItem.Name = "goToLunchToolStripMenuItem";
this.goToLunchToolStripMenuItem.Size = new System.Drawing.Size(513, 48);
this.goToLunchToolStripMenuItem.Text = "Start Lunch (&5)";
this.goToLunchToolStripMenuItem.Click += new System.EventHandler(this.goToLunchToolStripMenuItem_Click);
//
// toolStripMenuItemSeparator3
//
this.toolStripMenuItemSeparator3.Name = "toolStripMenuItemSeparator3";
this.toolStripMenuItemSeparator3.Size = new System.Drawing.Size(510, 6);
//
// configureToolStripMenuItem
//
this.configureToolStripMenuItem.Name = "configureToolStripMenuItem";
this.configureToolStripMenuItem.Size = new System.Drawing.Size(513, 48);
this.configureToolStripMenuItem.Text = "Configure";
this.configureToolStripMenuItem.Click += new System.EventHandler(this.configureToolStripMenuItem_Click);
//
// toolStripMenuItemSeparator4
//
this.toolStripMenuItemSeparator4.Name = "toolStripMenuItemSeparator4";
this.toolStripMenuItemSeparator4.Size = new System.Drawing.Size(510, 6);
//
// totalTasksTimeToolStripMenuItem
//
this.totalTasksTimeToolStripMenuItem.Name = "totalTasksTimeToolStripMenuItem";
this.totalTasksTimeToolStripMenuItem.Size = new System.Drawing.Size(513, 48);