Add support for touch events on Android

Currently on Android we treat all touch events as scroll events. Servo
is already capable of distinguishing between scroll-touches and regular
touch events (see `on_touch_move` in `components/compositor/touch.rs`),
so we should just be passing touch events through.

Servo however does not natively support fling gestures, so we continue
to use `GestureDetector` for that.
This commit is contained in:
Manish Goregaokar 2018-11-19 15:59:33 -08:00
parent f1dd31f704
commit 3bccb8c387
6 changed files with 158 additions and 19 deletions

View file

@ -50,6 +50,14 @@ public class JNIServo {
public native void scrollEnd(int dx, int dy, int x, int y);
public native void touchDown(float x, float y, int pointer_id);
public native void touchMove(float x, float y, int pointer_id);
public native void touchUp(float x, float y, int pointer_id);
public native void touchCancel(float x, float y, int pointer_id);
public native void pinchZoomStart(float factor, int x, int y);
public native void pinchZoom(float factor, int x, int y);

View file

@ -107,6 +107,22 @@ public class Servo {
mRunCallback.inGLThread(() -> mJNI.scrollEnd(dx, dy, x, y));
}
public void touchDown(float x, float y, int pointerId) {
mRunCallback.inGLThread(() -> mJNI.touchDown(x, y, pointerId));
}
public void touchMove(float x, float y, int pointerId) {
mRunCallback.inGLThread(() -> mJNI.touchMove(x, y, pointerId));
}
public void touchUp(float x, float y, int pointerId) {
mRunCallback.inGLThread(() -> mJNI.touchUp(x, y, pointerId));
}
public void touchCancel(float x, float y, int pointerId) {
mRunCallback.inGLThread(() -> mJNI.touchCancel(x, y, pointerId));
}
public void pinchZoomStart(float factor, int x, int y) {
mRunCallback.inGLThread(() -> mJNI.pinchZoomStart(factor, x, y));
}

View file

@ -53,7 +53,6 @@ public class ServoView extends GLSurfaceView
private int mLastY = 0;
private int mCurY = 0;
private boolean mFlinging;
private boolean mScrolling;
private boolean mZooming;
private float mZoomFactor = 1;
@ -199,7 +198,6 @@ public class ServoView extends GLSurfaceView
if (mFlinging && mScroller.isFinished()) {
mFlinging = false;
mScrolling = false;
mServo.scrollEnd(0, 0, mCurX, mCurY);
}
@ -215,7 +213,7 @@ public class ServoView extends GLSurfaceView
mLastX = mCurX;
mLastY = mCurY;
boolean scrollNecessary = mScrolling && (dx != 0 || dy != 0);
boolean scrollNecessary = mFlinging && (dx != 0 || dy != 0);
boolean zoomNecessary = mZooming && mZoomFactor != 1;
if (scrollNecessary) {
@ -231,7 +229,7 @@ public class ServoView extends GLSurfaceView
mServo.performUpdates();
}
if (mZooming || mScrolling || mAnimating) {
if (mZooming || mFlinging || mAnimating) {
Choreographer.getInstance().postFrameCallback(this);
} else {
mRedrawing = false;
@ -251,6 +249,8 @@ public class ServoView extends GLSurfaceView
mCurY = velocityY < 0 ? mPageHeight : 0;
mLastY = mCurY;
mScroller.fling(mCurX, mCurY, (int) velocityX, (int) velocityY, 0, mPageWidth, 0, mPageHeight);
mServo.scrollStart(0, 0, mCurX, mCurY);
startLooping();
return true;
}
@ -264,28 +264,31 @@ public class ServoView extends GLSurfaceView
mScaleGestureDetector.onTouchEvent(e);
int action = e.getActionMasked();
float x = e.getX();
float y = e.getY();
int pointerIndex = e.getActionIndex();
int pointerId = e.getPointerId(pointerIndex);
switch (action) {
case (MotionEvent.ACTION_DOWN):
mCurX = (int) e.getX();
mLastX = mCurX;
mCurY = (int) e.getY();
mLastY = mCurY;
mScroller.forceFinished(true);
mServo.touchDown(x, y, pointerId);
mFlinging = false;
mServo.scrollStart(0, 0, mCurX, mCurY);
mScrolling = true;
startLooping();
mScroller.forceFinished(true);
mCurX = (int) x;
mLastX = mCurX;
mCurY = (int) y;
mLastY = mCurY;
return true;
case (MotionEvent.ACTION_MOVE):
mCurX = (int) e.getX();
mCurY = (int) e.getY();
mCurX = (int) x;
mCurY = (int) y;
mServo.touchMove(x, y, pointerId);
return true;
case (MotionEvent.ACTION_UP):
mServo.touchUp(x, y, pointerId);
case (MotionEvent.ACTION_CANCEL):
if (!mFlinging) {
mScrolling = false;
mServo.scrollEnd(0, 0, mCurX, mCurY);
}
mServo.touchCancel(x, y, pointerId);
return true;
default:
return true;