From e3f4ca246dc30988850795757dc1cf0c19a3884f Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 19 Aug 2023 23:24:01 -0400 Subject: [PATCH] Create the list of games --- Cargo.lock | 2 +- kifu/core/src/api.rs | 6 --- kifu/gtk/Cargo.toml | 3 +- kifu/gtk/src/ui/game_database.rs | 66 ++++++++++++++++++++++---------- kifu/gtk/src/ui/home.rs | 49 +++++------------------- 5 files changed, 58 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 23c8ad3..de43e70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1348,7 +1348,7 @@ dependencies = [ "gtk4", "image", "kifu-core", - "screenplay", + "sgf", "tokio", ] diff --git a/kifu/core/src/api.rs b/kifu/core/src/api.rs index 4387187..fead3e7 100644 --- a/kifu/core/src/api.rs +++ b/kifu/core/src/api.rs @@ -69,17 +69,11 @@ pub struct CoreApp { impl CoreApp { 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"); - println!("config: {:?}", config); let db_path: DatabasePath = config.get().unwrap(); - println!("db_path: {:?}", 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 } } diff --git a/kifu/gtk/Cargo.toml b/kifu/gtk/Cargo.toml index d58e07f..21918ab 100644 --- a/kifu/gtk/Cargo.toml +++ b/kifu/gtk/Cargo.toml @@ -16,7 +16,8 @@ gtk = { version = "0.6", package = "gtk4", features = ["v4_8"] } image = { version = "0.24" } kifu-core = { path = "../core" } tokio = { version = "1.26", features = [ "full" ] } -screenplay = { path = "../../screenplay" } +# screenplay = { path = "../../screenplay" } +sgf = { path = "../../sgf" } [build-dependencies] glib-build-tools = "0.17" diff --git a/kifu/gtk/src/ui/game_database.rs b/kifu/gtk/src/ui/game_database.rs index 0113a70..6c72c31 100644 --- a/kifu/gtk/src/ui/game_database.rs +++ b/kifu/gtk/src/ui/game_database.rs @@ -1,59 +1,62 @@ -use glib::{Object, Properties}; +use glib::Object; use gtk::{glib, prelude::*, subclass::prelude::*}; +use kifu_core::ui::GamePreviewElement; use std::{cell::RefCell, rc::Rc}; #[derive(Default)] -pub struct IntegerObjectPrivate { - number: Rc>, +pub struct GameObjectPrivate { + game: Rc>>, } #[glib::object_subclass] -impl ObjectSubclass for IntegerObjectPrivate { - const NAME: &'static str = "IntegerObject"; - type Type = IntegerObject; +impl ObjectSubclass for GameObjectPrivate { + const NAME: &'static str = "GameObject"; + type Type = GameObject; } -impl ObjectImpl for IntegerObjectPrivate {} +impl ObjectImpl for GameObjectPrivate {} glib::wrapper! { - pub struct IntegerObject(ObjectSubclass); + pub struct GameObject(ObjectSubclass); } -impl IntegerObject { - pub fn new(number: i32) -> Self { +impl GameObject { + pub fn new(game: GamePreviewElement) -> Self { let s: Self = Object::builder().build(); - *s.imp().number.borrow_mut() = number; + *s.imp().game.borrow_mut() = Some(game); s } - pub fn number(&self) -> i32 { - self.imp().number.borrow().clone() + pub fn game(&self) -> Option { + self.imp().game.borrow().clone() } } pub struct GameDatabasePrivate { + model: gio::ListStore, list_view: gtk::ListView, } impl Default for GameDatabasePrivate { fn default() -> Self { - let vector: Vec = (0..=500).map(IntegerObject::new).collect(); + 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 label = gtk::Label::new(None); + let label = gtk::Label::new(Some("some kind of text")); list_item .downcast_ref::() .expect("Needs to be a ListItem") .set_child(Some(&label)); }); factory.connect_bind(move |_, list_item| { - let integer_object = list_item + let game_object = list_item .downcast_ref::() .expect("Needs to be ListItem") .item() - .and_downcast::() + .and_downcast::() .expect("The item has to be an IntegerObject."); let label = list_item @@ -63,12 +66,25 @@ impl Default for GameDatabasePrivate { .and_downcast::() .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)); - Self { list_view } + Self { model, list_view } } } @@ -90,7 +106,17 @@ glib::wrapper! { impl GameDatabase { pub fn new() -> Self { let s: Self = Object::builder().build(); + s.set_width_request(200); + s.set_height_request(200); 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); + } } diff --git a/kifu/gtk/src/ui/home.rs b/kifu/gtk/src/ui/home.rs index fd71d9a..533821b 100644 --- a/kifu/gtk/src/ui/home.rs +++ b/kifu/gtk/src/ui/home.rs @@ -87,44 +87,6 @@ impl PlayerDataEntry { } } -pub struct DatabaseGamePrivate { - game: Rc>, -} - -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); -} - -impl DatabaseGame { - pub fn new(preview: GamePreviewElement) -> Self { - let s: Self = Object::builder().build(); - *s.imp().game.borrow_mut() = preview; - s - } -} - pub struct HomePrivate { black_player: Rc>>, white_player: Rc>>, @@ -169,8 +131,15 @@ impl Home { let new_game_button = gtk::Button::builder().label(&view.start_game.label).build(); s.attach(&new_game_button, 2, 2, 1, 1); - let database = GameDatabase::new(); - s.attach(&database, 0, 3, 2, 2); + let library = GameDatabase::new(); + 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({ move |_| {