diff --git a/src/ui/collatable_container.rs b/src/ui/collatable_container.rs index 91911fa..dc58eef 100644 --- a/src/ui/collatable_container.rs +++ b/src/ui/collatable_container.rs @@ -25,10 +25,9 @@ pub struct CollatableSeriesContainer { impl CollatableFilmsContainer { pub fn new ( films : Vec ) -> Self { let collated_grid = leak ( - CollatedFilmsGrid :: new ( films , FilmsSortedBy :: Name ) ) ; - let film_collation_menu = FilmCollationMenu :: new ( | sorted_by , direction | { - collated_grid . set_sorting (sorted_by) ; - } ) ; + CollatedFilmsGrid :: new ( films , FilmsSortedBy :: Name , SortingDirection :: Ascending ) ) ; + let film_collation_menu = FilmCollationMenu :: new ( | sorting , direction | + collated_grid . set_sorting ( sorting , direction ) ) ; let widget = create_vertical_box ! ( film_collation_menu . get_widget () , @@ -39,7 +38,7 @@ impl CollatableFilmsContainer { } pub fn set_films ( & self , films : Vec ) { - self . collated_grid . set_films ( films , FilmsSortedBy :: Name ) ; + self . collated_grid . set_films ( films , FilmsSortedBy :: Name , SortingDirection :: Ascending ) ; } } impl CollatableSeriesContainer { diff --git a/src/ui/collated_grid.rs b/src/ui/collated_grid.rs index e787ea3..3081bee 100644 --- a/src/ui/collated_grid.rs +++ b/src/ui/collated_grid.rs @@ -13,9 +13,9 @@ use crate :: { collection :: * , ui :: component :: * } ; -pub enum FilmsSortedBy { Name , ReleaseDate , Runtime } -pub enum SeriesSortedBy { Name , FirstReleaseDate } -pub enum SortingDirection { Ascending , Descending } +# [ derive (PartialEq) ] pub enum FilmsSortedBy { Name , ReleaseDate , Runtime } +# [ derive (PartialEq) ] pub enum SeriesSortedBy { Name , FirstReleaseDate } +# [ derive (PartialEq) ] pub enum SortingDirection { Ascending , Descending } pub struct CollatedFilmsGrid { film_widget_pairs : RefCell < Vec < ( Film , Box ) > > , @@ -27,37 +27,37 @@ pub struct CollatedSeriesGrid { } impl CollatedFilmsGrid { - pub fn new ( films : Vec , sorting : FilmsSortedBy ) -> Self { + pub fn new ( films : Vec , 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 ) ; + component . set_films ( films , sorting , direction ) ; component } - pub fn set_films ( & self , films : Vec , sorting : FilmsSortedBy ) { + pub fn set_films ( & self , films : Vec , 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) { + for ( _ , film_widget ) in self . sort_film_widget_pairs ( sorting , direction ) { self . grid_widget . append ( & film_widget ) ; } } - pub fn set_sorting ( & self , sorting : FilmsSortedBy ) { + pub fn set_sorting ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) { self . grid_widget . remove_all () ; - for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) { + 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 ) -> Vec < ( Film , Box ) > { + 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 () ) ; @@ -69,7 +69,9 @@ impl CollatedFilmsGrid { FilmsSortedBy :: Runtime => film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) , } ) ; + if direction == SortingDirection :: Descending { sorted . reverse () } + // See it, say it, ... sorted } } diff --git a/src/ui/collation_menu.rs b/src/ui/collation_menu.rs index e259316..43527a8 100644 --- a/src/ui/collation_menu.rs +++ b/src/ui/collation_menu.rs @@ -6,16 +6,19 @@ use { prelude :: * , } , libadwaita :: * , - std :: cell :: * , + std :: { cell :: * , ops :: * } , } ; -use crate :: ui :: { collated_grid :: * , component :: * , utility :: * } ; +use crate :: { + ui :: { collated_grid :: * , component :: * , utility :: * } , + utility :: * , +} ; pub struct FilmCollationMenu { widget : Box , - sorting : RefCell < ( FilmsSortedBy , SortingDirection ) > , + sorting : & 'static RefCell < ( FilmsSortedBy , SortingDirection ) > , } pub struct SeriesCollationMenu { widget : Box } @@ -28,12 +31,11 @@ impl FilmCollationMenu { . name ("film-collation-menu") . css_classes ( ["toolbar"] ) . build () ; + let sorting = leak ( RefCell :: new ( ( FilmsSortedBy :: Name , SortingDirection :: Ascending ) ) ) ; - widget . append ( & create_sort_button ( & create_films_sort_menu (on_sort) ) ) ; + widget . append ( & create_sort_button ( & create_films_sort_menu ( sorting , on_sort ) ) ) ; widget . append ( & create_filter_button () ) ; - let sorting = RefCell :: new ( ( FilmsSortedBy :: Name , SortingDirection :: Ascending ) ) ; - Self { widget , sorting } } } @@ -67,7 +69,10 @@ fn create_sort_button ( sort_menu : & Popover ) -> SplitButton { . build () } -fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > ( on_sort : F ) -> Popover { +fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > ( + sorting : & 'static RefCell < ( FilmsSortedBy , SortingDirection ) > , + on_sort : F , +) -> Popover { let container = ListBox :: new () ; container . append ( & create_sort_menu_entry ( "Name" , false ) ) ; @@ -76,15 +81,15 @@ fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'stati container . select_row ( container . row_at_index (0) . as_ref () ) ; - container . connect_row_activated ( move | _ , row | on_sort ( - match row . index () { + container . connect_row_activated ( move | _ , row | { + let sorting_property = match row . index () { 0 => FilmsSortedBy :: Name , 1 => FilmsSortedBy :: ReleaseDate , 2 => FilmsSortedBy :: Runtime , _ => panic ! () , - } , - SortingDirection :: Ascending , - ) ) ; + } ; + on_sort ( sorting_property , SortingDirection :: Ascending ) + } ) ; Popover :: builder () . child ( & container )