Compare commits

...

2 Commits

6 changed files with 81 additions and 84 deletions

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

@ -13,8 +13,8 @@ fn handle_response(api: CoreApi, window: gtk::ApplicationWindow, message: CoreRe
CoreResponse::HomeView(view) => perftrace("HomeView", || { CoreResponse::HomeView(view) => perftrace("HomeView", || {
let api = api.clone(); let api = api.clone();
let new_game = Home::new(api, view); let home = Home::new(api, view);
window.set_child(Some(&new_game)); window.set_child(Some(&home));
}), }),
CoreResponse::PlayingFieldView(view) => perftrace("PlayingFieldView", || { CoreResponse::PlayingFieldView(view) => perftrace("PlayingFieldView", || {
let api = api.clone(); let api = api.clone();

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

@ -1,14 +1,11 @@
use crate::{ui::GamePreview, CoreApi}; use crate::CoreApi;
use glib::{Object, Properties}; use glib::Object;
use gtk::{glib, prelude::*, subclass::prelude::*}; use gtk::{glib, prelude::*, subclass::prelude::*};
use kifu_core::{ use kifu_core::{
ui::{GamePreviewElement, HomeView, PlayerElement}, ui::{HomeView, PlayerElement},
CoreRequest, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest, CoreRequest, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest,
}; };
use std::{ use std::{cell::RefCell, rc::Rc};
cell::{Cell, RefCell},
rc::Rc,
};
use super::GameDatabase; use super::GameDatabase;
@ -87,44 +84,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>>>,
@ -143,34 +102,51 @@ impl Default for HomePrivate {
impl ObjectSubclass for HomePrivate { impl ObjectSubclass for HomePrivate {
const NAME: &'static str = "Home"; const NAME: &'static str = "Home";
type Type = Home; type Type = Home;
type ParentType = gtk::Grid; type ParentType = gtk::Box;
} }
impl ObjectImpl for HomePrivate {} impl ObjectImpl for HomePrivate {}
impl WidgetImpl for HomePrivate {} impl WidgetImpl for HomePrivate {}
impl GridImpl for HomePrivate {} impl BoxImpl for HomePrivate {}
glib::wrapper! { glib::wrapper! {
pub struct Home(ObjectSubclass<HomePrivate>) @extends gtk::Grid, gtk::Widget; pub struct Home(ObjectSubclass<HomePrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
} }
impl Home { impl Home {
pub fn new(api: CoreApi, view: HomeView) -> Home { pub fn new(api: CoreApi, view: HomeView) -> Home {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_spacing(4);
s.set_homogeneous(false);
s.set_orientation(gtk::Orientation::Vertical);
let players = gtk::Box::builder()
.spacing(4)
.orientation(gtk::Orientation::Horizontal)
.build();
s.append(&players);
let black_player = PlayerDataEntry::new(view.black_player); let black_player = PlayerDataEntry::new(view.black_player);
s.attach(&black_player, 1, 1, 1, 1); players.append(&black_player);
*s.imp().black_player.borrow_mut() = Some(black_player.clone()); *s.imp().black_player.borrow_mut() = Some(black_player.clone());
let white_player = PlayerDataEntry::new(view.white_player); let white_player = PlayerDataEntry::new(view.white_player);
s.attach(&white_player, 2, 1, 1, 1); players.append(&white_player);
*s.imp().white_player.borrow_mut() = Some(white_player.clone()); *s.imp().white_player.borrow_mut() = Some(white_player.clone());
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.append(&new_game_button);
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)
.vexpand(true)
.hexpand(true)
.child(&library)
.build();
s.append(&library_view);
library.set_games(view.games);
new_game_button.connect_clicked({ new_game_button.connect_clicked({
move |_| { move |_| {