Move collation components into submodules

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

View file

@ -0,0 +1,226 @@
use {
gtk4 :: {
* ,
Align :: * ,
Orientation :: * ,
gdk :: Texture ,
prelude :: * ,
} ,
std :: { cell :: * , iter :: * , path :: Path } ,
} ;
use crate :: { collection :: * , ui :: { collatable_container :: * , component :: * } } ;
pub struct CollatedFilmsGrid {
film_widget_pairs : RefCell < Vec < ( Film , Box ) > > ,
grid_widget : FlowBox ,
}
pub struct CollatedSeriesGrid {
series_widget_pairs : RefCell < Vec < ( Series , Box ) > > ,
grid_widget : FlowBox ,
}
impl CollatedFilmsGrid {
pub fn new ( films : Vec <Film> , sorting : FilmsSortedBy , direction : SortingDirection ) -> Self {
let grid_widget = create_flow_box () ;
let film_widget_pairs = RefCell :: new ( vec ! () ) ;
let component = Self { film_widget_pairs , grid_widget } ;
component . set_films ( films , sorting , direction ) ;
component
}
pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSortedBy , direction : SortingDirection ) {
let widgets = films . iter ()
. map (create_film_entry)
. collect :: < Vec <_> > () ;
* self . film_widget_pairs . borrow_mut () = zip ( films , widgets )
. collect () ;
for ( _ , film_widget ) in self . sort_film_widget_pairs ( sorting , direction ) {
self . grid_widget . append ( & film_widget ) ;
}
}
pub fn set_sorting ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) {
self . grid_widget . remove_all () ;
for ( _ , film_widget ) in self . sort_film_widget_pairs ( sorting , direction ) {
self . grid_widget . append ( & film_widget ) ;
}
}
fn sort_film_widget_pairs ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) -> Vec < ( Film , Box ) > {
let mut sorted = Vec :: from (
self . film_widget_pairs . borrow () . as_slice () ) ;
sorted . sort_by ( | ( film_1 , _ ) , ( film_2 , _ ) | match sorting {
FilmsSortedBy :: Name =>
film_1 . name . cmp ( & film_2 . name ) ,
FilmsSortedBy :: ReleaseDate =>
film_1 . release_date . cmp ( & film_2 . release_date ) ,
FilmsSortedBy :: Runtime =>
film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) ,
} ) ;
if direction == SortingDirection :: Descending { sorted . reverse () }
// See it, say it, ...
sorted
}
}
impl CollatedSeriesGrid {
pub fn new ( series : Vec <Series> , sorting : SeriesSortedBy ) -> Self {
let grid_widget = create_flow_box () ;
let series_widget_pairs = RefCell :: new ( vec ! () ) ;
let component = Self { series_widget_pairs , grid_widget } ;
component . set_series ( series, sorting ) ;
component
}
pub fn set_series ( & self , series : Vec <Series> , sorting : SeriesSortedBy ) {
let widgets = series . iter ()
. map (create_series_entry)
. collect :: < Vec <_> > () ;
* self . series_widget_pairs . borrow_mut () = zip ( series , widgets )
. collect () ;
for ( _ , series_widget ) in self . sort_series_widget_pairs (sorting) {
self . grid_widget . append ( & series_widget ) ;
}
}
pub fn set_sorting ( & self , sorting : SeriesSortedBy ) {
self . grid_widget . remove_all () ;
for ( _ , series_widget ) in self . sort_series_widget_pairs (sorting) {
self . grid_widget . append ( & series_widget ) ;
}
}
fn sort_series_widget_pairs ( & self , sorting : SeriesSortedBy ) -> Vec < ( Series , Box ) > {
let mut sorted = Vec :: from (
self . series_widget_pairs . borrow () . as_slice () ) ;
sorted . sort_by ( | ( series_1 , _ ) , ( series_2 , _ ) | match sorting {
SeriesSortedBy :: Name => series_1 . name . cmp ( & series_2 . name ) ,
SeriesSortedBy :: FirstReleaseDate => todo ! () ,
} ) ;
sorted
}
}
impl Component <FlowBox> for CollatedFilmsGrid {
fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
}
impl Component <FlowBox> for CollatedSeriesGrid {
fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
}
fn create_flow_box () -> FlowBox {
FlowBox :: builder ()
. orientation (Horizontal)
. homogeneous (true)
. selection_mode ( SelectionMode :: None )
. build ()
}
pub fn create_film_entry ( film : & Film ) -> Box {
create_collection_item (
film . name . as_str () ,
film . original_name . as_deref () ,
film . poster_file_path . as_deref () ,
& create_film_details (film) ,
)
}
pub fn create_series_entry ( series : & Series ) -> Box {
create_collection_item (
series . name . as_str () ,
series . original_name . as_deref () ,
series . poster_file_path . as_deref () ,
& create_series_details (series) ,
)
}
fn create_collection_item (
name : & str ,
original_name : Option < & str > ,
poster_file_path : Option < & Path > ,
details_widget : & Box ,
) -> Box {
let container = Box :: builder ()
. orientation (Vertical)
. margin_top (20)
. margin_bottom (20)
. build () ;
if let Some (poster_file_path) = poster_file_path {
let poster_texture = Texture :: from_filename (poster_file_path) . unwrap () ;
container . append (
& Image :: builder ()
. paintable ( & poster_texture )
. width_request (300)
. height_request (300)
. margin_bottom (10)
. build ()
) ;
}
container . append (
& Label :: builder ()
. label ( format ! ( "<span size='large' weight='bold'>{}</span>" , name ) )
. use_markup (true)
. justify ( Justification :: Center )
. wrap (true)
. max_width_chars (1) // Not the actual limit, used instead to wrap more aggressively
. build ()
) ;
if let Some (original_name) = original_name {
container . append (
& Label :: builder ()
. label (original_name)
. justify ( Justification :: Center )
. wrap (true)
. max_width_chars (1) // Not the actual limit, used instead to wrap more aggressively
. build ()
) ;
}
container . append (details_widget) ;
container
}
fn create_film_details ( film : & Film ) -> Box {
let container = Box :: builder ()
. orientation (Horizontal)
. halign (Center)
. spacing (20)
. build () ;
container . append (
& Label :: builder () . label ( & film . release_date ) . build ()
) ;
container . append (
& Label :: builder () . label ( format ! ( "{}m" , film . runtime_minutes ) ) . build ()
) ;
container
}
fn create_series_details ( series : & Series ) -> Box {
let container = Box :: builder ()
. orientation (Horizontal)
. halign (Center)
. spacing (20)
. build () ;
// TODO
container
}

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 }
}

View file

@ -0,0 +1,85 @@
mod collated_grid ;
mod collation_menu ;
use gtk4 :: { * , Orientation :: * , prelude :: * } ;
use crate :: {
collection :: * ,
ui :: {
collatable_container :: { collated_grid :: * , collation_menu :: * } ,
component :: * ,
utility :: * ,
} ,
utility :: * ,
} ;
# [ derive (PartialEq) ] pub enum FilmsSortedBy { Name , ReleaseDate , Runtime }
# [ derive (PartialEq) ] pub enum SeriesSortedBy { Name , FirstReleaseDate }
# [ derive (PartialEq) ] pub enum SortingDirection { Ascending , Descending }
pub struct CollatableFilmsContainer {
collated_grid : & 'static CollatedFilmsGrid ,
widget : Box ,
}
pub struct CollatableSeriesContainer {
collated_grid : & 'static CollatedSeriesGrid ,
widget : Box ,
}
impl CollatableFilmsContainer {
pub fn new ( films : Vec <Film> ) -> Self {
let collated_grid = leak (
CollatedFilmsGrid :: new ( films , FilmsSortedBy :: Name , SortingDirection :: Ascending ) ) ;
let film_collation_menu = FilmCollationMenu :: new ( | sorting , direction |
collated_grid . set_sorting ( sorting , direction ) ) ;
let widget = g_box ! (
@ orientation : Vertical ,
film_collation_menu . get_widget () ,
& create_collection_scrolled_window ( collated_grid . get_widget () ) ,
) ;
Self { collated_grid , widget }
}
pub fn set_films ( & self , films : Vec <Film> ) {
self . collated_grid . set_films ( films , FilmsSortedBy :: Name , SortingDirection :: Ascending ) ;
}
}
impl CollatableSeriesContainer {
pub fn new ( series : Vec <Series> ) -> Self {
let collated_grid = leak (
CollatedSeriesGrid :: new ( series , SeriesSortedBy :: Name ) ) ;
let series_collation_menu = SeriesCollationMenu :: new ( |sorted_by| {
collated_grid . set_sorting (sorted_by) ;
} ) ;
let widget = g_box ! (
@ orientation : Vertical ,
series_collation_menu . get_widget () ,
& create_collection_scrolled_window ( collated_grid . get_widget () ) ,
) ;
Self { collated_grid , widget }
}
pub fn set_series ( & self , series : Vec <Series> ) {
self . collated_grid . set_series ( series , SeriesSortedBy :: Name ) ;
}
}
impl Component <Box> for CollatableFilmsContainer {
fn get_widget ( & self ) -> & Box { & self . widget }
}
impl Component <Box> for CollatableSeriesContainer {
fn get_widget ( & self ) -> & Box { & self . widget }
}
fn create_collection_scrolled_window ( child : & FlowBox ) -> ScrolledWindow {
ScrolledWindow :: builder ()
. child ( & vertically_filling ! (child) )
. propagate_natural_height (true)
. build ()
}