Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W3CPointerEvents: include screen coordinates in pointer events #38222

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -255,10 +257,21 @@ private void onDown(
}
}

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)};
Expand All @@ -270,6 +283,7 @@ private PointerEventState createEventState(int activePointerId, MotionEvent moti
offsetByPointerId.put(pointerId, offsetCoordinates);
hitPathByPointerId.put(pointerId, hitPath);
eventCoordinatesByPointerId.put(pointerId, eventCoordinates);
screenCoordinatesByPointerId.put(pointerId, eventCoordsToScreenCoords(eventCoordinates));
}

int surfaceId = UIManagerHelper.getSurfaceId(mRootViewGroup);
Expand All @@ -282,6 +296,7 @@ private PointerEventState createEventState(int activePointerId, MotionEvent moti
offsetByPointerId,
hitPathByPointerId,
eventCoordinatesByPointerId,
screenCoordinatesByPointerId,
mHoveringPointerIds); // Creates a copy of hovering pointer ids, as they may be updated
}

Expand Down Expand Up @@ -673,17 +688,22 @@ private int[] getChildOffsetRelativeToRoot(View childView) {
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()) {
float[] offsetValue = offsetEntry.getValue();
offsetValue[0] = rootX;
offsetValue[1] = rootY;
offsetEntry.setValue(rootOffset);
}

float[] zeroOffset = {0, 0};
for (Map.Entry<Integer, float[]> eventCoordsEntry : newEventCoords.entrySet()) {
float[] eventCoordsValue = eventCoordsEntry.getValue();
eventCoordsValue[0] = 0;
eventCoordsValue[1] = 0;
eventCoordsEntry.setValue(zeroOffset);
}

float[] screenCoords = eventCoordsToScreenCoords(rootOffset);
for (Map.Entry<Integer, float[]> screenCoordsEntry : newScreenCoords.entrySet()) {
screenCoordsEntry.setValue(screenCoords);
}

return new PointerEventState(
Expand All @@ -694,6 +714,7 @@ private PointerEventState normalizeToRoot(PointerEventState original, float root
newOffsets,
new HashMap<>(original.getHitPathByPointerId()),
newEventCoords,
newScreenCoords,
new HashSet<>(original.getHoveringPointerIds()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.react.uimanager.events;

import android.view.KeyEvent;
import android.view.MotionEvent;
import androidx.annotation.Nullable;
import androidx.core.util.Pools;
Expand Down Expand Up @@ -183,6 +184,13 @@ private List<WritableMap> createW3CPointerEvents() {
return w3cPointerEvents;
}

private void addModifierKeyData(WritableMap pointerEvent, int modifierKeyMask) {
pointerEvent.putBoolean("ctrlKey", (modifierKeyMask & KeyEvent.META_CTRL_ON) != 0);
pointerEvent.putBoolean("shiftKey", (modifierKeyMask & KeyEvent.META_SHIFT_ON) != 0);
pointerEvent.putBoolean("altKey", (modifierKeyMask & KeyEvent.META_ALT_ON) != 0);
pointerEvent.putBoolean("metaKey", (modifierKeyMask & KeyEvent.META_META_ON) != 0);
}

private WritableMap createW3CPointerEvent(int index) {
WritableMap pointerEvent = Arguments.createMap();
int pointerId = mMotionEvent.getPointerId(index);
Expand All @@ -207,6 +215,12 @@ private WritableMap createW3CPointerEvent(int index) {
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);
Expand Down Expand Up @@ -254,6 +268,8 @@ private WritableMap createW3CPointerEvent(int index) {
pointerEvent.putDouble("pressure", pressure);
pointerEvent.putDouble("tangentialPressure", 0.0);

addModifierKeyData(pointerEvent, mMotionEvent.getMetaState());

return pointerEvent;
}

Expand Down Expand Up @@ -328,6 +344,7 @@ public static class PointerEventState {
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(
Expand All @@ -338,6 +355,7 @@ public PointerEventState(
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;
Expand All @@ -346,6 +364,7 @@ public PointerEventState(
mOffsetByPointerId = offsetByPointerId;
mHitPathByPointerId = hitPathByPointerId;
mEventCoordinatesByPointerId = eventCoordinatesByPointerId;
mScreenCoordinatesByPointerId = screenCoordinatesByPointerId;
mHoveringPointerIds = new HashSet<>(hoveringPointerIds);
}

Expand Down Expand Up @@ -385,6 +404,10 @@ public final Map<Integer, float[]> getEventCoordinatesByPointerId() {
return mEventCoordinatesByPointerId;
}

public final Map<Integer, float[]> getScreenCoordinatesByPointerId() {
return mScreenCoordinatesByPointerId;
}

public final List<TouchTargetHelper.ViewTarget> getHitPathForActivePointer() {
return mHitPathByPointerId.get(mActivePointerId);
}
Expand Down