Allow UI layout updates without a window#8672
Allow UI layout updates without a window#8672ickshonpe wants to merge 26 commits intobevyengine:mainfrom
Conversation
* 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.
…` import in `bevy_ui::node_bundles`.
…ribute-for-TextFlags-import' into ui-windows-system
| use taffy::{prelude::Size, style_helpers::TaffyMaxContent, Taffy}; | ||
|
|
||
| #[derive(Component, Debug, Copy, Clone)] | ||
| pub struct LayoutContext { |
There was a problem hiding this comment.
Basic doc strings while we're here please.
There was a problem hiding this comment.
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.
crates/bevy_ui/src/lib.rs
Outdated
| ui_focus_system.in_set(UiSystem::Focus).after(InputSystem), | ||
| ( | ||
| ui_focus_system.in_set(UiSystem::Focus).after(InputSystem), | ||
| ui_windows_system, |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
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.
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
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. |
|
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. |
…tem` requires it now again.
|
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 |
djeedai
left a comment
There was a problem hiding this comment.
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>
Co-authored-by: Jerome Humbert <djeedai@gmail.com>
|
Can you add a simple migration guide for the renames? |
done I didn't make one before because |
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. |
Objective
Allow UI layout updates without a window present.
Solution
Make
LayoutContextinto a component that is used to mark an entity as a UI root entity with an associated Taffy root node andadd a
LayoutContextcomponent to the primary window entity inUiPlugin::build.ui_layout_systemnow picks the first entity containing aLayoutContextcomponent 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:
LayoutContextderivesComponent. It has a new fieldlayout_to_logical_factor,scale_factoris renamed tocombined_scale_factor,physical_sizeis renamed toroot_node_sizeand themin_sizeandmax_sizeare removed.UiPlugin::buildadds aLayoutContextcomponent to the primary window entity.LayoutContextentity, an associated Taffy node is added to the Taffy layout tree, which is then set as the root of the entire tree.Migration Guide
LayoutContextis now a component and its fields have been changed:minandmaxhave been removedscale_factorhas been renamed tocombined_scale_factorlayout_to_logical_factoris a new field. The coordinates used in the internal UI layout tree are multiplied bylayout_to_logical_factorafter each UI update before being written to theNodeandTransformcomponents.