From 62ead6b8bfd2cb8d341bfe0193659655bb7a90a6 Mon Sep 17 00:00:00 2001
From: Caeies <caeies@caeies.org>
Date: Mon, 31 Aug 2026 23:17:52 +0200
Subject: [PATCH 45/45] gtk4: Fix message area resize

* Avoid 100% CPU load due to call in loops.
* Only resize when resize events are triggered.

See RM #2222
---
 client/gui-gtk-4.0/gui_main.c | 21 +++++++++++++++++----
 client/gui-gtk-4.0/pages.c    |  4 +++-
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/client/gui-gtk-4.0/gui_main.c b/client/gui-gtk-4.0/gui_main.c
index 332dc06df1..20e9f43138 100644
--- a/client/gui-gtk-4.0/gui_main.c
+++ b/client/gui-gtk-4.0/gui_main.c
@@ -362,18 +362,21 @@ static void toplevel_focus(GtkWidget *w, GtkDirectionType arg,
 void main_message_area_resize(void *data)
 {
   if (get_current_client_page() == PAGE_GAME) {
-    static int old_width = 0, old_height = 0;
+    static int old_width = -1, old_height = -1;
     int width = gtk_widget_get_width(GTK_WIDGET(main_message_area));
     int height = gtk_widget_get_height(GTK_WIDGET(main_message_area));
 
     if (width != old_width
-        || height != old_height) {
+        || height != old_height || height == 0) {
+      /* XXX: At starts, we have a race condition with the chatline content
+       * recovery. It might consider that the bottom line is the first one,
+       * probably because gtk_widget_get_* calls returns 0. If you scroll up
+       * you will see the past content displayed.
+       */
       chatline_scroll_to_bottom(TRUE);
       old_width = width;
       old_height = height;
     }
-
-    add_idle_callback(main_message_area_resize, NULL);
   }
 }
 
@@ -1588,6 +1591,16 @@ static void setup_widgets(void)
                    G_CALLBACK(link_marks_clear_all), NULL);
   inputline_toolkit_view_append_button(view, button);
 
+  /* These two signals doesn't seems to be triggered for now */
+  g_signal_connect(main_message_area, "notify::default-width",
+                                   G_CALLBACK(main_message_area_resize), NULL);
+  g_signal_connect(main_message_area, "notify::default-height",
+                                   G_CALLBACK(main_message_area_resize), NULL);
+
+  /* Use parent resize to triggers properly the resize */
+  g_signal_connect(map_canvas, "resize",
+                                   G_CALLBACK(main_message_area_resize), NULL);
+
   /* Other things to take care of */
 
   gtk_widget_set_visible(gtk_window_get_child(GTK_WINDOW(toplevel)),
diff --git a/client/gui-gtk-4.0/pages.c b/client/gui-gtk-4.0/pages.c
index c371fc8e19..79120e9129 100644
--- a/client/gui-gtk-4.0/pages.c
+++ b/client/gui-gtk-4.0/pages.c
@@ -3604,7 +3604,9 @@ void real_set_client_page(enum client_pages new_page)
     refresh_chat_buttons();
     center_on_something();
     mapview_thaw();
-    add_idle_callback(main_message_area_resize, NULL);
+    /* Force a resize "just in case" */
+    /* this is probably a no-op most of the time */
+    main_message_area_resize(NULL);
     break;
   case PAGE_NETWORK:
     update_network_lists();
-- 
2.53.0

