mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
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:
parent
f1dd31f704
commit
3bccb8c387
6 changed files with 158 additions and 19 deletions
|
@ -9,7 +9,7 @@ use servo::embedder_traits::resources::{self, Resource};
|
||||||
use servo::embedder_traits::EmbedderMsg;
|
use servo::embedder_traits::EmbedderMsg;
|
||||||
use servo::euclid::{TypedPoint2D, TypedScale, TypedSize2D, TypedVector2D};
|
use servo::euclid::{TypedPoint2D, TypedScale, TypedSize2D, TypedVector2D};
|
||||||
use servo::msg::constellation_msg::TraversalDirection;
|
use servo::msg::constellation_msg::TraversalDirection;
|
||||||
use servo::script_traits::{MouseButton, TouchEventType};
|
use servo::script_traits::{MouseButton, TouchEventType, TouchId};
|
||||||
use servo::servo_config::opts;
|
use servo::servo_config::opts;
|
||||||
use servo::servo_config::prefs::{PrefValue, PREFS};
|
use servo::servo_config::prefs::{PrefValue, PREFS};
|
||||||
use servo::servo_url::ServoUrl;
|
use servo::servo_url::ServoUrl;
|
||||||
|
@ -312,6 +312,46 @@ impl ServoGlue {
|
||||||
self.process_event(event)
|
self.process_event(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Touch event: press down
|
||||||
|
pub fn touch_down(&mut self, x: f32, y: f32, pointer_id: i32) -> Result<(), &'static str> {
|
||||||
|
let event = WindowEvent::Touch(
|
||||||
|
TouchEventType::Down,
|
||||||
|
TouchId(pointer_id),
|
||||||
|
TypedPoint2D::new(x as f32, y as f32),
|
||||||
|
);
|
||||||
|
self.process_event(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Touch event: move touching finger
|
||||||
|
pub fn touch_move(&mut self, x: f32, y: f32, pointer_id: i32) -> Result<(), &'static str> {
|
||||||
|
let event = WindowEvent::Touch(
|
||||||
|
TouchEventType::Move,
|
||||||
|
TouchId(pointer_id),
|
||||||
|
TypedPoint2D::new(x as f32, y as f32),
|
||||||
|
);
|
||||||
|
self.process_event(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Touch event: Lift touching finger
|
||||||
|
pub fn touch_up(&mut self, x: f32, y: f32, pointer_id: i32) -> Result<(), &'static str> {
|
||||||
|
let event = WindowEvent::Touch(
|
||||||
|
TouchEventType::Up,
|
||||||
|
TouchId(pointer_id),
|
||||||
|
TypedPoint2D::new(x as f32, y as f32),
|
||||||
|
);
|
||||||
|
self.process_event(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cancel touch event
|
||||||
|
pub fn touch_cancel(&mut self, x: f32, y: f32, pointer_id: i32) -> Result<(), &'static str> {
|
||||||
|
let event = WindowEvent::Touch(
|
||||||
|
TouchEventType::Cancel,
|
||||||
|
TouchId(pointer_id),
|
||||||
|
TypedPoint2D::new(x as f32, y as f32),
|
||||||
|
);
|
||||||
|
self.process_event(event)
|
||||||
|
}
|
||||||
|
|
||||||
/// Start pinchzoom.
|
/// Start pinchzoom.
|
||||||
/// x/y are pinch origin coordinates.
|
/// x/y are pinch origin coordinates.
|
||||||
pub fn pinchzoom_start(&mut self, factor: f32, _x: u32, _y: u32) -> Result<(), &'static str> {
|
pub fn pinchzoom_start(&mut self, factor: f32, _x: u32, _y: u32) -> Result<(), &'static str> {
|
||||||
|
|
|
@ -205,6 +205,30 @@ pub extern "C" fn scroll(dx: i32, dy: i32, x: i32, y: i32) {
|
||||||
call(|s| s.scroll(dx as i32, dy as i32, x as u32, y as u32));
|
call(|s| s.scroll(dx as i32, dy as i32, x as u32, y as u32));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn touch_down(x: f32, y: f32, pointer_id: i32) {
|
||||||
|
debug!("touch down");
|
||||||
|
call(|s| s.touch_down(x, y, pointer_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn touch_up(x: f32, y: f32, pointer_id: i32) {
|
||||||
|
debug!("touch up");
|
||||||
|
call(|s| s.touch_up(x, y, pointer_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn touch_move(x: f32, y: f32, pointer_id: i32) {
|
||||||
|
debug!("touch move");
|
||||||
|
call(|s| s.touch_move(x, y, pointer_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn touch_cancel(x: f32, y: f32, pointer_id: i32) {
|
||||||
|
debug!("touch cancel");
|
||||||
|
call(|s| s.touch_cancel(x, y, pointer_id));
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn pinchzoom_start(factor: f32, x: i32, y: i32) {
|
pub extern "C" fn pinchzoom_start(factor: f32, x: i32, y: i32) {
|
||||||
debug!("pinchzoom_start");
|
debug!("pinchzoom_start");
|
||||||
|
|
|
@ -230,6 +230,54 @@ pub fn Java_org_mozilla_servoview_JNIServo_scroll(
|
||||||
call(&env, |s| s.scroll(dx as i32, dy as i32, x as u32, y as u32));
|
call(&env, |s| s.scroll(dx as i32, dy as i32, x as u32, y as u32));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn Java_org_mozilla_servoview_JNIServo_touchDown(
|
||||||
|
env: JNIEnv,
|
||||||
|
_: JClass,
|
||||||
|
x: jfloat,
|
||||||
|
y: jfloat,
|
||||||
|
pointer_id: jint,
|
||||||
|
) {
|
||||||
|
debug!("touchDown");
|
||||||
|
call(&env, |s| s.touch_down(x, y, pointer_id as i32));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn Java_org_mozilla_servoview_JNIServo_touchUp(
|
||||||
|
env: JNIEnv,
|
||||||
|
_: JClass,
|
||||||
|
x: jfloat,
|
||||||
|
y: jfloat,
|
||||||
|
pointer_id: jint,
|
||||||
|
) {
|
||||||
|
debug!("touchUp");
|
||||||
|
call(&env, |s| s.touch_up(x, y, pointer_id as i32));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn Java_org_mozilla_servoview_JNIServo_touchMove(
|
||||||
|
env: JNIEnv,
|
||||||
|
_: JClass,
|
||||||
|
x: jfloat,
|
||||||
|
y: jfloat,
|
||||||
|
pointer_id: jint,
|
||||||
|
) {
|
||||||
|
debug!("touchMove");
|
||||||
|
call(&env, |s| s.touch_move(x, y, pointer_id as i32));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn Java_org_mozilla_servoview_JNIServo_touchCancel(
|
||||||
|
env: JNIEnv,
|
||||||
|
_: JClass,
|
||||||
|
x: jfloat,
|
||||||
|
y: jfloat,
|
||||||
|
pointer_id: jint,
|
||||||
|
) {
|
||||||
|
debug!("touchCancel");
|
||||||
|
call(&env, |s| s.touch_cancel(x, y, pointer_id as i32));
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn Java_org_mozilla_servoview_JNIServo_pinchZoomStart(
|
pub fn Java_org_mozilla_servoview_JNIServo_pinchZoomStart(
|
||||||
env: JNIEnv,
|
env: JNIEnv,
|
||||||
|
|
|
@ -50,6 +50,14 @@ public class JNIServo {
|
||||||
|
|
||||||
public native void scrollEnd(int dx, int dy, int x, int y);
|
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 pinchZoomStart(float factor, int x, int y);
|
||||||
|
|
||||||
public native void pinchZoom(float factor, int x, int y);
|
public native void pinchZoom(float factor, int x, int y);
|
||||||
|
|
|
@ -107,6 +107,22 @@ public class Servo {
|
||||||
mRunCallback.inGLThread(() -> mJNI.scrollEnd(dx, dy, x, y));
|
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) {
|
public void pinchZoomStart(float factor, int x, int y) {
|
||||||
mRunCallback.inGLThread(() -> mJNI.pinchZoomStart(factor, x, y));
|
mRunCallback.inGLThread(() -> mJNI.pinchZoomStart(factor, x, y));
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,6 @@ public class ServoView extends GLSurfaceView
|
||||||
private int mLastY = 0;
|
private int mLastY = 0;
|
||||||
private int mCurY = 0;
|
private int mCurY = 0;
|
||||||
private boolean mFlinging;
|
private boolean mFlinging;
|
||||||
private boolean mScrolling;
|
|
||||||
|
|
||||||
private boolean mZooming;
|
private boolean mZooming;
|
||||||
private float mZoomFactor = 1;
|
private float mZoomFactor = 1;
|
||||||
|
@ -199,7 +198,6 @@ public class ServoView extends GLSurfaceView
|
||||||
|
|
||||||
if (mFlinging && mScroller.isFinished()) {
|
if (mFlinging && mScroller.isFinished()) {
|
||||||
mFlinging = false;
|
mFlinging = false;
|
||||||
mScrolling = false;
|
|
||||||
mServo.scrollEnd(0, 0, mCurX, mCurY);
|
mServo.scrollEnd(0, 0, mCurX, mCurY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,7 +213,7 @@ public class ServoView extends GLSurfaceView
|
||||||
mLastX = mCurX;
|
mLastX = mCurX;
|
||||||
mLastY = mCurY;
|
mLastY = mCurY;
|
||||||
|
|
||||||
boolean scrollNecessary = mScrolling && (dx != 0 || dy != 0);
|
boolean scrollNecessary = mFlinging && (dx != 0 || dy != 0);
|
||||||
boolean zoomNecessary = mZooming && mZoomFactor != 1;
|
boolean zoomNecessary = mZooming && mZoomFactor != 1;
|
||||||
|
|
||||||
if (scrollNecessary) {
|
if (scrollNecessary) {
|
||||||
|
@ -231,7 +229,7 @@ public class ServoView extends GLSurfaceView
|
||||||
mServo.performUpdates();
|
mServo.performUpdates();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mZooming || mScrolling || mAnimating) {
|
if (mZooming || mFlinging || mAnimating) {
|
||||||
Choreographer.getInstance().postFrameCallback(this);
|
Choreographer.getInstance().postFrameCallback(this);
|
||||||
} else {
|
} else {
|
||||||
mRedrawing = false;
|
mRedrawing = false;
|
||||||
|
@ -251,6 +249,8 @@ public class ServoView extends GLSurfaceView
|
||||||
mCurY = velocityY < 0 ? mPageHeight : 0;
|
mCurY = velocityY < 0 ? mPageHeight : 0;
|
||||||
mLastY = mCurY;
|
mLastY = mCurY;
|
||||||
mScroller.fling(mCurX, mCurY, (int) velocityX, (int) velocityY, 0, mPageWidth, 0, mPageHeight);
|
mScroller.fling(mCurX, mCurY, (int) velocityX, (int) velocityY, 0, mPageWidth, 0, mPageHeight);
|
||||||
|
mServo.scrollStart(0, 0, mCurX, mCurY);
|
||||||
|
startLooping();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,28 +264,31 @@ public class ServoView extends GLSurfaceView
|
||||||
mScaleGestureDetector.onTouchEvent(e);
|
mScaleGestureDetector.onTouchEvent(e);
|
||||||
|
|
||||||
int action = e.getActionMasked();
|
int action = e.getActionMasked();
|
||||||
|
float x = e.getX();
|
||||||
|
|
||||||
|
float y = e.getY();
|
||||||
|
|
||||||
|
int pointerIndex = e.getActionIndex();
|
||||||
|
int pointerId = e.getPointerId(pointerIndex);
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case (MotionEvent.ACTION_DOWN):
|
case (MotionEvent.ACTION_DOWN):
|
||||||
mCurX = (int) e.getX();
|
mServo.touchDown(x, y, pointerId);
|
||||||
mLastX = mCurX;
|
|
||||||
mCurY = (int) e.getY();
|
|
||||||
mLastY = mCurY;
|
|
||||||
mScroller.forceFinished(true);
|
|
||||||
mFlinging = false;
|
mFlinging = false;
|
||||||
mServo.scrollStart(0, 0, mCurX, mCurY);
|
mScroller.forceFinished(true);
|
||||||
mScrolling = true;
|
mCurX = (int) x;
|
||||||
startLooping();
|
mLastX = mCurX;
|
||||||
|
mCurY = (int) y;
|
||||||
|
mLastY = mCurY;
|
||||||
return true;
|
return true;
|
||||||
case (MotionEvent.ACTION_MOVE):
|
case (MotionEvent.ACTION_MOVE):
|
||||||
mCurX = (int) e.getX();
|
mCurX = (int) x;
|
||||||
mCurY = (int) e.getY();
|
mCurY = (int) y;
|
||||||
|
mServo.touchMove(x, y, pointerId);
|
||||||
return true;
|
return true;
|
||||||
case (MotionEvent.ACTION_UP):
|
case (MotionEvent.ACTION_UP):
|
||||||
|
mServo.touchUp(x, y, pointerId);
|
||||||
case (MotionEvent.ACTION_CANCEL):
|
case (MotionEvent.ACTION_CANCEL):
|
||||||
if (!mFlinging) {
|
mServo.touchCancel(x, y, pointerId);
|
||||||
mScrolling = false;
|
|
||||||
mServo.scrollEnd(0, 0, mCurX, mCurY);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue