mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
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
This commit is contained in:
1 parent
ac83f75b8c
commit
5e7a03f7b6
4 files changed
+46
No files matched your search
@@ -114,6 +114,32 @@ void LayoutableShadowNode::layout(LayoutContext layoutContext) {
|
||||
}
|
||||
}
|
||||
|
||||
ShadowNode::Shared LayoutableShadowNode::findNodeAtPoint(
|
||||
ShadowNode::Shared node,
|
||||
Point point) {
|
||||
auto layoutableShadowNode =
|
||||
dynamic_cast<const LayoutableShadowNode *>(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.
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <react/core/Sealable.h>
|
||||
#include <react/core/ShadowNode.h>
|
||||
#include <react/debug/DebugStringConvertible.h>
|
||||
#include <react/graphics/Geometry.h>
|
||||
#include <react/graphics/Transform.h>
|
||||
|
||||
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:
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <react/core/ShadowNodeFragment.h>
|
||||
#include <react/debug/SystraceSection.h>
|
||||
#include <react/graphics/Geometry.h>
|
||||
|
||||
#include <glog/logging.h>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user