From edf64e18364bb5430ba4dc2e6e440081d274dded Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Tue, 2 Jan 2024 03:24:50 +0200
Subject: [PATCH 31/31] gtk4/x: Fix animations

See RM #109

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-gtk-4.0/gui_main.c | 11 +++++++++++
 client/gui-gtk-4.0/gui_main.h |  1 +
 client/gui-gtk-4.0/mapview.c  |  1 -
 client/gui-gtk-4.0/pages.c    |  1 +
 client/gui-gtk-5.0/gui_main.c | 11 +++++++++++
 client/gui-gtk-5.0/gui_main.h |  1 +
 client/gui-gtk-5.0/mapview.c  |  1 -
 client/gui-gtk-5.0/pages.c    |  1 +
 8 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/client/gui-gtk-4.0/gui_main.c b/client/gui-gtk-4.0/gui_main.c
index b5c0553056..62d6ea4216 100644
--- a/client/gui-gtk-4.0/gui_main.c
+++ b/client/gui-gtk-4.0/gui_main.c
@@ -2428,6 +2428,17 @@ void add_idle_callback(void (callback)(void *), void *data)
   g_idle_add(idle_callback_wrapper, cb);
 }
 
+/**********************************************************************//**
+  Add idle callback for updating animations.
+**************************************************************************/
+void animation_idle_cb(void *data)
+{
+  if (get_current_client_page() == PAGE_GAME) {
+    update_animation();
+    add_idle_callback(animation_idle_cb, NULL);
+  }
+}
+
 /**********************************************************************//**
   Option callback for the 'allied_chat_only' gtk-gui option.
   This updates the state of the associated toggle button.
diff --git a/client/gui-gtk-4.0/gui_main.h b/client/gui-gtk-4.0/gui_main.h
index bd13c2e11b..6ad8a7f696 100644
--- a/client/gui-gtk-4.0/gui_main.h
+++ b/client/gui-gtk-4.0/gui_main.h
@@ -27,6 +27,7 @@
 #define GUI_GTK_DEFAULT_THEME_NAME FC_GTK4_DEFAULT_THEME_NAME
 
 void main_message_area_resize(void *data);
+void animation_idle_cb(void *data);
 
 /* Network string charset conversion */
 gchar *ntoh_str(const gchar *netstr);
diff --git a/client/gui-gtk-4.0/mapview.c b/client/gui-gtk-4.0/mapview.c
index 6f9b06d188..e190a89f5e 100644
--- a/client/gui-gtk-4.0/mapview.c
+++ b/client/gui-gtk-4.0/mapview.c
@@ -434,7 +434,6 @@ void map_canvas_draw(GtkDrawingArea *w, cairo_t *cr,
      * any pending updates, to make sure only the most up-to-date data
      * is written (otherwise drawing bugs happen when old data is copied
      * to screen). Then we draw all changed areas to the screen. */
-    update_animation();
     unqueue_mapview_updates(FALSE);
     cairo_scale(cr, mouse_zoom, mouse_zoom);
     cairo_set_source_surface(cr, mapview.store->surface, 0, 0);
diff --git a/client/gui-gtk-4.0/pages.c b/client/gui-gtk-4.0/pages.c
index c1b8fc0f33..aa05296b14 100644
--- a/client/gui-gtk-4.0/pages.c
+++ b/client/gui-gtk-4.0/pages.c
@@ -3588,6 +3588,7 @@ void real_set_client_page(enum client_pages new_page)
     center_on_something();
     mapview_thaw();
     add_idle_callback(main_message_area_resize, NULL);
+    add_idle_callback(animation_idle_cb, NULL);
     break;
   case PAGE_NETWORK:
     update_network_lists();
diff --git a/client/gui-gtk-5.0/gui_main.c b/client/gui-gtk-5.0/gui_main.c
index 2ae2e5f091..52771378d0 100644
--- a/client/gui-gtk-5.0/gui_main.c
+++ b/client/gui-gtk-5.0/gui_main.c
@@ -2481,6 +2481,17 @@ void add_idle_callback(void (callback)(void *), void *data)
   g_idle_add(idle_callback_wrapper, cb);
 }
 
+/**********************************************************************//**
+  Add idle callback for updating animations.
+**************************************************************************/
+void animation_idle_cb(void *data)
+{
+  if (get_current_client_page() == PAGE_GAME) {
+    update_animation();
+    add_idle_callback(animation_idle_cb, NULL);
+  }
+}
+
 /**********************************************************************//**
   Option callback for the 'allied_chat_only' gtk-gui option.
   This updates the state of the associated toggle button.
diff --git a/client/gui-gtk-5.0/gui_main.h b/client/gui-gtk-5.0/gui_main.h
index ce934dd692..9fc496bdb8 100644
--- a/client/gui-gtk-5.0/gui_main.h
+++ b/client/gui-gtk-5.0/gui_main.h
@@ -27,6 +27,7 @@
 #define GUI_GTK_DEFAULT_THEME_NAME FC_GTK5_DEFAULT_THEME_NAME
 
 void main_message_area_resize(void *data);
+void animation_idle_cb(void *data);
 
 /* Network string charset conversion */
 gchar *ntoh_str(const gchar *netstr);
diff --git a/client/gui-gtk-5.0/mapview.c b/client/gui-gtk-5.0/mapview.c
index 55b30dc586..421af01ad7 100644
--- a/client/gui-gtk-5.0/mapview.c
+++ b/client/gui-gtk-5.0/mapview.c
@@ -434,7 +434,6 @@ void map_canvas_draw(GtkDrawingArea *w, cairo_t *cr,
      * any pending updates, to make sure only the most up-to-date data
      * is written (otherwise drawing bugs happen when old data is copied
      * to screen). Then we draw all changed areas to the screen. */
-    update_animation();
     unqueue_mapview_updates(FALSE);
     cairo_scale(cr, mouse_zoom, mouse_zoom);
     cairo_set_source_surface(cr, mapview.store->surface, 0, 0);
diff --git a/client/gui-gtk-5.0/pages.c b/client/gui-gtk-5.0/pages.c
index 02976c29b4..6f556afc42 100644
--- a/client/gui-gtk-5.0/pages.c
+++ b/client/gui-gtk-5.0/pages.c
@@ -3588,6 +3588,7 @@ void real_set_client_page(enum client_pages new_page)
     center_on_something();
     mapview_thaw();
     add_idle_callback(main_message_area_resize, NULL);
+    add_idle_callback(animation_idle_cb, NULL);
     break;
   case PAGE_NETWORK:
     update_network_lists();
-- 
2.43.0

