diff --git a/ModernWpf.Controls/ModernWpf.Controls.csproj b/ModernWpf.Controls/ModernWpf.Controls.csproj index 79ce1c6c..ba1e10fe 100644 --- a/ModernWpf.Controls/ModernWpf.Controls.csproj +++ b/ModernWpf.Controls/ModernWpf.Controls.csproj @@ -19,7 +19,7 @@ ModernWpfUI - 0.1.191022003 + 0.1.191022005 0.1.0.0 0.1.0.0 Yimeng Wu diff --git a/ModernWpf/ThemeManager.cs b/ModernWpf/ThemeManager.cs index c1e4bab9..e50a3edd 100644 --- a/ModernWpf/ThemeManager.cs +++ b/ModernWpf/ThemeManager.cs @@ -22,6 +22,7 @@ public class ThemeManager : DependencyObject private static readonly Dictionary _defaultThemeDictionaries = new Dictionary(); + private bool _isInitialized; private bool _applicationStarted; #region ApplicationTheme @@ -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; + } } } @@ -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; + } } } @@ -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); @@ -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"); } @@ -657,17 +654,12 @@ 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; @@ -675,16 +667,13 @@ internal void Initialize() { 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(); diff --git a/ModernWpf/ThemeResources.cs b/ModernWpf/ThemeResources.cs index b1d45e4e..13869df2 100644 --- a/ModernWpf/ThemeResources.cs +++ b/ModernWpf/ThemeResources.cs @@ -37,7 +37,6 @@ internal static ThemeResources Current } } - private ApplicationTheme? _requestedTheme; /// /// Gets or sets a value that determines the light-dark preference for the overall /// theme of an app. @@ -48,31 +47,34 @@ internal static ThemeResources Current /// 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; /// /// Gets or sets the accent color of the app. /// 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(); } @@ -95,6 +97,7 @@ public Color? AccentColor private void DesignTimeInit() { + Debug.Assert(DesignMode.DesignModeEnabled); UpdateDesignTimeSystemColors(); UpdateDesignTimeThemeDictionary(); SystemParameters.StaticPropertyChanged += OnSystemParametersPropertyChanged; @@ -102,35 +105,27 @@ private void DesignTimeInit() 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 };