Skip to content

Commit aa2beed

Browse files
committed
Fix multiple timer initialized while changing slide
1 parent 4a32388 commit aa2beed

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

CollapseLauncher/XAMLs/Theme/CustomControls/PanelSlideshow.Events.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#nullable enable
2-
using CommunityToolkit.WinUI;
1+
using CommunityToolkit.WinUI;
32
using CommunityToolkit.WinUI.Animations;
43
using Hi3Helper.Data;
54
using Microsoft.UI.Input;
@@ -16,6 +15,7 @@
1615
using System.Numerics;
1716
using Windows.System;
1817

18+
#nullable enable
1919
namespace CollapseLauncher.XAMLs.Theme.CustomControls;
2020

2121
public partial class PanelSlideshow
@@ -59,6 +59,7 @@ private void ItemIndex_OnChange(int newIndex, int oldIndex)
5959

6060
// Restart slideshow timer
6161
RestartTimer(SlideshowDuration);
62+
ResetSlideshow();
6263

6364
IList<UIElement> elements = Items;
6465
UIElement newElement = elements[newIndex];

CollapseLauncher/XAMLs/Theme/CustomControls/PanelSlideshow.Timer.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.UI.Xaml;
1+
using CollapseLauncher.Extension;
2+
using Microsoft.UI.Xaml;
23
using Microsoft.UI.Xaml.Media.Animation;
34
using System;
45
using System.Threading;
@@ -22,16 +23,28 @@ private async void RestartTimer(double newDurationSeconds, int delayBeforeStartM
2223
{
2324
try
2425
{
25-
DisposeAndDeregisterTimer();
26-
if (!IsLoaded || newDurationSeconds == 0)
26+
if (!IsLoaded ||
27+
!_isTemplateLoaded ||
28+
newDurationSeconds == 0)
29+
{
30+
if (newDurationSeconds == 0)
31+
{
32+
_countdownProgressBar.Value = 0;
33+
}
34+
DisposeAndDeregisterTimer();
35+
36+
return;
37+
}
38+
39+
if (_timerStoryboard != null)
2740
{
2841
return;
2942
}
3043

3144
_countdownProgressBar.Minimum = 0;
3245
_countdownProgressBar.Maximum = 1;
3346

34-
_timerStoryboard = new Storyboard();
47+
Interlocked.Exchange(ref _timerStoryboard, new Storyboard());
3548
DoubleAnimation animation = new()
3649
{
3750
Duration = new Duration(TimeSpan.FromSeconds(newDurationSeconds)),
@@ -73,6 +86,7 @@ async void TimerStoryboardOnCompleted(object? sender, object e)
7386
VisualStateManager.GoToState(this, StateNameCountdownProgressBarFadeOut, true);
7487
await Task.Delay(500);
7588

89+
DisposeAndDeregisterTimer();
7690
ItemIndex++;
7791
}
7892
catch (Exception ex)
@@ -82,16 +96,6 @@ async void TimerStoryboardOnCompleted(object? sender, object e)
8296
}
8397
}
8498

85-
private void DisposeAndDeregisterTimer()
86-
{
87-
if (!_isTemplateLoaded)
88-
{
89-
return;
90-
}
91-
92-
Interlocked.Exchange(ref _timerStoryboard, null)?.Stop();
93-
}
94-
9599
/// <summary>
96100
/// Stops the slideshow countdown timer.
97101
/// </summary>
@@ -111,5 +115,20 @@ public void ResumeSlideshow()
111115
_timerStoryboard?.Resume();
112116
}
113117

118+
/// <summary>
119+
/// Resets the slideshow countdown timer.
120+
/// </summary>
121+
public void ResetSlideshow()
122+
{
123+
_timerStoryboard?.Seek(TimeSpan.FromSeconds(0));
124+
}
125+
126+
private void DisposeAndDeregisterTimer()
127+
{
128+
Storyboard? oldStoryboard = Interlocked.Exchange(ref _timerStoryboard, null);
129+
oldStoryboard?.Stop();
130+
oldStoryboard?.Children?.Clear();
131+
}
132+
114133
#endregion
115134
}

0 commit comments

Comments
 (0)