[gnome-games/wip/exalm/covers: 5/5] cover: Stop using GLib.Icon



commit 60d0229b15a4df37e0c650ec11540d8785a43ab0
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sun Nov 17 16:40:08 2019 +0500

    cover: Stop using GLib.Icon
    
    Since all covers are cached as files, return File instead and load the
    cover directly from it in CoverLoader. This is more flexible and allows
    to do things like cropping covers in future.

 plugins/steam/src/steam-cover.vala | 17 ++++++++---------
 src/core/cover-loader.vala         | 30 ++++++++++++++++++++++++------
 src/core/cover.vala                |  2 +-
 src/dummy/dummy-cover.vala         |  2 +-
 src/grilo/grilo-cover.vala         | 23 ++++++++++-------------
 src/utils/composite-cover.vala     |  2 +-
 src/utils/local-cover.vala         | 11 +++++------
 7 files changed, 50 insertions(+), 37 deletions(-)
---
diff --git a/plugins/steam/src/steam-cover.vala b/plugins/steam/src/steam-cover.vala
index a40d9805..60239563 100644
--- a/plugins/steam/src/steam-cover.vala
+++ b/plugins/steam/src/steam-cover.vala
@@ -8,7 +8,7 @@ public class Games.SteamCover : Object, Cover {
        };
 
        private string game_id;
-       private GLib.Icon icon;
+       private File? file;
        private bool resolving;
 
        public SteamCover (string game_id) {
@@ -16,16 +16,16 @@ public class Games.SteamCover : Object, Cover {
                resolving = false;
        }
 
-       public GLib.Icon? get_cover () {
+       public File? get_cover () {
                if (resolving)
-                       return icon;
+                       return file;
 
-               if (icon != null)
-                       return icon;
+               if (file != null)
+                       return file;
 
                load_cover ();
-               if (icon != null)
-                       return icon;
+               if (file != null)
+                       return file;
 
                resolving = true;
 
@@ -83,8 +83,7 @@ public class Games.SteamCover : Object, Cover {
                if (!FileUtils.test (cover_path, FileTest.EXISTS))
                        return;
 
-               var file = File.new_for_path (cover_path);
-               icon = new FileIcon (file);
+               file = File.new_for_path (cover_path);
 
                changed ();
        }
diff --git a/src/core/cover-loader.vala b/src/core/cover-loader.vala
index e79f27ca..e8e0bb00 100644
--- a/src/core/cover-loader.vala
+++ b/src/core/cover-loader.vala
@@ -24,6 +24,25 @@ public class Games.CoverLoader : Object {
                });
        }
 
+       private void get_dimensions (File file, int size, out int width, out int height) {
+               int w, h;
+               Gdk.Pixbuf.get_file_info (file.get_path (), out w, out h);
+
+               double aspect_ratio = (double) w / h;
+
+               width = w;
+               height = (int) (w / aspect_ratio);
+
+               if (width > h) {
+                       width = size;
+                       height = (int) (size / aspect_ratio);
+               }
+               else {
+                       height = size;
+                       width = (int) (size * aspect_ratio);
+               }
+       }
+
        private void run_loader_thread () {
                while (true) {
                        var request = request_queue.pop ();
@@ -36,18 +55,17 @@ public class Games.CoverLoader : Object {
                                continue;
                        }
 
-                       var g_icon = game.get_cover ().get_cover ();
-                       if (g_icon == null) {
+                       var file = game.get_cover ().get_cover ();
+                       if (file == null) {
                                run_callback (request, size, null);
                                continue;
                        }
 
-                       var theme = Gtk.IconTheme.get_default ();
-                       var lookup_flags = Gtk.IconLookupFlags.FORCE_SIZE | Gtk.IconLookupFlags.FORCE_REGULAR;
-                       var icon_info = theme.lookup_by_gicon (g_icon, (int) size, lookup_flags);
+                       int width, height;
+                       get_dimensions (file, size, out width, out height);
 
                        try {
-                               pixbuf = icon_info.load_icon ();
+                               pixbuf = new Gdk.Pixbuf.from_file_at_scale (file.get_path (), width, height, 
false);
                                save_cover_cache_to_disk (game, pixbuf, size);
                        }
                        catch (Error e) {
diff --git a/src/core/cover.vala b/src/core/cover.vala
index b2434b05..824d653d 100644
--- a/src/core/cover.vala
+++ b/src/core/cover.vala
@@ -3,5 +3,5 @@
 public interface Games.Cover : Object {
        public signal void changed ();
 
-       public abstract GLib.Icon? get_cover ();
+       public abstract File? get_cover ();
 }
diff --git a/src/dummy/dummy-cover.vala b/src/dummy/dummy-cover.vala
index f8252b7f..1db9fa72 100644
--- a/src/dummy/dummy-cover.vala
+++ b/src/dummy/dummy-cover.vala
@@ -1,7 +1,7 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 public class Games.DummyCover : Object, Cover {
-       public GLib.Icon? get_cover () {
+       public File? get_cover () {
                return null;
        }
 }
diff --git a/src/grilo/grilo-cover.vala b/src/grilo/grilo-cover.vala
index 5736237e..ef3ad045 100644
--- a/src/grilo/grilo-cover.vala
+++ b/src/grilo/grilo-cover.vala
@@ -3,7 +3,7 @@
 public class Games.GriloCover : Object, Cover {
        private GriloMedia media;
        private Uid uid;
-       private GLib.Icon icon;
+       private File? file;
        private bool resolving;
        private string cover_path;
 
@@ -14,12 +14,12 @@ public class Games.GriloCover : Object, Cover {
                resolving = false;
        }
 
-       public GLib.Icon? get_cover () {
+       public GLib.File? get_cover () {
                if (resolving)
-                       return icon;
+                       return file;
 
-               if (icon != null)
-                       return icon;
+               if (file != null)
+                       return file;
 
                try {
                        load_cover ();
@@ -27,17 +27,15 @@ public class Games.GriloCover : Object, Cover {
                catch (Error e) {
                        warning (e.message);
 
-                       return icon;
+                       return file;
                }
 
-               if (icon != null)
-                       return icon;
-
-               resolving = true;
+               if (file != null)
+                       return file;
 
                media.try_resolve_media ();
 
-               return icon;
+               return file;
        }
 
        private void on_media_resolved () {
@@ -106,8 +104,7 @@ public class Games.GriloCover : Object, Cover {
                if (!FileUtils.test (cover_path, FileTest.EXISTS))
                        return;
 
-               var file = File.new_for_path (cover_path);
-               icon = new FileIcon (file);
+               file = File.new_for_path (cover_path);
 
                changed ();
        }
diff --git a/src/utils/composite-cover.vala b/src/utils/composite-cover.vala
index 3cca1c8a..d46bf816 100644
--- a/src/utils/composite-cover.vala
+++ b/src/utils/composite-cover.vala
@@ -9,7 +9,7 @@ public class Games.CompositeCover : Object, Cover {
                        cover.changed.connect (on_cover_changed);
        }
 
-       public GLib.Icon? get_cover () {
+       public GLib.File? get_cover () {
                foreach (var cover in covers) {
                        var result_cover = cover.get_cover ();
                        if (result_cover != null)
diff --git a/src/utils/local-cover.vala b/src/utils/local-cover.vala
index 1190be58..f81e4406 100644
--- a/src/utils/local-cover.vala
+++ b/src/utils/local-cover.vala
@@ -3,15 +3,15 @@
 public class Games.LocalCover : Object, Cover {
        private Uri uri;
        private bool resolved;
-       private GLib.Icon? icon;
+       private File? file;
 
        public LocalCover (Uri uri) {
                this.uri = uri;
        }
 
-       public GLib.Icon? get_cover () {
+       public File? get_cover () {
                if (resolved)
-                       return icon;
+                       return file;
 
                resolved = true;
 
@@ -28,10 +28,9 @@ public class Games.LocalCover : Object, Cover {
                if (cover_path == null)
                        return null;
 
-               var file = File.new_for_path (cover_path);
-               icon = new FileIcon (file);
+               file = File.new_for_path (cover_path);
 
-               return icon;
+               return file;
        }
 
        private string? get_cover_path () throws Error {


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