Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text measurement algorithm #8425

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Measure for TextMeasure {
width: Option<f32>,
height: Option<f32>,
available_width: AvailableSpace,
available_height: AvailableSpace,
_available_height: AvailableSpace,
) -> Vec2 {
let x = width.unwrap_or_else(|| match available_width {
AvailableSpace::Definite(x) => x.clamp(
Expand All @@ -43,16 +43,10 @@ impl Measure for TextMeasure {

height
.map_or_else(
|| match available_height {
AvailableSpace::Definite(y) => {
let y = y.clamp(
self.info.max_width_content_size.y,
self.info.min_width_content_size.y,
);
self.info.compute_size(Vec2::new(x, y))
}
AvailableSpace::MinContent => Vec2::new(x, self.info.max_width_content_size.y),
AvailableSpace::MaxContent => Vec2::new(x, self.info.min_width_content_size.y),
|| match available_width {
AvailableSpace::Definite(_) => self.info.compute_size(Vec2::new(x, f32::MAX)),
AvailableSpace::MinContent => Vec2::new(x, self.info.min_width_content_size.y),
AvailableSpace::MaxContent => Vec2::new(x, self.info.max_width_content_size.y),
},
|y| Vec2::new(x, y),
)
Expand Down
20 changes: 17 additions & 3 deletions examples/ui/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
align_items: AlignItems::Start,
// Align content towards the center in the horizontal axis
justify_items: JustifyItems::Center,
// Add 20px padding to the top
padding: UiRect::top(Val::Px(20.)),
// Add 10px padding
padding: UiRect::all(Val::Px(10.)),
// Add an fr track to take up all the available space at the bottom of the column so that the text nodes
// can be top-aligned. Normally you'd use flexbox for this, but this is the CSS Grid example so we're using grid.
grid_template_rows: vec![GridTrack::auto(), GridTrack::auto(), GridTrack::fr(1.0)],
// Add a 10px gap between rows
gap: Size::height(Val::Px(10.)),
..default()
},
background_color: BackgroundColor(Color::BLACK),
Expand All @@ -135,11 +140,20 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
builder.spawn(TextBundle::from_section(
"Sidebar",
TextStyle {
font,
font: font.clone(),
font_size: 24.0,
color: Color::WHITE,
},
));
builder.spawn(TextBundle::from_section(
"A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely.",
TextStyle {
font: font.clone(),
font_size: 16.0,
color: Color::WHITE,
},
));
builder.spawn(NodeBundle::default());
});

// Footer / status bar
Expand Down