1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00

Add galley desired size calculation

This commit is contained in:
lucasmerlin
2025-04-17 12:06:03 +02:00
parent e80702c24c
commit f4ca54ba7d
2 changed files with 26 additions and 4 deletions

View File

@@ -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);

View File

@@ -772,6 +772,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)