Merge pull request #2 from Assistant/feature/themes-lolpants

Apply default theme based on Windows settings
This commit is contained in:
Jack Baron 2020-02-16 23:24:51 +00:00 committed by GitHub
commit b367c412a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 5 deletions

View file

@ -8,6 +8,7 @@ using ModAssistant.Pages;
using System.Xml;
using System.Windows.Markup;
using System.Reflection;
using Microsoft.Win32;
namespace ModAssistant
{
@ -63,8 +64,8 @@ namespace ModAssistant
/// Applies a loaded theme to ModAssistant.
/// </summary>
/// <param name="theme">Name of the theme.</param>
/// <param name="element">Page that this is called on (Used for refreshing button icon colors).</param>
public static void ApplyTheme(string theme)
/// <param name="sendMessage">Send message to MainText (default: true).</param>
public static void ApplyTheme(string theme, bool sendMessage = true)
{
if (loadedThemes.TryGetValue(theme, out ResourceDictionary newTheme))
{
@ -73,7 +74,10 @@ namespace ModAssistant
Application.Current.Resources.MergedDictionaries.Insert(0, newTheme);
Properties.Settings.Default.SelectedTheme = theme;
Properties.Settings.Default.Save();
MainWindow.Instance.MainText = $"Theme changed to {theme}.";
if (sendMessage)
{
MainWindow.Instance.MainText = $"Theme changed to {theme}.";
}
ReloadIcons();
}
else throw new ArgumentException($"{theme} does not exist.");
@ -104,6 +108,29 @@ namespace ModAssistant
else MessageBox.Show("Template theme already exists!");
}
/// <summary>
/// Finds the theme set on Windows and applies it.
/// </summary>
public static void ApplyWindowsTheme()
{
using (RegistryKey key = Registry.CurrentUser
.OpenSubKey("Software").OpenSubKey("Microsoft")
.OpenSubKey("Windows").OpenSubKey("CurrentVersion")
.OpenSubKey("Themes").OpenSubKey("Personalize"))
{
object registryValueObject = key?.GetValue("AppsUseLightTheme");
if (registryValueObject != null)
{
if ((int)registryValueObject <= 0)
{
ApplyTheme("Dark", false);
return;
}
}
ApplyTheme("Light", false);
}
}
/// <summary>
/// Loads a ResourceDictionary from either Embedded Resources or from a file location.
/// </summary>

View file

@ -63,11 +63,18 @@ namespace ModAssistant
Themes.LoadThemes();
try
{
Themes.ApplyTheme(Properties.Settings.Default.SelectedTheme);
if (Properties.Settings.Default.SelectedTheme != null)
{
Themes.ApplyTheme(Properties.Settings.Default.SelectedTheme, false);
}
else
{
Themes.ApplyWindowsTheme();
}
}
catch (ArgumentException)
{
Themes.ApplyTheme("Light");
Themes.ApplyTheme("Light", false);
MainText = "Theme not found, reverting to Light theme...";
}