The 660-line tray service mixed three concerns. Split into: - TrayIconInterop: all P/Invoke declarations, interop structs, and the subclass delegate (consumers use 'using static'), so the Win32 surface is isolated and reviewable. - SensorIconRenderer: the GDI drawing of the 16x16 per-sensor value icon. - TrayIconService: slimmed to tray orchestration (icon lifecycle, context menus, callback routing) at ~340 lines. Also dedupes the per-sensor settings-key helper onto SensorSelectionService.GetSensorSettingName. Pure relocation; behavior is unchanged. Verified by clean build, 199 tests, and an app smoke-run. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
200 lines
7.4 KiB
C#
200 lines
7.4 KiB
C#
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
|
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
// Copyright (C) LibreHardwareMonitor and Contributors.
|
|
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace LibreHardwareMonitor.Windows.WinUI.Services;
|
|
|
|
// Win32 interop for the tray icon: the shell notify-icon API, window subclassing for its callback messages, the popup
|
|
// context menu, and the GDI calls used to render the per-sensor icon bitmaps. Isolated here (consumers use
|
|
// `using static TrayIconInterop;`) so the P/Invoke surface is reviewable in one place, separate from the orchestration
|
|
// and rendering logic.
|
|
internal static class TrayIconInterop
|
|
{
|
|
internal delegate IntPtr SubclassProc(IntPtr hWnd, uint message, IntPtr wParam, IntPtr lParam, nuint subclassId, nuint refData);
|
|
|
|
[DllImport("Comctl32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool SetWindowSubclass(IntPtr hWnd, SubclassProc subclassProc, nuint subclassId, nuint refData);
|
|
|
|
[DllImport("Comctl32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool RemoveWindowSubclass(IntPtr hWnd, SubclassProc subclassProc, nuint subclassId);
|
|
|
|
[DllImport("Comctl32.dll")]
|
|
internal static extern IntPtr DefSubclassProc(IntPtr hWnd, uint message, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool Shell_NotifyIcon(uint message, ref NotifyIconData data);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
internal static extern IntPtr LoadImage(IntPtr instance, string name, uint type, int desiredWidth, int desiredHeight, uint load);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
internal static extern IntPtr LoadIcon(IntPtr instance, IntPtr iconName);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool DestroyIcon(IntPtr icon);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
internal static extern IntPtr CreatePopupMenu();
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool AppendMenu(IntPtr menu, uint flags, uint newItemId, string? newItem);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool DestroyMenu(IntPtr menu);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
internal static extern int TrackPopupMenuEx(IntPtr menu, uint flags, int x, int y, IntPtr windowHandle, IntPtr parameters);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool GetCursorPos(out Point point);
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool SetForegroundWindow(IntPtr windowHandle);
|
|
|
|
[DllImport("user32.dll")]
|
|
internal static extern IntPtr GetDC(IntPtr windowHandle);
|
|
|
|
[DllImport("user32.dll")]
|
|
internal static extern int ReleaseDC(IntPtr windowHandle, IntPtr deviceContext);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
internal static extern IntPtr CreateCompatibleDC(IntPtr deviceContext);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool DeleteDC(IntPtr deviceContext);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
internal static extern IntPtr SelectObject(IntPtr deviceContext, IntPtr gdiObject);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
internal static extern bool DeleteObject(IntPtr gdiObject);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
internal static extern IntPtr CreateDIBSection(IntPtr deviceContext, ref BitmapInfo bitmapInfo, uint usage, out IntPtr bits, IntPtr section, uint offset);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
internal static extern IntPtr CreateBitmap(int width, int height, uint planes, uint bitCount, IntPtr bits);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
internal static extern IntPtr CreateSolidBrush(uint color);
|
|
|
|
[DllImport("user32.dll")]
|
|
internal static extern int FillRect(IntPtr deviceContext, ref Rect rect, IntPtr brush);
|
|
|
|
[DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
|
|
internal static extern IntPtr CreateFont(int height, int width, int escapement, int orientation, int weight, uint italic, uint underline, uint strikeOut, uint charSet, uint outputPrecision, uint clipPrecision, uint quality, uint pitchAndFamily, string faceName);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
internal static extern int SetBkMode(IntPtr deviceContext, int mode);
|
|
|
|
[DllImport("gdi32.dll")]
|
|
internal static extern uint SetTextColor(IntPtr deviceContext, uint color);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
|
internal static extern int DrawText(IntPtr deviceContext, string text, int count, ref Rect rect, uint format);
|
|
|
|
[DllImport("user32.dll")]
|
|
internal static extern IntPtr CreateIconIndirect(ref IconInfo iconInfo);
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
internal struct NotifyIconData
|
|
{
|
|
public int cbSize;
|
|
public IntPtr hWnd;
|
|
public uint uID;
|
|
public uint uFlags;
|
|
public uint uCallbackMessage;
|
|
public IntPtr hIcon;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
|
|
public string szTip;
|
|
public uint dwState;
|
|
public uint dwStateMask;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
public string szInfo;
|
|
public uint uTimeoutOrVersion;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
|
public string szInfoTitle;
|
|
public uint dwInfoFlags;
|
|
public Guid guidItem;
|
|
public IntPtr hBalloonIcon;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
internal struct Point
|
|
{
|
|
public int X;
|
|
public int Y;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
internal struct Rect
|
|
{
|
|
public int Left;
|
|
public int Top;
|
|
public int Right;
|
|
public int Bottom;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
internal struct IconInfo
|
|
{
|
|
[MarshalAs(UnmanagedType.Bool)]
|
|
public bool IsIcon;
|
|
public int XHotspot;
|
|
public int YHotspot;
|
|
public IntPtr MaskBitmap;
|
|
public IntPtr ColorBitmap;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
internal struct BitmapInfoHeader
|
|
{
|
|
public uint Size;
|
|
public int Width;
|
|
public int Height;
|
|
public ushort Planes;
|
|
public ushort BitCount;
|
|
public uint Compression;
|
|
public uint SizeImage;
|
|
public int XPelsPerMeter;
|
|
public int YPelsPerMeter;
|
|
public uint ClrUsed;
|
|
public uint ClrImportant;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
internal struct BitmapInfo
|
|
{
|
|
public BitmapInfoHeader Header;
|
|
public uint Colors;
|
|
|
|
public static BitmapInfo Create(int width, int height)
|
|
{
|
|
return new BitmapInfo
|
|
{
|
|
Header = new BitmapInfoHeader
|
|
{
|
|
Size = (uint)Marshal.SizeOf<BitmapInfoHeader>(),
|
|
Width = width,
|
|
Height = -height,
|
|
Planes = 1,
|
|
BitCount = 32
|
|
}
|
|
};
|
|
}
|
|
}
|
|
}
|