From 5e7a03f7b66aab34968a95ee2a181b8af33fc3b4 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Mon, 23 Dec 2019 17:07:16 -0800 Subject: [PATCH] Implement findNodeAtPoint method Summary: This diff implements the findNodeAtPoint method to return the ShadowNode that is positioned into a Point of the screen. What's not supported: - Scroll position - Transform - return layoutable nodes that are contained inside a non-layoutable node Changelog: [internal] Reviewed By: shergin Differential Revision: D19190285 fbshipit-source-id: fdc0358dc21312e9950a4eb16c36020e9e43e33f --- .../core/layout/LayoutableShadowNode.cpp | 26 +++++++++++++++++++ .../fabric/core/layout/LayoutableShadowNode.h | 9 +++++++ ReactCommon/fabric/uimanager/UIManager.cpp | 7 +++++ ReactCommon/fabric/uimanager/UIManager.h | 4 +++ 4 files changed, 46 insertions(+) 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