From d3ebd8897f27c75ddf0bb75d401a6ea55761b1bd Mon Sep 17 00:00:00 2001 From: Rhin Date: Wed, 28 Aug 2019 23:26:24 -0500 Subject: [PATCH] GTextEditor: Set content size based on the visual line rects (#500) When we update our content size, the width & height is now calculated from the visual line rect size. Also now after we recompute all visual lines, if the total height is different, we re-update the content size. --- Libraries/LibGUI/GTextEditor.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Libraries/LibGUI/GTextEditor.cpp b/Libraries/LibGUI/GTextEditor.cpp index b7f8beca9da..caef69ac4d4 100644 --- a/Libraries/LibGUI/GTextEditor.cpp +++ b/Libraries/LibGUI/GTextEditor.cpp @@ -108,12 +108,18 @@ void GTextEditor::set_text(const StringView& text) void GTextEditor::update_content_size() { int content_width = 0; - for (auto& line : m_lines) - content_width = max(line.width(font()), content_width); + int content_height = 0; + for (auto& line : m_lines) { + line.for_each_visual_line([&](const Rect& rect, const StringView&, int) { + content_width = max(rect.width(), content_width); + content_height += rect.height(); + return IterationDecision::Continue; + }); + } content_width += m_horizontal_content_padding * 2; if (is_right_text_alignment(m_text_alignment)) content_width = max(frame_inner_rect().width(), content_width); - int content_height = line_count() * line_height(); + set_content_size({ content_width, content_height }); set_size_occupied_by_fixed_elements({ ruler_width(), 0 }); } @@ -1360,6 +1366,10 @@ void GTextEditor::recompute_all_visual_lines() line.m_visual_rect.set_y(y_offset); y_offset += line.m_visual_rect.height(); } + if (content_size().height() == y_offset) + return; + + update_content_size(); } void GTextEditor::Line::recompute_visual_lines()