From c167257aaef995f48c5d56e3d9d73c89ca09637f Mon Sep 17 00:00:00 2001 From: Dan Clark Date: Sat, 2 Nov 2024 20:04:51 +0000 Subject: [PATCH] Create a new Toolkit.Windows project --- Toolkit.Windows/IPointerMonitor.cs | 8 +- Toolkit.Windows/PointerButton.cs | 8 ++ Toolkit.Windows/PointerDragEventArgs.cs | 3 + .../PointerDragReleasedEventArgs.cs | 3 + Toolkit.Windows/PointerLocation.cs | 3 + ...kbarButtonMonitor.cs => PointerMonitor.cs} | 105 ++++++------------ Toolkit.Windows/PointerMovedEventArgs.cs | 3 + Toolkit.Windows/PointerPressedEventArgs.cs | 3 + Toolkit.Windows/PointerReleasedEventArgs.cs | 3 + Toolkit.Windows/Toolkit.Windows.csproj | 2 +- Toolkit.Windows/WndProcMessages.cs | 13 +++ Toolkit.sln | 52 ++++++++- 12 files changed, 129 insertions(+), 77 deletions(-) create mode 100644 Toolkit.Windows/PointerButton.cs create mode 100644 Toolkit.Windows/PointerDragEventArgs.cs create mode 100644 Toolkit.Windows/PointerDragReleasedEventArgs.cs create mode 100644 Toolkit.Windows/PointerLocation.cs rename Toolkit.Windows/{TaskbarButtonMonitor.cs => PointerMonitor.cs} (73%) create mode 100644 Toolkit.Windows/PointerMovedEventArgs.cs create mode 100644 Toolkit.Windows/PointerPressedEventArgs.cs create mode 100644 Toolkit.Windows/PointerReleasedEventArgs.cs create mode 100644 Toolkit.Windows/WndProcMessages.cs diff --git a/Toolkit.Windows/IPointerMonitor.cs b/Toolkit.Windows/IPointerMonitor.cs index 76502a0..0888c1d 100644 --- a/Toolkit.Windows/IPointerMonitor.cs +++ b/Toolkit.Windows/IPointerMonitor.cs @@ -1,5 +1,7 @@ -namespace Toolkit.Windows; +using Toolkit.Foundation; -public interface IPointerMonitor : - IInitializer, +namespace Toolkit.Windows; + +public interface IPointerMonitor : + IInitialization, IDisposable; diff --git a/Toolkit.Windows/PointerButton.cs b/Toolkit.Windows/PointerButton.cs new file mode 100644 index 0000000..8f87e3d --- /dev/null +++ b/Toolkit.Windows/PointerButton.cs @@ -0,0 +1,8 @@ +namespace Toolkit.Windows; + +public enum PointerButton +{ + Left, + Middle, + Right +} diff --git a/Toolkit.Windows/PointerDragEventArgs.cs b/Toolkit.Windows/PointerDragEventArgs.cs new file mode 100644 index 0000000..9db28f4 --- /dev/null +++ b/Toolkit.Windows/PointerDragEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Windows; + +public record PointerDragEventArgs(PointerLocation Location); diff --git a/Toolkit.Windows/PointerDragReleasedEventArgs.cs b/Toolkit.Windows/PointerDragReleasedEventArgs.cs new file mode 100644 index 0000000..572a578 --- /dev/null +++ b/Toolkit.Windows/PointerDragReleasedEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Windows; + +public record PointerDragReleasedEventArgs(PointerLocation Location, PointerButton Button = PointerButton.Left); diff --git a/Toolkit.Windows/PointerLocation.cs b/Toolkit.Windows/PointerLocation.cs new file mode 100644 index 0000000..cae5243 --- /dev/null +++ b/Toolkit.Windows/PointerLocation.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Windows; + +public record PointerLocation(int X, int Y); diff --git a/Toolkit.Windows/TaskbarButtonMonitor.cs b/Toolkit.Windows/PointerMonitor.cs similarity index 73% rename from Toolkit.Windows/TaskbarButtonMonitor.cs rename to Toolkit.Windows/PointerMonitor.cs index b8d8486..c734816 100644 --- a/Toolkit.Windows/TaskbarButtonMonitor.cs +++ b/Toolkit.Windows/PointerMonitor.cs @@ -1,57 +1,21 @@ -using Windows.Win32; +using System.Drawing; +using Windows.Win32; using Windows.Win32.UI.WindowsAndMessaging; using Windows.Win32.Foundation; using System.Diagnostics.CodeAnalysis; -using System.Drawing; +using Toolkit.Foundation; namespace Toolkit.Windows; -public record PointerPressed(PointerLocation Location, PointerButton Button = PointerButton.Left); -public record PointerDragReleased(PointerLocation Location, PointerButton Button = PointerButton.Left); - -public record PointerReleased(PointerLocation Location, PointerButton Button = PointerButton.Left); - -public record PointerLocation(int X, int Y); - -public record PointerMoved(PointerLocation Location); - -internal enum WndProcMessages +public class PointerMonitor(IPublisher publisher) : + IPointerMonitor { - WM_LBUTTONUP = 0x0202, - WM_MBUTTONUP = 0x0208, - WM_RBUTTONUP = 0x0205, - WM_MOUSEMOVE = 0x0200, - WM_SETTINGCHANGE = 0x001A, - WM_MBUTTONDOWN = 0x0207, - WM_LBUTTONDOWN = 0x0201, - WM_RBUTTONDOWN = 0x0204 -} - -public enum PointerButton -{ - Left, - Middle, - Right -} - -public record PointerDrag(PointerLocation Location); - - -public class PointerMonitor : IPointerMonitor -{ - private readonly IMessenger messenger; private bool isDisposed; + private bool isPointerDrag; private bool isPointerPressed; private HOOKPROC? mouseEventDelegate; private UnhookWindowsHookExSafeHandle? mouseHandle; - private bool isPointerDrag; - - public PointerMonitor(IMessenger messenger) - { - this.messenger = messenger; - } - ~PointerMonitor() { Dispose(false); @@ -63,11 +27,8 @@ public class PointerMonitor : IPointerMonitor GC.SuppressFinalize(this); } - public unsafe Task Initialize() - { + public unsafe void Initialize() => InitializeHook(); - return Task.CompletedTask; - } protected virtual void Dispose(bool disposing) { @@ -85,27 +46,6 @@ public class PointerMonitor : IPointerMonitor PInvoke.GetModuleHandle("user32.dll"), 0); } - private unsafe bool TryGetPointer(out Point point) - { - fixed (Point* lpPointLocal = &point) - { - return PInvoke.GetPhysicalCursorPos(lpPointLocal); - } - } - - private bool TryGetPointerLocation([MaybeNullWhen(false)] out PointerLocation location) - { - if (TryGetPointer(out Point point)) - { - location = new PointerLocation(point.X, point.Y); - return true; - - } - - location = null; - return false; - } - private LRESULT MouseProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode >= 0) @@ -160,16 +100,16 @@ public class PointerMonitor : IPointerMonitor isPointerDrag = true; } - messenger.Send(new PointerDrag(location)); + publisher.Publish(new PointerDragEventArgs(location)); } - messenger.Send(new PointerMoved(location)); + publisher.Publish(new PointerMovedEventArgs(location)); } private void SendPointerPressed(PointerLocation location, PointerButton button) { isPointerPressed = true; - messenger.Send(new PointerPressed(location, button)); + publisher.Publish(new PointerPressedEventArgs(location, button)); } private void SendPointerReleased(PointerLocation location, PointerButton button) @@ -179,11 +119,32 @@ public class PointerMonitor : IPointerMonitor if (isPointerDrag) { isPointerDrag = false; - messenger.Send(new PointerDragReleased(location, button)); + publisher.Publish(new PointerDragReleasedEventArgs(location, button)); } isPointerPressed = false; - messenger.Send(new PointerReleased(location, button)); + publisher.Publish(new PointerReleasedEventArgs(location, button)); } } + + private unsafe bool TryGetPointer(out Point point) + { + fixed (Point* lpPointLocal = &point) + { + return PInvoke.GetPhysicalCursorPos(lpPointLocal); + } + } + + private bool TryGetPointerLocation([MaybeNullWhen(false)] out PointerLocation location) + { + if (TryGetPointer(out Point point)) + { + location = new PointerLocation(point.X, point.Y); + return true; + + } + + location = null; + return false; + } } \ No newline at end of file diff --git a/Toolkit.Windows/PointerMovedEventArgs.cs b/Toolkit.Windows/PointerMovedEventArgs.cs new file mode 100644 index 0000000..c66df67 --- /dev/null +++ b/Toolkit.Windows/PointerMovedEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Windows; + +public record PointerMovedEventArgs(PointerLocation Location); diff --git a/Toolkit.Windows/PointerPressedEventArgs.cs b/Toolkit.Windows/PointerPressedEventArgs.cs new file mode 100644 index 0000000..0855470 --- /dev/null +++ b/Toolkit.Windows/PointerPressedEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Windows; + +public record PointerPressedEventArgs(PointerLocation Location, PointerButton Button = PointerButton.Left); diff --git a/Toolkit.Windows/PointerReleasedEventArgs.cs b/Toolkit.Windows/PointerReleasedEventArgs.cs new file mode 100644 index 0000000..691137e --- /dev/null +++ b/Toolkit.Windows/PointerReleasedEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Windows; + +public record PointerReleasedEventArgs(PointerLocation Location, PointerButton Button = PointerButton.Left); diff --git a/Toolkit.Windows/Toolkit.Windows.csproj b/Toolkit.Windows/Toolkit.Windows.csproj index 093ec63..ae4c7d3 100644 --- a/Toolkit.Windows/Toolkit.Windows.csproj +++ b/Toolkit.Windows/Toolkit.Windows.csproj @@ -1,7 +1,7 @@  - net8.0-windows + net9.0-windows enable enable True diff --git a/Toolkit.Windows/WndProcMessages.cs b/Toolkit.Windows/WndProcMessages.cs new file mode 100644 index 0000000..38591b3 --- /dev/null +++ b/Toolkit.Windows/WndProcMessages.cs @@ -0,0 +1,13 @@ +namespace Toolkit.Windows; + +internal enum WndProcMessages +{ + WM_LBUTTONUP = 0x0202, + WM_MBUTTONUP = 0x0208, + WM_RBUTTONUP = 0x0205, + WM_MOUSEMOVE = 0x0200, + WM_SETTINGCHANGE = 0x001A, + WM_MBUTTONDOWN = 0x0207, + WM_LBUTTONDOWN = 0x0201, + WM_RBUTTONDOWN = 0x0204 +} diff --git a/Toolkit.sln b/Toolkit.sln index 3afc236..d5ff926 100644 --- a/Toolkit.sln +++ b/Toolkit.sln @@ -9,30 +9,80 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Toolkit.UI.Avalonia", "Tool EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Toolkit.UI.Controls.Avalonia", "Toolkit.UI.Controls.Avalonia\Toolkit.UI.Controls.Avalonia.csproj", "{8841990D-A246-495D-9A40-C39BA4347505}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Toolkit.Avalonia", "Toolkit.Avalonia\Toolkit.Avalonia.csproj", "{9585A317-4405-4E39-BDBE-23EF822FBF34}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Toolkit.Avalonia", "Toolkit.Avalonia\Toolkit.Avalonia.csproj", "{9585A317-4405-4E39-BDBE-23EF822FBF34}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Toolkit.Windows", "Toolkit.Windows\Toolkit.Windows.csproj", "{08F06CCE-86F9-4885-84F0-B23B2E5A0813}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {66968F8D-689E-49D8-9370-DFF099C56202}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {66968F8D-689E-49D8-9370-DFF099C56202}.Debug|Any CPU.Build.0 = Debug|Any CPU + {66968F8D-689E-49D8-9370-DFF099C56202}.Debug|x64.ActiveCfg = Debug|x64 + {66968F8D-689E-49D8-9370-DFF099C56202}.Debug|x64.Build.0 = Debug|x64 + {66968F8D-689E-49D8-9370-DFF099C56202}.Debug|x86.ActiveCfg = Debug|x86 + {66968F8D-689E-49D8-9370-DFF099C56202}.Debug|x86.Build.0 = Debug|x86 {66968F8D-689E-49D8-9370-DFF099C56202}.Release|Any CPU.ActiveCfg = Release|Any CPU {66968F8D-689E-49D8-9370-DFF099C56202}.Release|Any CPU.Build.0 = Release|Any CPU + {66968F8D-689E-49D8-9370-DFF099C56202}.Release|x64.ActiveCfg = Release|x64 + {66968F8D-689E-49D8-9370-DFF099C56202}.Release|x64.Build.0 = Release|x64 + {66968F8D-689E-49D8-9370-DFF099C56202}.Release|x86.ActiveCfg = Release|x86 + {66968F8D-689E-49D8-9370-DFF099C56202}.Release|x86.Build.0 = Release|x86 {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Debug|x64.ActiveCfg = Debug|x64 + {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Debug|x64.Build.0 = Debug|x64 + {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Debug|x86.ActiveCfg = Debug|x86 + {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Debug|x86.Build.0 = Debug|x86 {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Release|Any CPU.ActiveCfg = Release|Any CPU {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Release|Any CPU.Build.0 = Release|Any CPU + {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Release|x64.ActiveCfg = Release|x64 + {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Release|x64.Build.0 = Release|x64 + {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Release|x86.ActiveCfg = Release|x86 + {E091FA94-2F15-403A-98D1-4557C2FF9A02}.Release|x86.Build.0 = Release|x86 {8841990D-A246-495D-9A40-C39BA4347505}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8841990D-A246-495D-9A40-C39BA4347505}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8841990D-A246-495D-9A40-C39BA4347505}.Debug|x64.ActiveCfg = Debug|x64 + {8841990D-A246-495D-9A40-C39BA4347505}.Debug|x64.Build.0 = Debug|x64 + {8841990D-A246-495D-9A40-C39BA4347505}.Debug|x86.ActiveCfg = Debug|x86 + {8841990D-A246-495D-9A40-C39BA4347505}.Debug|x86.Build.0 = Debug|x86 {8841990D-A246-495D-9A40-C39BA4347505}.Release|Any CPU.ActiveCfg = Release|Any CPU {8841990D-A246-495D-9A40-C39BA4347505}.Release|Any CPU.Build.0 = Release|Any CPU + {8841990D-A246-495D-9A40-C39BA4347505}.Release|x64.ActiveCfg = Release|x64 + {8841990D-A246-495D-9A40-C39BA4347505}.Release|x64.Build.0 = Release|x64 + {8841990D-A246-495D-9A40-C39BA4347505}.Release|x86.ActiveCfg = Release|x86 + {8841990D-A246-495D-9A40-C39BA4347505}.Release|x86.Build.0 = Release|x86 {9585A317-4405-4E39-BDBE-23EF822FBF34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9585A317-4405-4E39-BDBE-23EF822FBF34}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9585A317-4405-4E39-BDBE-23EF822FBF34}.Debug|x64.ActiveCfg = Debug|Any CPU + {9585A317-4405-4E39-BDBE-23EF822FBF34}.Debug|x64.Build.0 = Debug|Any CPU + {9585A317-4405-4E39-BDBE-23EF822FBF34}.Debug|x86.ActiveCfg = Debug|Any CPU + {9585A317-4405-4E39-BDBE-23EF822FBF34}.Debug|x86.Build.0 = Debug|Any CPU {9585A317-4405-4E39-BDBE-23EF822FBF34}.Release|Any CPU.ActiveCfg = Release|Any CPU {9585A317-4405-4E39-BDBE-23EF822FBF34}.Release|Any CPU.Build.0 = Release|Any CPU + {9585A317-4405-4E39-BDBE-23EF822FBF34}.Release|x64.ActiveCfg = Release|Any CPU + {9585A317-4405-4E39-BDBE-23EF822FBF34}.Release|x64.Build.0 = Release|Any CPU + {9585A317-4405-4E39-BDBE-23EF822FBF34}.Release|x86.ActiveCfg = Release|Any CPU + {9585A317-4405-4E39-BDBE-23EF822FBF34}.Release|x86.Build.0 = Release|Any CPU + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Debug|Any CPU.ActiveCfg = Debug|x64 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Debug|Any CPU.Build.0 = Debug|x64 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Debug|x64.ActiveCfg = Debug|x64 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Debug|x64.Build.0 = Debug|x64 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Debug|x86.ActiveCfg = Debug|x86 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Debug|x86.Build.0 = Debug|x86 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Release|Any CPU.ActiveCfg = Release|x64 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Release|Any CPU.Build.0 = Release|x64 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Release|x64.ActiveCfg = Release|x64 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Release|x64.Build.0 = Release|x64 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Release|x86.ActiveCfg = Release|x86 + {08F06CCE-86F9-4885-84F0-B23B2E5A0813}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE