From 94e733639491d54e8a4a7eea6c6ba019a120f8be Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 21 Dec 2023 13:48:11 -0800 Subject: [PATCH] Move trailing position functions (#1533) Summary: Pull Request resolved: https://github.com/facebook/yoga/pull/1533 X-link: https://github.com/facebook/react-native/pull/42031 I have some reservations about some of the conditional setting of trailing position in general, and some of the repeated transformations that neccesitates this, but these functions don't belong in `CalculateLayout.h`. For now, just move these to their own header. Reviewed By: joevilches Differential Revision: D52292121 fbshipit-source-id: 4a998a4390a8d045af45f5424adaf049ed635e7a --- yoga/algorithm/AbsoluteLayout.cpp | 1 + yoga/algorithm/CalculateLayout.cpp | 1 + yoga/algorithm/CalculateLayout.h | 27 ------------------ yoga/algorithm/TrailingPosition.h | 44 ++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 yoga/algorithm/TrailingPosition.h diff --git a/yoga/algorithm/AbsoluteLayout.cpp b/yoga/algorithm/AbsoluteLayout.cpp index 1da6c290..886aae13 100644 --- a/yoga/algorithm/AbsoluteLayout.cpp +++ b/yoga/algorithm/AbsoluteLayout.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace facebook::yoga { diff --git a/yoga/algorithm/CalculateLayout.cpp b/yoga/algorithm/CalculateLayout.cpp index 1aae098f..17703605 100644 --- a/yoga/algorithm/CalculateLayout.cpp +++ b/yoga/algorithm/CalculateLayout.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include diff --git a/yoga/algorithm/CalculateLayout.h b/yoga/algorithm/CalculateLayout.h index 955cbdbb..1d5a3fd7 100644 --- a/yoga/algorithm/CalculateLayout.h +++ b/yoga/algorithm/CalculateLayout.h @@ -35,31 +35,4 @@ bool calculateLayoutInternal( const uint32_t depth, const uint32_t generationCount); -// Given an offset to an edge, returns the offset to the opposite edge on the -// same axis. This assumes that the width/height of both nodes is determined at -// this point. -inline float getPositionOfOppositeEdge( - float position, - FlexDirection axis, - const yoga::Node* const containingNode, - const yoga::Node* const node) { - return containingNode->getLayout().measuredDimension(dimension(axis)) - - node->getLayout().measuredDimension(dimension(axis)) - position; -} - -inline void setChildTrailingPosition( - const yoga::Node* const node, - yoga::Node* const child, - const FlexDirection axis) { - child->setLayoutPosition( - getPositionOfOppositeEdge( - child->getLayout().position(flexStartEdge(axis)), axis, node, child), - flexEndEdge(axis)); -} - -inline bool needsTrailingPosition(const FlexDirection axis) { - return axis == FlexDirection::RowReverse || - axis == FlexDirection::ColumnReverse; -} - } // namespace facebook::yoga diff --git a/yoga/algorithm/TrailingPosition.h b/yoga/algorithm/TrailingPosition.h new file mode 100644 index 00000000..84d00edb --- /dev/null +++ b/yoga/algorithm/TrailingPosition.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include +#include +#include + +namespace facebook::yoga { + +// Given an offset to an edge, returns the offset to the opposite edge on the +// same axis. This assumes that the width/height of both nodes is determined at +// this point. +inline float getPositionOfOppositeEdge( + float position, + FlexDirection axis, + const yoga::Node* const containingNode, + const yoga::Node* const node) { + return containingNode->getLayout().measuredDimension(dimension(axis)) - + node->getLayout().measuredDimension(dimension(axis)) - position; +} + +inline void setChildTrailingPosition( + const yoga::Node* const node, + yoga::Node* const child, + const FlexDirection axis) { + child->setLayoutPosition( + getPositionOfOppositeEdge( + child->getLayout().position(flexStartEdge(axis)), axis, node, child), + flexEndEdge(axis)); +} + +inline bool needsTrailingPosition(const FlexDirection axis) { + return axis == FlexDirection::RowReverse || + axis == FlexDirection::ColumnReverse; +} + +} // namespace facebook::yoga