[gnome-boxes/wip/feborges/flowbox: 16/24] list-view: Bind GtkListBox to Collection model



commit a68605e38ec3c32584cd05d53dffd250f4d12a93
Author: Felipe Borges <felipeborges gnome org>
Date:   Wed Dec 20 18:00:49 2017 +0100

    list-view: Bind GtkListBox to Collection model
    
    We used to subscribe to the item_added and item_removed signals of
    Collection and insert the rows accordingly.
    
    Now the ListView listbox is binded directly to the Collection model
    and therefore the model-view mapping is done automatically.
    
    The changes in app.vala and app-window.vala are temporary so we
    don't brake the current implementation of IconView.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=791839

 src/app-window.vala |    2 +-
 src/app.vala        |    4 ++--
 src/list-view.vala  |   46 +++++++++++++++-------------------------------
 3 files changed, 18 insertions(+), 34 deletions(-)
---
diff --git a/src/app-window.vala b/src/app-window.vala
index 2b2e900..c6c4689 100644
--- a/src/app-window.vala
+++ b/src/app-window.vala
@@ -95,7 +95,7 @@ private class Boxes.AppWindow: Gtk.ApplicationWindow, Boxes.UI {
     [GtkChild]
     public Gtk.Stack below_bin;
     [GtkChild]
-    private IconView icon_view;
+    public IconView icon_view;
     [GtkChild]
     private ListView list_view;
 
diff --git a/src/app.vala b/src/app.vala
index fa4bd6f..f7e5508 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -127,10 +127,10 @@ private class Boxes.App: Gtk.Application {
         collection = new Collection ();
 
         collection.item_added.connect ((item) => {
-            main_window.foreach_view ((view) => { view.add_item (item); });
+            main_window.icon_view.add_item (item);
         });
         collection.item_removed.connect ((item) => {
-            main_window.foreach_view ((view) => { view.remove_item (item); });
+            main_window.icon_view.remove_item (item);
         });
 
         brokers.insert ("libvirt", LibvirtBroker.get_default ());
diff --git a/src/list-view.vala b/src/list-view.vala
index cf9fd03..0f6b624 100644
--- a/src/list-view.vala
+++ b/src/list-view.vala
@@ -113,28 +113,11 @@ private class Boxes.ListView: Gtk.ScrolledWindow, Boxes.ICollectionView, Boxes.U
             return;
         }
 
-        add_row (item);
         items_connections[item] = new ItemConnections (this, machine);
 
         item.set_state (window.ui_state);
     }
 
-    private void add_row (CollectionItem item) {
-        var box_row = new Gtk.ListBoxRow ();
-        size_group.add_widget (box_row);
-        var view_row = new ListViewRow (item);
-
-        view_row.notify["selected"].connect (() => {
-            propagate_view_row_selection (view_row);
-        });
-
-        box_row.visible = true;
-        view_row.visible = true;
-
-        box_row.add (view_row);
-        list_box.add (box_row);
-    }
-
     public void remove_item (CollectionItem item) {
         hidden_items.remove (item);
         items_connections.remove (item);
@@ -148,7 +131,6 @@ private class Boxes.ListView: Gtk.ScrolledWindow, Boxes.ICollectionView, Boxes.U
                 return;
 
             if (view_row.item == item) {
-                list_box.remove (box_row);
                 size_group.remove_widget (box_row);
             }
         });
@@ -205,8 +187,22 @@ private class Boxes.ListView: Gtk.ScrolledWindow, Boxes.ICollectionView, Boxes.U
     }
 
     private void setup_list_box () {
+        list_box.bind_model (App.app.collection.items, (item) => {
+            var box_row = new Gtk.ListBoxRow ();
+            size_group.add_widget (box_row);
+            var view_row = new ListViewRow (item as CollectionItem);
+            box_row.add (view_row);
+
+            view_row.notify["selected"].connect (() => {
+                propagate_view_row_selection (view_row);
+            });
+
+            box_row.visible = true;
+            view_row.visible = true;
+
+            return box_row;
+        });
         list_box.selection_mode = Gtk.SelectionMode.NONE;
-        list_box.set_sort_func (model_sort);
         list_box.set_filter_func (model_filter);
 
         list_box.row_activated.connect ((box_row) => {
@@ -241,18 +237,6 @@ private class Boxes.ListView: Gtk.ScrolledWindow, Boxes.ICollectionView, Boxes.U
         });
     }
 
-    private int model_sort (Gtk.ListBoxRow box_row1, Gtk.ListBoxRow box_row2) {
-        var view_row1 = box_row1.get_child () as ListViewRow;
-        var view_row2 = box_row2.get_child () as ListViewRow;
-        var item1 = view_row1.item;
-        var item2 = view_row2.item;
-
-        if (item1 == null || item2 == null)
-            return 0;
-
-        return item1.compare (item2);
-    }
-
     private bool model_filter (Gtk.ListBoxRow box_row) {
         var view = box_row.get_child () as ListViewRow;
         if (view  == null)


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