Skip to content

Commit

Permalink
Simplify the usage of theme dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Apr 6, 2020
1 parent bc5d592 commit aabe3f8
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 64 deletions.
15 changes: 3 additions & 12 deletions ModernWpf.Controls/NumberBox/NumberBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,26 +284,17 @@
<Grid.Resources>
<ui:ResourceDictionaryEx>
<ui:ResourceDictionaryEx.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<ResourceDictionary.MergedDictionaries>
<ui:ApplicationThemeResources Key="Light" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Key="Light" ui:ThemeDictionary.Key="Light">
<ui:StaticResource x:Key="RepeatButtonBorderBrushPointerOver" ResourceKey="TextControlBorderBrush" />
<ui:StaticResource x:Key="RepeatButtonBorderBrushPressed" ResourceKey="TextControlBorderBrush" />
</ResourceDictionary>

<ResourceDictionary x:Key="Dark">
<ResourceDictionary.MergedDictionaries>
<ui:ApplicationThemeResources Key="Dark" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Key="Dark" ui:ThemeDictionary.Key="Dark">
<ui:StaticResource x:Key="RepeatButtonBorderBrushPointerOver" ResourceKey="TextControlBorderBrush" />
<ui:StaticResource x:Key="RepeatButtonBorderBrushPressed" ResourceKey="TextControlBorderBrush" />
</ResourceDictionary>

<ResourceDictionary x:Key="HighContrast">
<ResourceDictionary.MergedDictionaries>
<ui:ApplicationThemeResources Key="HighContrast" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Key="HighContrast" ui:ThemeDictionary.Key="HighContrast">
<ui:StaticResource x:Key="RepeatButtonBorderBrushPointerOver" ResourceKey="SystemControlHighlightBaseMediumLowBrush" />
<ui:StaticResource x:Key="RepeatButtonBorderBrushPressed" ResourceKey="SystemControlHighlightTransparentBrush" />
</ResourceDictionary>
Expand Down
15 changes: 3 additions & 12 deletions ModernWpf.MahApps/Styles/Controls.NumericUpDown.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,17 @@
<Grid.Resources>
<ui:ResourceDictionaryEx>
<ui:ResourceDictionaryEx.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<ResourceDictionary.MergedDictionaries>
<ui:ApplicationThemeResources Key="Light" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Key="Light" ui:ThemeDictionary.Key="Light">
<ui:StaticResource x:Key="RepeatButtonBorderBrushPointerOver" ResourceKey="TextControlBorderBrush" />
<ui:StaticResource x:Key="RepeatButtonBorderBrushPressed" ResourceKey="TextControlBorderBrush" />
</ResourceDictionary>

<ResourceDictionary x:Key="Dark">
<ResourceDictionary.MergedDictionaries>
<ui:ApplicationThemeResources Key="Dark" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Key="Dark" ui:ThemeDictionary.Key="Dark">
<ui:StaticResource x:Key="RepeatButtonBorderBrushPointerOver" ResourceKey="TextControlBorderBrush" />
<ui:StaticResource x:Key="RepeatButtonBorderBrushPressed" ResourceKey="TextControlBorderBrush" />
</ResourceDictionary>

<ResourceDictionary x:Key="HighContrast">
<ResourceDictionary.MergedDictionaries>
<ui:ApplicationThemeResources Key="HighContrast" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Key="HighContrast" ui:ThemeDictionary.Key="HighContrast">
<ui:StaticResource x:Key="RepeatButtonBorderBrushPointerOver" ResourceKey="SystemControlHighlightBaseMediumLowBrush" />
<ui:StaticResource x:Key="RepeatButtonBorderBrushPressed" ResourceKey="SystemControlHighlightTransparentBrush" />
</ResourceDictionary>
Expand Down
32 changes: 15 additions & 17 deletions ModernWpf.SampleApp/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,46 @@
<ResourceDictionary.MergedDictionaries>
<ui:ThemeResources>
<ui:ThemeResources.ThemeDictionaries>

<ResourceDictionary x:Key="Light">
<ResourceDictionary x:Key="Light" ui:ThemeDictionary.Key="Light">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:DefaultThemeResources Key="Light" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="RegionBrush" Color="{StaticResource SystemAltHighColor}" />
</ResourceDictionary>
<presets:PresetResources TargetTheme="Light" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="AppBackgroundBrush" Color="#FFF1F1F1" />
<SolidColorBrush x:Key="AppContentBackgroundBrush" Color="{StaticResource SystemAltHighColor}" />
<SolidColorBrush x:Key="CustomThemeBrush" Color="Red" />
</ResourceDictionary>

<ResourceDictionary x:Key="Dark">
<ResourceDictionary x:Key="Dark" ui:ThemeDictionary.Key="Dark">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:DefaultThemeResources Key="Dark" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="RegionBrush" Color="{StaticResource SystemAltHighColor}" />
</ResourceDictionary>
<presets:PresetResources TargetTheme="Dark" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="AppBackgroundBrush" Color="#FF232323" />
<SolidColorBrush x:Key="AppContentBackgroundBrush" Color="#FF282828" />
<SolidColorBrush x:Key="CustomThemeBrush" Color="Yellow" />
</ResourceDictionary>

<ResourceDictionary x:Key="HighContrast">
<ResourceDictionary x:Key="HighContrast" ui:ThemeDictionary.Key="HighContrast">
<SolidColorBrush x:Key="AppBackgroundBrush" Color="{Binding Path=(ui:BindableSystemColors.WindowColor)}" />
<SolidColorBrush x:Key="AppContentBackgroundBrush" Color="{Binding Path=(ui:BindableSystemColors.WindowColor)}" />
<SolidColorBrush x:Key="RegionBrush" Color="Transparent" />
<SolidColorBrush x:Key="CustomThemeBrush" Color="Blue" />
</ResourceDictionary>

</ui:ThemeResources.ThemeDictionaries>
</ui:ThemeResources>

<ui:XamlControlsResources />
<sc:UISettingsResources />

<!-- Font Overrides -->
<!--<sc:FontOverrides FontFamily="Comic Sans MS" />-->
<!--<ResourceDictionary>
<FontFamily x:Key="DefaultFontFamily">Comic Sans MS</FontFamily>
<StaticResource x:Key="{x:Static SystemFonts.MessageFontFamilyKey}" ResourceKey="DefaultFontFamily" />
<StaticResource x:Key="ContentControlThemeFontFamily" ResourceKey="DefaultFontFamily" />
<StaticResource x:Key="PivotHeaderItemFontFamily" ResourceKey="DefaultFontFamily" />
<StaticResource x:Key="PivotTitleFontFamily" ResourceKey="DefaultFontFamily" />
</ResourceDictionary>-->

</ResourceDictionary.MergedDictionaries>

<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
Expand Down
5 changes: 4 additions & 1 deletion ModernWpf.SampleApp/Presets/Default/Dark.xaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="RegionColor">#FF000000</Color>
<SolidColorBrush x:Key="RegionBrush" Color="{StaticResource RegionColor}" />
</ResourceDictionary>
5 changes: 4 additions & 1 deletion ModernWpf.SampleApp/Presets/Default/Light.xaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="RegionColor">#FFFFFFFF</Color>
<SolidColorBrush x:Key="RegionBrush" Color="{StaticResource RegionColor}" />
</ResourceDictionary>
6 changes: 5 additions & 1 deletion ModernWpf.SampleApp/Presets/PresetResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ private void OnCurrentPresetChanged(object sender, EventArgs e)

private void ApplyCurrentPreset()
{
MergedDictionaries.Clear();
if (MergedDictionaries.Count > 0)
{
MergedDictionaries.Clear();
}

string assemblyName = GetType().Assembly.GetName().Name;
string currentPreset = PresetManager.Current.CurrentPreset;
var source = new Uri($"pack://application:,,,/{assemblyName};component/Presets/{currentPreset}/{TargetTheme}.xaml");
Expand Down
7 changes: 5 additions & 2 deletions ModernWpf/ApplicationThemeResources.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Diagnostics;
using System.Windows;

namespace ModernWpf
{
[Obsolete]
public class ApplicationThemeResources : ResourceDictionary
{
private string _key;
Expand Down Expand Up @@ -32,7 +32,10 @@ public string Key

private void UpdateContent()
{
MergedDictionaries.Clear();
if (MergedDictionaries.Count > 0)
{
MergedDictionaries.Clear();
}

ResourceDictionary themeDictionary = null;
ThemeResources.Current?.ThemeDictionaries.TryGetValue(Key, out themeDictionary);
Expand Down
6 changes: 5 additions & 1 deletion ModernWpf/ColorPaletteResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,10 @@ private void UpdateBrushes()
return;
}

MergedDictionaries.Clear();
if (MergedDictionaries.Count > 0)
{
MergedDictionaries.Clear();
}

if (TargetTheme == null || Count == 0)
{
Expand All @@ -412,6 +415,7 @@ private void UpdateBrushes()
var overrides = new ResourceDictionary();
var originalsToOverrides = new Dictionary<SolidColorBrush, SolidColorBrush>();

// TODO: recursive
foreach (DictionaryEntry entry in originals)
{
if (entry.Value is SolidColorBrush originalBrush)
Expand Down
6 changes: 5 additions & 1 deletion ModernWpf/DefaultThemeResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace ModernWpf
{
[Obsolete]
public class DefaultThemeResources : ResourceDictionary
{
private string _key;
Expand Down Expand Up @@ -31,7 +32,10 @@ public string Key

private void UpdateContent()
{
MergedDictionaries.Clear();
if (MergedDictionaries.Count > 0)
{
MergedDictionaries.Clear();
}

ResourceDictionary defaultThemeDictionary = ThemeManager.GetDefaultThemeDictionary(Key);

Expand Down
1 change: 1 addition & 0 deletions ModernWpf/ModernWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<UseWPF>true</UseWPF>
<NeutralLanguage>en-US</NeutralLanguage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions ModernWpf/ThemeDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Windows;

namespace ModernWpf
{
public static class ThemeDictionary
{
public static void SetKey(ResourceDictionary themeDictionary, string key)
{
var baseThemeDictionary = GetBaseThemeDictionary(key);
themeDictionary.MergedDictionaries.Insert(0, baseThemeDictionary);
}

private static ResourceDictionary GetBaseThemeDictionary(string key)
{
ResourceDictionary themeDictionary = null;
ThemeResources.Current?.ThemeDictionaries.TryGetValue(key, out themeDictionary);
return themeDictionary ?? ThemeManager.GetDefaultThemeDictionary(key);
}
}
}
23 changes: 18 additions & 5 deletions ModernWpf/ThemeResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private ResourceDictionary GetThemeDictionary(string key)

if (ThemeDictionaries.TryGetValue(key, out ResourceDictionary themeDictionary))
{
if (!ContainsDefaultThemeResources(themeDictionary))
if (!ContainsDefaultThemeResources(themeDictionary, defaultThemeDictionary))
{
themeDictionary.MergedDictionaries.Insert(0, defaultThemeDictionary);
}
Expand All @@ -328,23 +328,36 @@ private ResourceDictionary GetThemeDictionary(string key)
return themeDictionary;
}

private static bool ContainsDefaultThemeResources(ResourceDictionary dictionary)
private static bool ContainsDefaultThemeResources(ResourceDictionary dictionary, ResourceDictionary defaultResources)
{
if (dictionary is DefaultThemeResources)
if (dictionary == defaultResources ||
dictionary is DefaultThemeResources ||
dictionary is ApplicationThemeResources ||
SourceEquals(dictionary.Source, defaultResources.Source))
{
return true;
}

foreach (var mergedDictionary in dictionary.MergedDictionaries)
{
if (mergedDictionary is DefaultThemeResources ||
mergedDictionary != null && ContainsDefaultThemeResources(mergedDictionary))
if (mergedDictionary != null && ContainsDefaultThemeResources(mergedDictionary, defaultResources))
{
return true;
}
}

return false;

static bool SourceEquals(Uri x, Uri y)
{
if (x == null || y == null)
return false;

string xString = x.IsAbsoluteUri ? x.LocalPath : x.ToString();
string yString = y.IsAbsoluteUri ? y.LocalPath : y.ToString();

return string.Equals(xString, yString, StringComparison.OrdinalIgnoreCase);
}
}
}
}
3 changes: 2 additions & 1 deletion Settings.XamlStyler
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
ui:VisualStateGroupListener,
ImageBrush,
ui:BitmapIcon,
Storyboard
Storyboard,
ResourceDictionary
",
"SeparateByGroups": false,
"AttributeIndentation": 0,
Expand Down
4 changes: 2 additions & 2 deletions samples/FluentWPFSample/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
<!-- 2. MahApps theme resources -->
<ResourceDictionary Source="/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
<!-- 3. ModernWPF theme resources -->
<ui:DefaultThemeResources Key="Light" />
<ResourceDictionary Source="/ModernWpf;component/ThemeResources/Light.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FluentWPF;component/Styles/Colors.Dark.xaml" />
<ResourceDictionary Source="/FluentWPF;component/Styles/Brushes.xaml" />
<ResourceDictionary Source="/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />
<ui:DefaultThemeResources Key="Dark" />
<ResourceDictionary Source="/ModernWpf;component/ThemeResources/Dark.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="NavigationViewSelectionIndicatorForeground" Color="{StaticResource SystemBaseHighColor}" />
</ResourceDictionary>
Expand Down
10 changes: 2 additions & 8 deletions samples/SamplesCommon/AccentColorPicker.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,13 @@
<ui:GridView.Resources>
<ui:ResourceDictionaryEx>
<ui:ResourceDictionaryEx.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<ResourceDictionary.MergedDictionaries>
<ui:ApplicationThemeResources Key="Light" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Key="Light" ui:ThemeDictionary.Key="Light">
<SolidColorBrush x:Key="SystemControlHighlightListLowBrush" Color="{StaticResource SystemBaseHighColor}" />
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="{StaticResource SystemBaseHighColor}" />
<SolidColorBrush x:Key="SystemControlHighlightListAccentMediumBrush" Color="{StaticResource SystemBaseHighColor}" />
<SolidColorBrush x:Key="SystemControlForegroundBaseMediumHighBrush" Color="{StaticResource SystemAltHighColor}" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<ResourceDictionary.MergedDictionaries>
<ui:ApplicationThemeResources Key="Dark" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Key="Dark" ui:ThemeDictionary.Key="Dark">
<SolidColorBrush x:Key="SystemControlHighlightListLowBrush" Color="{StaticResource SystemBaseHighColor}" />
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="{StaticResource SystemBaseHighColor}" />
<SolidColorBrush x:Key="SystemControlHighlightListAccentMediumBrush" Color="{StaticResource SystemBaseHighColor}" />
Expand Down
Loading

0 comments on commit aabe3f8

Please sign in to comment.