-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWeekApplicationContext.cs
159 lines (135 loc) · 4.82 KB
/
WeekApplicationContext.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
#region Using statements
using Microsoft.Win32;
using System;
using System.Windows.Forms;
#endregion Using statements
namespace WeekNumber
{
internal class WeekApplicationContext : ApplicationContext
{
#region Internal Taskbar GUI
internal IGui Gui;
#endregion Internal Taskbar GUI
#region Private variables
private readonly Timer _timer;
private int _currentWeek;
private int _lastIconRes;
#endregion Private variables
#region Constructor
internal WeekApplicationContext()
{
try
{
Log.LogCaller();
Settings.StartWithWindows = Settings.SettingIsValue(Resources.StartWithWindows, true.ToString());
Application.ApplicationExit += OnApplicationExit;
SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged;
_currentWeek = Week.Current();
_lastIconRes = WeekIcon.GetIconResolution();
Gui = new TaskbarGui(_currentWeek, _lastIconRes);
Gui.UpdateRequest += GuiUpdateRequestHandler;
_timer = GetTimer;
}
catch (Exception ex)
{
_timer?.Stop();
Log.LogCaller();
Message.Show(Resources.UnhandledException, ex);
Application.Exit();
}
}
#endregion Constructor
#region Private Timer property
private Timer GetTimer
{
get
{
if (_timer != null)
{
return _timer;
}
//int calculatedInterval = 86400000 - ((DateTime.Now.Hour * 3600000) + (DateTime.Now.Minute * 60000) + (DateTime.Now.Second * 1000));
int calculatedInterval = 10000; // workaround for blurry icon due to Windows icon rendering bug, update icon every 10 seconds instead of when week change occurs only
Timer timer = new Timer
{
Interval = calculatedInterval,
Enabled = true
};
Log.Info = $"Timer interval={calculatedInterval / 1000}s";
timer.Tick += OnTimerTick;
return timer;
}
}
#endregion Private Timer property
#region Private event handlers
private void GuiUpdateRequestHandler(object sender, EventArgs e)
{
Log.LogCaller();
UpdateIcon(true, true);
}
private void OnApplicationExit(object sender, EventArgs e)
{
Log.LogCaller();
Cleanup(false);
}
private void OnUserPreferenceChanged(object sender, EventArgs e)
{
Log.LogCaller();
int iconRes = WeekIcon.GetIconResolution(true);
if (iconRes != _lastIconRes)
{
UpdateIcon(true, true);
_lastIconRes = iconRes;
}
}
private void OnTimerTick(object sender, EventArgs e)
{
UpdateIcon();
}
private void UpdateIcon(bool force = false, bool redrawContextMenu = false)
{
if (_currentWeek == Week.Current() && force == false)
{
return;
}
_timer?.Stop();
Application.DoEvents();
try
{
Log.LogCaller();
_currentWeek = Week.Current();
int iconResolution = Settings.GetIntSetting(Resources.IconResolution, (int)IconSize.Icon256);
Log.Info = $"Update icon with week number {_currentWeek} using resolution {iconResolution}x{iconResolution}, redraw context menu={redrawContextMenu}, forced update={force}";
Gui?.UpdateIcon(_currentWeek, iconResolution, redrawContextMenu);
}
catch (Exception ex)
{
Message.Show(Resources.FailedToSetIcon, ex);
Cleanup();
throw;
}
if (_timer != null)
{
int calculatedInterval = 86400000 - ((DateTime.Now.Hour * 3600000) + (DateTime.Now.Minute * 60000) + (DateTime.Now.Second * 1000));
_timer.Interval = calculatedInterval;
Log.Info = $"Timer interval={calculatedInterval / 1000}s";
_timer.Start();
}
}
#endregion Private event handlers
#region Private methods
private void Cleanup(bool forceExit = true)
{
Log.LogCaller();
_timer?.Stop();
_timer?.Dispose();
Gui?.Dispose();
Gui = null;
if (forceExit)
{
Application.Exit();
}
}
#endregion Private methods
}
}