1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00

Correctly calculate preferred/intrinsic size in AtomLayout

This commit is contained in:
lucasmerlin
2025-04-17 12:06:03 +02:00
parent 5194c0df3e
commit 3e07862b72
2 changed files with 39 additions and 2 deletions

View File

@@ -81,9 +81,21 @@ impl<'a> AtomKind<'a> {
) -> (Vec2, SizedAtomKind<'a>) {
match self {
AtomKind::Text(text) => {
let galley = text.into_galley(ui, wrap_mode, available_size.x, TextStyle::Button);
let wrap_mode = wrap_mode.unwrap_or(ui.wrap_mode());
let desired_size = matches!(wrap_mode, TextWrapMode::Truncate).then(|| {
text.clone()
.into_galley(
ui,
Some(TextWrapMode::Extend),
available_size.x,
TextStyle::Button,
)
.desired_size()
});
let galley =
text.into_galley(ui, Some(wrap_mode), available_size.x, TextStyle::Button);
(
galley.size(), // TODO(#5762): calculate the preferred size
desired_size.unwrap_or_else(|| galley.desired_size()),
SizedAtomKind::Text(galley),
)
}

View File

@@ -795,6 +795,31 @@ impl Galley {
self.rect.size()
}
// TODO: Instead return Option<Vec2>?
pub fn desired_size(&self) -> Vec2 {
let mut current_width: f32 = 0.0;
let mut widest_width: f32 = 0.0;
let mut height = self.rows.first().map_or(0.0, |row| row.height());
for row in &self.rows {
if current_width != 0.0 {
let space = row.glyphs.last();
if let Some(space) = space {
if space.chr.is_whitespace() {
// TODO: Needed or not? Doesn't seem like it's needed
// current_width += space.advance_width;
}
}
}
current_width += row.rect().width();
widest_width = widest_width.max(current_width);
if row.ends_with_newline {
height += row.height();
current_width = 0.0;
}
}
vec2(widest_width, height)
}
pub(crate) fn round_output_to_gui(&mut self) {
for placed_row in &mut self.rows {
// Optimization: only call `make_mut` if necessary (can cause a deep clone)