[polari] urlPreview: Add preview title



commit 578335882874e9c748511bc4ed430ef982c17df3
Author: Florian Müllner <fmuellner gnome org>
Date:   Mon Dec 16 03:29:27 2019 +0100

    urlPreview: Add preview title
    
    For non-image URLs, the page title can provide additional information,
    so display it alongside the preview.
    
    https://gitlab.gnome.org/GNOME/polari/merge_requests/142

 data/resources/application.css |  1 +
 src/thumbnailer.js             | 11 +++++++++--
 src/urlPreview.js              | 21 ++++++++++++++++++++-
 3 files changed, 30 insertions(+), 3 deletions(-)
---
diff --git a/data/resources/application.css b/data/resources/application.css
index a8abfba..a0a57a7 100644
--- a/data/resources/application.css
+++ b/data/resources/application.css
@@ -119,6 +119,7 @@ treeview.polari-server-room-list {
 
 .url-preview { padding: 8px; }
 .url-preview image { min-width: 120px; min-height: 90px; }
+.url-preview label { font-size: small; }
 
 .emoji-picker entry { margin: 6px; }
 
diff --git a/src/thumbnailer.js b/src/thumbnailer.js
index 2733213..db98ba7 100644
--- a/src/thumbnailer.js
+++ b/src/thumbnailer.js
@@ -1,6 +1,7 @@
+imports.gi.versions.Gdk = '3.0';
 imports.gi.versions.Gtk = '3.0';
 
-const { Gio, GLib, GObject, Gtk, WebKit2 } = imports.gi;
+const { Gdk, Gio, GLib, GObject, Gtk, WebKit2 } = imports.gi;
 const Cairo = imports.cairo;
 
 Gio._promisify(WebKit2.WebView.prototype, 'get_snapshot', 'get_snapshot_finish');
@@ -32,6 +33,9 @@ let PreviewWindow = GObject.registerClass({
         });
         this.add(this._view);
 
+        this._view.bind_property('title',
+            this, 'title', GObject.BindingFlags.SYNC_CREATE);
+
         this._view.connect('authenticate', (view, request) => {
             request.cancel();
             return true;
@@ -157,6 +161,7 @@ class App {
 
     _onSnapshotReady(window) {
         let surface = window.getSnapshot();
+        let title = window.title || this._uri;
         window.destroy();
 
         if (!surface)
@@ -187,7 +192,9 @@ class App {
         cr.paint();
         cr.$dispose();
 
-        target.writeToPNG(this._filename);
+        let pixbuf = Gdk.pixbuf_get_from_surface(target,
+            0, 0, targetWidth, targetHeight);
+        pixbuf.savev(this._filename, 'png', ['tEXt::Title'], [title]);
     }
 }
 
diff --git a/src/urlPreview.js b/src/urlPreview.js
index b6e7997..f17e0be 100644
--- a/src/urlPreview.js
+++ b/src/urlPreview.js
@@ -1,5 +1,5 @@
 /* exported URLPreview */
-const { Gio, GLib, GObject, Gtk } = imports.gi;
+const { Gio, GLib, GObject, Gtk, Pango } = imports.gi;
 
 class Thumbnailer {
     static getDefault() {
@@ -77,8 +77,10 @@ var URLPreview = GObject.registerClass({
         super._init(params);
 
         this.set({
+            orientation: Gtk.Orientation.VERTICAL,
             margin: 12,
             margin_start: 0,
+            spacing: 6,
         });
 
         let styleContext = this.get_style_context();
@@ -91,8 +93,25 @@ var URLPreview = GObject.registerClass({
         });
         this.add(this._image);
 
+        this._label = new Gtk.Label({
+            halign: Gtk.Align.START,
+            ellipsize: Pango.EllipsizeMode.END,
+            visible: true,
+        });
+        this._label.get_style_context().add_class(Gtk.STYLE_CLASS_DIM_LABEL);
+        this.add(this._label);
+
         Thumbnailer.getDefault().getThumbnail(this.uri, filename => {
             this._image.set_from_file(filename);
+
+            let title = null;
+            if (this._image.pixbuf)
+                title = this._image.pixbuf.get_option('tEXt::Title');
+
+            if (title) {
+                this._label.set_label(title);
+                this.tooltip_text = title;
+            }
         });
     }
 });


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