[gnome-games/wip/theawless/simplify-thumbnail: 4/5] game-thumbnail: Allow caching of icons and emblem



commit af0686e1cf97094bf95cf2a1720a8300e7c28f31
Author: theawless <theawless gmail com>
Date:   Tue Oct 2 20:23:50 2018 +0530

    game-thumbnail: Allow caching of icons and emblem

 src/ui/game-thumbnail.vala | 104 ++++++++++++++++++++-------------------------
 1 file changed, 47 insertions(+), 57 deletions(-)
---
diff --git a/src/ui/game-thumbnail.vala b/src/ui/game-thumbnail.vala
index e9307425..2e8ea454 100644
--- a/src/ui/game-thumbnail.vala
+++ b/src/ui/game-thumbnail.vala
@@ -13,6 +13,8 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
 
                        _uid = value;
 
+                       icon_cache = null;
+                       cover_cache = null;
                        queue_draw ();
                }
        }
@@ -26,6 +28,7 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
 
                        _icon = value;
 
+                       icon_cache = null;
                        queue_draw ();
                }
        }
@@ -51,9 +54,11 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
        }
 
        private bool tried_loading_cover;
+       private Gdk.Pixbuf? emblem_cache;
+       private Gdk.Pixbuf? icon_cache;
        private Gdk.Pixbuf? cover_cache;
-       private int previous_cover_width;
-       private int previous_cover_height;
+       private int cache_width;
+       private int cache_height;
 
        public struct DrawingContext {
                Cairo.Context cr;
@@ -87,11 +92,13 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
                        cr, window, style, state, width, height
                };
 
-               if (icon == null)
-                       return false;
-
-               if (cover == null)
-                       return false;
+               if (cache_height != context.height || cache_width != context.width) {
+                       cache_height = context.height;
+                       cache_width = context.width;
+                       emblem_cache = null;
+                       icon_cache = null;
+                       cover_cache = null;
+               }
 
                var drawn = false;
 
@@ -108,24 +115,22 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
        }
 
        public bool draw_icon (DrawingContext context) {
-               var g_icon = icon.get_icon ();
-               if (g_icon == null)
-                       return false;
-
-               var pixbuf = get_scaled_icon (context, g_icon, ICON_SCALE);
-               if (pixbuf == null)
+               if (icon_cache == null)
+                       load_icon_cache (context);
+               if (icon_cache == null)
                        return false;
 
                draw_background (context);
-               draw_pixbuf (context, pixbuf);
+               draw_pixbuf (context, icon_cache);
                draw_border (context);
 
                return true;
        }
 
        public bool draw_cover (DrawingContext context) {
-               var pixbuf = get_scaled_cover (context);
-               if (pixbuf == null)
+               if (cover_cache == null)
+                       load_cover_cache (context);
+               if (cover_cache == null)
                        return false;
 
                var border_radius = (int) context.style.get_property (Gtk.STYLE_PROPERTY_BORDER_RADIUS, 
context.state);
@@ -133,90 +138,77 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
                context.cr.set_source_rgb (0, 0, 0);
                rounded_rectangle (context.cr, 0.5, 0.5, context.width - 1, context.height - 1, 
border_radius);
                context.cr.fill ();
-               draw_pixbuf (context, pixbuf);
+               draw_pixbuf (context, cover_cache);
                draw_border (context);
 
                return true;
        }
 
        public void draw_emblem (DrawingContext context) {
-               var pixbuf = get_scaled_emblem (context, "applications-games-symbolic", EMBLEM_SCALE);
-               if (pixbuf == null)
+               if (emblem_cache == null)
+                       load_emblem_cache (context);
+               if (emblem_cache == null)
                        return;
 
                draw_background (context);
-               draw_pixbuf (context, pixbuf);
+               draw_pixbuf (context, emblem_cache);
                draw_border (context);
        }
 
-       private Gdk.Pixbuf? get_scaled_emblem (DrawingContext context, string icon_name, double scale) {
-               Gdk.Pixbuf? emblem = null;
-
+       private void load_emblem_cache (DrawingContext context) {
                var color = context.style.get_color (context.state);
-
                var theme = Gtk.IconTheme.get_default ();
-               var size = int.min (context.width, context.height) * scale;
-               var icon_info = theme.lookup_icon (icon_name, (int) size, Gtk.IconLookupFlags.FORCE_SIZE);
+               var size = int.min (context.width, context.height) * EMBLEM_SCALE;
+               var icon_info = theme.lookup_icon ("applications-games-symbolic", (int) size,
+                                                  Gtk.IconLookupFlags.FORCE_SIZE);
 
                if (icon_info == null) {
                        warning ("Couldn't find the emblem");
-                       return null;
+                       return;
                }
                try {
-                       return icon_info.load_symbolic (color);
+                       emblem_cache = icon_info.load_symbolic (color);
                } catch (GLib.Error e) {
                        warning (@"Couldn’t load the emblem: $(e.message)");
-                       return null;
                }
        }
 
-       private Gdk.Pixbuf? get_scaled_icon (DrawingContext context, GLib.Icon? icon, double scale) {
+       private void load_icon_cache (DrawingContext context) {
                if (icon == null)
-                       return null;
+                       return;
+               var g_icon = icon.get_icon ();
+               if (g_icon == null)
+                       return;
 
                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) * scale;
-               var icon_info = theme.lookup_by_gicon (icon, (int) size, lookup_flags);
+               var size = int.min (context.width, context.height) * ICON_SCALE;
+               var icon_info = theme.lookup_by_gicon (g_icon, (int) size, lookup_flags);
 
                if (icon_info == null) {
                        warning ("Couldn't find the icon");
-                       return null;
+                       return;
                }
                try {
-                       return icon_info.load_icon ();
+                       icon_cache = icon_info.load_icon ();
                }
                catch (Error e) {
                        warning (@"Couldn’t load the icon: $(e.message)");
-                       return null;
                }
        }
 
-       private Gdk.Pixbuf? get_scaled_cover (DrawingContext context) {
-               if (previous_cover_width != context.width) {
-                       previous_cover_width = context.width;
-                       cover_cache = null;
-                       tried_loading_cover = false;
-               }
-
-               if (previous_cover_height != context.height) {
-                       previous_cover_height = context.height;
-                       cover_cache = null;
-                       tried_loading_cover = false;
-               }
-
-               if (cover_cache != null)
-                       return cover_cache;
-
+       private void load_cover_cache (DrawingContext context) {
                var size = int.min (context.width, context.height);
 
                load_cover_cache_from_disk (context, size);
                if (cover_cache != null)
-                       return cover_cache;
+                       return;
 
+               if (cover == null)
+                       return;
                var g_icon = cover.get_cover ();
                if (g_icon == null)
-                       return null;
+                       return;
 
                var theme = Gtk.IconTheme.get_default ();
                var lookup_flags = Gtk.IconLookupFlags.FORCE_SIZE | Gtk.IconLookupFlags.FORCE_REGULAR;
@@ -224,7 +216,7 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
 
                if (icon_info == null) {
                        warning ("Couldn't find the cover");
-                       return null;
+                       return;
                }
                try {
                        cover_cache = icon_info.load_icon ();
@@ -233,8 +225,6 @@ private class Games.GameThumbnail: Gtk.DrawingArea {
                catch (Error e) {
                        warning (@"Couldn’t load the cover: $(e.message)");
                }
-
-               return cover_cache;
        }
 
        private void load_cover_cache_from_disk (DrawingContext context, int size) {


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