From 86330dac372cd706579d5fa79b5bd025b19726f3 Mon Sep 17 00:00:00 2001 From: Reinout Meliesie Date: Fri, 31 Jan 2025 16:52:51 +0100 Subject: [PATCH] Move sort button row activation handler into separate function --- .../collation_menu/mod.rs | 2 +- .../collation_menu/sort_button.rs | 104 ++++++++++-------- src/ui/utility.rs | 8 +- 3 files changed, 63 insertions(+), 51 deletions(-) diff --git a/src/ui/collatable_container/collation_menu/mod.rs b/src/ui/collatable_container/collation_menu/mod.rs index a0b1d32..1107928 100644 --- a/src/ui/collatable_container/collation_menu/mod.rs +++ b/src/ui/collatable_container/collation_menu/mod.rs @@ -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 } diff --git a/src/ui/collatable_container/collation_menu/sort_button.rs b/src/ui/collatable_container/collation_menu/sort_button.rs index 1905b1e..602b882 100644 --- a/src/ui/collatable_container/collation_menu/sort_button.rs +++ b/src/ui/collatable_container/collation_menu/sort_button.rs @@ -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 for FilmSortButton { fn get_widget ( & self ) -> & SplitButton { & self . widget } } + +fn on_row_activated < F : Fn (FilmsSorting) > ( + row : & ListBoxRow , + previous_sorting : & RefCell , + 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 ) ) ; + } +} diff --git a/src/ui/utility.rs b/src/ui/utility.rs index 41264be..65b8f72 100644 --- a/src/ui/utility.rs +++ b/src/ui/utility.rs @@ -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 } } }