[sushi/wip/cosimoc/no-clutter: 45/66] fallbackRenderer: rework how the file icon is set



commit d2074d5543792562f88cb846b00b55fb802c79d5
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun Apr 29 16:46:13 2018 -0700

    fallbackRenderer: rework how the file icon is set
    
    Instead of loading a pixbuf through the C library, return directly the
    GIcon from there. That simplifies things, and allows GtkImage to
    automatically do HiDpi handling for us.

 src/js/ui/fallbackRenderer.js    | 21 ++++++++++++++++++---
 src/libsushi/sushi-file-loader.c | 36 +++++-------------------------------
 src/libsushi/sushi-file-loader.h |  3 +--
 3 files changed, 24 insertions(+), 36 deletions(-)
---
diff --git a/src/js/ui/fallbackRenderer.js b/src/js/ui/fallbackRenderer.js
index 96dfe34..d4d577d 100644
--- a/src/js/ui/fallbackRenderer.js
+++ b/src/js/ui/fallbackRenderer.js
@@ -56,9 +56,9 @@ var FallbackRenderer = new Lang.Class({
 
         this._box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
                                   spacing: 6 });
-        this._image = new Gtk.Image({ icon_name: 'document',
-                                      pixel_size: 256 });
+        this._image = new Gtk.Image();
         this._box.pack_start(this._image, false, false, 0);
+        this._updateIcon(new Gio.ThemedIcon({ name: 'text-x-generic' }));
 
         let vbox = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL,
                                  spacing: 1,
@@ -133,6 +133,21 @@ var FallbackRenderer = new Lang.Class({
         this._dateLabel.set_markup(dateStr);
     },
 
+    _updateIcon: function(icon) {
+        let iconTheme = Gtk.IconTheme.get_default();
+        let iconInfo = iconTheme.lookup_by_gicon_for_scale(icon, 256,
+            this._image.scale_factor, 0);
+        if (!iconInfo)
+            return;
+
+        try {
+            let surface = iconInfo.load_surface(this._image.get_window());
+            this._image.surface = surface;
+        } catch (e) {
+            logError(e, `Error loading surface for icon ${icon.to_string()}`);
+        }
+    },
+
     _onFileInfoChanged : function() {
         if (!this._fileLoader.get_loading()) {
             this._spinner.stop();
@@ -140,7 +155,7 @@ var FallbackRenderer = new Lang.Class({
         }
 
         if (this._fileLoader.icon)
-            this._image.set_from_pixbuf(this._fileLoader.icon);
+            this._updateIcon(this._fileLoader.icon);
 
         this._applyLabels();
         this._mainWindow.resizeWindow();
diff --git a/src/libsushi/sushi-file-loader.c b/src/libsushi/sushi-file-loader.c
index a88d9c3..5602384 100644
--- a/src/libsushi/sushi-file-loader.c
+++ b/src/libsushi/sushi-file-loader.c
@@ -435,7 +435,7 @@ sushi_file_loader_get_property (GObject *object,
     g_value_take_string (value, sushi_file_loader_get_date_string (self));
     break;
   case PROP_ICON:
-    g_value_take_object (value, sushi_file_loader_get_icon (self));
+    g_value_set_object (value, sushi_file_loader_get_icon (self));
     break;
   case PROP_FILE:
     g_value_set_object (value, self->priv->file);
@@ -521,7 +521,7 @@ sushi_file_loader_class_init (SushiFileLoaderClass *klass)
     g_param_spec_object ("icon",
                          "Icon",
                          "The icon of the file",
-                         GDK_TYPE_PIXBUF,
+                         G_TYPE_ICON,
                          G_PARAM_READABLE);
 
   g_type_class_add_private (klass, sizeof (SushiFileLoaderPrivate));
@@ -567,41 +567,15 @@ sushi_file_loader_get_display_name (SushiFileLoader *self)
  * sushi_file_loader_get_icon:
  * @self:
  *
- * Returns: (transfer full):
+ * Returns: (transfer none):
  */
-GdkPixbuf *
+GIcon *
 sushi_file_loader_get_icon (SushiFileLoader *self)
 {
-  GdkPixbuf *retval;
-  GtkIconInfo *info;
-  GError *error = NULL;
-
   if (self->priv->info == NULL)
     return NULL;
 
-  info = gtk_icon_theme_lookup_by_gicon (gtk_icon_theme_get_default (),
-                                         g_file_info_get_icon (self->priv->info),
-                                         256, 0);
-
-  if (info == NULL)
-    return NULL;
-
-  retval = gtk_icon_info_load_icon (info, &error);
-  g_object_unref (info);
-
-  if (error != NULL) {
-    gchar *uri;
-
-    uri = g_file_get_uri (self->priv->file);
-    g_warning ("Unable to load icon for %s: %s", uri, error->message);
-
-    g_free (uri);
-    g_error_free (error);
-
-    return NULL;
-  }
-
-  return retval;
+  return g_file_info_get_icon (self->priv->info);
 }
 
 /**
diff --git a/src/libsushi/sushi-file-loader.h b/src/libsushi/sushi-file-loader.h
index 7a724f9..b883a5c 100644
--- a/src/libsushi/sushi-file-loader.h
+++ b/src/libsushi/sushi-file-loader.h
@@ -28,7 +28,6 @@
 
 #include <glib-object.h>
 #include <gio/gio.h>
-#include <gdk-pixbuf/gdk-pixbuf.h>
 
 G_BEGIN_DECLS
 
@@ -63,7 +62,7 @@ gchar *sushi_file_loader_get_display_name (SushiFileLoader *self);
 gchar *sushi_file_loader_get_size_string  (SushiFileLoader *self);
 gchar *sushi_file_loader_get_date_string  (SushiFileLoader *self);
 gchar *sushi_file_loader_get_content_type_string (SushiFileLoader *self);
-GdkPixbuf *sushi_file_loader_get_icon     (SushiFileLoader *self);
+GIcon *sushi_file_loader_get_icon     (SushiFileLoader *self);
 GFileType sushi_file_loader_get_file_type (SushiFileLoader *self);
 
 gboolean sushi_file_loader_get_loading (SushiFileLoader *self);


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