monorepo/kifu/gtk/src/ui/game_database.rs

123 lines
3.6 KiB
Rust
Raw Normal View History

2023-08-20 03:24:01 +00:00
use glib::Object;
2023-08-20 00:46:43 +00:00
use gtk::{glib, prelude::*, subclass::prelude::*};
2023-08-20 03:24:01 +00:00
use kifu_core::ui::GamePreviewElement;
2023-08-20 00:46:43 +00:00
use std::{cell::RefCell, rc::Rc};
#[derive(Default)]
2023-08-20 03:24:01 +00:00
pub struct GameObjectPrivate {
game: Rc<RefCell<Option<GamePreviewElement>>>,
2023-08-20 00:46:43 +00:00
}
#[glib::object_subclass]
2023-08-20 03:24:01 +00:00
impl ObjectSubclass for GameObjectPrivate {
const NAME: &'static str = "GameObject";
type Type = GameObject;
2023-08-20 00:46:43 +00:00
}
2023-08-20 03:24:01 +00:00
impl ObjectImpl for GameObjectPrivate {}
2023-08-20 00:46:43 +00:00
glib::wrapper! {
2023-08-20 03:24:01 +00:00
pub struct GameObject(ObjectSubclass<GameObjectPrivate>);
2023-08-20 00:46:43 +00:00
}
2023-08-20 03:24:01 +00:00
impl GameObject {
pub fn new(game: GamePreviewElement) -> Self {
2023-08-20 00:46:43 +00:00
let s: Self = Object::builder().build();
2023-08-20 03:24:01 +00:00
*s.imp().game.borrow_mut() = Some(game);
2023-08-20 00:46:43 +00:00
s
}
2023-08-20 03:24:01 +00:00
pub fn game(&self) -> Option<GamePreviewElement> {
self.imp().game.borrow().clone()
2023-08-20 00:46:43 +00:00
}
}
pub struct GameDatabasePrivate {
2023-08-20 03:24:01 +00:00
model: gio::ListStore,
2023-08-20 00:46:43 +00:00
list_view: gtk::ListView,
}
impl Default for GameDatabasePrivate {
fn default() -> Self {
2023-08-20 03:24:01 +00:00
let vector: Vec<GameObject> = vec![];
2023-08-20 00:46:43 +00:00
let model = gio::ListStore::new(glib::types::Type::OBJECT);
model.extend_from_slice(&vector);
let factory = gtk::SignalListItemFactory::new();
2023-08-20 03:24:01 +00:00
2023-08-20 00:46:43 +00:00
factory.connect_setup(move |_, list_item| {
2023-08-20 03:24:01 +00:00
let label = gtk::Label::new(Some("some kind of text"));
2023-08-20 00:46:43 +00:00
list_item
.downcast_ref::<gtk::ListItem>()
.expect("Needs to be a ListItem")
.set_child(Some(&label));
});
factory.connect_bind(move |_, list_item| {
2023-08-20 03:24:01 +00:00
let game_object = list_item
2023-08-20 00:46:43 +00:00
.downcast_ref::<gtk::ListItem>()
.expect("Needs to be ListItem")
.item()
2023-08-20 03:24:01 +00:00
.and_downcast::<GameObject>()
2023-08-20 00:46:43 +00:00
.expect("The item has to be an IntegerObject.");
let label = list_item
.downcast_ref::<gtk::ListItem>()
.expect("Needs to be ListItem")
.child()
.and_downcast::<gtk::Label>()
.expect("The child has to be an Label.");
2023-08-20 03:24:01 +00:00
label.set_label(
format!(
"{} vs. {}",
game_object
.game()
.map(|g| g.black_player)
.unwrap_or("black".to_owned()),
game_object
.game()
.map(|g| g.white_player)
.unwrap_or("white".to_owned())
)
.as_ref(),
);
2023-08-20 00:46:43 +00:00
});
2023-08-20 03:24:01 +00:00
let selection_model = gtk::NoSelection::new(Some(model.clone()));
2023-08-20 00:46:43 +00:00
let list_view = gtk::ListView::new(Some(selection_model), Some(factory));
2023-08-20 03:24:01 +00:00
Self { model, list_view }
2023-08-20 00:46:43 +00:00
}
}
#[glib::object_subclass]
impl ObjectSubclass for GameDatabasePrivate {
const NAME: &'static str = "GameDatabase";
type Type = GameDatabase;
type ParentType = gtk::Box;
}
impl ObjectImpl for GameDatabasePrivate {}
impl WidgetImpl for GameDatabasePrivate {}
impl BoxImpl for GameDatabasePrivate {}
glib::wrapper! {
pub struct GameDatabase(ObjectSubclass<GameDatabasePrivate>) @extends gtk::Widget, gtk::Box;
}
impl GameDatabase {
pub fn new() -> Self {
let s: Self = Object::builder().build();
2023-08-20 03:24:01 +00:00
s.set_width_request(200);
s.set_height_request(200);
2023-08-20 00:46:43 +00:00
s.append(&s.imp().list_view);
s
}
2023-08-20 03:24:01 +00:00
pub fn set_games(&self, games: Vec<GamePreviewElement>) {
let games = games
.into_iter()
.map(|g| GameObject::new(g))
.collect::<Vec<GameObject>>();
self.imp().model.extend_from_slice(&games);
}
2023-08-20 00:46:43 +00:00
}