Move collation components into submodules

This commit is contained in:
Reinout Meliesie 2025-01-31 11:38:06 +01:00
parent 5cccefbd4a
commit ebc8bc0d2f
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
7 changed files with 85 additions and 44 deletions

View file

@ -0,0 +1,143 @@
mod sort_button ;
use {
gtk4 :: {
Box , Image , Label , ListBox , ListBoxRow , Popover ,
Align :: * ,
Orientation :: * ,
prelude :: * ,
} ,
libadwaita :: * ,
std :: { cell :: * , ops :: * } ,
} ;
use crate :: {
ui :: { collatable_container :: * , component :: * , utility :: * } ,
utility :: * ,
} ;
pub struct FilmCollationMenu {
widget : Box ,
sorting : & 'static RefCell < ( FilmsSortedBy , SortingDirection ) > ,
}
pub struct SeriesCollationMenu { widget : Box }
impl FilmCollationMenu {
pub fn new < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > ( on_sort : F ) -> Self {
let widget = Box :: builder ()
. orientation (Horizontal)
. halign (Center)
. spacing (20)
. name ("film-collation-menu")
. css_classes ( ["toolbar"] )
. build () ;
let sorting = leak ( RefCell :: new ( ( FilmsSortedBy :: Name , SortingDirection :: Ascending ) ) ) ;
widget . append ( & create_sort_button ( & create_films_sort_menu ( sorting , on_sort ) ) ) ;
widget . append ( & create_filter_button () ) ;
Self { widget , sorting }
}
}
impl SeriesCollationMenu {
pub fn new < F : Fn (SeriesSortedBy) + 'static > ( on_sort : F ) -> Self {
let widget = Box :: builder ()
. orientation (Horizontal)
. halign (Center)
. spacing (20)
. css_classes ( ["toolbar"] )
. build () ;
widget . append ( & create_sort_button ( & create_series_sort_menu (on_sort) ) ) ;
widget . append ( & SplitButton :: builder () . label ("Filter") . build () ) ;
Self { widget }
}
}
impl Component <Box> for FilmCollationMenu {
fn get_widget ( & self ) -> & Box { & self . widget }
}
impl Component <Box> for SeriesCollationMenu {
fn get_widget ( & self ) -> & Box { & self . widget }
}
fn create_sort_button ( sort_menu : & Popover ) -> SplitButton {
SplitButton :: builder ()
. child ( & label ! ("Sort") )
. popover (sort_menu)
. build ()
}
fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > (
sorting : & 'static RefCell < ( FilmsSortedBy , SortingDirection ) > ,
on_sort : F ,
) -> Popover {
let container = ListBox :: new () ;
container . append ( & create_sort_menu_entry ( "Name" , false ) ) ;
container . append ( & create_sort_menu_entry ( "Release date" , false ) ) ;
container . append ( & create_sort_menu_entry ( "Runtime" , false ) ) ;
container . select_row ( container . row_at_index (0) . as_ref () ) ;
container . connect_row_activated ( move | _ , row | {
let sorting_property = match row . index () {
0 => FilmsSortedBy :: Name ,
1 => FilmsSortedBy :: ReleaseDate ,
2 => FilmsSortedBy :: Runtime ,
_ => panic ! () ,
} ;
on_sort ( sorting_property , SortingDirection :: Ascending )
} ) ;
Popover :: builder ()
. child ( & container )
. css_classes ( ["menu"] )
. build ()
}
fn create_series_sort_menu < F : Fn (SeriesSortedBy) + 'static > ( on_sort : F ) -> Popover {
let container = ListBox :: new () ;
container . append ( & create_sort_menu_entry ( "Name" , false ) ) ;
container . append ( & create_sort_menu_entry ( "First release date" , false ) ) ;
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 ! () ,
} ) ) ;
Popover :: builder ()
. child ( & container )
. css_classes ( ["menu"] )
. build ()
}
fn create_sort_menu_entry ( label : & str , reverse : bool ) -> ListBoxRow {
let icon = match reverse {
false => Image :: from_icon_name ("view-sort-ascending-symbolic") ,
true => Image :: from_icon_name ("view-sort-descending-symbolic") ,
} ;
let container = g_box ! (
@ orientation : Horizontal ,
& Label :: builder ()
. label (label)
. hexpand (true)
. halign (Start)
. build () ,
& icon ,
) ;
container . set_spacing (20) ;
ListBoxRow :: builder () . child ( & container ) . build ()
}
fn create_filter_button () -> SplitButton {
SplitButton :: builder () . label ("Filter") . build ()
}

View file

@ -0,0 +1,21 @@
use libadwaita :: * ;
use crate :: ui :: { * , utility :: * } ;
pub struct FilmSortButton { widget : SplitButton }
impl FilmSortButton {
pub fn new () -> Self {
let widget = SplitButton :: builder ()
. child ( & label ! ("Sort") )
. build () ;
Self { widget }
}
}
impl Component <SplitButton> for FilmSortButton {
fn get_widget ( & self ) -> & SplitButton { & self . widget }
}