[polari] thumbnailer: Generate fallback icons on failure



commit f78f37bed5816fde26a5d20473f3a4e396941e67
Author: Florian Müllner <fmuellner gnome org>
Date:   Wed Jul 29 17:10:15 2020 +0200

    thumbnailer: Generate fallback icons on failure
    
    We currently rely on GtkImage to fall back to `image-missing` when the
    filename points to a non-existent file. That's not only not the best
    fallback, but also means that we try to generate the thumbnail over
    and over again.
    
    Avoid this by generating an appropriate fallback icon based on the
    guessed content type.
    
    https://gitlab.gnome.org/GNOME/polari/-/issues/149

 src/thumbnailer.js | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
---
diff --git a/src/thumbnailer.js b/src/thumbnailer.js
index e2ad0a54..5de6349f 100644
--- a/src/thumbnailer.js
+++ b/src/thumbnailer.js
@@ -9,6 +9,7 @@ Gio._promisify(WebKit2.WebView.prototype, 'run_javascript', 'run_javascript_fini
 
 const PREVIEW_WIDTH = 120;
 const PREVIEW_HEIGHT = 90;
+const FALLBACK_ICON_SIZE = 64;
 
 let PreviewWindow = GObject.registerClass({
     Properties: {
@@ -158,7 +159,7 @@ class App {
 
         window.realize();
         window.connect('snapshot-ready', this._onSnapshotReady.bind(this));
-        window.connect('snapshot-failed', () => window.destroy());
+        window.connect('snapshot-failed', this._onSnapshotFailed.bind(this));
         window.connect('destroy', () => Gtk.main_quit());
 
         Gtk.main();
@@ -201,6 +202,20 @@ class App {
             0, 0, targetWidth, targetHeight);
         pixbuf.savev(this._filename, 'png', ['tEXt::Title'], [title]);
     }
+
+    _onSnapshotFailed(window) {
+        const context = window.get_style_context();
+        context.set_state(Gtk.StateFlags.BACKDROP);
+        const color = context.get_color(context.get_state());
+        window.destroy();
+
+        const [type] = Gio.content_type_guess(this._uri, null);
+        const icon = Gio.content_type_get_symbolic_icon(type);
+        const theme = Gtk.IconTheme.get_default();
+        const info = theme.lookup_by_gicon(icon, FALLBACK_ICON_SIZE, 0);
+        const [pixbuf] = info.load_symbolic(color, null, null, null);
+        pixbuf.savev(this._filename, 'png', [], []);
+    }
 }
 
 let [url, filename] = ARGV;


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