zoodex/src/ui/collatable_container/collated_grid.rs

225 lines
6.6 KiB
Rust
Raw Normal View History

2025-02-05 14:57:07 +01:00
use gtk4 :: * ;
use gtk4 :: Align :: * ;
use gtk4 :: Orientation :: * ;
use gtk4 :: gdk :: * ;
use gtk4 :: gio :: * ;
use gtk4 :: pango :: * ;
use gtk4 :: pango :: Weight :: * ;
use gtk4 :: prelude :: * ;
use std :: cell :: * ;
use std :: iter :: * ;
use std :: path :: Path ;
use std :: path :: PathBuf ;
use crate :: ui :: collatable_container :: * ;
use crate :: ui :: component :: * ;
pub struct CollatedFilmsGrid {
2025-02-07 16:22:34 +01:00
film_widget_pairs : RefCell < Vec < ( FilmOverview , Button ) > > ,
grid_widget : FlowBox ,
}
pub struct CollatedSeriesGrid {
2025-02-07 16:22:34 +01:00
series_widget_pairs : RefCell < Vec < ( SeriesOverview , Button ) > > ,
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 ) ;
}
2025-02-07 16:22:34 +01:00
self . film_widget_pairs . replace ( 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 ) ;
}
}
2025-02-07 16:22:34 +01:00
fn sort_film_widget_pairs ( & self , sorting : FilmsSorting ) -> Vec < ( FilmOverview , Button ) > {
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 ) ;
}
2025-02-07 16:22:34 +01:00
self . series_widget_pairs . replace ( 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 ) ;
}
}
2025-02-07 16:22:34 +01:00
fn sort_series_widget_pairs ( & self , sorting : SeriesSorting ) -> Vec < ( SeriesOverview , Button ) > {
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 =>
series_1 . first_release_date . cmp ( & series_2 . first_release_date ) ,
} ) ;
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-07 16:22:34 +01:00
async fn create_film_entry ( film : & FilmOverview ) -> Button {
button ! (
@ css_classes : & [ "flat" , "open-collection-item-button" ] ;
@ connect_clicked : |_| todo ! () ;
& create_collection_item (
film . name . as_str () ,
film . original_name . as_deref () ,
film . poster_file_path . as_deref () ,
& create_film_details (film) ,
) . await ,
)
}
2025-02-07 16:22:34 +01:00
async fn create_series_entry ( series : & SeriesOverview ) -> Button {
button ! (
@ css_classes : & [ "flat" , "open-collection-item-button" ] ;
@ connect_clicked : |_| todo ! () ;
& create_collection_item (
series . name . as_str () ,
series . original_name . as_deref () ,
series . poster_file_path . as_deref () ,
& create_series_details (series) ,
) . 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 {
g_box ! (
@ option_children ;
@ orientation : Vertical ;
@ margin_top : 20 ;
@ margin_bottom : 20 ;
match poster_file_path {
Some (poster_file_path) => {
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 () ;
Some ( image ! (
@ paintable : & poster_texture ;
@ width_request : 300 ;
@ height_request : 300 ;
@ margin_bottom : 10 ;
) )
} ,
None => None ,
} . as_ref () ,
Some ( & label ! (
@ justify : Justification :: Center ;
@ wrap : true ;
@ max_width_chars : 1 ; // Not the actual limit, used instead to wrap more aggressively
@ attributes : & pango_attributes ! ( @ scale : SCALE_LARGE ; @ weight : Bold ; ) ;
name ,
) ) ,
match original_name {
Some (original_name) => Some ( label ! (
@ justify : Justification :: Center ;
@ wrap : true ;
@ max_width_chars : 1 ; // Not the actual limit, used instead to wrap more aggressively
original_name ,
) ) ,
None => None ,
} . as_ref () ,
Some (details_widget) ,
)
}
fn create_film_details ( film : & FilmOverview ) -> Box {
2025-01-31 17:38:52 +01:00
g_box ! (
@ orientation : Horizontal ;
@ halign : Center ;
@ spacing : 20 ;
& label ! ( film . release_date . as_str () ) ,
& label ! ( format ! ( "{}m" , film . runtime_minutes ) . as_str () ) ,
2025-01-31 17:38:52 +01:00
)
}
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
)
}