Repository navigation
Expand file tree
/
Copy pathTimer.cs
More file actions
89 lines (76 loc) · 1.98 KB
/
Copy pathTimer.cs
File metadata and controls
89 lines (76 loc) · 1.98 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
using System.IO;
using System.Media;
namespace DynamicIsland;
/// <summary>The island's one countdown.</summary>
sealed class Countdown
{
TimeSpan _left;
DateTime _resumed;
public TimeSpan Total { get; private set; }
/// <summary>Set and not finished yet: counting down or paused.</summary>
public bool Active { get; private set; }
public bool Running { get; private set; }
public TimeSpan Left
{
get
{
TimeSpan left = Running ? _left - (DateTime.UtcNow - _resumed) : _left;
return left > TimeSpan.Zero ? left : TimeSpan.Zero;
}
}
/// <summary>Share of the time still to go, 1 → 0.</summary>
public double Share => Total > TimeSpan.Zero ? Left / Total : 0;
public void Start(TimeSpan total)
{
Total = _left = total;
_resumed = DateTime.UtcNow;
Active = Running = true;
}
/// <summary>Pause / resume.</summary>
public void Toggle()
{
if (!Active) return;
if (Running) _left = Left;
else _resumed = DateTime.UtcNow;
Running = !Running;
}
public void Stop()
{
Active = Running = false;
_left = TimeSpan.Zero;
}
}
/// <summary>Rings until told to stop: the Windows alarm sound, looped.</summary>
sealed class Alarm
{
SoundPlayer? _player;
public void Ring()
{
Stop();
try
{
string wav = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Media", "Alarm01.wav");
if (!File.Exists(wav))
{
SystemSounds.Exclamation.Play();
return;
}
_player = new SoundPlayer(wav);
_player.PlayLooping();
}
catch (Exception ex)
{
App.Log(ex);
}
}
public void Stop()
{
try
{
_player?.Stop();
_player?.Dispose();
}
catch { }
_player = null;
}
}