Move sorting functionality into collated grid, reuse existing widgets

This commit is contained in:
Reinout Meliesie 2024-11-27 11:47:20 +01:00
parent 60c367beb7
commit 64fc8ba425
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
6 changed files with 147 additions and 74 deletions

View file

@ -17,7 +17,7 @@ pub struct Collection {
pub video_file_path : Option <PathBuf> , pub video_file_path : Option <PathBuf> ,
} }
pub struct Series { # [ derive (Clone) ] pub struct Series {
pub uuid : String , pub uuid : String ,
pub name : String , pub name : String ,
pub original_name : Option <String> , pub original_name : Option <String> ,

View file

@ -21,7 +21,7 @@ fn main () -> ExitCode {
} }
fn on_activate ( app : & Application ) { fn on_activate ( app : & Application ) {
let mut ui = UI :: new (app) ; let ui = UI :: new (app) ;
let collection_handle = spawn_blocking ( || let collection_handle = spawn_blocking ( ||
read_collection_file () . unwrap () read_collection_file () . unwrap ()

View file

@ -1,4 +1,4 @@
use { gtk4 :: { * , prelude :: * } , std :: cell :: * } ; use gtk4 :: { * , prelude :: * } ;
use crate :: { use crate :: {
collection :: * , collection :: * ,
@ -8,63 +8,55 @@ use crate :: {
pub enum FilmsSortedBy { Name , ReleaseDate , Runtime }
pub enum SeriesSortedBy { Name , FirstReleaseDate }
pub struct CollatableFilmsContainer { pub struct CollatableFilmsContainer {
films : & 'static RefCell < Vec <Film> > , collated_grid : & 'static CollatedFilmsGrid ,
flow_box : & 'static CollatedFilmsGrid ,
widget : gtk4 :: Box , widget : gtk4 :: Box ,
} }
pub struct CollatableSeriesContainer { pub struct CollatableSeriesContainer {
series : Vec <Series> , collated_grid : & 'static CollatedSeriesGrid ,
flow_box : CollatedSeriesGrid ,
widget : gtk4 :: Box , widget : gtk4 :: Box ,
} }
impl CollatableFilmsContainer { impl CollatableFilmsContainer {
pub fn new ( films : Vec <Film> ) -> Self { pub fn new ( films : Vec <Film> ) -> Self {
let films = leak ( RefCell :: new (films) ) ; let collated_grid = leak ( CollatedFilmsGrid :: new ( films , FilmsSortedBy :: Name ) ) ;
let flow_box = leak ( CollatedFilmsGrid :: new ( films . borrow () . as_slice () ) ) ;
let widget = create_vertical_box ! ( let widget = create_vertical_box ! (
& create_film_collection_menu ( |sorted_by| { & create_film_collection_menu ( |sorted_by| {
flow_box . set_films ( films . borrow () . as_slice () ) ; collated_grid . set_sorting (sorted_by) ;
} ) , } ) ,
& create_collection_scrolled_window ( flow_box . get_widget () ) , & create_collection_scrolled_window ( collated_grid . get_widget () ) ,
) ; ) ;
Self { films , flow_box , widget } Self { collated_grid , widget }
} }
pub fn set_films ( & self , films : Vec <Film> ) { pub fn set_films ( & self , films : Vec <Film> ) {
* self . films . borrow_mut () = films ; self . collated_grid . set_films ( films , FilmsSortedBy :: Name ) ;
self . flow_box . set_films ( self . films . borrow () . as_slice () ) ;
} }
pub fn get_widget ( & self ) -> & gtk4 :: Box { & self . widget } pub fn get_widget ( & self ) -> & gtk4 :: Box { & self . widget }
} }
impl CollatableSeriesContainer { impl CollatableSeriesContainer {
pub fn new ( series : Vec <Series> ) -> Self { pub fn new ( series : Vec <Series> ) -> Self {
let flow_box = CollatedSeriesGrid :: new ( series . as_slice () ) ; let collated_grid = leak ( CollatedSeriesGrid :: new ( series , SeriesSortedBy :: Name ) ) ;
let widget = create_vertical_box ! ( let widget = create_vertical_box ! (
& create_series_collection_menu ( |sorted_by| { & create_series_collection_menu ( |sorted_by| {
// TODO collated_grid . set_sorting (sorted_by) ;
} ) , } ) ,
& create_collection_scrolled_window ( flow_box . get_widget () ) , & create_collection_scrolled_window ( collated_grid . get_widget () ) ,
) ; ) ;
Self { series, flow_box , widget } Self { collated_grid , widget }
} }
pub fn set_series ( & mut self , series : Vec <Series> ) { pub fn set_series ( & self , series : Vec <Series> ) {
self . series = series ; self . collated_grid . set_series ( series , SeriesSortedBy :: Name ) ;
self . flow_box . set_series ( self . series . as_slice () ) ;
} }
pub fn get_widget ( & self ) -> & gtk4 :: Box { & self . widget } pub fn get_widget ( & self ) -> & gtk4 :: Box { & self . widget }
} }
fn create_collection_scrolled_window ( child : & impl IsA <Widget> ) -> ScrolledWindow { fn create_collection_scrolled_window ( child : & FlowBox ) -> ScrolledWindow {
ScrolledWindow :: builder () ScrolledWindow :: builder ()
. child ( & create_vertical_filler_container (child) ) . child ( & create_vertical_filler_container (child) )
. propagate_natural_height (true) . propagate_natural_height (true)

View file

@ -4,66 +4,145 @@ use {
Align :: * , Align :: * ,
Orientation :: * , Orientation :: * ,
gdk :: Texture , gdk :: Texture ,
glib :: * ,
prelude :: * , prelude :: * ,
} , } ,
std :: path :: * , std :: { cell :: * , iter :: * , path :: * } ,
} ; } ;
use crate :: collection :: * ; use crate :: collection :: * ;
pub struct CollatedFilmsGrid { widget : FlowBox } pub enum FilmsSortedBy { Name , ReleaseDate , Runtime }
pub struct CollatedSeriesGrid { widget : FlowBox } pub enum SeriesSortedBy { Name , FirstReleaseDate }
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 { impl CollatedFilmsGrid {
pub fn new ( films : & [Film] ) -> Self { pub fn new ( films : Vec <Film> , sorting : FilmsSortedBy ) -> Self {
let widget = create_collection_flow_box () ; let grid_widget = create_flow_box () ;
let film_widgets = films . iter ()
. map (create_film_entry)
. collect :: < Vec <_> > () ;
let film_widget_pairs = RefCell :: new ( zip ( films , film_widgets )
. collect :: < Vec <_> > () ) ;
for film in films { let film_widget_pairs_sorted = sort_film_widget_pairs (
widget . append ( & create_film_item (film) ) ; film_widget_pairs . borrow () . as_slice () , sorting ) ;
for ( _ , film_widget ) in & film_widget_pairs_sorted {
grid_widget . append (film_widget) ;
} }
Self { widget } Self { film_widget_pairs , grid_widget }
} }
pub fn set_films ( & self , films : & [Film] ) { pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSortedBy ) {
self . widget . remove_all () ; let film_widgets = films . iter ()
for film in films { . map (create_film_entry)
let widget = self . widget . clone () ; . collect :: < Vec <_> > () ;
let film = film . clone () ; * self . film_widget_pairs . borrow_mut () = zip ( films , film_widgets )
. collect :: < Vec <_> > () ;
spawn_future_local ( async move { let film_widget_pairs_sorted = sort_film_widget_pairs (
widget . append ( & create_film_item ( & film ) ) ; self . film_widget_pairs . borrow () . as_slice () , sorting ) ;
} ) ; for ( _ , film_widget ) in & film_widget_pairs_sorted {
self . grid_widget . append (film_widget) ;
} }
} }
pub fn get_widget ( & self ) -> & FlowBox { & self . widget } pub fn set_sorting ( & self , sorting : FilmsSortedBy ) {
self . grid_widget . remove_all () ;
let film_widget_pairs_sorted = sort_film_widget_pairs (
self . film_widget_pairs . borrow () . as_slice () , sorting ) ;
for ( _ , film_widget ) in & film_widget_pairs_sorted {
self . grid_widget . append (film_widget) ;
}
}
pub fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
} }
impl CollatedSeriesGrid { impl CollatedSeriesGrid {
pub fn new ( series : & [Series] ) -> Self { pub fn new ( series : Vec <Series> , sorting : SeriesSortedBy ) -> Self {
let widget = create_collection_flow_box () ; let grid_widget = create_flow_box () ;
let series_widgets = series . iter ()
. map (create_series_entry)
. collect :: < Vec <_> > () ;
let series_widget_pairs = RefCell :: new ( zip ( series , series_widgets )
. collect :: < Vec <_> > () ) ;
for series in series { let series_widget_pairs_sorted = sort_series_widget_pairs (
widget . append ( & create_series_item (series) ) ; series_widget_pairs . borrow () . as_slice () , sorting ) ;
for ( _ , series_widget ) in & series_widget_pairs_sorted {
grid_widget . append (series_widget) ;
} }
Self { widget } Self { series_widget_pairs , grid_widget }
} }
pub fn set_series ( & self , series : & [Series] ) { pub fn set_series ( & self , series : Vec <Series> , sorting : SeriesSortedBy ) {
self . widget . remove_all () ; let series_widgets = series . iter ()
for series in series { . map (create_series_entry)
self . widget . append ( & create_series_item (series) ) ; . collect :: < Vec <_> > () ;
* self . series_widget_pairs . borrow_mut () = zip ( series , series_widgets )
. collect :: < Vec <_> > () ;
let series_widget_pairs_sorted = sort_series_widget_pairs (
self . series_widget_pairs . borrow () . as_slice () , sorting ) ;
for ( _ , series_widget ) in & series_widget_pairs_sorted {
self . grid_widget . append (series_widget) ;
} }
} }
pub fn get_widget ( & self ) -> & FlowBox { & self . widget } pub fn set_sorting ( & self , sorting : SeriesSortedBy ) {
self . grid_widget . remove_all () ;
let series_widget_pairs_sorted = sort_series_widget_pairs (
self . series_widget_pairs . borrow () . as_slice () , sorting ) ;
for ( _ , series_widget ) in & series_widget_pairs_sorted {
self . grid_widget . append (series_widget) ;
}
}
pub fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
} }
fn create_collection_flow_box () -> FlowBox { fn sort_film_widget_pairs (
film_widget_pairs : & [ ( Film , Box ) ] ,
sorting : FilmsSortedBy ,
) -> Vec < ( Film , Box ) > {
let mut sorted = Vec :: from (film_widget_pairs) ;
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 ) ,
} ) ;
sorted
}
fn sort_series_widget_pairs (
series_widget_pairs : & [ ( Series, Box ) ] ,
sorting : SeriesSortedBy ,
) -> Vec < ( Series , Box ) > {
let mut sorted = Vec :: from (series_widget_pairs) ;
sorted . sort_by ( | ( series_1 , _ ) , ( series_2 , _ ) | match sorting {
SeriesSortedBy :: Name => series_1 . name . cmp ( & series_2 . name ) ,
SeriesSortedBy :: FirstReleaseDate => todo ! () ,
} ) ;
sorted
}
fn create_flow_box () -> FlowBox {
FlowBox :: builder () FlowBox :: builder ()
. orientation (Horizontal) . orientation (Horizontal)
. homogeneous (true) . homogeneous (true)
@ -71,7 +150,7 @@ fn create_collection_flow_box () -> FlowBox {
. build () . build ()
} }
pub fn create_film_item ( film : & Film ) -> gtk4 :: Box { pub fn create_film_entry ( film : & Film ) -> Box {
create_collection_item ( create_collection_item (
film . name . as_str () , film . name . as_str () ,
film . original_name . as_deref () , film . original_name . as_deref () ,
@ -79,7 +158,7 @@ pub fn create_film_item ( film : & Film ) -> gtk4 :: Box {
& create_film_details (film) , & create_film_details (film) ,
) )
} }
pub fn create_series_item ( series : & Series ) -> gtk4 :: Box { pub fn create_series_entry ( series : & Series ) -> Box {
create_collection_item ( create_collection_item (
series . name . as_str () , series . name . as_str () ,
series . original_name . as_deref () , series . original_name . as_deref () ,
@ -92,9 +171,9 @@ fn create_collection_item (
name : & str , name : & str ,
original_name : Option < & str > , original_name : Option < & str > ,
poster_file_path : Option < & Path > , poster_file_path : Option < & Path > ,
details_widget : & gtk4 :: Box , details_widget : & Box ,
) -> gtk4 :: Box { ) -> Box {
let container = gtk4 :: Box :: builder () let container = Box :: builder ()
. orientation (Vertical) . orientation (Vertical)
. margin_top (20) . margin_top (20)
. margin_bottom (20) . margin_bottom (20)
@ -138,8 +217,8 @@ fn create_collection_item (
container container
} }
fn create_film_details ( film : & Film ) -> gtk4 :: Box { fn create_film_details ( film : & Film ) -> Box {
let container = gtk4 :: Box :: builder () let container = Box :: builder ()
. orientation (Horizontal) . orientation (Horizontal)
. halign (Center) . halign (Center)
. spacing (20) . spacing (20)
@ -154,8 +233,8 @@ fn create_film_details ( film : & Film ) -> gtk4 :: Box {
container container
} }
fn create_series_details ( series : & Series ) -> gtk4 :: Box { fn create_series_details ( series : & Series ) -> Box {
let container = gtk4 :: Box :: builder () let container = Box :: builder ()
. orientation (Horizontal) . orientation (Horizontal)
. halign (Center) . halign (Center)
. spacing (20) . spacing (20)

View file

@ -8,12 +8,12 @@ use {
libadwaita :: * , libadwaita :: * ,
} ; } ;
use crate :: ui :: { collatable_container :: * , utility :: * } ; use crate :: ui :: { collated_grid :: * , utility :: * } ;
pub fn create_film_collection_menu <F> ( on_sort : F ) -> gtk4 :: Box pub fn create_film_collection_menu < F : Fn (FilmsSortedBy) + 'static > ( on_sort : F )
where F : Fn (FilmsSortedBy) + 'static { -> gtk4 :: Box {
let container = gtk4 :: Box :: builder () let container = gtk4 :: Box :: builder ()
. orientation (Horizontal) . orientation (Horizontal)
. halign (Center) . halign (Center)
@ -26,8 +26,8 @@ where F : Fn (FilmsSortedBy) + 'static {
container container
} }
pub fn create_series_collection_menu < F : Fn (SeriesSortedBy) + 'static > ( on_sort : F )
pub fn create_series_collection_menu ( on_sort : fn (SeriesSortedBy) ) -> gtk4 :: Box { -> gtk4 :: Box {
let container = gtk4 :: Box :: builder () let container = gtk4 :: Box :: builder ()
. orientation (Horizontal) . orientation (Horizontal)
. halign (Center) . halign (Center)
@ -48,8 +48,8 @@ fn create_sort_button ( sort_menu : & Popover ) -> SplitButton {
. build () . build ()
} }
fn create_films_sort_menu <F> ( on_sort : F ) -> Popover fn create_films_sort_menu < F : Fn (FilmsSortedBy) + 'static > ( on_sort : F )
where F : Fn (FilmsSortedBy) + 'static { -> Popover {
let container = ListBox :: new () ; let container = ListBox :: new () ;
container . append ( & create_sort_menu_entry ( "Name" , false , true ) ) ; container . append ( & create_sort_menu_entry ( "Name" , false , true ) ) ;
@ -70,8 +70,8 @@ where F : Fn (FilmsSortedBy) + 'static {
. css_classes ( ["menu"] ) . css_classes ( ["menu"] )
. build () . build ()
} }
fn create_series_sort_menu < F : Fn (SeriesSortedBy) + 'static > ( on_sort : F )
fn create_series_sort_menu ( on_sort : fn (SeriesSortedBy) ) -> Popover { -> Popover {
let container = ListBox :: new () ; let container = ListBox :: new () ;
container . append ( & create_sort_menu_entry ( "Name" , false , true ) ) ; container . append ( & create_sort_menu_entry ( "Name" , false , true ) ) ;
@ -91,7 +91,9 @@ fn create_series_sort_menu ( on_sort : fn (SeriesSortedBy) ) -> Popover {
. build () . build ()
} }
fn create_sort_menu_entry ( label : & str , reverse : bool , selected : bool ) -> ListBoxRow { fn create_sort_menu_entry (
label : & str , reverse : bool , selected : bool ,
) -> ListBoxRow {
let icon = match reverse { let icon = match reverse {
false => Image :: from_icon_name ("view-sort-ascending-symbolic") , false => Image :: from_icon_name ("view-sort-ascending-symbolic") ,
true => Image :: from_icon_name ("view-sort-descending-symbolic") , true => Image :: from_icon_name ("view-sort-descending-symbolic") ,

View file

@ -35,7 +35,7 @@ impl UI {
pub fn show_window ( & self ) { self . window . set_visible (true) } pub fn show_window ( & self ) { self . window . set_visible (true) }
pub fn render_collection ( & mut self , collection : Collection ) { pub fn render_collection ( & self , collection : Collection ) {
self . films_container . set_films ( collection . films ) ; self . films_container . set_films ( collection . films ) ;
self . series_container . set_series ( collection . series ) ; self . series_container . set_series ( collection . series ) ;
} }