Move collation components into submodules

This commit is contained in:
Reinout Meliesie 2025-01-31 11:38:06 +01:00
commit ebc8bc0d2f
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
7 changed files with 85 additions and 44 deletions

View file

@ -0,0 +1,226 @@
use {
gtk4 :: {
* ,
Align :: * ,
Orientation :: * ,
gdk :: Texture ,
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 <Film> , sorting : FilmsSortedBy , direction : SortingDirection ) -> Self {
let grid_widget = create_flow_box () ;
let film_widget_pairs = RefCell :: new ( vec ! () ) ;
let component = Self { film_widget_pairs , grid_widget } ;
component . set_films ( films , sorting , direction ) ;
component
}
pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSortedBy , direction : SortingDirection ) {
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 , direction ) {
self . grid_widget . append ( & film_widget ) ;
}
}
pub fn set_sorting ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) {
self . grid_widget . remove_all () ;
for ( _ , film_widget ) in self . sort_film_widget_pairs ( sorting , direction ) {
self . grid_widget . append ( & film_widget ) ;
}
}
fn sort_film_widget_pairs ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) -> Vec < ( Film , Box ) > {
let mut sorted = Vec :: from (
self . film_widget_pairs . borrow () . as_slice () ) ;
sorted . sort_by ( | ( film_1 , _ ) , ( film_2 , _ ) | match sorting {
FilmsSortedBy :: Name =>
film_1 . name . cmp ( & film_2 . name ) ,
FilmsSortedBy :: ReleaseDate =>
film_1 . release_date . cmp ( & film_2 . release_date ) ,
FilmsSortedBy :: Runtime =>
film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) ,
} ) ;
if direction == SortingDirection :: Descending { sorted . reverse () }
// See it, say it, ...
sorted
}
}
impl CollatedSeriesGrid {
pub fn new ( series : Vec <Series> , sorting : SeriesSortedBy ) -> Self {
let grid_widget = create_flow_box () ;
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 <Series> , sorting : SeriesSortedBy ) {
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 : SeriesSortedBy ) {
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 : SeriesSortedBy ) -> Vec < ( Series , Box ) > {
let mut sorted = Vec :: from (
self . series_widget_pairs . borrow () . as_slice () ) ;
sorted . sort_by ( | ( series_1 , _ ) , ( series_2 , _ ) | match sorting {
SeriesSortedBy :: Name => series_1 . name . cmp ( & series_2 . name ) ,
SeriesSortedBy :: FirstReleaseDate => todo ! () ,
} ) ;
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 }
}
fn create_flow_box () -> FlowBox {
FlowBox :: builder ()
. orientation (Horizontal)
. homogeneous (true)
. selection_mode ( SelectionMode :: None )
. build ()
}
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 ( format ! ( "<span size='large' weight='bold'>{}</span>" , name ) )
. use_markup (true)
. 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 {
let container = Box :: builder ()
. orientation (Horizontal)
. halign (Center)
. spacing (20)
. build () ;
container . append (
& Label :: builder () . label ( & film . release_date ) . build ()
) ;
container . append (
& Label :: builder () . label ( format ! ( "{}m" , film . runtime_minutes ) ) . build ()
) ;
container
}
fn create_series_details ( series : & Series ) -> Box {
let container = Box :: builder ()
. orientation (Horizontal)
. halign (Center)
. spacing (20)
. build () ;
// TODO
container
}