2023-08-25 04:07:29 +00:00
|
|
|
use adw::{prelude::*, subclass::prelude::*};
|
2023-08-20 03:24:01 +00:00
|
|
|
use glib::Object;
|
2023-10-05 16:19:57 +00:00
|
|
|
use gtk::glib;
|
2024-03-22 02:39:28 +00:00
|
|
|
// use kifu_core::ui::GamePreviewElement;
|
2024-03-22 03:30:24 +00:00
|
|
|
use sgf::Game;
|
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 {
|
2024-03-22 03:30:24 +00:00
|
|
|
game: Rc<RefCell<Option<Game>>>,
|
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 {
|
2024-03-22 03:30:24 +00:00
|
|
|
pub fn new(game: Game) -> 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
|
|
|
|
}
|
|
|
|
|
2024-03-22 03:30:24 +00:00
|
|
|
pub fn game(&self) -> Option<Game> {
|
2023-08-20 03:24:01 +00:00
|
|
|
self.imp().game.borrow().clone()
|
2023-08-20 00:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 16:40:46 +00:00
|
|
|
pub struct LibraryPrivate {
|
2023-08-20 03:24:01 +00:00
|
|
|
model: gio::ListStore,
|
2023-08-25 04:07:29 +00:00
|
|
|
list_view: gtk::ColumnView,
|
2023-08-20 00:46:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-20 16:40:46 +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-11-14 15:05:56 +00:00
|
|
|
let model = gio::ListStore::new::<GameObject>();
|
2023-08-20 00:46:43 +00:00
|
|
|
model.extend_from_slice(&vector);
|
2023-08-25 04:07:29 +00:00
|
|
|
|
|
|
|
/*
|
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| {
|
2023-08-20 16:31:44 +00:00
|
|
|
let preview = GamePreview::new();
|
2023-08-20 00:46:43 +00:00
|
|
|
list_item
|
|
|
|
.downcast_ref::<gtk::ListItem>()
|
|
|
|
.expect("Needs to be a ListItem")
|
2023-08-20 16:31:44 +00:00
|
|
|
.set_child(Some(&preview));
|
2023-08-20 00:46:43 +00:00
|
|
|
});
|
|
|
|
factory.connect_bind(move |_, list_item| {
|
2023-08-20 16:31:44 +00:00
|
|
|
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>()
|
2023-08-20 16:31:44 +00:00
|
|
|
.expect("The item has to be a GameObject.");
|
2023-08-20 00:46:43 +00:00
|
|
|
|
2023-08-20 16:31:44 +00:00
|
|
|
let preview = list_item
|
2023-08-20 00:46:43 +00:00
|
|
|
.downcast_ref::<gtk::ListItem>()
|
|
|
|
.expect("Needs to be ListItem")
|
|
|
|
.child()
|
2023-08-20 16:31:44 +00:00
|
|
|
.and_downcast::<GamePreview>()
|
|
|
|
.expect("The child has to be a GamePreview object.");
|
2023-08-20 00:46:43 +00:00
|
|
|
|
2023-08-20 16:31:44 +00:00
|
|
|
match game_element.game() {
|
|
|
|
Some(game) => preview.set_game(game),
|
|
|
|
None => (),
|
|
|
|
};
|
2023-08-20 00:46:43 +00:00
|
|
|
});
|
2023-08-25 04:07:29 +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()));
|
2024-03-22 02:39:28 +00:00
|
|
|
let list_view = gtk::ColumnView::builder()
|
|
|
|
.model(&selection_model)
|
|
|
|
.hexpand(true)
|
|
|
|
.build();
|
2023-08-25 04:07:29 +00:00
|
|
|
|
|
|
|
fn make_factory<F>(bind: F) -> gtk::SignalListItemFactory
|
|
|
|
where
|
2024-03-22 03:30:24 +00:00
|
|
|
F: Fn(Game) -> String + 'static,
|
2023-08-25 04:07:29 +00:00
|
|
|
{
|
|
|
|
let factory = gtk::SignalListItemFactory::new();
|
|
|
|
factory.connect_setup(|_, list_item| {
|
|
|
|
list_item
|
|
|
|
.downcast_ref::<gtk::ListItem>()
|
|
|
|
.unwrap()
|
|
|
|
.set_child(Some(
|
|
|
|
>k::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
2023-10-05 16:19:57 +00:00
|
|
|
.ellipsize(gtk::pango::EllipsizeMode::End)
|
2023-08-25 04:07:29 +00:00
|
|
|
.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();
|
2023-10-05 16:19:57 +00:00
|
|
|
if let Some(game) = game.game() {
|
|
|
|
preview.set_text(&bind(game))
|
|
|
|
}
|
2023-08-25 04:07:29 +00:00
|
|
|
});
|
|
|
|
factory
|
|
|
|
}
|
|
|
|
|
2024-03-22 02:39:28 +00:00
|
|
|
list_view.append_column(
|
|
|
|
>k::ColumnViewColumn::builder()
|
|
|
|
.title("date")
|
|
|
|
.factory(&make_factory(|g| {
|
2024-03-22 03:30:24 +00:00
|
|
|
g.dates
|
2024-03-22 02:39:28 +00:00
|
|
|
.iter()
|
|
|
|
.map(|date| {
|
|
|
|
format!("{}", date)
|
|
|
|
/*
|
|
|
|
let l = locale!("en-US").into();
|
|
|
|
let options = length::Bag::from_date_style(length::Date::Medium);
|
|
|
|
let date = Date::try_new_iso_date(date.
|
|
|
|
let dtfmt =
|
|
|
|
DateFormatter::try_new_with_length(&l, options).unwrap();
|
|
|
|
dtfmt.format(date).unwrap()
|
|
|
|
*/
|
|
|
|
})
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
}))
|
|
|
|
.expand(true)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
list_view.append_column(
|
|
|
|
>k::ColumnViewColumn::builder()
|
|
|
|
.title("game")
|
|
|
|
.factory(&make_factory(|g| {
|
|
|
|
g.game_name.unwrap_or("Unnamed".to_owned())
|
|
|
|
}))
|
|
|
|
.expand(true)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
list_view.append_column(
|
|
|
|
>k::ColumnViewColumn::builder()
|
|
|
|
.title("black")
|
|
|
|
.factory(&make_factory(|g| {
|
2024-03-22 03:30:24 +00:00
|
|
|
g.black_player.name.unwrap_or("Black".to_owned())
|
2024-03-22 02:39:28 +00:00
|
|
|
}))
|
|
|
|
.expand(true)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
list_view.append_column(
|
|
|
|
>k::ColumnViewColumn::builder()
|
|
|
|
.title("white")
|
|
|
|
.factory(&make_factory(|g| {
|
2024-03-22 03:30:24 +00:00
|
|
|
g.white_player.name.unwrap_or("White".to_owned())
|
2024-03-22 02:39:28 +00:00
|
|
|
}))
|
|
|
|
.expand(true)
|
|
|
|
.build(),
|
|
|
|
);
|
2023-08-25 04:07:29 +00:00
|
|
|
list_view.append_column(>k::ColumnViewColumn::new(
|
|
|
|
Some("result"),
|
2024-03-22 02:39:28 +00:00
|
|
|
Some(make_factory(|g| {
|
|
|
|
g.result.map(|d| format!("{}", d)).unwrap_or("".to_owned())
|
|
|
|
})),
|
2023-08-25 04:07:29 +00:00
|
|
|
));
|
|
|
|
|
2023-08-20 03:24:01 +00:00
|
|
|
Self { model, list_view }
|
2023-08-20 00:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
2023-08-20 16:40:46 +00:00
|
|
|
impl ObjectSubclass for LibraryPrivate {
|
|
|
|
const NAME: &'static str = "Library";
|
|
|
|
type Type = Library;
|
2023-08-25 04:07:29 +00:00
|
|
|
type ParentType = adw::Bin;
|
2023-08-20 00:46:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-20 16:40:46 +00:00
|
|
|
impl ObjectImpl for LibraryPrivate {}
|
|
|
|
impl WidgetImpl for LibraryPrivate {}
|
2023-08-25 04:07:29 +00:00
|
|
|
impl BinImpl for LibraryPrivate {}
|
2023-08-20 00:46:43 +00:00
|
|
|
|
|
|
|
glib::wrapper! {
|
2023-08-25 04:07:29 +00:00
|
|
|
pub struct Library(ObjectSubclass<LibraryPrivate>) @extends adw::Bin, gtk::Widget;
|
2023-08-20 00:46:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 16:19:57 +00:00
|
|
|
impl Default for Library {
|
|
|
|
fn default() -> Self {
|
2023-08-20 00:46:43 +00:00
|
|
|
let s: Self = Object::builder().build();
|
2023-08-25 04:07:29 +00:00
|
|
|
|
|
|
|
s.set_child(Some(&s.imp().list_view));
|
2023-08-20 00:46:43 +00:00
|
|
|
s
|
|
|
|
}
|
2023-10-05 16:19:57 +00:00
|
|
|
}
|
2023-08-20 03:24:01 +00:00
|
|
|
|
2023-10-05 16:19:57 +00:00
|
|
|
impl Library {
|
2024-03-22 03:30:24 +00:00
|
|
|
pub fn set_games(&self, games: Vec<Game>) {
|
2023-08-20 03:24:01 +00:00
|
|
|
let games = games
|
|
|
|
.into_iter()
|
2023-10-05 16:19:57 +00:00
|
|
|
.map(GameObject::new)
|
2023-08-20 03:24:01 +00:00
|
|
|
.collect::<Vec<GameObject>>();
|
|
|
|
self.imp().model.extend_from_slice(&games);
|
|
|
|
}
|
2023-08-20 00:46:43 +00:00
|
|
|
}
|