-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
243 lines (227 loc) · 8.86 KB
/
MainWindow.xaml.cs
File metadata and controls
243 lines (227 loc) · 8.86 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
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Drawing;
using System.Windows.Forms;
namespace Timer
{
public partial class MainWindow : Window
{
private NotifyIcon _notifyIcon;
ulong Time;
ulong PausedTime; // This is time when timer stopped
bool IsWorking = false; // The timer is working now?
public static bool UnitInSeconds;
public static string Theme;
// For accuracy
DateTime TimePassedAt = new DateTime();
TimeSpan Span = new TimeSpan();
MediaPlayer player = new MediaPlayer(); // For the alarm
public MainWindow()
{
InitializeComponent();
UpdateTheme();
if (Properties.Settings.Default.UnitInSeconds) UnitInSeconds = true; else UnitInSeconds = false;
}
public static void UpdateTheme()
{
if (!Properties.Settings.Default.DarkTheme) Theme = "Themes/Light"; else Theme = "Themes/Dark";
ResourceDictionary resourceDict = System.Windows.Application.LoadComponent(new Uri($"{Theme}.xaml", UriKind.Relative)) as ResourceDictionary;
System.Windows.Application.Current.Resources.Clear();
System.Windows.Application.Current.Resources.MergedDictionaries.Add(resourceDict);
}
private async void Bt_Start_Click(object sender, RoutedEventArgs e)
{
FreezeButtons();
await Task.Run(() =>
{
Thread.Sleep(255);
ButtonsSwitch(true);
});
try
{
Time = Convert.ToUInt32(TBox_Time.Text);
if (!UnitInSeconds) Time *= 60;
await Task.Run(() => WTimer());
}
catch (Exception)
{
TBlock_TimeLeft.Text = "Enter positive number!";
ButtonsSwitch(false);
}
}
private void Bt_Stop_Click(object sender, RoutedEventArgs e)
{
IsWorking = false;
Time = 0;
Dispatcher.Invoke(new Action(() => TBlock_TimeLeft.Text = $"Time left: 00 : 00 : 00"));
Thread.Sleep(255);
ButtonsSwitch(false);
}
private void Bt_Pause_Click(object sender, RoutedEventArgs e)
{
PausedTime = Time;
IsWorking = false;
Time = 0;
Thread.Sleep(255);
PauseSwitch(true);
}
private async void Bt_Resume_Click(object sender, RoutedEventArgs e)
{
FreezeButtons();
await Task.Run(() =>
{
Thread.Sleep(255);
PauseSwitch(false);
});
IsWorking = true;
Time = PausedTime;
await Task.Run(() => WTimer());
}
private void Bt_Settings_Click(object sender, RoutedEventArgs e) => _ = new SettingsWindow();
private void WTimer()
{
TimePassedAt = DateTime.Now.AddSeconds(Convert.ToDouble(Time));
IsWorking = true;
ulong Working = 0;
ulong STime; byte h; byte m; byte s; int hh; byte mm; byte ss;
while (Time != 0)
{
Time -= 1;
++Working;
STime = Time;
hh = (byte)(STime / 36000); STime -= Convert.ToUInt64(36000 * hh);
h = (byte)(STime / 3600); STime -= Convert.ToUInt64(3600 * h);
mm = (byte)(STime / 600); STime -= Convert.ToUInt64(600 * mm);
m = (byte)(STime / 60); STime -= Convert.ToUInt64(60 * m);
ss = (byte)(STime / 10); STime -= Convert.ToUInt64(10 * ss);
s = (byte)(STime);
Dispatcher.Invoke(new Action(() => TBlock_TimeLeft.Text = $"Time left: {hh}{h} : {mm}{m} : {ss}{s}"));
Thread.Sleep(250);
if (!IsWorking) break;
Thread.Sleep(250);
if (!IsWorking) break;
Thread.Sleep(250);
if (!IsWorking) break;
Thread.Sleep(250); // Немного говнокода))
if (Working > 200)
{
Working = 0;
Span = TimePassedAt - DateTime.Now;
Time = Convert.ToUInt64(Span.TotalSeconds);
}
}
if (IsWorking)
{
try
{
Dispatcher.Invoke(new Action(() =>
{
player.Open(new Uri($"{Environment.CurrentDirectory}\\Resources\\Bell.mp3"));
player.Volume = 1d;
player.Play();
}));
_notifyIcon = new NotifyIcon
{
BalloonTipText = @"Time is passed!",
Text = @"Timer",
Icon = new Icon("Resources//Icon.ico"),
Visible = true
};
_notifyIcon.ShowBalloonTip(1000);
}
catch (Exception e)
{
System.Windows.MessageBox.Show("Error: Bell.mp3 not found!\n " + e);
}
finally
{
ButtonsSwitch(false);
}
}
IsWorking = false;
Dispatcher.Invoke(new Action(() => TBlock_TimeLeft.Text = $"Time left: 00 : 00 : 00"));
}
/// <param name="TimerIsWorking">Is the timer working?</param>
private void ButtonsSwitch(bool TimerIsWorking)
{
if(TimerIsWorking)
{
Dispatcher.Invoke(new Action(() =>
{
Bt_Start.IsEnabled = false;
Bt_Start.Visibility = Visibility.Hidden;
Bt_Pause.IsEnabled = true;
Bt_Pause.Visibility = Visibility.Visible;
Bt_Stop.IsEnabled = true;
Bt_Stop.Visibility = Visibility.Visible;
Bt_Resume.IsEnabled = false;
Bt_Resume.Visibility = Visibility.Hidden;
}));
}
else
{
Dispatcher.Invoke(new Action(() =>
{
Bt_Start.IsEnabled = true;
Bt_Start.Visibility = Visibility.Visible;
Bt_Pause.IsEnabled = false;
Bt_Pause.Visibility = Visibility.Hidden;
Bt_Stop.IsEnabled = false;
Bt_Stop.Visibility = Visibility.Hidden;
Bt_Resume.IsEnabled = false;
Bt_Resume.Visibility = Visibility.Hidden;
}));
}
}
/// <param>Turn off all buttons</param>
private void FreezeButtons()
{
Dispatcher.Invoke(new Action(() =>
{
Bt_Start.IsEnabled = false;
Bt_Start.Visibility = Visibility.Hidden;
Bt_Pause.IsEnabled = false;
Bt_Pause.Visibility = Visibility.Hidden;
Bt_Stop.IsEnabled = false;
Bt_Stop.Visibility = Visibility.Hidden;
Bt_Resume.IsEnabled = false;
Bt_Resume.Visibility = Visibility.Hidden;
}));
}
/// <param name="IsEnabled">Is the pause enabled?</param>
private void PauseSwitch(bool IsEnabled)
{
if(IsEnabled)
{
Dispatcher.Invoke(new Action(() =>
{
Bt_Start.IsEnabled = false;
Bt_Start.Visibility = Visibility.Hidden;
Bt_Pause.IsEnabled = false;
Bt_Pause.Visibility = Visibility.Hidden;
Bt_Stop.IsEnabled = true;
Bt_Stop.Visibility = Visibility.Visible;
Bt_Resume.IsEnabled = true;
Bt_Resume.Visibility = Visibility.Visible;
}));
}
else
{
Dispatcher.Invoke(new Action(() =>
{
Bt_Start.IsEnabled = false;
Bt_Start.Visibility = Visibility.Hidden;
Bt_Pause.IsEnabled = true;
Bt_Pause.Visibility = Visibility.Visible;
Bt_Stop.IsEnabled = true;
Bt_Stop.Visibility = Visibility.Visible;
Bt_Resume.IsEnabled = false;
Bt_Resume.Visibility = Visibility.Hidden;
}));
}
}
}
}