Move sort button row activation handler into separate function

This commit is contained in:
Reinout Meliesie 2025-01-31 16:52:51 +01:00
parent e2330cae50
commit 86330dac37
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
3 changed files with 63 additions and 51 deletions

View file

@ -30,7 +30,7 @@ impl FilmCollationMenu {
@ spacing : 20 , @ spacing : 20 ,
@ widget_name : "film-collation-menu" , @ widget_name : "film-collation-menu" ,
@ css_classes : [ "toolbar" ] , @ css_classes : [ "toolbar" ] ,
* film_sort_button . get_widget () * film_sort_button . get_widget () ,
) ; ) ;
Self { widget } Self { widget }

View file

@ -1,4 +1,4 @@
use { gtk4 :: Align :: * , libadwaita :: * , std :: cell :: * } ; use { gtk4 :: { ListBoxRow , Align :: * } , libadwaita :: * , std :: cell :: * } ;
use crate :: { use crate :: {
utility :: * , utility :: * ,
@ -14,56 +14,34 @@ pub struct FilmSortButton {
impl FilmSortButton { impl FilmSortButton {
pub fn new < F : Fn (FilmsSorting) + '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 ,
label ! ( @ hexpand : true , @ halign : Start , "Name" ) ,
icon ! ("view-sort-ascending-symbolic") ,
) ,
g_box ! (
@ orientation : Horizontal , @ spacing : 20 ,
label ! ( @ hexpand : true , @ halign : Start , "Release date" ) ,
icon ! ("view-sort-ascending-symbolic") ,
) ,
g_box ! (
@ orientation : Horizontal , @ spacing : 20 ,
label ! ( @ hexpand : true , @ halign : Start , "Runtime" ) ,
icon ! ("view-sort-ascending-symbolic") ,
) ,
) ;
let widget = split_button ! (
@ popover : popover ! ( @ css_classes : [ "menu" ] , list_box ) ,
label ! ("Sort") ,
) ;
let previous_sorting = leak ( RefCell :: new ( FilmsSorting :: default () ) ) ; let previous_sorting = leak ( RefCell :: new ( FilmsSorting :: default () ) ) ;
list_box . connect_row_activated ( move | _ , row | { let widget = split_button ! (
let sorting_property = match row . index () { @ popover : popover ! (
0 => FilmProperty :: Name , @ css_classes : [ "menu" ] ,
1 => FilmProperty :: ReleaseDate , list_box ! (
2 => FilmProperty :: Runtime , @ connect_row_activated : move | _ , row | {
_ => panic ! () , on_row_activated ( row , previous_sorting , & on_sort ) ;
} ;
if sorting_property == previous_sorting . borrow () . property {
let previous_sorting_direction = previous_sorting . borrow () . direction ;
match previous_sorting_direction {
Ascending => {
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Descending ) ) ;
on_sort ( FilmsSorting :: new ( sorting_property , Descending ) ) ;
} , } ,
Descending => { g_box ! (
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Ascending ) ) ; @ orientation : Horizontal , @ spacing : 20 ,
on_sort ( FilmsSorting :: new ( sorting_property , Ascending ) ) ; label ! ( @ hexpand : true , @ halign : Start , "Name" ) ,
} , icon ! ("view-sort-ascending-symbolic") ,
} ) ,
} else { g_box ! (
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Ascending ) ) ; @ orientation : Horizontal , @ spacing : 20 ,
on_sort ( FilmsSorting :: new ( sorting_property , Ascending ) ) ; label ! ( @ hexpand : true , @ halign : Start , "Release date" ) ,
} icon ! ("view-sort-ascending-symbolic") ,
} ) ; ) ,
g_box ! (
@ orientation : Horizontal , @ spacing : 20 ,
label ! ( @ hexpand : true , @ halign : Start , "Runtime" ) ,
icon ! ("view-sort-ascending-symbolic") ,
) ,
) ,
) ,
label ! ("Sort") ,
) ;
Self { widget , previous_sorting } Self { widget , previous_sorting }
} }
@ -72,3 +50,33 @@ impl FilmSortButton {
impl Component <SplitButton> for FilmSortButton { impl Component <SplitButton> for FilmSortButton {
fn get_widget ( & self ) -> & SplitButton { & self . widget } fn get_widget ( & self ) -> & SplitButton { & self . widget }
} }
fn on_row_activated < F : Fn (FilmsSorting) > (
row : & ListBoxRow ,
previous_sorting : & RefCell <FilmsSorting> ,
on_sort : & F ,
) {
let sorting_property = match row . index () {
0 => FilmProperty :: Name ,
1 => FilmProperty :: ReleaseDate ,
2 => FilmProperty :: Runtime ,
_ => panic ! () ,
} ;
if sorting_property == previous_sorting . borrow () . property {
let previous_sorting_direction = previous_sorting . borrow () . direction ;
match previous_sorting_direction {
Ascending => {
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Descending ) ) ;
on_sort ( FilmsSorting :: new ( sorting_property , Descending ) ) ;
} ,
Descending => {
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
on_sort ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
} ,
}
} else {
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
on_sort ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
}
}

View file

@ -42,9 +42,13 @@ macro_rules ! view_stack { (
container container
} } } } } }
macro_rules ! list_box { ( $ ( $ child : expr ) , * $ (,) ? ) => { { macro_rules ! list_box { (
$ ( @ connect_row_activated : $ connect_row_activated : expr , ) ?
$ ( $ child : expr ) , + $ (,) ?
) => { {
let container = gtk4 :: ListBox :: new () ; let container = gtk4 :: ListBox :: new () ;
$ ( container . append ( & $ child ) ; ) * $ ( container . connect_row_activated ( $ connect_row_activated ) ; ) ?
$ ( container . append ( & $ child ) ; ) +
container container
} } } } } }