Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -932,14 +932,15 @@ public ResourceDictionary Resources
oldValue.RemoveOwner(this);
}

if(ThemeManager.DeferredAppThemeLoading && !_resourcesInitialized)
if(_reloadFluentDictionary && !_resourcesInitialized)
{
if(value != null)
{
var uri = ThemeManager.GetThemeResource(ThemeMode);
value.MergedDictionaries.Insert(0, new ResourceDictionary() { Source = uri });
ThemeManager.SkipAppThemeModeSyncing = true;
}
ThemeManager.DeferredAppThemeLoading = false;
_reloadFluentDictionary = false;
}

if (value != null)
Expand All @@ -960,6 +961,11 @@ public ResourceDictionary Resources
{
InvalidateResourceReferences(new ResourcesChangeInfo(oldValue, value));
}

if(ThemeManager.SkipAppThemeModeSyncing)
{
ThemeManager.SkipAppThemeModeSyncing = false;
}
}
}

Expand Down Expand Up @@ -993,7 +999,9 @@ public ThemeMode ThemeMode
// If the resources are not initializd,
// fluent dictionary included will be reset.
// Hence, deferring the step.
ThemeManager.DeferredAppThemeLoading = true;
ThemeManager.OnApplicationThemeChanged(oldValue, value);
_reloadFluentDictionary = true;
_resourcesInitialized = false;
return;
}

Expand Down Expand Up @@ -1732,20 +1740,19 @@ internal int RunInternal(Window window)

internal void InvalidateResourceReferences(ResourcesChangeInfo info)
{
_resourcesInitialized = true;

// Sync needs to be performed only under the following conditions:
// - the resource change event raised is due to a collection change
// i.e. it is not a IsIndividualResourceAddOperation
// - the event is not raised due to the change in Application.ThemeMode
// i.e. SkipAppThemeModeSyncing is set to true
// - if application's ThemeMode and Resources sync is enabled.
// i.e. IsAppThemeModeSyncEnabled is set to true
if (!ThemeManager.SkipAppThemeModeSyncing
&& ThemeManager.IsAppThemeModeSyncEnabled)
if (!info.IsIndividualResourceAddOperation
&& !ThemeManager.SkipAppThemeModeSyncing)
{
ThemeManager.SyncThemeMode();
ThemeManager.SyncThemeMode(info);
}

_resourcesInitialized = true;

// Invalidate ResourceReference properties on all the windows.
// we Clone() the collection b/c if we don't then some other thread can be
Expand Down Expand Up @@ -2490,6 +2497,7 @@ private object RunDispatcher(object ignore)

private ThemeMode _themeMode = ThemeMode.None;
private bool _resourcesInitialized = false;
private bool _reloadFluentDictionary = false;

private SecurityCriticalDataForSet<MimeType> _appMimeType;
private IServiceProvider _serviceProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ internal ResourcesChangeInfo(

#endregion Constructors

#region Internal Properties

internal IEnumerable<ResourceDictionary> OldDictionaries
{
get { return _oldDictionaries; }
}

internal IEnumerable<ResourceDictionary> NewDictionaries
{
get { return _newDictionaries; }
}

#endregion

#region Operations

/// <summary>
Expand Down Expand Up @@ -199,6 +213,11 @@ internal bool IsResourceAddOperation
get { return _key != null || (_newDictionaries != null && _newDictionaries.Count > 0); }
}

internal bool IsIndividualResourceAddOperation
{
get { return _key != null; }
}

// This member is used to identify the container when a style change happens
internal DependencyObject Container
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,14 @@ internal static void OnWindowThemeChanged(Window window, ThemeMode oldThemeMode,
ApplyFluentOnWindow(window);
}

internal static bool SyncThemeMode()
internal static bool SyncThemeMode(ResourcesChangeInfo info)
{
if (!ContainsFluentThemeDictionary(info.OldDictionaries)
&& !ContainsFluentThemeDictionary(info.NewDictionaries))
{
return false;
}

ThemeMode themeMode = GetThemeModeFromResourceDictionary(Application.Current.Resources);

if (Application.Current.ThemeMode != themeMode)
Expand All @@ -137,46 +143,6 @@ internal static bool SyncThemeMode()
return false;
}

internal static void SyncThemeModeAndResources()
{
// Since, this is called from window there is a possiblity that the application
// instance is null. Hence, we need to check for null.
if(Application.Current == null)
return;

ThemeMode themeMode = Application.Current.ThemeMode;
var rd = Application.Current.Resources;

bool resyncThemeMode = false;
int index = LastIndexOfFluentThemeDictionary(rd);

if (index == -1)
{
// This means that ThemeMode was set but Resources were not set during initialization.
// Hence we need to resync.
if (themeMode != ThemeMode.None)
{
resyncThemeMode = true;
}
}
else
{
// If index > 0, then Fluent theme dictionary was added manually.
// If ThemeMode is None, and yet there is a Fluent theme dictionary, hence that was manually set.
// Hence we need to resync.
if (index > 0 || themeMode == ThemeMode.None)
{
themeMode = GetThemeModeFromSourceUri(rd.MergedDictionaries[index].Source);
resyncThemeMode = true;
}
}

if (resyncThemeMode)
{
Application.Current.ThemeMode = themeMode;
}
}

internal static void ApplyStyleOnWindow(Window window)
{
if (!IsFluentThemeEnabled && window.ThemeMode == ThemeMode.None)
Expand Down Expand Up @@ -316,8 +282,6 @@ private static void ApplyStyleOnWindow(Window window, bool useLightColors)

#region Internal Properties

internal static bool IsAppThemeModeSyncEnabled { get; set; } = false;

internal static bool IsFluentThemeEnabled
{
get
Expand All @@ -328,8 +292,6 @@ internal static bool IsFluentThemeEnabled
}
}

internal static bool DeferredAppThemeLoading { get; set; } = false;

internal static bool SkipAppThemeModeSyncing { get; set; } = false;

internal static double DefaultFluentThemeFontSize => 14;
Expand Down Expand Up @@ -446,6 +408,26 @@ private static int LastIndexOfFluentThemeDictionary(ResourceDictionary rd)
return -1;
}

private static bool ContainsFluentThemeDictionary(IEnumerable<ResourceDictionary> dictionaries)
{
if(dictionaries == null)
return false;

foreach (var dictionary in dictionaries)
{
if (dictionary.Source != null)
{
if (dictionary.Source.ToString().StartsWith(FluentThemeResourceDictionaryUri,
StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}

return false;
}

private static IEnumerable<int> FindAllFluentThemeResourceDictionaryIndices(ResourceDictionary rd)
{
ArgumentNullException.ThrowIfNull(rd, nameof(rd));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2558,24 +2558,9 @@ internal void CreateSourceWindow(bool duringShow)

if (Standard.Utility.IsOSWindows10OrNewer)
{
if(!ThemeManager.IsAppThemeModeSyncEnabled)
{
ThemeManager.SyncThemeModeAndResources();
ThemeManager.IsAppThemeModeSyncEnabled = true;
}

if(ThemeManager.IsFluentThemeEnabled)
{

if(ThemeManager.DeferredAppThemeLoading)
{
ThemeManager.OnApplicationThemeChanged(ThemeMode.None, Application.Current.ThemeMode);
ThemeManager.DeferredAppThemeLoading = false;
}
else
{
ThemeManager.ApplyStyleOnWindow(this);
}
ThemeManager.ApplyStyleOnWindow(this);
}

if(_deferThemeLoading)
Expand Down