Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exposing baseline function in litho #783

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/CommonPropsHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.litho.drawable.DrawableUtils;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaBaselineFunction;
import com.facebook.yoga.YogaConstants;
import com.facebook.yoga.YogaDirection;
import com.facebook.yoga.YogaEdge;
Expand Down Expand Up @@ -304,6 +305,11 @@ public void useHeightAsBaseline(boolean useHeightAsBaseline) {
getOrCreateLayoutProps().useHeightAsBaseline(useHeightAsBaseline);
}

@Override
public void useCustomBaselineFunction(YogaBaselineFunction yogaBaselineFunction) {
getOrCreateLayoutProps().useCustomBaselineFunction(yogaBaselineFunction);
}

@Override
public void touchExpansionPx(YogaEdge edge, @Px int touchExpansion) {
getOrCreateOtherProps().touchExpansionPx(edge, touchExpansion);
Expand Down Expand Up @@ -990,6 +996,7 @@ static class DefaultLayoutProps implements CopyableLayoutProps {
private static final int PFLAG_MARGIN_AUTO_IS_SET = 1 << 27;
private static final int PFLAG_IS_REFERENCE_BASELINE_IS_SET = 1 << 28;
private static final int PFLAG_USE_HEIGHT_AS_BASELINE_IS_SET = 1 << 29;
private static final int PFLAG_USE_CUSTOM_BASELINE_FUNCTION = 1 << 30;

private int mPrivateFlags;

Expand Down Expand Up @@ -1021,6 +1028,7 @@ static class DefaultLayoutProps implements CopyableLayoutProps {
@Nullable private Edges mPaddings;
@Nullable private Edges mPaddingPercents;
@Nullable private Edges mPositionPercents;
@Nullable private YogaBaselineFunction mYogaBaselineFunction;
private boolean mIsReferenceBaseline;
private boolean mUseHeightAsBaseline;

Expand Down Expand Up @@ -1227,6 +1235,12 @@ public void useHeightAsBaseline(boolean useHeightAsBaseline) {
mUseHeightAsBaseline = useHeightAsBaseline;
}

@Override
public void useCustomBaselineFunction(YogaBaselineFunction yogaBaselineFunction) {
mPrivateFlags |= PFLAG_USE_CUSTOM_BASELINE_FUNCTION;
mYogaBaselineFunction = yogaBaselineFunction;
}

@Override
public void copyInto(LayoutProps target) {
if ((mPrivateFlags & PFLAG_WIDTH_IS_SET) != 0L) {
Expand Down Expand Up @@ -1351,6 +1365,9 @@ public void copyInto(LayoutProps target) {
if ((mPrivateFlags & PFLAG_USE_HEIGHT_AS_BASELINE_IS_SET) != 0L) {
target.useHeightAsBaseline(mUseHeightAsBaseline);
}
if ((mPrivateFlags & PFLAG_USE_CUSTOM_BASELINE_FUNCTION) != 0L) {
target.useCustomBaselineFunction(mYogaBaselineFunction);
}
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.facebook.litho.drawable.ComparableColorDrawable;
import com.facebook.litho.drawable.ComparableDrawable;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaBaselineFunction;
import com.facebook.yoga.YogaDirection;
import com.facebook.yoga.YogaEdge;
import com.facebook.yoga.YogaJustify;
Expand Down Expand Up @@ -2210,6 +2211,15 @@ public T useHeightAsBaseline(boolean useHeightAsBaseline) {
return getThis();
}

/**
* Developers can provide a custom baseline function to calculate the special baseline modes
* which are not handled by Litho or Yoga layout, for instance baseline-first or baseline-last.
*/
public T useCustomBaselineFunction(YogaBaselineFunction yogaBaselineFunction) {
mComponent.getOrCreateCommonProps().useCustomBaselineFunction(yogaBaselineFunction);
return getThis();
}

public T viewTag(@Nullable Object viewTag) {
mComponent.getOrCreateCommonProps().viewTag(viewTag);
return getThis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,13 @@ public float baseline(YogaNode yogaNode, float width, float height) {
}
}

@Override
public void useCustomBaselineFunction(YogaBaselineFunction yogaBaselineFunction) {
if (yogaBaselineFunction != null) {
mYogaNode.setBaselineFunction(yogaBaselineFunction);
}
}

@Override
public InternalNode visibilityChangedHandler(
@Nullable EventHandler<VisibilityChangedEvent> visibilityChangedHandler) {
Expand Down
3 changes: 3 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/LayoutProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaBaselineFunction;
import com.facebook.yoga.YogaDirection;
import com.facebook.yoga.YogaEdge;
import com.facebook.yoga.YogaPositionType;
Expand Down Expand Up @@ -83,4 +84,6 @@ interface LayoutProps {
void isReferenceBaseline(boolean isReferenceBaseline);

void useHeightAsBaseline(boolean useHeightAsBaseline);

void useCustomBaselineFunction(YogaBaselineFunction yogaBaselineFunction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaBaselineFunction;
import com.facebook.yoga.YogaDirection;
import com.facebook.yoga.YogaEdge;
import com.facebook.yoga.YogaFlexDirection;
Expand Down Expand Up @@ -845,6 +846,9 @@ public void isReferenceBaseline(boolean isReferenceBaseline) {}
@Override
public void useHeightAsBaseline(boolean useHeightAsBaseline) {}

@Override
public void useCustomBaselineFunction(YogaBaselineFunction yogaBaselineFunction) {}

@Override
public @Nullable InternalNode wrap(YogaWrap wrap) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.view.View;
import com.facebook.litho.testing.inlinelayoutspec.InlineLayoutSpec;
import com.facebook.litho.testing.testrunner.LithoTestRunner;
import com.facebook.yoga.YogaBaselineFunction;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,6 +33,8 @@
public class YogaAlignBaselineTest {

private ComponentContext mContext;
private static final YogaBaselineFunction YOGA_BASELINE_FUNCTION =
(node, width, height) -> height;

@Before
public void setup() {
Expand Down Expand Up @@ -91,7 +94,7 @@ protected Component onCreateLayout(ComponentContext c) {
.heightPx(800)
.child(Column.create(c).widthPx(500).heightPx(300).wrapInView())
.child(Column.create(c).widthPx(500).heightPx(400).wrapInView())
.useHeightAsBaseline(true))
.useCustomBaselineFunction(YOGA_BASELINE_FUNCTION))
.alignItems(BASELINE)
.widthPx(1000)
.heightPx(1000)
Expand Down Expand Up @@ -166,7 +169,7 @@ protected Component onCreateLayout(ComponentContext c) {
.heightPx(800)
.child(Column.create(c).widthPx(500).heightPx(300).wrapInView())
.child(Column.create(c).widthPx(500).heightPx(400).wrapInView())
.useHeightAsBaseline(true))
.useCustomBaselineFunction(YOGA_BASELINE_FUNCTION))
.alignItems(BASELINE)
.widthPx(2000)
.heightPx(2000)
Expand Down