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 ,
|
@ 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 }
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use { gtk4 :: Align :: * , libadwaita :: * , std :: cell :: * } ;
|
use { gtk4 :: { ListBoxRow , Align :: * } , libadwaita :: * , std :: cell :: * } ;
|
||||||
|
|
||||||
use crate :: {
|
use crate :: {
|
||||||
utility :: * ,
|
utility :: * ,
|
||||||
|
@ -14,7 +14,15 @@ 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 ! (
|
let previous_sorting = leak ( RefCell :: new ( FilmsSorting :: default () ) ) ;
|
||||||
|
|
||||||
|
let widget = split_button ! (
|
||||||
|
@ popover : popover ! (
|
||||||
|
@ css_classes : [ "menu" ] ,
|
||||||
|
list_box ! (
|
||||||
|
@ connect_row_activated : move | _ , row | {
|
||||||
|
on_row_activated ( row , previous_sorting , & on_sort ) ;
|
||||||
|
} ,
|
||||||
g_box ! (
|
g_box ! (
|
||||||
@ orientation : Horizontal , @ spacing : 20 ,
|
@ orientation : Horizontal , @ spacing : 20 ,
|
||||||
label ! ( @ hexpand : true , @ halign : Start , "Name" ) ,
|
label ! ( @ hexpand : true , @ halign : Start , "Name" ) ,
|
||||||
|
@ -30,16 +38,24 @@ impl FilmSortButton {
|
||||||
label ! ( @ hexpand : true , @ halign : Start , "Runtime" ) ,
|
label ! ( @ hexpand : true , @ halign : Start , "Runtime" ) ,
|
||||||
icon ! ("view-sort-ascending-symbolic") ,
|
icon ! ("view-sort-ascending-symbolic") ,
|
||||||
) ,
|
) ,
|
||||||
) ;
|
) ,
|
||||||
|
) ,
|
||||||
let widget = split_button ! (
|
|
||||||
@ popover : popover ! ( @ css_classes : [ "menu" ] , list_box ) ,
|
|
||||||
label ! ("Sort") ,
|
label ! ("Sort") ,
|
||||||
) ;
|
) ;
|
||||||
|
|
||||||
let previous_sorting = leak ( RefCell :: new ( FilmsSorting :: default () ) ) ;
|
Self { widget , previous_sorting }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
list_box . connect_row_activated ( move | _ , row | {
|
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 () {
|
let sorting_property = match row . index () {
|
||||||
0 => FilmProperty :: Name ,
|
0 => FilmProperty :: Name ,
|
||||||
1 => FilmProperty :: ReleaseDate ,
|
1 => FilmProperty :: ReleaseDate ,
|
||||||
|
@ -63,12 +79,4 @@ impl FilmSortButton {
|
||||||
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
|
previous_sorting . replace ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
|
||||||
on_sort ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
|
on_sort ( FilmsSorting :: new ( sorting_property , Ascending ) ) ;
|
||||||
}
|
}
|
||||||
} ) ;
|
|
||||||
|
|
||||||
Self { widget , previous_sorting }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Component <SplitButton> for FilmSortButton {
|
|
||||||
fn get_widget ( & self ) -> & SplitButton { & self . widget }
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue