1
- using System ;
1
+ using MaterialDesignColors ;
2
+ using System ;
2
3
using System . Linq ;
3
4
using System . Text . RegularExpressions ;
4
5
using System . Windows ;
5
6
using System . Windows . Media ;
6
- using MaterialDesignColors ;
7
7
8
8
namespace MaterialDesignThemes . Wpf
9
9
{
@@ -20,7 +20,7 @@ public void SetLightDark(bool isDark)
20
20
var source =
21
21
$ "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.{ ( isDark ? "Dark" : "Light" ) } .xaml";
22
22
var newResourceDictionary = new ResourceDictionary ( ) { Source = new Uri ( source ) } ;
23
-
23
+
24
24
Application . Current . Resources . MergedDictionaries . Remove ( existingResourceDictionary ) ;
25
25
Application . Current . Resources . MergedDictionaries . Add ( newResourceDictionary ) ;
26
26
@@ -37,52 +37,46 @@ public void SetLightDark(bool isDark)
37
37
Application . Current . Resources . MergedDictionaries . Add ( newMahAppsResourceDictionary ) ;
38
38
}
39
39
40
- public void ReplacePrimaryColor ( Swatch swatch )
40
+ public void ReplacePrimaryColor ( Swatch swatch , bool mahapps = false )
41
41
{
42
42
if ( swatch == null ) throw new ArgumentNullException ( nameof ( swatch ) ) ;
43
43
44
- ResourceDictionary oldColorResourceDictionary ;
45
- if ( ! TryFindSwatchDictionary ( Application . Current . Resources , "PrimaryHueMidBrush" , out oldColorResourceDictionary ) )
46
- throw new ApplicationException ( "Unable to find primary color definition in Application resources." ) ;
47
-
48
44
var list = swatch . PrimaryHues . ToList ( ) ;
49
45
var light = list [ 2 ] ;
50
46
var mid = swatch . ExemplarHue ;
51
47
var dark = list [ 7 ] ;
52
48
53
- //TODO reuse some of the dupes, freeze.
54
-
55
- var newColorResourceDictionary = new ResourceDictionary
56
- {
57
- { "PrimaryHueLightBrush" , new SolidColorBrush ( light . Color ) } ,
58
- { "PrimaryHueLightForegroundBrush" , new SolidColorBrush ( light . Foreground ) } ,
59
- { "PrimaryHueMidBrush" , new SolidColorBrush ( mid . Color ) } ,
60
- { "PrimaryHueMidForegroundBrush" , new SolidColorBrush ( mid . Foreground ) } ,
61
- { "PrimaryHueDarkBrush" , new SolidColorBrush ( dark . Color ) } ,
62
- { "PrimaryHueDarkForegroundBrush" , new SolidColorBrush ( dark . Foreground ) }
63
- } ;
64
-
65
- if ( oldColorResourceDictionary . Keys . OfType < string > ( ) . Contains ( "HighlightBrush" ) )
49
+ foreach ( var color in swatch . PrimaryHues )
66
50
{
67
- newColorResourceDictionary . Add ( "HighlightBrush" , new SolidColorBrush ( dark . Color ) ) ;
68
- newColorResourceDictionary . Add ( "AccentColorBrush" , new SolidColorBrush ( list [ 5 ] . Color ) ) ;
69
- newColorResourceDictionary . Add ( "AccentColorBrush2" , new SolidColorBrush ( list [ 4 ] . Color ) ) ;
70
- newColorResourceDictionary . Add ( "AccentColorBrush3" , new SolidColorBrush ( list [ 3 ] . Color ) ) ;
71
- newColorResourceDictionary . Add ( "AccentColorBrush4" , new SolidColorBrush ( list [ 2 ] . Color ) ) ;
72
- newColorResourceDictionary . Add ( "WindowTitleColorBrush" , new SolidColorBrush ( dark . Color ) ) ;
73
- newColorResourceDictionary . Add ( "AccentSelectedColorBrush" , new SolidColorBrush ( list [ 5 ] . Foreground ) ) ;
74
- newColorResourceDictionary . Add ( "ProgressBrush" , new LinearGradientBrush ( dark . Color , list [ 3 ] . Color , 90.0 ) ) ;
75
- newColorResourceDictionary . Add ( "CheckmarkFill" , new SolidColorBrush ( list [ 5 ] . Color ) ) ;
76
- newColorResourceDictionary . Add ( "RightArrowFill" , new SolidColorBrush ( list [ 5 ] . Color ) ) ;
77
- newColorResourceDictionary . Add ( "IdealForegroundColorBrush" , new SolidColorBrush ( list [ 5 ] . Foreground ) ) ;
78
- newColorResourceDictionary . Add ( "IdealForegroundDisabledBrush" , new SolidColorBrush ( dark . Color ) { Opacity = .4 } ) ;
51
+ ReplaceEntry ( color . Name , color . Color ) ;
52
+ ReplaceEntry ( color . Name + "Foreground" , color . Foreground ) ;
79
53
}
80
54
81
- Application . Current . Resources . MergedDictionaries . Remove ( oldColorResourceDictionary ) ;
82
- Application . Current . Resources . MergedDictionaries . Add ( newColorResourceDictionary ) ;
55
+ ReplaceEntry ( "PrimaryHueLightBrush" , new SolidColorBrush ( light . Color ) ) ;
56
+ ReplaceEntry ( "PrimaryHueLightForegroundBrush" , new SolidColorBrush ( light . Foreground ) ) ;
57
+ ReplaceEntry ( "PrimaryHueMidBrush" , new SolidColorBrush ( mid . Color ) ) ;
58
+ ReplaceEntry ( "PrimaryHueMidForegroundBrush" , new SolidColorBrush ( mid . Foreground ) ) ;
59
+ ReplaceEntry ( "PrimaryHueDarkBrush" , new SolidColorBrush ( dark . Color ) ) ;
60
+ ReplaceEntry ( "PrimaryHueDarkForegroundBrush" , new SolidColorBrush ( dark . Foreground ) ) ;
61
+
62
+ if ( mahapps )
63
+ {
64
+ ReplaceEntry ( "HighlightBrush" , new SolidColorBrush ( dark . Color ) ) ;
65
+ ReplaceEntry ( "AccentColorBrush" , new SolidColorBrush ( list [ 5 ] . Color ) ) ;
66
+ ReplaceEntry ( "AccentColorBrush2" , new SolidColorBrush ( list [ 4 ] . Color ) ) ;
67
+ ReplaceEntry ( "AccentColorBrush3" , new SolidColorBrush ( list [ 3 ] . Color ) ) ;
68
+ ReplaceEntry ( "AccentColorBrush4" , new SolidColorBrush ( list [ 2 ] . Color ) ) ;
69
+ ReplaceEntry ( "WindowTitleColorBrush" , new SolidColorBrush ( dark . Color ) ) ;
70
+ ReplaceEntry ( "AccentSelectedColorBrush" , new SolidColorBrush ( list [ 5 ] . Foreground ) ) ;
71
+ ReplaceEntry ( "ProgressBrush" , new LinearGradientBrush ( dark . Color , list [ 3 ] . Color , 90.0 ) ) ;
72
+ ReplaceEntry ( "CheckmarkFill" , new SolidColorBrush ( list [ 5 ] . Color ) ) ;
73
+ ReplaceEntry ( "RightArrowFill" , new SolidColorBrush ( list [ 5 ] . Color ) ) ;
74
+ ReplaceEntry ( "IdealForegroundColorBrush" , new SolidColorBrush ( list [ 5 ] . Foreground ) ) ;
75
+ ReplaceEntry ( "IdealForegroundDisabledBrush" , new SolidColorBrush ( dark . Color ) { Opacity = .4 } ) ;
76
+ }
83
77
}
84
78
85
- public void ReplacePrimaryColor ( string name )
79
+ public void ReplacePrimaryColor ( string name , bool mahapps = false )
86
80
{
87
81
if ( name == null ) throw new ArgumentNullException ( nameof ( name ) ) ;
88
82
@@ -92,25 +86,21 @@ public void ReplacePrimaryColor(string name)
92
86
if ( swatch == null )
93
87
throw new ArgumentException ( $ "No such swatch '{ name } '", nameof ( name ) ) ;
94
88
95
- ReplacePrimaryColor ( swatch ) ;
89
+ ReplacePrimaryColor ( swatch , mahapps ) ;
96
90
}
97
91
98
92
public void ReplaceAccentColor ( Swatch swatch )
99
93
{
100
94
if ( swatch == null ) throw new ArgumentNullException ( nameof ( swatch ) ) ;
101
95
102
- ResourceDictionary oldColorResourceDictionary ;
103
- if ( ! TryFindSwatchDictionary ( Application . Current . Resources , "SecondaryAccentBrush" , out oldColorResourceDictionary ) )
104
- throw new ApplicationException ( "Unable to find accent color definition in Application resources." ) ;
105
-
106
- var newColorResourceDictionary = new ResourceDictionary
96
+ foreach ( var color in swatch . AccentHues )
107
97
{
108
- { "SecondaryAccentBrush" , new SolidColorBrush ( swatch . AccentExemplarHue . Color ) } ,
109
- { "SecondaryAccentForegroundBrush ", new SolidColorBrush ( swatch . AccentExemplarHue . Foreground ) } ,
110
- } ;
98
+ ReplaceEntry ( color . Name , color . Color ) ;
99
+ ReplaceEntry ( color . Name + "Foreground ", color . Foreground ) ;
100
+ }
111
101
112
- Application . Current . Resources . MergedDictionaries . Remove ( oldColorResourceDictionary ) ;
113
- Application . Current . Resources . MergedDictionaries . Add ( newColorResourceDictionary ) ;
102
+ ReplaceEntry ( "SecondaryAccentBrush" , new SolidColorBrush ( swatch . AccentExemplarHue . Color ) ) ;
103
+ ReplaceEntry ( "SecondaryAccentForegroundBrush" , new SolidColorBrush ( swatch . AccentExemplarHue . Foreground ) ) ;
114
104
}
115
105
116
106
public void ReplaceAccentColor ( string name )
@@ -126,10 +116,31 @@ public void ReplaceAccentColor(string name)
126
116
ReplaceAccentColor ( swatch ) ;
127
117
}
128
118
129
- private static bool TryFindSwatchDictionary ( ResourceDictionary parentDictionary , string expectedBrushName , out ResourceDictionary dictionary )
119
+ /// <summary>
120
+ /// Replaces a certain entry anywhere in the parent dictionary and its merged dictionaries
121
+ /// </summary>
122
+ /// <param name="entryName">The entry to replace</param>
123
+ /// <param name="newValue">The new entry value</param>
124
+ /// <param name="parentDictionary">The root dictionary to start searching at. Null means using Application.Current.Resources</param>
125
+ /// <returns>Weather the value was replaced (true) or not (false)</returns>
126
+ private static bool ReplaceEntry ( object entryName , object newValue , ResourceDictionary parentDictionary = null )
130
127
{
131
- dictionary = parentDictionary . MergedDictionaries . SingleOrDefault ( rd => rd [ expectedBrushName ] != null ) ;
132
- return dictionary != null ;
128
+ if ( parentDictionary == null )
129
+ parentDictionary = Application . Current . Resources ;
130
+
131
+ if ( parentDictionary . Contains ( entryName ) )
132
+ {
133
+ parentDictionary [ entryName ] = newValue ;
134
+ return true ;
135
+ }
136
+
137
+ foreach ( var dictionary in parentDictionary . MergedDictionaries )
138
+ {
139
+ if ( ReplaceEntry ( entryName , newValue , dictionary ) )
140
+ return true ;
141
+ }
142
+
143
+ return false ;
133
144
}
134
145
}
135
146
}
0 commit comments