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
parent 2ec2fda116
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 :: Orientation :: * ;
use gtk4 :: gdk :: * ; use gtk4 :: gdk :: * ;
use gtk4 :: gio :: * ; use gtk4 :: gio :: * ;
use gtk4 :: glib :: * ;
use gtk4 :: pango :: * ; use gtk4 :: pango :: * ;
use gtk4 :: pango :: Weight :: * ; use gtk4 :: pango :: Weight :: * ;
use gtk4 :: prelude :: * ; 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 ) > > , media_widget_pairs : RefCell < Vec < ( A :: Overview , Button ) > > ,
grid_widget : FlowBox , grid_widget : FlowBox ,
on_media_selected : & 'static dyn Fn ( A :: Overview ) ,
} }
impl < A : MediaAdapter > CollatedMediaGrid <A> { 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 ! ( let grid_widget = flow_box ! (
@ orientation : Horizontal ; @ orientation : Horizontal ;
@ homogeneous : true ; @ homogeneous : true ;
@ selection_mode : SelectionMode :: None ; @ selection_mode : SelectionMode :: None ;
) ; ) ;
let media_widget_pairs = RefCell :: new ( vec ! () ) ; 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 ) { 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 () ; let mut widgets = Vec :: new () ;
for media in media . as_slice () { 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 () ) ; self . media_widget_pairs . replace ( zip ( media , widgets ) . collect () ) ;
@ -46,36 +49,14 @@ impl < A : MediaAdapter > CollatedMediaGrid <A> {
} }
} }
pub fn set_sorting ( & self , sorting : A :: Sorting ) { async fn create_media_entry ( & self , media : & A :: Overview ) -> Button {
self . grid_widget . remove_all () ;
for ( _ , widget ) in self . sort_media_widget_pairs (sorting) {
self . grid_widget . append ( & widget ) ;
}
}
fn sort_media_widget_pairs ( & self , sorting : A :: Sorting ) -> Vec < ( A :: Overview , Button ) > {
let mut sorted = Vec :: from (
self . media_widget_pairs . borrow () . as_slice () ,
) ;
sorted . sort_by ( | ( media_1 , _ ) , ( media_2 , _ ) |
A :: compare_by ( media_1 , media_2 , sorting ) ,
) ;
// See it, say it, ...
sorted
}
}
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 ! ( button ! (
@ css_classes : & [ "flat" , "open-collection-item-button" ] ; @ css_classes : & [ "flat" , "open-collection-item-button" ] ;
@ connect_clicked : |_| todo ! () ; @ connect_clicked : clone ! (
# [strong] media ,
# [ strong ( rename_to = on_media_selected ) ] self . on_media_selected ,
move |_| on_media_selected ( media . clone () ) ,
) ;
& g_box ! ( & g_box ! (
@ option_children ; @ option_children ;
@ orientation : Vertical ; @ orientation : Vertical ;
@ -85,8 +66,8 @@ async fn create_media_entry < M : MediaOverview > ( media : & M ) -> Button {
match media . get_poster_file_path () { match media . get_poster_file_path () {
Some (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_file_path = PathBuf :: from (poster_file_path) ; // God forbid `Path` would work with `clone ! ()`
let poster_texture = spawn_blocking ( move || let poster_texture = spawn_blocking (
Texture :: from_filename (poster_file_path) . unwrap () move || Texture :: from_filename (poster_file_path) . unwrap () ,
) . await . unwrap () ; ) . await . unwrap () ;
Some ( image ! ( Some ( image ! (
@ paintable : & poster_texture ; @ paintable : & poster_texture ;
@ -132,3 +113,29 @@ async fn create_media_entry < M : MediaOverview > ( media : & M ) -> Button {
) , ) ,
) )
} }
pub fn set_sorting ( & self , sorting : A :: Sorting ) {
self . grid_widget . remove_all () ;
for ( _ , widget ) in self . sort_media_widget_pairs (sorting) {
self . grid_widget . append ( & widget ) ;
}
}
fn sort_media_widget_pairs ( & self , sorting : A :: Sorting ) -> Vec < ( A :: Overview , Button ) > {
let mut sorted = Vec :: from (
self . media_widget_pairs . borrow () . as_slice () ,
) ;
sorted . sort_by ( | ( media_1 , _ ) , ( media_2 , _ ) |
A :: compare_by ( media_1 , media_2 , sorting ) ,
) ;
// See it, say it, ...
sorted
}
}
impl < A : MediaAdapter > Component for CollatedMediaGrid <A> {
fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
}

View file

@ -67,8 +67,8 @@ pub struct CollatableMediaContainer < A : MediaAdapter + 'static > {
} }
impl < A : MediaAdapter > CollatableMediaContainer <A> { impl < A : MediaAdapter > CollatableMediaContainer <A> {
pub fn new () -> Self { pub fn new ( on_media_selected : impl Fn ( A :: Overview ) + 'static ) -> Self {
let collated_grid = leak ( CollatedMediaGrid :: new () ) ; let collated_grid = leak ( CollatedMediaGrid :: new (on_media_selected) ) ;
let collation_menu = MediaCollationMenu :: new :: <A> ( |sorting| let collation_menu = MediaCollationMenu :: new :: <A> ( |sorting|
collated_grid . set_sorting (sorting) , collated_grid . set_sorting (sorting) ,
) ; ) ;

View file

@ -6,41 +6,47 @@ use futures :: * ;
use gtk4 :: Orientation :: * ; use gtk4 :: Orientation :: * ;
use gtk4 :: prelude :: * ; use gtk4 :: prelude :: * ;
use libadwaita :: * ; use libadwaita :: * ;
use libadwaita :: prelude :: * ;
use libadwaita :: ViewSwitcherPolicy :: * ; use libadwaita :: ViewSwitcherPolicy :: * ;
use crate :: data_manager :: * ; use crate :: data_manager :: * ;
use crate :: ui :: collatable_container :: * ; use crate :: ui :: collatable_container :: * ;
use crate :: ui :: component :: * ; use crate :: ui :: component :: * ;
use crate :: ui :: utility :: * ; use crate :: ui :: utility :: * ;
use crate :: utility :: * ;
pub struct UI { pub struct UI {
window : ApplicationWindow , window : & 'static ApplicationWindow ,
films_component : CollatableMediaContainer <FilmsAdapter> , films_component : CollatableMediaContainer <FilmsAdapter> ,
series_component : CollatableMediaContainer <SeriesAdapter> , series_component : CollatableMediaContainer <SeriesAdapter> ,
} }
impl UI { impl UI {
pub fn new ( application : & Application ) -> UI { pub fn new ( application : & Application ) -> UI {
let films_component = CollatableMediaContainer :: <FilmsAdapter> :: new () ; let window = leak ( application_window ! (
let series_component = CollatableMediaContainer :: <SeriesAdapter> :: new () ; @ application : application ;
let switcher = view_stack ! ( @ title : "Zoödex" ;
) ) ;
let films_component = CollatableMediaContainer :: <FilmsAdapter> :: new (
|film| dialog ! () . present ( Some (window) ) ,
) ;
let series_component = CollatableMediaContainer :: <SeriesAdapter> :: new (
|series| dialog ! () . present ( Some (window) ) ,
) ;
let switched = view_stack ! (
( "Films" , "camera-video-symbolic" , films_component . get_widget () ) , ( "Films" , "camera-video-symbolic" , films_component . get_widget () ) ,
( "Series" , "video-display-symbolic" , series_component . get_widget () ) , ( "Series" , "video-display-symbolic" , series_component . get_widget () ) ,
) ; ) ;
let header_bar = header_bar ! ( let header_bar = header_bar ! (
& view_switcher ! ( @ policy : Wide ; & switcher ) , & view_switcher ! ( @ policy : Wide ; & switched ) ,
) ; ) ;
let window = application_window ! ( window . set_content ( Some (
@ application : application ; & toolbar_view ! ( @ top_bar : & header_bar ; & switched ) ,
@ title : "Zoödex" ; ) ) ;
& toolbar_view ! (
@ top_bar : & header_bar ;
& switcher ,
) ,
) ;
UI { window , films_component , series_component } UI { window , films_component , series_component }
} }

View file

@ -187,13 +187,13 @@ macro_rules ! bin { ( $ ( @ vexpand : $ vexpand : expr ; ) ? ) => { {
macro_rules ! application_window { ( macro_rules ! application_window { (
@ application : $ application : expr ; @ application : $ application : expr ;
@ title : $ title : expr ; @ title : $ title : expr ;
$ content : expr $ (,) ? $ ( $ content : expr $ (,) ? ) ?
) => { { ) => { {
use libadwaita :: prelude :: * ; use libadwaita :: prelude :: * ;
let window = libadwaita :: ApplicationWindow :: new ( $ application ) ; let window = libadwaita :: ApplicationWindow :: new ( $ application ) ;
window . set_title ( Some ( $ title ) ) ; window . set_title ( Some ( $ title ) ) ;
window . set_content ( Some ( $ content ) ) ; $ ( window . set_content ( Some ( $ content ) ) ; ) ?
window window
} } } } } }
@ -207,6 +207,11 @@ macro_rules ! toolbar_view { (
widget widget
} } } } } }
macro_rules ! dialog { () => { {
let widget = libadwaita :: Dialog :: new () ;
widget
} } }
macro_rules ! vertically_filling { ( $ child : expr ) => { macro_rules ! vertically_filling { ( $ child : expr ) => {
g_box ! ( g_box ! (
@ orientation : gtk4 :: Orientation :: Vertical ; @ orientation : gtk4 :: Orientation :: Vertical ;
@ -247,6 +252,7 @@ pub (crate) use {
bin , bin ,
application_window , application_window ,
toolbar_view , toolbar_view ,
dialog ,
vertically_filling , vertically_filling ,
pango_attributes , pango_attributes ,
} ; } ;