mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
W3CPointerEvents: include screen coordinates in pointer events (#38222)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38222 Changelog: [Android] [Fixed] - W3CPointerEvents: include screen coordinates in pointer events The [spec](https://www.w3.org/TR/uievents/#idl-mouseevent) says there should be properties on mouse events (and hence pointer events) indicating the screen coordinates of the event (i.e. coords relative to screen of the device). This change adds those properties. Reviewed By: javache Differential Revision: D47162962 fbshipit-source-id: 5bb9780882459b3550bbac792b7d7cf9f5c10860
This commit is contained in:
committed by
Facebook GitHub Bot
parent
5a57c0bf3a
commit
3460ff5d04
+23
@@ -50,6 +50,8 @@ public class JSPointerDispatcher {
|
||||
private int mLastButtonState = 0;
|
||||
private final ViewGroup mRootViewGroup;
|
||||
|
||||
private static final int[] sRootScreenCoords = {0, 0};
|
||||
|
||||
// Set globally for hover interactions, referenced for coalescing hover events
|
||||
|
||||
public JSPointerDispatcher(ViewGroup viewGroup) {
|
||||
@@ -255,10 +257,21 @@ public class JSPointerDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
private float[] eventCoordsToScreenCoords(float[] eventCoords) {
|
||||
float[] screenCoords = new float[2];
|
||||
mRootViewGroup.getLocationOnScreen(sRootScreenCoords);
|
||||
|
||||
screenCoords[0] = eventCoords[0] + sRootScreenCoords[0];
|
||||
screenCoords[1] = eventCoords[1] + sRootScreenCoords[1];
|
||||
|
||||
return screenCoords;
|
||||
}
|
||||
|
||||
private PointerEventState createEventState(int activePointerId, MotionEvent motionEvent) {
|
||||
Map<Integer, float[]> offsetByPointerId = new HashMap<Integer, float[]>();
|
||||
Map<Integer, List<ViewTarget>> hitPathByPointerId = new HashMap<Integer, List<ViewTarget>>();
|
||||
Map<Integer, float[]> eventCoordinatesByPointerId = new HashMap<Integer, float[]>();
|
||||
Map<Integer, float[]> screenCoordinatesByPointerId = new HashMap<Integer, float[]>();
|
||||
for (int index = 0; index < motionEvent.getPointerCount(); index++) {
|
||||
float[] offsetCoordinates = new float[2];
|
||||
float[] eventCoordinates = new float[] {motionEvent.getX(index), motionEvent.getY(index)};
|
||||
@@ -270,6 +283,7 @@ public class JSPointerDispatcher {
|
||||
offsetByPointerId.put(pointerId, offsetCoordinates);
|
||||
hitPathByPointerId.put(pointerId, hitPath);
|
||||
eventCoordinatesByPointerId.put(pointerId, eventCoordinates);
|
||||
screenCoordinatesByPointerId.put(pointerId, eventCoordsToScreenCoords(eventCoordinates));
|
||||
}
|
||||
|
||||
int surfaceId = UIManagerHelper.getSurfaceId(mRootViewGroup);
|
||||
@@ -282,6 +296,7 @@ public class JSPointerDispatcher {
|
||||
offsetByPointerId,
|
||||
hitPathByPointerId,
|
||||
eventCoordinatesByPointerId,
|
||||
screenCoordinatesByPointerId,
|
||||
mHoveringPointerIds); // Creates a copy of hovering pointer ids, as they may be updated
|
||||
}
|
||||
|
||||
@@ -673,6 +688,8 @@ public class JSPointerDispatcher {
|
||||
private PointerEventState normalizeToRoot(PointerEventState original, float rootX, float rootY) {
|
||||
Map<Integer, float[]> newOffsets = new HashMap<>(original.getOffsetByPointerId());
|
||||
Map<Integer, float[]> newEventCoords = new HashMap<>(original.getEventCoordinatesByPointerId());
|
||||
Map<Integer, float[]> newScreenCoords =
|
||||
new HashMap<>(original.getScreenCoordinatesByPointerId());
|
||||
|
||||
float[] rootOffset = {rootX, rootY};
|
||||
for (Map.Entry<Integer, float[]> offsetEntry : newOffsets.entrySet()) {
|
||||
@@ -684,6 +701,11 @@ public class JSPointerDispatcher {
|
||||
eventCoordsEntry.setValue(zeroOffset);
|
||||
}
|
||||
|
||||
float[] screenCoords = eventCoordsToScreenCoords(rootOffset);
|
||||
for (Map.Entry<Integer, float[]> screenCoordsEntry : newScreenCoords.entrySet()) {
|
||||
screenCoordsEntry.setValue(screenCoords);
|
||||
}
|
||||
|
||||
return new PointerEventState(
|
||||
original.getPrimaryPointerId(),
|
||||
original.getActivePointerId(),
|
||||
@@ -692,6 +714,7 @@ public class JSPointerDispatcher {
|
||||
newOffsets,
|
||||
new HashMap<>(original.getHitPathByPointerId()),
|
||||
newEventCoords,
|
||||
newScreenCoords,
|
||||
new HashSet<>(original.getHoveringPointerIds()));
|
||||
}
|
||||
|
||||
|
||||
+13
@@ -215,6 +215,12 @@ public class PointerEvent extends Event<PointerEvent> {
|
||||
pointerEvent.putDouble("clientX", clientX);
|
||||
pointerEvent.putDouble("clientY", clientY);
|
||||
|
||||
float[] screenCoords = mEventState.getScreenCoordinatesByPointerId().get(pointerId);
|
||||
double screenX = PixelUtil.toDIPFromPixel(screenCoords[0]);
|
||||
double screenY = PixelUtil.toDIPFromPixel(screenCoords[1]);
|
||||
pointerEvent.putDouble("screenX", screenX);
|
||||
pointerEvent.putDouble("screenY", screenY);
|
||||
|
||||
// x,y values are aliases of clientX, clientY
|
||||
pointerEvent.putDouble("x", clientX);
|
||||
pointerEvent.putDouble("y", clientY);
|
||||
@@ -338,6 +344,7 @@ public class PointerEvent extends Event<PointerEvent> {
|
||||
private Map<Integer, float[]> mOffsetByPointerId;
|
||||
private Map<Integer, List<TouchTargetHelper.ViewTarget>> mHitPathByPointerId;
|
||||
private Map<Integer, float[]> mEventCoordinatesByPointerId;
|
||||
private Map<Integer, float[]> mScreenCoordinatesByPointerId;
|
||||
private Set<Integer> mHoveringPointerIds;
|
||||
|
||||
public PointerEventState(
|
||||
@@ -348,6 +355,7 @@ public class PointerEvent extends Event<PointerEvent> {
|
||||
Map<Integer, float[]> offsetByPointerId,
|
||||
Map<Integer, List<TouchTargetHelper.ViewTarget>> hitPathByPointerId,
|
||||
Map<Integer, float[]> eventCoordinatesByPointerId,
|
||||
Map<Integer, float[]> screenCoordinatesByPointerId,
|
||||
Set<Integer> hoveringPointerIds) {
|
||||
mPrimaryPointerId = primaryPointerId;
|
||||
mActivePointerId = activePointerId;
|
||||
@@ -356,6 +364,7 @@ public class PointerEvent extends Event<PointerEvent> {
|
||||
mOffsetByPointerId = offsetByPointerId;
|
||||
mHitPathByPointerId = hitPathByPointerId;
|
||||
mEventCoordinatesByPointerId = eventCoordinatesByPointerId;
|
||||
mScreenCoordinatesByPointerId = screenCoordinatesByPointerId;
|
||||
mHoveringPointerIds = new HashSet<>(hoveringPointerIds);
|
||||
}
|
||||
|
||||
@@ -395,6 +404,10 @@ public class PointerEvent extends Event<PointerEvent> {
|
||||
return mEventCoordinatesByPointerId;
|
||||
}
|
||||
|
||||
public final Map<Integer, float[]> getScreenCoordinatesByPointerId() {
|
||||
return mScreenCoordinatesByPointerId;
|
||||
}
|
||||
|
||||
public final List<TouchTargetHelper.ViewTarget> getHitPathForActivePointer() {
|
||||
return mHitPathByPointerId.get(mActivePointerId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user