Skip to content

Commit

Permalink
perf: Push active system theme to ResourceDictionary
Browse files Browse the repository at this point in the history
Avoids multiple conditional resolutions when resolving resources
  • Loading branch information
jeromelaban committed May 20, 2021
1 parent f4651c9 commit 2322829
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/Uno.UI/UI/Xaml/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ internal SpecializedResourceDictionary.ResourceKey RequestedThemeForResources
return _requestedThemeForResources;
}

private set => _requestedThemeForResources = value;
private set
{
_requestedThemeForResources = value;
ResourceDictionary.SetActiveTheme(value);
}
}

internal ElementTheme ActualElementTheme => (_themeSetExplicitly, RequestedTheme) switch
Expand Down
18 changes: 5 additions & 13 deletions src/Uno.UI/UI/Xaml/ResourceDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ internal bool TryGetValue(string resourceKey, out object value, bool shouldCheck
internal bool TryGetValue(Type resourceKey, out object value, bool shouldCheckSystem)
=> TryGetValue(new ResourceKey(resourceKey), out value, shouldCheckSystem);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryGetValue(in ResourceKey resourceKey, out object value, bool shouldCheckSystem) =>
TryGetValue(resourceKey, ResourceKey.Empty, out value, shouldCheckSystem);

Expand Down Expand Up @@ -534,23 +535,14 @@ public StaticResourceAliasRedirect(string resourceKey, XamlParseContext parseCon

internal static object GetStaticResourceAliasPassthrough(string resourceKey, XamlParseContext parseContext) => new StaticResourceAliasRedirect(resourceKey, parseContext);

internal static void SetActiveTheme(SpecializedResourceDictionary.ResourceKey key)
=> Themes.Active = key;

private static class Themes
{
public static SpecializedResourceDictionary.ResourceKey Light { get; } = "Light";
public static SpecializedResourceDictionary.ResourceKey Default { get; } = "Default";
public static SpecializedResourceDictionary.ResourceKey Active
{
get
{
var res = Application.Current?.RequestedThemeForResources;
if (res?.Key != null)
{
return res.Value;
}

return Light;
}
}
public static SpecializedResourceDictionary.ResourceKey Active { get; set; } = Light;
}
}
}

0 comments on commit 2322829

Please sign in to comment.