Replace custom widget macros with Relm4 view macro based ones

Also move margin styling to the CSS file.
This commit is contained in:
Reinout Meliesie 2026-01-02 13:41:27 +01:00
commit 6446d43d76
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
9 changed files with 299 additions and 435 deletions

View file

@ -1,4 +1,4 @@
use gtk4 :: * ;
use gtk4 :: { Button , FlowBox , Image , Justification , Label , SelectionMode } ;
use gtk4 :: Align :: * ;
use gtk4 :: Orientation :: * ;
use gtk4 :: gdk :: * ;
@ -24,12 +24,14 @@ pub struct CollatedMediaGrid < A : MediaAdapter > {
impl < A : MediaAdapter > CollatedMediaGrid <A> {
pub fn new ( on_media_selected : impl Fn ( A :: Overview ) + 'static ) -> Self {
let grid_widget = flow_box ! (
@ orientation : Horizontal ;
@ homogeneous : true ;
@ css_classes : & [ "collatable-container" ] ;
@ selection_mode : SelectionMode :: None ;
) ;
let grid_widget = view_expr ! {
FlowBox {
set_homogeneous : true ,
set_selection_mode : SelectionMode :: None ,
set_css_classes : & [ "collatable-container" ] ,
set_orientation : Horizontal ,
}
} ;
let media_widget_pairs = RefCell :: new ( Vec :: new () ) ;
let on_media_selected = leak (on_media_selected) ;
@ -51,82 +53,99 @@ 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 ;
@ valign : Center ; // I.e. do not fill parent vertically
@ margin_top : 20 ;
@ margin_bottom : 20 ;
view_expr ! {
Button {
set_css_classes : & [ "flat" , "collection-item-button" ] ,
{
let home_directory = var_os ("HOME") . unwrap () ;
let xdg_data_home = var_os ("XDG_DATA_HOME") ;
connect_clicked : clone ! (
# [ strong ] media ,
# [ strong ( rename_to = on_media_selected ) ] self . on_media_selected ,
move |_| on_media_selected ( media . clone () ) ,
) ,
let data_dir = match xdg_data_home {
Some (xdg_data_home) => concat_os_str ! ( xdg_data_home , "/zoodex" ) ,
None => concat_os_str ! ( home_directory , "/.local/share/zoodex" ) ,
} ;
set_child : Some ( & view_expr ! {
gtk4 :: Box {
set_css_classes : & [ "collection-item-box" ] ,
set_valign : Center ,
set_orientation : Vertical ,
let poster_file_path = concat_os_str ! ( data_dir , "/posters/" , media . get_uuid () ) ;
// Poster
append_opt : & {
let home_directory = var_os ("HOME") . unwrap () ;
let xdg_data_home = var_os ("XDG_DATA_HOME") ;
let poster_texture = spawn_blocking (
move || Texture :: from_filename (poster_file_path) ,
) . await . unwrap () ;
let data_dir = match xdg_data_home {
Some (xdg_data_home) => concat_os_str ! ( xdg_data_home , "/zoodex" ) ,
None => concat_os_str ! ( home_directory , "/.local/share/zoodex" ) ,
} ;
match poster_texture {
Ok (poster_texture) => Some ( image ! (
@ margin_bottom : 10 ;
@ pixel_size : 300 ;
@ paintable : & poster_texture ;
) ) ,
Err (error) => {
if error . matches ( IOErrorEnum :: NotFound ) {
None // The file not existing simply means there is no poster for this piece of media
} else {
panic ! ( "{}" , error ) // Any other error means something unexpected went wrong
let poster_file_path = concat_os_str ! ( data_dir , "/posters/" , media . get_uuid () ) ;
let poster_texture = spawn_blocking (
move || Texture :: from_filename (poster_file_path) ,
) . await . unwrap () ;
match poster_texture {
Ok (poster_texture) => Some ( view_expr ! {
Image {
set_paintable : Some ( & poster_texture ) ,
set_pixel_size : 300 ,
set_css_classes : & [ "collection-item-image" ] ,
}
} ) ,
Err (error) => {
if error . matches ( IOErrorEnum :: NotFound ) {
None // The file not existing simply means there is no poster for this piece of media
} else {
panic ! ( "{}" , error ) // Any other error means something unexpected went wrong
}
} ,
}
} ,
// Name
append : & view_expr ! {
Label {
set_attributes : Some ( & pango_attributes ! ( scale : SCALE_LARGE , weight : Bold ) ) ,
set_justify : Justification :: Center ,
set_max_width_chars : 1 , // Not the actual limit, used instead to wrap more aggressively
set_wrap : true ,
set_label : media . get_name () . as_str () ,
}
} ,
// Original name
append_opt : & media . get_original_name () . map ( |original_name| view_expr ! {
Label {
set_justify : Justification :: Center ,
set_max_width_chars : 1 ,
set_wrap : true ,
set_label : original_name . as_str () ,
}
} ) ,
// Details
append : & view_expr ! {
gtk4 :: Box {
set_spacing : 20 ,
set_halign : Center ,
set_orientation : Horizontal ,
// Release date
append : & view_expr ! {
Label { set_label : media . get_release_date () . split ('-') . next () . unwrap () }
} ,
// Runtime
append_opt : & media . get_runtime_minutes () . map ( |runtime_minutes| view_expr ! {
Label { set_label : format ! ( "{}m" , runtime_minutes ) . as_str () }
} ) ,
}
} ,
}
} . 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 () ,
) ) ,
media . get_original_name () . map ( |original_name| label ! (
@ justify : Justification :: Center ;
@ wrap : true ;
@ max_width_chars : 1 ; // Not the actual limit, used instead to wrap more aggressively
original_name . as_str () ,
) ) . as_ref () ,
Some ( & g_box ! (
@ option_children ;
@ orientation : Horizontal ;
@ halign : Center ;
@ spacing : 20 ;
Some ( & label ! (
media . get_release_date () . split ('-') . next () . unwrap () ,
) ) ,
media . get_runtime_minutes () . map (
|runtime_minutes| label ! ( format ! ( "{}m" , runtime_minutes ) . as_str () ) ,
) . as_ref () ,
) ) ,
) ,
)
} ) ,
}
}
}
pub fn set_sorting ( & self , sorting : A :: Sorting ) {