From 5d836f88efa1ae6a3ee031ef2a3176942cb7773e Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sat, 23 Jul 2022 01:20:48 -0400 Subject: [PATCH] Avoid deprecated input API in win32 backend --- .../Input/WindowsMouseDevice.cs | 21 +++++++++++++++---- src/Windows/Avalonia.Win32/WindowImpl.cs | 7 +++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Input/WindowsMouseDevice.cs b/src/Windows/Avalonia.Win32/Input/WindowsMouseDevice.cs index b1064ae25d..224ffdc3fd 100644 --- a/src/Windows/Avalonia.Win32/Input/WindowsMouseDevice.cs +++ b/src/Windows/Avalonia.Win32/Input/WindowsMouseDevice.cs @@ -8,15 +8,28 @@ namespace Avalonia.Win32.Input { class WindowsMouseDevice : MouseDevice { - public WindowsMouseDevice() : base(new WindowsMousePointer()) + private readonly IPointer _pointer; + public WindowsMouseDevice() : base(WindowsMousePointer.CreatePointer(out var pointer)) { - + _pointer = pointer; + } + + // Normally user should use IPointer.Capture instead of MouseDevice.Capture, + // But on Windows we need to handle WM_MOUSE capture manually without having access to the Pointer. + internal void Capture(IInputElement control) + { + _pointer.Capture(control); } - class WindowsMousePointer : Pointer + internal class WindowsMousePointer : Pointer { - public WindowsMousePointer() : base(Pointer.GetNextFreeId(),PointerType.Mouse, true) + private WindowsMousePointer() : base(Pointer.GetNextFreeId(),PointerType.Mouse, true) + { + } + + public static WindowsMousePointer CreatePointer(out WindowsMousePointer pointer) { + return pointer = new WindowsMousePointer(); } protected override void PlatformCapture(IInputElement element) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index 2f1a116af7..6d5cba9946 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -72,7 +72,7 @@ namespace Avalonia.Win32 private const WindowStyles WindowStateMask = (WindowStyles.WS_MAXIMIZE | WindowStyles.WS_MINIMIZE); private readonly TouchDevice _touchDevice; - private readonly MouseDevice _mouseDevice; + private readonly WindowsMouseDevice _mouseDevice; private readonly PenDevice _penDevice; private readonly ManagedDeferredRendererLock _rendererLock; private readonly FramebufferManager _framebuffer; @@ -689,10 +689,9 @@ namespace Avalonia.Win32 public void BeginMoveDrag(PointerPressedEventArgs e) { - _mouseDevice.Capture(null); + e.Pointer.Capture(null); DefWindowProc(_hwnd, (int)WindowsMessage.WM_NCLBUTTONDOWN, new IntPtr((int)HitTestValues.HTCAPTION), IntPtr.Zero); - e.Pointer.Capture(null); } public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e) @@ -702,7 +701,7 @@ namespace Avalonia.Win32 #if USE_MANAGED_DRAG _managedDrag.BeginResizeDrag(edge, ScreenToClient(MouseDevice.Position.ToPoint(_scaling))); #else - _mouseDevice.Capture(null); + e.Pointer.Capture(null); DefWindowProc(_hwnd, (int)WindowsMessage.WM_NCLBUTTONDOWN, new IntPtr((int)s_edgeLookup[edge]), IntPtr.Zero); #endif