Skip to content

Stable UiStack - #25441

Merged
cart merged 14 commits into
bevyengine:mainfrom
ickshonpe:stable-uistack
Aug 25, 2026
Merged

Stable UiStack#25441
cart merged 14 commits into
bevyengine:mainfrom
ickshonpe:stable-uistack

Conversation

@ickshonpe

@ickshonpe ickshonpe commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Objective

The order of UI nodes with the same GlobalZIndex in UiStack should be stable across frames.

Otherwise the order of UI nodes can change unpredictably after table moves. The associated issue has a simple example of this occuring.

Fixes #25410

Solution

If two Nodes have the same GlobalZIndex, ties are now decided in order by::

  1. The node with the higher ZIndex.
  2. The node that was newly added this frame. If both nodes are new then ordering falls back to archetype interation ordering as before.
  3. The node with a changed GlobalZIndex or ZIndex.

Otherwise the order is preserved from the previous frame. The previous ordering is stored in a local EntityHashMap.

Testing

Includes three new tests covering the new rules:

  • order_of_stack_roots_should_be_preserved_between_frames
  • last_updated_stack_root_should_be_on_top
  • order_of_parented_stack_roots_should_be_preserved_between_frames

…be_preserved_between_frames` inserts marker components to cause a table move, then checks that the order hasn't changed.

`last_updated_root_should_be_on_top`:
Spawns some root nodes with `GlobalZIndex(0)` and then reinserts `GlobalZIndex(0)` on the node at the bottom of the stack, and checks that it has moved to the top.
…vocabulary instead. The root of these stacks isn't necessarily a root node.
@ickshonpe ickshonpe added A-UI Graphical user interfaces, styles, layouts, and widgets S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 17, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in UI Aug 17, 2026
@ickshonpe ickshonpe added C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Aug 17, 2026
Comment thread crates/bevy_ui/src/stack.rs Outdated
@ickshonpe ickshonpe added this to the 0.19.2 milestone Aug 18, 2026

/// `GlobalZIndex` allows a [`Node`] entity anywhere in the UI hierarchy to escape the implicit draw ordering of the UI's layout tree and
/// be rendered above or below other UI nodes.
/// Root UI nodes without a `GlobalZIndex` component receive an implicit global z-index of `0`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a particularly useful subtle comment, thank you.

@ickshonpe
ickshonpe requested a review from Zeophlite August 20, 2026 09:10
@Zeophlite Zeophlite added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 20, 2026
@alice-i-cecile
alice-i-cecile added this pull request to the merge queue Aug 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 20, 2026
@cart

cart commented Aug 25, 2026

Copy link
Copy Markdown
Member

The root sort is more expensive, but ideally we don't have toooooo many roots, so this seems fine to me.

@cart
cart added this pull request to the merge queue Aug 25, 2026
Merged via the queue into bevyengine:main with commit d1b0718 Aug 25, 2026
38 checks passed
@github-project-automation github-project-automation Bot moved this from Needs SME Triage to Done in UI Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

UiStack's ordering is non-deterministic

4 participants