1
- using MaterialDesignColors ;
1
+ using System . Windows . Media ;
2
+ using MaterialDesignColors ;
2
3
3
4
namespace MaterialDesignThemes . Wpf ;
4
5
@@ -10,9 +11,8 @@ public BaseTheme? BaseTheme
10
11
get => _baseTheme ;
11
12
set
12
13
{
13
- if ( _baseTheme != value )
14
+ if ( SetField ( ref _baseTheme , value ) )
14
15
{
15
- _baseTheme = value ;
16
16
SetTheme ( ) ;
17
17
}
18
18
}
@@ -24,9 +24,8 @@ public PrimaryColor? PrimaryColor
24
24
get => _primaryColor ;
25
25
set
26
26
{
27
- if ( _primaryColor != value )
27
+ if ( SetField ( ref _primaryColor , value ) )
28
28
{
29
- _primaryColor = value ;
30
29
SetTheme ( ) ;
31
30
}
32
31
}
@@ -38,9 +37,8 @@ public SecondaryColor? SecondaryColor
38
37
get => _secondaryColor ;
39
38
set
40
39
{
41
- if ( _secondaryColor != value )
40
+ if ( SetField ( ref _secondaryColor , value ) )
42
41
{
43
- _secondaryColor = value ;
44
42
SetTheme ( ) ;
45
43
}
46
44
}
@@ -52,27 +50,44 @@ public ColorAdjustment? ColorAdjustment
52
50
get => _colorAdjustment ;
53
51
set
54
52
{
55
- if ( _colorAdjustment != value )
53
+ if ( SetField ( ref _colorAdjustment , value ) )
56
54
{
57
- _colorAdjustment = value ;
58
55
SetTheme ( ) ;
59
56
}
60
57
}
61
58
}
62
59
63
60
private void SetTheme ( )
64
61
{
65
- if ( BaseTheme is BaseTheme baseTheme &&
66
- PrimaryColor is PrimaryColor primaryColor &&
67
- SecondaryColor is SecondaryColor secondaryColor )
62
+ if ( BaseTheme is not BaseTheme baseTheme ||
63
+ PrimaryColor is not PrimaryColor primaryColor ||
64
+ SecondaryColor is not SecondaryColor secondaryColor )
68
65
{
69
- Theme theme = Theme . Create ( baseTheme ,
70
- SwatchHelper . Lookup [ ( MaterialDesignColor ) primaryColor ] ,
71
- SwatchHelper . Lookup [ ( MaterialDesignColor ) secondaryColor ] ) ;
72
- theme . ColorAdjustment = ColorAdjustment ;
73
-
74
- ApplyTheme ( theme ) ;
66
+ return ;
75
67
}
68
+
69
+ // only perform the registry lookup if needed, and only once
70
+ Lazy < Color ? > accentColor = new ( Theme . GetSystemAccentColor ) ;
71
+
72
+ Color colorPrimary = primaryColor == MaterialDesignColors . PrimaryColor . Inherit
73
+ ? ( accentColor . Value ?? SwatchHelper . Lookup . First ( ) . Value )
74
+ : SwatchHelper . Lookup [ ( MaterialDesignColor ) primaryColor ] ;
75
+
76
+ Color colorSecondary = secondaryColor == MaterialDesignColors . SecondaryColor . Inherit
77
+ ? ( accentColor . Value ?? SwatchHelper . Lookup . First ( ) . Value )
78
+ : SwatchHelper . Lookup [ ( MaterialDesignColor ) secondaryColor ] ;
79
+
80
+ Theme theme = Theme . Create ( baseTheme , colorPrimary , colorSecondary ) ;
81
+ theme . ColorAdjustment = ColorAdjustment ;
82
+
83
+ ApplyTheme ( theme ) ;
84
+ }
85
+
86
+ protected static bool SetField < T > ( ref T field , T value )
87
+ {
88
+ if ( EqualityComparer < T > . Default . Equals ( field , value ) ) return false ;
89
+ field = value ;
90
+ return true ;
76
91
}
77
92
78
93
protected virtual void ApplyTheme ( Theme theme ) =>
0 commit comments