Skip to content

Commit

Permalink
Use CSS terminology for box sizing
Browse files Browse the repository at this point in the history
Summary:
Yoga passes `MeasureMode`/`YGMeasureMode` to express constraints in how a box should be measured, given definite or indefinite available space.

This is modeled after Android [MeasureSpec](https://developer.android.com/reference/android/view/View.MeasureSpec), with a table above `calculateLayoutImpl()` explaining the CSS terms they map to. This can be confusing when flipping between the spec, and code.

This switches internal usages to the CSS terms, but leaves around `YGMeasureMode` since it is the public API passed to measure functions.

Differential Revision: D51068417

fbshipit-source-id: 2a4ebd327bb63a97840b987e817e18587b4c06d8
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Nov 25, 2023
1 parent c12c1ce commit 4899ee6
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 268 deletions.
8 changes: 4 additions & 4 deletions packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,13 @@ bool YGNodeCanUseCachedMeasurement(
float marginColumn,
YGConfigRef config) {
return yoga::canUseCachedMeasurement(
scopedEnum(widthMode),
sizingMode(scopedEnum(widthMode)),
availableWidth,
scopedEnum(heightMode),
sizingMode(scopedEnum(heightMode)),
availableHeight,
scopedEnum(lastWidthMode),
sizingMode(scopedEnum(lastWidthMode)),
lastAvailableWidth,
scopedEnum(lastHeightMode),
sizingMode(scopedEnum(lastHeightMode)),
lastAvailableHeight,
lastComputedWidth,
lastComputedHeight,
Expand Down
13 changes: 13 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/YGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ typedef struct YGSize {
} YGSize;

/**
* Returns the computed dimensions of the node, following the contraints of
* `widthMode` and `heightMode`:
*
* YGMeasureModeUndefined: The parent has not imposed any constraint on the
* child. It can be whatever size it wants.
*
* YGMeasureModeAtMost: The child can be as large as it wants up to the
* specified size.
*
* YGMeasureModeExactly: The parent has determined an exact size for the
* child. The child is going to be given those bounds regardless of how big it
* wants to be.
*
* @returns the size of the leaf node, measured under the given contraints.
*/
typedef YGSize (*YGMeasureFunc)(
Expand Down
70 changes: 35 additions & 35 deletions packages/react-native/ReactCommon/yoga/yoga/algorithm/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,52 @@
namespace facebook::yoga {

static inline bool sizeIsExactAndMatchesOldMeasuredSize(
MeasureMode sizeMode,
SizingMode sizeMode,
float size,
float lastComputedSize) {
return sizeMode == MeasureMode::Exactly &&
return sizeMode == SizingMode::StretchFit &&
yoga::inexactEquals(size, lastComputedSize);
}

static inline bool oldSizeIsUnspecifiedAndStillFits(
MeasureMode sizeMode,
static inline bool oldSizeIsMaxContentAndStillFits(
SizingMode sizeMode,
float size,
MeasureMode lastSizeMode,
SizingMode lastSizeMode,
float lastComputedSize) {
return sizeMode == MeasureMode::AtMost &&
lastSizeMode == MeasureMode::Undefined &&
return sizeMode == SizingMode::FitContent &&
lastSizeMode == SizingMode::MaxContent &&
(size >= lastComputedSize || yoga::inexactEquals(size, lastComputedSize));
}

static inline bool newMeasureSizeIsStricterAndStillValid(
MeasureMode sizeMode,
static inline bool newSizeIsStricterAndStillValid(
SizingMode sizeMode,
float size,
MeasureMode lastSizeMode,
SizingMode lastSizeMode,
float lastSize,
float lastComputedSize) {
return lastSizeMode == MeasureMode::AtMost &&
sizeMode == MeasureMode::AtMost && !std::isnan(lastSize) &&
!std::isnan(size) && !std::isnan(lastComputedSize) && lastSize > size &&
return lastSizeMode == SizingMode::FitContent &&
sizeMode == SizingMode::FitContent && yoga::isDefined(lastSize) &&
yoga::isDefined(size) && yoga::isDefined(lastComputedSize) &&
lastSize > size &&
(lastComputedSize <= size || yoga::inexactEquals(size, lastComputedSize));
}

bool canUseCachedMeasurement(
const MeasureMode widthMode,
const SizingMode widthMode,
const float availableWidth,
const MeasureMode heightMode,
const SizingMode heightMode,
const float availableHeight,
const MeasureMode lastWidthMode,
const SizingMode lastWidthMode,
const float lastAvailableWidth,
const MeasureMode lastHeightMode,
const SizingMode lastHeightMode,
const float lastAvailableHeight,
const float lastComputedWidth,
const float lastComputedHeight,
const float marginRow,
const float marginColumn,
const yoga::Config* const config) {
if ((!std::isnan(lastComputedHeight) && lastComputedHeight < 0) ||
(!std::isnan(lastComputedWidth) && lastComputedWidth < 0)) {
if ((yoga::isDefined(lastComputedHeight) && lastComputedHeight < 0) ||
((yoga::isDefined(lastComputedWidth)) && lastComputedWidth < 0)) {
return false;
}

Expand Down Expand Up @@ -87,33 +88,32 @@ bool canUseCachedMeasurement(
hasSameWidthSpec ||
sizeIsExactAndMatchesOldMeasuredSize(
widthMode, availableWidth - marginRow, lastComputedWidth) ||
oldSizeIsUnspecifiedAndStillFits(
oldSizeIsMaxContentAndStillFits(
widthMode,
availableWidth - marginRow,
lastWidthMode,
lastComputedWidth) ||
newMeasureSizeIsStricterAndStillValid(
newSizeIsStricterAndStillValid(
widthMode,
availableWidth - marginRow,
lastWidthMode,
lastAvailableWidth,
lastComputedWidth);

const bool heightIsCompatible =
hasSameHeightSpec ||
const bool heightIsCompatible = hasSameHeightSpec ||
sizeIsExactAndMatchesOldMeasuredSize(
heightMode, availableHeight - marginColumn, lastComputedHeight) ||
oldSizeIsUnspecifiedAndStillFits(
heightMode,
availableHeight - marginColumn,
lastHeightMode,
lastComputedHeight) ||
newMeasureSizeIsStricterAndStillValid(
heightMode,
availableHeight - marginColumn,
lastHeightMode,
lastAvailableHeight,
lastComputedHeight);
heightMode,
availableHeight - marginColumn,
lastComputedHeight) ||
oldSizeIsMaxContentAndStillFits(heightMode,
availableHeight - marginColumn,
lastHeightMode,
lastComputedHeight) ||
newSizeIsStricterAndStillValid(heightMode,
availableHeight - marginColumn,
lastHeightMode,
lastAvailableHeight,
lastComputedHeight);

return widthIsCompatible && heightIsCompatible;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/react-native/ReactCommon/yoga/yoga/algorithm/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

#pragma once

#include <yoga/algorithm/SizingMode.h>
#include <yoga/config/Config.h>
#include <yoga/enums/MeasureMode.h>

namespace facebook::yoga {

bool canUseCachedMeasurement(
MeasureMode widthMode,
SizingMode widthMode,
float availableWidth,
MeasureMode heightMode,
SizingMode heightMode,
float availableHeight,
MeasureMode lastWidthMode,
SizingMode lastWidthMode,
float lastAvailableWidth,
MeasureMode lastHeightMode,
SizingMode lastHeightMode,
float lastAvailableHeight,
float lastComputedWidth,
float lastComputedHeight,
Expand Down
Loading

0 comments on commit 4899ee6

Please sign in to comment.