Skip to content

Commit 26013e6

Browse files
committed
Fix a bug that causes incorrect allocation of total width to multiple SIZING_GROW children
1 parent c3f2baf commit 26013e6

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

clay.h

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,52 +1769,55 @@ typedef enum
17691769
// will actually be distributed. So we keep looping until either all the excess width is distributed or
17701770
// we have exhausted all our containers that can change size along this axis
17711771
float Clay__DistributeSizeAmongChildren(bool xAxis, float sizeToDistribute, Clay__LayoutElementPointerArray resizableContainerBuffer, Clay__SizeDistributionType distributionType) {
1772-
Clay__LayoutElementPointerArray backBuffer = Clay__layoutElementReusableBuffer;
1773-
backBuffer.length = 0;
1772+
Clay__LayoutElementPointerArray remainingElements = Clay__layoutElementReusableBuffer;
1773+
remainingElements.length = 0;
1774+
1775+
for (int i = 0; i < resizableContainerBuffer.length; ++i) {
1776+
Clay__LayoutElementPointerArray_Add(&remainingElements, *Clay__LayoutElementPointerArray_Get(&resizableContainerBuffer, i));
1777+
}
17741778

1775-
Clay__LayoutElementPointerArray remainingElements = resizableContainerBuffer;
1776-
float totalDistributedSize;
17771779
while (sizeToDistribute != 0 && remainingElements.length > 0) {
1778-
totalDistributedSize = 0;
1780+
float dividedSize = sizeToDistribute / (float)remainingElements.length;
17791781
for (int childOffset = 0; childOffset < remainingElements.length; childOffset++) {
17801782
Clay_LayoutElement *childElement = *Clay__LayoutElementPointerArray_Get(&remainingElements, childOffset);
17811783
Clay_SizingAxis childSizing = xAxis ? childElement->layoutConfig->sizing.width : childElement->layoutConfig->sizing.height;
17821784
float *childSize = xAxis ? &childElement->dimensions.width : &childElement->dimensions.height;
17831785
float childMinSize = xAxis ? childElement->minDimensions.width : childElement->minDimensions.height;
1786+
bool canDistribute = true;
17841787

17851788
if ((sizeToDistribute < 0 && *childSize == childSizing.sizeMinMax.min) || (sizeToDistribute > 0 && *childSize == childSizing.sizeMinMax.max)) {
1786-
continue;
1789+
canDistribute = false;
17871790
}
1788-
1789-
if (!xAxis && childElement->elementType == CLAY__LAYOUT_ELEMENT_TYPE_IMAGE) {
1790-
continue; // Currently, we don't support squishing aspect ratio images on their Y axis as it would break ratio
1791+
// Currently, we don't support squishing aspect ratio images on their Y axis as it would break ratio
1792+
else if (!xAxis && childElement->elementType == CLAY__LAYOUT_ELEMENT_TYPE_IMAGE) {
1793+
canDistribute = false;
1794+
}
1795+
else {
1796+
switch (distributionType) {
1797+
case CLAY__SIZE_DISTRIBUTION_TYPE_RESIZEABLE_CONTAINER: break;
1798+
case CLAY__SIZE_DISTRIBUTION_TYPE_GROW_CONTAINER: if (childSizing.type != CLAY__SIZING_TYPE_GROW) canDistribute = false; break;
1799+
case CLAY__SIZE_DISTRIBUTION_TYPE_SCROLL_CONTAINER: if ((childElement->elementType != CLAY__LAYOUT_ELEMENT_TYPE_SCROLL_CONTAINER || (xAxis && !childElement->elementConfig.scrollElementConfig->horizontal) || (!xAxis && !childElement->elementConfig.scrollElementConfig->vertical))) canDistribute = false; break;
1800+
}
17911801
}
17921802

1793-
switch (distributionType) {
1794-
case CLAY__SIZE_DISTRIBUTION_TYPE_RESIZEABLE_CONTAINER: break;
1795-
case CLAY__SIZE_DISTRIBUTION_TYPE_GROW_CONTAINER: if (childSizing.type != CLAY__SIZING_TYPE_GROW) { continue; } break;
1796-
case CLAY__SIZE_DISTRIBUTION_TYPE_SCROLL_CONTAINER: if ((childElement->elementType != CLAY__LAYOUT_ELEMENT_TYPE_SCROLL_CONTAINER || (xAxis && !childElement->elementConfig.scrollElementConfig->horizontal) || (!xAxis && !childElement->elementConfig.scrollElementConfig->vertical))) { continue; } break;
1803+
if (!canDistribute) {
1804+
Clay__LayoutElementPointerArray_RemoveSwapback(&remainingElements, childOffset);
1805+
childOffset--;
1806+
continue;
17971807
}
17981808

1799-
float dividedSize = sizeToDistribute / (float)(remainingElements.length - childOffset);
18001809
float oldChildSize = *childSize;
18011810
*childSize = CLAY__MAX(CLAY__MAX(CLAY__MIN(childSizing.sizeMinMax.max, *childSize + dividedSize), childSizing.sizeMinMax.min), childMinSize);
18021811
float diff = *childSize - oldChildSize;
1803-
if (diff != 0) {
1804-
Clay__LayoutElementPointerArray_Add(&backBuffer, childElement);
1812+
if (diff > -0.01 && diff < 0.01) {
1813+
Clay__LayoutElementPointerArray_RemoveSwapback(&remainingElements, childOffset);
1814+
childOffset--;
1815+
continue;
18051816
}
18061817
sizeToDistribute -= diff;
1807-
totalDistributedSize += diff;
1808-
}
1809-
if (totalDistributedSize == 0) {
1810-
break;
18111818
}
1812-
// Flip the buffers
1813-
Clay__LayoutElementPointerArray temp = remainingElements;
1814-
remainingElements = backBuffer;
1815-
backBuffer = temp;
18161819
}
1817-
return sizeToDistribute;
1820+
return (sizeToDistribute > -0.01 && sizeToDistribute < 0.01) ? 0 : sizeToDistribute;
18181821
}
18191822

18201823
void Clay__SizeContainersAlongAxis(bool xAxis) {

0 commit comments

Comments
 (0)