diff --git a/crates/egui/src/widget_layout.rs b/crates/egui/src/widget_layout.rs index 62cfc13c8..92bb03cca 100644 --- a/crates/egui/src/widget_layout.rs +++ b/crates/egui/src/widget_layout.rs @@ -332,10 +332,7 @@ impl<'a> AtomicKind<'a> { match self { AtomicKind::Text(text) => { let galley = text.into_galley(ui, None, available_size.x, TextStyle::Button); - ( - galley.size(), // TODO - SizedAtomicKind::Text(galley), - ) + (galley.desired_size(), SizedAtomicKind::Text(galley)) } AtomicKind::Image(image) => { let max_size = Vec2::splat(font_size); diff --git a/crates/epaint/src/text/text_layout_types.rs b/crates/epaint/src/text/text_layout_types.rs index 4bd15d3e3..780a82479 100644 --- a/crates/epaint/src/text/text_layout_types.rs +++ b/crates/epaint/src/text/text_layout_types.rs @@ -772,6 +772,31 @@ impl Galley { self.rect.size() } + // TODO: Instead return Option? + 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)