From f96dc274d16b97b4a3e9429cec4c9cca28030bec Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 9 Mar 2020 05:52:50 -0700 Subject: [PATCH] FindNodeAtPoint now accounts for scrollView offset Summary: Changelog: [internal] FindNodeAtPoint was not taking transform into account. Reviewed By: JoshuaGross Differential Revision: D20288026 fbshipit-source-id: a8ff09bc265de983e42610c5d23837a2000f9dd2 --- .../core/layout/LayoutableShadowNode.cpp | 5 +- .../fabric/core/tests/FindNodeAtPointTest.cpp | 117 ++++++++++++++++++ ReactCommon/fabric/core/tests/TestComponent.h | 8 +- ReactCommon/fabric/uimanager/UIManager.cpp | 33 ++++- ReactCommon/fabric/uimanager/UIManager.h | 5 +- 5 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 ReactCommon/fabric/core/tests/FindNodeAtPointTest.cpp diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp index 0119090c2ab..b630cd29f48 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp @@ -203,13 +203,14 @@ ShadowNode::Shared LayoutableShadowNode::findNodeAtPoint( return nullptr; } auto frame = layoutableShadowNode->getLayoutMetrics().frame; - auto isPointInside = frame.containsPoint(point); + auto transformedFrame = frame * layoutableShadowNode->getTransform(); + auto isPointInside = transformedFrame.containsPoint(point); if (!isPointInside) { return nullptr; } - auto newPoint = point - frame.origin; + auto newPoint = point - transformedFrame.origin; for (const auto &childShadowNode : node->getChildren()) { auto hitView = findNodeAtPoint(childShadowNode, newPoint); if (hitView) { diff --git a/ReactCommon/fabric/core/tests/FindNodeAtPointTest.cpp b/ReactCommon/fabric/core/tests/FindNodeAtPointTest.cpp new file mode 100644 index 00000000000..1d16585c7e2 --- /dev/null +++ b/ReactCommon/fabric/core/tests/FindNodeAtPointTest.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include "TestComponent.h" + +using namespace facebook::react; + +class FindNodeAtPointTest : public ::testing::Test { + protected: + FindNodeAtPointTest() + : eventDispatcher_(std::shared_ptr()), + componentDescriptor_(TestComponentDescriptor({eventDispatcher_})) { + auto traits = TestShadowNode::BaseTraits(); + + auto familyA = std::make_shared( + ShadowNodeFamilyFragment{ + /* .tag = */ 9, + /* .surfaceId = */ 1, + /* .eventEmitter = */ nullptr, + }, + eventDispatcher_, + componentDescriptor_); + + nodeA_ = std::make_shared( + ShadowNodeFragment{ + /* .props = */ std::make_shared(), + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), + }, + familyA, + traits); + + auto familyAA = std::make_shared( + ShadowNodeFamilyFragment{ + /* .tag = */ 10, + /* .surfaceId = */ 1, + /* .eventEmitter = */ nullptr, + }, + eventDispatcher_, + componentDescriptor_); + + nodeAA_ = std::make_shared( + ShadowNodeFragment{ + /* .props = */ std::make_shared(), + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), + }, + familyAA, + traits); + + auto familyAAA = std::make_shared( + ShadowNodeFamilyFragment{ + /* .tag = */ 11, + /* .surfaceId = */ 1, + /* .eventEmitter = */ nullptr, + }, + eventDispatcher_, + componentDescriptor_); + + nodeAAA_ = std::make_shared( + ShadowNodeFragment{ + /* .props = */ std::make_shared(), + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), + }, + familyAAA, + traits); + + nodeA_->appendChild(nodeAA_); + nodeAA_->appendChild(nodeAAA_); + + auto layoutMetrics = EmptyLayoutMetrics; + + layoutMetrics.frame = facebook::react::Rect{ + facebook::react::Point{0, 0}, facebook::react::Size{1000, 1000}}; + nodeA_->setLayoutMetrics(layoutMetrics); + + layoutMetrics.frame = facebook::react::Rect{ + facebook::react::Point{100, 100}, facebook::react::Size{100, 100}}; + nodeAA_->setLayoutMetrics(layoutMetrics); + + layoutMetrics.frame = facebook::react::Rect{facebook::react::Point{10, 10}, + facebook::react::Size{10, 10}}; + nodeAAA_->setLayoutMetrics(layoutMetrics); + } + + std::shared_ptr eventDispatcher_; + std::shared_ptr nodeA_; + std::shared_ptr nodeAA_; + std::shared_ptr nodeAAA_; + TestComponentDescriptor componentDescriptor_; +}; + +TEST_F(FindNodeAtPointTest, withoutTransform) { + EXPECT_EQ( + LayoutableShadowNode::findNodeAtPoint(nodeA_, {115, 115}), nodeAAA_); + EXPECT_EQ(LayoutableShadowNode::findNodeAtPoint(nodeA_, {105, 105}), nodeAA_); + EXPECT_EQ(LayoutableShadowNode::findNodeAtPoint(nodeA_, {900, 900}), nodeA_); + EXPECT_EQ( + LayoutableShadowNode::findNodeAtPoint(nodeA_, {1001, 1001}), nullptr); +} + +TEST_F(FindNodeAtPointTest, viewIsTranslated) { + nodeA_->_transform = + Transform::Identity() * Transform::Translate(-100, -100, 0); + + EXPECT_EQ(LayoutableShadowNode::findNodeAtPoint(nodeA_, {15, 15}), nodeAAA_); + EXPECT_EQ(LayoutableShadowNode::findNodeAtPoint(nodeA_, {5, 5}), nodeAA_); +} + +TEST_F(FindNodeAtPointTest, viewIsScaled) { + nodeAAA_->_transform = Transform::Identity() * Transform::Scale(0.5, 0.5, 0); + + EXPECT_EQ(LayoutableShadowNode::findNodeAtPoint(nodeA_, {116, 116}), nodeAA_); +} diff --git a/ReactCommon/fabric/core/tests/TestComponent.h b/ReactCommon/fabric/core/tests/TestComponent.h index 3f468459e51..2de9bdd4032 100644 --- a/ReactCommon/fabric/core/tests/TestComponent.h +++ b/ReactCommon/fabric/core/tests/TestComponent.h @@ -53,9 +53,15 @@ class TestShadowNode : public ConcreteViewShadowNode< public: using ConcreteViewShadowNode::ConcreteViewShadowNode; - bool setLayoutMetrics(LayoutMetrics layoutMetrics) { + Transform _transform{Transform::Identity()}; + + bool setLayoutMetrics(LayoutMetrics layoutMetrics) override { return YogaLayoutableShadowNode::setLayoutMetrics(layoutMetrics); } + + Transform getTransform() const override { + return _transform; + } }; class TestComponentDescriptor diff --git a/ReactCommon/fabric/uimanager/UIManager.cpp b/ReactCommon/fabric/uimanager/UIManager.cpp index 2328cf5aa57..52ad7333fd3 100644 --- a/ReactCommon/fabric/uimanager/UIManager.cpp +++ b/ReactCommon/fabric/uimanager/UIManager.cpp @@ -128,10 +128,39 @@ void UIManager::clearJSResponder() const { } } +ShadowNode::Shared const *UIManager::getNewestCloneOfShadowNode( + ShadowNode::Shared const &shadowNode) const { + auto findNewestChildInParent = + [&](auto const &parentNode) -> ShadowNode::Shared const * { + for (auto const &child : parentNode.getChildren()) { + if (ShadowNode::sameFamily(*child, *shadowNode)) { + return &child; + } + } + return nullptr; + }; + + ShadowNode const *ancestorShadowNode; + shadowTreeRegistry_.visit( + shadowNode->getSurfaceId(), [&](ShadowTree const &shadowTree) { + shadowTree.tryCommit( + [&](RootShadowNode::Shared const &oldRootShadowNode) { + ancestorShadowNode = oldRootShadowNode.get(); + return nullptr; + }, + stateReconciliationEnabled_); + }); + + auto ancestors = shadowNode->getFamily().getAncestors(*ancestorShadowNode); + + return findNewestChildInParent(ancestors.rbegin()->first.get()); +} + ShadowNode::Shared UIManager::findNodeAtPoint( - const ShadowNode::Shared &node, + ShadowNode::Shared const &node, Point point) const { - return LayoutableShadowNode::findNodeAtPoint(node, point); + return LayoutableShadowNode::findNodeAtPoint( + *getNewestCloneOfShadowNode(node), point); } void UIManager::setNativeProps( diff --git a/ReactCommon/fabric/uimanager/UIManager.h b/ReactCommon/fabric/uimanager/UIManager.h index aabed542c0f..53a18ab417d 100644 --- a/ReactCommon/fabric/uimanager/UIManager.h +++ b/ReactCommon/fabric/uimanager/UIManager.h @@ -91,9 +91,12 @@ class UIManager final : public ShadowTreeDelegate { void clearJSResponder() const; ShadowNode::Shared findNodeAtPoint( - const ShadowNode::Shared &shadowNode, + ShadowNode::Shared const &shadowNode, Point point) const; + ShadowNode::Shared const *getNewestCloneOfShadowNode( + ShadowNode::Shared const &shadowNode) const; + /* * Returns layout metrics of given `shadowNode` relative to * `ancestorShadowNode` (relative to the root node in case if provided