Skip to content
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
22 changes: 20 additions & 2 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bevy_text::{
LineHeight, RemSize, ScaleCx, TextBounds, TextColor, TextError, TextFont, TextLayout,
TextLayoutInfo, TextMeasureInfo, TextPipeline, TextReader, TextSection, TextWriter,
};
use taffy::{style::AvailableSpace, MaybeMath};
use taffy::{style::AvailableSpace, MaybeMath, ResolveOrZero};
use tracing::error;

/// UI text system flags.
Expand Down Expand Up @@ -181,16 +181,34 @@ impl TextMeasure {

impl Measure for TextMeasure {
fn measure(&mut self, measure_args: MeasureArgs) -> Vec2 {
let width = measure_args.resolve_width();
let mut width = measure_args.resolve_width();
let height = measure_args.resolve_height();

let MeasureArgs {
available_width,
buffer,
font_system,
style,
..
} = measure_args;

// The text is wrapped inside the content box, so subtract horizontal padding and border.
if style.box_sizing == taffy::style::BoxSizing::BorderBox {
let context = taffy::Size {
width: width.effective,
height: height.effective,
};
let calc = |_, _| 0.;
let padding = style.padding.resolve_or_zero(context, calc);
let border = style.border.resolve_or_zero(context, calc);
let total_x_inset = padding.left + padding.right + border.left + border.right;
width.min = width.min.map(|min| (min - total_x_inset).max(0.));
width.max = width.max.map(|max| (max - total_x_inset).max(0.));
width.effective = width
.effective
.map(|effective| (effective - total_x_inset).max(0.));
}

let x = width
.effective
.unwrap_or_else(|| match available_width {
Expand Down
19 changes: 19 additions & 0 deletions examples/testbed/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,25 @@ mod text_wrap {
));
}
}

commands.spawn((
Node {
position_type: PositionType::Absolute,
padding: UiRect::px(10.0, 10.0, 8.0, 6.0),
top: px(300.0),
left: px(300.0),
width: vmin(30.0),
..default()
},
BackgroundColor::from(bevy::color::palettes::css::GREEN),
Text::new("initial text here"),
TextColor(Color::WHITE),
TextLayout {
justify: Justify::Left,
linebreak: LineBreak::WordBoundary,
},
DespawnOnExit(super::Scene::TextWrap),
));
}
}

Expand Down