react / react/yoga

[Java] Outdated node layout values in custom baseline function

Open
#616 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Java
Dominant language
C++
Stars
18.9k
Forks
1.6k
Avg merge
1m
Merged PRs (30d)
1

Description

Report

Issues and Steps to Reproduce

In custom baseline function, the YogaNode java object is containing old layout values (position, width, height etc.).

Sample code to reproduce issue (Comments added in the custom baseline function representing log output) :

YogaNode root = new YogaNode();
root.setWidth(500f);
root.setHeight(500f);
root.setFlexDirection(YogaFlexDirection.ROW);
root.setAlignItems(YogaAlign.BASELINE);
root.setWrap(YogaWrap.WRAP);

YogaNode child0 = new YogaNode();
child0.setWidth(50f);
child0.setHeight(50f);
root.addChildAt(child0, 0);

YogaNode child1 = new YogaNode();
child1.setBaselineFunction(new YogaBaselineFunction() {
    @Override
    public float baseline(YogaNode yogaNode, float width, float height) {
        YogaNode middleChild = yogaNode.getChildAt(1);

        // Output x: NaN, y: NaN
        Log.d(TAG, String.format("x: %f, y: %f", middleChild.getLayoutX(), middleChild.getLayoutY()));


        /*
        Outputs :

        <div
        layout="
        width: 25;
        height: 1;
        top: 25;
        left: 0;
        "
        style="
        width: 25px;
        height: 1px;
        "
        >
        </div>
        */
        middleChild.print();

        return middleChild.getLayoutY()+middleChild.getLayoutHeight();
    }
});
child1.setFlexDirection(YogaFlexDirection.COLUMN);
child1.setWidth(200f);
child1.setHeight(100f);
root.addChildAt(child1, 1);

YogaNode child1_0 = new YogaNode();
child1_0.setWidth(25f);
child1_0.setHeight(25f);
child1.addChildAt(child1_0, 0);

YogaNode child1_1 = new YogaNode();
child1_1.setWidth(25f);
child1_1.setHeight(1f);
child1.addChildAt(child1_1, 1);

YogaNode child1_2 = new YogaNode();
child1_2.setWidth(25f);
child1_2.setHeight(25f);
child1.addChildAt(child1_2, 2);

YogaNode child2 = new YogaNode();
child2.setWidth(50f);
child2.setHeight(50f);
root.addChildAt(child2, 2);

root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

Expected Behavior

Inside custom baseline function javaYogaNode.getLayoutX() etc. in java side should give same values as the values obtained using YGNodeLayoutGetLeft(node) etc. in C side

Actual Behavior

Values are different since javaYogaNode.getLayoutX() is returning the value of mLeft field in YogaNode class. When you call calculateLayout, only after calculating the layout of all the nodes, layout fields are updated in java object. But in between calculation of layout, custom baseline function will be called.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the YogaNode layout accessors, calculateLayout, and the YogaBaselineFunction callback shown in the reproduction; compare Java values during the callback with YGNodeLayoutGetLeft and related C-side getters. Reproduce the sample and verify that layout values observed inside the custom baseline function match the values exposed after layout calculation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, java
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.