From bad6712338a9fd98c1975a46d26a8b94d28b02fb Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Sun, 29 Nov 2015 10:56:31 +0100 Subject: [PATCH] Improve motion event handling after long press When mouse reporting is enabled, do not send mouse events on up after a long press, since that causes e.g. the cursor to move in vim when lifting the finger after long pressing for the menu. --- .../view/GestureAndScaleRecognizer.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/termux/view/GestureAndScaleRecognizer.java b/app/src/main/java/com/termux/view/GestureAndScaleRecognizer.java index 861adbd418..0374436022 100644 --- a/app/src/main/java/com/termux/view/GestureAndScaleRecognizer.java +++ b/app/src/main/java/com/termux/view/GestureAndScaleRecognizer.java @@ -6,7 +6,7 @@ import android.view.ScaleGestureDetector; /** A combination of {@link GestureDetector} and {@link ScaleGestureDetector}. */ -public class GestureAndScaleRecognizer { +public final class GestureAndScaleRecognizer { public interface Listener { boolean onSingleTapUp(MotionEvent e); @@ -29,6 +29,7 @@ public interface Listener { private final GestureDetector mGestureDetector; private final ScaleGestureDetector mScaleDetector; final Listener mListener; + boolean isAfterLongPress; public GestureAndScaleRecognizer(Context context, Listener listener) { mListener = listener; @@ -52,6 +53,7 @@ public boolean onDown(MotionEvent e) { @Override public void onLongPress(MotionEvent e) { mListener.onLongPress(e); + isAfterLongPress = true; } }, null, true /* ignoreMultitouch */); @@ -88,8 +90,17 @@ public boolean onScale(ScaleGestureDetector detector) { public void onTouchEvent(MotionEvent event) { mGestureDetector.onTouchEvent(event); mScaleDetector.onTouchEvent(event); - if (event.getAction() == MotionEvent.ACTION_UP) { - mListener.onUp(event); + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + isAfterLongPress = false; + break; + case MotionEvent.ACTION_UP: + if (!isAfterLongPress) { + // This behaviour is desired when in e.g. vim with mouse events, where we do not + // want to move the cursor when lifting finger after a long press. + mListener.onUp(event); + } + break; } } @@ -97,4 +108,4 @@ public boolean isInProgress() { return mScaleDetector.isInProgress(); } -} \ No newline at end of file +}