MouseTestHelper.cs 4.9 KB

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