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

167 lines
5.1 KiB
Rust
Raw Normal View History

use crate::ui::GamePreview;
use adw::{prelude::*, subclass::prelude::*};
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 LibraryPrivate {
2023-08-20 03:24:01 +00:00
model: gio::ListStore,
list_view: gtk::ColumnView,
2023-08-20 00:46:43 +00:00
}
impl Default for LibraryPrivate {
2023-08-20 00:46:43 +00:00
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);
/*
2023-08-20 00:46:43 +00:00
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| {
let preview = GamePreview::new();
2023-08-20 00:46:43 +00:00
list_item
.downcast_ref::<gtk::ListItem>()
.expect("Needs to be a ListItem")
.set_child(Some(&preview));
2023-08-20 00:46:43 +00:00
});
factory.connect_bind(move |_, list_item| {
let game_element = 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>()
.expect("The item has to be a GameObject.");
2023-08-20 00:46:43 +00:00
let preview = list_item
2023-08-20 00:46:43 +00:00
.downcast_ref::<gtk::ListItem>()
.expect("Needs to be ListItem")
.child()
.and_downcast::<GamePreview>()
.expect("The child has to be a GamePreview object.");
2023-08-20 00:46:43 +00:00
match game_element.game() {
Some(game) => preview.set_game(game),
None => (),
};
2023-08-20 00:46:43 +00:00
});
*/
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()));
let list_view = gtk::ColumnView::builder().model(&selection_model).build();
fn make_factory<F>(bind: F) -> gtk::SignalListItemFactory
where
F: Fn(GamePreviewElement) -> String + 'static,
{
let factory = gtk::SignalListItemFactory::new();
factory.connect_setup(|_, list_item| {
list_item
.downcast_ref::<gtk::ListItem>()
.unwrap()
.set_child(Some(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.ellipsize(pango::EllipsizeMode::End)
.build(),
))
});
factory.connect_bind(move |_, list_item| {
let list_item = list_item.downcast_ref::<gtk::ListItem>().unwrap();
let game = list_item.item().and_downcast::<GameObject>().unwrap();
let preview = list_item.child().and_downcast::<gtk::Label>().unwrap();
match game.game() {
Some(game) => preview.set_text(&bind(game)),
None => (),
};
});
factory
}
list_view.append_column(&gtk::ColumnViewColumn::new(
Some("date"),
Some(make_factory(|g| g.date)),
));
list_view.append_column(&gtk::ColumnViewColumn::new(
Some("title"),
Some(make_factory(|g| g.name)),
));
list_view.append_column(&gtk::ColumnViewColumn::new(
Some("black"),
Some(make_factory(|g| g.black_player)),
));
list_view.append_column(&gtk::ColumnViewColumn::new(
Some("white"),
Some(make_factory(|g| g.white_player)),
));
list_view.append_column(&gtk::ColumnViewColumn::new(
Some("result"),
Some(make_factory(|g| g.result)),
));
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 LibraryPrivate {
const NAME: &'static str = "Library";
type Type = Library;
type ParentType = adw::Bin;
2023-08-20 00:46:43 +00:00
}
impl ObjectImpl for LibraryPrivate {}
impl WidgetImpl for LibraryPrivate {}
impl BinImpl for LibraryPrivate {}
2023-08-20 00:46:43 +00:00
glib::wrapper! {
pub struct Library(ObjectSubclass<LibraryPrivate>) @extends adw::Bin, gtk::Widget;
2023-08-20 00:46:43 +00:00
}
impl Library {
2023-08-20 00:46:43 +00:00
pub fn new() -> Self {
let s: Self = Object::builder().build();
s.set_child(Some(&s.imp().list_view));
2023-08-20 00:46:43 +00:00
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
}