Skip to content

Commit

Permalink
Move trailing position functions (facebook#1533)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#1533

X-link: facebook/react-native#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
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Dec 21, 2023
1 parent c319c36 commit 94e7336
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
1 change: 1 addition & 0 deletions yoga/algorithm/AbsoluteLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <yoga/algorithm/Align.h>
#include <yoga/algorithm/BoundAxis.h>
#include <yoga/algorithm/CalculateLayout.h>
#include <yoga/algorithm/TrailingPosition.h>

namespace facebook::yoga {

Expand Down
1 change: 1 addition & 0 deletions yoga/algorithm/CalculateLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <yoga/algorithm/FlexLine.h>
#include <yoga/algorithm/PixelGrid.h>
#include <yoga/algorithm/SizingMode.h>
#include <yoga/algorithm/TrailingPosition.h>
#include <yoga/debug/AssertFatal.h>
#include <yoga/debug/Log.h>
#include <yoga/debug/NodeToString.h>
Expand Down
27 changes: 0 additions & 27 deletions yoga/algorithm/CalculateLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions yoga/algorithm/TrailingPosition.h
Original file line number Diff line number Diff line change
@@ -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 <yoga/Yoga.h>
#include <yoga/algorithm/FlexDirection.h>
#include <yoga/event/event.h>
#include <yoga/node/Node.h>

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

0 comments on commit 94e7336

Please sign in to comment.