Skip to content

Commit ad28b65

Browse files
authored
SwipeGestureRecognizer return actual detected directions (#20619)
* detected direction * unit test * fix
1 parent 6cb942e commit ad28b65

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

src/Controls/src/Core/SwipeGestureRecognizer.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,47 @@ bool ISwipeGestureController.DetectSwipe(View sender, SwipeDirection direction)
6565
var detected = false;
6666
var threshold = Threshold;
6767

68+
var detectedDirection = (SwipeDirection)(0);
69+
6870
if (direction.IsLeft())
6971
{
70-
detected |= _totalX < -threshold;
72+
if (_totalX < -threshold)
73+
{
74+
detected = true;
75+
detectedDirection |= SwipeDirection.Left;
76+
}
7177
}
7278

7379
if (direction.IsRight())
7480
{
75-
detected |= _totalX > threshold;
81+
if (_totalX > threshold)
82+
{
83+
detected = true;
84+
detectedDirection |= SwipeDirection.Right;
85+
}
7686
}
7787

7888
if (direction.IsDown())
7989
{
80-
detected |= _totalY > threshold;
90+
if (_totalY > threshold)
91+
{
92+
detected = true;
93+
detectedDirection |= SwipeDirection.Down;
94+
}
8195
}
8296

8397
if (direction.IsUp())
8498
{
85-
detected |= _totalY < -threshold;
99+
if (_totalY < -threshold)
100+
{
101+
detected = true;
102+
detectedDirection |= SwipeDirection.Up;
103+
}
86104
}
87105

88106
if (detected)
89107
{
90-
SendSwiped(sender, direction);
108+
SendSwiped(sender, detectedDirection);
91109
}
92110

93111
return detected;

src/Controls/tests/Core.UnitTests/Gestures/SwipeGestureRecognizerTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,39 @@ public void SwipeIgnoredIfBelowThresholdTest()
8383
((ISwipeGestureController)swipe).DetectSwipe(view, SwipeDirection.Up);
8484
Assert.False(detected);
8585
}
86+
87+
[Fact]
88+
public void SwipedEventDirectionMatchesTotalXTestWithFlags()
89+
{
90+
var view = new View();
91+
var swipe = new SwipeGestureRecognizer();
92+
93+
SwipeDirection direction = SwipeDirection.Up;
94+
swipe.Swiped += (object sender, SwipedEventArgs e) =>
95+
{
96+
direction = e.Direction;
97+
};
98+
99+
((ISwipeGestureController)swipe).SendSwipe(view, totalX: -150, totalY: 10);
100+
((ISwipeGestureController)swipe).DetectSwipe(view, SwipeDirection.Left | SwipeDirection.Right);
101+
Assert.Equal(SwipeDirection.Left, direction);
102+
}
103+
104+
[Fact]
105+
public void SwipedEventDirectionMatchesTotalYTestWithFlags()
106+
{
107+
var view = new View();
108+
var swipe = new SwipeGestureRecognizer();
109+
110+
SwipeDirection direction = SwipeDirection.Left;
111+
swipe.Swiped += (object sender, SwipedEventArgs e) =>
112+
{
113+
direction = e.Direction;
114+
};
115+
116+
((ISwipeGestureController)swipe).SendSwipe(view, totalX: 10, totalY: -150);
117+
((ISwipeGestureController)swipe).DetectSwipe(view, SwipeDirection.Up | SwipeDirection.Down);
118+
Assert.Equal(SwipeDirection.Up, direction);
119+
}
86120
}
87121
}

0 commit comments

Comments
 (0)