|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Windows.UI.Input.Preview.Injection; |
| 6 | + |
| 7 | +namespace CommunityToolkit.Tests.Input; |
| 8 | + |
| 9 | +public partial class InputSimulator |
| 10 | +{ |
| 11 | + public void StartTouch() |
| 12 | + { |
| 13 | + Assert.IsNotNull(_input); |
| 14 | + |
| 15 | + _input.InitializeTouchInjection( |
| 16 | + InjectedInputVisualizationMode.Default); |
| 17 | + } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Simulates a touch press on screen at the coordinates provided, in app-local coordinates. For instance use <code>App.ContentRoot.CoordinatesTo(element)</code>. |
| 21 | + /// </summary> |
| 22 | + /// <param name="point"></param> |
| 23 | + /// <returns></returns> |
| 24 | + public uint TouchDown(Point point) |
| 25 | + { |
| 26 | + // Create a unique pointer ID for the injected touch pointer. |
| 27 | + // Multiple input pointers would require more robust handling. |
| 28 | + uint pointerId = _currentPointerId++; |
| 29 | + |
| 30 | + var injectionPoint = TranslatePointForWindow(point); |
| 31 | + |
| 32 | + // Create a touch data point for pointer down. |
| 33 | + // Each element in the touch data list represents a single touch contact. |
| 34 | + // For this example, we're mirroring a single mouse pointer. |
| 35 | + List<InjectedInputTouchInfo> touchData = new() |
| 36 | + { |
| 37 | + new() |
| 38 | + { |
| 39 | + Contact = new InjectedInputRectangle |
| 40 | + { |
| 41 | + Left = 30, Top = 30, Bottom = 30, Right = 30 |
| 42 | + }, |
| 43 | + PointerInfo = new InjectedInputPointerInfo |
| 44 | + { |
| 45 | + PointerId = pointerId, |
| 46 | + PointerOptions = |
| 47 | + InjectedInputPointerOptions.PointerDown | |
| 48 | + InjectedInputPointerOptions.InContact | |
| 49 | + InjectedInputPointerOptions.New, |
| 50 | + TimeOffsetInMilliseconds = 0, |
| 51 | + PixelLocation = new InjectedInputPoint |
| 52 | + { |
| 53 | + PositionX = (int)injectionPoint.X , |
| 54 | + PositionY = (int)injectionPoint.Y |
| 55 | + } |
| 56 | + }, |
| 57 | + Pressure = 1.0, |
| 58 | + TouchParameters = |
| 59 | + InjectedInputTouchParameters.Pressure | |
| 60 | + InjectedInputTouchParameters.Contact |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + // Inject the touch input. |
| 65 | + _input.InjectTouchInput(touchData); |
| 66 | + |
| 67 | + return pointerId; |
| 68 | + } |
| 69 | + |
| 70 | + public void TouchMove(uint pointerId, int cX, int cY) |
| 71 | + { |
| 72 | + // Create a touch data point for pointer up. |
| 73 | + List<InjectedInputTouchInfo> touchData = new() |
| 74 | + { |
| 75 | + new() |
| 76 | + { |
| 77 | + Contact = new InjectedInputRectangle |
| 78 | + { |
| 79 | + Left = 30, Top = 30, Bottom = 30, Right = 30 |
| 80 | + }, |
| 81 | + PointerInfo = new InjectedInputPointerInfo |
| 82 | + { |
| 83 | + PointerId = pointerId, |
| 84 | + PointerOptions = |
| 85 | + InjectedInputPointerOptions.InRange | |
| 86 | + InjectedInputPointerOptions.InContact, |
| 87 | + TimeOffsetInMilliseconds = 0, |
| 88 | + PixelLocation = new InjectedInputPoint |
| 89 | + { |
| 90 | + PositionX = (int)cX , |
| 91 | + PositionY = (int)cY |
| 92 | + } |
| 93 | + }, |
| 94 | + Pressure = 1.0, |
| 95 | + TouchParameters = |
| 96 | + InjectedInputTouchParameters.Pressure | |
| 97 | + InjectedInputTouchParameters.Contact |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + // Inject the touch input. |
| 102 | + _input.InjectTouchInput(touchData); |
| 103 | + } |
| 104 | + |
| 105 | + public void TouchUp(uint pointerId) |
| 106 | + { |
| 107 | + Assert.IsNotNull(_input); |
| 108 | + |
| 109 | + // Create a touch data point for pointer up. |
| 110 | + List<InjectedInputTouchInfo> touchData = new() |
| 111 | + { |
| 112 | + new() |
| 113 | + { |
| 114 | + PointerInfo = new InjectedInputPointerInfo |
| 115 | + { |
| 116 | + PointerId = pointerId, |
| 117 | + PointerOptions = InjectedInputPointerOptions.PointerUp |
| 118 | + } |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + // Inject the touch input. |
| 123 | + _input.InjectTouchInput(touchData); |
| 124 | + } |
| 125 | + |
| 126 | + public void StopTouch() |
| 127 | + { |
| 128 | + // Shut down the virtual input device. |
| 129 | + _input.UninitializeTouchInjection(); |
| 130 | + } |
| 131 | +} |
0 commit comments