forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceDictionaryExtensions.cs
More file actions
225 lines (196 loc) · 12.9 KB
/
Copy pathResourceDictionaryExtensions.cs
File metadata and controls
225 lines (196 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation;
namespace MaterialDesignThemes.Wpf
{
public static class ResourceDictionaryExtensions
{
private static Guid CurrentThemeKey { get; } = Guid.NewGuid();
private static Guid ThemeManagerKey { get; } = Guid.NewGuid();
public static void SetTheme(this ResourceDictionary resourceDictionary, ITheme theme)
{
if (resourceDictionary == null) throw new ArgumentNullException(nameof(resourceDictionary));
SetSolidColorBrush(resourceDictionary, "PrimaryHueLightBrush", theme.PrimaryLight.Color);
SetSolidColorBrush(resourceDictionary, "PrimaryHueLightForegroundBrush", theme.PrimaryLight.ForegroundColor ?? theme.PrimaryLight.Color.ContrastingForegroundColor());
SetSolidColorBrush(resourceDictionary, "PrimaryHueMidBrush", theme.PrimaryMid.Color);
SetSolidColorBrush(resourceDictionary, "PrimaryHueMidForegroundBrush", theme.PrimaryMid.ForegroundColor ?? theme.PrimaryMid.Color.ContrastingForegroundColor());
SetSolidColorBrush(resourceDictionary, "PrimaryHueDarkBrush", theme.PrimaryDark.Color);
SetSolidColorBrush(resourceDictionary, "PrimaryHueDarkForegroundBrush", theme.PrimaryDark.ForegroundColor ?? theme.PrimaryDark.Color.ContrastingForegroundColor());
SetSolidColorBrush(resourceDictionary, "SecondaryHueLightBrush", theme.SecondaryLight.Color);
SetSolidColorBrush(resourceDictionary, "SecondaryHueLightForegroundBrush", theme.SecondaryLight.ForegroundColor ?? theme.SecondaryLight.Color.ContrastingForegroundColor());
SetSolidColorBrush(resourceDictionary, "SecondaryHueMidBrush", theme.SecondaryMid.Color);
SetSolidColorBrush(resourceDictionary, "SecondaryHueMidForegroundBrush", theme.SecondaryMid.ForegroundColor ?? theme.SecondaryMid.Color.ContrastingForegroundColor());
SetSolidColorBrush(resourceDictionary, "SecondaryHueDarkBrush", theme.SecondaryDark.Color);
SetSolidColorBrush(resourceDictionary, "SecondaryHueDarkForegroundBrush", theme.SecondaryDark.ForegroundColor ?? theme.SecondaryDark.Color.ContrastingForegroundColor());
//NB: These are here for backwards compatibility, and will be removed in a future version.
SetSolidColorBrush(resourceDictionary, "SecondaryAccentBrush", theme.SecondaryMid.Color);
SetSolidColorBrush(resourceDictionary, "SecondaryAccentForegroundBrush", theme.SecondaryMid.ForegroundColor ?? theme.SecondaryMid.Color.ContrastingForegroundColor());
SetSolidColorBrush(resourceDictionary, "ValidationErrorBrush", theme.ValidationError);
resourceDictionary["ValidationErrorColor"] = theme.ValidationError;
SetSolidColorBrush(resourceDictionary, "MaterialDesignBackground", theme.Background);
SetSolidColorBrush(resourceDictionary, "MaterialDesignPaper", theme.Paper);
SetSolidColorBrush(resourceDictionary, "MaterialDesignCardBackground", theme.CardBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignToolBarBackground", theme.ToolBarBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignBody", theme.Body);
SetSolidColorBrush(resourceDictionary, "MaterialDesignBodyLight", theme.BodyLight);
SetSolidColorBrush(resourceDictionary, "MaterialDesignColumnHeader", theme.ColumnHeader);
SetSolidColorBrush(resourceDictionary, "MaterialDesignCheckBoxOff", theme.CheckBoxOff);
SetSolidColorBrush(resourceDictionary, "MaterialDesignCheckBoxDisabled", theme.CheckBoxDisabled);
SetSolidColorBrush(resourceDictionary, "MaterialDesignTextBoxBorder", theme.TextBoxBorder);
SetSolidColorBrush(resourceDictionary, "MaterialDesignDivider", theme.Divider);
SetSolidColorBrush(resourceDictionary, "MaterialDesignSelection", theme.Selection);
SetSolidColorBrush(resourceDictionary, "MaterialDesignToolForeground", theme.ToolForeground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignToolBackground", theme.ToolBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignFlatButtonClick", theme.FlatButtonClick);
SetSolidColorBrush(resourceDictionary, "MaterialDesignFlatButtonRipple", theme.FlatButtonRipple);
SetSolidColorBrush(resourceDictionary, "MaterialDesignToolTipBackground", theme.ToolTipBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignChipBackground", theme.ChipBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignSnackbarBackground", theme.SnackbarBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignSnackbarMouseOver", theme.SnackbarMouseOver);
SetSolidColorBrush(resourceDictionary, "MaterialDesignSnackbarRipple", theme.SnackbarRipple);
SetSolidColorBrush(resourceDictionary, "MaterialDesignTextFieldBoxBackground", theme.TextFieldBoxBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignTextFieldBoxHoverBackground", theme.TextFieldBoxHoverBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignTextFieldBoxDisabledBackground", theme.TextFieldBoxDisabledBackground);
SetSolidColorBrush(resourceDictionary, "MaterialDesignTextAreaBorder", theme.TextAreaBorder);
SetSolidColorBrush(resourceDictionary, "MaterialDesignTextAreaInactiveBorder", theme.TextAreaInactiveBorder);
SetSolidColorBrush(resourceDictionary, "MaterialDesignDataGridRowHoverBackground", theme.DataGridRowHoverBackground);
if (!(resourceDictionary.GetThemeManager() is ThemeManager themeManager))
{
resourceDictionary[ThemeManagerKey] = themeManager = new ThemeManager(resourceDictionary);
}
ITheme oldTheme = resourceDictionary.GetTheme();
resourceDictionary[CurrentThemeKey] = theme;
themeManager.OnThemeChange(oldTheme, theme);
}
public static ITheme GetTheme(this ResourceDictionary resourceDictionary)
{
if (resourceDictionary == null) throw new ArgumentNullException(nameof(resourceDictionary));
if (resourceDictionary[CurrentThemeKey] is ITheme theme)
{
return theme;
}
Color secondaryMid = GetColor("SecondaryHueMidBrush", "SecondaryAccentBrush");
Color secondaryMidForeground = GetColor("SecondaryHueMidForegroundBrush", "SecondaryAccentForegroundBrush");
if (!TryGetColor("SecondaryHueLightBrush", out Color secondaryLight))
{
secondaryLight = secondaryMid.Lighten();
}
if (!TryGetColor("SecondaryHueLightForegroundBrush", out Color secondaryLightForeground))
{
secondaryLightForeground = secondaryLight.ContrastingForegroundColor();
}
if (!TryGetColor("SecondaryHueDarkBrush", out Color secondaryDark))
{
secondaryDark = secondaryMid.Darken();
}
if (!TryGetColor("SecondaryHueDarkForegroundBrush", out Color secondaryDarkForeground))
{
secondaryDarkForeground = secondaryDark.ContrastingForegroundColor();
}
//Attempt to simply look up the appropriate resources
return new Theme
{
PrimaryLight = new ColorPair(GetColor("PrimaryHueLightBrush"), GetColor("PrimaryHueLightForegroundBrush")),
PrimaryMid = new ColorPair(GetColor("PrimaryHueMidBrush"), GetColor("PrimaryHueMidForegroundBrush")),
PrimaryDark = new ColorPair(GetColor("PrimaryHueDarkBrush"), GetColor("PrimaryHueDarkForegroundBrush")),
SecondaryLight = new ColorPair(secondaryLight, secondaryLightForeground),
SecondaryMid = new ColorPair(secondaryMid, secondaryMidForeground),
SecondaryDark = new ColorPair(secondaryDark, secondaryDarkForeground),
Background = GetColor("MaterialDesignBackground"),
Body = GetColor("MaterialDesignBody"),
BodyLight = GetColor("MaterialDesignBodyLight"),
CardBackground = GetColor("MaterialDesignCardBackground"),
CheckBoxDisabled = GetColor("MaterialDesignCheckBoxDisabled"),
CheckBoxOff = GetColor("MaterialDesignCheckBoxOff"),
ChipBackground = GetColor("MaterialDesignChipBackground"),
ColumnHeader = GetColor("MaterialDesignColumnHeader"),
DataGridRowHoverBackground = GetColor("MaterialDesignDataGridRowHoverBackground"),
Divider = GetColor("MaterialDesignDivider"),
FlatButtonClick = GetColor("MaterialDesignFlatButtonClick"),
FlatButtonRipple = GetColor("MaterialDesignFlatButtonRipple"),
Selection = GetColor("MaterialDesignSelection"),
SnackbarBackground = GetColor("MaterialDesignSnackbarBackground"),
SnackbarMouseOver = GetColor("MaterialDesignSnackbarMouseOver"),
SnackbarRipple = GetColor("MaterialDesignSnackbarRipple"),
TextAreaBorder = GetColor("MaterialDesignTextAreaBorder"),
TextAreaInactiveBorder = GetColor("MaterialDesignTextAreaInactiveBorder"),
TextBoxBorder = GetColor("MaterialDesignTextBoxBorder"),
TextFieldBoxBackground = GetColor("MaterialDesignTextFieldBoxBackground"),
TextFieldBoxDisabledBackground = GetColor("MaterialDesignTextFieldBoxDisabledBackground"),
TextFieldBoxHoverBackground = GetColor("MaterialDesignTextFieldBoxHoverBackground"),
ToolBackground = GetColor("MaterialDesignToolBackground"),
ToolBarBackground = GetColor("MaterialDesignToolBarBackground"),
ToolForeground = GetColor("MaterialDesignToolForeground"),
ToolTipBackground = GetColor("MaterialDesignToolTipBackground"),
Paper = GetColor("MaterialDesignPaper"),
ValidationError = GetColor("ValidationErrorBrush")
};
Color GetColor(params string[] keys)
{
foreach (string key in keys)
{
if (TryGetColor(key, out Color color))
{
return color;
}
}
throw new InvalidOperationException($"Could not locate required resource with key(s) '{string.Join(", ", keys)}'");
}
bool TryGetColor(string key, out Color color)
{
if (resourceDictionary[key] is SolidColorBrush brush)
{
color = brush.Color;
return true;
}
color = default;
return false;
}
}
public static IThemeManager GetThemeManager(this ResourceDictionary resourceDictionary)
{
if (resourceDictionary == null) throw new ArgumentNullException(nameof(resourceDictionary));
return resourceDictionary[ThemeManagerKey] as IThemeManager;
}
internal static void SetSolidColorBrush(this ResourceDictionary sourceDictionary, string name, Color value)
{
if (sourceDictionary == null) throw new ArgumentNullException(nameof(sourceDictionary));
if (name == null) throw new ArgumentNullException(nameof(name));
sourceDictionary[name + "Color"] = value;
if (sourceDictionary[name] is SolidColorBrush brush)
{
if (brush.Color == value) return;
if (!brush.IsFrozen)
{
var animation = new ColorAnimation
{
From = brush.Color,
To = value,
Duration = new Duration(TimeSpan.FromMilliseconds(300))
};
brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
return;
}
}
var newBrush = new SolidColorBrush(value);
newBrush.Freeze();
sourceDictionary[name] = newBrush;
}
private class ThemeManager : IThemeManager
{
private ResourceDictionary _ResourceDictionary;
public ThemeManager(ResourceDictionary resourceDictionary)
{
_ResourceDictionary = resourceDictionary ?? throw new ArgumentNullException(nameof(resourceDictionary));
}
public event EventHandler<ThemeChangedEventArgs> ThemeChanged;
public void OnThemeChange(ITheme oldTheme, ITheme newTheme)
{
ThemeChanged?.Invoke(this, new ThemeChangedEventArgs(_ResourceDictionary, oldTheme, newTheme));
}
}
}
}