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 facebook/yoga#505 Closes https://github.com/facebook/yoga/pull/506 Reviewed By: astreet Differential Revision: D4954016 Pulled By: emilsjolander fbshipit-source-id: d28bd5d174cd76951fb94df85e3b0cfab7f81ff7
This commit is contained in:
committed by
Facebook Github Bot
parent
e3dbef7cbd
commit
203577724e
@@ -56,4 +56,15 @@ public class YogaConfig {
|
||||
public void setPointScaleFactor(float pixelsInPoint) {
|
||||
jni_YGConfigSetPointScaleFactor(mNativePointer, pixelsInPoint);
|
||||
}
|
||||
|
||||
private native void jni_YGConfigSetUseLegacyStretchBehaviour(long nativePointer, boolean useLegacyStretchBehaviour);
|
||||
|
||||
/**
|
||||
* Yoga previously had an error where containers would take the maximum space possible instead of the minimum
|
||||
* like they are supposed to. In practice this resulted in implicit behaviour similar to align-self: stretch;
|
||||
* Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour.
|
||||
*/
|
||||
public void setUseLegacyStretchBehaviour(boolean useLegacyStretchBehaviour) {
|
||||
jni_YGConfigSetUseLegacyStretchBehaviour(mNativePointer, useLegacyStretchBehaviour);
|
||||
}
|
||||
}
|
||||
|
@@ -13,8 +13,7 @@ import com.facebook.proguard.annotations.DoNotStrip;
|
||||
|
||||
@DoNotStrip
|
||||
public enum YogaExperimentalFeature {
|
||||
WEB_FLEX_BASIS(0),
|
||||
MIN_FLEX_FIX(1);
|
||||
WEB_FLEX_BASIS(0);
|
||||
|
||||
private int mIntValue;
|
||||
|
||||
@@ -29,7 +28,6 @@ public enum YogaExperimentalFeature {
|
||||
public static YogaExperimentalFeature fromInt(int value) {
|
||||
switch (value) {
|
||||
case 0: return WEB_FLEX_BASIS;
|
||||
case 1: return MIN_FLEX_FIX;
|
||||
default: throw new IllegalArgumentException("Unknown enum value: " + value);
|
||||
}
|
||||
}
|
||||
|
@@ -408,6 +408,11 @@ void jni_YGConfigSetPointScaleFactor(alias_ref<jobject>, jlong nativePointer, jf
|
||||
YGConfigSetPointScaleFactor(config, pixelsInPoint);
|
||||
}
|
||||
|
||||
void jni_YGConfigSetUseLegacyStretchBehaviour(alias_ref<jobject>, jlong nativePointer, jboolean useLegacyStretchBehaviour) {
|
||||
const YGConfigRef config = _jlong2YGConfigRef(nativePointer);
|
||||
YGConfigSetUseLegacyStretchBehaviour(config, useLegacyStretchBehaviour);
|
||||
}
|
||||
|
||||
jint jni_YGNodeGetInstanceCount(alias_ref<jclass> clazz) {
|
||||
return YGNodeGetInstanceCount();
|
||||
}
|
||||
@@ -504,6 +509,7 @@ jint JNI_OnLoad(JavaVM *vm, void *) {
|
||||
YGMakeNativeMethod(jni_YGConfigSetExperimentalFeatureEnabled),
|
||||
YGMakeNativeMethod(jni_YGConfigSetUseWebDefaults),
|
||||
YGMakeNativeMethod(jni_YGConfigSetPointScaleFactor),
|
||||
YGMakeNativeMethod(jni_YGConfigSetUseLegacyStretchBehaviour),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@@ -1791,4 +1791,145 @@ public class YGAlignItemsTest {
|
||||
assertEquals(72f, root_child0_child0.getLayoutHeight(), 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_align_center_should_size_based_on_content() {
|
||||
YogaConfig config = new YogaConfig();
|
||||
|
||||
final YogaNode root = new YogaNode(config);
|
||||
root.setAlignItems(YogaAlign.CENTER);
|
||||
root.setMargin(YogaEdge.TOP, 20f);
|
||||
root.setWidth(100f);
|
||||
root.setHeight(100f);
|
||||
|
||||
final YogaNode root_child0 = new YogaNode(config);
|
||||
root_child0.setJustifyContent(YogaJustify.CENTER);
|
||||
root_child0.setFlexShrink(1f);
|
||||
root.addChildAt(root_child0, 0);
|
||||
|
||||
final YogaNode root_child0_child0 = new YogaNode(config);
|
||||
root_child0_child0.setFlexGrow(1f);
|
||||
root_child0_child0.setFlexShrink(1f);
|
||||
root_child0.addChildAt(root_child0_child0, 0);
|
||||
|
||||
final YogaNode root_child0_child0_child0 = new YogaNode(config);
|
||||
root_child0_child0_child0.setWidth(20f);
|
||||
root_child0_child0_child0.setHeight(20f);
|
||||
root_child0_child0.addChildAt(root_child0_child0_child0, 0);
|
||||
root.setDirection(YogaDirection.LTR);
|
||||
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
|
||||
|
||||
assertEquals(0f, root.getLayoutX(), 0.0f);
|
||||
assertEquals(20f, root.getLayoutY(), 0.0f);
|
||||
assertEquals(100f, root.getLayoutWidth(), 0.0f);
|
||||
assertEquals(100f, root.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(40f, root_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(20f, root_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0_child0_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0_child0_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
root.setDirection(YogaDirection.RTL);
|
||||
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
|
||||
|
||||
assertEquals(0f, root.getLayoutX(), 0.0f);
|
||||
assertEquals(20f, root.getLayoutY(), 0.0f);
|
||||
assertEquals(100f, root.getLayoutWidth(), 0.0f);
|
||||
assertEquals(100f, root.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(40f, root_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(20f, root_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0_child0_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0_child0_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0_child0.getLayoutHeight(), 0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_align_strech_should_size_based_on_parent() {
|
||||
YogaConfig config = new YogaConfig();
|
||||
|
||||
final YogaNode root = new YogaNode(config);
|
||||
root.setMargin(YogaEdge.TOP, 20f);
|
||||
root.setWidth(100f);
|
||||
root.setHeight(100f);
|
||||
|
||||
final YogaNode root_child0 = new YogaNode(config);
|
||||
root_child0.setJustifyContent(YogaJustify.CENTER);
|
||||
root_child0.setFlexShrink(1f);
|
||||
root.addChildAt(root_child0, 0);
|
||||
|
||||
final YogaNode root_child0_child0 = new YogaNode(config);
|
||||
root_child0_child0.setFlexGrow(1f);
|
||||
root_child0_child0.setFlexShrink(1f);
|
||||
root_child0.addChildAt(root_child0_child0, 0);
|
||||
|
||||
final YogaNode root_child0_child0_child0 = new YogaNode(config);
|
||||
root_child0_child0_child0.setWidth(20f);
|
||||
root_child0_child0_child0.setHeight(20f);
|
||||
root_child0_child0.addChildAt(root_child0_child0_child0, 0);
|
||||
root.setDirection(YogaDirection.LTR);
|
||||
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
|
||||
|
||||
assertEquals(0f, root.getLayoutX(), 0.0f);
|
||||
assertEquals(20f, root.getLayoutY(), 0.0f);
|
||||
assertEquals(100f, root.getLayoutWidth(), 0.0f);
|
||||
assertEquals(100f, root.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(100f, root_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(100f, root_child0_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0_child0_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0_child0_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
root.setDirection(YogaDirection.RTL);
|
||||
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
|
||||
|
||||
assertEquals(0f, root.getLayoutX(), 0.0f);
|
||||
assertEquals(20f, root.getLayoutY(), 0.0f);
|
||||
assertEquals(100f, root.getLayoutWidth(), 0.0f);
|
||||
assertEquals(100f, root.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(100f, root_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(0f, root_child0_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(100f, root_child0_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0.getLayoutHeight(), 0.0f);
|
||||
|
||||
assertEquals(80f, root_child0_child0_child0.getLayoutX(), 0.0f);
|
||||
assertEquals(0f, root_child0_child0_child0.getLayoutY(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0_child0.getLayoutWidth(), 0.0f);
|
||||
assertEquals(20f, root_child0_child0_child0.getLayoutHeight(), 0.0f);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -357,7 +357,6 @@ public class YGMinMaxDimensionTest {
|
||||
@Test
|
||||
public void test_flex_grow_to_min() {
|
||||
YogaConfig config = new YogaConfig();
|
||||
config.setExperimentalFeatureEnabled(YogaExperimentalFeature.MIN_FLEX_FIX, true);
|
||||
|
||||
final YogaNode root = new YogaNode(config);
|
||||
root.setWidth(100f);
|
||||
@@ -412,7 +411,6 @@ public class YGMinMaxDimensionTest {
|
||||
@Test
|
||||
public void test_flex_grow_in_at_most_container() {
|
||||
YogaConfig config = new YogaConfig();
|
||||
config.setExperimentalFeatureEnabled(YogaExperimentalFeature.MIN_FLEX_FIX, true);
|
||||
|
||||
final YogaNode root = new YogaNode(config);
|
||||
root.setFlexDirection(YogaFlexDirection.ROW);
|
||||
|
@@ -1027,7 +1027,6 @@ public class YGPercentageTest {
|
||||
@Test
|
||||
public void test_percentage_container_in_wrapping_container() {
|
||||
YogaConfig config = new YogaConfig();
|
||||
config.setExperimentalFeatureEnabled(YogaExperimentalFeature.MIN_FLEX_FIX, true);
|
||||
|
||||
final YogaNode root = new YogaNode(config);
|
||||
root.setJustifyContent(YogaJustify.CENTER);
|
||||
|
Reference in New Issue
Block a user