Skip to content

Commit 992e37c

Browse files
woehrl01facebook-github-bot
authored andcommitted
Fix sizing of non strech items
Summary: Fixes the sizing of items so that under most scenarios it calcultes its height by it's content for non exact measurings. This introduces a new useLegacyStretchBehaviour flag on the config to opt out of this change as it is breaking. See react/yoga#505 Closes react/yoga#506 Reviewed By: astreet Differential Revision: D4954016 Pulled By: emilsjolander fbshipit-source-id: d28bd5d174cd76951fb94df85e3b0cfab7f81ff7
1 parent 9934131 commit 992e37c

9 files changed

Lines changed: 40 additions & 16 deletions

File tree

React/Views/RCTShadowView.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ + (YGConfigRef)yogaConfig
5454
yogaConfig = YGConfigNew();
5555
// Turnig off pixel rounding.
5656
YGConfigSetPointScaleFactor(yogaConfig, 0.0);
57+
YGConfigSetUseLegacyStretchBehaviour(yogaConfig, true);
5758
});
5859
return yogaConfig;
5960
}
@@ -347,7 +348,6 @@ - (instancetype)init
347348
_reactSubviews = [NSMutableArray array];
348349

349350
_yogaNode = YGNodeNewWithConfig([[self class] yogaConfig]);
350-
351351
YGNodeSetContext(_yogaNode, (__bridge void *)self);
352352
YGNodeSetPrintFunc(_yogaNode, RCTPrint);
353353
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public ReactShadowNode() {
8787
if (sYogaConfig == null) {
8888
sYogaConfig = new YogaConfig();
8989
sYogaConfig.setPointScaleFactor(0f);
90+
sYogaConfig.setUseLegacyStretchBehaviour(true);
9091
}
9192
if (node == null) {
9293
node = new YogaNode(sYogaConfig);

ReactAndroid/src/main/java/com/facebook/yoga/YogaConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,15 @@ public void setUseWebDefaults(boolean useWebDefaults) {
5656
public void setPointScaleFactor(float pixelsInPoint) {
5757
jni_YGConfigSetPointScaleFactor(mNativePointer, pixelsInPoint);
5858
}
59+
60+
private native void jni_YGConfigSetUseLegacyStretchBehaviour(long nativePointer, boolean useLegacyStretchBehaviour);
61+
62+
/**
63+
* Yoga previously had an error where containers would take the maximum space possible instead of the minimum
64+
* like they are supposed to. In practice this resulted in implicit behaviour similar to align-self: stretch;
65+
* Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour.
66+
*/
67+
public void setUseLegacyStretchBehaviour(boolean useLegacyStretchBehaviour) {
68+
jni_YGConfigSetUseLegacyStretchBehaviour(mNativePointer, useLegacyStretchBehaviour);
69+
}
5970
}

ReactAndroid/src/main/java/com/facebook/yoga/YogaExperimentalFeature.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
@DoNotStrip
1515
public enum YogaExperimentalFeature {
16-
WEB_FLEX_BASIS(0),
17-
MIN_FLEX_FIX(1);
16+
WEB_FLEX_BASIS(0);
1817

1918
private int mIntValue;
2019

@@ -29,7 +28,6 @@ public int intValue() {
2928
public static YogaExperimentalFeature fromInt(int value) {
3029
switch (value) {
3130
case 0: return WEB_FLEX_BASIS;
32-
case 1: return MIN_FLEX_FIX;
3331
default: throw new IllegalArgumentException("Unknown enum value: " + value);
3432
}
3533
}

ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ void jni_YGConfigSetPointScaleFactor(alias_ref<jobject>, jlong nativePointer, jf
408408
YGConfigSetPointScaleFactor(config, pixelsInPoint);
409409
}
410410

411+
void jni_YGConfigSetUseLegacyStretchBehaviour(alias_ref<jobject>, jlong nativePointer, jboolean useLegacyStretchBehaviour) {
412+
const YGConfigRef config = _jlong2YGConfigRef(nativePointer);
413+
YGConfigSetUseLegacyStretchBehaviour(config, useLegacyStretchBehaviour);
414+
}
415+
411416
jint jni_YGNodeGetInstanceCount(alias_ref<jclass> clazz) {
412417
return YGNodeGetInstanceCount();
413418
}
@@ -504,6 +509,7 @@ jint JNI_OnLoad(JavaVM *vm, void *) {
504509
YGMakeNativeMethod(jni_YGConfigSetExperimentalFeatureEnabled),
505510
YGMakeNativeMethod(jni_YGConfigSetUseWebDefaults),
506511
YGMakeNativeMethod(jni_YGConfigSetPointScaleFactor),
512+
YGMakeNativeMethod(jni_YGConfigSetUseLegacyStretchBehaviour),
507513
});
508514
});
509515
}

ReactCommon/yoga/yoga/YGEnums.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ const char *YGExperimentalFeatureToString(const YGExperimentalFeature value){
9191
switch(value){
9292
case YGExperimentalFeatureWebFlexBasis:
9393
return "web-flex-basis";
94-
case YGExperimentalFeatureMinFlexFix:
95-
return "min-flex-fix";
9694
}
9795
return "unknown";
9896
}

ReactCommon/yoga/yoga/YGEnums.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ typedef YG_ENUM_BEGIN(YGEdge) {
6262
} YG_ENUM_END(YGEdge);
6363
WIN_EXPORT const char *YGEdgeToString(const YGEdge value);
6464

65-
#define YGExperimentalFeatureCount 2
65+
#define YGExperimentalFeatureCount 1
6666
typedef YG_ENUM_BEGIN(YGExperimentalFeature) {
6767
YGExperimentalFeatureWebFlexBasis,
68-
YGExperimentalFeatureMinFlexFix,
6968
} YG_ENUM_END(YGExperimentalFeature);
7069
WIN_EXPORT const char *YGExperimentalFeatureToString(const YGExperimentalFeature value);
7170

ReactCommon/yoga/yoga/Yoga.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ typedef struct YGStyle {
9797
typedef struct YGConfig {
9898
bool experimentalFeatures[YGExperimentalFeatureCount + 1];
9999
bool useWebDefaults;
100+
bool useLegacyStretchBehaviour;
100101
float pointScaleFactor;
101102
} YGConfig;
102103

@@ -202,7 +203,6 @@ static YGNode gYGNodeDefaults = {
202203
static YGConfig gYGConfigDefaults = {
203204
.experimentalFeatures =
204205
{
205-
[YGExperimentalFeatureMinFlexFix] = false,
206206
[YGExperimentalFeatureWebFlexBasis] = false,
207207
},
208208
.useWebDefaults = false,
@@ -2199,23 +2199,25 @@ static void YGNodelayoutImpl(const YGNodeRef node,
21992199
// If the main dimension size isn't known, it is computed based on
22002200
// the line length, so there's no more space left to distribute.
22012201

2202+
bool sizeBasedOnContent = false;
22022203
// If we don't measure with exact main dimension we want to ensure we don't violate min and max
22032204
if (measureModeMainDim != YGMeasureModeExactly) {
22042205
if (!YGFloatIsUndefined(minInnerMainDim) && sizeConsumedOnCurrentLine < minInnerMainDim) {
22052206
availableInnerMainDim = minInnerMainDim;
22062207
} else if (!YGFloatIsUndefined(maxInnerMainDim) && sizeConsumedOnCurrentLine > maxInnerMainDim) {
22072208
availableInnerMainDim = maxInnerMainDim;
2208-
} else if (YGConfigIsExperimentalFeatureEnabled(node->config, YGExperimentalFeatureMinFlexFix) &&
2209-
(totalFlexGrowFactors == 0 || YGResolveFlexGrow(node) == 0)) {
2210-
// TODO: this needs to be moved out of experimental feature, as this is legitimate fix
2211-
// If we don't have any children to flex or we can't flex the node itself,
2212-
// space we've used is all space we need
2213-
availableInnerMainDim = sizeConsumedOnCurrentLine;
2209+
} else {
2210+
if (!node->config->useLegacyStretchBehaviour && (totalFlexGrowFactors == 0 || YGResolveFlexGrow(node) == 0)) {
2211+
// If we don't have any children to flex or we can't flex the node itself,
2212+
// space we've used is all space we need
2213+
availableInnerMainDim = sizeConsumedOnCurrentLine;
2214+
}
2215+
sizeBasedOnContent = true;
22142216
}
22152217
}
22162218

22172219
float remainingFreeSpace = 0;
2218-
if (!YGFloatIsUndefined(availableInnerMainDim)) {
2220+
if ((!sizeBasedOnContent || node->config->useLegacyStretchBehaviour) && !YGFloatIsUndefined(availableInnerMainDim)) {
22192221
remainingFreeSpace = availableInnerMainDim - sizeConsumedOnCurrentLine;
22202222
} else if (sizeConsumedOnCurrentLine < 0) {
22212223
// availableInnerMainDim is indefinite which means the node is being sized
@@ -3427,6 +3429,10 @@ void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled) {
34273429
config->useWebDefaults = enabled;
34283430
}
34293431

3432+
void YGConfigSetUseLegacyStretchBehaviour(const YGConfigRef config, const bool useLegacyStretchBehaviour) {
3433+
config->useLegacyStretchBehaviour = useLegacyStretchBehaviour;
3434+
}
3435+
34303436
bool YGConfigGetUseWebDefaults(const YGConfigRef config) {
34313437
return config->useWebDefaults;
34323438
}

ReactCommon/yoga/yoga/Yoga.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ WIN_EXPORT void YGLog(YGLogLevel level, const char *message, ...);
225225
// If you want to avoid rounding - set PointScaleFactor to 0
226226
WIN_EXPORT void YGConfigSetPointScaleFactor(const YGConfigRef config, const float pixelsInPoint);
227227

228+
// Yoga previously had an error where containers would take the maximum space possible instead of the minimum
229+
// like they are supposed to. In practice this resulted in implicit behaviour similar to align-self: stretch;
230+
// Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour.
231+
WIN_EXPORT void YGConfigSetUseLegacyStretchBehaviour(const YGConfigRef config, const bool useLegacyStretchBehaviour);
232+
228233
// YGConfig
229234
WIN_EXPORT YGConfigRef YGConfigNew(void);
230235
WIN_EXPORT void YGConfigFree(const YGConfigRef config);

0 commit comments

Comments
 (0)