This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
214 lines (192 loc) · 7.2 KB
/
MainWindow.xaml.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
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Threading;
namespace Timer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public readonly struct TimerStruct
{
public TimerStruct(int hours, int minutes, int seconds, string title = "")
{
Hours = hours;
Minutes = minutes;
Seconds = seconds;
Title = title;
}
public string Title { get; }
public int Hours { get; }
public int Minutes { get; }
public int Seconds { get; }
public string AsText
{
get
{
string text = "";
text = Hours == 0 ? "00:" : Hours < 10 ? "0" + Hours + ":" : Hours + ":";
text += Minutes == 0 ? "00:" : Minutes < 10 ? "0" + Minutes + ":" : Minutes + ":";
text += Seconds == 0 ? "00" : Seconds < 10 ? "0" + Seconds: Seconds;
return text;
}
}
public int ToSeconds => Hours * 60 * 60 + Minutes * 60 + Seconds;
}
private readonly DispatcherTimer TimerSelf = new();
private readonly OpenFileDialog OpenFileDialog = new();
private readonly MediaPlayer MediaPlayer = new();
public MainWindow()
{
InitializeComponent();
List.SelectionChanged += List_SelectionChanged;
TimerSelf.Tick += TimerSelf_Tick;
TimerSelf.Interval = new TimeSpan(0, 0, 0, 1);
}
private int seconds;
private void TimerSelf_Tick(object sender, EventArgs e)
{
seconds -= 1;
ProgressBar.Value = CurrentTimer.ToSeconds - seconds;
TimerOne.Text = $"{seconds / 3600:00}:{seconds / 60 % 60:00}:{seconds % 60:00}";
//TimerOne.Dispatcher.Invoke(() =>
//{
//});
if (seconds > 0) return;
TimerSelf.Stop();
MediaPlayer.Play();
//Dispatcher.Invoke(() => );
}
public void AddTimerToList(TimerStruct timer)
{
List.Items.Add(timer);
}
private void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = List.SelectedItem;
var isNull = item == null;
if (!isNull)
{
CurrentTimer = (TimerStruct)List.SelectedItem;
ProgressBar.Maximum = seconds;
Title.Text = CurrentTimer.Title;
TimerOne.Text = CurrentTimer.AsText;
RemoveBtn.Visibility = Visibility.Visible;
StartBtn.Visibility = Visibility.Visible;
PauseBtn.Visibility = Visibility.Visible;
DropBtn.Visibility = Visibility.Visible;
NextBtn.Visibility = Visibility.Visible;
MusicBtn.Visibility = Visibility.Visible;
}
else
{
Title.Text = "";
TimerOne.Text = "";
RemoveBtn.Visibility = Visibility.Collapsed;
StartBtn.Visibility = Visibility.Collapsed;
PauseBtn.Visibility = Visibility.Collapsed;
DropBtn.Visibility = Visibility.Collapsed;
NextBtn.Visibility = Visibility.Collapsed;
MusicBtn.Visibility = Visibility.Collapsed;
}
}
private TimerStruct _CurrentStruct;
private TimerStruct CurrentTimer
{
get => _CurrentStruct;
set
{
seconds = (value.Hours * 60 * 60) + value.Minutes * 60 + value.Seconds;
Title.Text = CurrentTimer.Title;
TimerOne.Text = CurrentTimer.AsText;
_CurrentStruct = value;
ProgressBar.Visibility = Visibility.Visible;
}
}
private void OnBackgroundClick(object sender, MouseButtonEventArgs e) => Keyboard.ClearFocus();
private double CalculateSize(double size, int delta)
{
size += delta > 0 ? +1 : -1;
size = size < 14 ? 14 : size;
return size;
}
private void OnMouseWheelResize(object sender, MouseWheelEventArgs e)
{
switch (sender)
{
case TextBlock textBlock:
textBlock.FontSize = CalculateSize(textBlock.FontSize, e.Delta);
break;
case TextBox textBox:
textBox.FontSize = CalculateSize(textBox.FontSize, e.Delta);;
break;
}
}
private void OnClickAddTimer(object sender, RoutedEventArgs e)
{
ModalControl.Content ??= new AddTimerModal(this);
ModalControl.Visibility = Visibility.Visible;
((BlurEffect)Layout.Effect).Radius = 5;
}
private void OnClickRemoveSelected(object sender, RoutedEventArgs e)
{
var item = List.SelectedItem;
if (item != null)
{
if (item is TimerStruct t)
{
var c = CurrentTimer;
if (t.Equals(c)) TimerSelf.Stop();
}
List.Items.RemoveAt(List.Items.IndexOf(item));
}
}
private void OnClickStartTimer(object sender, RoutedEventArgs e) => TimerSelf.Start();
private void OnClickPauseTimer(object sender, RoutedEventArgs e) => TimerSelf.Stop();
private void OnClickDropTimer(object sender, RoutedEventArgs e)
{
MediaPlayer.Stop();
TimerSelf.Stop();
CurrentTimer = CurrentTimer;
}
private void OnClickSetupNext(object sender, RoutedEventArgs e)
{
int current = List.SelectedIndex;
if (List.Items.Count > current + 1)
{
TimerSelf.Stop();
MediaPlayer.Stop();
List.SelectedIndex = current + 1;
}
else
{
if(List.Items.Count==1)return;
TimerSelf.Stop();
MediaPlayer.Stop();
List.SelectedIndex = 0;
}
}
private void OnClickSelectMusic(object sender, RoutedEventArgs e)
{
if (OpenFileDialog.ShowDialog(this).Value)
{
FileInfo file = new(OpenFileDialog.FileName);
MediaPlayer.Open(new Uri(file.FullName, UriKind.Absolute));
((Button)sender).Content = file.Name.Length > 10 ? file.Name[..10] + "..." : file.Name;
}
}
public void CloseModal()
{
ModalControl.Visibility = Visibility.Collapsed;
((BlurEffect)Layout.Effect).Radius = 0;
}
private void OnCurrentTimerSizeChanged(object sender, SizeChangedEventArgs e) => ProgressBar.Height = e.NewSize.Height;
}
}