[fractal/fractal-next] item_list: Small refactor



commit 7e98373ea0efbaba5253171813140a892cdcbd4e
Author: Kai A. Hiller <V02460 gmail com>
Date:   Thu Aug 5 14:53:42 2021 +0200

    item_list: Small refactor
    
    item_list: Restrict interface a bit
    item_list: Make room-list a property
    item_list: Use actual list lenght on item update

 src/session/sidebar/item_list.rs | 68 +++++++++++++++++++++++++++-------------
 src/session/sidebar/sidebar.rs   |  3 +-
 2 files changed, 47 insertions(+), 24 deletions(-)
---
diff --git a/src/session/sidebar/item_list.rs b/src/session/sidebar/item_list.rs
index b8b1be5d..8042d3a8 100644
--- a/src/session/sidebar/item_list.rs
+++ b/src/session/sidebar/item_list.rs
@@ -8,6 +8,7 @@ use crate::session::{
 };
 
 mod imp {
+    use once_cell::sync::Lazy;
     use once_cell::unsync::OnceCell;
 
     use super::*;
@@ -25,7 +26,37 @@ mod imp {
         type Interfaces = (gio::ListModel,);
     }
 
-    impl ObjectImpl for ItemList {}
+    impl ObjectImpl for ItemList {
+        fn properties() -> &'static [glib::ParamSpec] {
+            static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
+                vec![glib::ParamSpec::new_object(
+                    "room-list",
+                    "Room list",
+                    "Data model for the categories",
+                    RoomList::static_type(),
+                    glib::ParamFlags::WRITABLE | glib::ParamFlags::CONSTRUCT_ONLY,
+                )]
+            });
+
+            PROPERTIES.as_ref()
+        }
+
+        fn set_property(
+            &self,
+            obj: &Self::Type,
+            _id: usize,
+            value: &glib::Value,
+            pspec: &glib::ParamSpec,
+        ) {
+            match pspec.name() {
+                "room-list" => {
+                    let x = value.get().unwrap();
+                    obj.set_room_list(&x)
+                }
+                _ => unimplemented!(),
+            }
+        }
+    }
 
     impl ListModelImpl for ItemList {
         fn item_type(&self, _list_model: &Self::Type) -> glib::Type {
@@ -53,32 +84,25 @@ glib::wrapper! {
         @implements gio::ListModel;
 }
 
-impl Default for ItemList {
-    fn default() -> Self {
-        Self::new()
-    }
-}
-
 impl ItemList {
-    pub fn new() -> Self {
-        glib::Object::new(&[]).expect("Failed to create ItemList")
+    pub fn new(room_list: &RoomList) -> Self {
+        glib::Object::new(&[("room-list", room_list)]).expect("Failed to create ItemList")
     }
 
-    pub fn set_room_list(&self, room_list: &RoomList) {
+    fn set_room_list(&self, room_list: &RoomList) {
         let priv_ = imp::ItemList::from_instance(self);
 
-        priv_
-            .list
-            .set([
-                Entry::new(ContentType::Explore).upcast::<glib::Object>(),
-                Category::new(RoomType::Invited, room_list).upcast::<glib::Object>(),
-                Category::new(RoomType::Favorite, room_list).upcast::<glib::Object>(),
-                Category::new(RoomType::Normal, room_list).upcast::<glib::Object>(),
-                Category::new(RoomType::LowPriority, room_list).upcast::<glib::Object>(),
-                Category::new(RoomType::Left, room_list).upcast::<glib::Object>(),
-            ])
-            .unwrap();
+        let list = [
+            Entry::new(ContentType::Explore).upcast::<glib::Object>(),
+            Category::new(RoomType::Invited, room_list).upcast::<glib::Object>(),
+            Category::new(RoomType::Favorite, room_list).upcast::<glib::Object>(),
+            Category::new(RoomType::Normal, room_list).upcast::<glib::Object>(),
+            Category::new(RoomType::LowPriority, room_list).upcast::<glib::Object>(),
+            Category::new(RoomType::Left, room_list).upcast::<glib::Object>(),
+        ];
+        let len = list.len() as u32;
 
-        self.items_changed(0, 0, 6);
+        priv_.list.set(list).unwrap();
+        self.items_changed(0, 0, len);
     }
 }
diff --git a/src/session/sidebar/sidebar.rs b/src/session/sidebar/sidebar.rs
index 50ff61ce..19fe5a82 100644
--- a/src/session/sidebar/sidebar.rs
+++ b/src/session/sidebar/sidebar.rs
@@ -197,8 +197,7 @@ impl Sidebar {
         };
 
         // TODO: hide empty categories
-        let item_list = ItemList::new();
-        item_list.set_room_list(&room_list);
+        let item_list = ItemList::new(&room_list);
         let tree_model = gtk::TreeListModel::new(&item_list, false, true, |item| {
             item.clone().downcast::<gio::ListModel>().ok()
         });


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