C++ Cleanup 5/N: Reorganize Utils (#1357)
Summary: X-link: https://github.com/facebook/react-native/pull/39222 Pull Request resolved: https://github.com/facebook/yoga/pull/1357 X-link: https://github.com/facebook/react-native/pull/39199 ## This diff This splits `Utils.h` and `Utils.cpp`, and tweaks naming and namespaces. ## This stack The organization of the C++ internals of Yoga are in need of attention. 1. Some of the C++ internals are namespaced, but others not. 2. Some of the namespaces include `detail`, but are meant to be used outside of the translation unit (FB Clang Tidy rules warn on any usage of these) 2. Most of the files are in a flat hierarchy, except for event tracing in its own folder 3. Some files and functions begin with YG, others don’t 4. Some functions are uppercase, others are not 5. Almost all of the interesting logic is in Yoga.cpp, and the file is too large to reason about 6. There are multiple grab bag files where folks put random functions they need in (Utils, BitUtils, Yoga-Internal.h) 7. There is no clear indication from file structure or type naming what is private vs not 8. Handles like `YGNodeRef` and `YGConfigRef` can be used to access internals just by importing headers This stack does some much needed spring cleaning: 1. All non-public headers and C++ implementation details are in separate folders from the root level `yoga`. This will give us room to split up logic and add more files without too large a flat hierarchy 3. All private C++ internals are under the `facebook::yoga` namespace. Details namespaces are only ever used within the same header, as they are intended 4. Utils files are split 5. Most C++ internals drop the YG prefix 6. Most C++ internal function names are all lower camel case 7. We start to split up Yoga.cpp 8. Every header beginning with YG or at the top-level directory is public and C only, with the exception of Yoga-Internal.h which has non-public functions for bindings 9. It is not possible to use private APIs without static casting handles to internal classes This will give us more leeway to continue splitting monolithic files, and consistent guidelines for style in new files as well. These changes should not be breaking to any project using only public Yoga headers. This includes every usage of Yoga in fbsource except for RN Fabric which is currently tied to internals. This refactor should make that boundary clearer. bypass-github-export-checks Reviewed By: shwanton Differential Revision: D48847260 fbshipit-source-id: b99df3029cd66257a7ae64de28c13e8751ceb20c
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b959774af7
commit
c029041707
58
yoga/algorithm/CollectFlexItemsRowValues.h
Normal file
58
yoga/algorithm/CollectFlexItemsRowValues.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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 <vector>
|
||||
#include <yoga/node/Node.h>
|
||||
|
||||
namespace facebook::yoga {
|
||||
|
||||
// This struct is an helper model to hold the data for step 4 of flexbox algo,
|
||||
// which is collecting the flex items in a line.
|
||||
//
|
||||
// - itemsOnLine: Number of items which can fit in a line considering the
|
||||
// available Inner dimension, the flex items computed flexbasis and their
|
||||
// margin. It may be different than the difference between start and end
|
||||
// indicates because we skip over absolute-positioned items.
|
||||
//
|
||||
// - sizeConsumedOnCurrentLine: It is accumulation of the dimensions and margin
|
||||
// of all the children on the current line. This will be used in order to
|
||||
// either set the dimensions of the node if none already exist or to compute
|
||||
// the remaining space left for the flexible children.
|
||||
//
|
||||
// - totalFlexGrowFactors: total flex grow factors of flex items which are to be
|
||||
// laid in the current line
|
||||
//
|
||||
// - totalFlexShrinkFactors: total flex shrink factors of flex items which are
|
||||
// to be laid in the current line
|
||||
//
|
||||
// - endOfLineIndex: Its the end index of the last flex item which was examined
|
||||
// and it may or may not be part of the current line(as it may be absolutely
|
||||
// positioned or including it may have caused to overshoot availableInnerDim)
|
||||
//
|
||||
// - relativeChildren: Maintain a vector of the child nodes that can shrink
|
||||
// and/or grow.
|
||||
|
||||
struct CollectFlexItemsRowValues {
|
||||
uint32_t itemsOnLine;
|
||||
float sizeConsumedOnCurrentLine;
|
||||
float totalFlexGrowFactors;
|
||||
float totalFlexShrinkScaledFactors;
|
||||
uint32_t endOfLineIndex;
|
||||
std::vector<yoga::Node*> relativeChildren;
|
||||
float remainingFreeSpace;
|
||||
// The size of the mainDim for the row after considering size, padding, margin
|
||||
// and border of flex items. This is used to calculate maxLineDim after going
|
||||
// through all the rows to decide on the main axis size of owner.
|
||||
float mainDim;
|
||||
// The size of the crossDim for the row after considering size, padding,
|
||||
// margin and border of flex items. Used for calculating containers crossSize.
|
||||
float crossDim;
|
||||
};
|
||||
|
||||
} // namespace facebook::yoga
|
46
yoga/algorithm/FlexDirection.h
Normal file
46
yoga/algorithm/FlexDirection.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
namespace facebook::yoga {
|
||||
|
||||
inline bool isRow(const YGFlexDirection flexDirection) {
|
||||
return flexDirection == YGFlexDirectionRow ||
|
||||
flexDirection == YGFlexDirectionRowReverse;
|
||||
}
|
||||
|
||||
inline bool isColumn(const YGFlexDirection flexDirection) {
|
||||
return flexDirection == YGFlexDirectionColumn ||
|
||||
flexDirection == YGFlexDirectionColumnReverse;
|
||||
}
|
||||
|
||||
inline YGFlexDirection resolveDirection(
|
||||
const YGFlexDirection flexDirection,
|
||||
const YGDirection direction) {
|
||||
if (direction == YGDirectionRTL) {
|
||||
if (flexDirection == YGFlexDirectionRow) {
|
||||
return YGFlexDirectionRowReverse;
|
||||
} else if (flexDirection == YGFlexDirectionRowReverse) {
|
||||
return YGFlexDirectionRow;
|
||||
}
|
||||
}
|
||||
|
||||
return flexDirection;
|
||||
}
|
||||
|
||||
inline YGFlexDirection resolveCrossDirection(
|
||||
const YGFlexDirection flexDirection,
|
||||
const YGDirection direction) {
|
||||
return isColumn(flexDirection)
|
||||
? resolveDirection(YGFlexDirectionRow, direction)
|
||||
: YGFlexDirectionColumn;
|
||||
}
|
||||
|
||||
} // namespace facebook::yoga
|
32
yoga/algorithm/ResolveValue.h
Normal file
32
yoga/algorithm/ResolveValue.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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/YGFloatOptional.h>
|
||||
|
||||
namespace facebook::yoga {
|
||||
|
||||
inline YGFloatOptional resolveValue(
|
||||
const YGValue value,
|
||||
const float ownerSize) {
|
||||
switch (value.unit) {
|
||||
case YGUnitPoint:
|
||||
return YGFloatOptional{value.value};
|
||||
case YGUnitPercent:
|
||||
return YGFloatOptional{value.value * ownerSize * 0.01f};
|
||||
default:
|
||||
return YGFloatOptional{};
|
||||
}
|
||||
}
|
||||
|
||||
inline YGFloatOptional resolveValue(CompactValue value, float ownerSize) {
|
||||
return resolveValue((YGValue) value, ownerSize);
|
||||
}
|
||||
|
||||
} // namespace facebook::yoga
|
Reference in New Issue
Block a user