use { gtk4 :: { * , Align :: * , Orientation :: * , gdk :: * , pango :: { * , Weight :: * } , prelude :: * , } , std :: { cell :: * , iter :: * , path :: Path } , } ; use crate :: { collection :: * , ui :: { collatable_container :: * , component :: * } } ; pub struct CollatedFilmsGrid { film_widget_pairs : RefCell < Vec < ( Film , Box ) > > , grid_widget : FlowBox , } pub struct CollatedSeriesGrid { series_widget_pairs : RefCell < Vec < ( Series , Box ) > > , grid_widget : FlowBox , } impl CollatedFilmsGrid { pub fn new ( films : Vec , sorting : FilmsSorting ) -> Self { let grid_widget = flow_box ! ( @ orientation : Horizontal ; @ homogeneous : true ; @ selection_mode : SelectionMode :: None ; ) ; let film_widget_pairs = RefCell :: new ( vec ! () ) ; let component = Self { film_widget_pairs , grid_widget } ; component . set_films ( films , sorting ) ; component } pub fn set_films ( & self , films : Vec , sorting : FilmsSorting ) { let widgets = films . iter () . map (create_film_entry) . collect :: < Vec <_> > () ; * self . film_widget_pairs . borrow_mut () = zip ( films , widgets ) . collect () ; for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) { self . grid_widget . append ( & film_widget ) ; } } pub fn set_sorting ( & self , sorting : FilmsSorting ) { self . grid_widget . remove_all () ; 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 < ( Film , Box ) > { let mut sorted = Vec :: from ( self . film_widget_pairs . borrow () . as_slice () ) ; sorted . sort_by ( | ( film_1 , _ ) , ( film_2 , _ ) | match sorting . property { FilmProperty :: Name => film_1 . name . cmp ( & film_2 . name ) , FilmProperty :: ReleaseDate => film_1 . release_date . cmp ( & film_2 . release_date ) , FilmProperty :: Runtime => film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) , } ) ; if sorting . direction == SortingDirection :: Descending { sorted . reverse () } // See it, say it, ... sorted } } impl CollatedSeriesGrid { pub fn new ( series : Vec , sorting : SeriesSorting ) -> Self { let grid_widget = flow_box ! ( @ orientation : Horizontal ; @ homogeneous : true ; @ selection_mode : SelectionMode :: None ; ) ; let series_widget_pairs = RefCell :: new ( vec ! () ) ; let component = Self { series_widget_pairs , grid_widget } ; component . set_series ( series, sorting ) ; component } pub fn set_series ( & self , series : Vec , sorting : SeriesSorting ) { let widgets = series . iter () . map (create_series_entry) . collect :: < Vec <_> > () ; * 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 ) ; } } 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 < ( Series , Box ) > { let mut sorted = Vec :: from ( self . series_widget_pairs . borrow () . as_slice () ) ; sorted . sort_by ( | ( series_1 , _ ) , ( series_2 , _ ) | match sorting . property { SeriesProperty :: Name => series_1 . name . cmp ( & series_2 . name ) , SeriesProperty :: FirstReleaseDate => todo ! () , } ) ; if sorting . direction == SortingDirection :: Descending { sorted . reverse () } // See it, say it, ... sorted } } impl Component for CollatedFilmsGrid { fn get_widget ( & self ) -> & FlowBox { & self . grid_widget } } impl Component for CollatedSeriesGrid { fn get_widget ( & self ) -> & FlowBox { & self . grid_widget } } pub fn create_film_entry ( film : & Film ) -> Box { create_collection_item ( film . name . as_str () , film . original_name . as_deref () , film . poster_file_path . as_deref () , & create_film_details (film) , ) } pub fn create_series_entry ( series : & Series ) -> Box { create_collection_item ( series . name . as_str () , series . original_name . as_deref () , series . poster_file_path . as_deref () , & create_series_details (series) , ) } 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 { let poster_texture = Texture :: from_filename (poster_file_path) . unwrap () ; container . append ( & Image :: builder () . paintable ( & poster_texture ) . width_request (300) . height_request (300) . margin_bottom (10) . build () ) ; } container . append ( & Label :: builder () . 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 : & Film ) -> Box { g_box ! ( @ orientation : Horizontal ; @ halign : Center ; @ spacing : 20 ; label ! ( film . release_date . as_str () ) , label ! ( format ! ( "{}m" , film . runtime_minutes ) . as_str () ) , ) } fn create_series_details ( series : & Series ) -> Box { g_box ! ( @ orientation : Horizontal ; @ halign : Center ; @ spacing : 20 ; // TODO label ! ("????") , ) }