Skip to content

Merge Style properties into Node. Use ComputedNode for computed properties.#15975

Merged
cart merged 5 commits intobevyengine:mainfrom
cart:node-style-merge
Oct 18, 2024
Merged

Merge Style properties into Node. Use ComputedNode for computed properties.#15975
cart merged 5 commits intobevyengine:mainfrom
cart:node-style-merge

Conversation

@cart
Copy link
Copy Markdown
Member

@cart cart commented Oct 18, 2024

Objective

Continue improving the user experience of our UI Node API in the direction specified by Bevy's Next Generation Scene / UI System

Solution

As specified in the document above, merge Style fields into Node, and move "computed Node fields" into ComputedNode (I chose this name over something like ComputedNodeLayout because it currently contains more than just layout info. If we want to break this up / rename these concepts, lets do that in a separate PR). Style has been removed.

This accomplishes a number of goals:

Ergonomics wins

Specifying both Node and Style is now no longer required for non-default styles

Before:

commands.spawn((
    Node::default(),
    Style {
        width:  Val::Px(100.),
        ..default()
    },
));

After:

commands.spawn(Node {
    width:  Val::Px(100.),
    ..default()
});

Conceptual clarity

Style was never a comprehensive "style sheet". It only defined "core" style properties that all Nodes shared. Any "styled property" that couldn't fit that mold had to be in a separate component. A "real" style system would style properties across components (Node, Button, etc). We have plans to build a true style system (see the doc linked above).

By moving the Style fields to Node, we fully embrace Node as the driving concept and remove the "style system" confusion.

Next Steps

  • Consider identifying and splitting out "style properties that aren't core to Node". This should not happen for Bevy 0.15.

Migration Guide

Move any fields set on Style into Node and replace all Style component usage with Node.

Before:

commands.spawn((
    Node::default(),
    Style {
        width:  Val::Px(100.),
        ..default()
    },
));

After:

commands.spawn(Node {
    width:  Val::Px(100.),
    ..default()
});

For any usage of the "computed node properties" that used to live on Node, use ComputedNode instead:

Before:

fn system(nodes: Query<&Node>) {
    for node in &nodes {
        let computed_size = node.size();
    }
}

After:

fn system(computed_nodes: Query<&ComputedNode>) {
    for computed_node in &computed_nodes {
        let computed_size = computed_node.size();
    }
}

@cart cart added A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Oct 18, 2024
@cart
Copy link
Copy Markdown
Member Author

cart commented Oct 18, 2024

In #15898 I said this should wait until after Bevy 0.15. I've re-evaluated the cost:benefit of doing this now and I think we should merge this for Bevy 0.15. I was wrong. The wins here are big and the cost of porting is lower than I expected.

I had it in my head that we needed to sort out the more controversial "Style breakup" problems as part of this, but I don't really see the need. Doing this now actually makes a future breakup easier as it can be framed as "just" moving fields out of Node.

@cart cart added this to the 0.15 milestone Oct 18, 2024
@alice-i-cecile alice-i-cecile added the S-Needs-Review Needs reviewer attention (from anyone!) to move forward label Oct 18, 2024
@alice-i-cecile
Copy link
Copy Markdown
Member

I'm on board with doing this now :) I don't think this is the end game, but it's much nicer as an interim solution.

@alice-i-cecile alice-i-cecile added the M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Oct 18, 2024
Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

The semantics are much clearer, I'm extremely glad to have cleared up the Style name, and the reduced boilerplate is nice.

I don't think the migration will be hard at all: rename uses of Node to ComputedNode, then rename uses of Style to Node.

Copy link
Copy Markdown
Contributor

@ickshonpe ickshonpe left a comment

Choose a reason for hiding this comment

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

I've been unreasonably miserable all week about the previous required components changes so really pleased with this change, It fixes everything I was unhappy about before.

/// - [MDN: Basic Concepts of Grid Layout](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout)
/// - [A Complete Guide To CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different CSS Grid properties and how they work.
/// - [CSS Grid Garden](https://cssgridgarden.com/). An interactive tutorial/game that teaches the essential parts of CSS Grid in a fun engaging way.
///
Copy link
Copy Markdown
Contributor

@ickshonpe ickshonpe Oct 18, 2024

Choose a reason for hiding this comment

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

Feel like there needs to be a brief breakdown of the role of each required component in the doc comments for Node. Goes outside of the scope of this PR a bit though maybe.

Copy link
Copy Markdown
Contributor

@tim-blackbird tim-blackbird left a comment

Choose a reason for hiding this comment

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

LGTM, just some missed variable names left

You may want to adjust these docs as well

@cart cart enabled auto-merge October 18, 2024 22:11
@cart cart disabled auto-merge October 18, 2024 22:11
@cart
Copy link
Copy Markdown
Member Author

cart commented Oct 18, 2024

Ah wait missed a couple of non-code comment things.

@cart cart enabled auto-merge October 18, 2024 22:15
@cart cart added this pull request to the merge queue Oct 18, 2024
Merged via the queue into bevyengine:main with commit 015f2c6 Oct 18, 2024
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-Usability A targeted quality-of-life change that makes Bevy easier to use M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants