forked from Kinnara/ModernWpf
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMahAppsColorPaletteResources.cs
259 lines (220 loc) · 7.64 KB
/
MahAppsColorPaletteResources.cs
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace ModernWpf.MahApps
{
public class MahAppsColorPaletteResources : ResourceDictionary, ISupportInitialize
{
private const string ColorPrefix = "MahApps.Colors.";
private const string BrushPrefix = "MahApps.Brushes.";
private readonly BindingHelper _bindingHelper;
public MahAppsColorPaletteResources()
{
_bindingHelper = new BindingHelper();
_bindingHelper.AccentBaseChanged += OnAccentBaseChanged; ;
_bindingHelper.HighlightChanged += OnHighlightChanged;
}
private Color? _accentBase;
public Color? AccentBase
{
get => _accentBase;
set
{
if (SetAccentColor(ref _accentBase, value))
{
UpdateAccentShades();
UpdateBrush("Theme.ShowcaseBrush", value);
}
}
}
private BindingBase _accentBaseBinding;
public BindingBase AccentBaseBinding
{
get => _accentBaseBinding;
set
{
if (_accentBaseBinding != value)
{
_accentBaseBinding = value;
BindingOperations.SetBinding(_bindingHelper, BindingHelper.AccentBaseProperty, value);
}
}
}
private Color? _highlight;
public Color? Highlight
{
get => _highlight;
set => SetAccentColor(ref _highlight, value);
}
private BindingBase _highlightBinding;
public BindingBase HighlightBinding
{
get => _highlightBinding;
set
{
if (_highlightBinding != value)
{
_highlightBinding = value;
BindingOperations.SetBinding(_bindingHelper, BindingHelper.HighlightProperty, value);
}
}
}
private Color? _accent;
private Color? Accent
{
get => _accent;
set => SetAccentColor(ref _accent, value);
}
private Color? _accent2;
private Color? Accent2
{
get => _accent2;
set => SetAccentColor(ref _accent2, value);
}
private Color? _accent3;
private Color? Accent3
{
get => _accent3;
set => SetAccentColor(ref _accent3, value);
}
private Color? _accent4;
private Color? Accent4
{
get => _accent4;
set => SetAccentColor(ref _accent4, value);
}
private static Color GetAccentShade(Color accentBase, byte alpha)
{
accentBase.A = alpha;
return accentBase;
}
private void UpdateAccentShades()
{
if (AccentBase.HasValue)
{
var accentBase = AccentBase.Value;
Accent = GetAccentShade(accentBase, 0xCC);
Accent2 = GetAccentShade(accentBase, 0x99);
Accent3 = GetAccentShade(accentBase, 0x66);
Accent4 = GetAccentShade(accentBase, 0x33);
}
else
{
Accent = null;
Accent2 = null;
Accent3 = null;
Accent4 = null;
}
}
private void UpdateBrush(string key, Color? color)
{
if (color.HasValue)
{
this[key] = new SolidColorBrush(color.Value);
}
else
{
Remove(key);
}
}
private bool SetAccentColor(ref Color? storage, Color? value, [CallerMemberName]string propertyName = null)
{
if (storage != value)
{
string colorKey = ColorPrefix + propertyName;
string brushKey = BrushPrefix + propertyName;
if (storage.HasValue)
{
Remove(brushKey);
Remove(colorKey);
}
storage = value;
if (storage.HasValue)
{
Add(colorKey, storage.Value);
Add(brushKey, new SolidColorBrush(value.Value));
}
return true;
}
return false;
}
private void OnHighlightChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
Highlight = e.NewValue;
}
private void OnAccentBaseChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
if (_accentBaseBinding != null)
{
AccentBase = e.NewValue;
}
}
public new void EndInit()
{
base.EndInit();
if (AccentBase == null && AccentBaseBinding == null)
{
var defaultBinding = new Binding
{
Path = new PropertyPath(ThemeManager.ActualAccentColorProperty),
Source = ThemeManager.Current
};
AccentBaseBinding = defaultBinding;
HighlightBinding = defaultBinding;
}
}
void ISupportInitialize.EndInit()
{
EndInit();
}
private class BindingHelper : DependencyObject
{
#region Highlight
public static readonly DependencyProperty HighlightProperty =
DependencyProperty.Register(
nameof(Highlight),
typeof(Color),
typeof(BindingHelper),
new PropertyMetadata(OnHighlightChanged));
public Color Highlight
{
get => (Color)GetValue(HighlightProperty);
set => SetValue(HighlightProperty, value);
}
public event RoutedPropertyChangedEventHandler<Color> HighlightChanged;
private static void OnHighlightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((BindingHelper)d).OnHighlightChanged((Color)e.OldValue, (Color)e.NewValue);
}
private void OnHighlightChanged(Color oldHighlight, Color newHighlight)
{
HighlightChanged?.Invoke(this, new RoutedPropertyChangedEventArgs<Color>(oldHighlight, newHighlight));
}
#endregion
#region AccentBase
public static readonly DependencyProperty AccentBaseProperty =
DependencyProperty.Register(
nameof(AccentBase),
typeof(Color),
typeof(BindingHelper),
new PropertyMetadata(OnAccentBaseChanged));
public Color AccentBase
{
get => (Color)GetValue(AccentBaseProperty);
set => SetValue(AccentBaseProperty, value);
}
public event RoutedPropertyChangedEventHandler<Color> AccentBaseChanged;
private static void OnAccentBaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((BindingHelper)d).OnAccentBaseChanged((Color)e.OldValue, (Color)e.NewValue);
}
private void OnAccentBaseChanged(Color oldAccentBase, Color newAccentBase)
{
AccentBaseChanged?.Invoke(this, new RoutedPropertyChangedEventArgs<Color>(oldAccentBase, newAccentBase));
}
#endregion
}
}
}