Skip to content

Commit 8d12066

Browse files
committed
Change classes to use new swatches
1 parent dc429b0 commit 8d12066

File tree

3 files changed

+110
-81
lines changed

3 files changed

+110
-81
lines changed

MahMaterialDragablzMashUp/PaletteSelectorViewModel.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
using System;
1+
using MaterialDesignColors;
2+
using MaterialDesignThemes.Wpf;
23
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Windows;
74
using System.Windows.Input;
8-
using MahApps.Metro;
9-
using MaterialDesignColors;
10-
using MaterialDesignThemes.Wpf;
115

126
namespace MahMaterialDragablzMashUp
137
{
@@ -31,7 +25,7 @@ private static void ApplyBase(bool isDark)
3125

3226
private static void ApplyPrimary(Swatch swatch)
3327
{
34-
new PaletteHelper().ReplacePrimaryColor(swatch);
28+
new PaletteHelper().ReplacePrimaryColor(swatch, true);
3529
}
3630

3731
public ICommand ApplyAccentCommand { get; } = new AnotherCommandImplementation(o => ApplyAccent((Swatch)o));

MaterialDesignColors.Wpf/SwatchesProvider.cs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Globalization;
5-
using System.IO;
65
using System.Linq;
76
using System.Reflection;
87
using System.Resources;
@@ -23,46 +22,71 @@ public SwatchesProvider()
2322
var dictionaryEntries = resourceSet.OfType<DictionaryEntry>().ToList();
2423
var assemblyName = assembly.GetName().Name;
2524

26-
var regex = new Regex(@"^themes\/materialdesigncolor\.(?<name>[a-z]+)\.baml$");
25+
var regex = new Regex(@"^themes\/materialdesigncolor\.(?<name>[a-z]+)\.(?<type>primary|accent)\.baml$");
26+
27+
Swatches =
28+
dictionaryEntries
29+
.Select(x => new { key = x.Key.ToString(), match = regex.Match(x.Key.ToString()) })
30+
.Where(x => x.match.Success && x.match.Groups["name"].Value != "black")
31+
.GroupBy(x => x.match.Groups["name"].Value)
32+
.Select(x =>
33+
CreateSwatch
34+
(
35+
x.Key,
36+
Read(assemblyName, x.SingleOrDefault(y => y.match.Groups["type"].Value == "primary")?.key),
37+
Read(assemblyName, x.SingleOrDefault(y => y.match.Groups["type"].Value == "accent")?.key)
38+
))
39+
.ToList();
2740

28-
Swatches = dictionaryEntries.Select(de =>
29-
new
30-
{
31-
key = de.Key.ToString(),
32-
match = regex.Match(de.Key.ToString()),
33-
}).Where(a => a.match.Success && a.match.Groups["name"].Value != "black")
34-
.Select(a => CreateSwatch(a.match.Groups["name"].Value, Read(assemblyName, a.key)))
35-
.OrderBy(s => s.Name)
36-
.ToList();
3741
}
3842

3943
public IEnumerable<Swatch> Swatches { get; }
4044

41-
private static Swatch CreateSwatch(string name, ResourceDictionary resourceDictionary)
45+
private static Swatch CreateSwatch(string name, ResourceDictionary primaryDictionary, ResourceDictionary accentDictionary)
4246
{
4347
var primaryHues = new List<Hue>();
4448
var accentHues = new List<Hue>();
4549

46-
foreach (var entry in resourceDictionary.OfType<DictionaryEntry>()
47-
.OrderBy(de => de.Key)
48-
.Where(de => !de.Key.ToString().EndsWith("Foreground")))
50+
if (primaryDictionary != null)
4951
{
50-
var targetList = (entry.Key.ToString().StartsWith("Primary") ? primaryHues : accentHues);
52+
foreach (var entry in primaryDictionary.OfType<DictionaryEntry>()
53+
.OrderBy(de => de.Key)
54+
.Where(de => !de.Key.ToString().EndsWith("Foreground", StringComparison.Ordinal)))
55+
{
56+
var colour = (Color)entry.Value;
57+
var foregroundColour = (Color)
58+
primaryDictionary.OfType<DictionaryEntry>()
59+
.Single(de => de.Key.ToString().Equals(entry.Key.ToString() + "Foreground"))
60+
.Value;
61+
62+
primaryHues.Add(new Hue(entry.Key.ToString(), colour, foregroundColour));
63+
}
64+
}
5165

52-
var colour = (Color) entry.Value;
53-
var foregroundColour = (Color)
54-
resourceDictionary.OfType<DictionaryEntry>()
55-
.Single(de => de.Key.ToString().Equals(entry.Key.ToString() + "Foreground"))
56-
.Value;
66+
if (accentDictionary != null)
67+
{
68+
foreach (var entry in accentDictionary.OfType<DictionaryEntry>()
69+
.OrderBy(de => de.Key)
70+
.Where(de => !de.Key.ToString().EndsWith("Foreground", StringComparison.Ordinal)))
71+
{
72+
var colour = (Color)entry.Value;
73+
var foregroundColour = (Color)
74+
accentDictionary.OfType<DictionaryEntry>()
75+
.Single(de => de.Key.ToString().Equals(entry.Key.ToString() + "Foreground"))
76+
.Value;
5777

58-
targetList.Add(new Hue(entry.Key.ToString(), colour, foregroundColour));
78+
accentHues.Add(new Hue(entry.Key.ToString(), colour, foregroundColour));
79+
}
5980
}
6081

6182
return new Swatch(name, primaryHues, accentHues);
6283
}
6384

6485
private static ResourceDictionary Read(string assemblyName, string path)
6586
{
87+
if (assemblyName == null || path == null)
88+
return null;
89+
6690
return (ResourceDictionary)Application.LoadComponent(new Uri(
6791
$"/{assemblyName};component/{path.Replace(".baml", ".xaml")}",
6892
UriKind.RelativeOrAbsolute));
Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
1+
using MaterialDesignColors;
2+
using System;
23
using System.Linq;
34
using System.Text.RegularExpressions;
45
using System.Windows;
56
using System.Windows.Media;
6-
using MaterialDesignColors;
77

88
namespace MaterialDesignThemes.Wpf
99
{
@@ -20,7 +20,7 @@ public void SetLightDark(bool isDark)
2020
var source =
2121
$"pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.{(isDark ? "Dark" : "Light")}.xaml";
2222
var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
23-
23+
2424
Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
2525
Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
2626

@@ -37,52 +37,46 @@ public void SetLightDark(bool isDark)
3737
Application.Current.Resources.MergedDictionaries.Add(newMahAppsResourceDictionary);
3838
}
3939

40-
public void ReplacePrimaryColor(Swatch swatch)
40+
public void ReplacePrimaryColor(Swatch swatch, bool mahapps = false)
4141
{
4242
if (swatch == null) throw new ArgumentNullException(nameof(swatch));
4343

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-
4844
var list = swatch.PrimaryHues.ToList();
4945
var light = list[2];
5046
var mid = swatch.ExemplarHue;
5147
var dark = list[7];
5248

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)
6650
{
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);
7953
}
8054

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+
}
8377
}
8478

85-
public void ReplacePrimaryColor(string name)
79+
public void ReplacePrimaryColor(string name, bool mahapps = false)
8680
{
8781
if (name == null) throw new ArgumentNullException(nameof(name));
8882

@@ -92,25 +86,21 @@ public void ReplacePrimaryColor(string name)
9286
if (swatch == null)
9387
throw new ArgumentException($"No such swatch '{name}'", nameof(name));
9488

95-
ReplacePrimaryColor(swatch);
89+
ReplacePrimaryColor(swatch, mahapps);
9690
}
9791

9892
public void ReplaceAccentColor(Swatch swatch)
9993
{
10094
if (swatch == null) throw new ArgumentNullException(nameof(swatch));
10195

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)
10797
{
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+
}
111101

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));
114104
}
115105

116106
public void ReplaceAccentColor(string name)
@@ -126,10 +116,31 @@ public void ReplaceAccentColor(string name)
126116
ReplaceAccentColor(swatch);
127117
}
128118

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)
130127
{
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;
133144
}
134145
}
135146
}

0 commit comments

Comments
 (0)