Skip to content

Commit 2322829

Browse files
committed
perf: Push active system theme to ResourceDictionary
Avoids multiple conditional resolutions when resolving resources
1 parent f4651c9 commit 2322829

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/Uno.UI/UI/Xaml/Application.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ internal SpecializedResourceDictionary.ResourceKey RequestedThemeForResources
140140
return _requestedThemeForResources;
141141
}
142142

143-
private set => _requestedThemeForResources = value;
143+
private set
144+
{
145+
_requestedThemeForResources = value;
146+
ResourceDictionary.SetActiveTheme(value);
147+
}
144148
}
145149

146150
internal ElementTheme ActualElementTheme => (_themeSetExplicitly, RequestedTheme) switch

src/Uno.UI/UI/Xaml/ResourceDictionary.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ internal bool TryGetValue(string resourceKey, out object value, bool shouldCheck
125125
internal bool TryGetValue(Type resourceKey, out object value, bool shouldCheckSystem)
126126
=> TryGetValue(new ResourceKey(resourceKey), out value, shouldCheckSystem);
127127

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

@@ -534,23 +535,14 @@ public StaticResourceAliasRedirect(string resourceKey, XamlParseContext parseCon
534535

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

538+
internal static void SetActiveTheme(SpecializedResourceDictionary.ResourceKey key)
539+
=> Themes.Active = key;
540+
537541
private static class Themes
538542
{
539543
public static SpecializedResourceDictionary.ResourceKey Light { get; } = "Light";
540544
public static SpecializedResourceDictionary.ResourceKey Default { get; } = "Default";
541-
public static SpecializedResourceDictionary.ResourceKey Active
542-
{
543-
get
544-
{
545-
var res = Application.Current?.RequestedThemeForResources;
546-
if (res?.Key != null)
547-
{
548-
return res.Value;
549-
}
550-
551-
return Light;
552-
}
553-
}
545+
public static SpecializedResourceDictionary.ResourceKey Active { get; set; } = Light;
554546
}
555547
}
556548
}

0 commit comments

Comments
 (0)