Skip to content

Commit

Permalink
Implement Percentage support for gap styles (#980)
Browse files Browse the repository at this point in the history
Summary:

X-link: facebook/yoga#1643

Changelog [Internal]:
- Added percentage value for flex layout gap
- Wired up to pass proper available width and height to implement this feature

Differential Revision: D56002340
  • Loading branch information
realsoelynn authored and facebook-github-bot committed Apr 12, 2024
1 parent 2d938d8 commit 57e9865
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/yoga/src/main/cpp/yoga/YGNodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ void YGNodeStyleSetGap(
node, scopedEnum(gutter), value::points(gapLength));
}

void YGNodeStyleSetGapPercent(YGNodeRef node, YGGutter gutter, float percent) {
updateStyle<&Style::gap, &Style::setGap>(
node, scopedEnum(gutter), value::percent(percent));
}

float YGNodeStyleGetGap(const YGNodeConstRef node, const YGGutter gutter) {
auto gapLength = resolveRef(node)->style().gap(scopedEnum(gutter));
if (gapLength.isUndefined() || gapLength.isAuto()) {
Expand Down
2 changes: 2 additions & 0 deletions lib/yoga/src/main/cpp/yoga/YGNodeStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ YG_EXPORT float YGNodeStyleGetBorder(YGNodeConstRef node, YGEdge edge);

YG_EXPORT void
YGNodeStyleSetGap(YGNodeRef node, YGGutter gutter, float gapLength);
YG_EXPORT void
YGNodeStyleSetGapPercent(YGNodeRef node, YGGutter gutter, float gapLength);
YG_EXPORT float YGNodeStyleGetGap(YGNodeConstRef node, YGGutter gutter);

YG_EXPORT void YGNodeStyleSetWidth(YGNodeRef node, float width);
Expand Down
10 changes: 7 additions & 3 deletions lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,8 @@ static void justifyMainAxis(
node->style().computeFlexEndPaddingAndBorder(
mainAxis, direction, ownerWidth);

const float gap = node->style().computeGapForAxis(mainAxis);
const float gap =
node->style().computeGapForAxis(mainAxis, availableInnerMainDim);
// If we are using "at most" rules in the main axis, make sure that
// remainingFreeSpace is 0 when min main dimension is not given
if (sizingModeMainDim == SizingMode::FitContent &&
Expand Down Expand Up @@ -1380,7 +1381,8 @@ static void calculateLayoutImpl(
generationCount);

if (childCount > 1) {
totalMainDim += node->style().computeGapForAxis(mainAxis) *
totalMainDim +=
node->style().computeGapForAxis(mainAxis, availableInnerMainDim) *
static_cast<float>(childCount - 1);
}

Expand All @@ -1404,7 +1406,8 @@ static void calculateLayoutImpl(
// Accumulated cross dimensions of all lines so far.
float totalLineCrossDim = 0;

const float crossAxisGap = node->style().computeGapForAxis(crossAxis);
const float crossAxisGap =
node->style().computeGapForAxis(crossAxis, availableInnerCrossDim);

// Max main dimension of all the lines.
float maxLineMainDim = 0;
Expand All @@ -1414,6 +1417,7 @@ static void calculateLayoutImpl(
node,
ownerDirection,
mainAxisownerSize,
availableInnerMainDim,
availableInnerWidth,
availableInnerMainDim,
startOfLineIndex,
Expand Down
4 changes: 3 additions & 1 deletion lib/yoga/src/main/cpp/yoga/algorithm/FlexLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ FlexLine calculateFlexLine(
yoga::Node* const node,
const Direction ownerDirection,
const float mainAxisownerSize,
const float mainAxisAvailableSize,
const float availableInnerWidth,
const float availableInnerMainDim,
const size_t startOfLineIndex,
Expand All @@ -34,7 +35,8 @@ FlexLine calculateFlexLine(
const FlexDirection mainAxis = resolveDirection(
node->style().flexDirection(), node->resolveDirection(ownerDirection));
const bool isNodeFlexWrap = node->style().flexWrap() != Wrap::NoWrap;
const float gap = node->style().computeGapForAxis(mainAxis);
const float gap =
node->style().computeGapForAxis(mainAxis, mainAxisAvailableSize);

// Add items to the current line until it's full or we run out of items.
for (; endOfLineIndex < node->getChildren().size(); endOfLineIndex++) {
Expand Down
1 change: 1 addition & 0 deletions lib/yoga/src/main/cpp/yoga/algorithm/FlexLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ FlexLine calculateFlexLine(
yoga::Node* node,
Direction ownerDirection,
float mainAxisownerSize,
float mainAxisAvailableSize,
float availableInnerWidth,
float availableInnerMainDim,
size_t startOfLineIndex,
Expand Down
6 changes: 2 additions & 4 deletions lib/yoga/src/main/cpp/yoga/style/Style.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,9 @@ class YG_EXPORT Style {
computeInlineEndMargin(axis, Direction::LTR, widthSize);
}

float computeGapForAxis(FlexDirection axis) const {
float computeGapForAxis(FlexDirection axis, float ownerSize) const {
auto gap = isRow(axis) ? computeColumnGap() : computeRowGap();
// TODO: Validate percentage gap, and expose ability to set percentage to
// public API
return maxOrDefined(gap.resolve(0.0f /*ownerSize*/).unwrap(), 0.0f);
return maxOrDefined(gap.resolve(ownerSize).unwrap(), 0.0f);
}

bool flexStartMarginIsAuto(FlexDirection axis, Direction direction) const {
Expand Down
1 change: 1 addition & 0 deletions lib/yoga/src/main/java/com/facebook/yoga/YogaNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public class YogaNative {
static native void jni_YGNodeStyleSetAspectRatioJNI(long nativePointer, float aspectRatio);
static native float jni_YGNodeStyleGetGapJNI(long nativePointer, int gutter);
static native void jni_YGNodeStyleSetGapJNI(long nativePointer, int gutter, float gapLength);
static native void jni_YGNodeStyleSetGapPercentJNI(long nativePointer, int gutter, float gapLength);
static native void jni_YGNodeSetHasMeasureFuncJNI(long nativePointer, boolean hasMeasureFunc);
static native void jni_YGNodeSetHasBaselineFuncJNI(long nativePointer, boolean hasMeasureFunc);
static native void jni_YGNodeSetStyleInputsJNI(long nativePointer, float[] styleInputsArray, int size);
Expand Down
2 changes: 2 additions & 0 deletions lib/yoga/src/main/java/com/facebook/yoga/YogaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public interface Inputs {

public abstract void setGap(YogaGutter gutter, float gapLength);

public abstract void setGapPercent(YogaGutter gutter, float gapLength);

public abstract float getLayoutX();

public abstract float getLayoutY();
Expand Down
5 changes: 5 additions & 0 deletions lib/yoga/src/main/java/com/facebook/yoga/YogaNodeJNIBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -721,4 +721,9 @@ public float getGap(YogaGutter gutter) {
public void setGap(YogaGutter gutter, float gapLength) {
YogaNative.jni_YGNodeStyleSetGapJNI(mNativePointer, gutter.intValue(), gapLength);
}

@Override
public void setGapPercent(YogaGutter gutter, float gapLength) {
YogaNative.jni_YGNodeStyleSetGapPercentJNI(mNativePointer, gutter.intValue(), gapLength);
}
}
15 changes: 15 additions & 0 deletions lib/yogajni/src/main/cpp/jni/YGJNIVanilla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,18 @@ static void jni_YGNodeStyleSetGapJNI(
static_cast<float>(gapLength));
}

static void jni_YGNodeStyleSetGapPercentJNI(
JNIEnv* /*env*/,
jobject /*obj*/,
jlong nativePointer,
jint gutter,
jfloat gapLength) {
YGNodeStyleSetGapPercent(
_jlong2YGNodeRef(nativePointer),
static_cast<YGGutter>(gutter),
static_cast<float>(gapLength));
}

// Yoga specific properties, not compatible with flexbox specification
YG_NODE_JNI_STYLE_PROP(jfloat, float, AspectRatio);

Expand Down Expand Up @@ -944,6 +956,9 @@ static JNINativeMethod methods[] = {
(void*)jni_YGNodeSetHasMeasureFuncJNI},
{"jni_YGNodeStyleGetGapJNI", "(JI)F", (void*)jni_YGNodeStyleGetGapJNI},
{"jni_YGNodeStyleSetGapJNI", "(JIF)V", (void*)jni_YGNodeStyleSetGapJNI},
{"jni_YGNodeStyleSetGapPercentJNI",
"(JIF)V",
(void*)jni_YGNodeStyleSetGapPercentJNI},
{"jni_YGNodeSetHasBaselineFuncJNI",
"(JZ)V",
(void*)jni_YGNodeSetHasBaselineFuncJNI},
Expand Down

0 comments on commit 57e9865

Please sign in to comment.