zoodex/src/ui/collatable_container/collated_grid.rs

222 lines
6.2 KiB
Rust
Raw Normal View History

use {
gtk4 :: {
* ,
2025-02-05 00:03:26 +01:00
Align :: * ,
Orientation :: * ,
2025-02-04 14:45:57 +01:00
gdk :: * ,
2025-02-05 13:51:22 +01:00
gio :: * ,
2025-02-04 14:45:57 +01:00
pango :: { * , Weight :: * } ,
prelude :: * ,
} ,
2025-02-05 13:51:22 +01:00
std :: { cell :: * , iter :: * , path :: { Path , PathBuf } } ,
} ;
use crate :: ui :: { collatable_container :: * , component :: * } ;
pub struct CollatedFilmsGrid {
film_widget_pairs : RefCell < Vec < ( FilmOverview , Box ) > > ,
grid_widget : FlowBox ,
}
pub struct CollatedSeriesGrid {
series_widget_pairs : RefCell < Vec < ( SeriesOverview , Box ) > > ,
grid_widget : FlowBox ,
}
impl CollatedFilmsGrid {
2025-02-05 13:51:22 +01:00
pub fn new () -> Self {
2025-01-31 17:38:52 +01:00
let grid_widget = flow_box ! (
@ orientation : Horizontal ;
@ homogeneous : true ;
@ selection_mode : SelectionMode :: None ;
2025-01-31 17:38:52 +01:00
) ;
let film_widget_pairs = RefCell :: new ( vec ! () ) ;
2025-02-05 13:51:22 +01:00
Self { film_widget_pairs , grid_widget }
}
2025-02-05 13:51:22 +01:00
pub async fn set_films ( & self , films : Vec <FilmOverview> , sorting : FilmsSorting ) {
let mut widgets = Vec :: new () ;
for film in films . as_slice () {
widgets . push ( create_film_entry (film) . await ) ;
}
* self . film_widget_pairs . borrow_mut () = zip ( films , widgets )
. collect () ;
2025-01-31 16:24:22 +01:00
for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) {
self . grid_widget . append ( & film_widget ) ;
}
}
2025-01-31 16:24:22 +01:00
pub fn set_sorting ( & self , sorting : FilmsSorting ) {
self . grid_widget . remove_all () ;
2025-01-31 16:24:22 +01:00
for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) {
self . grid_widget . append ( & film_widget ) ;
}
}
fn sort_film_widget_pairs ( & self , sorting : FilmsSorting ) -> Vec < ( FilmOverview , Box ) > {
let mut sorted = Vec :: from (
self . film_widget_pairs . borrow () . as_slice () ) ;
2025-01-31 16:24:22 +01:00
sorted . sort_by ( | ( film_1 , _ ) , ( film_2 , _ ) | match sorting . property {
FilmProperty :: Name =>
film_1 . name . cmp ( & film_2 . name ) ,
2025-01-31 16:24:22 +01:00
FilmProperty :: ReleaseDate =>
film_1 . release_date . cmp ( & film_2 . release_date ) ,
2025-01-31 16:24:22 +01:00
FilmProperty :: Runtime =>
film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) ,
} ) ;
2025-01-31 16:24:22 +01:00
if sorting . direction == SortingDirection :: Descending { sorted . reverse () }
2025-01-30 17:56:43 +01:00
// See it, say it, ...
sorted
}
}
impl CollatedSeriesGrid {
2025-02-05 13:51:22 +01:00
pub fn new () -> Self {
2025-01-31 17:38:52 +01:00
let grid_widget = flow_box ! (
@ orientation : Horizontal ;
@ homogeneous : true ;
@ selection_mode : SelectionMode :: None ;
2025-01-31 17:38:52 +01:00
) ;
let series_widget_pairs = RefCell :: new ( vec ! () ) ;
2025-02-05 13:51:22 +01:00
Self { series_widget_pairs , grid_widget }
}
2025-02-05 13:51:22 +01:00
pub async fn set_series ( & self , series : Vec <SeriesOverview> , sorting : SeriesSorting ) {
let mut widgets = Vec :: new () ;
for series in series . as_slice () {
widgets . push ( create_series_entry (series) . await ) ;
}
* self . series_widget_pairs . borrow_mut () = zip ( series , widgets )
. collect () ;
for ( _ , series_widget ) in self . sort_series_widget_pairs (sorting) {
self . grid_widget . append ( & series_widget ) ;
}
}
2025-01-31 16:24:22 +01:00
pub fn set_sorting ( & self , sorting : SeriesSorting ) {
self . grid_widget . remove_all () ;
for ( _ , series_widget ) in self . sort_series_widget_pairs (sorting) {
self . grid_widget . append ( & series_widget ) ;
}
}
fn sort_series_widget_pairs ( & self , sorting : SeriesSorting ) -> Vec < ( SeriesOverview , Box ) > {
let mut sorted = Vec :: from (
self . series_widget_pairs . borrow () . as_slice () ) ;
2025-01-31 16:24:22 +01:00
sorted . sort_by ( | ( series_1 , _ ) , ( series_2 , _ ) | match sorting . property {
2025-01-31 17:09:22 +01:00
SeriesProperty :: Name =>
series_1 . name . cmp ( & series_2 . name ) ,
SeriesProperty :: FirstReleaseDate =>
todo ! () ,
} ) ;
2025-01-31 17:09:22 +01:00
if sorting . direction == SortingDirection :: Descending { sorted . reverse () }
2025-01-31 17:09:22 +01:00
// See it, say it, ...
sorted
}
}
impl Component <FlowBox> for CollatedFilmsGrid {
fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
}
impl Component <FlowBox> for CollatedSeriesGrid {
fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
}
2025-02-05 13:51:22 +01:00
pub async fn create_film_entry ( film : & FilmOverview ) -> Box {
create_collection_item (
film . name . as_str () ,
film . original_name . as_deref () ,
film . poster_file_path . as_deref () ,
& create_film_details (film) ,
2025-02-05 13:51:22 +01:00
) . await
}
2025-02-05 13:51:22 +01:00
pub async fn create_series_entry ( series : & SeriesOverview ) -> Box {
create_collection_item (
series . name . as_str () ,
series . original_name . as_deref () ,
series . poster_file_path . as_deref () ,
& create_series_details (series) ,
2025-02-05 13:51:22 +01:00
) . await
}
2025-02-05 13:51:22 +01:00
async fn create_collection_item (
name : & str ,
original_name : Option < & str > ,
poster_file_path : Option < & Path > ,
details_widget : & Box ,
) -> Box {
let container = Box :: builder ()
. orientation (Vertical)
. margin_top (20)
. margin_bottom (20)
. build () ;
if let Some (poster_file_path) = poster_file_path {
2025-02-05 13:51:22 +01:00
let poster_file_path = PathBuf :: from (poster_file_path) ; // God forbid `Path` would work with `clone ! ()`
let poster_texture = spawn_blocking ( move ||
Texture :: from_filename (poster_file_path) . unwrap ()
) . await . unwrap () ;
container . append (
& Image :: builder ()
. paintable ( & poster_texture )
. width_request (300)
. height_request (300)
. margin_bottom (10)
. build ()
) ;
}
container . append (
& Label :: builder ()
2025-02-04 14:45:57 +01:00
. label (name)
. attributes ( & pango_attributes ! ( @ scale : SCALE_LARGE ; @ weight : Bold ; ) )
. justify ( Justification :: Center )
. wrap (true)
. max_width_chars (1) // Not the actual limit, used instead to wrap more aggressively
. build ()
) ;
if let Some (original_name) = original_name {
container . append (
& Label :: builder ()
. label (original_name)
. justify ( Justification :: Center )
. wrap (true)
. max_width_chars (1) // Not the actual limit, used instead to wrap more aggressively
. build ()
) ;
}
container . append (details_widget) ;
container
}
fn create_film_details ( film : & FilmOverview ) -> Box {
2025-01-31 17:38:52 +01:00
g_box ! (
@ orientation : Horizontal ;
@ halign : Center ;
@ spacing : 20 ;
2025-01-31 17:38:52 +01:00
label ! ( film . release_date . as_str () ) ,
label ! ( format ! ( "{}m" , film . runtime_minutes ) . as_str () ) ,
)
}
fn create_series_details ( series : & SeriesOverview ) -> Box {
2025-01-31 17:38:52 +01:00
g_box ! (
@ orientation : Horizontal ;
@ halign : Center ;
@ spacing : 20 ;
label ! ( series . first_release_date . as_str () ) ,
2025-01-31 17:38:52 +01:00
)
}