Skip to content

Commit fb1f11f

Browse files
authored
Allow Inherit to be specified for the CustomColorTheme as well (#3816)
This allows for using the system theme accent color when using the CustomColorTheme.
1 parent ae59e31 commit fb1f11f

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

src/MainDemo.Wpf/App.xaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
<ResourceDictionary>
1313
<ResourceDictionary.MergedDictionaries>
1414
<!-- This is the current way to set up your app's initial theme -->
15+
<!-- PrimaryColor and SecondaryColor also support the constant string "Inherit" to specify the color should use the system theme accent color -->
1516
<materialDesign:BundledTheme BaseTheme="Inherit"
1617
ColorAdjustment="{materialDesign:ColorAdjustment}"
1718
PrimaryColor="DeepPurple"
1819
SecondaryColor="Lime" />
1920

20-
<!-- If you would prefer to use your own colors, there is an option for that as well: -->
21-
<!--<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="Aqua" SecondaryColor="DarkGreen" />-->
21+
<!--
22+
If you would prefer to use your own colors, there is an option for that as well:
23+
PrimaryColor and SecondaryColor also support the constant string "Inherit" to specify the color should use the system theme accent color
24+
-->
25+
<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="Aqua" SecondaryColor="#FF006400" />
2226

2327
<!-- You can also use the built-in theme dictionaries: -->
2428
<!--

src/MaterialDesignColors.Wpf/SwatchHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace MaterialDesignColors;
55

66
public static class SwatchHelper
77
{
8-
public static IEnumerable<ISwatch> Swatches { get; } = new ISwatch[]
9-
{
8+
public static IEnumerable<ISwatch> Swatches { get; } =
9+
[
1010
new RedSwatch(),
1111
new PinkSwatch(),
1212
new PurpleSwatch(),
@@ -26,7 +26,7 @@ public static class SwatchHelper
2626
new BrownSwatch(),
2727
new GreySwatch(),
2828
new BlueGreySwatch(),
29-
};
29+
];
3030

3131
public static IDictionary<MaterialDesignColor, Color> Lookup { get; } = Swatches.SelectMany(o => o.Lookup).ToDictionary(o => o.Key, o => o.Value);
3232
}

src/MaterialDesignThemes.Wpf/BundledTheme.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ PrimaryColor is not PrimaryColor primaryColor ||
7070
Lazy<Color?> accentColor = new(Theme.GetSystemAccentColor);
7171

7272
Color colorPrimary = primaryColor == MaterialDesignColors.PrimaryColor.Inherit
73-
? (accentColor.Value ?? SwatchHelper.Lookup.First().Value)
73+
? (accentColor.Value ?? default)
7474
: SwatchHelper.Lookup[(MaterialDesignColor)primaryColor];
7575

7676
Color colorSecondary = secondaryColor == MaterialDesignColors.SecondaryColor.Inherit
77-
? (accentColor.Value ?? SwatchHelper.Lookup.First().Value)
77+
? (accentColor.Value ?? default)
7878
: SwatchHelper.Lookup[(MaterialDesignColor)secondaryColor];
7979

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

src/MaterialDesignThemes.Wpf/CustomColorTheme.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows.Media;
1+
using System.ComponentModel;
2+
using System.Windows.Media;
23

34
namespace MaterialDesignThemes.Wpf;
45

@@ -19,6 +20,7 @@ public BaseTheme? BaseTheme
1920
}
2021

2122
private Color? _primaryColor;
23+
[TypeConverter(typeof(InheritSystemColorTypeConverter))]
2224
public Color? PrimaryColor
2325
{
2426
get => _primaryColor;
@@ -33,6 +35,7 @@ public Color? PrimaryColor
3335
}
3436

3537
private Color? _secondaryColor;
38+
[TypeConverter(typeof(InheritSystemColorTypeConverter))]
3639
public Color? SecondaryColor
3740
{
3841
get => _secondaryColor;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.ComponentModel;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Windows.Media;
4+
5+
namespace MaterialDesignThemes.Wpf;
6+
7+
internal sealed class InheritSystemColorTypeConverter : TypeConverter
8+
{
9+
private const string Inherit = "Inherit";
10+
11+
private ColorConverter ColorConverter { get; } = new();
12+
13+
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
14+
=> sourceType == typeof(string) ||
15+
ColorConverter.CanConvertFrom(context, sourceType) ||
16+
base.CanConvertFrom(context, sourceType);
17+
18+
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
19+
=> ColorConverter.CanConvertTo(context, destinationType) ||
20+
base.CanConvertTo(context, destinationType);
21+
22+
public override object ConvertFrom(ITypeDescriptorContext? td, System.Globalization.CultureInfo? ci, object? value)
23+
{
24+
if (value is null)
25+
{
26+
throw GetConvertFromException(value);
27+
}
28+
29+
string? s = value as string ?? throw new ArgumentNullException(nameof(value));
30+
31+
if (string.Equals(s, Inherit, StringComparison.OrdinalIgnoreCase))
32+
{
33+
return Theme.GetSystemAccentColor() ?? default;
34+
}
35+
36+
return ColorConverter.ConvertFrom(td, ci, s);
37+
}
38+
39+
public override object ConvertTo(ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, Type destinationType)
40+
{
41+
if (value is Color color &&
42+
color != default &&
43+
color == Theme.GetSystemAccentColor())
44+
{
45+
return Inherit;
46+
}
47+
return ColorConverter.ConvertTo(context, culture, value, destinationType);
48+
}
49+
}

0 commit comments

Comments
 (0)