From 7e98373ea0efbaba5253171813140a892cdcbd4e Mon Sep 17 00:00:00 2001 From: "Kai A. Hiller" Date: Thu, 5 Aug 2021 14:53:42 +0200 Subject: [PATCH] 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> = 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::(), - Category::new(RoomType::Invited, room_list).upcast::(), - Category::new(RoomType::Favorite, room_list).upcast::(), - Category::new(RoomType::Normal, room_list).upcast::(), - Category::new(RoomType::LowPriority, room_list).upcast::(), - Category::new(RoomType::Left, room_list).upcast::(), - ]) - .unwrap(); + let list = [ + Entry::new(ContentType::Explore).upcast::(), + Category::new(RoomType::Invited, room_list).upcast::(), + Category::new(RoomType::Favorite, room_list).upcast::(), + Category::new(RoomType::Normal, room_list).upcast::(), + Category::new(RoomType::LowPriority, room_list).upcast::(), + Category::new(RoomType::Left, room_list).upcast::(), + ]; + 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::().ok() });