Skip to content

Commit

Permalink
Allow for a slightly delayed theme registration (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored Aug 23, 2023
1 parent 2a75990 commit 58779a0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SKConfettiView : SKAnimatedSurfaceView

public SKConfettiView()
{
Themes.SKConfettiViewResources.EnsureRegistered();
ResourceLoader<Themes.SKConfettiViewResources>.EnsureRegistered(this);

SizeChanged += OnSizeChanged;
PropertyChanged += (_, e) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

public partial class SKConfettiViewResources : ResourceDictionary
{
private static bool registered;

public SKConfettiViewResources()
{
InitializeComponent();
}

internal static void EnsureRegistered() =>
ResourceLoader.EnsureRegistered<SKConfettiViewResources>(ref registered);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class SKLottieView : SKAnimatedSurfaceView

public SKLottieView()
{
Themes.SKLottieViewResources.EnsureRegistered();
ResourceLoader<Themes.SKLottieViewResources>.EnsureRegistered(this);

IsAnimationEnabled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

public partial class SKLottieViewResources : ResourceDictionary
{
private static bool registered;

public SKLottieViewResources()
{
InitializeComponent();
}

internal static void EnsureRegistered() =>
ResourceLoader.EnsureRegistered<SKLottieViewResources>(ref registered);
}
30 changes: 27 additions & 3 deletions source/SkiaSharp.Extended.UI/Utils/ResourceLoader.shared.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
namespace SkiaSharp.Extended.UI;

internal static class ResourceLoader
internal static class ResourceLoader<T>
where T : ResourceDictionary, new()
{
internal static void EnsureRegistered<T>(ref bool registered)
where T : ResourceDictionary, new()
private static bool registered;

internal static void EnsureRegistered(VisualElement? element = null)
{
if (registered)
return;

// try register with the current app
var merged = Application.Current?.Resources?.MergedDictionaries;
if (merged != null)
{
Expand All @@ -26,5 +29,26 @@ internal static void EnsureRegistered<T>(ref bool registered)
registered = true;
}
}

#if !XAMARIN_FORMS
// the app may not be ready yet - if this page is part of the app's constructor
// so we will wait until this view gets a window, then we will try again
if (!registered && element != null)
{
element.PropertyChanged += OnPropertyChanged;

static void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != VisualElement.WindowProperty.PropertyName)
return;

// the window changed, so try one more time
EnsureRegistered();

if (sender is VisualElement ve)
ve.PropertyChanged -= OnPropertyChanged;
}
}
#endif
}
}

0 comments on commit 58779a0

Please sign in to comment.