Skip to content

UI node layout doesn't refresh when patching entity children #9492

@viridia

Description

@viridia

Bevy version

0.11.1

What you did

I'm writing a UI system that has a "virtual DOM" similar to what React does. This entails doing a "diff" between the old and new node hierarchy, and then incrementally patching the tree of entities based on what's changed.

What I have discovered is that Bevy UI doesn't like this very much. Specifically, when I have a node that has some number of children, and I replace some of the children but not others, the layout gets messed up. Even more significant, is that if I resize the window even a tiny bit, the layout fixes itself. So something is not getting refreshed.

Specifically, what I am doing is (pseudo-code):

  • For a given entity
    • visit each child to see if that child changed.
      • If the child changed, replace it with a new child entity and despawn the old one and its descendants.
      • If the child did not change, keep it
    • Now take the vector of all the new child ids - both the ones that were kept from before, and the ones that are new - and call parent.replace_children().
    • Finally, recurse downward and do the same for each child element.

What went wrong

What I'm seeing is that the node layout is random, nodes appearing displaced from where they should be, nodes off the edge of the screen and so on.

Additional information

The problem only seems to manifest when I enable mixing of new and old children. I can disable the bug in one of two ways: either by re-creating the node tree every time (by disabling the diff algorithm), or by not mutating the tree at all.

I realize that you'll probably want a reproducible case. I can provide this, but it's a considerable amount of work and will take some time. That is, I can easily demonstrate the bug in my current github repo, but if you want a "minimal" example I'll need to spend time cutting out all of the parts of the game that aren't relevant to the bug, which can go on a branch or something.

Let me know what you need.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-TransformTranslations, rotations and scalesA-UIGraphical user interfaces, styles, layouts, and widgetsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions