From 2a021a35fcecca31d918b7eeaf9fa9dfa885841d Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 8 Mar 2024 10:02:20 -0500 Subject: [PATCH] LibGUI: Consider carriage returns when counting lines in a label We create labels in the Browser from text on web documents, which may use carriage returns. LibGfx will split lines on CR already, but LibGUI would not consider CRs when computing the height of the containing widget. The result was text that would not fit in the label's box. --- Userland/Libraries/LibGUI/Application.cpp | 4 ++-- Userland/Libraries/LibGUI/Label.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGUI/Application.cpp b/Userland/Libraries/LibGUI/Application.cpp index d0006b8ca8..05d761bc83 100644 --- a/Userland/Libraries/LibGUI/Application.cpp +++ b/Userland/Libraries/LibGUI/Application.cpp @@ -29,9 +29,9 @@ public: { m_label->set_text(move(tooltip)); int tooltip_width = m_label->effective_min_size().width().as_int() + 10; - int line_count = m_label->text().count("\n"sv); + int line_count = m_label->text().bytes_as_string_view().count_lines(); int font_size = m_label->font().pixel_size_rounded_up(); - int tooltip_height = font_size * (1 + line_count) + ((font_size + 1) / 2) * line_count + 8; + int tooltip_height = font_size * line_count + ((font_size + 1) / 2) * (line_count - 1) + 8; Gfx::IntRect desktop_rect = Desktop::the().rect(); if (tooltip_width > desktop_rect.width()) diff --git a/Userland/Libraries/LibGUI/Label.cpp b/Userland/Libraries/LibGUI/Label.cpp index 59e8af5412..6e715bce51 100644 --- a/Userland/Libraries/LibGUI/Label.cpp +++ b/Userland/Libraries/LibGUI/Label.cpp @@ -96,7 +96,7 @@ int Label::text_calculated_preferred_width() const int Label::text_calculated_preferred_height() const { - return static_cast(ceilf(font().preferred_line_height()) * (m_text.count("\n"sv) + 1)); + return static_cast(ceilf(font().preferred_line_height()) * m_text.bytes_as_string_view().count_lines()); } Optional Label::calculated_preferred_size() const