Include non-virtual nodes when collecting NodeRegions

Summary: Before this patch, we only collected virtual nodes in NodeRegions, because NodeRegions are only needed to implement ReactCompoundView.reactTargetForTouch() which is only interested in virtual nodes. In the next patch, FlatViewGroup will implement ReactCompoundViewGroup interface which requires knowledge of both virtual and non-virtual children. As a step towards that, we need to include non-virtual nodes in NodeRegions. This patch is implementing that. By itself, it should have not cause any changes in application behavior: we add non-virtual nodes to NodeRegions and mark them as non-virtual, then skip all non-virtual nodes in reactTagForTouch().

Reviewed By: ahmedre

Differential Revision: D3018047
This commit is contained in:
Denis Koroskin 2016-03-07 20:06:31 -08:00 committed by Ahmed El-Helw
parent 8014147013
commit b52928c484
6 changed files with 56 additions and 21 deletions

View File

@ -251,10 +251,15 @@ import com.facebook.react.uimanager.annotations.ReactProp;
mNodeRegions = nodeRegion; mNodeRegions = nodeRegion;
} }
/* package */ void updateNodeRegion(float left, float top, float right, float bottom) { /* package */ void updateNodeRegion(
if (mNodeRegion.mLeft != left || mNodeRegion.mTop != top || float left,
mNodeRegion.mRight != right || mNodeRegion.mBottom != bottom) { float top,
setNodeRegion(new NodeRegion(left, top, right, bottom, getReactTag())); float right,
float bottom,
boolean isVirtual) {
if (mNodeRegion.mLeft != left || mNodeRegion.mTop != top || mNodeRegion.mRight != right ||
mNodeRegion.mBottom != bottom || mNodeRegion.mIsVirtual != isVirtual) {
setNodeRegion(new NodeRegion(left, top, right, bottom, getReactTag(), isVirtual));
} }
} }

View File

@ -123,7 +123,7 @@ import com.facebook.react.views.image.ImageLoadEvent;
"TouchTargetHelper should not allow calling this method when pointer events are NONE"); "TouchTargetHelper should not allow calling this method when pointer events are NONE");
if (mPointerEvents != PointerEvents.BOX_ONLY) { if (mPointerEvents != PointerEvents.BOX_ONLY) {
NodeRegion nodeRegion = nodeRegionWithinBounds(touchX, touchY); NodeRegion nodeRegion = virtualNodeRegionWithinBounds(touchX, touchY);
if (nodeRegion != null) { if (nodeRegion != null) {
return nodeRegion.getReactTag(touchX, touchY); return nodeRegion.getReactTag(touchX, touchY);
} }
@ -277,7 +277,7 @@ import com.facebook.react.views.image.ImageLoadEvent;
if (mPointerEvents == PointerEvents.BOX_NONE) { if (mPointerEvents == PointerEvents.BOX_NONE) {
// We cannot always return false here because some child nodes could be flatten into this View // We cannot always return false here because some child nodes could be flatten into this View
NodeRegion nodeRegion = nodeRegionWithinBounds(ev.getX(), ev.getY()); NodeRegion nodeRegion = virtualNodeRegionWithinBounds(ev.getX(), ev.getY());
if (nodeRegion == null) { if (nodeRegion == null) {
// no child to handle this touch event, bailing out. // no child to handle this touch event, bailing out.
return false; return false;
@ -411,9 +411,13 @@ import com.facebook.react.views.image.ImageLoadEvent;
LAYOUT_REQUESTS.clear(); LAYOUT_REQUESTS.clear();
} }
private NodeRegion nodeRegionWithinBounds(float touchX, float touchY) { private NodeRegion virtualNodeRegionWithinBounds(float touchX, float touchY) {
for (int i = mNodeRegions.length - 1; i >= 0; --i) { for (int i = mNodeRegions.length - 1; i >= 0; --i) {
NodeRegion nodeRegion = mNodeRegions[i]; NodeRegion nodeRegion = mNodeRegions[i];
if (!nodeRegion.mIsVirtual) {
// only interested in virtual nodes
continue;
}
if (nodeRegion.withinBounds(touchX, touchY)) { if (nodeRegion.withinBounds(touchX, touchY)) {
return nodeRegion; return nodeRegion;
} }

View File

@ -11,20 +11,28 @@ package com.facebook.react.flat;
/* package */ class NodeRegion { /* package */ class NodeRegion {
/* package */ static final NodeRegion[] EMPTY_ARRAY = new NodeRegion[0]; /* package */ static final NodeRegion[] EMPTY_ARRAY = new NodeRegion[0];
/* package */ static final NodeRegion EMPTY = new NodeRegion(0, 0, 0, 0, -1); /* package */ static final NodeRegion EMPTY = new NodeRegion(0, 0, 0, 0, -1, false);
/* package */ final float mLeft; /* package */ final float mLeft;
/* package */ final float mTop; /* package */ final float mTop;
/* package */ final float mRight; /* package */ final float mRight;
/* package */ final float mBottom; /* package */ final float mBottom;
/* package */ final int mTag; /* package */ final int mTag;
/* package */ final boolean mIsVirtual;
/* package */ NodeRegion(float left, float top, float right, float bottom, int tag) { /* package */ NodeRegion(
float left,
float top,
float right,
float bottom,
int tag,
boolean isVirtual) {
mLeft = left; mLeft = left;
mTop = top; mTop = top;
mRight = right; mRight = right;
mBottom = bottom; mBottom = bottom;
mTag = tag; mTag = tag;
mIsVirtual = isVirtual;
} }
/* package */ final boolean withinBounds(float touchX, float touchY) { /* package */ final boolean withinBounds(float touchX, float touchY) {

View File

@ -223,9 +223,14 @@ import com.facebook.react.uimanager.annotations.ReactProp;
} }
@Override @Override
/* package */ void updateNodeRegion(float left, float top, float right, float bottom) { /* package */ void updateNodeRegion(
float left,
float top,
float right,
float bottom,
boolean isVirtual) {
if (mDrawCommand == null) { if (mDrawCommand == null) {
super.updateNodeRegion(left, top, right, bottom); super.updateNodeRegion(left, top, right, bottom, isVirtual);
return; return;
} }
@ -239,8 +244,9 @@ import com.facebook.react.uimanager.annotations.ReactProp;
Layout newLayout = mDrawCommand.getLayout(); Layout newLayout = mDrawCommand.getLayout();
if (nodeRegion.mLeft != left || nodeRegion.mTop != top || if (nodeRegion.mLeft != left || nodeRegion.mTop != top ||
nodeRegion.mRight != right || nodeRegion.mBottom != bottom || nodeRegion.mRight != right || nodeRegion.mBottom != bottom ||
layout != newLayout) { nodeRegion.mIsVirtual != isVirtual || layout != newLayout) {
setNodeRegion(new TextNodeRegion(left, top, right, bottom, getReactTag(), newLayout)); setNodeRegion(
new TextNodeRegion(left, top, right, bottom, getReactTag(), isVirtual, newLayout));
} }
} }

View File

@ -197,13 +197,14 @@ import com.facebook.react.uimanager.events.EventDispatcher;
float left, float left,
float top, float top,
float right, float right,
float bottom) { float bottom,
boolean isVirtual) {
if (left == right || top == bottom) { if (left == right || top == bottom) {
// no point in adding an empty NodeRegion // no point in adding an empty NodeRegion
return; return;
} }
node.updateNodeRegion(left, top, right, bottom); node.updateNodeRegion(left, top, right, bottom, isVirtual);
mNodeRegions.add(node.getNodeRegion()); mNodeRegions.add(node.getNodeRegion());
} }
@ -291,7 +292,7 @@ import com.facebook.react.uimanager.events.EventDispatcher;
// FlatViewGroup.reactTagForTouch() will always return RCTText's id. To fix the issue, // FlatViewGroup.reactTagForTouch() will always return RCTText's id. To fix the issue,
// manually add nodeRegion so it will have exactly one NodeRegion, and virtual nodes will // manually add nodeRegion so it will have exactly one NodeRegion, and virtual nodes will
// be able to receive touch events. // be able to receive touch events.
addNodeRegion(node, left, top, right, bottom); addNodeRegion(node, left, top, right, bottom, true);
} }
boolean descendantUpdated = collectStateRecursively( boolean descendantUpdated = collectStateRecursively(
@ -520,9 +521,15 @@ import com.facebook.react.uimanager.events.EventDispatcher;
float right = left + width; float right = left + width;
float bottom = top + height; float bottom = top + height;
boolean mountsToView = node.mountsToView();
final boolean updated; final boolean updated;
if (node.mountsToView()) { if (!parentIsAndroidView) {
addNodeRegion(node, left, top, right, bottom, !mountsToView);
}
if (mountsToView) {
ensureBackingViewIsCreated(node); ensureBackingViewIsCreated(node);
addNativeChild(node); addNativeChild(node);
@ -549,8 +556,6 @@ import com.facebook.react.uimanager.events.EventDispatcher;
updateViewBounds(node, left, top, right, bottom); updateViewBounds(node, left, top, right, bottom);
} }
} else { } else {
addNodeRegion(node, left, top, right, bottom);
updated = collectStateRecursively( updated = collectStateRecursively(
node, node,
left, left,

View File

@ -15,8 +15,15 @@ import android.text.Spanned;
/* package */ final class TextNodeRegion extends NodeRegion { /* package */ final class TextNodeRegion extends NodeRegion {
private final Layout mLayout; private final Layout mLayout;
TextNodeRegion(float left, float top, float right, float bottom, int tag, Layout layout) { /* package */ TextNodeRegion(
super(left, top, right, bottom, tag); float left,
float top,
float right,
float bottom,
int tag,
boolean isVirtual,
Layout layout) {
super(left, top, right, bottom, tag, isVirtual);
mLayout = layout; mLayout = layout;
} }