From 6d281e4e4492f89c81131b84b5d0f0cf5957eeb2 Mon Sep 17 00:00:00 2001 From: Jakob Date: Fri, 14 Jul 2023 12:37:00 +0200 Subject: [PATCH] add dark mode --- EnvyUpdate/App.xaml | 1 - EnvyUpdate/App.xaml.cs | 9 +++++++++ EnvyUpdate/MainWindow.xaml.cs | 7 ++----- EnvyUpdate/Util.cs | 29 +++++++++++++++++++++++++---- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/EnvyUpdate/App.xaml b/EnvyUpdate/App.xaml index de53160..99d92b4 100644 --- a/EnvyUpdate/App.xaml +++ b/EnvyUpdate/App.xaml @@ -7,7 +7,6 @@ - diff --git a/EnvyUpdate/App.xaml.cs b/EnvyUpdate/App.xaml.cs index 3bd4437..92c551b 100644 --- a/EnvyUpdate/App.xaml.cs +++ b/EnvyUpdate/App.xaml.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Threading.Tasks; using System.Windows; using Windows.Foundation.Collections; +using Wpf.Ui.Markup; namespace EnvyUpdate { @@ -18,6 +19,14 @@ namespace EnvyUpdate { protected override void OnStartup(StartupEventArgs e) { + Wpf.Ui.Appearance.Accent.ApplySystemAccent(); + ThemesDictionary themedict = new ThemesDictionary(); + if (Util.IsDarkTheme()) + themedict.Theme = Wpf.Ui.Appearance.ThemeType.Dark; + else + themedict.Theme = Wpf.Ui.Appearance.ThemeType.Light; + Application.Current.Resources.MergedDictionaries.Add(themedict); + // Listen to notification activation ToastNotificationManagerCompat.OnActivated += toastArgs => { diff --git a/EnvyUpdate/MainWindow.xaml.cs b/EnvyUpdate/MainWindow.xaml.cs index 1d9eb37..1f6b74d 100644 --- a/EnvyUpdate/MainWindow.xaml.cs +++ b/EnvyUpdate/MainWindow.xaml.cs @@ -24,6 +24,8 @@ namespace EnvyUpdate public MainWindow() { + InitializeComponent(); + // Try to get command line arguments try { @@ -83,11 +85,6 @@ namespace EnvyUpdate GlobalVars.isMobile = Util.IsMobile(); Debug.LogToFile("INFO Mobile: " + GlobalVars.isMobile); - - InitializeComponent(); - Wpf.Ui.Appearance.Accent.ApplySystemAccent(); - - } private void Window_StateChanged(object sender, EventArgs e) { diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index c9fec1f..91e3124 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -6,13 +6,9 @@ using System.IO; using System.Linq; using System.Management; using System.Net; -using System.Security.Cryptography; using System.Text.RegularExpressions; -using System.Threading.Tasks; using System.Windows; using System.Xml.Linq; -using Windows.Devices.Radios; -using Windows.UI.Xaml.Input; namespace EnvyUpdate { @@ -552,6 +548,31 @@ namespace EnvyUpdate } } + public static bool IsDarkTheme() + { + try + { + Debug.LogToFile("INFO Trying to get app theme..."); + int res = (int)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", "AppsUseLightTheme", -1); + switch (res) + { + case 0: + Debug.LogToFile("INFO Using dark theme."); + return true; + case 1: + Debug.LogToFile("INFO Using light theme."); + return false; + default: + throw new IndexOutOfRangeException(); + } + } + catch (Exception) + { + Debug.LogToFile("WARN Could not determine theme. Setting light theme."); + return false; + } + } + public static void DownloadFile(string fileURL, string savePath) { //TODO Implement downloading