Move sort button row activation handler into separate function
This commit is contained in:
parent
e2330cae50
commit
86330dac37
3 changed files with 63 additions and 51 deletions
|
@ -30,7 +30,7 @@ impl FilmCollationMenu {
|
|||
@ spacing : 20 ,
|
||||
@ widget_name : "film-collation-menu" ,
|
||||
@ css_classes : [ "toolbar" ] ,
|
||||
* film_sort_button . get_widget ()
|
||||
* film_sort_button . get_widget () ,
|
||||
) ;
|
||||
|
||||
Self { widget }
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use { gtk4 :: Align :: * , libadwaita :: * , std :: cell :: * } ;
|
||||
use { gtk4 :: { ListBoxRow , Align :: * } , libadwaita :: * , std :: cell :: * } ;
|
||||
|
||||
use crate :: {
|
||||
utility :: * ,
|
||||
|
@ -14,56 +14,34 @@ pub struct FilmSortButton {
|
|||
|
||||
impl FilmSortButton {
|
||||
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 () ) ) ;
|
||||
|
||||
list_box . connect_row_activated ( move | _ , row | {
|
||||
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 ) ) ;
|
||||
let widget = split_button ! (
|
||||
@ popover : popover ! (
|
||||
@ css_classes : [ "menu" ] ,
|
||||
list_box ! (
|
||||
@ connect_row_activated : move | _ , row | {
|
||||
on_row_activated ( row , previous_sorting , & on_sort ) ;
|
||||
} ,
|
||||
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 ) ) ;
|
||||
}
|
||||
} ) ;
|
||||
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") ,
|
||||
) ,
|
||||
) ,
|
||||
) ,
|
||||
label ! ("Sort") ,
|
||||
) ;
|
||||
|
||||
Self { widget , previous_sorting }
|
||||
}
|
||||
|
@ -72,3 +50,33 @@ impl FilmSortButton {
|
|||
impl Component <SplitButton> for FilmSortButton {
|
||||
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 ) ) ;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,9 +42,13 @@ macro_rules ! view_stack { (
|
|||
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 () ;
|
||||
$ ( container . append ( & $ child ) ; ) *
|
||||
$ ( container . connect_row_activated ( $ connect_row_activated ) ; ) ?
|
||||
$ ( container . append ( & $ child ) ; ) +
|
||||
container
|
||||
} } }
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue