Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwipeGestureRecognizer return actual detected directions #20619

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading