Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/MaterialDesignColors.Wpf/MaterialDesignColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public enum PrimaryColor
{
Inherit,
Red = MaterialDesignColor.Red500,
Pink = MaterialDesignColor.Pink500,
Purple = MaterialDesignColor.Purple500,
Expand All @@ -25,6 +26,7 @@ public enum PrimaryColor

public enum SecondaryColor
{
Inherit,
Red = MaterialDesignColor.RedSecondary,
Pink = MaterialDesignColor.PinkSecondary,
Purple = MaterialDesignColor.PurpleSecondary,
Expand Down
42 changes: 30 additions & 12 deletions src/MaterialDesignThemes.Wpf/BundledTheme.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MaterialDesignColors;
using System.Windows.Media;
using MaterialDesignColors;

namespace MaterialDesignThemes.Wpf;

Expand All @@ -10,9 +11,8 @@ public BaseTheme? BaseTheme
get => _baseTheme;
set
{
if (_baseTheme != value)
if (SetField(ref _baseTheme, value))
Comment thread
corvinsz marked this conversation as resolved.
{
_baseTheme = value;
SetTheme();
}
}
Expand Down Expand Up @@ -62,17 +62,35 @@ public ColorAdjustment? ColorAdjustment

private void SetTheme()
{
if (BaseTheme is BaseTheme baseTheme &&
PrimaryColor is PrimaryColor primaryColor &&
SecondaryColor is SecondaryColor secondaryColor)
if (BaseTheme is not BaseTheme baseTheme ||
PrimaryColor is not PrimaryColor primaryColor ||
SecondaryColor is not SecondaryColor secondaryColor)
{
Theme theme = Theme.Create(baseTheme,
SwatchHelper.Lookup[(MaterialDesignColor)primaryColor],
SwatchHelper.Lookup[(MaterialDesignColor)secondaryColor]);
theme.ColorAdjustment = ColorAdjustment;

ApplyTheme(theme);
return;
}

// only perform the registry lookup if needed, and only once
Lazy<Color?> accentColor = new(Theme.GetSystemAccentColor);

Color colorPrimary = primaryColor == MaterialDesignColors.PrimaryColor.Inherit
? (accentColor.Value ?? SwatchHelper.Lookup.First().Value)
: SwatchHelper.Lookup[(MaterialDesignColor)primaryColor];

Color colorSecondary = secondaryColor == MaterialDesignColors.SecondaryColor.Inherit
? (accentColor.Value ?? SwatchHelper.Lookup.First().Value)
: SwatchHelper.Lookup[(MaterialDesignColor)secondaryColor];

Theme theme = Theme.Create(baseTheme, colorPrimary, colorSecondary);
theme.ColorAdjustment = ColorAdjustment;

ApplyTheme(theme);
}

protected bool SetField<T>(ref T field, T value)
Comment thread
corvinsz marked this conversation as resolved.
Outdated
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
return true;
}

protected virtual void ApplyTheme(Theme theme) =>
Expand Down
41 changes: 40 additions & 1 deletion src/MaterialDesignThemes.Wpf/Theme.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Media;
using MaterialDesignColors;
using Microsoft.Win32;

Expand Down Expand Up @@ -31,6 +32,44 @@ public partial class Theme
}
}

/// <summary>
/// Get the current Windows accent color.
/// Based on ControlzEx
/// https://github.com/ControlzEx/ControlzEx/blob/48230bb023c588e1b7eb86ea83f7ddf7d25be735/src/ControlzEx/Theming/WindowsThemeHelper.cs#L53
/// </summary>
/// <returns></returns>
public static Color? GetSystemAccentColor()
{
Color? accentColor = null;

try
{
var registryValue = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", "ColorizationColor", null);

if (registryValue is null)
{
return null;
}

// We get negative values out of the registry, so we have to cast to int from object first.
// Casting from int to uint works afterwards and converts the number correctly.
var pp = (uint)(int)registryValue;
if (pp > 0)
{
var bytes = BitConverter.GetBytes(pp);
accentColor = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
}

return accentColor;
}
catch (Exception exception)
{
Trace.TraceError(exception.ToString());
}

return accentColor;
}

public static Theme Create(BaseTheme baseTheme, Color primary, Color secondary)
{
Theme theme = new();
Expand Down