[gnome-music/search] Implement basic filtering



commit 7e25e597eacb031fefd0ad03484b449df273c33b
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Sat Jun 22 20:05:18 2013 +0200

    Implement basic filtering

 src/searchbar.js |   23 ++++++++++++++++++++++-
 src/view.js      |   17 +++++++++--------
 src/window.js    |   22 +++++++++++-----------
 3 files changed, 42 insertions(+), 20 deletions(-)
---
diff --git a/src/searchbar.js b/src/searchbar.js
index fa175a0..938827e 100644
--- a/src/searchbar.js
+++ b/src/searchbar.js
@@ -28,6 +28,8 @@ const Searchbar = new Lang.Class({
     Name: "Searchbar",
 
     _init: function() {
+        this.view = null;
+
         let frame = new Gtk.Frame({ shadow_type: Gtk.ShadowType.IN,
                                     opacity: 0.9 });
         frame.get_style_context().add_class('documents-dropdown');
@@ -36,6 +38,7 @@ const Searchbar = new Lang.Class({
         frame.add(this._grid);
 
         this._searchEntry = new Gd.TaggedEntry();
+        this._searchEntry.connect("changed", Lang.bind(this, this.search_entry_changed));
         this._grid.add(this._searchEntry)
 
         this.widget = new Gtk.Revealer({ halign: Gtk.Align.CENTER,
@@ -46,6 +49,16 @@ const Searchbar = new Lang.Class({
         this.widget.show_all();
     },
 
+    setViewFilter: function(model, iter, user_data) {
+        if(this._searchEntry.visible){
+            let search_string = this._searchEntry.text.toLowerCase();
+            let name = model.get_value(iter,2);
+            if (name != null)
+                return name.toLowerCase().indexOf(search_string) > -1
+        }
+        return true;
+    },
+
     _onItemActivated: function() {
         this.emit('item-activated');
     },
@@ -56,6 +69,14 @@ const Searchbar = new Lang.Class({
 
     hide: function() {
         this.widget.reveal_child = false;
-    }
+    },
+
+    search_entry_changed: function() {
+        this.search_term = this._searchEntry.text;
+        if (this.view) {
+            this.view.filter.refilter();
+        }
+    },
+
 });
 Signals.addSignalMethods(Searchbar.prototype);
diff --git a/src/view.js b/src/view.js
index acc2fdd..5cfc607 100644
--- a/src/view.js
+++ b/src/view.js
@@ -66,7 +66,7 @@ const ViewContainer = new Lang.Class({
         this._adjustmentValueId = 0;
         this._adjustmentChangedId = 0;
         this._scrollbarVisibleId = 0;
-        this._model = Gtk.ListStore.new([
+        this._model = Gtk.TreeStore.new([
             GObject.TYPE_STRING,
             GObject.TYPE_STRING,
             GObject.TYPE_STRING,
@@ -83,7 +83,8 @@ const ViewContainer = new Lang.Class({
             shadow_type:    Gtk.ShadowType.NONE
         });
         this.view.set_view_type(Gd.MainViewType.ICON);
-        this.view.set_model(this._model);
+        this.filter = this._model.filter_new(null);
+        this.view.set_model(this.filter);
 
         let _box = new Gtk.Box({orientation: Gtk.Orientation.VERTICAL});
         _box.pack_start(this.view, true, true, 0);
@@ -109,9 +110,9 @@ const ViewContainer = new Lang.Class({
         this.header_bar = header_bar;
         this.header_bar._searchButton.connect('toggled',Lang.bind(this,function (button) {
             if (button.get_active()) {
-                this.header_bar._searchBar.show()
+                this.header_bar.get_stack()._searchBar.show()
             } else {
-                this.header_bar._searchBar.hide()
+                this.header_bar.get_stack()._searchBar.hide()
             }
         }));
         this.header_bar._selectButton.connect('toggled',Lang.bind(this,function (button) {
@@ -202,7 +203,7 @@ const ViewContainer = new Lang.Class({
     _addItem: function(source, param, item) {
         if (item != null) {
             this._offset += 1;
-            var iter = this._model.append();
+            var iter = this._model.append(null);
             var artist = "Unknown"
             if (item.get_author() != null)
                 artist = item.get_author();
@@ -358,7 +359,7 @@ const Songs = new Lang.Class({
     _addItem: function(source, param, item) {
         if (item != null) {
             this._offset += 1;
-            var iter = this._model.append();
+            var iter = this._model.append(null);
             if ((item.get_title() == null) && (item.get_url() != null)) {
                 item.set_title (extractFileName(item.get_url()));
             }
@@ -509,7 +510,7 @@ const Artists = new Lang.Class({
     _populate: function(widget, param) {
         let selection = this.view.get_generic_view().get_selection();
         if (!selection.get_selected()[0]) {
-            this._allIter = this._model.append();
+            this._allIter = this._model.append(null);
             this._artists["All Artists".toLowerCase()] = {"iter": this._allIter, "albums": []};
             this._model.set(
                 this._allIter,
@@ -568,7 +569,7 @@ const Artists = new Lang.Class({
         if (item.get_string(Grl.METADATA_KEY_ARTIST) != null)
             artist = item.get_string(Grl.METADATA_KEY_ARTIST)
         if (this._artists[artist.toLowerCase()] == undefined) {
-            var iter = this._model.append();
+            var iter = this._model.append(null);
             this._artists[artist.toLowerCase()] = {"iter": iter, "albums": []}
             this._model.set(
             iter,
diff --git a/src/window.js b/src/window.js
index b7005fb..aed4eda 100644
--- a/src/window.js
+++ b/src/window.js
@@ -68,14 +68,13 @@ const MainWindow = new Lang.Class({
             transition_duration: 100,
             visible: true
         });
+        this._stack._searchBar = new Searchbar.Searchbar();
         this.toolbar.set_stack(this._stack);
 
-        this.toolbar._searchBar =  new Searchbar.Searchbar();
-
         this._stackOverlay = new Gtk.Overlay({ visible: true });
         this._stackOverlay.get_style_context().add_class('documents-scrolledwin');
         this._stackOverlay.add(this._stack);
-        this._stackOverlay.add_overlay(this.toolbar._searchBar.widget);
+        this._stackOverlay.add_overlay(this._stack._searchBar.widget);
 
         this._box.pack_start(this.toolbar, false, false, 0);
         this._box.pack_start(this._stackOverlay, true, true, 0);
@@ -98,13 +97,16 @@ const MainWindow = new Lang.Class({
                 this.views[i].title,
                 this.views[i].title
             );
+            this.views[i].filter.set_visible_func(
+                Lang.bind(this._stack._searchBar, this._stack._searchBar.setViewFilter));
         }
 
         this._onNotifyModelId = this._stack.connect("notify::visible-child", Lang.bind(this, 
this._onNotifyMode));
         this.connect("destroy",Lang.bind(this, function(){
             this._stack.disconnect(this._onNotifyModelId);
         }));
-  
+
+        this._stack._searchBar.view = this.views[0];
         this.views[0].populate();
         }
         //To revert to the No Music View when no songs are found
@@ -121,13 +123,11 @@ const MainWindow = new Lang.Class({
 
     _onNotifyMode: function(stack, param) {
         // Slide out artist list on switching to artists view
-        if(stack.get_visible_child().title == "Artists"){
-            stack.get_visible_child().stack.set_visible_child_name("dummy")
-            stack.get_visible_child().stack.set_visible_child_name("artists")
+        let view = stack.get_visible_child();
+        if(view.title == "Artists"){
+            view.stack.set_visible_child_name("dummy")
+            view.stack.set_visible_child_name("artists")
         }
-    },
-
-    _toggleView: function(btn, i) {
-        this._stack.set_visible_child(this.views[i])
+        stack._searchBar.view = view;
     },
 });


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