Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <yoga/Yoga.h>
- YGSize measure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode) {
- return YGSize { 0, 0};
- }
- void dumpLayout(YGNodeRef node, char const * indent) {
- float L = YGNodeLayoutGetLeft(node);
- float T = YGNodeLayoutGetTop(node);
- float W = YGNodeLayoutGetWidth(node);
- float H = YGNodeLayoutGetHeight(node);
- printf("%s L=%f, T=%f, W=%f, H=%f, %p\n", indent, L, T, W, H, node);
- uint32_t k = YGNodeGetChildCount(node);
- for (uint32_t i = 0; i < k; i++) {
- YGNodeRef child = YGNodeGetChild(node, i);
- dumpLayout(child, indent - 4);
- }
- }
- void dumpLayout(YGNodeRef node) {
- static char const indentBuffer[] = " ";
- dumpLayout(node, indentBuffer + sizeof(indentBuffer) - 1);
- }
- static float const width = 50.f; // Irrelevant
- // Case1: layout directly - works fine, child gets height of 12
- void works1(YGNodeRef parent) {
- YGNodeCalculateLayout(parent, width, 30.f, YGDirectionLTR);
- dumpLayout(parent);
- }
- // Case2: measure, then layout - bug, child gets cached height of 10
- void bug(YGNodeRef parent) {
- YGNodeCalculateLayout(parent, width, NAN, YGDirectionLTR);
- dumpLayout(parent);
- YGNodeCalculateLayout(parent, width, 30.f, YGDirectionLTR);
- dumpLayout(parent);
- }
- // Case2: measure, layout, re-layout - works, because cache gets invalidated
- void works2(YGNodeRef parent) {
- YGNodeCalculateLayout(parent, width, NAN, YGDirectionLTR);
- dumpLayout(parent);
- YGNodeCalculateLayout(parent, width, 10.f, YGDirectionLTR);
- dumpLayout(parent);
- YGNodeCalculateLayout(parent, width, 30.f, YGDirectionLTR);
- dumpLayout(parent);
- }
- void test(void (*body)(YGNodeRef), char const * name) {
- printf("-- Case: %s --\n", name);
- YGConfigRef config = YGConfigNew();
- YGConfigSetExperimentalFeatureEnabled(config, YGExperimentalFeatureWebFlexBasis, true);
- YGConfigSetPointScaleFactor(config, 3.f);
- YGNodeRef parent = YGNodeNewWithConfig(config);
- YGNodeStyleSetFlexDirection(parent, YGFlexDirectionRow);
- YGNodeStyleSetAlignItems(parent, YGAlignFlexStart);
- YGNodeStyleSetAlignContent(parent, YGAlignFlexStart);
- YGNodeRef child = YGNodeNewWithConfig(config);
- YGNodeStyleSetMinHeightPercent(child, 40.f);
- YGNodeStyleSetMaxHeightPercent(child, 60.f);
- YGNodeStyleSetHeight(child, 10.f);
- YGNodeStyleSetWidth(child, width);
- YGNodeSetMeasureFunc(child, measure);
- YGNodeInsertChild(parent, child, 0);
- body(parent);
- YGNodeFree(parent);
- YGNodeFree(child);
- YGConfigFree(config);
- }
- int main() {
- test(works1, "1. layout");
- test(bug, "2. measure & layout");
- test(works2, "3. measure, layout, re-layout");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement