-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTheme.cs
246 lines (196 loc) · 5.8 KB
/
Theme.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Tools;
using HandyControl.Tools.Helper;
using HandyControl.Tools.Interop;
using Microsoft.Win32;
namespace HandyControl.Themes;
public class Theme : ResourceDictionary
{
public Theme()
{
if (DesignerHelper.IsInDesignMode)
{
MergedDictionaries.Add(ResourceHelper.GetSkin(SkinType.Default));
MergedDictionaries.Add(ResourceHelper.GetTheme());
}
else
{
InitResource();
}
}
#region Skin
private SkinType _manualSkinType;
private SkinType _skin;
public virtual SkinType Skin
{
get => _skin;
set
{
if (_skin == value) return;
_skin = value;
UpdateSkin();
}
}
public static readonly DependencyProperty SkinProperty = DependencyProperty.RegisterAttached(
"Skin", typeof(SkinType), typeof(Theme), new PropertyMetadata(default(SkinType), OnSkinChanged));
private static void OnSkinChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not FrameworkElement element)
{
return;
}
var skin = (SkinType) e.NewValue;
var themes = new List<Theme>();
GetAllThemes(element.Resources, ref themes);
if (themes.Count > 0)
{
foreach (var theme in themes)
{
theme.Skin = skin;
}
}
else
{
element.Resources.MergedDictionaries.Add(new Theme
{
Skin = skin
});
}
}
private static void GetAllThemes(ResourceDictionary resourceDictionary, ref List<Theme> themes)
{
if (resourceDictionary is Theme theme)
{
themes.Add(theme);
}
// we must consider it's MergedDictionaries
foreach (var dictionaryMergedDictionary in resourceDictionary.MergedDictionaries)
{
GetAllThemes(dictionaryMergedDictionary, ref themes);
}
}
public static void SetSkin(DependencyObject element, SkinType value)
=> element.SetValue(SkinProperty, value);
public static SkinType GetSkin(DependencyObject element)
=> (SkinType) element.GetValue(SkinProperty);
#endregion
#region SyncWithSystem
private bool _syncWithSystem;
public bool SyncWithSystem
{
get => _syncWithSystem;
set
{
_syncWithSystem = value;
if (value)
{
_manualSkinType = _skin;
SyncWithSystemTheme();
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
}
else
{
SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged;
_skin = _manualSkinType;
UpdateSkin();
}
}
}
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (e.Category == UserPreferenceCategory.General)
{
SyncWithSystemTheme();
}
}
private void SyncWithSystemTheme()
{
_skin = SystemHelper.DetermineIfInLightThemeMode() ? SkinType.Default : SkinType.Dark;
UpdateSkin();
}
#endregion
#region AccentColor
private Color? _accentColor;
public Color? AccentColor
{
get => _accentColor;
set
{
_accentColor = value;
if (value == null)
{
_precSkin = null;
}
UpdateSkin();
}
}
#endregion
#region Source
private Uri _source;
public new Uri Source
{
get => DesignerHelper.IsInDesignMode ? null : _source;
set => _source = value;
}
#endregion
public string Name { get; set; }
private SkinType _prevSkinType;
private ResourceDictionary _precSkin;
public virtual ResourceDictionary GetSkin(SkinType skinType)
{
if (_precSkin == null || _prevSkinType != skinType)
{
_precSkin = ResourceHelper.GetSkin(skinType);
_prevSkinType = skinType;
}
if (!SyncWithSystem)
{
if (AccentColor != null)
{
UpdateAccentColor(AccentColor.Value);
}
}
else
{
InteropMethods.DwmGetColorizationColor(out var color, out _);
UpdateAccentColor(ColorHelper.ToColor(color));
}
return _precSkin;
}
public static Theme GetTheme(string name, ResourceDictionary resourceDictionary)
{
if (string.IsNullOrEmpty(name) || resourceDictionary == null)
{
return null;
}
return resourceDictionary.MergedDictionaries.OfType<Theme>().FirstOrDefault(item => Equals(item.Name, name));
}
public virtual ResourceDictionary GetTheme() => ResourceHelper.GetTheme();
private void InitResource()
{
if (DesignerHelper.IsInDesignMode)
{
return;
}
MergedDictionaries.Clear();
MergedDictionaries.Add(GetSkin(Skin));
MergedDictionaries.Add(GetTheme());
}
private void UpdateAccentColor(Color color)
{
_precSkin[ResourceToken.PrimaryColor] = color;
_precSkin[ResourceToken.DarkPrimaryColor] = color;
_precSkin[ResourceToken.TitleColor] = color;
_precSkin[ResourceToken.SecondaryTitleColor] = color;
}
private void UpdateSkin() => MergedDictionaries[0] = GetSkin(Skin);
}
public class StandaloneTheme : Theme
{
public override ResourceDictionary GetTheme() => ResourceHelper.GetStandaloneTheme();
}