[Core] Fix a bug where extra child gap was added to the dimensions of clipped containers

This commit is contained in:
Nic Barker 2025-06-11 10:40:58 +10:00
parent 35b45d939b
commit 3ccfa0f8fa

28
clay.h
View file

@ -1795,13 +1795,13 @@ void Clay__CloseElement(void) {
} }
Clay_LayoutElement *openLayoutElement = Clay__GetOpenLayoutElement(); Clay_LayoutElement *openLayoutElement = Clay__GetOpenLayoutElement();
Clay_LayoutConfig *layoutConfig = openLayoutElement->layoutConfig; Clay_LayoutConfig *layoutConfig = openLayoutElement->layoutConfig;
bool elementHasScrollHorizontal = false; bool elementHasClipHorizontal = false;
bool elementHasScrollVertical = false; bool elementHasClipVertical = false;
for (int32_t i = 0; i < openLayoutElement->elementConfigs.length; i++) { for (int32_t i = 0; i < openLayoutElement->elementConfigs.length; i++) {
Clay_ElementConfig *config = Clay__ElementConfigArraySlice_Get(&openLayoutElement->elementConfigs, i); Clay_ElementConfig *config = Clay__ElementConfigArraySlice_Get(&openLayoutElement->elementConfigs, i);
if (config->type == CLAY__ELEMENT_CONFIG_TYPE_CLIP) { if (config->type == CLAY__ELEMENT_CONFIG_TYPE_CLIP) {
elementHasScrollHorizontal = config->config.clipElementConfig->horizontal; elementHasClipHorizontal = config->config.clipElementConfig->horizontal;
elementHasScrollVertical = config->config.clipElementConfig->vertical; elementHasClipVertical = config->config.clipElementConfig->vertical;
context->openClipElementStack.length--; context->openClipElementStack.length--;
break; break;
} else if (config->type == CLAY__ELEMENT_CONFIG_TYPE_FLOATING) { } else if (config->type == CLAY__ELEMENT_CONFIG_TYPE_FLOATING) {
@ -1822,19 +1822,21 @@ void Clay__CloseElement(void) {
Clay_LayoutElement *child = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex); Clay_LayoutElement *child = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex);
openLayoutElement->dimensions.width += child->dimensions.width; openLayoutElement->dimensions.width += child->dimensions.width;
openLayoutElement->dimensions.height = CLAY__MAX(openLayoutElement->dimensions.height, child->dimensions.height + topBottomPadding); openLayoutElement->dimensions.height = CLAY__MAX(openLayoutElement->dimensions.height, child->dimensions.height + topBottomPadding);
// Minimum size of child elements doesn't matter to scroll containers as they can shrink and hide their contents // Minimum size of child elements doesn't matter to clip containers as they can shrink and hide their contents
if (!elementHasScrollHorizontal) { if (!elementHasClipHorizontal) {
openLayoutElement->minDimensions.width += child->minDimensions.width; openLayoutElement->minDimensions.width += child->minDimensions.width;
} }
if (!elementHasScrollVertical) { if (!elementHasClipVertical) {
openLayoutElement->minDimensions.height = CLAY__MAX(openLayoutElement->minDimensions.height, child->minDimensions.height + topBottomPadding); openLayoutElement->minDimensions.height = CLAY__MAX(openLayoutElement->minDimensions.height, child->minDimensions.height + topBottomPadding);
} }
Clay__int32_tArray_Add(&context->layoutElementChildren, childIndex); Clay__int32_tArray_Add(&context->layoutElementChildren, childIndex);
} }
float childGap = (float)(CLAY__MAX(openLayoutElement->childrenOrTextContent.children.length - 1, 0) * layoutConfig->childGap); float childGap = (float)(CLAY__MAX(openLayoutElement->childrenOrTextContent.children.length - 1, 0) * layoutConfig->childGap);
openLayoutElement->dimensions.width += childGap; // TODO this is technically a bug with childgap and scroll containers openLayoutElement->dimensions.width += childGap;
if (!elementHasClipHorizontal) {
openLayoutElement->minDimensions.width += childGap; openLayoutElement->minDimensions.width += childGap;
} }
}
else if (layoutConfig->layoutDirection == CLAY_TOP_TO_BOTTOM) { else if (layoutConfig->layoutDirection == CLAY_TOP_TO_BOTTOM) {
openLayoutElement->dimensions.height = topBottomPadding; openLayoutElement->dimensions.height = topBottomPadding;
openLayoutElement->minDimensions.height = topBottomPadding; openLayoutElement->minDimensions.height = topBottomPadding;
@ -1843,19 +1845,21 @@ void Clay__CloseElement(void) {
Clay_LayoutElement *child = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex); Clay_LayoutElement *child = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex);
openLayoutElement->dimensions.height += child->dimensions.height; openLayoutElement->dimensions.height += child->dimensions.height;
openLayoutElement->dimensions.width = CLAY__MAX(openLayoutElement->dimensions.width, child->dimensions.width + leftRightPadding); openLayoutElement->dimensions.width = CLAY__MAX(openLayoutElement->dimensions.width, child->dimensions.width + leftRightPadding);
// Minimum size of child elements doesn't matter to scroll containers as they can shrink and hide their contents // Minimum size of child elements doesn't matter to clip containers as they can shrink and hide their contents
if (!elementHasScrollVertical) { if (!elementHasClipVertical) {
openLayoutElement->minDimensions.height += child->minDimensions.height; openLayoutElement->minDimensions.height += child->minDimensions.height;
} }
if (!elementHasScrollHorizontal) { if (!elementHasClipHorizontal) {
openLayoutElement->minDimensions.width = CLAY__MAX(openLayoutElement->minDimensions.width, child->minDimensions.width + leftRightPadding); openLayoutElement->minDimensions.width = CLAY__MAX(openLayoutElement->minDimensions.width, child->minDimensions.width + leftRightPadding);
} }
Clay__int32_tArray_Add(&context->layoutElementChildren, childIndex); Clay__int32_tArray_Add(&context->layoutElementChildren, childIndex);
} }
float childGap = (float)(CLAY__MAX(openLayoutElement->childrenOrTextContent.children.length - 1, 0) * layoutConfig->childGap); float childGap = (float)(CLAY__MAX(openLayoutElement->childrenOrTextContent.children.length - 1, 0) * layoutConfig->childGap);
openLayoutElement->dimensions.height += childGap; // TODO this is technically a bug with childgap and scroll containers openLayoutElement->dimensions.height += childGap;
if (!elementHasClipVertical) {
openLayoutElement->minDimensions.height += childGap; openLayoutElement->minDimensions.height += childGap;
} }
}
context->layoutElementChildrenBuffer.length -= openLayoutElement->childrenOrTextContent.children.length; context->layoutElementChildrenBuffer.length -= openLayoutElement->childrenOrTextContent.children.length;