Skip to content

Commit

Permalink
Add buttons
Browse files Browse the repository at this point in the history
Summary: Changelog: [Internal] Add support for buttons/button property according to W3C PointerEvent spec

Reviewed By: vincentriemer

Differential Revision: D39460411

fbshipit-source-id: 3544a9b00e870a6028e37417ca9e4de5af62ef70
  • Loading branch information
Luna Wei authored and facebook-github-bot committed Sep 13, 2022
1 parent 57b6bce commit e8a778d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class JSPointerDispatcher {

private int mChildHandlingNativeGesture = -1;
private int mPrimaryPointerId = UNSET_POINTER_ID;
private int mLastButtonState = 0;
private long mDownStartTime = TouchEvent.UNSET;
private long mHoverInteractionKey = TouchEvent.UNSET;
private final ViewGroup mRootViewGroup;
Expand Down Expand Up @@ -101,7 +102,8 @@ private void onUp(
activeTargetTag,
motionEvent,
targetCoordinates,
mPrimaryPointerId));
mPrimaryPointerId,
mLastButtonState));
}

if (!supportsHover) {
Expand All @@ -115,7 +117,8 @@ private void onUp(
activeTargetTag,
motionEvent,
targetCoordinates,
mPrimaryPointerId));
mPrimaryPointerId,
mLastButtonState));
}

List<ViewTarget> leaveViewTargets =
Expand Down Expand Up @@ -169,7 +172,8 @@ private void onDown(
activeTargetTag,
motionEvent,
targetCoordinates,
mPrimaryPointerId));
mPrimaryPointerId,
mLastButtonState));
}

List<ViewTarget> enterViewTargets =
Expand All @@ -196,23 +200,19 @@ private void onDown(
activeTargetTag,
motionEvent,
targetCoordinates,
mPrimaryPointerId));
mPrimaryPointerId,
mLastButtonState));
}
}

public void handleMotionEvent(MotionEvent motionEvent, EventDispatcher eventDispatcher) {
int action = motionEvent.getActionMasked();

// Ignore hover enter/exit because we determine this ourselves
if (action == MotionEvent.ACTION_HOVER_EXIT || action == MotionEvent.ACTION_HOVER_ENTER) {
// Don't fire any pointer events if child view is handling native gesture
if (mChildHandlingNativeGesture != -1) {
return;
}

int surfaceId = UIManagerHelper.getSurfaceId(mRootViewGroup);

// Only relevant for POINTER_UP/POINTER_DOWN actions, otherwise 0
int actionIndex = motionEvent.getActionIndex();

float[] targetCoordinates = new float[2];
List<ViewTarget> hitPath =
TouchTargetHelper.findTargetPathAndCoordinatesForTouch(
Expand All @@ -225,13 +225,12 @@ public void handleMotionEvent(MotionEvent motionEvent, EventDispatcher eventDisp
return;
}

int action = motionEvent.getActionMasked();
int surfaceId = UIManagerHelper.getSurfaceId(mRootViewGroup);

TouchTargetHelper.ViewTarget activeViewTarget = hitPath.get(0);
int activeTargetTag = activeViewTarget.getViewId();

if (mChildHandlingNativeGesture != -1) {
return;
}

switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
Expand All @@ -258,7 +257,8 @@ public void handleMotionEvent(MotionEvent motionEvent, EventDispatcher eventDisp
motionEvent,
targetCoordinates,
coalescingKey,
mPrimaryPointerId));
mPrimaryPointerId,
mLastButtonState));
}
break;
case MotionEvent.ACTION_UP:
Expand All @@ -274,6 +274,8 @@ public void handleMotionEvent(MotionEvent motionEvent, EventDispatcher eventDisp
"Warning : Motion Event was ignored. Action=" + action + " Target=" + activeTargetTag);
return;
}

mLastButtonState = motionEvent.getButtonState();
}

private static boolean isAnyoneListeningForBubblingEvent(
Expand Down Expand Up @@ -334,7 +336,13 @@ private void dispatchEventForViewTargets(
int viewId = viewTarget.getViewId();
dispatcher.dispatchEvent(
PointerEvent.obtain(
eventName, surfaceId, viewId, motionEvent, targetCoordinates, mPrimaryPointerId));
eventName,
surfaceId,
viewId,
motionEvent,
targetCoordinates,
mPrimaryPointerId,
mLastButtonState));
}
}

Expand Down Expand Up @@ -427,7 +435,8 @@ private void onMove(
lastTargetTag,
motionEvent,
targetCoordinates,
mPrimaryPointerId));
mPrimaryPointerId,
mLastButtonState));
}

// target -> root
Expand Down Expand Up @@ -459,7 +468,8 @@ private void onMove(
targetTag,
motionEvent,
targetCoordinates,
mPrimaryPointerId));
mPrimaryPointerId,
mLastButtonState));
}

// target -> root
Expand Down Expand Up @@ -529,7 +539,8 @@ private void dispatchCancelEvent(
targetTag,
motionEvent,
targetCoordinates,
mPrimaryPointerId));
mPrimaryPointerId,
mLastButtonState));
}

// TODO(luwe) - Need to fire pointer out here as well:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static PointerEvent obtain(
int viewTag,
MotionEvent motionEventToCopy,
float[] offsetCoords,
int primaryPointerId) {
int primaryPointerId,
int lastButtonState) {
PointerEvent event = EVENTS_POOL.acquire();
if (event == null) {
event = new PointerEvent();
Expand All @@ -44,7 +45,8 @@ public static PointerEvent obtain(
Assertions.assertNotNull(motionEventToCopy),
offsetCoords,
0,
primaryPointerId);
primaryPointerId,
lastButtonState);
return event;
}

Expand All @@ -55,7 +57,8 @@ public static PointerEvent obtain(
MotionEvent motionEventToCopy,
float[] offsetCoords,
int coalescingKey,
int primaryPointerId) {
int primaryPointerId,
int lastButtonState) {
PointerEvent event = EVENTS_POOL.acquire();
if (event == null) {
event = new PointerEvent();
Expand All @@ -67,7 +70,8 @@ public static PointerEvent obtain(
Assertions.assertNotNull(motionEventToCopy),
offsetCoords,
coalescingKey,
primaryPointerId);
primaryPointerId,
lastButtonState);
return event;
}

Expand All @@ -78,6 +82,7 @@ public static PointerEvent obtain(
private float mOffsetY;
private @Nullable List<WritableMap> mPointersEventData;
private int mPrimaryPointerId;
private int mLastButtonState;

private void init(
String eventName,
Expand All @@ -86,7 +91,8 @@ private void init(
MotionEvent motionEventToCopy,
float[] offsetCoords,
int coalescingKey,
int primaryPointerId) {
int primaryPointerId,
int lastButtonState) {

super.init(surfaceId, viewTag, motionEventToCopy.getEventTime());
mEventName = eventName;
Expand All @@ -95,6 +101,7 @@ private void init(
mOffsetX = offsetCoords[0];
mOffsetY = offsetCoords[1];
mPrimaryPointerId = primaryPointerId;
mLastButtonState = lastButtonState;
}

private PointerEvent() {}
Expand Down Expand Up @@ -209,6 +216,11 @@ private WritableMap createPointerEventData(int index) {
pointerEvent.putDouble("tiltY", 0);
}

int buttonState = mMotionEvent.getButtonState();
pointerEvent.putInt("buttons", buttonState);
pointerEvent.putInt(
"button", PointerEventHelper.getButtonChange(mLastButtonState, buttonState));

return pointerEvent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ public static enum EVENT {
}
}

// https://w3c.github.io/pointerevents/#the-button-property
public static int getButtonChange(int lastButtonState, int currentButtonState) {
int changedMask = currentButtonState ^ lastButtonState;
if (changedMask == 0) {
return -1;
}

switch (changedMask) {
case MotionEvent.BUTTON_PRIMARY: // left button, touch/pen contact
return 0;
case MotionEvent.BUTTON_TERTIARY: // middle mouse
return 1;
case MotionEvent.BUTTON_SECONDARY: // rightbutton, Pen barrel button
return 2;
case MotionEvent.BUTTON_BACK:
return 3;
case MotionEvent.BUTTON_FORWARD:
return 4;
// TOD0 - Pen eraser button maps to what?
}
return -1;
}

public static boolean isPrimary(int pointerId, int primaryPointerId, MotionEvent event) {
if (supportsHover(event)) {
return true;
Expand Down

0 comments on commit e8a778d

Please sign in to comment.