Skip to content

Commit

Permalink
fix(progressring): The AnimatedVisualSource was recreated too often, …
Browse files Browse the repository at this point in the history
…loosing the playing state of the current player. Logic has been added to only create a new source when required.
  • Loading branch information
carldebilly committed Sep 14, 2021
1 parent 9cae465 commit 59c4904
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/ProgressRing/ProgressRing.cs
Expand Up @@ -26,6 +26,14 @@ public partial class ProgressRing : Control
private Panel? _layoutRoot;
private double _oldValue = 0d;
private Uri? _currentSourceUri = null;
private LoadedAsset _loadedAsset;

private enum LoadedAsset : byte
{
NotLoaded,
Indeterminate,
Determinate
}

public static DependencyProperty IsActiveProperty { get; } = DependencyProperty.Register(
nameof(IsActive), typeof(bool), typeof(ProgressRing), new FrameworkPropertyMetadata(true, OnIsActivePropertyChanged));
Expand Down Expand Up @@ -121,28 +129,32 @@ protected override void OnApplyTemplate()
UpdateLottieProgress();
ChangeVisualState();

OnForegroundPropertyChanged(this, ForegroundProperty);
OnBackgroundPropertyChanged(this, BackgroundProperty);
SetLottieForegroundColor();
SetLottieBackgroundColor();
}

private static void OnIsIndeterminatePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
(dependencyObject as ProgressRing)?.ChangeVisualState();
}

private void OnForegroundPropertyChanged(DependencyObject sender, DependencyProperty dp)
private void OnForegroundPropertyChanged(DependencyObject sender, DependencyProperty dp) => SetLottieForegroundColor();

private void OnBackgroundPropertyChanged(DependencyObject sender, DependencyProperty dp) => SetLottieBackgroundColor();

private void SetLottieForegroundColor()
{
if (_player?.Source is IThemableAnimatedVisualSource source
&& Brush.TryGetColorWithOpacity(Foreground, out var foreground))
&& Brush.TryGetColorWithOpacity(Foreground, out var foreground))
{
source.SetColorThemeProperty("Foreground", foreground);
}
}

private void OnBackgroundPropertyChanged(DependencyObject sender, DependencyProperty dp)
private void SetLottieBackgroundColor()
{
if (_player?.Source is IThemableAnimatedVisualSource source
&& Brush.TryGetColorWithOpacity(Background, out var background))
&& Brush.TryGetColorWithOpacity(Background, out var background))
{
source.SetColorThemeProperty("Background", background);
}
Expand Down Expand Up @@ -186,21 +198,27 @@ private void OnMinimumPropertyChanged(DependencyPropertyChangedEventArgs args)

private void OnDeterminateSourcePropertyChanged(DependencyPropertyChangedEventArgs args)
{
SetAnimatedVisualPlayerSource();
SetAnimatedVisualPlayerSource(force: true);
}

private void OnIndeterminateSourcePropertyChanged(DependencyPropertyChangedEventArgs args)
{
SetAnimatedVisualPlayerSource();
SetAnimatedVisualPlayerSource(force: true);
}

private void SetAnimatedVisualPlayerSource()
private void SetAnimatedVisualPlayerSource(bool force = false)
{
if (_lottieProvider != null && _player != null)
{
var isIndeterminate = IsIndeterminate;
if (isIndeterminate)
{
if (_loadedAsset == LoadedAsset.Indeterminate && !force)
{
return; // already loaded
}
_loadedAsset = LoadedAsset.Indeterminate;

var indeterminateSource = IndeterminateSource;
if (indeterminateSource == null)
{
Expand All @@ -217,6 +235,12 @@ private void SetAnimatedVisualPlayerSource()
}
else
{
if (_loadedAsset == LoadedAsset.Determinate && !force)
{
return; // already loaded
}
_loadedAsset = LoadedAsset.Determinate;

var determinateSource = DeterminateSource;
if (determinateSource == null)
{
Expand All @@ -231,7 +255,9 @@ private void SetAnimatedVisualPlayerSource()
: determinateSource;
}
}


SetLottieForegroundColor();
SetLottieBackgroundColor();
}
else if (_player != null && _layoutRoot != null)
{
Expand Down

0 comments on commit 59c4904

Please sign in to comment.