Skip to content

Commit

Permalink
Added the dark mode and highcontrast mode detection when starting the…
Browse files Browse the repository at this point in the history
… app (might be useful to add a watcher to react to system settings updates). #141
  • Loading branch information
wokhan committed Dec 24, 2022
1 parent 8dca13e commit 41d538f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
24 changes: 24 additions & 0 deletions Common/UI/Themes/ThemeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Win32;

using System;
using System.Linq;
using System.Windows;

namespace Wokhan.WindowsFirewallNotifier.Common.UI.Themes
{
public static partial class ThemeHelper
{
public static string GetCurrentTheme()
{
if (SystemParameters.HighContrast)
{
return "System";
}

using (RegistryKey? key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"))
{
return (int?)key?.GetValue("AppsUseLightTheme") == 0 ? "Dark" : "Light";
}
}
}
}
4 changes: 3 additions & 1 deletion Console/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Wokhan.WindowsFirewallNotifier.Common.Helpers;
using Wokhan.WindowsFirewallNotifier.Common.Logging;
using Wokhan.WindowsFirewallNotifier.Common.Processes;
using Wokhan.WindowsFirewallNotifier.Common.UI.Themes;

namespace Wokhan.WindowsFirewallNotifier.Console
{
Expand Down Expand Up @@ -66,7 +67,8 @@ private void Application_Startup(object sender, StartupEventArgs e)
Resources["ConsoleSizeHeight"] = Settings.Default.ConsoleSizeHeight;
}

Resources.MergedDictionaries[0].Source = new Uri($"pack://application:,,,/Wokhan.WindowsFirewallNotifier.Common;component/UI/Themes/{Settings.Default.Theme}.xaml");
var theme = ThemeHelper.GetCurrentTheme();
Resources.MergedDictionaries[0].Source = new Uri($"pack://application:,,,/Wokhan.WindowsFirewallNotifier.Common;component/UI/Themes/{theme}.xaml");
}

internal void RestartAsAdmin()
Expand Down
4 changes: 3 additions & 1 deletion Notifier/UI/Windows/NotificationWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Wokhan.WindowsFirewallNotifier.Common.Config;
using Wokhan.WindowsFirewallNotifier.Common.Processes;
using Wokhan.WindowsFirewallNotifier.Common.Logging;
using Wokhan.WindowsFirewallNotifier.Common.UI.Themes;

namespace Wokhan.WindowsFirewallNotifier.Notifier.UI.Windows
{
Expand Down Expand Up @@ -77,7 +78,8 @@ public NotificationWindow()
{
InitializeComponent();

this.Resources.MergedDictionaries[0].Source = new Uri($"pack://application:,,,/Wokhan.WindowsFirewallNotifier.Common;component/UI/Themes/{Settings.Default.Theme}.xaml");
var theme = ThemeHelper.GetCurrentTheme();
this.Resources.MergedDictionaries[0].Source = new Uri($"pack://application:,,,/Wokhan.WindowsFirewallNotifier.Common;component/UI/Themes/{theme}.xaml");

notifierTrayIcon = NotifierTrayIcon.Init(this);

Expand Down

0 comments on commit 41d538f

Please sign in to comment.