Skip to content

Commit 2aeccdd

Browse files
authored
Adding support for ColorReferences in themes (#3395)
Bumped demos to net8
1 parent a7d2143 commit 2aeccdd

File tree

18 files changed

+864
-173
lines changed

18 files changed

+864
-173
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Copyright>Copyright © 2022</Copyright>
44
<Company>Mulholland Software/James Willock</Company>
55

6-
<LangVersion>11.0</LangVersion>
6+
<LangVersion>12.0</LangVersion>
77
<ErrorReport>prompt</ErrorReport>
88

99
<SignAssembly>true</SignAssembly>

MahMaterialDragablzMashUp/MahAppsDragablzDemo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFrameworks>net472;net6.0-windows;net7.0-windows</TargetFrameworks>
5+
<TargetFrameworks>net472;net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks>
66
<UseWPF>true</UseWPF>
77
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
88
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

MainDemo.Wpf/MaterialDesignDemo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFrameworks>net472;net6.0-windows;net7.0-windows</TargetFrameworks>
5+
<TargetFrameworks>net472;net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks>
66
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
77
<Prefer32Bit Condition="'$(TargetFramework)' == 'net472'">true</Prefer32Bit>
88
<ApplicationIcon>favicon.ico</ApplicationIcon>

MaterialDesign3.Demo.Wpf/MaterialDesign3Demo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFrameworks>net472;net6.0-windows;net7.0-windows</TargetFrameworks>
5+
<TargetFrameworks>net472;net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks>
66
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
77
<Prefer32Bit Condition="'$(TargetFramework)' == 'net472'">true</Prefer32Bit>
88
<ApplicationIcon>favicon.ico</ApplicationIcon>

MaterialDesignColors.Wpf/ColorPair.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,17 @@
55
namespace MaterialDesignColors;
66

77
[DebuggerDisplay($"Color: {{{nameof(Color)}}}; Foreground: {{{nameof(ForegroundColor)}}}")]
8-
public struct ColorPair
8+
public readonly struct ColorPair(Color color, Color? foregroundColor)
99
{
10-
public Color Color { get; set; }
10+
public static implicit operator ColorPair(Color color) => new(color, null);
1111

12-
/// <summary>
13-
/// The foreground or opposite color. If left null, this will be calculated for you.
14-
/// Calculated by calling ColorAssist.ContrastingForegroundColor()
15-
/// </summary>
16-
public Color? ForegroundColor { get; set; }
12+
public Color Color => color;
1713

18-
public static implicit operator ColorPair(Color color) => new ColorPair(color);
14+
public Color? ForegroundColor => foregroundColor;
1915

2016
public ColorPair(Color color)
21-
{
22-
Color = color;
23-
ForegroundColor = null;
24-
}
25-
26-
public ColorPair(Color color, Color? foreground)
27-
{
28-
Color = color;
29-
ForegroundColor = foreground;
30-
}
17+
: this(color, null)
18+
{ }
3119

3220
public Color GetForegroundColor() => ForegroundColor ?? Color.ContrastingForegroundColor();
3321
}

MaterialDesignThemes.MahApps/MahAppsCustomColorTheme.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void ThemeManagerOnThemeChanged(object? sender, Wpf.ThemeChangedEventArg
8787

8888
Theme newTheme = e.NewTheme;
8989

90-
BaseTheme baseTheme = newTheme.Background.IsLightColor()
90+
BaseTheme baseTheme = ((Color)newTheme.Background).IsLightColor()
9191
? Wpf.BaseTheme.Light
9292
: Wpf.BaseTheme.Dark;
9393

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Windows.Media;
2+
3+
namespace MaterialDesignThemes.Wpf.Tests;
4+
5+
public class ThemeTests
6+
{
7+
[Fact]
8+
public void CanSetForegroundWithColor()
9+
{
10+
var theme = Theme.Create(BaseTheme.Dark, Colors.Red, Colors.Blue);
11+
theme.Foreground = Colors.Green;
12+
13+
Assert.Equal<Color>(Colors.Green, theme.Foreground);
14+
}
15+
16+
[Fact]
17+
public void CanSetForegroundWithThemeColorReference()
18+
{
19+
var theme = Theme.Create(BaseTheme.Dark, Colors.Red, Colors.Blue);
20+
theme.Foreground = ThemeColorReference.PrimaryMid;
21+
22+
Assert.Equal<Color>(Colors.Red, theme.Foreground);
23+
}
24+
25+
[Fact]
26+
public void CanSetForegroundWithColorReference()
27+
{
28+
var theme = Theme.Create(BaseTheme.Dark, Colors.Red, Colors.Blue);
29+
theme.Foreground = ColorReference.PrimaryMid;
30+
31+
Assert.Equal<Color>(Colors.Red, theme.Foreground);
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Windows.Media;
2+
3+
namespace MaterialDesignThemes.Wpf;
4+
5+
public readonly record struct ColorReference(ThemeColorReference ThemeReference, Color? Color)
6+
{
7+
public static ColorReference SecondaryLight { get; } = new ColorReference(ThemeColorReference.SecondaryLight, null);
8+
public static ColorReference SecondaryMid { get; } = new ColorReference(ThemeColorReference.SecondaryMid, null);
9+
public static ColorReference SecondaryDark { get; } = new ColorReference(ThemeColorReference.SecondaryDark, null);
10+
public static ColorReference PrimaryLight { get; } = new ColorReference(ThemeColorReference.PrimaryLight, null);
11+
public static ColorReference PrimaryMid { get; } = new ColorReference(ThemeColorReference.PrimaryMid, null);
12+
public static ColorReference PrimaryDark { get; } = new ColorReference(ThemeColorReference.PrimaryDark, null);
13+
14+
public static implicit operator ColorReference(Color color) => new(ThemeColorReference.None, color);
15+
public static implicit operator ColorReference(ThemeColorReference @ref) => new(@ref, null);
16+
public static implicit operator Color(ColorReference color) => color.Color ??
17+
throw new InvalidOperationException($"{nameof(ColorReference)} does not contain any color");
18+
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2626
</Content>
2727
</ItemGroup>
28+
<ItemGroup>
29+
<Compile Include="..\Shims\IsExternalInit.cs" Link="IsExternalInit.cs" />
30+
</ItemGroup>
2831
<ItemGroup>
2932
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" />
3033
</ItemGroup>

MaterialDesignThemes.Wpf/ResourceDictionaryExtensions.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Runtime.CompilerServices;
2-
using System.Windows.Media;
1+
using System.Windows.Media;
32
using System.Windows.Media.Animation;
43
using MaterialDesignColors;
54
using MaterialDesignColors.ColorManipulation;
@@ -220,12 +219,9 @@ private static void AdjustColors(Color background, ColorAdjustment colorAdjustme
220219
}
221220
}
222221

223-
private class ThemeManager : IThemeManager
222+
private class ThemeManager(ResourceDictionary resourceDictionary) : IThemeManager
224223
{
225-
private readonly ResourceDictionary _resourceDictionary;
226-
227-
public ThemeManager(ResourceDictionary resourceDictionary)
228-
=> _resourceDictionary = resourceDictionary ?? throw new ArgumentNullException(nameof(resourceDictionary));
224+
private readonly ResourceDictionary _resourceDictionary = resourceDictionary ?? throw new ArgumentNullException(nameof(resourceDictionary));
229225

230226
public event EventHandler<ThemeChangedEventArgs>? ThemeChanged;
231227

0 commit comments

Comments
 (0)