diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp index 083dee04273..05740d4bd75 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp @@ -114,6 +114,32 @@ void LayoutableShadowNode::layout(LayoutContext layoutContext) { } } +ShadowNode::Shared LayoutableShadowNode::findNodeAtPoint( + ShadowNode::Shared node, + Point point) { + auto layoutableShadowNode = + dynamic_cast(node.get()); + + if (!layoutableShadowNode) { + return nullptr; + } + auto frame = layoutableShadowNode->getLayoutMetrics().frame; + auto isPointInside = frame.containsPoint(point); + + if (!isPointInside) { + return nullptr; + } + + auto newPoint = point - frame.origin; + for (const auto &childShadowNode : node->getChildren()) { + auto hitView = findNodeAtPoint(childShadowNode, newPoint); + if (hitView) { + return hitView; + } + } + return isPointInside ? node : nullptr; +} + void LayoutableShadowNode::layoutChildren(LayoutContext layoutContext) { // Default implementation does nothing. } diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.h b/ReactCommon/fabric/core/layout/LayoutableShadowNode.h index 22442219b26..88cd38ea3b2 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.h +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.h @@ -17,6 +17,7 @@ #include #include #include +#include #include namespace facebook { @@ -86,6 +87,14 @@ class LayoutableShadowNode : public virtual Sealable { LayoutableShadowNode const &ancestorLayoutableShadowNode, LayoutInspectingPolicy policy) const; + /* + * Returns the ShadowNode that is rendered at the Point received as a + * parameter. + */ + static ShadowNode::Shared findNodeAtPoint( + ShadowNode::Shared node, + Point point); + protected: /* * Clean or Dirty layout state: diff --git a/ReactCommon/fabric/uimanager/UIManager.cpp b/ReactCommon/fabric/uimanager/UIManager.cpp index 4fc668736fb..1efc4dca603 100644 --- a/ReactCommon/fabric/uimanager/UIManager.cpp +++ b/ReactCommon/fabric/uimanager/UIManager.cpp @@ -9,6 +9,7 @@ #include #include +#include #include @@ -134,6 +135,12 @@ void UIManager::clearJSResponder() const { } } +ShadowNode::Shared UIManager::findNodeAtPoint( + const ShadowNode::Shared &node, + Point point) const { + return LayoutableShadowNode::findNodeAtPoint(node, point); +} + void UIManager::setNativeProps( ShadowNode const &shadowNode, RawProps const &rawProps) const { diff --git a/ReactCommon/fabric/uimanager/UIManager.h b/ReactCommon/fabric/uimanager/UIManager.h index 9723615c970..741567ee68d 100644 --- a/ReactCommon/fabric/uimanager/UIManager.h +++ b/ReactCommon/fabric/uimanager/UIManager.h @@ -88,6 +88,10 @@ class UIManager final : public ShadowTreeDelegate { void clearJSResponder() const; + ShadowNode::Shared findNodeAtPoint( + const ShadowNode::Shared &shadowNode, + Point point) const; + /* * Returns layout metrics of given `shadowNode` relative to * `ancestorShadowNode` (relative to the root node in case if provided