From 9eec2c33e603e58c384291ebfa6c3f8b518512d9 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Wed, 21 Nov 2018 17:10:30 -0800 Subject: [PATCH] Fabric: LayoutableShadowNode::getRelativeLayoutMetrics() Summary: The generic method that returns relative (to some specified node) layout metrics. We need this as a building block to implement `UIManager.measure*()` methods family. Reviewed By: mdvacca Differential Revision: D12932549 fbshipit-source-id: 79064551d32b0cd40e72dc87d0ed194e3779ba29 --- .../core/layout/LayoutableShadowNode.cpp | 30 +++++++++++++++++++ .../fabric/core/layout/LayoutableShadowNode.h | 6 ++++ 2 files changed, 36 insertions(+) diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp index 61ae72fcc..f502fd4d0 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -35,6 +36,35 @@ bool LayoutableShadowNode::LayoutableShadowNode::isLayoutOnly() const { return false; } +LayoutMetrics LayoutableShadowNode::getRelativeLayoutMetrics( + const LayoutableShadowNode &ancestorLayoutableShadowNode) const { + std::vector> ancestors; + + auto &ancestorShadowNode = + dynamic_cast(ancestorLayoutableShadowNode); + auto &shadowNode = dynamic_cast(*this); + + if (!shadowNode.constructAncestorPath(ancestorShadowNode, ancestors)) { + return EmptyLayoutMetrics; + } + + auto layoutMetrics = getLayoutMetrics(); + + for (const auto ¤tShadowNode : ancestors) { + auto layoutableCurrentShadowNode = + dynamic_cast(¤tShadowNode.get()); + + if (!layoutableCurrentShadowNode) { + return EmptyLayoutMetrics; + } + + layoutMetrics.frame.origin += + layoutableCurrentShadowNode->getLayoutMetrics().frame.origin; + } + + return layoutMetrics; +} + void LayoutableShadowNode::cleanLayout() { isLayoutClean_ = true; } diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.h b/ReactCommon/fabric/core/layout/LayoutableShadowNode.h index 74c82a2ce..58b767443 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.h +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.h @@ -59,6 +59,12 @@ class LayoutableShadowNode : public virtual Sealable { */ virtual bool isLayoutOnly() const; + /* + * Returns layout metrics relatively to the given ancestor node. + */ + LayoutMetrics getRelativeLayoutMetrics( + const LayoutableShadowNode &ancestorLayoutableShadowNode) const; + protected: /* * Clean or Dirty layout state: