-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathThemeManager.cs
More file actions
144 lines (130 loc) · 5.49 KB
/
Copy pathThemeManager.cs
File metadata and controls
144 lines (130 loc) · 5.49 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
using Trimble.Modus.Components.Hosting;
using Trimble.Modus.Components.Styles;
namespace Trimble.Modus.Components.Helpers
{
public class ThemeManager
{
private static ResourceDictionary LightThemeColorResourceDictionary { get; set; }
private static ResourceDictionary DarkThemeColorResourceDictionary { get; set; }
private static ResourceDictionary DarkThemeStylingResourceDictionary { get; set; }
private static ResourceDictionary LightThemeStylingResourceDictionary { get; set; }
internal static AppTheme CurrentTheme { get; private set; }
public static void Initialize(ModusConfig appConfig = null)
{
IncludeFromResources(new Styles.Colors());
UpdateColorsDictionary(appConfig);
UpdateStylingDictionary(appConfig);
UpdateTheme(Application.Current.RequestedTheme);
RegisterStyles();
Application.Current.RequestedThemeChanged += Current_RequestedThemeChanged;
}
public static void RegisterStyles()
{
Application.Current.Resources.MergedDictionaries.Add(new ModusStyles());
Application.Current.Resources.MergedDictionaries.Add(new ConvertersDictionary());
}
private static void UpdateColorsDictionary(ModusConfig appConfig = null)
{
var defaultLightThemeColors = new Styles.LightThemeColors();
var defaultDarkThemeColors = new Styles.DarkThemeColors();
if (appConfig != null)
{
if (appConfig.LightThemeColors != null)
{
LightThemeColorResourceDictionary = UpdateDictionary(appConfig.LightThemeColors, defaultLightThemeColors);
}
else
{
LightThemeColorResourceDictionary = defaultLightThemeColors;
}
if (appConfig.DarkThemeColors != null)
{
DarkThemeColorResourceDictionary = UpdateDictionary(appConfig.DarkThemeColors, defaultDarkThemeColors);
}
else
{
DarkThemeColorResourceDictionary = defaultDarkThemeColors;
}
}
else
{
LightThemeColorResourceDictionary = defaultLightThemeColors;
DarkThemeColorResourceDictionary = defaultDarkThemeColors;
}
}
private static void UpdateStylingDictionary(ModusConfig appConfig = null)
{
if (appConfig != null)
{
LightThemeStylingResourceDictionary = appConfig.LightThemeStyles;
DarkThemeStylingResourceDictionary = appConfig.DarkThemeStyles;
}
}
private static ResourceDictionary UpdateDictionary(ResourceDictionary keyValuePairs, ResourceDictionary defaultKeyValuePairs)
{
if (keyValuePairs != null && keyValuePairs.Count > 0)
{
return UpdateDefaulThemeWithCustomTheme(defaultKeyValuePairs, keyValuePairs);
}
else
{
return defaultKeyValuePairs;
}
}
private static ResourceDictionary UpdateDefaulThemeWithCustomTheme(ResourceDictionary defaultTheme, ResourceDictionary customTheme)
{
var theme = customTheme;
var notExistsInDictionary = defaultTheme.Where(x => !customTheme.ContainsKey(x.Key)).ToDictionary(x => x.Key, x => x.Value);
foreach (var item in notExistsInDictionary)
{
var keyExists = theme.ContainsKey(item.Key);
if (keyExists)
{
theme[item.Key] = item.Value;
}
else
{
theme.Add(item.Key, item.Value);
}
}
return theme;
}
private static void Current_RequestedThemeChanged(object sender, AppThemeChangedEventArgs e)
{
UpdateTheme(e.RequestedTheme);
}
private static void UpdateTheme(AppTheme theme = AppTheme.Light)
{
CurrentTheme = Application.Current.RequestedTheme;
if (theme == AppTheme.Dark)
{
IncludeFromResources(DarkThemeColorResourceDictionary);
IncludeFromResources(new Styles.DarkThemeStyling());
IncludeFromResources(DarkThemeStylingResourceDictionary);
}
else
{
IncludeFromResources(LightThemeColorResourceDictionary);
IncludeFromResources(new Styles.LightThemeStyling());
IncludeFromResources(LightThemeStylingResourceDictionary);
}
}
/// <summary>
/// Includes the given resource dictionary with the Application.Current.Resources
/// </summary>
/// <param name="keyValuePairs"></param>
/// <param name="needToAdd"></param>
private static void IncludeFromResources(ResourceDictionary keyValuePairs)
{
if (keyValuePairs != null)
{
var dictionaryExists = Application.Current.Resources.MergedDictionaries.Contains(keyValuePairs);
if (dictionaryExists)
{
Application.Current.Resources.MergedDictionaries.Remove(keyValuePairs);
}
Application.Current.Resources.MergedDictionaries.Add(keyValuePairs);
}
}
}
}