LibGUI: Simplify GUI::Label preferred height calculation

No need to use a TextLayout here, we can just count the number of lines
and multiply that by the font's preferred line height.

In addition to being much simpler, it also fixes a bug where labels were
got too tall if we calculated their preferred height before assigning
a final width to them.
This commit is contained in:
Andreas Kling 2023-01-06 13:41:51 +01:00
parent ef7eb60f22
commit 3332ce01ce

View file

@ -10,7 +10,6 @@
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Palette.h>
#include <LibGfx/TextLayout.h>
#include <LibGfx/TextWrapping.h>
REGISTER_WIDGET(GUI, Label)
@ -117,7 +116,7 @@ void Label::size_to_fit()
int Label::text_calculated_preferred_height() const
{
return static_cast<int>(ceilf(Gfx::TextLayout(font(), Utf8View { m_text }, text_rect().to_type<float>()).bounding_rect(Gfx::TextWrapping::Wrap).height()));
return static_cast<int>(ceilf(font().preferred_line_height()) * (m_text.count("\n"sv) + 1));
}
Optional<UISize> Label::calculated_preferred_size() const