Skip to content
Merged
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 @@ -1752,7 +1752,7 @@ internal void InvalidateResourceReferences(ResourcesChangeInfo info)
if (!info.IsIndividualResourceChange
&& !ThemeManager.SkipAppThemeModeSyncing)
{
ThemeManager.SyncThemeMode();
ThemeManager.SyncApplicationThemeMode();
}

// Invalidate ResourceReference properties on all the windows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ public ResourceDictionary Resources
}
set
{
bool invalidateResources = false;

ResourceDictionary oldValue = ResourcesField.GetValue(this);
ResourcesField.SetValue(this, value);

Expand All @@ -727,6 +729,11 @@ public ResourceDictionary Resources
oldValue.RemoveOwner(this);
}

if(this is Window window)
{
window.AddFluentDictionary(value, out invalidateResources);
}

if (value != null)
{
if (!value.ContainsOwner(this))
Expand All @@ -743,7 +750,7 @@ public ResourceDictionary Resources
// final invalidation & it is no worse than the old code that also did not invalidate in this case
// Removed the not-empty check to allow invalidations in the case that the old dictionary
// is replaced with a new empty dictionary
if (oldValue != value)
if (oldValue != value || invalidateResources)
{
TreeWalkHelper.InvalidateOnResourcesChange(this, null, new ResourcesChangeInfo(oldValue, value));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Standard;
using Microsoft.Win32;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows.Interop;
using System.Windows.Media;
Expand Down Expand Up @@ -123,7 +124,7 @@ internal static void OnWindowThemeChanged(Window window, ThemeMode oldThemeMode,
ApplyFluentOnWindow(window);
}

internal static bool SyncThemeMode()
internal static bool SyncApplicationThemeMode()
{
ThemeMode themeMode = GetThemeModeFromResourceDictionary(Application.Current.Resources);

Expand All @@ -132,9 +133,20 @@ internal static bool SyncThemeMode()
Application.Current.ThemeMode = themeMode;
return themeMode == ThemeMode.None ? false : true;
}

return false;
}

internal static void SyncWindowThemeMode(Window window)
{
ThemeMode themeMode = GetThemeModeFromResourceDictionary(window.Resources);

if(window.ThemeMode != themeMode)
{
window.ThemeMode = themeMode;
}
}

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

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

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

internal static double DefaultFluentThemeFontSize => 14;

internal static WindowCollection FluentEnabledWindows { get; set; } = new WindowCollection();
Expand Down Expand Up @@ -376,6 +390,8 @@ private static void AddOrUpdateThemeResources(ResourceDictionary rd, ResourceDic

int index = LastIndexOfFluentThemeDictionary(rd);

IgnoreWindowResourcesChange = true;

if (index >= 0)
{
rd.MergedDictionaries[index] = newDictionary;
Expand All @@ -384,6 +400,8 @@ private static void AddOrUpdateThemeResources(ResourceDictionary rd, ResourceDic
{
rd.MergedDictionaries.Insert(0, newDictionary);
}

IgnoreWindowResourcesChange = false;
}

private static int LastIndexOfFluentThemeDictionary(ResourceDictionary rd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,17 @@ internal static void InvalidateOnResourcesChange(
{
Debug.Assert(fe != null || fce != null, "Node with the resources change notification must be an FE or an FCE.");

// Here we are syncing the window's Theme mode if resource dictionary changes.
// The IgnoreWindowResourcesChange is a flag set to make sure the ThemeMode change does not cause an infinite loop of resource changes.
if(fe is Window currentWindow)
{
currentWindow.AreResourcesInitialized = true;
if(!ThemeManager.IgnoreWindowResourcesChange)
{
ThemeManager.SyncWindowThemeMode(currentWindow);
}
}

// We're interested in changes to the Template property that occur during
// the walk - if the template has changed we don't need to invalidate
// template-driven properties a second time. The HasTemplateChanged property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,14 @@ public ThemeMode ThemeMode
ThemeMode oldTheme = _themeMode;
_themeMode = value;

if(!AreResourcesInitialized)
{
ThemeManager.OnWindowThemeChanged(this, oldTheme, value);
AreResourcesInitialized = false;

_reloadFluentDictionary = true;
}

if(IsSourceWindowNull)
{
_deferThemeLoading = true;
Expand Down Expand Up @@ -2120,6 +2128,22 @@ internal Point LogicalToDeviceUnits(Point ptLogicalUnits)
return ptDeviceUnits;
}

internal void AddFluentDictionary(ResourceDictionary value, out bool invalidateResources)
{
invalidateResources = false;

if(_reloadFluentDictionary && !AreResourcesInitialized)
{
if(value != null && ThemeMode != ThemeMode.None)
{
value.MergedDictionaries.Insert(0, ThemeManager.GetThemeDictionary(ThemeMode));
invalidateResources = true;
}

_reloadFluentDictionary = false;
}
}

internal static bool VisibilityToBool(Visibility v)
{
switch (v)
Expand Down Expand Up @@ -3282,6 +3306,19 @@ bool IWindowService.UserResized
{
get { return false; }
}

internal bool AreResourcesInitialized
{
get
{
return _resourcesInitialized;
}
set
{
_resourcesInitialized = value;
}
}

#endregion Internal Properties

//----------------------------------------------
Expand Down Expand Up @@ -7205,7 +7242,9 @@ private EventHandlerList Events

private SourceWindowHelper _swh; // object that will hold the window
private Window _ownerWindow; // owner window

private bool _reloadFluentDictionary = false;
private bool _resourcesInitialized = false;

// keeps track of the owner hwnd
// we need this one b/c a owner/parent
// can be set through the WindowInteropHandler
Expand Down