You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/src/Connected.Components/Utilities/TouchGestures.cs

77 lines
2.1 KiB

using Connected.Enums;
using Microsoft.AspNetCore.Components.Web;
using System.Reflection.Metadata.Ecma335;
namespace Connected.Utilities;
public static class TouchGestures
{
public static TouchGesture GetTouchGesture(TouchPoint[] TouchPoints, DateTime GestureStart)
{
try
{
if (TouchPoints is not null)
{
/* handling tap
* TouchPoints --> array with one point inside suggesting user tapped the screen
*/
if (TouchPoints.Length == 1)
{
var point = TouchPoints[0];
return TouchGesture.Tap;
}
/* handling swipe with one finger
* TouchPoints --> array of exactly two points start and end point, suggesting one finger was used and dragged across the screen
*/
if (TouchPoints.Length == 2)
{
var startPoint = TouchPoints[0];
var endPoint = TouchPoints[1];
const double swipeThreshold = 0.8;
var diffX = startPoint.ClientX - endPoint.ClientX;
var diffY = startPoint.ClientY - endPoint.ClientY;
var diffTime = DateTime.Now - GestureStart;
var velocityX = Math.Abs(diffX / diffTime.Milliseconds);
var velocityY = Math.Abs(diffY / diffTime.Milliseconds);
//dismiss touch gestures if user slowly touched the screen, preventing accidental or unwanted touches
if (velocityX < swipeThreshold && velocityY < swipeThreshold)
return TouchGesture.None;
//preventing false gesture detection if the swipe is too diagonal
if (Math.Abs(velocityX - velocityY) < 0.5)
return TouchGesture.None;
if (velocityX >= swipeThreshold)
{
if (diffX < 0)
return TouchGesture.SwipeRight;
else
return TouchGesture.SwipeLeft;
}
if (velocityY >= swipeThreshold)
{
if (diffX < 0)
return TouchGesture.SwipeUp;
else
return TouchGesture.SwipeDown;
}
}
/* handling zoom, pinch, mutifinger swipe
* TouchPoints --> array of more than 2 points, suggesting two or more fingers were used to make a gesture
*/
if (TouchPoints.Length > 2)
{
//TODO
}
}
return TouchGesture.None;
}
catch
{
return TouchGesture.None;
}
}
}