2024-11-26 17:20:53 +01:00
|
|
|
use {
|
|
|
|
|
gtk4 :: {
|
|
|
|
|
* ,
|
|
|
|
|
Align :: * ,
|
|
|
|
|
Orientation :: * ,
|
|
|
|
|
gdk :: Texture ,
|
|
|
|
|
prelude :: * ,
|
|
|
|
|
} ,
|
2024-11-27 15:22:55 +01:00
|
|
|
std :: { cell :: * , iter :: * , path :: Path } ,
|
2024-11-26 17:20:53 +01:00
|
|
|
} ;
|
|
|
|
|
|
2024-11-27 15:22:55 +01:00
|
|
|
use crate :: { collection :: * , ui :: component :: * } ;
|
2024-11-26 17:20:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
pub enum FilmsSortedBy { Name , ReleaseDate , Runtime }
|
|
|
|
|
pub enum SeriesSortedBy { Name , FirstReleaseDate }
|
2024-11-26 17:20:53 +01:00
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
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 ,
|
|
|
|
|
}
|
2024-11-26 17:20:53 +01:00
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
impl CollatedFilmsGrid {
|
|
|
|
|
pub fn new ( films : Vec <Film> , sorting : FilmsSortedBy ) -> Self {
|
|
|
|
|
let grid_widget = create_flow_box () ;
|
|
|
|
|
let film_widgets = films . iter ()
|
|
|
|
|
. map (create_film_entry)
|
|
|
|
|
. collect :: < Vec <_> > () ;
|
|
|
|
|
let film_widget_pairs = RefCell :: new ( zip ( films , film_widgets )
|
|
|
|
|
. collect :: < Vec <_> > () ) ;
|
|
|
|
|
|
|
|
|
|
let film_widget_pairs_sorted = sort_film_widget_pairs (
|
|
|
|
|
film_widget_pairs . borrow () . as_slice () , sorting ) ;
|
|
|
|
|
for ( _ , film_widget ) in & film_widget_pairs_sorted {
|
|
|
|
|
grid_widget . append (film_widget) ;
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
Self { film_widget_pairs , grid_widget }
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSortedBy ) {
|
|
|
|
|
let film_widgets = films . iter ()
|
|
|
|
|
. map (create_film_entry)
|
|
|
|
|
. collect :: < Vec <_> > () ;
|
|
|
|
|
* self . film_widget_pairs . borrow_mut () = zip ( films , film_widgets )
|
|
|
|
|
. collect :: < Vec <_> > () ;
|
|
|
|
|
|
|
|
|
|
let film_widget_pairs_sorted = sort_film_widget_pairs (
|
|
|
|
|
self . film_widget_pairs . borrow () . as_slice () , sorting ) ;
|
|
|
|
|
for ( _ , film_widget ) in & film_widget_pairs_sorted {
|
|
|
|
|
self . grid_widget . append (film_widget) ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_sorting ( & self , sorting : FilmsSortedBy ) {
|
|
|
|
|
self . grid_widget . remove_all () ;
|
2024-11-26 17:20:53 +01:00
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
let film_widget_pairs_sorted = sort_film_widget_pairs (
|
|
|
|
|
self . film_widget_pairs . borrow () . as_slice () , sorting ) ;
|
|
|
|
|
for ( _ , film_widget ) in & film_widget_pairs_sorted {
|
|
|
|
|
self . grid_widget . append (film_widget) ;
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl CollatedSeriesGrid {
|
2024-11-27 11:47:20 +01:00
|
|
|
pub fn new ( series : Vec <Series> , sorting : SeriesSortedBy ) -> Self {
|
|
|
|
|
let grid_widget = create_flow_box () ;
|
|
|
|
|
let series_widgets = series . iter ()
|
|
|
|
|
. map (create_series_entry)
|
|
|
|
|
. collect :: < Vec <_> > () ;
|
|
|
|
|
let series_widget_pairs = RefCell :: new ( zip ( series , series_widgets )
|
|
|
|
|
. collect :: < Vec <_> > () ) ;
|
|
|
|
|
|
|
|
|
|
let series_widget_pairs_sorted = sort_series_widget_pairs (
|
|
|
|
|
series_widget_pairs . borrow () . as_slice () , sorting ) ;
|
|
|
|
|
for ( _ , series_widget ) in & series_widget_pairs_sorted {
|
|
|
|
|
grid_widget . append (series_widget) ;
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
Self { series_widget_pairs , grid_widget }
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
pub fn set_series ( & self , series : Vec <Series> , sorting : SeriesSortedBy ) {
|
|
|
|
|
let series_widgets = series . iter ()
|
|
|
|
|
. map (create_series_entry)
|
|
|
|
|
. collect :: < Vec <_> > () ;
|
|
|
|
|
* self . series_widget_pairs . borrow_mut () = zip ( series , series_widgets )
|
|
|
|
|
. collect :: < Vec <_> > () ;
|
|
|
|
|
|
|
|
|
|
let series_widget_pairs_sorted = sort_series_widget_pairs (
|
|
|
|
|
self . series_widget_pairs . borrow () . as_slice () , sorting ) ;
|
|
|
|
|
for ( _ , series_widget ) in & series_widget_pairs_sorted {
|
|
|
|
|
self . grid_widget . append (series_widget) ;
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
pub fn set_sorting ( & self , sorting : SeriesSortedBy ) {
|
|
|
|
|
self . grid_widget . remove_all () ;
|
|
|
|
|
|
|
|
|
|
let series_widget_pairs_sorted = sort_series_widget_pairs (
|
|
|
|
|
self . series_widget_pairs . borrow () . as_slice () , sorting ) ;
|
|
|
|
|
for ( _ , series_widget ) in & series_widget_pairs_sorted {
|
|
|
|
|
self . grid_widget . append (series_widget) ;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-27 15:22:55 +01:00
|
|
|
}
|
2024-11-27 11:47:20 +01:00
|
|
|
|
2024-11-27 15:22:55 +01:00
|
|
|
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 }
|
2024-11-27 11:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sort_film_widget_pairs (
|
|
|
|
|
film_widget_pairs : & [ ( Film , Box ) ] ,
|
|
|
|
|
sorting : FilmsSortedBy ,
|
|
|
|
|
) -> Vec < ( Film , Box ) > {
|
|
|
|
|
let mut sorted = Vec :: from (film_widget_pairs) ;
|
|
|
|
|
|
|
|
|
|
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 ) ,
|
|
|
|
|
} ) ;
|
|
|
|
|
|
|
|
|
|
sorted
|
|
|
|
|
}
|
|
|
|
|
fn sort_series_widget_pairs (
|
|
|
|
|
series_widget_pairs : & [ ( Series, Box ) ] ,
|
|
|
|
|
sorting : SeriesSortedBy ,
|
|
|
|
|
) -> Vec < ( Series , Box ) > {
|
|
|
|
|
let mut sorted = Vec :: from (series_widget_pairs) ;
|
|
|
|
|
|
|
|
|
|
sorted . sort_by ( | ( series_1 , _ ) , ( series_2 , _ ) | match sorting {
|
|
|
|
|
SeriesSortedBy :: Name => series_1 . name . cmp ( & series_2 . name ) ,
|
|
|
|
|
SeriesSortedBy :: FirstReleaseDate => todo ! () ,
|
|
|
|
|
} ) ;
|
|
|
|
|
|
|
|
|
|
sorted
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
fn create_flow_box () -> FlowBox {
|
2024-11-26 17:20:53 +01:00
|
|
|
FlowBox :: builder ()
|
|
|
|
|
. orientation (Horizontal)
|
|
|
|
|
. homogeneous (true)
|
|
|
|
|
. selection_mode ( SelectionMode :: None )
|
|
|
|
|
. build ()
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
pub fn create_film_entry ( film : & Film ) -> Box {
|
2024-11-26 17:20:53 +01:00
|
|
|
create_collection_item (
|
|
|
|
|
film . name . as_str () ,
|
|
|
|
|
film . original_name . as_deref () ,
|
|
|
|
|
film . poster_file_path . as_deref () ,
|
|
|
|
|
& create_film_details (film) ,
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-11-27 11:47:20 +01:00
|
|
|
pub fn create_series_entry ( series : & Series ) -> Box {
|
2024-11-26 17:20:53 +01:00
|
|
|
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 > ,
|
2024-11-27 11:47:20 +01:00
|
|
|
details_widget : & Box ,
|
|
|
|
|
) -> Box {
|
|
|
|
|
let container = Box :: builder ()
|
2024-11-26 17:20:53 +01:00
|
|
|
. 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
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
fn create_film_details ( film : & Film ) -> Box {
|
|
|
|
|
let container = Box :: builder ()
|
2024-11-26 17:20:53 +01:00
|
|
|
. 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
|
|
|
|
|
}
|
2024-11-27 11:47:20 +01:00
|
|
|
fn create_series_details ( series : & Series ) -> Box {
|
|
|
|
|
let container = Box :: builder ()
|
2024-11-26 17:20:53 +01:00
|
|
|
. orientation (Horizontal)
|
|
|
|
|
. halign (Center)
|
|
|
|
|
. spacing (20)
|
|
|
|
|
. build () ;
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
|
|
container
|
|
|
|
|
}
|