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 :: * ;
|
2024-11-26 17:20:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
pub struct CollatedFilmsGrid {
|
2025-02-07 16:22:34 +01:00
|
|
|
film_widget_pairs : RefCell < Vec < ( FilmOverview , Button ) > > ,
|
2024-11-27 11:47:20 +01:00
|
|
|
grid_widget : FlowBox ,
|
|
|
|
|
}
|
|
|
|
|
pub struct CollatedSeriesGrid {
|
2025-02-07 16:22:34 +01:00
|
|
|
series_widget_pairs : RefCell < Vec < ( SeriesOverview , Button ) > > ,
|
2024-11-27 11:47:20 +01:00
|
|
|
grid_widget : FlowBox ,
|
|
|
|
|
}
|
2024-11-26 17:20:53 +01:00
|
|
|
|
2024-11-27 11:47:20 +01:00
|
|
|
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 ! (
|
2025-02-04 15:02:58 +01:00
|
|
|
@ orientation : Horizontal ;
|
|
|
|
|
@ homogeneous : true ;
|
|
|
|
|
@ selection_mode : SelectionMode :: None ;
|
2025-01-31 17:38:52 +01:00
|
|
|
) ;
|
2024-11-28 21:35:55 +01:00
|
|
|
let film_widget_pairs = RefCell :: new ( vec ! () ) ;
|
2024-11-27 11:47:20 +01:00
|
|
|
|
2025-02-05 13:51:22 +01:00
|
|
|
Self { film_widget_pairs , grid_widget }
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
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 () ) ;
|
2024-11-27 11:47:20 +01:00
|
|
|
|
2025-01-31 16:24:22 +01:00
|
|
|
for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) {
|
2024-11-28 21:35:55 +01:00
|
|
|
self . grid_widget . append ( & film_widget ) ;
|
2024-11-27 11:47:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 16:24:22 +01:00
|
|
|
pub fn set_sorting ( & self , sorting : FilmsSorting ) {
|
2024-11-27 11:47:20 +01:00
|
|
|
self . grid_widget . remove_all () ;
|
2024-11-26 17:20:53 +01:00
|
|
|
|
2025-01-31 16:24:22 +01:00
|
|
|
for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) {
|
2024-11-28 21:35:55 +01:00
|
|
|
self . grid_widget . append ( & film_widget ) ;
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-28 21:35:55 +01:00
|
|
|
|
2025-02-07 16:22:34 +01:00
|
|
|
fn sort_film_widget_pairs ( & self , sorting : FilmsSorting ) -> Vec < ( FilmOverview , Button ) > {
|
2024-11-28 21:35:55 +01:00
|
|
|
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 =>
|
2024-11-28 21:35:55 +01:00
|
|
|
film_1 . name . cmp ( & film_2 . name ) ,
|
2025-01-31 16:24:22 +01:00
|
|
|
FilmProperty :: ReleaseDate =>
|
2024-11-28 21:35:55 +01:00
|
|
|
film_1 . release_date . cmp ( & film_2 . release_date ) ,
|
2025-01-31 16:24:22 +01:00
|
|
|
FilmProperty :: Runtime =>
|
2024-11-28 21:35:55 +01:00
|
|
|
film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) ,
|
|
|
|
|
} ) ;
|
2025-01-31 16:24:22 +01:00
|
|
|
if sorting . direction == SortingDirection :: Descending { sorted . reverse () }
|
2024-11-28 21:35:55 +01:00
|
|
|
|
2025-01-30 17:56:43 +01:00
|
|
|
// See it, say it, ...
|
2024-11-28 21:35:55 +01:00
|
|
|
sorted
|
|
|
|
|
}
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
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 ! (
|
2025-02-04 15:02:58 +01:00
|
|
|
@ orientation : Horizontal ;
|
|
|
|
|
@ homogeneous : true ;
|
|
|
|
|
@ selection_mode : SelectionMode :: None ;
|
2025-01-31 17:38:52 +01:00
|
|
|
) ;
|
2024-11-28 21:35:55 +01:00
|
|
|
let series_widget_pairs = RefCell :: new ( vec ! () ) ;
|
2024-11-27 11:47:20 +01:00
|
|
|
|
2025-02-05 13:51:22 +01:00
|
|
|
Self { series_widget_pairs , grid_widget }
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
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 () ) ;
|
2024-11-27 11:47:20 +01:00
|
|
|
|
2024-11-28 21:35:55 +01:00
|
|
|
for ( _ , series_widget ) in self . sort_series_widget_pairs (sorting) {
|
|
|
|
|
self . grid_widget . append ( & series_widget ) ;
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 16:24:22 +01:00
|
|
|
pub fn set_sorting ( & self , sorting : SeriesSorting ) {
|
2024-11-27 11:47:20 +01:00
|
|
|
self . grid_widget . remove_all () ;
|
|
|
|
|
|
2024-11-28 21:35:55 +01:00
|
|
|
for ( _ , series_widget ) in self . sort_series_widget_pairs (sorting) {
|
|
|
|
|
self . grid_widget . append ( & series_widget ) ;
|
2024-11-27 11:47:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-28 21:35:55 +01:00
|
|
|
|
2025-02-07 16:22:34 +01:00
|
|
|
fn sort_series_widget_pairs ( & self , sorting : SeriesSorting ) -> Vec < ( SeriesOverview , Button ) > {
|
2024-11-28 21:35:55 +01:00
|
|
|
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 =>
|
2025-02-07 01:46:51 +01:00
|
|
|
series_1 . first_release_date . cmp ( & series_2 . first_release_date ) ,
|
2024-11-28 21:35:55 +01:00
|
|
|
} ) ;
|
2025-01-31 17:09:22 +01:00
|
|
|
if sorting . direction == SortingDirection :: Descending { sorted . reverse () }
|
2024-11-28 21:35:55 +01:00
|
|
|
|
2025-01-31 17:09:22 +01:00
|
|
|
// See it, say it, ...
|
2024-11-28 21:35:55 +01:00
|
|
|
sorted
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
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 ,
|
|
|
|
|
)
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
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 ,
|
|
|
|
|
)
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-05 13:51:22 +01:00
|
|
|
async fn create_collection_item (
|
2024-11-26 17:20:53 +01:00
|
|
|
name : & str ,
|
|
|
|
|
original_name : Option < & str > ,
|
|
|
|
|
poster_file_path : Option < & Path > ,
|
2024-11-27 11:47:20 +01:00
|
|
|
details_widget : & Box ,
|
|
|
|
|
) -> Box {
|
2025-02-07 15:19:41 +01:00
|
|
|
g_box ! (
|
|
|
|
|
@ option_children ;
|
2025-02-07 13:36:14 +01:00
|
|
|
@ orientation : Vertical ;
|
|
|
|
|
@ margin_top : 20 ;
|
|
|
|
|
@ margin_bottom : 20 ;
|
2024-11-26 17:20:53 +01:00
|
|
|
|
2025-02-07 15:19:41 +01:00
|
|
|
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 ! (
|
2025-02-07 13:54:44 +01:00
|
|
|
@ justify : Justification :: Center ;
|
|
|
|
|
@ wrap : true ;
|
|
|
|
|
@ max_width_chars : 1 ; // Not the actual limit, used instead to wrap more aggressively
|
2025-02-07 15:19:41 +01:00
|
|
|
@ 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) ,
|
|
|
|
|
)
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-04 23:46:51 +01:00
|
|
|
fn create_film_details ( film : & FilmOverview ) -> Box {
|
2025-01-31 17:38:52 +01:00
|
|
|
g_box ! (
|
2025-02-04 15:02:58 +01:00
|
|
|
@ orientation : Horizontal ;
|
|
|
|
|
@ halign : Center ;
|
|
|
|
|
@ spacing : 20 ;
|
2025-02-07 13:36:14 +01:00
|
|
|
& label ! ( film . release_date . as_str () ) ,
|
|
|
|
|
& label ! ( format ! ( "{}m" , film . runtime_minutes ) . as_str () ) ,
|
2025-01-31 17:38:52 +01:00
|
|
|
)
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|
2025-02-04 23:46:51 +01:00
|
|
|
fn create_series_details ( series : & SeriesOverview ) -> Box {
|
2025-01-31 17:38:52 +01:00
|
|
|
g_box ! (
|
2025-02-04 15:02:58 +01:00
|
|
|
@ orientation : Horizontal ;
|
|
|
|
|
@ halign : Center ;
|
|
|
|
|
@ spacing : 20 ;
|
2025-02-07 13:36:14 +01:00
|
|
|
& label ! ( series . first_release_date . as_str () ) ,
|
2025-01-31 17:38:52 +01:00
|
|
|
)
|
2024-11-26 17:20:53 +01:00
|
|
|
}
|