diff --git a/src/MicaWPF.Core/Services/IThemeDictionaryService.cs b/src/MicaWPF.Core/Services/IThemeDictionaryService.cs index 351e53e..6ae54aa 100644 --- a/src/MicaWPF.Core/Services/IThemeDictionaryService.cs +++ b/src/MicaWPF.Core/Services/IThemeDictionaryService.cs @@ -3,6 +3,7 @@ // using System.ComponentModel; +using MicaWPF.Core.Enums; namespace MicaWPF.Core.Services; @@ -31,4 +32,10 @@ public interface IThemeDictionaryService /// Force the current theme source to reload. /// void RefreshThemeSource(); + + /// + /// Gets the current theme from the loaded resource dictionnary. + /// + /// The current ap theme. + WindowsTheme GetCurrentResourcesTheme(); } \ No newline at end of file diff --git a/src/MicaWPF.Core/Services/ThemeDictionaryService.cs b/src/MicaWPF.Core/Services/ThemeDictionaryService.cs index cba2a4c..3a607e0 100644 --- a/src/MicaWPF.Core/Services/ThemeDictionaryService.cs +++ b/src/MicaWPF.Core/Services/ThemeDictionaryService.cs @@ -5,7 +5,9 @@ using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; +using MicaWPF.Core.Enums; using MicaWPF.Core.Helpers; +using MicaWPF.Core.Styles; namespace MicaWPF.Core.Services; @@ -54,6 +56,22 @@ public void SetThemeSource(Uri source) } } + public WindowsTheme GetCurrentResourcesTheme() + { + var dictionnaries = Application.Current.Resources.MergedDictionaries; + foreach (var dictionary in dictionnaries) + { + if (dictionary is not ThemeDictionaryBase themeDictionary) + { + continue; + } + + return themeDictionary.Theme; + } + + return WindowsTheme.Auto; + } + public void RefreshThemeSource() { if (_currentThemeSource is not null) diff --git a/src/MicaWPF.Core/Services/ThemeService.cs b/src/MicaWPF.Core/Services/ThemeService.cs index 564ef22..5c95d8b 100644 --- a/src/MicaWPF.Core/Services/ThemeService.cs +++ b/src/MicaWPF.Core/Services/ThemeService.cs @@ -25,7 +25,7 @@ public class ThemeService : IThemeService public ThemeService() { - ChangeTheme(WindowsTheme.Auto); + ChangeTheme(MicaWPFServiceUtility.ThemeDictionaryService.GetCurrentResourcesTheme()); SetThemeAware(); } diff --git a/src/MicaWPF.Core/Styles/ThemeDictionaryBase.cs b/src/MicaWPF.Core/Styles/ThemeDictionaryBase.cs index 6cef1a7..0a44f09 100644 --- a/src/MicaWPF.Core/Styles/ThemeDictionaryBase.cs +++ b/src/MicaWPF.Core/Styles/ThemeDictionaryBase.cs @@ -13,31 +13,32 @@ namespace MicaWPF.Core.Styles; [UsableDuringInitialization(true)] public abstract class ThemeDictionaryBase : ResourceDictionary { - private WindowsTheme? _theme = null; private WindowsTheme? _designModeTheme = null; public ThemeDictionaryBase() { if (DesignTimeHelper.IsDesignMode && !_designModeTheme.HasValue) { - SetTheme(WindowsTheme.Light); + SetTheme(Enums.WindowsTheme.Light); return; } - if(!_theme.HasValue) + if(!WindowsTheme.HasValue) { - SetTheme(WindowsTheme.Auto); + SetTheme(Enums.WindowsTheme.Auto); } } + public WindowsTheme? WindowsTheme { get; private set; } = null; + public virtual string SourceLocation { get; } = string.Empty; public WindowsTheme Theme { - get => _theme!.Value; + get => WindowsTheme!.Value; set { - _theme = value; + WindowsTheme = value; if (!DesignTimeHelper.IsDesignMode) { SetTheme(value); @@ -62,7 +63,7 @@ protected void SetTheme(WindowsTheme value) { var themeName = value switch { - WindowsTheme.Dark => "MicaDark", + Enums.WindowsTheme.Dark => "MicaDark", _ => "MicaLight", };