Skip to content

Commit

Permalink
SwipeGestureRecognizer return actual detected directions (dotnet#20619)
Browse files Browse the repository at this point in the history
* detected direction

* unit test

* fix
  • Loading branch information
espenrl authored Mar 4, 2024
1 parent 6cb942e commit ad28b65
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Controls/src/Core/SwipeGestureRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,47 @@ bool ISwipeGestureController.DetectSwipe(View sender, SwipeDirection direction)
var detected = false;
var threshold = Threshold;

var detectedDirection = (SwipeDirection)(0);

if (direction.IsLeft())
{
detected |= _totalX < -threshold;
if (_totalX < -threshold)
{
detected = true;
detectedDirection |= SwipeDirection.Left;
}
}

if (direction.IsRight())
{
detected |= _totalX > threshold;
if (_totalX > threshold)
{
detected = true;
detectedDirection |= SwipeDirection.Right;
}
}

if (direction.IsDown())
{
detected |= _totalY > threshold;
if (_totalY > threshold)
{
detected = true;
detectedDirection |= SwipeDirection.Down;
}
}

if (direction.IsUp())
{
detected |= _totalY < -threshold;
if (_totalY < -threshold)
{
detected = true;
detectedDirection |= SwipeDirection.Up;
}
}

if (detected)
{
SendSwiped(sender, direction);
SendSwiped(sender, detectedDirection);
}

return detected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,39 @@ public void SwipeIgnoredIfBelowThresholdTest()
((ISwipeGestureController)swipe).DetectSwipe(view, SwipeDirection.Up);
Assert.False(detected);
}

[Fact]
public void SwipedEventDirectionMatchesTotalXTestWithFlags()
{
var view = new View();
var swipe = new SwipeGestureRecognizer();

SwipeDirection direction = SwipeDirection.Up;
swipe.Swiped += (object sender, SwipedEventArgs e) =>
{
direction = e.Direction;
};

((ISwipeGestureController)swipe).SendSwipe(view, totalX: -150, totalY: 10);
((ISwipeGestureController)swipe).DetectSwipe(view, SwipeDirection.Left | SwipeDirection.Right);
Assert.Equal(SwipeDirection.Left, direction);
}

[Fact]
public void SwipedEventDirectionMatchesTotalYTestWithFlags()
{
var view = new View();
var swipe = new SwipeGestureRecognizer();

SwipeDirection direction = SwipeDirection.Left;
swipe.Swiped += (object sender, SwipedEventArgs e) =>
{
direction = e.Direction;
};

((ISwipeGestureController)swipe).SendSwipe(view, totalX: 10, totalY: -150);
((ISwipeGestureController)swipe).DetectSwipe(view, SwipeDirection.Up | SwipeDirection.Down);
Assert.Equal(SwipeDirection.Up, direction);
}
}
}

0 comments on commit ad28b65

Please sign in to comment.