[gnome-games] ui: Use filter func again



commit 3a1031372ac533902a594f1c995d9c2f56c0b530
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Tue Feb 11 01:40:08 2020 +0500

    ui: Use filter func again
    
    Despite the warning, it works reliably unlike the ugly custom solution I
    had. Boxes does the same, oh well.

 src/ui/collection-box.vala       |  2 ++
 src/ui/collection-icon-view.vala | 23 ++++++++++++++---------
 src/ui/platforms-view.vala       | 32 +++++++++-----------------------
 3 files changed, 25 insertions(+), 32 deletions(-)
---
diff --git a/src/ui/collection-box.vala b/src/ui/collection-box.vala
index 31931139..49c0299b 100644
--- a/src/ui/collection-box.vala
+++ b/src/ui/collection-box.vala
@@ -162,6 +162,8 @@ private class Games.CollectionBox : Gtk.Box {
        private void on_visible_child_changed () {
                if (viewstack.visible_child == collection_view)
                        collection_view.reset_scroll_position ();
+               else
+                       platform_view.select_first_visible_row ();
 
                is_subview_open = false;
        }
diff --git a/src/ui/collection-icon-view.vala b/src/ui/collection-icon-view.vala
index 222af0f1..c92b5c9a 100644
--- a/src/ui/collection-icon-view.vala
+++ b/src/ui/collection-icon-view.vala
@@ -18,8 +18,6 @@ private class Games.CollectionIconView : Gtk.Bin {
                set {
                        _game_model = value;
                        flow_box.bind_model (game_model, add_game);
-
-                       game_model.items_changed.connect (apply_filter);
                }
        }
 
@@ -52,6 +50,7 @@ private class Games.CollectionIconView : Gtk.Bin {
 
        construct {
                flow_box.max_children_per_line = uint.MAX;
+               flow_box.set_filter_func (filter_box);
        }
 
        [GtkCallback]
@@ -95,7 +94,7 @@ private class Games.CollectionIconView : Gtk.Bin {
 
        public void set_filter (string[] filtering_terms) {
                this.filtering_terms = filtering_terms;
-               apply_filter ();
+               flow_box.invalidate_filter ();
        }
 
        public void reset_scroll_position () {
@@ -173,13 +172,19 @@ private class Games.CollectionIconView : Gtk.Bin {
                return game_view;
        }
 
-       public void apply_filter () {
-               flow_box.foreach (widget => {
-                       var child = widget as Gtk.FlowBoxChild;
-                       var game_view = child.get_child () as GameIconView;
+       public void invalidate_filter () {
+               flow_box.invalidate_filter ();
+       }
+
+       private bool filter_box (Gtk.FlowBoxChild child) {
+               var game_view = child.get_child () as GameIconView;
+               if (game_view == null)
+                       return false;
+
+               if (game_view.game == null)
+                       return false;
 
-                       widget.visible = filter_game (game_view.game);
-               });
+               return filter_game (game_view.game);
        }
 
        private bool filter_game (Game game) {
diff --git a/src/ui/platforms-view.vala b/src/ui/platforms-view.vala
index 95adf13d..dac8c578 100644
--- a/src/ui/platforms-view.vala
+++ b/src/ui/platforms-view.vala
@@ -20,8 +20,6 @@ private class Games.PlatformsView : Gtk.Bin {
 
        private string[] filtering_terms;
 
-       private bool reset_selected_row;
-
        private GameModel _game_model;
        public GameModel game_model {
                get { return _game_model; }
@@ -31,8 +29,6 @@ private class Games.PlatformsView : Gtk.Bin {
 
                        var platform_model = new PlatformModel (value);
                        list_box.bind_model (platform_model, add_platform);
-
-                       platform_model.items_changed.connect (apply_filter);
                }
        }
 
@@ -57,24 +53,14 @@ private class Games.PlatformsView : Gtk.Bin {
 
        construct {
                collection_view.set_game_filter (filter_game);
-               reset_selected_row = true;
+               list_box.set_filter_func (filter_list);
        }
 
-       private void apply_filter () {
-               list_box.foreach (widget => {
-                       var row = widget as PlatformListItem;
-
-                       if (row == null)
-                               return;
-
-                       widget.set_visible (filter_list (row));
-               });
-
-               if (reset_selected_row)
-                       select_first_visible_row ();
-       }
+       private bool filter_list (Gtk.ListBoxRow? row) {
+               var item = row as PlatformListItem;
+               if (item == null)
+                       return false;
 
-       private bool filter_list (PlatformListItem item) {
                if (item.platform == null)
                        return false;
 
@@ -123,8 +109,8 @@ private class Games.PlatformsView : Gtk.Bin {
                this.filtering_terms = filtering_terms;
                collection_view.set_filter (filtering_terms);
 
-               reset_selected_row = true;
-               apply_filter ();
+               list_box.invalidate_filter ();
+               select_first_visible_row ();
        }
 
        public bool gamepad_button_press_event (Manette.Event event) {
@@ -225,11 +211,11 @@ private class Games.PlatformsView : Gtk.Bin {
                selected_platform = row.platform;
                subview_title = selected_platform.get_name ();
 
-               collection_view.apply_filter ();
+               collection_view.invalidate_filter ();
                collection_view.reset_scroll_position ();
        }
 
-       private void select_first_visible_row () {
+       public void select_first_visible_row () {
                foreach (var child in list_box.get_children ()) {
                        var row = child as Gtk.ListBoxRow;
 


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