Introduce sorting structs

This commit is contained in:
Reinout Meliesie 2025-01-31 16:24:22 +01:00
parent 04755fabb7
commit e2330cae50
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
5 changed files with 106 additions and 71 deletions

View file

@ -7,14 +7,12 @@ use {
prelude :: * ,
} ,
libadwaita :: * ,
std :: { cell :: * , ops :: * } ,
std :: ops :: * ,
} ;
use crate :: {
ui :: {
component :: * , utility :: * ,
collatable_container :: { * , collation_menu :: sort_button :: * } } ,
utility :: * ,
use crate :: ui :: {
component :: * , utility :: * ,
collatable_container :: { * , collation_menu :: sort_button :: * } ,
} ;
@ -23,7 +21,7 @@ pub struct FilmCollationMenu { widget : Box }
pub struct SeriesCollationMenu { widget : Box }
impl FilmCollationMenu {
pub fn new < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > ( on_sort : F ) -> Self {
pub fn new < F : Fn (FilmsSorting) + 'static > ( on_sort : F ) -> Self {
let film_sort_button = FilmSortButton :: new (on_sort) ;
let widget = g_box ! (
@ -39,7 +37,7 @@ impl FilmCollationMenu {
}
}
impl SeriesCollationMenu {
pub fn new < F : Fn (SeriesSortedBy) + 'static > ( on_sort : F ) -> Self {
pub fn new < F : Fn (SeriesSorting) + 'static > ( on_sort : F ) -> Self {
let widget = Box :: builder ()
. orientation (Horizontal)
. halign (Center)
@ -68,10 +66,7 @@ fn create_sort_button ( sort_menu : & Popover ) -> SplitButton {
. build ()
}
fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > (
sorting : & 'static RefCell < ( FilmsSortedBy , SortingDirection ) > ,
on_sort : F ,
) -> Popover {
fn create_films_sort_menu < F : Fn (FilmsSorting) + 'static > ( on_sort : F ) -> Popover {
let container = ListBox :: new () ;
container . append ( & create_sort_menu_entry ( "Name" , false ) ) ;
@ -82,12 +77,12 @@ fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'stati
container . connect_row_activated ( move | _ , row | {
let sorting_property = match row . index () {
0 => FilmsSortedBy :: Name ,
1 => FilmsSortedBy :: ReleaseDate ,
2 => FilmsSortedBy :: Runtime ,
0 => FilmProperty :: Name ,
1 => FilmProperty :: ReleaseDate ,
2 => FilmProperty :: Runtime ,
_ => panic ! () ,
} ;
on_sort ( sorting_property , SortingDirection :: Ascending )
on_sort ( FilmsSorting :: new ( sorting_property , SortingDirection :: Ascending ) ) ;
} ) ;
Popover :: builder ()
@ -95,7 +90,7 @@ fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'stati
. css_classes ( ["menu"] )
. build ()
}
fn create_series_sort_menu < F : Fn (SeriesSortedBy) + 'static > ( on_sort : F ) -> Popover {
fn create_series_sort_menu < F : Fn (SeriesSorting) + 'static > ( on_sort : F ) -> Popover {
let container = ListBox :: new () ;
container . append ( & create_sort_menu_entry ( "Name" , false ) ) ;
@ -103,11 +98,14 @@ fn create_series_sort_menu < F : Fn (SeriesSortedBy) + 'static > ( on_sort : F )
container . select_row ( container . row_at_index (0) . as_ref () ) ;
container . connect_row_activated ( move | _ , row | on_sort ( match row . index () {
0 => SeriesSortedBy :: Name ,
1 => SeriesSortedBy :: FirstReleaseDate ,
_ => panic ! () ,
} ) ) ;
container . connect_row_activated ( move | _ , row | {
let sorting_property = match row . index () {
0 => SeriesProperty :: Name ,
1 => SeriesProperty :: FirstReleaseDate ,
_ => panic ! () ,
} ;
on_sort ( SeriesSorting :: new ( sorting_property , SortingDirection :: Ascending ) ) ;
} ) ;
Popover :: builder ()
. child ( & container )

View file

@ -1,16 +1,19 @@
use { gtk4 :: Align :: * , libadwaita :: * , std :: cell :: * } ;
use crate :: { utility :: * , ui :: { * , utility :: * } } ;
use crate :: {
utility :: * ,
ui :: { * , utility :: * , collatable_container :: SortingDirection :: * } ,
} ;
pub struct FilmSortButton {
widget : SplitButton ,
previous_sorting : & 'static RefCell < ( FilmsSortedBy , SortingDirection ) > ,
previous_sorting : & 'static RefCell <FilmsSorting> ,
}
impl FilmSortButton {
pub fn new < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > ( on_sort : F ) -> Self {
pub fn new < F : Fn (FilmsSorting) + 'static > ( on_sort : F ) -> Self {
let list_box = list_box ! (
g_box ! (
@ orientation : Horizontal , @ spacing : 20 ,
@ -34,34 +37,31 @@ impl FilmSortButton {
label ! ("Sort") ,
) ;
let previous_sorting = leak ( RefCell :: new (
( FilmsSortedBy :: Name , SortingDirection :: Ascending )
) ) ;
let previous_sorting = leak ( RefCell :: new ( FilmsSorting :: default () ) ) ;
list_box . connect_row_activated ( move | _ , row | {
let sorting_property = match row . index () {
0 => FilmsSortedBy :: Name ,
1 => FilmsSortedBy :: ReleaseDate ,
2 => FilmsSortedBy :: Runtime ,
0 => FilmProperty :: Name ,
1 => FilmProperty :: ReleaseDate ,
2 => FilmProperty :: Runtime ,
_ => panic ! () ,
} ;
let previous_sorting_property = previous_sorting . borrow () . 0 ;
if sorting_property == previous_sorting_property {
let previous_sorting_direction = previous_sorting . borrow () . 1 ;
if sorting_property == previous_sorting . borrow () . property {
let previous_sorting_direction = previous_sorting . borrow () . direction ;
match previous_sorting_direction {
SortingDirection :: Ascending => {
previous_sorting . replace ( ( sorting_property , SortingDirection :: Descending ) ) ;
on_sort ( sorting_property , SortingDirection :: Descending ) ;
Ascending => {
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Descending ) ) ;
on_sort ( FilmsSorting :: new ( sorting_property , Descending ) ) ;
} ,
SortingDirection :: Descending => {
previous_sorting . replace ( ( sorting_property , SortingDirection :: Ascending ) ) ;
on_sort ( sorting_property , SortingDirection :: Ascending ) ;
Descending => {
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
on_sort ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
} ,
}
} else {
previous_sorting . replace ( ( sorting_property , SortingDirection :: Ascending ) ) ;
on_sort ( sorting_property , SortingDirection :: Ascending ) ;
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
on_sort ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
}
} ) ;