[polari] tabCompletion: Ensure row widgets are reused when rebuilding the list



commit 0bdfbedcb64b245431ea840b9d72c22e9dbb6fbd
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Sep 24 14:54:34 2014 +0200

    tabCompletion: Ensure row widgets are reused when rebuilding the list
    
    The previous approach of destroying all widgets and recreating the list
    from scratch wasn't too friendly with the GC, as quite a few widgets
    would be created and artificially kept alive longer on every change
    in the userlist of the tracked rooms.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=732290

 src/tabCompletion.js |   34 +++++++++++++++++++++++++++-------
 1 files changed, 27 insertions(+), 7 deletions(-)
---
diff --git a/src/tabCompletion.js b/src/tabCompletion.js
index 3df8bdd..62c396d 100644
--- a/src/tabCompletion.js
+++ b/src/tabCompletion.js
@@ -24,6 +24,8 @@ const TabCompletion = new Lang.Class({
         this._list.connect('row-activated', Lang.bind(this, this._stop));
         this._list.connect('keynav-failed', Lang.bind(this, this._onKeynavFailed));
         this._popup.add(this._list);
+
+        this._widgetMap = {};
     },
 
     _showPopup: function() {
@@ -59,16 +61,34 @@ const TabCompletion = new Lang.Class({
             return;
         }
 
+        let widgetMap = {};
+
+        for (let i = 0; i < completions.length; i++) {
+            let nick = completions[i];
+            let row = this._widgetMap[nick];
+
+            if (row) {
+                widgetMap[nick] = row;
+                this._list.remove(row);
+            } else {
+                row = new Gtk.ListBoxRow();
+                row._text = nick;
+                row._casefoldedText = row._text.toLowerCase();
+                row.add(new Gtk.Label({ label: row._text,
+                                        halign: Gtk.Align.START,
+                                        margin_start: 6,
+                                        margin_end: 6 }));
+                widgetMap[nick] = row;
+            }
+        }
+
+        this._widgetMap = widgetMap;
+
+        // All remaining rows are going unused
         this._list.foreach(function(r) { r.destroy(); });
 
         for (let i = 0; i < completions.length; i++) {
-            let row = new Gtk.ListBoxRow();
-            row._text = completions[i];
-            row._casefoldedText = row._text.toLowerCase();
-            row.add(new Gtk.Label({ label: row._text,
-                                    halign: Gtk.Align.START,
-                                    margin_start: 6,
-                                    margin_end: 6 }));
+            let row = this._widgetMap[completions[i]];
             this._list.add(row);
         }
         this._canComplete = completions.length > 0;


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