Pārlūkot izejas kodu

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.
Rhin 5 gadi atpakaļ
vecāks
revīzija
d3ebd8897f
1 mainītis faili ar 13 papildinājumiem un 3 dzēšanām
  1. 13 3
      Libraries/LibGUI/GTextEditor.cpp

+ 13 - 3
Libraries/LibGUI/GTextEditor.cpp

@@ -108,12 +108,18 @@ void GTextEditor::set_text(const StringView& text)
 void GTextEditor::update_content_size()
 void GTextEditor::update_content_size()
 {
 {
     int content_width = 0;
     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;
     content_width += m_horizontal_content_padding * 2;
     if (is_right_text_alignment(m_text_alignment))
     if (is_right_text_alignment(m_text_alignment))
         content_width = max(frame_inner_rect().width(), content_width);
         content_width = max(frame_inner_rect().width(), content_width);
-    int content_height = line_count() * line_height();
+
     set_content_size({ content_width, content_height });
     set_content_size({ content_width, content_height });
     set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
     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);
         line.m_visual_rect.set_y(y_offset);
         y_offset += line.m_visual_rect.height();
         y_offset += line.m_visual_rect.height();
     }
     }
+    if (content_size().height() == y_offset)
+        return;
+
+    update_content_size();
 }
 }
 
 
 void GTextEditor::Line::recompute_visual_lines()
 void GTextEditor::Line::recompute_visual_lines()