Skip to content

Commit

Permalink
Allow for setting Inherit for the base theme (#1920)
Browse files Browse the repository at this point in the history
Will retrieve the Windows Light/Dark theme and use that to apply the theme.
  • Loading branch information
Keboo committed Jun 14, 2020
1 parent 49c2124 commit dbe4dde
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
4 changes: 2 additions & 2 deletions MahMaterialDragablzMashUp/App.xaml
Expand Up @@ -5,14 +5,14 @@
xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"
xmlns:smtx="clr-namespace:ShowMeTheXAML;assembly=ShowMeTheXAML"
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
xmlns:smtxAe="clr-namespace:ShowMeTheXAML.AvalonEdit;assembly=ShowMeTheXAML.AvalonEdit"
xmlns:smtxAe="clr-namespace:ShowMeTheXAML.AvalonEdit;assembly=ShowMeTheXAML.AvalonEdit"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:mahDemo="clr-namespace:MahAppsDragablzDemo"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:MahAppsBundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Purple"/>
<materialDesign:MahAppsBundledTheme BaseTheme="Inherit" PrimaryColor="DeepPurple" SecondaryColor="Purple"/>

<!-- MahApps -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
Expand Down
13 changes: 7 additions & 6 deletions MainDemo.Wpf/App.xaml
Expand Up @@ -12,16 +12,17 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- This is the current way to setup your app's initial theme -->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />

<!-- However, however you can make it simpler by just using one of the built-in themes. This is functionally identical to what is above.-->
<!--<materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" />-->
<materialDesign:BundledTheme BaseTheme="Inherit" PrimaryColor="DeepPurple" SecondaryColor="Lime" />

<!-- If you would prefer to use your own colors there is an option for that as well -->
<!--<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="Aqua" SecondaryColor="DarkGreen" />-->

<!-- You can also use the built in theme dictionaries as well
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
-->

<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />

<ResourceDictionary Source="pack://application:,,,/ShowMeTheXAML.AvalonEdit;component/Themes/xamldisplayer.xaml" />
Expand Down
6 changes: 6 additions & 0 deletions MaterialDesignThemes.MahApps/BaseThemeExtensions.cs
@@ -1,6 +1,7 @@
using System;
using ControlzEx.Theming;
using MaterialDesignThemes.Wpf;
using Theme = MaterialDesignThemes.Wpf.Theme;

namespace MaterialDesignThemes.MahApps
{
Expand All @@ -12,6 +13,11 @@ public static string GetMahAppsBaseColorScheme(this BaseTheme baseTheme)
{
BaseTheme.Light => ThemeManager.BaseColorLightConst,
BaseTheme.Dark => ThemeManager.BaseColorDarkConst,
BaseTheme.Inherit => Theme.GetSystemTheme() switch
{
BaseTheme.Dark => ThemeManager.BaseColorDarkConst,
_ => ThemeManager.BaseColorLightConst
},
_ => throw new InvalidOperationException()
};
}
Expand Down
26 changes: 26 additions & 0 deletions MaterialDesignThemes.Wpf/Theme.cs
@@ -1,6 +1,7 @@
using System;
using System.Windows.Media;
using MaterialDesignColors;
using Microsoft.Win32;

namespace MaterialDesignThemes.Wpf
{
Expand All @@ -9,6 +10,31 @@ public class Theme : ITheme
public static IBaseTheme Light { get; } = new MaterialDesignLightTheme();
public static IBaseTheme Dark { get; } = new MaterialDesignDarkTheme();

/// <summary>
/// Get the current Windows theme.
/// Based on ControlzEx
/// https://github.com/ControlzEx/ControlzEx/blob/48230bb023c588e1b7eb86ea83f7ddf7d25be735/src/ControlzEx/Theming/WindowsThemeHelper.cs#L19
/// </summary>
/// <returns></returns>
public static BaseTheme? GetSystemTheme()
{
try
{
var registryValue = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", null);

if (registryValue is null)
{
return null;
}

return Convert.ToBoolean(registryValue) ? BaseTheme.Light : BaseTheme.Dark;
}
catch (Exception)
{
return null;
}
}

public static Theme Create(IBaseTheme baseTheme, Color primary, Color accent)
{
if (baseTheme == null) throw new ArgumentNullException(nameof(baseTheme));
Expand Down
7 changes: 7 additions & 0 deletions MaterialDesignThemes.Wpf/ThemeExtensions.cs
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Media;
using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation;
using Microsoft.Win32;

namespace MaterialDesignThemes.Wpf
{
Expand Down Expand Up @@ -33,6 +35,11 @@ public static IBaseTheme GetBaseTheme(this BaseTheme baseTheme)
{
case BaseTheme.Dark: return Theme.Dark;
case BaseTheme.Light: return Theme.Light;
case BaseTheme.Inherit: return Theme.GetSystemTheme() switch
{
BaseTheme.Dark => Theme.Dark,
_ => Theme.Light
};
default: throw new InvalidOperationException();
}
}
Expand Down

0 comments on commit dbe4dde

Please sign in to comment.