Skip to content

Commit

Permalink
Keep ThemeManager and ThemeResources in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Oct 22, 2019
1 parent 0e204ab commit b5675ea
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 66 deletions.
2 changes: 1 addition & 1 deletion ModernWpf.Controls/ModernWpf.Controls.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<PropertyGroup>
<PackageId>ModernWpfUI</PackageId>
<Version>0.1.191022003</Version>
<Version>0.1.191022005</Version>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion>
<Authors>Yimeng Wu</Authors>
Expand Down
67 changes: 28 additions & 39 deletions ModernWpf/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ThemeManager : DependencyObject

private static readonly Dictionary<string, ResourceDictionary> _defaultThemeDictionaries = new Dictionary<string, ResourceDictionary>();

private bool _isInitialized;
private bool _applicationStarted;

#region ApplicationTheme
Expand Down Expand Up @@ -85,18 +86,16 @@ private void OnActualApplicationThemeChanged(DependencyPropertyChangedEventArgs

private void UpdateActualApplicationTheme()
{
if (!ColorsHelper.IsInitialized)
{
return;
}

if (UsingSystemTheme)
{
ActualApplicationTheme = GetDefaultAppTheme();
}
else
if (_applicationStarted)
{
ActualApplicationTheme = ApplicationTheme ?? ModernWpf.ApplicationTheme.Light;
if (UsingSystemTheme)
{
ActualApplicationTheme = GetDefaultAppTheme();
}
else
{
ActualApplicationTheme = ApplicationTheme ?? ModernWpf.ApplicationTheme.Light;
}
}
}

Expand Down Expand Up @@ -168,18 +167,16 @@ private void ApplyAccentColor()

private void UpdateActualAccentColor()
{
if (!ColorsHelper.IsInitialized)
{
return;
}

if (UsingSystemAccentColor)
{
ActualAccentColor = ColorsHelper.GetSystemAccentColor();
}
else
if (ColorsHelper.IsInitialized)
{
ActualAccentColor = AccentColor ?? ColorsHelper.DefaultAccentColor;
if (UsingSystemAccentColor)
{
ActualAccentColor = ColorsHelper.GetSystemAccentColor();
}
else
{
ActualAccentColor = AccentColor ?? ColorsHelper.DefaultAccentColor;
}
}
}

Expand Down Expand Up @@ -572,11 +569,11 @@ private ThemeManager()

public static ThemeManager Current { get; } = new ThemeManager();

internal static Uri DefaultLightSource { get; } = GetDefaultSource(LightKey);
//internal static Uri DefaultLightSource { get; } = GetDefaultSource(LightKey);

internal static Uri DefaultDarkSource { get; } = GetDefaultSource(DarkKey);
//internal static Uri DefaultDarkSource { get; } = GetDefaultSource(DarkKey);

internal static Uri DefaultHighContrastSource { get; } = GetDefaultSource(HighContrastKey);
//internal static Uri DefaultHighContrastSource { get; } = GetDefaultSource(HighContrastKey);

//internal static ResourceDictionary DefaultLightThemeDictionary => GetDefaultThemeDictionary(LightKey);

Expand All @@ -588,7 +585,7 @@ private ThemeManager()

internal bool UsingSystemAccentColor => ColorsHelper.SystemColorsSupported && AccentColor == null;

internal static Uri GetDefaultSource(string theme)
private static Uri GetDefaultSource(string theme)
{
return PackUriHelper.GetAbsoluteUri($"ThemeResources/{theme}.xaml");
}
Expand Down Expand Up @@ -657,34 +654,26 @@ private static ResourceDictionary FindDictionary(ResourceDictionary parent, Uri

internal void Initialize()
{
Debug.Assert(ThemeResources.Current != null);

if (ReadLocalValue(ApplicationThemeProperty) == DependencyProperty.UnsetValue)
if (_isInitialized)
{
ApplicationTheme = ThemeResources.Current.RequestedTheme;
return;
}

if (ReadLocalValue(AccentColorProperty) == DependencyProperty.UnsetValue)
{
AccentColor = ThemeResources.Current.AccentColor;
}
Debug.Assert(ThemeResources.Current != null);

SystemParameters.StaticPropertyChanged += OnSystemParametersChanged;

if (Application.Current != null)
{
Application.Current.Startup += OnApplicationStartup;
}

_isInitialized = true;
}

private void OnApplicationStartup(object sender, StartupEventArgs e)
{
_applicationStarted = true;
InitializeThemeResources();
}

private void InitializeThemeResources()
{
ColorsHelper.BackgroundColorChanged += OnSystemBackgroundColorChanged;
ColorsHelper.AccentColorChanged += OnSystemAccentColorChanged;
ColorsHelper.Initialize();
Expand Down
47 changes: 21 additions & 26 deletions ModernWpf/ThemeResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ internal static ThemeResources Current
}
}

private ApplicationTheme? _requestedTheme;
/// <summary>
/// Gets or sets a value that determines the light-dark preference for the overall
/// theme of an app.
Expand All @@ -48,31 +47,34 @@ internal static ThemeResources Current
/// </returns>
public ApplicationTheme? RequestedTheme
{
get => _requestedTheme;
get => ThemeManager.Current.ApplicationTheme;
set
{
if (_requestedTheme != value)
if (ThemeManager.Current.ApplicationTheme != value)
{
_requestedTheme = value;
OnThemePropertyChanged();
ThemeManager.Current.SetCurrentValue(ThemeManager.ApplicationThemeProperty, value);

if (!IsInitializePending && DesignMode.DesignModeEnabled)
{
UpdateDesignTimeThemeDictionary();
}
}
}
}

private Color? _accentColor;
/// <summary>
/// Gets or sets the accent color of the app.
/// </summary>
public Color? AccentColor
{
get => _accentColor;
get => ThemeManager.Current.AccentColor;
set
{
if (_accentColor != value)
if (ThemeManager.Current.AccentColor != value)
{
_accentColor = value;
ThemeManager.Current.SetCurrentValue(ThemeManager.AccentColorProperty, value);

if (!IsInitializePending)
if (!IsInitializePending && DesignMode.DesignModeEnabled)
{
UpdateDesignTimeSystemColors();
}
Expand All @@ -95,42 +97,35 @@ public Color? AccentColor

private void DesignTimeInit()
{
Debug.Assert(DesignMode.DesignModeEnabled);
UpdateDesignTimeSystemColors();
UpdateDesignTimeThemeDictionary();
SystemParameters.StaticPropertyChanged += OnSystemParametersPropertyChanged;
}

private void UpdateDesignTimeSystemColors()
{
if (DesignMode.DesignModeEnabled)
Debug.Assert(DesignMode.DesignModeEnabled);
var rd = GetDesignTimeSystemColors();
if (MergedDictionaries.Count == 0)
{
var rd = GetDesignTimeSystemColors();
if (MergedDictionaries.Count == 0)
{
MergedDictionaries.Add(rd);
}
else
{
MergedDictionaries[0] = rd;
}
MergedDictionaries.Add(rd);
}
}

private void OnThemePropertyChanged()
{
if (!IsInitializePending && DesignMode.DesignModeEnabled)
else
{
UpdateDesignTimeThemeDictionary();
MergedDictionaries[0] = rd;
}
}

private void UpdateDesignTimeThemeDictionary()
{
Debug.Assert(DesignMode.DesignModeEnabled);
ApplyApplicationTheme(RequestedTheme ?? ApplicationTheme.Light);
}

private ResourceDictionary GetDesignTimeSystemColors()
{
Debug.Assert(DesignMode.DesignModeEnabled);
if (AccentColor.HasValue)
{
return new ColorPaletteResources { Accent = AccentColor };
Expand Down

0 comments on commit b5675ea

Please sign in to comment.