Skip to content

Commit

Permalink
Merge pull request #96 from Simnico99/fix-theme-not-applying
Browse files Browse the repository at this point in the history
Fixed theme not applying on start.
  • Loading branch information
Simnico99 committed Jan 31, 2024
2 parents d5201aa + 2fd5d28 commit 31d6394
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/MicaWPF.Core/Services/IThemeDictionaryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// </copyright>

using System.ComponentModel;
using MicaWPF.Core.Enums;

namespace MicaWPF.Core.Services;

Expand Down Expand Up @@ -31,4 +32,10 @@ public interface IThemeDictionaryService
/// Force the current theme source to reload.
/// </summary>
void RefreshThemeSource();

/// <summary>
/// Gets the current theme from the loaded resource dictionnary.
/// </summary>
/// <returns>The current ap theme.</returns>
WindowsTheme GetCurrentResourcesTheme();
}
18 changes: 18 additions & 0 deletions src/MicaWPF.Core/Services/ThemeDictionaryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/MicaWPF.Core/Services/ThemeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ThemeService : IThemeService

public ThemeService()
{
ChangeTheme(WindowsTheme.Auto);
ChangeTheme(MicaWPFServiceUtility.ThemeDictionaryService.GetCurrentResourcesTheme());
SetThemeAware();
}

Expand Down
15 changes: 8 additions & 7 deletions src/MicaWPF.Core/Styles/ThemeDictionaryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -62,7 +63,7 @@ protected void SetTheme(WindowsTheme value)
{
var themeName = value switch
{
WindowsTheme.Dark => "MicaDark",
Enums.WindowsTheme.Dark => "MicaDark",
_ => "MicaLight",
};

Expand Down

0 comments on commit 31d6394

Please sign in to comment.