MouseTestHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Avalonia.Input;
  2. using Avalonia.Interactivity;
  3. using Avalonia.VisualTree;
  4. namespace Avalonia.UnitTests
  5. {
  6. public class MouseTestHelper
  7. {
  8. private Pointer _pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
  9. private ulong _nextStamp = 1;
  10. private ulong Timestamp() => _nextStamp++;
  11. private InputModifiers _pressedButtons;
  12. public IInputElement Captured => _pointer.Captured;
  13. InputModifiers Convert(MouseButton mouseButton)
  14. => (mouseButton == MouseButton.Left ? InputModifiers.LeftMouseButton
  15. : mouseButton == MouseButton.Middle ? InputModifiers.MiddleMouseButton
  16. : mouseButton == MouseButton.Right ? InputModifiers.RightMouseButton : InputModifiers.None);
  17. int ButtonCount(PointerPointProperties props)
  18. {
  19. var rv = 0;
  20. if (props.IsLeftButtonPressed)
  21. rv++;
  22. if (props.IsMiddleButtonPressed)
  23. rv++;
  24. if (props.IsRightButtonPressed)
  25. rv++;
  26. return rv;
  27. }
  28. private MouseButton _pressedButton;
  29. KeyModifiers GetModifiers(InputModifiers modifiers) =>
  30. (KeyModifiers)((int)modifiers & (int)RawInputModifiers.KeyboardMask);
  31. public void Down(IInteractive target, MouseButton mouseButton = MouseButton.Left, Point position = default,
  32. InputModifiers modifiers = default, int clickCount = 1)
  33. {
  34. Down(target, target, mouseButton, position, modifiers, clickCount);
  35. }
  36. public void Down(IInteractive target, IInteractive source, MouseButton mouseButton = MouseButton.Left,
  37. Point position = default, InputModifiers modifiers = default, int clickCount = 1)
  38. {
  39. _pressedButtons |= Convert(mouseButton);
  40. var props = new PointerPointProperties((RawInputModifiers)_pressedButtons,
  41. mouseButton == MouseButton.Left ? PointerUpdateKind.LeftButtonPressed
  42. : mouseButton == MouseButton.Middle ? PointerUpdateKind.MiddleButtonPressed
  43. : mouseButton == MouseButton.Right ? PointerUpdateKind.RightButtonPressed : PointerUpdateKind.Other
  44. );
  45. if (ButtonCount(props) > 1)
  46. Move(target, source, position);
  47. else
  48. {
  49. _pressedButton = mouseButton;
  50. _pointer.Capture((IInputElement)target);
  51. target.RaiseEvent(new PointerPressedEventArgs(source, _pointer, (IVisual)source, position, Timestamp(), props,
  52. GetModifiers(modifiers), clickCount));
  53. }
  54. }
  55. public void Move(IInteractive target, in Point position, InputModifiers modifiers = default) => Move(target, target, position, modifiers);
  56. public void Move(IInteractive target, IInteractive source, in Point position, InputModifiers modifiers = default)
  57. {
  58. target.RaiseEvent(new PointerEventArgs(InputElement.PointerMovedEvent, source, _pointer, (IVisual)target, position,
  59. Timestamp(), new PointerPointProperties((RawInputModifiers)_pressedButtons, PointerUpdateKind.Other), GetModifiers(modifiers)));
  60. }
  61. public void Up(IInteractive target, MouseButton mouseButton = MouseButton.Left, Point position = default,
  62. InputModifiers modifiers = default)
  63. => Up(target, target, mouseButton, position, modifiers);
  64. public void Up(IInteractive target, IInteractive source, MouseButton mouseButton = MouseButton.Left,
  65. Point position = default, InputModifiers modifiers = default)
  66. {
  67. var conv = Convert(mouseButton);
  68. _pressedButtons = (_pressedButtons | conv) ^ conv;
  69. var props = new PointerPointProperties((RawInputModifiers)_pressedButtons,
  70. mouseButton == MouseButton.Left ? PointerUpdateKind.LeftButtonReleased
  71. : mouseButton == MouseButton.Middle ? PointerUpdateKind.MiddleButtonReleased
  72. : mouseButton == MouseButton.Right ? PointerUpdateKind.RightButtonReleased : PointerUpdateKind.Other
  73. );
  74. if (ButtonCount(props) == 0)
  75. {
  76. target.RaiseEvent(new PointerReleasedEventArgs(source, _pointer, (IVisual)target, position,
  77. Timestamp(), props, GetModifiers(modifiers), _pressedButton));
  78. _pointer.Capture(null);
  79. }
  80. else
  81. Move(target, source, position);
  82. }
  83. public void Click(IInteractive target, MouseButton button = MouseButton.Left, Point position = default,
  84. InputModifiers modifiers = default)
  85. => Click(target, target, button, position, modifiers);
  86. public void Click(IInteractive target, IInteractive source, MouseButton button = MouseButton.Left,
  87. Point position = default, InputModifiers modifiers = default)
  88. {
  89. Down(target, source, button, position, modifiers);
  90. Up(target, source, button, position, modifiers);
  91. }
  92. public void Enter(IInteractive target)
  93. {
  94. target.RaiseEvent(new PointerEventArgs(InputElement.PointerEnterEvent, target, _pointer, (IVisual)target, default,
  95. Timestamp(), new PointerPointProperties((RawInputModifiers)_pressedButtons, PointerUpdateKind.Other), KeyModifiers.None));
  96. }
  97. public void Leave(IInteractive target)
  98. {
  99. target.RaiseEvent(new PointerEventArgs(InputElement.PointerLeaveEvent, target, _pointer, (IVisual)target, default,
  100. Timestamp(), new PointerPointProperties((RawInputModifiers)_pressedButtons, PointerUpdateKind.Other), KeyModifiers.None));
  101. }
  102. }
  103. }