Skip to content

Commit d25dc62

Browse files
committed
feat: yoga update
1 parent 39714cc commit d25dc62

4 files changed

Lines changed: 134 additions & 64 deletions

File tree

packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,9 @@ static float computeMinContentMainSize(
734734
const Direction leafDirection = node->resolveDirection(ownerDirection);
735735
const float paddingAndBorder =
736736
node->style().computeFlexStartPaddingAndBorder(
737-
requestedAxis, leafDirection, ownerWidth) +
737+
requestedAxis, leafDirection, ownerWidth, node) +
738738
node->style().computeFlexEndPaddingAndBorder(
739-
requestedAxis, leafDirection, ownerWidth);
739+
requestedAxis, leafDirection, ownerWidth, node);
740740
return (wantRow ? size.width : size.height) + paddingAndBorder;
741741
}
742742

@@ -762,25 +762,26 @@ static float computeMinContentMainSize(
762762

763763
float childMain = computeMinContentMainSize(
764764
child, nodeMainAxis, direction, ownerWidth, ownerHeight);
765-
childMain += child->style().computeMarginForAxis(nodeMainAxis, ownerWidth);
765+
childMain +=
766+
child->style().computeMarginForAxis(nodeMainAxis, ownerWidth, child);
766767

767768
float childCross = computeMinContentMainSize(
768769
child, nodeCrossAxis, direction, ownerWidth, ownerHeight);
769770
childCross +=
770-
child->style().computeMarginForAxis(nodeCrossAxis, ownerWidth);
771+
child->style().computeMarginForAxis(nodeCrossAxis, ownerWidth, child);
771772

772773
mainTotal += childMain;
773774
crossMax = std::max(crossMax, childCross);
774775
}
775776

776777
mainTotal += node->style().computeFlexStartPaddingAndBorder(
777-
nodeMainAxis, direction, ownerWidth) +
778+
nodeMainAxis, direction, ownerWidth, node) +
778779
node->style().computeFlexEndPaddingAndBorder(
779-
nodeMainAxis, direction, ownerWidth);
780+
nodeMainAxis, direction, ownerWidth, node);
780781
crossMax += node->style().computeFlexStartPaddingAndBorder(
781-
nodeCrossAxis, direction, ownerWidth) +
782+
nodeCrossAxis, direction, ownerWidth, node) +
782783
node->style().computeFlexEndPaddingAndBorder(
783-
nodeCrossAxis, direction, ownerWidth);
784+
nodeCrossAxis, direction, ownerWidth, node);
784785

785786
const bool nodeMainIsRow = isRow(nodeMainAxis);
786787
const float widthMin = nodeMainIsRow ? mainTotal : crossMax;
@@ -868,7 +869,7 @@ static FloatOptional computeAutoMinMainSize(
868869

869870
// §4.5: cap by the max main size.
870871
const FloatOptional maxMain = child->style().resolvedMaxDimension(
871-
direction, mainDim, ownerMainAxisSize, ownerWidth);
872+
direction, mainDim, ownerMainAxisSize, ownerWidth, child);
872873
if (maxMain.isDefined() && floor > maxMain) {
873874
floor = maxMain;
874875
}
@@ -2111,8 +2112,8 @@ static void calculateLayoutImpl(
21112112
leadingCrossDim += yoga::maxOrDefined(0.0f, remainingCrossDim / 2);
21122113
} else if (child->style().flexEndMarginIsAuto(crossAxis, direction)) {
21132114
// No-Op
2114-
} else if (child->style().flexStartMarginIsAuto(
2115-
crossAxis, direction)) {
2115+
} else if (
2116+
child->style().flexStartMarginIsAuto(crossAxis, direction)) {
21162117
leadingCrossDim += yoga::maxOrDefined(0.0f, remainingCrossDim);
21172118
} else if (alignItem == Align::FlexStart) {
21182119
// No-Op
@@ -2755,10 +2756,11 @@ void calculateLayout(
27552756
node->style().computeMarginForAxis(
27562757
FlexDirection::Row, ownerWidth, node));
27572758
widthSizingMode = SizingMode::StretchFit;
2758-
} else if (style
2759-
.resolvedMaxDimension(
2760-
direction, Dimension::Width, ownerWidth, ownerWidth, node)
2761-
.isDefined()) {
2759+
} else if (
2760+
style
2761+
.resolvedMaxDimension(
2762+
direction, Dimension::Width, ownerWidth, ownerWidth, node)
2763+
.isDefined()) {
27622764
width = style
27632765
.resolvedMaxDimension(
27642766
direction, Dimension::Width, ownerWidth, ownerWidth, node)
@@ -2783,14 +2785,11 @@ void calculateLayout(
27832785
node->style().computeMarginForAxis(
27842786
FlexDirection::Column, ownerWidth, node));
27852787
heightSizingMode = SizingMode::StretchFit;
2786-
} else if (style
2787-
.resolvedMaxDimension(
2788-
direction,
2789-
Dimension::Height,
2790-
ownerHeight,
2791-
ownerWidth,
2792-
node)
2793-
.isDefined()) {
2788+
} else if (
2789+
style
2790+
.resolvedMaxDimension(
2791+
direction, Dimension::Height, ownerHeight, ownerWidth, node)
2792+
.isDefined()) {
27942793
height =
27952794
style
27962795
.resolvedMaxDimension(

packages/react-native/ReactCommon/yoga/yoga/style/Style.h

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,13 @@ class YG_EXPORT Style {
293293
Direction direction,
294294
Dimension axis,
295295
float referenceLength,
296-
float ownerWidth) const {
296+
float ownerWidth,
297+
YGNodeConstRef node) const {
297298
const auto handle = minDimensions_[yoga::to_underlying(axis)];
298299
if (handle.isUndefined()) {
299300
return FloatOptional{};
300301
}
301-
FloatOptional value = resolve(handle, referenceLength);
302+
FloatOptional value = resolve(handle, referenceLength, node);
302303
if (boxSizing() == BoxSizing::BorderBox || !value.isDefined()) {
303304
return value;
304305
}
@@ -322,12 +323,13 @@ class YG_EXPORT Style {
322323
Direction direction,
323324
Dimension axis,
324325
float referenceLength,
325-
float ownerWidth) const {
326+
float ownerWidth,
327+
YGNodeConstRef node) const {
326328
const auto handle = maxDimensions_[yoga::to_underlying(axis)];
327329
if (handle.isUndefined()) {
328330
return FloatOptional{};
329331
}
330-
FloatOptional value = resolve(handle, referenceLength);
332+
FloatOptional value = resolve(handle, referenceLength, node);
331333
if (boxSizing() == BoxSizing::BorderBox || !value.isDefined()) {
332334
return value;
333335
}
@@ -416,72 +418,87 @@ class YG_EXPORT Style {
416418
float computeFlexStartPosition(
417419
FlexDirection axis,
418420
Direction direction,
419-
float axisSize) const {
420-
return resolve(computePosition(flexStartEdge(axis), direction), axisSize)
421+
float axisSize,
422+
YGNodeConstRef node) const {
423+
return resolve(
424+
computePosition(flexStartEdge(axis), direction), axisSize, node)
421425
.unwrapOrDefault(0.0f);
422426
}
423427

424428
float computeInlineStartPosition(
425429
FlexDirection axis,
426430
Direction direction,
427-
float axisSize) const {
431+
float axisSize,
432+
YGNodeConstRef node) const {
428433
return resolve(
429434
computePosition(inlineStartEdge(axis, direction), direction),
430-
axisSize)
435+
axisSize,
436+
node)
431437
.unwrapOrDefault(0.0f);
432438
}
433439

434440
float computeFlexEndPosition(
435441
FlexDirection axis,
436442
Direction direction,
437-
float axisSize) const {
438-
return resolve(computePosition(flexEndEdge(axis), direction), axisSize)
443+
float axisSize,
444+
YGNodeConstRef node) const {
445+
return resolve(
446+
computePosition(flexEndEdge(axis), direction), axisSize, node)
439447
.unwrapOrDefault(0.0f);
440448
}
441449

442450
float computeInlineEndPosition(
443451
FlexDirection axis,
444452
Direction direction,
445-
float axisSize) const {
453+
float axisSize,
454+
YGNodeConstRef node) const {
446455
return resolve(
447456
computePosition(inlineEndEdge(axis, direction), direction),
448-
axisSize)
457+
axisSize,
458+
node)
449459
.unwrapOrDefault(0.0f);
450460
}
451461

452462
float computeFlexStartMargin(
453463
FlexDirection axis,
454464
Direction direction,
455-
float widthSize) const {
456-
return resolve(computeMargin(flexStartEdge(axis), direction), widthSize)
465+
float widthSize,
466+
YGNodeConstRef node) const {
467+
return resolve(
468+
computeMargin(flexStartEdge(axis), direction), widthSize, node)
457469
.unwrapOrDefault(0.0f);
458470
}
459471

460472
float computeInlineStartMargin(
461473
FlexDirection axis,
462474
Direction direction,
463-
float widthSize) const {
475+
float widthSize,
476+
YGNodeConstRef node) const {
464477
return resolve(
465478
computeMargin(inlineStartEdge(axis, direction), direction),
466-
widthSize)
479+
widthSize,
480+
node)
467481
.unwrapOrDefault(0.0f);
468482
}
469483

470484
float computeFlexEndMargin(
471485
FlexDirection axis,
472486
Direction direction,
473-
float widthSize) const {
474-
return resolve(computeMargin(flexEndEdge(axis), direction), widthSize)
487+
float widthSize,
488+
YGNodeConstRef node) const {
489+
return resolve(computeMargin(flexEndEdge(axis), direction), widthSize, node)
475490
.unwrapOrDefault(0.0f);
476491
}
477492

478493
float computeInlineEndMargin(
479494
FlexDirection axis,
480495
Direction direction,
481-
float widthSize) const {
496+
float widthSize,
497+
YGNodeConstRef node) const {
482498
return resolve(
483499
computeMargin(inlineEndEdge(axis, direction), direction),
484-
widthSize)
500+
widthSize,
501+
node)
485502
.unwrapOrDefault(0.0f);
486503
}
487504

@@ -490,7 +507,8 @@ class YG_EXPORT Style {
490507
Direction direction,
491508
YGNodeConstRef node) const {
492509
return maxOrDefined(
493-
resolve(computeBorder(flexStartEdge(axis), direction), 0.0f).unwrap(),
510+
resolve(computeBorder(flexStartEdge(axis), direction), 0.0f, node)
511+
.unwrap(),
494512
0.0f);
495513
}
496514

@@ -500,7 +518,9 @@ class YG_EXPORT Style {
500518
YGNodeConstRef node) const {
501519
return maxOrDefined(
502520
resolve(
503-
computeBorder(inlineStartEdge(axis, direction), direction), 0.0f)
521+
computeBorder(inlineStartEdge(axis, direction), direction),
522+
0.0f,
523+
node)
504524
.unwrap(),
505525
0.0f);
506526
}
@@ -510,7 +530,8 @@ class YG_EXPORT Style {
510530
Direction direction,
511531
YGNodeConstRef node) const {
512532
return maxOrDefined(
513-
resolve(computeBorder(flexEndEdge(axis), direction), 0.0f).unwrap(),
533+
resolve(computeBorder(flexEndEdge(axis), direction), 0.0f, node)
534+
.unwrap(),
514535
0.0f);
515536
}
516537

@@ -519,7 +540,10 @@ class YG_EXPORT Style {
519540
Direction direction,
520541
YGNodeConstRef node) const {
521542
return maxOrDefined(
522-
resolve(computeBorder(inlineEndEdge(axis, direction), direction), 0.0f)
543+
resolve(
544+
computeBorder(inlineEndEdge(axis, direction), direction),
545+
0.0f,
546+
node)
523547
.unwrap(),
524548
0.0f);
525549
}
@@ -530,7 +554,7 @@ class YG_EXPORT Style {
530554
float widthSize,
531555
YGNodeConstRef node) const {
532556
return maxOrDefined(
533-
resolve(computePadding(flexStartEdge(axis), direction), widthSize)
557+
resolve(computePadding(flexStartEdge(axis), direction), widthSize, node)
534558
.unwrap(),
535559
0.0f);
536560
}
@@ -543,7 +567,8 @@ class YG_EXPORT Style {
543567
return maxOrDefined(
544568
resolve(
545569
computePadding(inlineStartEdge(axis, direction), direction),
546-
widthSize)
570+
widthSize,
571+
node)
547572
.unwrap(),
548573
0.0f);
549574
}
@@ -554,7 +579,7 @@ class YG_EXPORT Style {
554579
float widthSize,
555580
YGNodeConstRef node) const {
556581
return maxOrDefined(
557-
resolve(computePadding(flexEndEdge(axis), direction), widthSize)
582+
resolve(computePadding(flexEndEdge(axis), direction), widthSize, node)
558583
.unwrap(),
559584
0.0f);
560585
}
@@ -567,7 +592,8 @@ class YG_EXPORT Style {
567592
return maxOrDefined(
568593
resolve(
569594
computePadding(inlineEndEdge(axis, direction), direction),
570-
widthSize)
595+
widthSize,
596+
node)
571597
.unwrap(),
572598
0.0f);
573599
}
@@ -632,6 +658,8 @@ class YG_EXPORT Style {
632658
FlexDirection axis,
633659
float widthSize,
634660
YGNodeConstRef node) const {
661+
// The total margin for a given axis does not depend on the direction
662+
// so hardcoding LTR here to avoid piping direction to this function
635663
return computeInlineStartMargin(axis, Direction::LTR, widthSize, node) +
636664
computeInlineEndMargin(axis, Direction::LTR, widthSize, node);
637665
}
@@ -641,7 +669,7 @@ class YG_EXPORT Style {
641669
float ownerSize,
642670
YGNodeConstRef node) const {
643671
auto gap = isRow(axis) ? computeColumnGap() : computeRowGap();
644-
return maxOrDefined(resolve(gap, ownerSize).unwrap(), 0.0f);
672+
return maxOrDefined(resolve(gap, ownerSize, node).unwrap(), 0.0f);
645673
}
646674

647675
float computeGapForDimension(
@@ -650,7 +678,7 @@ class YG_EXPORT Style {
650678
YGNodeConstRef node) const {
651679
auto gap =
652680
dimension == Dimension::Width ? computeColumnGap() : computeRowGap();
653-
return maxOrDefined(resolve(gap, ownerSize).unwrap(), 0.0f);
681+
return maxOrDefined(resolve(gap, ownerSize, node).unwrap(), 0.0f);
654682
}
655683

656684
bool flexStartMarginIsAuto(FlexDirection axis, Direction direction) const {
@@ -913,14 +941,28 @@ class YG_EXPORT Style {
913941
* StyleLength/StyleSizeLength object on the stack during hot-path overhead
914942
* calculations.
915943
*/
916-
FloatOptional resolve(StyleValueHandle handle, float referenceLength) const {
944+
FloatOptional resolve(
945+
StyleValueHandle handle,
946+
float referenceLength,
947+
YGNodeConstRef node) const {
917948
if (handle.isPoint()) {
918949
return FloatOptional{pool_.getStoredValue(handle)};
919950
}
920951
if (handle.isPercent()) {
921952
return FloatOptional{
922953
pool_.getStoredValue(handle) * referenceLength * 0.01f};
923954
}
955+
if (handle.isDynamic()) {
956+
auto callback = pool_.getDynamicCallback(handle);
957+
if (callback) {
958+
return FloatOptional{callback(
959+
node,
960+
pool_.getDynamicCallbackID(handle),
961+
YGValueDynamicContext{referenceLength})
962+
.value};
963+
}
964+
}
965+
924966
return FloatOptional{};
925967
}
926968

packages/react-native/ReactCommon/yoga/yoga/style/StyleValueHandle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class StyleValueHandle {
5757
return type() == Type::Point;
5858
}
5959

60+
constexpr bool isDynamic() const {
61+
return type() == Type::Dynamic;
62+
}
63+
6064
private:
6165
friend class StyleValuePool;
6266

0 commit comments

Comments
 (0)