Skip to content

Commit e7ddcf4

Browse files
authored
[Fix] Don't ignore application resource invalidations (#9527)
* [Fix] Don't ignore application resource invalidations * Fix ignoring legitimate ResourceChange operations * Removing the IsIndividualAddOperationCheck
1 parent 7b9ea81 commit e7ddcf4

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,12 +1734,17 @@ internal void InvalidateResourceReferences(ResourcesChangeInfo info)
17341734
{
17351735
_resourcesInitialized = true;
17361736

1737-
if(!ThemeManager.IgnoreAppResourcesChange)
1738-
{
1739-
if(ThemeManager.SyncThemeModeAndResources())
1740-
{
1741-
return;
1742-
}
1737+
// Sync needs to be performed only under the following conditions:
1738+
// - the resource change event raised is due to a collection change
1739+
// i.e. it is not a IsIndividualResourceAddOperation
1740+
// - the event is not raised due to the change in Application.ThemeMode
1741+
// i.e. SkipAppThemeModeSyncing is set to true
1742+
// - if application's ThemeMode and Resources sync is enabled.
1743+
// i.e. IsAppThemeModeSyncEnabled is set to true
1744+
if (!ThemeManager.SkipAppThemeModeSyncing
1745+
&& ThemeManager.IsAppThemeModeSyncEnabled)
1746+
{
1747+
ThemeManager.SyncThemeMode();
17431748
}
17441749

17451750
// Invalidate ResourceReference properties on all the windows.

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeManager.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static void OnSystemThemeChanged()
1818
{
1919
if (IsFluentThemeEnabled)
2020
{
21-
IgnoreAppResourcesChange = true;
21+
SkipAppThemeModeSyncing = true;
2222

2323
try
2424
{
@@ -51,7 +51,7 @@ internal static void OnSystemThemeChanged()
5151
}
5252
finally
5353
{
54-
IgnoreAppResourcesChange = false;
54+
SkipAppThemeModeSyncing = false;
5555
}
5656

5757
}
@@ -76,7 +76,7 @@ internal static void OnSystemThemeChanged()
7676

7777
internal static void OnApplicationThemeChanged(ThemeMode oldThemeMode, ThemeMode newThemeMode)
7878
{
79-
IgnoreAppResourcesChange = true;
79+
SkipAppThemeModeSyncing = true;
8080

8181
try
8282
{
@@ -107,7 +107,7 @@ internal static void OnApplicationThemeChanged(ThemeMode oldThemeMode, ThemeMode
107107
}
108108
finally
109109
{
110-
IgnoreAppResourcesChange = false;
110+
SkipAppThemeModeSyncing = false;
111111
}
112112
}
113113

@@ -125,12 +125,9 @@ internal static void OnWindowThemeChanged(Window window, ThemeMode oldThemeMode,
125125
ApplyFluentOnWindow(window);
126126
}
127127

128-
internal static bool SyncThemeModeAndResources()
128+
internal static bool SyncThemeMode()
129129
{
130-
if (DeferSyncingThemeModeAndResources)
131-
return true;
132-
133-
ThemeMode themeMode = GetThemeModeFromResourceDictionary(Application.Current.Resources);
130+
ThemeMode themeMode = GetThemeModeFromResourceDictionary(Application.Current.Resources);
134131

135132
if (Application.Current.ThemeMode != themeMode)
136133
{
@@ -140,9 +137,11 @@ internal static bool SyncThemeModeAndResources()
140137
return false;
141138
}
142139

143-
internal static void SyncDeferredThemeModeAndResources()
140+
internal static void SyncThemeModeAndResources()
144141
{
145-
if (Application.Current == null)
142+
// Since, this is called from window there is a possiblity that the application
143+
// instance is null. Hence, we need to check for null.
144+
if(Application.Current == null)
146145
return;
147146

148147
ThemeMode themeMode = Application.Current.ThemeMode;
@@ -317,7 +316,7 @@ private static void ApplyStyleOnWindow(Window window, bool useLightColors)
317316

318317
#region Internal Properties
319318

320-
internal static bool DeferSyncingThemeModeAndResources { get; set; } = true;
319+
internal static bool IsAppThemeModeSyncEnabled { get; set; } = false;
321320

322321
internal static bool IsFluentThemeEnabled
323322
{
@@ -331,7 +330,7 @@ internal static bool IsFluentThemeEnabled
331330

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

334-
internal static bool IgnoreAppResourcesChange { get; set; } = false;
333+
internal static bool SkipAppThemeModeSyncing { get; set; } = false;
335334

336335
internal static double DefaultFluentThemeFontSize => 14;
337336

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,10 +2558,10 @@ internal void CreateSourceWindow(bool duringShow)
25582558

25592559
if (Standard.Utility.IsOSWindows10OrNewer)
25602560
{
2561-
if(ThemeManager.DeferSyncingThemeModeAndResources)
2561+
if(!ThemeManager.IsAppThemeModeSyncEnabled)
25622562
{
2563-
ThemeManager.DeferSyncingThemeModeAndResources = false;
2564-
ThemeManager.SyncDeferredThemeModeAndResources();
2563+
ThemeManager.SyncThemeModeAndResources();
2564+
ThemeManager.IsAppThemeModeSyncEnabled = true;
25652565
}
25662566

25672567
if(ThemeManager.IsFluentThemeEnabled)

0 commit comments

Comments
 (0)