From bc26e012f3041529939d38ac522c18499ddff982 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Mon, 12 Apr 2021 19:46:14 +0200
Subject: Stop fucking flickering again in room list

---
 src/Body.cpp | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

(limited to 'src')

diff --git a/src/Body.cpp b/src/Body.cpp
index 4c715ed..5c40c96 100644
--- a/src/Body.cpp
+++ b/src/Body.cpp
@@ -100,6 +100,7 @@ namespace QuickMedia {
         author_color = other.author_color;
         description_color = other.description_color;
         extra = other.extra;
+        calculated_height = other.calculated_height;
         return *this;
     }
 
@@ -548,7 +549,7 @@ namespace QuickMedia {
             while(num_items_scrolled < selected_int_diff_abs && i < num_items) {
                 if(items[i]->visible) {
                     const bool merge_with_previous = body_item_merge_handler && body_item_merge_handler(prev_body_item, items[i].get());
-                    page_scroll += get_item_height(items[i].get(), size.x, selected_int_diff_abs < 50, true, merge_with_previous);
+                    page_scroll += get_item_height(items[i].get(), size.x, selected_int_diff_abs < 50, true, merge_with_previous, i);
                     if(merge_with_previous)
                         page_scroll -= spacing_y;
                     page_scroll += spacing_y;
@@ -566,7 +567,7 @@ namespace QuickMedia {
                 if(items[i]->visible) {
                     prev_body_item = get_previous_visible_item(i);
                     const bool merge_with_previous = body_item_merge_handler && body_item_merge_handler(prev_body_item, items[i].get());
-                    page_scroll -= get_item_height(items[i].get(), size.x, selected_int_diff_abs < 50, true, merge_with_previous);
+                    page_scroll -= get_item_height(items[i].get(), size.x, selected_int_diff_abs < 50, true, merge_with_previous, i);
                     if(merge_with_previous)
                         page_scroll += spacing_y;
                     page_scroll -= spacing_y;
@@ -580,7 +581,7 @@ namespace QuickMedia {
         bool merge_with_previous = false;
 
         merge_with_previous = body_item_merge_handler && body_item_merge_handler(get_previous_visible_item(selected_item), items[selected_item].get());
-        selected_item_height = get_item_height(items[selected_item].get(), size.x, true, true, merge_with_previous);
+        selected_item_height = get_item_height(items[selected_item].get(), size.x, true, true, merge_with_previous, selected_item);
         selected_item_height += spacing_y;
         bool selected_item_fits_on_screen = selected_item_height <= size.y;
         selected_line_top_visible = pos.y - start_y + page_scroll >= 0.0f;
@@ -648,7 +649,9 @@ namespace QuickMedia {
             const bool merge_with_previous = body_item_merge_handler && body_item_merge_handler(prev_body_item, item.get());
 
             item->last_drawn_time = elapsed_time_sec;
-            float item_height = get_item_height(item.get(), size.x, true, true, merge_with_previous);
+            float extra_page_scroll = page_scroll;
+            float item_height = get_item_height(item.get(), size.x, true, true, merge_with_previous, i);
+            prev_pos.y += (page_scroll - extra_page_scroll);
             float item_height_with_merge = item_height;
             item_height_with_merge += spacing_y;
             prev_pos.y -= item_height_with_merge;
@@ -694,6 +697,10 @@ namespace QuickMedia {
             if(merge_with_previous)
                 after_pos.y -= spacing_y;
 
+            float extra_page_scroll = page_scroll;
+            float item_height = get_item_height(item.get(), size.x, true, true, merge_with_previous, i);
+            after_pos.y += (page_scroll - extra_page_scroll);
+
             if(after_pos.y < start_y) {
                 items_cut_off = true;
                 first_item_fully_visible = false;
@@ -710,7 +717,6 @@ namespace QuickMedia {
             }
 
             item->last_drawn_time = elapsed_time_sec;
-            float item_height = get_item_height(item.get(), size.x, true, true, merge_with_previous);
 
             // This is needed here rather than above the loop, since update_dirty_text cant be called inside scissor because it corrupts the text for some reason
             glEnable(GL_SCISSOR_TEST);
@@ -898,7 +904,7 @@ namespace QuickMedia {
         update_dirty_state(item, size.x);
         item->last_drawn_time = draw_timer.getElapsedTime().asMilliseconds();
         sf::Vector2u window_size = window.getSize();
-        get_item_height(item, size.x, true, false);
+        get_item_height(item, size.x, true, false, false, -1);
         if(!is_embedded) {
             glEnable(GL_SCISSOR_TEST);
             glScissor(pos.x, (int)window_size.y - (int)pos.y - (int)size.y, size.x, size.y);
@@ -1143,7 +1149,7 @@ namespace QuickMedia {
         }
     }
 
-    float Body::get_item_height(BodyItem *item, float width, bool load_texture, bool include_embedded_item, bool merge_with_previous) {
+    float Body::get_item_height(BodyItem *item, float width, bool load_texture, bool include_embedded_item, bool merge_with_previous, int item_index) {
         float image_height = 0.0f;
         float text_offset_x = padding_x;
         if(draw_thumbnails && !item->thumbnail_url.empty() && !merge_with_previous) {
@@ -1236,6 +1242,11 @@ namespace QuickMedia {
 
         item_height = std::max(item_height, image_height);
         item_height += (padding_y * 2.0f);
+
+        if(attach_side == AttachSide::TOP && item_index != -1 && item_index < selected_item && item->calculated_height >= 0.0f)
+            page_scroll += (item_height - item->calculated_height);
+        item->calculated_height = item_height;
+
         return item_height;
     }
 
-- 
cgit v1.2.3-70-g09d2