Skip to content

Commit 6799360

Browse files
committed
Update RivePlayer to refresh with DispatcherQueueTimer
DispatcherQueueTimer is cross-compatible and cleaner/more correct than the custom timer from before.
1 parent 9045236 commit 6799360

File tree

3 files changed

+24
-70
lines changed

3 files changed

+24
-70
lines changed

labs/RivePlayer/src/AnimationTimer.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.

labs/RivePlayer/src/RivePlayer.Platform.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ protected override void OnApplyTemplate()
5858
xamlCanvas.PaintSurface += OnPaintSurface;
5959
_skiaSurface.Content = xamlCanvas;
6060
#endif
61-
_animationTimer = new AnimationTimer(this, fps: 120);
6261
}
6362
base.OnApplyTemplate();
6463
}

labs/RivePlayer/src/RivePlayer.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
using Windows.Storage.Streams;
1010
using Windows.Storage;
1111

12+
#if !WINAPPSDK
13+
using DQ = Windows.System.DispatcherQueue;
14+
using Windows.System;
15+
#else
16+
using DQ = Microsoft.UI.Dispatching.DispatcherQueue;
17+
using Microsoft.UI.Dispatching;
18+
#endif
19+
1220
namespace CommunityToolkit.Labs.WinUI.Rive;
1321

1422
/// <summary>
@@ -31,7 +39,7 @@ public sealed partial class RivePlayer
3139
private readonly ConcurrentQueue<Action> sceneActionsQueue = new();
3240

3341
// Continuously invalidates the panel for repaint.
34-
private AnimationTimer? _animationTimer;
42+
private DispatcherQueueTimer _animationTimer;
3543

3644
// State machine inputs to set once the current async source load finishes.
3745
// The null-ness of this object also tells us whether an async load operation is currently running.
@@ -62,14 +70,26 @@ public RivePlayer()
6270
(object s, PointerRoutedEventArgs e) => HandlePointerEvent(_scene.PointerMove, e);
6371
this.PointerReleased +=
6472
(object s, PointerRoutedEventArgs e) => HandlePointerEvent(_scene.PointerUp, e);
73+
74+
_animationTimer = DQ.GetForCurrentThread().CreateTimer();
75+
_animationTimer.Interval = TimeSpan.FromSeconds(1.0 / 120); // Refresh at 120fps.
76+
_animationTimer.Tick += AnimationTimer_Tick;
6577
}
6678

67-
private void OnXamlRootChanged(bool isHostVisible)
79+
private void AnimationTimer_Tick(DispatcherQueueTimer t, object e)
6880
{
69-
if (_animationTimer == null)
81+
if (!IsLoaded)
7082
{
71-
return;
83+
_animationTimer.Stop();
84+
}
85+
else
86+
{
87+
InvalidateAnimation();
7288
}
89+
}
90+
91+
private void OnXamlRootChanged(bool isHostVisible)
92+
{
7393
if (isHostVisible)
7494
{
7595
_animationTimer.Start();

0 commit comments

Comments
 (0)