Implement structure to open modal when clicking on media item

This commit is contained in:
Reinout Meliesie 2025-02-12 14:11:53 +01:00
commit be92920cc5
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
4 changed files with 101 additions and 82 deletions

View file

@ -3,6 +3,7 @@ use gtk4 :: Align :: * ;
use gtk4 :: Orientation :: * ;
use gtk4 :: gdk :: * ;
use gtk4 :: gio :: * ;
use gtk4 :: glib :: * ;
use gtk4 :: pango :: * ;
use gtk4 :: pango :: Weight :: * ;
use gtk4 :: prelude :: * ;
@ -15,21 +16,23 @@ use crate :: ui :: component :: * ;
pub struct CollatedMediaGrid < A : MediaAdapter > {
pub struct CollatedMediaGrid < A : MediaAdapter + 'static > {
media_widget_pairs : RefCell < Vec < ( A :: Overview , Button ) > > ,
grid_widget : FlowBox ,
on_media_selected : & 'static dyn Fn ( A :: Overview ) ,
}
impl < A : MediaAdapter > CollatedMediaGrid <A> {
pub fn new () -> Self {
pub fn new ( on_media_selected : impl Fn ( A :: Overview ) + 'static ) -> Self {
let grid_widget = flow_box ! (
@ orientation : Horizontal ;
@ homogeneous : true ;
@ selection_mode : SelectionMode :: None ;
) ;
let media_widget_pairs = RefCell :: new ( vec ! () ) ;
let on_media_selected = leak (on_media_selected) ;
Self { media_widget_pairs , grid_widget }
Self { media_widget_pairs , grid_widget , on_media_selected }
}
pub async fn set_media ( & self , media : Vec < A :: Overview > , sorting : A :: Sorting ) {
@ -37,7 +40,7 @@ impl < A : MediaAdapter > CollatedMediaGrid <A> {
let mut widgets = Vec :: new () ;
for media in media . as_slice () {
widgets . push ( create_media_entry (media) . await ) ;
widgets . push ( self . create_media_entry (media) . await ) ;
}
self . media_widget_pairs . replace ( zip ( media , widgets ) . collect () ) ;
@ -46,6 +49,71 @@ impl < A : MediaAdapter > CollatedMediaGrid <A> {
}
}
async fn create_media_entry ( & self , media : & A :: Overview ) -> Button {
button ! (
@ css_classes : & [ "flat" , "open-collection-item-button" ] ;
@ connect_clicked : clone ! (
# [strong] media ,
# [ strong ( rename_to = on_media_selected ) ] self . on_media_selected ,
move |_| on_media_selected ( media . clone () ) ,
) ;
& g_box ! (
@ option_children ;
@ orientation : Vertical ;
@ margin_top : 20 ;
@ margin_bottom : 20 ;
match media . get_poster_file_path () {
Some (poster_file_path) => {
let poster_file_path = PathBuf :: from (poster_file_path) ; // God forbid `Path` would work with `clone ! ()`
let poster_texture = spawn_blocking (
move || Texture :: from_filename (poster_file_path) . unwrap () ,
) . await . unwrap () ;
Some ( image ! (
@ paintable : & poster_texture ;
@ width_request : 300 ;
@ height_request : 300 ;
@ margin_bottom : 10 ;
) )
} ,
None => None ,
} . as_ref () ,
Some ( & label ! (
@ justify : Justification :: Center ;
@ wrap : true ;
@ max_width_chars : 1 ; // Not the actual limit, used instead to wrap more aggressively
@ attributes : & pango_attributes ! ( @ scale : SCALE_LARGE ; @ weight : Bold ; ) ;
media . get_name () . as_str () ,
) ) ,
match media . get_original_name () {
Some (original_name) => Some ( label ! (
@ justify : Justification :: Center ;
@ wrap : true ;
@ max_width_chars : 1 ; // Not the actual limit, used instead to wrap more aggressively
original_name . as_str () ,
) ) ,
None => None ,
} . as_ref () ,
Some ( & g_box ! (
@ option_children ;
@ orientation : Horizontal ;
@ halign : Center ;
@ spacing : 20 ;
Some ( & label ! ( media . get_release_date () . as_str () ) ) ,
match media . get_runtime_minutes () {
Some (runtime_minutes) => Some (
label ! ( format ! ( "{}m" , runtime_minutes ) . as_str () ) ,
) ,
None => None ,
} . as_ref () ,
) ) ,
) ,
)
}
pub fn set_sorting ( & self , sorting : A :: Sorting ) {
self . grid_widget . remove_all () ;
@ -71,64 +139,3 @@ impl < A : MediaAdapter > CollatedMediaGrid <A> {
impl < A : MediaAdapter > Component for CollatedMediaGrid <A> {
fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
}
async fn create_media_entry < M : MediaOverview > ( media : & M ) -> Button {
button ! (
@ css_classes : & [ "flat" , "open-collection-item-button" ] ;
@ connect_clicked : |_| todo ! () ;
& g_box ! (
@ option_children ;
@ orientation : Vertical ;
@ margin_top : 20 ;
@ margin_bottom : 20 ;
match media . get_poster_file_path () {
Some (poster_file_path) => {
let poster_file_path = PathBuf :: from (poster_file_path) ; // God forbid `Path` would work with `clone ! ()`
let poster_texture = spawn_blocking ( move ||
Texture :: from_filename (poster_file_path) . unwrap ()
) . await . unwrap () ;
Some ( image ! (
@ paintable : & poster_texture ;
@ width_request : 300 ;
@ height_request : 300 ;
@ margin_bottom : 10 ;
) )
} ,
None => None ,
} . as_ref () ,
Some ( & label ! (
@ justify : Justification :: Center ;
@ wrap : true ;
@ max_width_chars : 1 ; // Not the actual limit, used instead to wrap more aggressively
@ attributes : & pango_attributes ! ( @ scale : SCALE_LARGE ; @ weight : Bold ; ) ;
media . get_name () . as_str () ,
) ) ,
match media . get_original_name () {
Some (original_name) => Some ( label ! (
@ justify : Justification :: Center ;
@ wrap : true ;
@ max_width_chars : 1 ; // Not the actual limit, used instead to wrap more aggressively
original_name . as_str () ,
) ) ,
None => None ,
} . as_ref () ,
Some ( & g_box ! (
@ option_children ;
@ orientation : Horizontal ;
@ halign : Center ;
@ spacing : 20 ;
Some ( & label ! ( media . get_release_date () . as_str () ) ) ,
match media . get_runtime_minutes () {
Some (runtime_minutes) => Some (
label ! ( format ! ( "{}m" , runtime_minutes ) . as_str () ) ,
) ,
None => None ,
} . as_ref () ,
) ) ,
) ,
)
}