Skip to content

Commit

Permalink
Improve motion event handling after long press
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fornwall committed Nov 29, 2015
1 parent b54c790 commit bad6712
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions app/src/main/java/com/termux/view/GestureAndScaleRecognizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -52,6 +53,7 @@ public boolean onDown(MotionEvent e) {
@Override
public void onLongPress(MotionEvent e) {
mListener.onLongPress(e);
isAfterLongPress = true;
}
}, null, true /* ignoreMultitouch */);

Expand Down Expand Up @@ -88,13 +90,22 @@ 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;
}
}

public boolean isInProgress() {
return mScaleDetector.isInProgress();
}

}
}

0 comments on commit bad6712

Please sign in to comment.