Show a list of games in the library #64

Merged
savanni merged 9 commits from kifu/game-library into main 2023-08-20 17:02:14 +00:00
5 changed files with 58 additions and 68 deletions
Showing only changes of commit e3f4ca246d - Show all commits

2
Cargo.lock generated
View File

@ -1348,7 +1348,7 @@ dependencies = [
"gtk4", "gtk4",
"image", "image",
"kifu-core", "kifu-core",
"screenplay", "sgf",
"tokio", "tokio",
] ]

View File

@ -69,17 +69,11 @@ pub struct CoreApp {
impl CoreApp { impl CoreApp {
pub fn new(config_path: std::path::PathBuf) -> Self { pub fn new(config_path: std::path::PathBuf) -> Self {
println!("config_path: {:?}", config_path);
let config = Config::from_path(config_path).expect("configuration to open"); let config = Config::from_path(config_path).expect("configuration to open");
println!("config: {:?}", config);
let db_path: DatabasePath = config.get().unwrap(); let db_path: DatabasePath = config.get().unwrap();
println!("db_path: {:?}", db_path);
let state = Arc::new(RwLock::new(AppState::new(db_path))); let state = Arc::new(RwLock::new(AppState::new(db_path)));
println!("config: {:?}", config);
println!("games database: {:?}", state.read().unwrap().database.len());
Self { config, state } Self { config, state }
} }

View File

@ -16,7 +16,8 @@ gtk = { version = "0.6", package = "gtk4", features = ["v4_8"] }
image = { version = "0.24" } image = { version = "0.24" }
kifu-core = { path = "../core" } kifu-core = { path = "../core" }
tokio = { version = "1.26", features = [ "full" ] } tokio = { version = "1.26", features = [ "full" ] }
screenplay = { path = "../../screenplay" } # screenplay = { path = "../../screenplay" }
sgf = { path = "../../sgf" }
[build-dependencies] [build-dependencies]
glib-build-tools = "0.17" glib-build-tools = "0.17"

View File

@ -1,59 +1,62 @@
use glib::{Object, Properties}; use glib::Object;
use gtk::{glib, prelude::*, subclass::prelude::*}; use gtk::{glib, prelude::*, subclass::prelude::*};
use kifu_core::ui::GamePreviewElement;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
#[derive(Default)] #[derive(Default)]
pub struct IntegerObjectPrivate { pub struct GameObjectPrivate {
number: Rc<RefCell<i32>>, game: Rc<RefCell<Option<GamePreviewElement>>>,
} }
#[glib::object_subclass] #[glib::object_subclass]
impl ObjectSubclass for IntegerObjectPrivate { impl ObjectSubclass for GameObjectPrivate {
const NAME: &'static str = "IntegerObject"; const NAME: &'static str = "GameObject";
type Type = IntegerObject; type Type = GameObject;
} }
impl ObjectImpl for IntegerObjectPrivate {} impl ObjectImpl for GameObjectPrivate {}
glib::wrapper! { glib::wrapper! {
pub struct IntegerObject(ObjectSubclass<IntegerObjectPrivate>); pub struct GameObject(ObjectSubclass<GameObjectPrivate>);
} }
impl IntegerObject { impl GameObject {
pub fn new(number: i32) -> Self { pub fn new(game: GamePreviewElement) -> Self {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
*s.imp().number.borrow_mut() = number; *s.imp().game.borrow_mut() = Some(game);
s s
} }
pub fn number(&self) -> i32 { pub fn game(&self) -> Option<GamePreviewElement> {
self.imp().number.borrow().clone() self.imp().game.borrow().clone()
} }
} }
pub struct GameDatabasePrivate { pub struct GameDatabasePrivate {
model: gio::ListStore,
list_view: gtk::ListView, list_view: gtk::ListView,
} }
impl Default for GameDatabasePrivate { impl Default for GameDatabasePrivate {
fn default() -> Self { fn default() -> Self {
let vector: Vec<IntegerObject> = (0..=500).map(IntegerObject::new).collect(); let vector: Vec<GameObject> = vec![];
let model = gio::ListStore::new(glib::types::Type::OBJECT); let model = gio::ListStore::new(glib::types::Type::OBJECT);
model.extend_from_slice(&vector); model.extend_from_slice(&vector);
let factory = gtk::SignalListItemFactory::new(); let factory = gtk::SignalListItemFactory::new();
factory.connect_setup(move |_, list_item| { factory.connect_setup(move |_, list_item| {
let label = gtk::Label::new(None); let label = gtk::Label::new(Some("some kind of text"));
list_item list_item
.downcast_ref::<gtk::ListItem>() .downcast_ref::<gtk::ListItem>()
.expect("Needs to be a ListItem") .expect("Needs to be a ListItem")
.set_child(Some(&label)); .set_child(Some(&label));
}); });
factory.connect_bind(move |_, list_item| { factory.connect_bind(move |_, list_item| {
let integer_object = list_item let game_object = list_item
.downcast_ref::<gtk::ListItem>() .downcast_ref::<gtk::ListItem>()
.expect("Needs to be ListItem") .expect("Needs to be ListItem")
.item() .item()
.and_downcast::<IntegerObject>() .and_downcast::<GameObject>()
.expect("The item has to be an IntegerObject."); .expect("The item has to be an IntegerObject.");
let label = list_item let label = list_item
@ -63,12 +66,25 @@ impl Default for GameDatabasePrivate {
.and_downcast::<gtk::Label>() .and_downcast::<gtk::Label>()
.expect("The child has to be an Label."); .expect("The child has to be an Label.");
label.set_label(&integer_object.number().to_string()); 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(),
);
}); });
let selection_model = gtk::SingleSelection::new(Some(model)); let selection_model = gtk::NoSelection::new(Some(model.clone()));
let list_view = gtk::ListView::new(Some(selection_model), Some(factory)); let list_view = gtk::ListView::new(Some(selection_model), Some(factory));
Self { list_view } Self { model, list_view }
} }
} }
@ -90,7 +106,17 @@ glib::wrapper! {
impl GameDatabase { impl GameDatabase {
pub fn new() -> Self { pub fn new() -> Self {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_width_request(200);
s.set_height_request(200);
s.append(&s.imp().list_view); s.append(&s.imp().list_view);
s s
} }
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);
}
} }

View File

@ -87,44 +87,6 @@ impl PlayerDataEntry {
} }
} }
pub struct DatabaseGamePrivate {
game: Rc<RefCell<GamePreviewElement>>,
}
impl Default for DatabaseGamePrivate {
fn default() -> Self {
Self {
game: Rc::new(RefCell::new(GamePreviewElement {
date: vec![],
black_player: "".to_owned(),
black_rank: None,
white_player: "".to_owned(),
white_rank: None,
})),
}
}
}
#[glib::object_subclass]
impl ObjectSubclass for DatabaseGamePrivate {
const NAME: &'static str = "DatabaseGame";
type Type = DatabaseGame;
}
impl ObjectImpl for DatabaseGamePrivate {}
glib::wrapper! {
pub struct DatabaseGame(ObjectSubclass<DatabaseGamePrivate>);
}
impl DatabaseGame {
pub fn new(preview: GamePreviewElement) -> Self {
let s: Self = Object::builder().build();
*s.imp().game.borrow_mut() = preview;
s
}
}
pub struct HomePrivate { pub struct HomePrivate {
black_player: Rc<RefCell<Option<PlayerDataEntry>>>, black_player: Rc<RefCell<Option<PlayerDataEntry>>>,
white_player: Rc<RefCell<Option<PlayerDataEntry>>>, white_player: Rc<RefCell<Option<PlayerDataEntry>>>,
@ -169,8 +131,15 @@ impl Home {
let new_game_button = gtk::Button::builder().label(&view.start_game.label).build(); let new_game_button = gtk::Button::builder().label(&view.start_game.label).build();
s.attach(&new_game_button, 2, 2, 1, 1); s.attach(&new_game_button, 2, 2, 1, 1);
let database = GameDatabase::new(); let library = GameDatabase::new();
s.attach(&database, 0, 3, 2, 2); let library_view = gtk::ScrolledWindow::builder()
.hscrollbar_policy(gtk::PolicyType::Never)
.min_content_width(360)
.child(&library)
.build();
s.attach(&library_view, 0, 3, 4, 2);
library.set_games(view.games);
new_game_button.connect_clicked({ new_game_button.connect_clicked({
move |_| { move |_| {