Skip to content

Commit

Permalink
Fix secondary tap on Android (#10619)
Browse files Browse the repository at this point in the history
Co-authored-by: Rachel Kang <rachelkang@microsoft.com>
  • Loading branch information
2 people authored and rmarinho committed Jan 27, 2023
1 parent 7c6a87a commit 4ade8f8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class TapGestureGalleryPage : BasePage
Label relativeToToggleButtonPosition = new Label();
Label relativeToContainerPosition = new Label();
Button toggleButton;
int clickCount = 0;

public TapGestureGalleryPage()
{
Expand Down Expand Up @@ -184,6 +185,8 @@ void OnTapped(object sender, System.EventArgs e)
void HandleTapCommand(Color backgroundColor)
{
changeColorBoxView.BackgroundColor = backgroundColor;
changeColorBoxView.Text = $"Tap Gesture Gallery: {clickCount}";
clickCount++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ namespace Microsoft.Maui.Controls.Platform;

internal static class MotionEventExtensions
{
public static bool IsSecondary(this MotionEvent me)
{
var buttonState = me?.ButtonState ?? MotionEventButtonState.Primary;

return
buttonState == MotionEventButtonState.Secondary ||
buttonState == MotionEventButtonState.StylusSecondary;
}

internal static Point? CalculatePosition(this MotionEvent? e, IElement? sourceElement, IElement? relativeElement)
{
var context = sourceElement?.Handler?.MauiContext?.Context;
Expand Down
23 changes: 22 additions & 1 deletion src/Controls/src/Core/Platform/Android/InnerGestureListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ bool GestureDetector.IOnDoubleTapListener.OnSingleTapConfirmed(MotionEvent e)
if (_disposed)
return false;

if (!HasDoubleTapHandler())
// The secondary button state only surfaces inside `OnSingleTapConfirmed`
// Inside 'OnSingleTap' the e.ButtonState doesn't indicate a secondary click
// So, if the gesture recognizer here has primary/secondary we want to ignore
// the _tapDelegate call that accounts for secondary clicks
if (!HasDoubleTapHandler() &&
(!e.IsSecondary() || HasSingleTapHandlerWithPrimaryAndSecondary()))
{
// We're not worried about double-tap, so OnSingleTapUp has already run the delegate
// there's nothing for us to do here
Expand Down Expand Up @@ -236,6 +241,21 @@ internal void EndScrolling()
_isScrolling = false;
}

bool HasSingleTapHandlerWithPrimaryAndSecondary()
{
if (_tapGestureRecognizers == null)
return false;

var check = ButtonsMask.Primary | ButtonsMask.Secondary;
foreach (var gesture in _tapGestureRecognizers(1))
{
if ((gesture.Buttons & check) == check)
return true;
}

return false;
}

bool HasDoubleTapHandler()
{
if (_tapGestureRecognizers == null)
Expand All @@ -247,6 +267,7 @@ bool HasSingleTapHandler()
{
if (_tapGestureRecognizers == null)
return false;

return _tapGestureRecognizers(1).Any();
}
}
Expand Down

0 comments on commit 4ade8f8

Please sign in to comment.