[gnome-games/wip/exalm/hidpi: 7/7] savestate-listbox-row: Reload thumbnail on scale changes



commit 4f83bde662ec05f13c028fb97026327084d5150a
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sat Aug 17 02:08:40 2019 +0500

    savestate-listbox-row: Reload thumbnail on scale changes
    
    Now that we can draw the thumbnail properly, we need to handle scale
    changes immediately.

 data/ui/savestate-listbox-row.ui  |  1 +
 src/ui/savestate-listbox-row.vala | 86 ++++++++++++++++++++++-----------------
 2 files changed, 49 insertions(+), 38 deletions(-)
---
diff --git a/data/ui/savestate-listbox-row.ui b/data/ui/savestate-listbox-row.ui
index 77cf7d30..ddf66e45 100644
--- a/data/ui/savestate-listbox-row.ui
+++ b/data/ui/savestate-listbox-row.ui
@@ -3,6 +3,7 @@
   <requires lib="gtk+" version="3.24"/>
   <template class="GamesSavestateListBoxRow" parent="GtkListBoxRow">
     <property name="visible">true</property>
+    <signal name="notify::scale-factor" handler="on_scale_factor_changed"/>
     <style>
       <class name="savestate-row"/>
     </style>
diff --git a/src/ui/savestate-listbox-row.vala b/src/ui/savestate-listbox-row.vala
index cc606f17..8605326e 100644
--- a/src/ui/savestate-listbox-row.vala
+++ b/src/ui/savestate-listbox-row.vala
@@ -32,44 +32,7 @@ private class Games.SavestateListBoxRow : Gtk.ListBoxRow {
             /* xgettext:no-c-format */
                        date_label.label = creation_date.format (_("%-e %b %Y %X"));
 
-                       // Load the savestate thumbnail
-                       var screenshot_path = savestate.get_screenshot_path ();
-                       var screenshot_width = 0;
-                       var screenshot_height = 0;
-
-                       Gdk.Pixbuf.get_file_info (screenshot_path, out screenshot_width, out 
screenshot_height);
-
-                       var aspect_ratio = (double) savestate.get_screenshot_aspect_ratio ();
-
-                       // A fallback for migrated savestates
-                       if (aspect_ratio == 0)
-                               aspect_ratio = (double) screenshot_width / screenshot_height;
-
-                       // Calculate the thumbnail width and height
-                       var thumbnail_width = screenshot_width;
-                       var thumbnail_height = (int) (screenshot_width / aspect_ratio);
-
-                       if (thumbnail_width > thumbnail_height) {
-                               thumbnail_width = THUMBNAIL_SIZE;
-                               thumbnail_height = (int) (THUMBNAIL_SIZE / aspect_ratio);
-                       }
-                       else {
-                               thumbnail_height = THUMBNAIL_SIZE;
-                               thumbnail_width = (int) (THUMBNAIL_SIZE * aspect_ratio);
-                       }
-
-                       thumbnail_width *= scale_factor;
-                       thumbnail_height *= scale_factor;
-
-                       try {
-                               pixbuf = new Gdk.Pixbuf.from_file_at_scale (screenshot_path,
-                                                                           thumbnail_width,
-                                                                           thumbnail_height,
-                                                                           false);
-                       }
-                       catch (Error e) {
-                               warning ("Failed to load savestate thumbnail: %s", e.message);
-                       }
+                       load_thumbnail ();
                }
        }
 
@@ -79,6 +42,53 @@ private class Games.SavestateListBoxRow : Gtk.ListBoxRow {
                Object (savestate: savestate);
        }
 
+       private void load_thumbnail () {
+               if (savestate == null)
+                       return;
+
+               var screenshot_path = savestate.get_screenshot_path ();
+               var screenshot_width = 0;
+               var screenshot_height = 0;
+
+               Gdk.Pixbuf.get_file_info (screenshot_path, out screenshot_width, out screenshot_height);
+
+               var aspect_ratio = (double) savestate.get_screenshot_aspect_ratio ();
+
+               // A fallback for migrated savestates
+               if (aspect_ratio == 0)
+                       aspect_ratio = (double) screenshot_width / screenshot_height;
+
+               var thumbnail_width = screenshot_width;
+               var thumbnail_height = (int) (screenshot_width / aspect_ratio);
+
+               if (thumbnail_width > thumbnail_height) {
+                       thumbnail_width = THUMBNAIL_SIZE;
+                       thumbnail_height = (int) (THUMBNAIL_SIZE / aspect_ratio);
+               }
+               else {
+                       thumbnail_height = THUMBNAIL_SIZE;
+                       thumbnail_width = (int) (THUMBNAIL_SIZE * aspect_ratio);
+               }
+
+               thumbnail_width *= scale_factor;
+               thumbnail_height *= scale_factor;
+
+               try {
+                       pixbuf = new Gdk.Pixbuf.from_file_at_scale (screenshot_path,
+                                                                   thumbnail_width,
+                                                                   thumbnail_height,
+                                                                   false);
+               }
+               catch (Error e) {
+                       warning ("Failed to load savestate thumbnail: %s", e.message);
+               }
+       }
+
+       [GtkCallback]
+       private void on_scale_factor_changed () {
+               load_thumbnail ();
+       }
+
        public void set_name (string name) {
                name_label.label = name;
                savestate.set_name (name);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]