Skip to content

Commit b69e30c

Browse files
authored
Fix MahApp themeing changes. (#1489)
1 parent 63af4bf commit b69e30c

File tree

17 files changed

+457
-228
lines changed

17 files changed

+457
-228
lines changed

MahMaterialDragablzMashUp/App.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
5-
xmlns:mdMahApps="clr-namespace:MaterialDesignThemes.MahApps;assembly=MaterialDesignThemes.MahApps"
65
xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"
76
StartupUri="MainWindow.xaml">
87
<Application.Resources>

MainDemo.Wpf/MaterialDesignDemo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</PropertyGroup>
2626
<PropertyGroup>
2727
<ApplicationIcon>favicon.ico</ApplicationIcon>
28+
<LangVersion>8.0</LangVersion>
2829
</PropertyGroup>
2930
<Import Project="..\packages\ShowMeTheXAML.MSBuild\build\ShowMeTheXAML.MSBuild.targets" Label="Paket" />
3031
<Target Name="ShowMeTheXAML_EnsureXamlCreated" AfterTargets="BeforeCompile">

MaterialDesignColors.Wpf.Tests/MaterialDesignColors.Wpf.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<AssemblyTitle>MaterialDesignColors.Wpf.Fixture</AssemblyTitle>
99
<Product>MaterialDesignColors.Wpf.Fixture</Product>
1010
<Copyright>Copyright © 2015</Copyright>
11+
<LangVersion>8.0</LangVersion>
1112
</PropertyGroup>
1213
<Import Project="..\packages\xunit.core\build\xunit.core.props" Condition="Exists('..\packages\xunit.core\build\xunit.core.props')" Label="Paket" />
1314
<Import Project="..\packages\xunit.runner.visualstudio\build\$(__paket__xunit_runner_visualstudio_props).props" Condition="Exists('..\packages\xunit.runner.visualstudio\build\$(__paket__xunit_runner_visualstudio_props).props')" Label="Paket" />

MaterialDesignColors.Wpf/MaterialDesignColors.Wpf.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<AssemblyVersion>1.0.1</AssemblyVersion>
1717
<ExtrasEnableWpfProjectSetup>true</ExtrasEnableWpfProjectSetup>
1818
<Configurations>Debug;Release;AppVeyor</Configurations>
19+
<LangVersion>8.0</LangVersion>
1920
</PropertyGroup>
2021
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'AppVeyor|AnyCPU'">
2122
<OutputPath>bin\AppVeyor\</OutputPath>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup Condition="'$(TargetFramework)'=='net472'">
10+
<Reference Include="PresentationCore" />
11+
<Reference Include="PresentationFramework" />
12+
<Reference Include="System" />
13+
<Reference Include="System.Core" />
14+
<Reference Include="System.Xaml" />
15+
<Reference Include="System.Xml.Linq" />
16+
<Reference Include="System.Data.DataSetExtensions" />
17+
<Reference Include="Microsoft.CSharp" />
18+
<Reference Include="System.Data" />
19+
<Reference Include="System.Xml" />
20+
</ItemGroup>
21+
22+
<Import Project="..\packages\xunit.core\build\xunit.core.props" Condition="Exists('..\packages\xunit.core\build\xunit.core.props')" Label="Paket" />
23+
<Import Project="..\packages\xunit.runner.visualstudio\build\$(__paket__xunit_runner_visualstudio_props).props" Condition="Exists('..\packages\xunit.runner.visualstudio\build\$(__paket__xunit_runner_visualstudio_props).props')" Label="Paket" />
24+
<Import Project="..\packages\xunit.core\build\xunit.core.targets" Condition="Exists('..\packages\xunit.core\build\xunit.core.targets')" Label="Paket" />
25+
26+
<Import Project="..\.paket\Paket.Restore.targets" />
27+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace MaterialDesignThemes.MahApps.Tests
4+
{
5+
[TestClass]
6+
public class UnitTest1
7+
{
8+
[TestMethod]
9+
public void TestMethod1()
10+
{
11+
}
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Shouldly
2+
xunit
3+
xunit.runner.visualstudio
4+
Xunit.StaFact
5+
System.Data.DataSetExtensions
6+
Microsoft.CSharp
7+
System.Net.Http
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using MahApps.Metro;
3+
using MaterialDesignThemes.Wpf;
4+
5+
namespace MaterialDesignThemes.MahApps
6+
{
7+
internal static class BaseThemeExtensions
8+
{
9+
public static string GetMahAppsBaseColorScheme(this BaseTheme baseTheme)
10+
{
11+
return baseTheme switch
12+
{
13+
Wpf.BaseTheme.Light => ThemeManager.BaseColorLight,
14+
Wpf.BaseTheme.Dark => ThemeManager.BaseColorDark,
15+
_ => throw new InvalidOperationException()
16+
};
17+
}
18+
}
19+
}
Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
using System.Windows;
1+
using System;
2+
using System.Linq;
3+
using System.Windows;
24
using System.Windows.Media;
5+
using MahApps.Metro;
6+
using MaterialDesignColors;
37
using MaterialDesignColors.ColorManipulation;
48
using MaterialDesignThemes.Wpf;
59

610
namespace MaterialDesignThemes.MahApps
711
{
812
public class MahAppsBundledTheme : BundledTheme
913
{
14+
private static Guid GeneratedKey { get; } = Guid.NewGuid();
15+
1016
protected override void ApplyTheme(ITheme theme)
1117
{
1218
base.ApplyTheme(theme);
13-
if (BaseTheme is BaseTheme baseTheme)
19+
if (TryGetResourceDictionaries(theme, out ResourceDictionary light, out ResourceDictionary dark))
1420
{
15-
this.SetMahApps(theme, baseTheme);
21+
switch (BaseTheme)
22+
{
23+
case Wpf.BaseTheme.Light:
24+
MergedDictionaries.Add(light);
25+
break;
26+
case Wpf.BaseTheme.Dark:
27+
MergedDictionaries.Add(dark);
28+
break;
29+
}
30+
1631
IThemeManager themeManager = this.GetThemeManager();
1732
if (themeManager != null)
1833
{
@@ -21,16 +36,79 @@ protected override void ApplyTheme(ITheme theme)
2136
}
2237
}
2338

24-
private static void ThemeManagerOnThemeChanged(object sender, ThemeChangedEventArgs e)
39+
private bool TryGetResourceDictionaries(ITheme theme, out ResourceDictionary light, out ResourceDictionary dark)
40+
{
41+
if (PrimaryColor is PrimaryColor primaryColor &&
42+
SecondaryColor is SecondaryColor secondaryColor &&
43+
BaseTheme is BaseTheme baseTheme)
44+
{
45+
light = GetResourceDictionary(theme, primaryColor, secondaryColor, Wpf.BaseTheme.Light);
46+
dark = GetResourceDictionary(theme, primaryColor, secondaryColor, Wpf.BaseTheme.Dark);
47+
return true;
48+
}
49+
else
50+
{
51+
light = null;
52+
dark = null;
53+
return false;
54+
}
55+
56+
static ResourceDictionary GetResourceDictionary(ITheme theme, PrimaryColor primaryColor, SecondaryColor secondaryColor, BaseTheme baseTheme)
57+
{
58+
string baseColorScheme = baseTheme.GetMahAppsBaseColorScheme();
59+
string colorScheme = $"MaterialDesign.{primaryColor}.{secondaryColor}";
60+
61+
ResourceDictionary rv;
62+
if (ThemeManager.Themes.FirstOrDefault(x => x.BaseColorScheme == baseColorScheme && x.ColorScheme == colorScheme) is global::MahApps.Metro.Theme mahAppsTheme)
63+
{
64+
rv = mahAppsTheme.Resources;
65+
rv.SetMahApps(theme, baseTheme);
66+
return rv;
67+
}
68+
69+
rv = new ResourceDictionary();
70+
rv[GeneratedKey] = GeneratedKey;
71+
rv.SetMahApps(theme, baseTheme);
72+
73+
rv["Theme.Name"] = $"MaterialDesign.{baseColorScheme}.{primaryColor}.{secondaryColor}";
74+
rv["Theme.DisplayName"] = $"Material Design {primaryColor} with {secondaryColor}";
75+
rv["Theme.ColorScheme"] = colorScheme;
76+
ThemeManager.AddTheme(rv);
77+
78+
return rv;
79+
}
80+
}
81+
82+
private void ThemeManagerOnThemeChanged(object sender, ThemeChangedEventArgs e)
2583
{
2684
ResourceDictionary resourceDictionary = e.ResourceDictionary;
2785

2886
ITheme newTheme = e.NewTheme;
29-
87+
3088
var foreground = newTheme.Background.ContrastingForegroundColor();
3189

3290
BaseTheme baseTheme = foreground == Colors.Black ? Wpf.BaseTheme.Light : Wpf.BaseTheme.Dark;
33-
resourceDictionary.SetMahApps(newTheme, baseTheme);
91+
92+
if (TryGetResourceDictionaries(newTheme, out ResourceDictionary light, out ResourceDictionary dark))
93+
{
94+
for (int i = resourceDictionary.MergedDictionaries.Count - 1; i >= 0; i--)
95+
{
96+
var dictionary = resourceDictionary.MergedDictionaries[i];
97+
if (dictionary.Keys.Cast<object>().OfType<Guid>().Any(x => x == GeneratedKey))
98+
{
99+
resourceDictionary.MergedDictionaries.RemoveAt(i);
100+
}
101+
}
102+
switch (baseTheme)
103+
{
104+
case Wpf.BaseTheme.Light:
105+
resourceDictionary.MergedDictionaries.Add(light);
106+
break;
107+
case Wpf.BaseTheme.Dark:
108+
resourceDictionary.MergedDictionaries.Add(dark);
109+
break;
110+
}
111+
}
34112
}
35113
}
36114
}
Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
using System.Windows;
1+
using System;
2+
using System.Linq;
3+
using System.Windows;
24
using System.Windows.Media;
5+
using MahApps.Metro;
36
using MaterialDesignColors.ColorManipulation;
47
using MaterialDesignThemes.Wpf;
58

69
namespace MaterialDesignThemes.MahApps
710
{
811
public class MahAppsCustomColorTheme : CustomColorTheme
912
{
13+
private static Guid GeneratedKey { get; } = Guid.NewGuid();
14+
1015
protected override void ApplyTheme(ITheme theme)
1116
{
1217
base.ApplyTheme(theme);
13-
if (BaseTheme is BaseTheme baseTheme)
18+
if (TryGetResourceDictionaries(theme, out ResourceDictionary light, out ResourceDictionary dark))
1419
{
15-
this.SetMahApps(theme, baseTheme);
20+
switch (BaseTheme)
21+
{
22+
case Wpf.BaseTheme.Light:
23+
MergedDictionaries.Add(light);
24+
break;
25+
case Wpf.BaseTheme.Dark:
26+
MergedDictionaries.Add(dark);
27+
break;
28+
}
29+
1630
IThemeManager themeManager = this.GetThemeManager();
1731
if (themeManager != null)
1832
{
@@ -21,7 +35,50 @@ protected override void ApplyTheme(ITheme theme)
2135
}
2236
}
2337

24-
private static void ThemeManagerOnThemeChanged(object sender, ThemeChangedEventArgs e)
38+
private bool TryGetResourceDictionaries(ITheme theme, out ResourceDictionary light, out ResourceDictionary dark)
39+
{
40+
if (PrimaryColor is Color primaryColor &&
41+
SecondaryColor is Color secondaryColor &&
42+
BaseTheme is BaseTheme baseTheme)
43+
{
44+
light = GetResourceDictionary(theme, primaryColor, secondaryColor, Wpf.BaseTheme.Light);
45+
dark = GetResourceDictionary(theme, primaryColor, secondaryColor, Wpf.BaseTheme.Dark);
46+
return true;
47+
}
48+
else
49+
{
50+
light = null;
51+
dark = null;
52+
return false;
53+
}
54+
55+
static ResourceDictionary GetResourceDictionary(ITheme theme, Color primaryColor, Color secondaryColor, BaseTheme baseTheme)
56+
{
57+
string baseColorScheme = baseTheme.GetMahAppsBaseColorScheme();
58+
string colorScheme = $"MaterialDesign.{primaryColor}.{secondaryColor}";
59+
60+
ResourceDictionary rv;
61+
if (ThemeManager.Themes.FirstOrDefault(x => x.BaseColorScheme == baseColorScheme && x.ColorScheme == colorScheme) is global::MahApps.Metro.Theme mahAppsTheme)
62+
{
63+
rv = mahAppsTheme.Resources;
64+
rv.SetMahApps(theme, baseTheme);
65+
return rv;
66+
}
67+
68+
rv = new ResourceDictionary();
69+
rv[GeneratedKey] = GeneratedKey;
70+
rv.SetMahApps(theme, baseTheme);
71+
72+
rv["Theme.Name"] = $"MaterialDesign.{baseColorScheme}.{primaryColor}.{secondaryColor}";
73+
rv["Theme.DisplayName"] = $"Material Design {primaryColor} with {secondaryColor}";
74+
rv["Theme.ColorScheme"] = colorScheme;
75+
ThemeManager.AddTheme(rv);
76+
77+
return rv;
78+
}
79+
}
80+
81+
private void ThemeManagerOnThemeChanged(object sender, ThemeChangedEventArgs e)
2582
{
2683
ResourceDictionary resourceDictionary = e.ResourceDictionary;
2784

@@ -30,7 +87,27 @@ private static void ThemeManagerOnThemeChanged(object sender, ThemeChangedEventA
3087
var foreground = newTheme.Background.ContrastingForegroundColor();
3188

3289
BaseTheme baseTheme = foreground == Colors.Black ? Wpf.BaseTheme.Light : Wpf.BaseTheme.Dark;
33-
resourceDictionary.SetMahApps(newTheme, baseTheme);
90+
91+
if (TryGetResourceDictionaries(newTheme, out ResourceDictionary light, out ResourceDictionary dark))
92+
{
93+
for (int i = resourceDictionary.MergedDictionaries.Count - 1; i >= 0; i--)
94+
{
95+
var dictionary = resourceDictionary.MergedDictionaries[i];
96+
if (dictionary.Keys.Cast<object>().OfType<Guid>().Any(x => x == GeneratedKey))
97+
{
98+
resourceDictionary.MergedDictionaries.RemoveAt(i);
99+
}
100+
}
101+
switch (baseTheme)
102+
{
103+
case Wpf.BaseTheme.Light:
104+
resourceDictionary.MergedDictionaries.Add(light);
105+
break;
106+
case Wpf.BaseTheme.Dark:
107+
resourceDictionary.MergedDictionaries.Add(dark);
108+
break;
109+
}
110+
}
34111
}
35112
}
36113
}

0 commit comments

Comments
 (0)