[Core] Fix floating element exit transitions (#608)

This commit is contained in:
Bach Le 2026-04-26 14:13:00 +08:00 committed by GitHub
parent 08963800a4
commit a7b86ab7fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

14
clay.h
View file

@ -4462,10 +4462,12 @@ Clay_RenderCommandArray Clay_EndLayout(float deltaTime) {
Clay_LayoutElementHashMapItem *parentHashMapItem = Clay__GetHashMapItem(data->parentId);
// Don't exit transition if the parent has also exited and SKIP_WHEN_PARENT_EXITS is used
if (config->exit.trigger == CLAY_TRANSITION_EXIT_TRIGGER_WHEN_PARENT_EXITS || !parentHashMapItem || parentHashMapItem->generation > context->generation) {
// This if only runs one single time when the element first starts exiting
if (data->state != CLAY_TRANSITION_STATE_EXITING) {
if (parentHashMapItem->generation <= context->generation) {
data->elementThisFrame->config.floating.attachTo = CLAY_ATTACH_TO_ROOT;
data->elementThisFrame->config.floating.offset = CLAY__INIT(Clay_Vector2) { hashMapItem->boundingBox.x, hashMapItem->boundingBox.y };
data->elementThisFrame->config.floating.parentId = Clay__HashString(CLAY_STRING("Clay__RootContainer"), 0).id;
}
data->elementThisFrame->exiting = true;
data->elementThisFrame->config.layout.sizing.width = CLAY_SIZING_FIXED(data->elementThisFrame->dimensions.width);
@ -4476,6 +4478,8 @@ Clay_RenderCommandArray Clay_EndLayout(float deltaTime) {
data->targetState = config->exit.setFinalState(data->targetState, config->properties);
}
// Below this line runs every frame while element is exiting -----------
// Clone the entire subtree back into the main UI layout tree
Clay__int32_tArray bfsBuffer = context->openLayoutElementStack;
bfsBuffer.length = 0;
@ -4507,7 +4511,9 @@ Clay_RenderCommandArray Clay_EndLayout(float deltaTime) {
}
// Reattach the inserted subtree to its previous parent if it still exists
if (parentHashMapItem->generation > context->generation) {
// and the exiting element is not floating
Clay_FloatingElementConfig* floatingConfig = &hashMapItem->layoutElement->config.floating;
if (parentHashMapItem->generation > context->generation && floatingConfig->attachTo == CLAY_ATTACH_TO_NONE) {
Clay_LayoutElement *parentElement = parentHashMapItem->layoutElement;
int32_t newChildrenStartIndex = context->layoutElementChildren.length;
bool found = false;
@ -4527,12 +4533,12 @@ Clay_RenderCommandArray Clay_EndLayout(float deltaTime) {
}
parentElement->children.length++;
parentElement->children.elements = &context->layoutElementChildren.internalArray[newChildrenStartIndex];
// Otherwise, just attach to the root as a floating element
// Otherwise, create the tree root for the floating element (needs to be created every frame)
} else {
Clay__LayoutElementTreeRootArray_Add(&context->layoutElementTreeRoots, CLAY__INIT(Clay__LayoutElementTreeRoot) {
.layoutElementIndex = (int32_t)(data->elementThisFrame - context->layoutElements.internalArray),
.parentId = Clay__HashString(CLAY_STRING("Clay__RootContainer"), 0).id,
.zIndex = 1,
.parentId = floatingConfig->parentId,
.zIndex = floatingConfig->zIndex,
});
}
// Parent exited, just delete child without exit transition