Tray icon now respects current system theme
This commit is contained in:
@@ -12,9 +12,6 @@ namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
private const int CallbackMessage = 0x400;
|
||||
private const uint IconVersion = 0x4;
|
||||
|
||||
private const int WM_LBUTTONUP = 0x0202;
|
||||
private const int WM_MBUTTONUP = 0x0208;
|
||||
private const int WM_RBUTTONUP = 0x0205;
|
||||
private readonly object _lock = new();
|
||||
private readonly IntPtr _windowHandle;
|
||||
private bool _isDisposed;
|
||||
@@ -134,36 +131,29 @@ namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
private void InvokeIconInvoked(MouseButton mouseButton)
|
||||
{
|
||||
IconInvoked?.Invoke(this, new NotificationIconInvokedEventArgs { MouseButton = mouseButton });
|
||||
}
|
||||
|
||||
private void RemoveNotificationIcon() => WriteNotifyIconData(NotifyIconCommand.Delete, NotifyIconDataMember.Message);
|
||||
|
||||
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
if (msg == CallbackMessage)
|
||||
{
|
||||
var mouseButton = MouseButton.Left;
|
||||
var isInvoked = false;
|
||||
|
||||
switch ((uint)lParam)
|
||||
{
|
||||
case WM_LBUTTONUP:
|
||||
isInvoked = true;
|
||||
mouseButton = MouseButton.Left;
|
||||
case (uint)WndProcMessages.WM_LBUTTONUP:
|
||||
InvokeIconInvoked(MouseButton.Left);
|
||||
break;
|
||||
|
||||
case WM_MBUTTONUP:
|
||||
isInvoked = true;
|
||||
mouseButton = MouseButton.Middle;
|
||||
case (uint)WndProcMessages.WM_MBUTTONUP:
|
||||
InvokeIconInvoked(MouseButton.Middle);
|
||||
break;
|
||||
|
||||
case WM_RBUTTONUP:
|
||||
isInvoked = true;
|
||||
mouseButton = MouseButton.Right;
|
||||
case (uint)WndProcMessages.WM_RBUTTONUP:
|
||||
InvokeIconInvoked(MouseButton.Right);
|
||||
break;
|
||||
}
|
||||
|
||||
if (isInvoked)
|
||||
{
|
||||
IconInvoked?.Invoke(this, new NotificationIconInvokedEventArgs { MouseButton = mouseButton });
|
||||
}
|
||||
}
|
||||
|
||||
return DefWindowProcW(hwnd, (uint)msg, wParam, (lParam));
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
internal static class RegistryHelper
|
||||
{
|
||||
public static TValue GetDwordValue<TValue>(string key, string valueName)
|
||||
{
|
||||
using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
|
||||
using var subKey = baseKey.OpenSubKey(key);
|
||||
|
||||
return (TValue)subKey.GetValue(valueName, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,13 +8,10 @@ namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
public static class SystemInformationHelper
|
||||
{
|
||||
private const int SM_CXSCREEN = 0;
|
||||
|
||||
private const int SM_CYSCREEN = 1;
|
||||
|
||||
private const int SPI_GETWORKAREA = 48;
|
||||
|
||||
public static Rect VirtualScreen => GetVirtualScreen();
|
||||
|
||||
public static Rect WorkingArea => GetWorkingArea();
|
||||
|
||||
public static int GetCurrentDpi()
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
using NotificationFlyout.Wpf.UI.Extensions;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using Windows.UI.ViewManagement;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public class SystemPersonalisationHelper
|
||||
{
|
||||
private readonly UISettings _settings = new();
|
||||
|
||||
private SystemTheme _currentTheme;
|
||||
|
||||
|
||||
private SystemPersonalisationHelper(Window window)
|
||||
{
|
||||
var source = HwndSource.FromHwnd(window.GetHandle());
|
||||
source.AddHook(new HwndSourceHook(WndProc));
|
||||
|
||||
_settings.ColorValuesChanged += OnColorValuesChanged;
|
||||
_currentTheme = GetSystemTheme();
|
||||
}
|
||||
|
||||
public event EventHandler<ThemeChangedEventArgs> ThemeChanged;
|
||||
|
||||
public SystemTheme SystemTheme => GetSystemTheme();
|
||||
|
||||
public static SystemPersonalisationHelper Create(Window window)
|
||||
{
|
||||
return new SystemPersonalisationHelper(window);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
private SystemTheme GetSystemTheme()
|
||||
{
|
||||
var uiTheme = _settings.GetColorValue(UIColorType.Background).ToString();
|
||||
return uiTheme == "#FFFFFFFF" ? SystemTheme.Light : SystemTheme.Dark;
|
||||
}
|
||||
|
||||
private void OnColorValuesChanged(UISettings sender, object args)
|
||||
{
|
||||
RaiseThemeChangedEvent();
|
||||
}
|
||||
|
||||
private void RaiseThemeChangedEvent()
|
||||
{
|
||||
var theme = GetSystemTheme();
|
||||
if (theme != _currentTheme)
|
||||
{
|
||||
ThemeChanged?.Invoke(this, new ThemeChangedEventArgs(theme));
|
||||
_currentTheme = theme;
|
||||
}
|
||||
}
|
||||
|
||||
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
if (msg == (int)WndProcMessages.WM_SETTINGCHANGE)
|
||||
{
|
||||
RaiseThemeChangedEvent();
|
||||
}
|
||||
|
||||
return DefWindowProcW(hwnd, (uint)msg, wParam, (lParam));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using NotificationFlyout.Wpf.UI.Extensions;
|
||||
using System;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public static class SystemSettingsHelper
|
||||
{
|
||||
public static SystemTheme DefaultSystemTheme => GetDefaultSystemTheme();
|
||||
|
||||
private static SystemTheme GetDefaultSystemTheme()
|
||||
{
|
||||
return Environment.OSVersion.IsGreaterThan(OperatingSystemVersion.Windows10_1809) && DoesSystemUsesLightTheme() ? SystemTheme.Light : SystemTheme.Dark;
|
||||
}
|
||||
|
||||
private static bool DoesSystemUsesLightTheme()
|
||||
{
|
||||
var personalizeKey = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
|
||||
return RegistryHelper.GetDwordValue<int>(personalizeKey, "SystemUsesLightTheme") > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,9 @@ namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
public class TaskbarHelper
|
||||
{
|
||||
private const string ShellTrayHandleName = "Shell_TrayWnd";
|
||||
|
||||
private const int SPI_SETWORKAREA = 0x002F;
|
||||
|
||||
private const int WSETTINGCHANGE = 0x001A;
|
||||
|
||||
private static readonly uint WTASKBARCREATED = PInvoke.RegisterWindowMessage("TaskbarCreated");
|
||||
private readonly uint WM_TASKBARCREATED = PInvoke.RegisterWindowMessage("TaskbarCreated");
|
||||
|
||||
private TaskbarHelper(Window window)
|
||||
{
|
||||
@@ -99,7 +96,7 @@ namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
|
||||
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
if (msg == WTASKBARCREATED || msg == WSETTINGCHANGE && (int)wParam == SPI_SETWORKAREA)
|
||||
if (msg == WM_TASKBARCREATED || msg == (int)WndProcMessages.WM_SETTINGCHANGE && (int)wParam == SPI_SETWORKAREA)
|
||||
{
|
||||
TaskbarChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public class ThemeChangedEventArgs : EventArgs
|
||||
{
|
||||
internal ThemeChangedEventArgs(SystemTheme theme)
|
||||
{
|
||||
Theme = theme;
|
||||
}
|
||||
|
||||
public SystemTheme Theme { get; private set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
internal enum WndProcMessages
|
||||
{
|
||||
WM_LBUTTONUP = 0x0202,
|
||||
WM_MBUTTONUP = 0x0208,
|
||||
WM_RBUTTONUP = 0x0205,
|
||||
WM_MOUSEMOVE = 0x0200,
|
||||
WM_SETTINGCHANGE = 0x001A,
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.19041.1" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user