Tray icon now respects current system theme

This commit is contained in:
Daniel Clark
2021-02-07 16:24:20 +00:00
parent fbf7713705
commit 72941706d9
17 changed files with 213 additions and 93 deletions
@@ -0,0 +1,35 @@
using System;
using Windows.UI.ViewManagement;
namespace NotificationFlyout.Shared.UI.Helpers
{
public class SystemPersonalisationHelper
{
private readonly UISettings _settings = new UISettings();
private SystemPersonalisationHelper()
{
_settings.ColorValuesChanged += _settings_ColorValuesChanged;
}
private void _settings_ColorValuesChanged(UISettings sender, object args)
{
ThemeChanged?.Invoke(this, EventArgs.Empty);
}
public event EventHandler ThemeChanged;
public SystemTheme SystemTheme => GetSystemTheme();
public static SystemPersonalisationHelper Create()
{
return new SystemPersonalisationHelper();
}
private SystemTheme GetSystemTheme()
{
var uiTheme = _settings.GetColorValue(UIColorType.Background).ToString();
return uiTheme == "#FFFFFFFF" ? SystemTheme.Light : SystemTheme.Dark;
}
}
}
@@ -0,0 +1,8 @@
namespace NotificationFlyout.Shared.UI.Helpers
{
public enum SystemTheme
{
Dark,
Light,
}
}
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.19041.1" />
</ItemGroup>
</Project>
Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

@@ -12,6 +12,7 @@
</PropertyGroup>
<ItemGroup>
<None Remove="Assets\Icon-Light.ico" />
<None Remove="Assets\Icon.ico" />
</ItemGroup>
@@ -27,6 +28,7 @@
</ItemGroup>
<ItemGroup>
<Resource Include="Assets\Icon-Light.ico" />
<Resource Include="Assets\Icon.ico" />
</ItemGroup>
@@ -4,6 +4,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sample="clr-namespace:NotificationFlyout.Sample;assembly=NotificationFlyout.Sample"
IconSource="/Assets/Icon.ico"
LightIconSource="/Assets/Icon.ico">
LightIconSource="/Assets/Icon-Light.ico">
<sample:NotificationFlyoutPresenter />
</NotificationFlyout>
@@ -1,6 +1,4 @@
using NotificationFlyout.Uwp.UI.Controls;
using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Wpf.UI.Helpers;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
@@ -25,8 +23,6 @@ namespace NotificationFlyout.Wpf.UI.Controls
typeof(NotificationFlyoutPresenter), typeof(NotificationFlyout),
new PropertyMetadata(null, OnFlyoutPresenterPropertyChanged));
private const string ShellTrayHandleName = "Shell_TrayWnd";
private readonly NotificationFlyoutXamlHost _xamlHost;
public NotificationFlyout()
@@ -82,21 +78,7 @@ namespace NotificationFlyout.Wpf.UI.Controls
private void OnIconPropertyChanged()
{
SetIcon();
}
private void SetIcon()
{
var shellTrayHandle = WindowHelper.GetHandle(ShellTrayHandleName);
if (shellTrayHandle == null) return;
var dpi = WindowHelper.GetDpi(shellTrayHandle);
var iconSource = SystemSettingsHelper.DefaultSystemTheme == SystemTheme.Dark ? IconSource : LightIconSource;
if (iconSource == null) return;
using var icon = iconSource.ConvertToIcon(dpi);
_xamlHost.SetNotificationIcon(icon.Handle);
_xamlHost.SetIcons(IconSource, LightIconSource);
}
}
}
@@ -11,10 +11,16 @@ namespace NotificationFlyout.Wpf.UI.Controls
{
internal class NotificationFlyoutXamlHost : Window
{
private const double MaximumOffset = 80;
private const string ShellTrayHandleName = "Shell_TrayWnd";
private const double WindowSize = 5;
private ImageSource _defaultIconSource;
private ImageSource _lightIconSource;
private NotificationIconHelper _notificationIconHelper;
private SystemPersonalisationHelper _systemPersonalisationHelper;
private TaskbarHelper _taskbarHelper;
private WindowsXamlHost _xamlHost;
private bool _isLoaded;
public NotificationFlyoutXamlHost()
{
@@ -42,9 +48,12 @@ namespace NotificationFlyout.Wpf.UI.Controls
}
}
internal void SetNotificationIcon(IntPtr handle)
internal void SetIcons(ImageSource defaultIconSource, ImageSource lightIconSource)
{
_notificationIconHelper.SetIcon(handle);
_defaultIconSource = defaultIconSource;
_lightIconSource = lightIconSource;
UpdateIcon();
}
internal void ShowFlyout()
@@ -83,7 +92,16 @@ namespace NotificationFlyout.Wpf.UI.Controls
PrepareNotificationIcon();
PrepareTaskbar();
_isLoaded = true;
UpdateWindow();
UpdateIcon();
}
private void OnSystemThemeChanged(object sender, EventArgs args)
{
UpdateIcon();
}
private void OnTaskbarChanged(object sender, EventArgs args)
@@ -99,14 +117,17 @@ namespace NotificationFlyout.Wpf.UI.Controls
ResizeMode = ResizeMode.NoResize;
AllowsTransparency = true;
Background = new SolidColorBrush(Colors.Transparent);
Height = 5;
Width = 5;
Height = WindowSize;
Width = WindowSize;
}
private void PrepareNotificationIcon()
{
_notificationIconHelper = NotificationIconHelper.Create(this);
_notificationIconHelper.IconInvoked += OnIconInvoked;
_systemPersonalisationHelper = SystemPersonalisationHelper.Create(this);
_systemPersonalisationHelper.ThemeChanged += OnSystemThemeChanged;
}
private void PrepareTaskbar()
@@ -128,8 +149,26 @@ namespace NotificationFlyout.Wpf.UI.Controls
Content = _xamlHost;
}
private void UpdateIcon()
{
if (!_isLoaded) return;
var shellTrayHandle = WindowHelper.GetHandle(ShellTrayHandleName);
if (shellTrayHandle == null) return;
var dpi = WindowHelper.GetDpi(shellTrayHandle);
var iconSource = _systemPersonalisationHelper.SystemTheme == SystemTheme.Dark ? _defaultIconSource : _lightIconSource;
if (iconSource == null) return;
using var icon = iconSource.ConvertToIcon(dpi);
_notificationIconHelper.SetIcon(icon.Handle);
}
private void UpdateWindow()
{
if (!_isLoaded) return;
var flyoutHost = GetFlyoutHost();
if (flyoutHost == null) return;
@@ -138,8 +177,8 @@ namespace NotificationFlyout.Wpf.UI.Controls
Left = taskbarState.Screen.WorkingArea.Left;
Top = taskbarState.Screen.WorkingArea.Top;
var windowWidth = 5 * this.DpiX();
var windowHeight = 5 * this.DpiY();
var windowWidth = WindowSize * this.DpiX();
var windowHeight = WindowSize * this.DpiY();
double top, left, height, width;
@@ -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>