-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskbarGui.cs
136 lines (111 loc) · 3.91 KB
/
TaskbarGui.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
#region Using statements
using System.Runtime.Versioning;
using WeekNumberLite2.Properties;
#endregion
namespace WeekNumberLite2
{
internal class TaskbarGui : IDisposable, IGui
{
#region Private variables
private NotifyIcon? _notifyIcon;
private readonly WeekNumberLite2ContextMenu _contextMenu;
private int _latestWeek;
#endregion Private variables
#region Constructor
[SupportedOSPlatform("windows")]
internal TaskbarGui(int week)
{
_latestWeek = week;
_contextMenu = new WeekNumberLite2ContextMenu();
_notifyIcon = GetNotifyIcon(_contextMenu.ContextMenu);
UpdateIcon(week, ref _notifyIcon);
}
#endregion Constructor
#region Public UpdateIcon method
/// <summary>
/// Updates icon on GUI with given week number
/// </summary>
/// <param name="weekNumber">The week number to display on icon</param>
[SupportedOSPlatform("windows")]
public void UpdateIcon(int weekNumber)
{
UpdateIcon(weekNumber, ref _notifyIcon);
}
#endregion Public UpdateIcon method
#region Private UpdateIcon method
[SupportedOSPlatform("windows")]
private void UpdateIcon(int weekNumber, ref NotifyIcon? notifyIcon)
{
try
{
string weekDayPrefix = string.Empty;
string longDateString = DateTime.Now.ToLongDateString();
const string SWEDISH_LONG_DATE_PREFIX_STRING = "den ";
if (Thread.CurrentThread.CurrentUICulture.Name == Resources.Swedish || longDateString.StartsWith(SWEDISH_LONG_DATE_PREFIX_STRING))
{
weekDayPrefix = Message.SWEDISH_DAY_OF_WEEK_PREFIX[(int)DateTime.Now.DayOfWeek];
}
if (notifyIcon != null)
{
notifyIcon.Text = $"{Resources.Week} {weekNumber}\r\n{weekDayPrefix}{DateTime.Now.ToLongDateString()}";
Icon prevIcon = notifyIcon.Icon;
notifyIcon.Icon = WeekIcon.GetIcon(weekNumber);
WeekIcon.CleanupIcon(ref prevIcon);
}
}
finally
{
if (_latestWeek != weekNumber)
{
_latestWeek = weekNumber;
}
}
}
#endregion Private UpdateIcon method
#region Private helper property to create NotifyIcon
private static NotifyIcon GetNotifyIcon(ContextMenuStrip? contextMenu)
{
return new() { Visible = true, ContextMenuStrip = contextMenu };
}
#endregion Private helper property to create NotifyIcon
#region IDisposable methods
/// <summary>
/// Disposes the GUI resources
/// </summary>
[SupportedOSPlatform("windows")]
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
[SupportedOSPlatform("windows")]
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
CleanupNotifyIcon();
_contextMenu.Dispose();
}
[SupportedOSPlatform("windows")]
private void CleanupNotifyIcon()
{
if (_notifyIcon is null)
{
return;
}
_notifyIcon.Visible = false;
if (_notifyIcon.Icon != null)
{
_ = NativeMethods.DestroyIcon(_notifyIcon.Icon.Handle);
_notifyIcon.Icon?.Dispose();
}
_notifyIcon.ContextMenuStrip?.Items.Clear();
_notifyIcon.ContextMenuStrip?.Dispose();
_notifyIcon.Dispose();
_notifyIcon = null;
}
#endregion IDisposable methods
}
}