Skip to content

Overflow clip margin#15561

Merged
alice-i-cecile merged 66 commits intobevyengine:mainfrom
ickshonpe:overflow-clip-margin
Oct 16, 2024
Merged

Overflow clip margin#15561
alice-i-cecile merged 66 commits intobevyengine:mainfrom
ickshonpe:overflow-clip-margin

Conversation

@ickshonpe
Copy link
Copy Markdown
Contributor

@ickshonpe ickshonpe commented Oct 1, 2024

Objective

Limited implementation of the CSS property overflow-clip-margin
https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-clip-margin

Allows you to control the visible area for clipped content when using overfllow-clip, -hidden, or -scroll and expand it with a margin.

Based on #15442

Fixes #15468

Solution

Adds a new field to Style: overflow_clip_margin: OverflowClipMargin.
The field is ignored unless overflow-clip, -hidden or -scroll is set on at least one axis.

OverflowClipMargin has these associated constructor functions:

pub const fn content_box() -> Self;
pub const fn padding_box() -> Self;
pub const fn border_box() -> Self;

You can also use the method with_margin to increases the size of the visible area:

commands
  .spawn(NodeBundle {
      style: Style {
          width: Val::Px(100.),
          height: Val::Px(100.),
          padding: UiRect::all(Val::Px(20.)),
          border: UiRect::all(Val::Px(5.)),
          overflow: Overflow::clip(),
          overflow_clip_margin: OverflowClipMargin::border_box().with_margin(25.),
          ..Default::default()
      },
      border_color: Color::BLACK.into(),
      background_color: GRAY.into(),
      ..Default::default()
  })

with_margin expects a length in logical pixels, negative values are clamped to zero.

Notes

  • To keep this PR as simple as possible I omitted responsive margin values support. This could be added in a follow up if we want it.
  • CSS also supports a margin-box option but we don't have access to the margin values in Node so it's probably not feasible to implement atm.

Testing

cargo run --example overflow_clip_margin

overflow-clip-margin

Migration Guide

Style has a new field OverflowClipMargin. It allows users to set the visible area for clipped content when using overflow-clip, -hidden, or -scroll and expand it with a margin.

There are three associated constructor functions content_box, padding_box and border_box:

  • content_box: elements painted outside of the content box area (the innermost part of the node excluding the padding and border) of the node are clipped. This is the new default behaviour.
  • padding_box: elements painted outside outside of the padding area of the node are clipped.
  • border_box: elements painted outside of the bounds of the node are clipped. This matches the behaviour from Bevy 0.14.

There is also a with_margin method that increases the size of the visible area by the given number in logical pixels, negative margin values are clamped to zero.

OverflowClipMargin is ignored unless overflow-clip, -hidden or -scroll is also set on at least one axis of the UI node.

ickshonpe and others added 30 commits September 10, 2024 11:08
…ourselves in each extraction function which is really complicated and fragile and has lead to a number of bugs.

Adds an `Inset` type, a `border: Inset` field to `Node`, updates the border field in `ui_layout_system`, and removes the border calculations and extra queries from the extraction functions.
Removed the `physical_rect` method from `Node`. It doesn't transform the `Node`'s bounding rect to physical coords as the name and docs imply.
Fixed `outline_radius` comment.
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed C-Examples An addition or correction to our examples labels Oct 1, 2024
@alice-i-cecile alice-i-cecile added the D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes label Oct 1, 2024
Copy link
Copy Markdown
Contributor

@nicoburns nicoburns left a comment

Choose a reason for hiding this comment

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

Looks good to me. Web only implements this for overflow:clip (not hidden or scroll). Not sure what the reasons for that is.

@ickshonpe ickshonpe mentioned this pull request Oct 12, 2024
@BenjaminBrienen
Copy link
Copy Markdown
Contributor

Really nice showcase

@alice-i-cecile alice-i-cecile 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 Oct 14, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 15, 2024
@alice-i-cecile alice-i-cecile removed this pull request from the merge queue due to a manual request Oct 15, 2024
@alice-i-cecile
Copy link
Copy Markdown
Member

This going to have merge conflicts with its sibling PR, but ping me tomorrow and I'll get this in.

github-merge-queue bot pushed a commit that referenced this pull request Oct 15, 2024
# Objective

Change UI clipping to respect borders and padding.

Fixes #15335

## Solution

Based on #15163

1. Add a `padding` field to `Node`.
2. In `ui_layout_size` copy the padding values from taffy to
`Node::padding`.
4. Determine the node's content box (The innermost part of the node
excluding the padding and border).
5. In `update_clipping` perform the clipping intersection with the
node's content box.

## Notes

* `Rect` probably needs some helper methods for working with insets but
because `Rect` and `BorderRect` are in different crates it's awkward to
add them. Left for a follow up.
* We could have another `Overflow` variant (probably called
`Overflow::Hidden`) to that clips inside of the border box instead of
the content box. Left it out here as I'm not certain about the naming or
behaviour though. If this PR is adopted, it would be trivial to add a
`Hidden` variant in a follow up.
* Depending on UI scaling there are sometimes gaps in the layout:
<img width="532" alt="rounding-bug"
src="https://github.com/user-attachments/assets/cc29aa0d-44fe-403f-8f0e-cd28a8b1d1b3">
This is caused by existing bugs in `ui_layout_system`'s coordinates
rounding and not anything to do with the changes in this PR.

## Testing

This PR also changes the `overflow` example to display borders on the
overflow nodes so you can see how this works:

#### main (The image is clipped at the edges of the node, overwriting
the border).
<img width="722" alt="main_overflow"
src="https://github.com/user-attachments/assets/eb316cd0-fff8-46ee-b481-e0cd6bab3f5c">

#### this PR  (The image is clipped at the edges of the node's border).
<img width="711" alt="content-box-clip"
src="https://github.com/user-attachments/assets/fb302e56-9302-47b9-9a29-ec3e15fe9a9f">

## Migration Guide

Migration guide is on #15561

---------

Co-authored-by: UkoeHB <[email protected]>
@ickshonpe
Copy link
Copy Markdown
Contributor Author

This going to have merge conflicts with its sibling PR, but ping me tomorrow and I'll get this in.

Weren't too bad, should be ready now.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 16, 2024
Merged via the queue into bevyengine:main with commit 6d3965f Oct 16, 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-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improved UI overflow API

5 participants