Skip to content

Comments

Allow UI layout updates without a window#8672

Open
ickshonpe wants to merge 26 commits intobevyengine:mainfrom
ickshonpe:ui-windows-system
Open

Allow UI layout updates without a window#8672
ickshonpe wants to merge 26 commits intobevyengine:mainfrom
ickshonpe:ui-windows-system

Conversation

@ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented May 24, 2023

Objective

Allow UI layout updates without a window present.

Solution

Make LayoutContext into a component that is used to mark an entity as a UI root entity with an associated Taffy root node and
add a LayoutContext component to the primary window entity in UiPlugin::build.

ui_layout_system now picks the first entity containing a LayoutContext component and introduces a corresponding taffy node to the Taffy layout tree (given that it isn't present already). This node is then set as the root of the entire tree. Then the UI layout update proceeds as normal.


Changelog

changes:

  • LayoutContext derives Component. It has a new field layout_to_logical_factor, scale_factor is renamed to combined_scale_factor, physical_size is renamed to root_node_size and the min_size and max_size are removed.
  • UiPlugin::build adds a LayoutContext component to the primary window entity.
  • For the first LayoutContext entity, an associated Taffy node is added to the Taffy layout tree, which is then set as the root of the entire tree.
  • Added a simple test that adds and then removes a UI node.

Migration Guide

LayoutContext is now a component and its fields have been changed:

  • min and max have been removed
  • scale_factor has been renamed to combined_scale_factor
  • layout_to_logical_factor is a new field. The coordinates used in the internal UI layout tree are multiplied by layout_to_logical_factor after each UI update before being written to the Node and Transform components.

ickshonpe added 8 commits May 24, 2023 10:54
* Added `physical_to_logical_factor` field to `LayoutContext`.
* Created `ui_windows_system` to track rack's window and scale factor changes and set the `LayoutContext`.
* Removed window queries and event reader params from `ui_layout_system`, it only queries for a `LayoutContext` and the UI tree data now.
…ribute-for-TextFlags-import' into ui-windows-system
@alice-i-cecile alice-i-cecile added A-UI Graphical user interfaces, styles, layouts, and widgets C-Code-Quality A section of code that is hard to understand or change labels May 24, 2023
use taffy::{prelude::Size, style_helpers::TaffyMaxContent, Taffy};

#[derive(Component, Debug, Copy, Clone)]
pub struct LayoutContext {
Copy link
Member

Choose a reason for hiding this comment

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

Basic doc strings while we're here please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added doc comments, they might need some more work. I changed the names of the fields as well.
I'm not sure about the name LayoutContext either but I guess it's okay for now.

ui_focus_system.in_set(UiSystem::Focus).after(InputSystem),
(
ui_focus_system.in_set(UiSystem::Focus).after(InputSystem),
ui_windows_system,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ui_windows_system,
ui_windows_system.in_set(UiSystem::Window),

I'm not sure if there are other missing ordering constraints, but this is definitely implied by the doc strings above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah, I wasn't sure whether to add a label to UiSet or not. ui_window_system runs in PreUpdate so there shouldn't be any need for ordering constraints.

ickshonpe and others added 2 commits May 25, 2023 00:28
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
* Renamed the fields of `LayoutContext` to match their purpose and added doc comments.
* Added more comments to `ui_layout_system`.
* Created a new module `window` and moved `ui_windows_system` to it.
@ickshonpe ickshonpe changed the title Make ui_layout_system easier to test by moving the window logic into a separate system. Move UI window logic into a separate system May 25, 2023
@djeedai
Copy link
Contributor

djeedai commented May 27, 2023

Make ui_layout_system easier to test by moving the window logic into a separate system.

I disagree with that approach. Testing calls for a modular block of code, which means a function. Adding a separate system is a more heavyweight process that adds to the pile of things the scheduler has to do, and should only be done for runtime reasons. Can we move the logic into a function called from the same system it's currently in? That should solve the testability issue without requiring an additional system.

@ickshonpe
Copy link
Contributor Author

Make ui_layout_system easier to test by moving the window logic into a separate system.

I disagree with that approach. Testing calls for a modular block of code, which means a function. Adding a separate system is a more heavyweight process that adds to the pile of things the scheduler has to do, and should only be done for runtime reasons. Can we move the logic into a function called from the same system it's currently in? That should solve the testability issue without requiring an additional system.

Ah, that's a good point. There is no need for a new system just for this change. The situation is a bit muddled and confused because it's not clear yet how multi ui root nodes and multi-window ui support will be implemented. I'll refactor this a bit.

@djeedai
Copy link
Contributor

djeedai commented May 27, 2023

To be fair I'm on a phone so didn't look in details at the PR sorry. Just wanted to raise a general point. If that's too complicated for this case I'm fine using a separate system. It was just a suggestion in general, from experience I at least tend to put everything in a system function, and I think Bevy does that too, and that as you noted makes testing a lot harder. Separate non-system functions are nicer for testing but a pain to pass arguments to unfortunately.

ickshonpe added 2 commits May 27, 2023 15:18
* deleted the `window` module
* `ui_layout_system` only runs if an entity with a `LayoutContext` exists.
* `UiPlugin::build` adds a `LayoutContext` component to the primary window entity.
@ickshonpe ickshonpe changed the title Move UI window logic into a separate system Allow UI layout updates without a window May 27, 2023
@ickshonpe
Copy link
Contributor Author

It was really helpful, I was making things over complicated. The implementation is much better now and realised I could easily hack together multiple window UI on top of this as well:

multiple_window_ui.mp4

Copy link
Contributor

@djeedai djeedai left a comment

Choose a reason for hiding this comment

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

Couple of things I'm not sure about, but fine to get overridden by any UI expert if they feel we can merge as is.

Co-authored-by: Jerome Humbert <djeedai@gmail.com>
@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 May 30, 2023
@alice-i-cecile
Copy link
Member

Can you add a simple migration guide for the renames?

@ickshonpe
Copy link
Contributor Author

Can you add a simple migration guide for the renames?

done

I didn't make one before because LayoutContext was added after the Bevy 10 release, do we need them for changes to features new to the next version?

@alice-i-cecile
Copy link
Member

I didn't make one before because LayoutContext was added after the Bevy 10 release, do we need them for changes to features new to the next version?

Ah I'd missed that. These are nice to include in the PR description still in case folks are working off main or the PR gets delayed. Ideally we'll exclude it from the migration guide notes but a false positive like that does no harm.

@BenjaminBrienen BenjaminBrienen added C-Feature A new feature, making something new possible C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed C-Code-Quality A section of code that is hard to understand or change labels Jan 22, 2025
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-Feature A new feature, making something new possible C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants