use crate::ui::GamePreview; use glib::Object; use gtk::{glib, prelude::*, subclass::prelude::*}; use kifu_core::ui::GamePreviewElement; use std::{cell::RefCell, rc::Rc}; #[derive(Default)] pub struct GameObjectPrivate { game: Rc>>, } #[glib::object_subclass] impl ObjectSubclass for GameObjectPrivate { const NAME: &'static str = "GameObject"; type Type = GameObject; } impl ObjectImpl for GameObjectPrivate {} glib::wrapper! { pub struct GameObject(ObjectSubclass); } impl GameObject { pub fn new(game: GamePreviewElement) -> Self { let s: Self = Object::builder().build(); *s.imp().game.borrow_mut() = Some(game); s } pub fn game(&self) -> Option { self.imp().game.borrow().clone() } } pub struct LibraryPrivate { model: gio::ListStore, list_view: gtk::ListView, } impl Default for LibraryPrivate { fn default() -> Self { let vector: Vec = vec![]; let model = gio::ListStore::new(glib::types::Type::OBJECT); model.extend_from_slice(&vector); let factory = gtk::SignalListItemFactory::new(); factory.connect_setup(move |_, list_item| { let preview = GamePreview::new(); list_item .downcast_ref::() .expect("Needs to be a ListItem") .set_child(Some(&preview)); }); factory.connect_bind(move |_, list_item| { let game_element = list_item .downcast_ref::() .expect("Needs to be ListItem") .item() .and_downcast::() .expect("The item has to be a GameObject."); let preview = list_item .downcast_ref::() .expect("Needs to be ListItem") .child() .and_downcast::() .expect("The child has to be a GamePreview object."); match game_element.game() { Some(game) => preview.set_game(game), None => (), }; }); let selection_model = gtk::NoSelection::new(Some(model.clone())); let list_view = gtk::ListView::new(Some(selection_model), Some(factory)); list_view.set_hexpand(true); Self { model, list_view } } } #[glib::object_subclass] impl ObjectSubclass for LibraryPrivate { const NAME: &'static str = "Library"; type Type = Library; type ParentType = gtk::Box; } impl ObjectImpl for LibraryPrivate {} impl WidgetImpl for LibraryPrivate {} impl BoxImpl for LibraryPrivate {} glib::wrapper! { pub struct Library(ObjectSubclass) @extends gtk::Widget, gtk::Box; } impl Library { pub fn new() -> Self { let s: Self = Object::builder().build(); s.set_hexpand(true); s.append(&s.imp().list_view); s } pub fn set_games(&self, games: Vec) { let games = games .into_iter() .map(|g| GameObject::new(g)) .collect::>(); self.imp().model.extend_from_slice(&games); } }