[gnome-games] sidebar-view: Hide empty developers and platforms



commit 63006052310451f3c3cefc6f903b3118b2d84c17
Author: Yetizone <andreii lisita gmail com>
Date:   Tue Jan 8 11:31:33 2019 +0200

    sidebar-view: Hide empty developers and platforms

 src/ui/collection-box.vala | 14 +++++++------
 src/ui/sidebar-view.vala   | 51 ++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 53 insertions(+), 12 deletions(-)
---
diff --git a/src/ui/collection-box.vala b/src/ui/collection-box.vala
index 954d72bb..e54e7ebb 100644
--- a/src/ui/collection-box.vala
+++ b/src/ui/collection-box.vala
@@ -142,14 +142,10 @@ private class Games.CollectionBox : Gtk.Box {
 
        [GtkCallback]
        private void on_visible_child_changed () {
-               if (viewstack.visible_child == platform_view) {
+               if (viewstack.visible_child == platform_view)
                        platform_view.filtering_text = search_bar.text;
-                       platform_view.select_default_row ();
-               }
-               else if (viewstack.visible_child == developer_view) {
+               else if (viewstack.visible_child == developer_view)
                        developer_view.filtering_text = search_bar.text;
-                       developer_view.select_default_row ();
-               }
                else {
                        collection_view.filtering_text = search_bar.text;
                        collection_view.reset_scroll_position ();
@@ -164,6 +160,12 @@ private class Games.CollectionBox : Gtk.Box {
                        developer_view.filtering_text = search_bar.text;
                else
                        collection_view.filtering_text = search_bar.text;
+
+               // Changing the filtering_text for the Developer|PlatformView might
+               // cause the currently selected sidebar row to become empty and therefore
+               // hidden. In this case the first visible row will become selected and
+               // this causes the search bar to lose focus so we have to regrab it here
+               search_bar.focus_entry ();
        }
 
        public bool search_bar_handle_event (Gdk.Event event) {
diff --git a/src/ui/sidebar-view.vala b/src/ui/sidebar-view.vala
index 202c1433..3e7385fa 100644
--- a/src/ui/sidebar-view.vala
+++ b/src/ui/sidebar-view.vala
@@ -4,8 +4,45 @@
 private abstract class Games.SidebarView : Gtk.Box {
        public signal void game_activated (Game game);
 
+       private string[] filtering_terms;
        public string filtering_text {
-               set { collection_view.filtering_text = value; }
+               set {
+                       collection_view.filtering_text = value;
+
+                       if (value != null)
+                               filtering_terms = value.split (" ");
+
+                       hide_empty_sidebar_items ();
+               }
+       }
+
+       private void hide_empty_sidebar_items () {
+               // Create an array of all the games which fit the search text entered
+               // in the top search bar
+               Game[] visible_games = {};
+
+               for (int i = 0; i < model.get_n_items (); i++) {
+                       var game = model.get_item (i) as Game;
+
+                       if (game.matches_search_terms (filtering_terms))
+                               visible_games += game;
+               }
+
+               foreach (var row in list_box.get_children ()) {
+                       var sidebar_item = row as SidebarListItem;
+                       var is_row_visible = false; // Assume row doesn't have any games to show
+
+                       foreach (var game in visible_games) {
+                               if (sidebar_item.has_game (game)) {
+                                       is_row_visible = true;
+                                       break;
+                               }
+                       }
+
+                       row.visible = is_row_visible;
+               }
+
+               select_default_row ();
        }
 
        private ulong model_items_changed_id;
@@ -195,11 +232,13 @@ private abstract class Games.SidebarView : Gtk.Box {
        }
 
        public void select_default_row () {
-               var row = list_box.get_row_at_index (0) as Gtk.ListBoxRow;
-
-               if (row == null)
-                       return;
+               foreach (var child in list_box.get_children ()) {
+                       var row = child as Gtk.ListBoxRow;
 
-               on_list_box_row_selected (row);
+                       if (row.visible) {
+                               on_list_box_row_selected (row);
+                               break;
+                       }
+               }
        }
 }


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