TouchTestHelper.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Avalonia.Input;
  2. using Avalonia.Interactivity;
  3. using Avalonia.VisualTree;
  4. namespace Avalonia.UnitTests
  5. {
  6. public class TouchTestHelper
  7. {
  8. private readonly Pointer _pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Touch, true);
  9. private ulong _nextStamp = 1;
  10. private ulong Timestamp() => _nextStamp++;
  11. public IInputElement Captured => _pointer.Captured;
  12. public void Down(Interactive target, Point position = default, KeyModifiers modifiers = default)
  13. {
  14. Down(target, target, position, modifiers);
  15. }
  16. public void Down(Interactive target, Interactive source, Point position = default, KeyModifiers modifiers = default)
  17. {
  18. _pointer.Capture((IInputElement)target);
  19. source.RaiseEvent(new PointerPressedEventArgs(source, _pointer, (Visual)source, position, Timestamp(),
  20. new(RawInputModifiers.LeftMouseButton, PointerUpdateKind.LeftButtonPressed),
  21. modifiers));
  22. }
  23. public void Move(Interactive target, in Point position, KeyModifiers modifiers = default) => Move(target, target, position, modifiers);
  24. public void Move(Interactive target, Interactive source, in Point position, KeyModifiers modifiers = default)
  25. {
  26. var e = new PointerEventArgs(InputElement.PointerMovedEvent, source, _pointer, (Visual)target, position,
  27. Timestamp(), new(RawInputModifiers.LeftMouseButton, PointerUpdateKind.Other), modifiers);
  28. if (_pointer.CapturedGestureRecognizer != null)
  29. _pointer.CapturedGestureRecognizer.PointerMovedInternal(e);
  30. else
  31. target.RaiseEvent(e);
  32. }
  33. public void Up(Interactive target, Point position = default, KeyModifiers modifiers = default)
  34. => Up(target, target, position, modifiers);
  35. public void Up(Interactive target, Interactive source, Point position = default, KeyModifiers modifiers = default)
  36. {
  37. var e = new PointerReleasedEventArgs(source, _pointer, (Visual)target, position, Timestamp(),
  38. new(RawInputModifiers.None, PointerUpdateKind.LeftButtonReleased), modifiers, MouseButton.Left);
  39. if (_pointer.CapturedGestureRecognizer != null)
  40. _pointer.CapturedGestureRecognizer.PointerReleasedInternal(e);
  41. else
  42. source.RaiseEvent(e);
  43. Cancel();
  44. }
  45. public void Tap(Interactive target, Point position = default, KeyModifiers modifiers = default)
  46. => Tap(target, target, position, modifiers);
  47. public void Tap(Interactive target, Interactive source, Point position = default, KeyModifiers modifiers = default)
  48. {
  49. Down(target, source, position, modifiers);
  50. Up(target, source, position, modifiers);
  51. }
  52. public void Cancel()
  53. {
  54. _pointer.Capture(null);
  55. _pointer.CaptureGestureRecognizer(null);
  56. _pointer.IsGestureRecognitionSkipped = false;
  57. }
  58. }
  59. }