From 988d4016ea3298bd1e0894ffbdc5639ca260f834 Mon Sep 17 00:00:00 2001 From: Reinout Meliesie Date: Tue, 26 Nov 2024 16:49:19 +0100 Subject: [PATCH] Sorting films now uses latest films vec, but it hangs the UI --- src/collection.rs | 2 +- src/ui/dynamic_container.rs | 22 +++++++++++----------- src/ui/internal.rs | 27 ++++++++++++++++----------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/collection.rs b/src/collection.rs index 7ac7ce9..f24638b 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -7,7 +7,7 @@ pub struct Collection { pub series : Vec , } -pub struct Film { +# [ derive (Clone) ] pub struct Film { pub uuid : String , pub name : String , pub original_name : Option , diff --git a/src/ui/dynamic_container.rs b/src/ui/dynamic_container.rs index 5083b75..3d1c5e1 100644 --- a/src/ui/dynamic_container.rs +++ b/src/ui/dynamic_container.rs @@ -10,14 +10,14 @@ use crate :: { } ; + pub enum FilmsSortedBy { Name , ReleaseDate , Runtime } pub enum SeriesSortedBy { Name , FirstReleaseDate } pub struct FilmsContainer { - films : Vec , - flow_box : FilmsFlowBox , + films : & 'static RefCell < Vec > , + flow_box : & 'static FilmsFlowBox , widget : gtk4 :: Box , - sorted_by : & 'static Cell , } pub struct SeriesContainer { series : Vec , @@ -27,21 +27,21 @@ pub struct SeriesContainer { impl FilmsContainer { pub fn new ( films : Vec ) -> Self { - let flow_box = FilmsFlowBox :: new ( films . as_slice () ) ; - let sorted_by = leak ( Cell :: new ( FilmsSortedBy :: Name ) ) ; + let films = leak ( RefCell :: new (films) ) ; + let flow_box = leak ( FilmsFlowBox :: new ( films . borrow () . as_slice () ) ) ; let widget = create_vertical_box ! ( - & create_film_collection_menu ( |new_sorted_by| { - sorted_by . replace (new_sorted_by) ; + & create_film_collection_menu ( |sorted_by| { + flow_box . set_films ( films . borrow () . as_slice () ) ; } ) , & create_collection_scrolled_window ( flow_box . get_widget () ) , ) ; - Self { films , flow_box , widget , sorted_by } + Self { films , flow_box , widget } } - pub fn set_films ( & mut self , films : Vec ) { - self . films = films ; - self . flow_box . set_films ( self . films . as_slice () ) ; + pub fn set_films ( & self , films : Vec ) { + * self . films . borrow_mut () = films ; + self . flow_box . set_films ( self . films . borrow () . as_slice () ) ; } pub fn get_widget ( & self ) -> & gtk4 :: Box { & self . widget } diff --git a/src/ui/internal.rs b/src/ui/internal.rs index e4fe47a..6354c16 100644 --- a/src/ui/internal.rs +++ b/src/ui/internal.rs @@ -4,6 +4,7 @@ use { Align :: * , Orientation :: * , gdk :: Texture , + glib :: * , prelude :: * , } , libadwaita :: { * , ViewSwitcherPolicy :: * } , @@ -62,7 +63,12 @@ impl FilmsFlowBox { pub fn set_films ( & self , films : & [Film] ) { self . widget . remove_all () ; for film in films { - self . widget . append ( & create_film_item (film) ) ; + let widget = self . widget . clone () ; + let film = film . clone () ; + + spawn_future_local ( async move { + widget . append ( & create_film_item ( & film ) ) ; + } ) ; } } @@ -155,16 +161,15 @@ fn create_collection_item ( . build () ; if let Some (poster_file_path) = poster_file_path { - if let Ok (poster_texture) = Texture :: from_filename (poster_file_path) { - container . append ( - & Image :: builder () - . paintable ( & poster_texture ) - . width_request (300) - . height_request (300) - . margin_bottom (10) - . build () - ) ; - } + 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 (