[gnome-games] thumbnail: Cache covers on disk



commit 30038f1fb21fe862c0d66a98b6c264dc3c9c3850
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sat Aug 4 10:27:20 2018 +0200

    thumbnail: Cache covers on disk
    
    This should save some loading time, avoiding to load huge covers but
    instead images just at the needed size.

 src/ui/game-icon-view.vala |  1 +
 src/ui/game-thumbnail.vala | 59 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 1 deletion(-)
---
diff --git a/src/ui/game-icon-view.vala b/src/ui/game-icon-view.vala
index e6fde0d9..57f0f981 100644
--- a/src/ui/game-icon-view.vala
+++ b/src/ui/game-icon-view.vala
@@ -11,6 +11,7 @@ private class Games.GameIconView : Gtk.Box {
 
                        _game = value;
 
+                       thumbnail.uid = game.get_uid ();
                        thumbnail.icon = game.get_icon ();
                        thumbnail.cover = game.get_cover ();
                        title.label = game.name;
diff --git a/src/ui/game-thumbnail.vala b/src/ui/game-thumbnail.vala
index 8e6b0c4c..bbd3ce9a 100644
--- a/src/ui/game-thumbnail.vala
+++ b/src/ui/game-thumbnail.vala
@@ -8,6 +8,19 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
        private const double ICON_SCALE = 0.75;
        private const double FRAME_RADIUS = 2; // Equal to border-radius in style css.
 
+       private Uid _uid;
+       public Uid uid {
+               get { return _uid; }
+               set {
+                       if (_uid == value)
+                               return;
+
+                       _uid = value;
+
+                       queue_draw ();
+               }
+       }
+
        private Icon _icon;
        public Icon icon {
                get { return _icon; }
@@ -182,13 +195,32 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
                if (cover_cache != null)
                        return cover_cache;
 
+               var size = int.min (context.width, context.height);
+
+               string cover_cache_path;
+               try {
+                       cover_cache_path = get_cover_cache_path (size);
+               }
+               catch (Error e) {
+                       critical (e.message);
+
+                       return null;
+               }
+
+               try {
+                       cover_cache = new Gdk.Pixbuf.from_file_at_scale (cover_cache_path, context.width, 
context.height, true);
+
+                       return cover_cache;
+               }
+               catch (Error e) {
+               }
+
                var g_icon = cover.get_cover ();
                if (g_icon == null)
                        return null;
 
                var theme = Gtk.IconTheme.get_default ();
                var lookup_flags = Gtk.IconLookupFlags.FORCE_SIZE | Gtk.IconLookupFlags.FORCE_REGULAR;
-               var size = int.min (context.width, context.height);
                var icon_info = theme.lookup_by_gicon (g_icon, (int) size, lookup_flags);
 
                try {
@@ -198,9 +230,34 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
                        warning (@"Couldn’t load the icon: $(e.message)\n");
                }
 
+               Application.try_make_dir (Application.get_covers_cache_dir (size));
+               var now = new GLib.DateTime.now_local ();
+               var creation_time = now.to_string ();
+
+               try {
+                       cover_cache.save (cover_cache_path, "png",
+                                         "tEXt::Software", "GNOME Games",
+                                         "tEXt::Creation Time", creation_time.to_string (),
+                                         null);
+               }
+               catch (Error e) {
+                       critical (e.message);
+               }
+
+
                return cover_cache;
        }
 
+       private string get_cover_cache_path (int size) throws Error {
+               var dir = Application.get_covers_cache_dir (size);
+
+               assert (uid != null);
+
+               var uid = uid.get_uid ();
+
+               return @"$dir/$uid.png";
+       }
+
        private void invalidate_cover () {
                cover_cache = null;
                queue_draw ();


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