Browse Source

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
merge-requests/1327/merge
Kai A. Hiller 5 years ago committed by Julian Sparber
parent
commit
7e98373ea0
  1. 68
      src/session/sidebar/item_list.rs
  2. 3
      src/session/sidebar/sidebar.rs

68
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);
}
}

3
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()
});

Loading…
Cancel
Save