[gnome-boxes/wip/exalm/libhandy1: 4/6] collection-filter-view: Turn filter func into an enum



commit 567d5022c53ac73f019459877cfc3fc2a7033d9d
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Tue May 26 19:45:28 2020 +0500

    collection-filter-view: Turn filter func into an enum
    
    This will allow to easily have it settable from UI files.

 src/app-window.vala                 |  4 ++--
 src/collection-filter-switcher.vala | 19 +++++-------------
 src/collection-filter-view.vala     | 39 +++++++++++++++++++++++++++++--------
 3 files changed, 38 insertions(+), 24 deletions(-)
---
diff --git a/src/app-window.vala b/src/app-window.vala
index 862cb733..4167fa5b 100644
--- a/src/app-window.vala
+++ b/src/app-window.vala
@@ -500,7 +500,7 @@ private void on_machine_deleted_notify () {
            on_delete_event ();
     }
 
-    public void set_filter_func (CollectionFilterFunc? filter_func) {
-        collection_view.filter_func = filter_func;
+    public void set_filter_type (CollectionFilterView.FilterType filter_type) {
+        collection_view.filter_type = filter_type;
     }
 }
diff --git a/src/collection-filter-switcher.vala b/src/collection-filter-switcher.vala
index cf198745..b0a6ce35 100644
--- a/src/collection-filter-switcher.vala
+++ b/src/collection-filter-switcher.vala
@@ -17,24 +17,15 @@ public void setup_ui (AppWindow window) {
         all_button.active = true;
         activate_button (all_button);
         App.app.call_when_ready (on_app_ready);
-
-        window.set_filter_func (null);
     }
 
-    private unowned CollectionFilterFunc? get_filter_func () {
+    private CollectionFilterView.FilterType get_filter_type () {
         if (active_button == all_button)
-            return null;
+            return CollectionFilterView.FilterType.ALL;
         if (active_button == favorites_button)
-            return favorites_filter_func;
+            return CollectionFilterView.FilterType.FAVORITES;
         else
-            return null;
-    }
-
-    private bool favorites_filter_func (Boxes.CollectionItem item) {
-        assert (item != null && item is Machine);
-        var machine = item as Machine;
-
-        return "favorite" in machine.config.categories;
+            return CollectionFilterView.FilterType.ALL;
     }
 
     private void on_app_ready () {
@@ -62,6 +53,6 @@ private void activate_button (Gtk.ToggleButton button) {
                 toggle_button.active = toggle_button == active_button;
         }
 
-        window.set_filter_func (get_filter_func ());
+        window.set_filter_type (get_filter_type ());
     }
 }
diff --git a/src/collection-filter-view.vala b/src/collection-filter-view.vala
index d9d2f07e..05dbebd4 100644
--- a/src/collection-filter-view.vala
+++ b/src/collection-filter-view.vala
@@ -5,6 +5,11 @@
     public UIState previous_ui_state { get; protected set; }
     public UIState ui_state { get; protected set; }
 
+    public enum FilterType {
+        ALL,
+        FAVORITES,
+    }
+
     [GtkChild]
     private Gtk.Stack stack;
     [GtkChild]
@@ -14,13 +19,25 @@
 
     public AppWindow.ViewType view_type { get; set; default = AppWindow.ViewType.ICON; }
 
-    private unowned CollectionFilterFunc _filter_func = null;
-    public unowned CollectionFilterFunc filter_func {
-        get { return _filter_func; }
+    private FilterType _filter_type = FilterType.ALL;
+    public FilterType filter_type {
+        get { return _filter_type; }
         set {
-            _filter_func = value;
-            icon_view.filter.filter_func = value;
-            list_view.filter.filter_func = value;
+            _filter_type = value;
+
+            CollectionFilterFunc? filter_func = null;
+            switch (filter_type) {
+            default:
+            case FilterType.ALL:
+                filter_func = null;
+                break;
+            case FilterType.FAVORITES:
+                filter_func = favorites_filter_func;
+                break;
+            }
+
+            icon_view.filter.filter_func = filter_func;
+            list_view.filter.filter_func = filter_func;
         }
     }
 
@@ -37,8 +54,7 @@
     }
 
     construct {
-        notify["view-type"].connect (ui_state_changed);
-    }
+        notify["view-type"].connect (ui_state_changed);    }
 
     public void setup_ui (AppWindow window) {
         icon_view.setup_ui (window);
@@ -61,4 +77,11 @@ private void ui_state_changed () {
             list_view.show ();
         }
     }
+
+    private bool favorites_filter_func (Boxes.CollectionItem item) {
+        assert (item != null && item is Machine);
+        var machine = item as Machine;
+
+        return "favorite" in machine.config.categories;
+    }
 }


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