Skip to content

Commit 631960e

Browse files
committed
Flash Timer
1 parent 1638790 commit 631960e

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

ContextSwitch/FloatingTimer.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FloatingTimer : Window
2323
readonly WindowApi w;
2424
DateTime endtime;
2525
readonly TextBlock tb;
26-
readonly DispatcherQueueTimer timer;
26+
readonly DispatcherQueueTimer timer, ringtimer;
2727
readonly SolidColorBrush background = new(Color.FromArgb(255 / 2, 0x20, 0x20, 0x20));
2828
public FloatingTimer()
2929
{
@@ -53,13 +53,17 @@ public FloatingTimer()
5353
};
5454

5555
timer = DispatcherQueue.CreateTimer();
56+
ringtimer = DispatcherQueue.CreateTimer();
5657
timer.Interval = TimeSpan.FromMilliseconds(500);
58+
ringtimer.Interval = TimeSpan.FromMilliseconds(1000);
5759
timer.Tick += (_, _) => TimerCallback();
60+
ringtimer.Tick += (_, _) => ToggleRingState();
5861
w = WindowApi.FromWindowHandle((nint)AppWindow.Id.Value);
5962
w.SetTopMost();
6063
w.IsTtileBarVisible = false;
6164
w[WindowStyles.THICKFRAME] = false;
6265
w[WindowStyles.Border] = false;
66+
w[WindowExStyles.TOOLWINDOW] = true;
6367
// Do not hit-test this window
6468
w[WindowExStyles.Layered] = true;
6569
w[WindowExStyles.Transparent] = true;
@@ -121,6 +125,10 @@ void UpdateSize()
121125
public bool IsTimerRunning => endtime > DateTime.Now;
122126
public void Start(TimeSpan timerDuration)
123127
{
128+
ringtimer.Stop();
129+
if (ringTimerAbnormalState)
130+
ToggleRingState();
131+
keyReset = false;
124132
var content = (StackPanel)Content;
125133
if (content.Children.Count == 3)
126134
{
@@ -131,11 +139,16 @@ public void Start(TimeSpan timerDuration)
131139
background.Color = Color.FromArgb(255 / 2, 0x20, 0x20, 0x20);
132140
endtime = DateTime.Now + timerDuration;
133141
TimerCallback();
134-
timer.Start();
142+
if (IsTimerRunning)
143+
timer.Start();
135144
lockHide = false;
136145
Content.Opacity = 1;
137146
ToHide = DateTime.Now + TimeSpan.FromSeconds(5);
138147
}
148+
public void Stop()
149+
{
150+
endtime = DateTime.Now;
151+
}
139152
bool lockHide = false;
140153
void TimerCallback()
141154
{
@@ -159,13 +172,13 @@ void TimerCallback()
159172
}
160173
} else
161174
{
175+
timer.Stop();
162176
tb.Text = "00:00";
163-
background.Color = Colors.Red;
164177
ElementSoundPlayer.State = ElementSoundPlayerState.On;
165-
ElementSoundPlayer.Play(ElementSoundKind.Invoke);
166-
ElementSoundPlayer.State = ElementSoundPlayerState.Off;
178+
ToggleRingState();
179+
ringtimer.Start();
180+
lockHide = true;
167181
Content.Opacity = 1;
168-
timer.Stop();
169182
keyReset = true;
170183
((StackPanel)Content).Children.Add(new TextBlock {
171184
FontSize = 11,
@@ -176,4 +189,18 @@ void TimerCallback()
176189
UpdateSize();
177190
}
178191
}
192+
bool ringTimerAbnormalState;
193+
void ToggleRingState()
194+
{
195+
if (ringTimerAbnormalState)
196+
{
197+
background.Color = Color.FromArgb(255 / 2, 0x20, 0x20, 0x20);
198+
ElementSoundPlayer.State = ElementSoundPlayerState.Off;
199+
} else
200+
{
201+
background.Color = Colors.Red;
202+
ElementSoundPlayer.Play(ElementSoundKind.Invoke);
203+
}
204+
ringTimerAbnormalState = !ringTimerAbnormalState;
205+
}
179206
}

0 commit comments

Comments
 (0)