diff --git a/LightBulb.PlatformInterop/GlobalHotKey.cs b/LightBulb.PlatformInterop/GlobalHotKey.cs index df9da5b..914c8ee 100644 --- a/LightBulb.PlatformInterop/GlobalHotKey.cs +++ b/LightBulb.PlatformInterop/GlobalHotKey.cs @@ -59,6 +59,7 @@ public partial class GlobalHotKey public static GlobalHotKey? TryRegister(int virtualKey, int modifiers, Action callback) { var handle = Interlocked.Increment(ref _lastHotKeyHandle); + if ( !NativeMethods.RegisterHotKey( WndProcSponge.Default.Handle, diff --git a/LightBulb.PlatformInterop/Internal/NativeMethods.User32.cs b/LightBulb.PlatformInterop/Internal/NativeMethods.User32.cs index 504874b..9bb855f 100644 --- a/LightBulb.PlatformInterop/Internal/NativeMethods.User32.cs +++ b/LightBulb.PlatformInterop/Internal/NativeMethods.User32.cs @@ -9,24 +9,24 @@ internal static partial class NativeMethods private const string User32 = "user32.dll"; [DllImport(User32, SetLastError = true)] - public static extern bool GetMonitorInfo(nint hMonitor, ref MonitorInfoEx lpmi); - + public static extern bool EnumDisplayMonitors( + nint hdc, + nint lprcClip, + EnumMonitorsProc lpfnEnum, + nint dwData + ); + [DllImport(User32, SetLastError = true)] - public static extern ushort RegisterClassEx(ref WndClassEx lpwcx); + public static extern nint MonitorFromWindow(nint hWnd, uint dwFlags); [DllImport(User32, SetLastError = true)] - public static extern bool UnregisterClass(string lpClassName, IntPtr hInstance); + public static extern bool GetMonitorInfo(nint hMonitor, ref MonitorInfoEx lpmi); [DllImport(User32, SetLastError = true)] - public static extern nint MonitorFromWindow(nint hWnd, uint dwFlags); + public static extern ushort RegisterClassEx(ref WndClassEx lpwcx); [DllImport(User32, SetLastError = true)] - public static extern bool EnumDisplayMonitors( - nint hdc, - nint lprcClip, - EnumMonitorsProc lpfnEnum, - nint dwData - ); + public static extern bool UnregisterClass(string lpClassName, IntPtr hInstance); [DllImport(User32, SetLastError = true)] public static extern nint CreateWindowEx( diff --git a/LightBulb.PlatformInterop/Internal/WndClassEx.cs b/LightBulb.PlatformInterop/Internal/WndClassEx.cs index 3ff78eb..0f33a4e 100644 --- a/LightBulb.PlatformInterop/Internal/WndClassEx.cs +++ b/LightBulb.PlatformInterop/Internal/WndClassEx.cs @@ -5,14 +5,18 @@ namespace LightBulb.PlatformInterop.Internal; [StructLayout(LayoutKind.Sequential)] internal readonly record struct WndClassEx { - public WndClassEx() => Size = (uint)Marshal.SizeOf(this); + public WndClassEx() + { + Size = (uint)Marshal.SizeOf(this); + Instance = Marshal.GetHINSTANCE(typeof(WndClassEx).Module); + } public uint Size { get; } public uint Style { get; init; } public required WndProc WndProc { get; init; } public int ClassExtra { get; init; } public int WindowExtra { get; init; } - public nint Instance { get; init; } + public nint Instance { get; } public nint Icon { get; init; } public nint Cursor { get; init; } public nint Background { get; init; } diff --git a/LightBulb.PlatformInterop/Internal/WndProcMessage.cs b/LightBulb.PlatformInterop/Internal/WndProcMessage.cs index 72b7a1e..6a81ca1 100644 --- a/LightBulb.PlatformInterop/Internal/WndProcMessage.cs +++ b/LightBulb.PlatformInterop/Internal/WndProcMessage.cs @@ -5,7 +5,7 @@ namespace LightBulb.PlatformInterop.Internal; internal readonly record struct WndProcMessage(uint Id, nint WParam, nint LParam) { - public T DeserializeLParam() => + public T GetLParam() => (T?)Marshal.PtrToStructure(LParam, typeof(T)) ?? throw new InvalidOperationException( $"Failed to deserialize WndProc message's lParam to {typeof(T)}." diff --git a/LightBulb.PlatformInterop/Internal/WndProcSponge.cs b/LightBulb.PlatformInterop/Internal/WndProcSponge.cs index 022fd3a..50e32a7 100644 --- a/LightBulb.PlatformInterop/Internal/WndProcSponge.cs +++ b/LightBulb.PlatformInterop/Internal/WndProcSponge.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Runtime.InteropServices; using LightBulb.PlatformInterop.Utils; namespace LightBulb.PlatformInterop.Internal; @@ -71,8 +70,7 @@ internal partial class WndProcSponge var classInfo = new WndClassEx { ClassName = ClassName, - WndProc = wndProc, - Instance = Marshal.GetHINSTANCE(typeof(WndProcSponge).Module) + WndProc = wndProc }; var classHandle = NativeMethods.RegisterClassEx(ref classInfo); diff --git a/LightBulb.PlatformInterop/PowerSettingNotification.cs b/LightBulb.PlatformInterop/PowerSettingNotification.cs index 0d78415..77174f7 100644 --- a/LightBulb.PlatformInterop/PowerSettingNotification.cs +++ b/LightBulb.PlatformInterop/PowerSettingNotification.cs @@ -14,7 +14,7 @@ public partial class PowerSettingNotification(nint handle, Guid powerSettingId, m => { // Filter out other power events - if (m.DeserializeLParam().PowerSettingId != powerSettingId) + if (m.GetLParam().PowerSettingId != powerSettingId) return; callback(); diff --git a/LightBulb.PlatformInterop/Window.cs b/LightBulb.PlatformInterop/Window.cs index 6fbf6e9..c4e9d5f 100644 --- a/LightBulb.PlatformInterop/Window.cs +++ b/LightBulb.PlatformInterop/Window.cs @@ -76,7 +76,7 @@ public bool IsFullScreen() if (TryGetRect() is not { } windowRect) return false; - // Calculate absolute window client rect (not relative to the window) + // Calculate the absolute window client rect (not relative to the window) var windowClientRect = TryGetClientRect() ?? Rect.Empty; var absoluteWindowClientRect = new Rect(