[gnome-maps] Add icons to search popup



commit 84e5881f510de786c3826d35f3fdbd23303f8d72
Author: Jonas Danielsson <jonas threetimestwo org>
Date:   Sat Aug 24 15:39:18 2013 +0200

    Add icons to search popup
    
    https://bugzilla.gnome.org/show_bug.cgi?id=706715

 src/mainWindow.js  |   18 +++++++++++++++---
 src/searchPopup.js |   13 ++++++++++---
 src/utils.js       |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 6 deletions(-)
---
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 636f686..4de10b9 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -22,6 +22,7 @@
  */
 
 const Gdk = imports.gi.Gdk;
+const GdkPixbuf = imports.gi.GdkPixbuf;
 const GLib = imports.gi.GLib;
 const Gtk = imports.gi.Gtk;
 const GObject = imports.gi.GObject;
@@ -42,9 +43,12 @@ const _CONFIGURE_ID_TIMEOUT = 100; // msecs
 const _WINDOW_MIN_WIDTH = 600;
 const _WINDOW_MIN_HEIGHT = 500;
 
+const _PLACE_ICON_SIZE = 20;
+
 const SearchResults = {
-    COL_DESCRIPTION:  0,
-    COL_LOCATION:     1
+    COL_ICON:         0,
+    COL_DESCRIPTION:  1,
+    COL_LOCATION:     2
 };
 
 const MainWindow = new Lang.Class({
@@ -84,7 +88,8 @@ const MainWindow = new Lang.Class({
         this._searchPopup = new SearchPopup.SearchPopup(10);
 
         let model = new Gtk.ListStore();
-        model.set_column_types([GObject.TYPE_STRING,
+        model.set_column_types([GdkPixbuf.Pixbuf,
+                                GObject.TYPE_STRING,
                                 GObject.TYPE_OBJECT]);
         this._searchPopup.setModel(model);
         this._searchPopup.connect('selected',
@@ -240,6 +245,7 @@ const MainWindow = new Lang.Class({
         places.forEach(function(place) {
             let iter = model.append();
             let location = place.get_location();
+            let icon = place.icon;
 
             if (location == null)
                 return;
@@ -250,6 +256,12 @@ const MainWindow = new Lang.Class({
                        SearchResults.COL_LOCATION],
                       [description_markup,
                        location]);
+
+            if (icon !== null) {
+                Utils.load_icon(icon, _PLACE_ICON_SIZE, function(pixbuf) {
+                    model.set(iter, [SearchResults.COL_ICON], [pixbuf]);
+                });
+            }
         });
         this._searchPopup.show();
     },
diff --git a/src/searchPopup.js b/src/searchPopup.js
index 2d98da7..618a7c9 100644
--- a/src/searchPopup.js
+++ b/src/searchPopup.js
@@ -24,7 +24,8 @@ const Lang = imports.lang;
 const Utils = imports.utils;
 
 const Columns = {
-    TEXT: 0
+    ICON: 0,
+    TEXT: 1
 };
 
 const SearchPopup = new Lang.Class({
@@ -58,15 +59,21 @@ const SearchPopup = new Lang.Class({
     },
 
     _initList: function() {
-        let cell = new Gtk.CellRendererText({ xpad: 16,
-                                              ypad: 8 });
         let column = new Gtk.TreeViewColumn();
 
         this._treeView.append_column(column);
+
+        let cell = new Gtk.CellRendererPixbuf({ xpad: 2 });
+        column.pack_start(cell, false);
+        column.add_attribute(cell, 'pixbuf', Columns.ICON);
+
+        cell = new Gtk.CellRendererText({ xpad: 8,
+                                          ypad: 8 });
         column.pack_start(cell, true);
         column.add_attribute(cell, 'markup', Columns.TEXT);
 
         this._cellHeight = column.cell_get_size(null)[3];
+        this._cellHeight += cell.get_preferred_height(this._treeView)[0];
     },
 
     _onListButtonPress: function(widget, event) {
diff --git a/src/utils.js b/src/utils.js
index 621d9ef..6a8082c 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -37,6 +37,8 @@ const _ = imports.gettext.gettext;
 let debugInit = false;
 let debugEnabled = false;
 
+let _iconStore = {};
+
 function debug(str) {
     if (!debugInit) {
         let env = GLib.getenv('MAPS_DEBUG');
@@ -130,3 +132,47 @@ function getUIObject(res, ids) {
     });
     return ret;
 }
+
+function load_icon(icon, size, loadCompleteCallback) {
+    if (icon instanceof Gio.FileIcon) {
+        _load_file_icon(icon, loadCompleteCallback);
+    } else if (icon instanceof Gio.ThemedIcon) {
+        _load_themed_icon(icon, size, loadCompleteCallback);
+    }
+}
+
+function _load_file_icon(icon, loadCompleteCallback) {
+    let pixbuf = _iconStore[icon.file.get_uri()];
+
+    if (pixbuf) { // check if the icon is cached
+        loadCompleteCallback(pixbuf);
+        return;
+    }
+
+    icon.load_async(-1, null, function(icon, res) {
+        try {
+            let stream = icon.load_finish(res, null)[0];
+
+            pixbuf =
+                GdkPixbuf.Pixbuf.new_from_stream(stream, null);
+
+            _iconStore[icon.file.get_uri()] = pixbuf;
+            loadCompleteCallback(pixbuf);
+        } catch(e) {
+            log("Failed to load pixbuf: " + e);
+        }
+    });
+}
+
+function _load_themed_icon(icon, size, loadCompleteCallback) {
+    let theme = Gtk.IconTheme.get_default();
+    let flags = Gtk.IconLookupFlags.GENERIC_FALLBACK;
+    let info = theme.lookup_by_gicon(icon, size, flags);
+
+    try {
+        let pixbuf = info.load_icon();
+        loadCompleteCallback(pixbuf);
+    } catch(e) {
+        log("Failed to load pixbuf: " + e);
+    }
+}


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