Introduce image UI macro, require explicit borrowing in UI macros

This commit is contained in:
Reinout Meliesie 2025-02-07 13:36:14 +01:00
parent 0dffdbc660
commit 38adb5f4b9
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
7 changed files with 88 additions and 75 deletions

View file

@ -12,7 +12,7 @@ pub struct ApplicationHeaderBar { widget : HeaderBar }
impl ApplicationHeaderBar { impl ApplicationHeaderBar {
pub fn new ( collection_view_stack : & CollectionViewStack ) -> Self { pub fn new ( collection_view_stack : & CollectionViewStack ) -> Self {
let widget = header_bar ! ( let widget = header_bar ! (
view_switcher ! ( @ policy : Wide ; collection_view_stack . get_widget () ) , & view_switcher ! ( @ policy : Wide ; collection_view_stack . get_widget () ) ,
) ; ) ;
Self { widget } Self { widget }
} }

View file

@ -156,11 +156,11 @@ async fn create_collection_item (
poster_file_path : Option < & Path > , poster_file_path : Option < & Path > ,
details_widget : & Box , details_widget : & Box ,
) -> Box { ) -> Box {
let container = Box :: builder () let container = g_box ! (
. orientation (Vertical) @ orientation : Vertical ;
. margin_top (20) @ margin_top : 20 ;
. margin_bottom (20) @ margin_bottom : 20 ;
. build () ; ) ;
if let Some (poster_file_path) = poster_file_path { if let Some (poster_file_path) = 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 ! ()`
@ -169,12 +169,12 @@ async fn create_collection_item (
) . await . unwrap () ; ) . await . unwrap () ;
container . append ( container . append (
& Image :: builder () & image ! (
. paintable ( & poster_texture ) @ paintable : & poster_texture ;
. width_request (300) @ width_request : 300 ;
. height_request (300) @ height_request : 300 ;
. margin_bottom (10) @ margin_bottom : 10 ;
. build () )
) ; ) ;
} }
@ -209,8 +209,8 @@ fn create_film_details ( film : & FilmOverview ) -> Box {
@ orientation : Horizontal ; @ orientation : Horizontal ;
@ halign : Center ; @ halign : Center ;
@ spacing : 20 ; @ spacing : 20 ;
label ! ( film . release_date . as_str () ) , & label ! ( film . release_date . as_str () ) ,
label ! ( format ! ( "{}m" , film . runtime_minutes ) . as_str () ) , & label ! ( format ! ( "{}m" , film . runtime_minutes ) . as_str () ) ,
) )
} }
fn create_series_details ( series : & SeriesOverview ) -> Box { fn create_series_details ( series : & SeriesOverview ) -> Box {
@ -218,6 +218,6 @@ fn create_series_details ( series : & SeriesOverview ) -> Box {
@ orientation : Horizontal ; @ orientation : Horizontal ;
@ halign : Center ; @ halign : Center ;
@ spacing : 20 ; @ spacing : 20 ;
label ! ( series . first_release_date . as_str () ) , & label ! ( series . first_release_date . as_str () ) ,
) )
} }

View file

@ -25,8 +25,8 @@ impl FilmCollationMenu {
@ halign : Center ; @ halign : Center ;
@ spacing : 20 ; @ spacing : 20 ;
@ widget_name : "film-collation-menu" ; @ widget_name : "film-collation-menu" ;
@ css_classes : [ "toolbar" ] ; @ css_classes : & [ "toolbar" ] ;
* film_sort_button . get_widget () , film_sort_button . get_widget () ,
) ; ) ;
Self { widget } Self { widget }
@ -41,8 +41,8 @@ impl SeriesCollationMenu {
@ halign : Center ; @ halign : Center ;
@ spacing : 20 ; @ spacing : 20 ;
@ widget_name : "series-collation-menu" ; @ widget_name : "series-collation-menu" ;
@ css_classes : [ "toolbar" ] ; @ css_classes : & [ "toolbar" ] ;
* series_sort_button . get_widget () , series_sort_button . get_widget () ,
) ; ) ;
Self { widget } Self { widget }

View file

@ -25,36 +25,36 @@ impl FilmSortButton {
let previous_sorting = leak ( RefCell :: new ( FilmsSorting :: default () ) ) ; let previous_sorting = leak ( RefCell :: new ( FilmsSorting :: default () ) ) ;
let sort_icons = leak ( [ let sort_icons = leak ( [
icon ! ("view-sort-ascending-symbolic") , image ! ( @ icon_name : "view-sort-ascending-symbolic" ; ) ,
icon ! ("view-sort-ascending-symbolic") , image ! ( @ icon_name : "view-sort-ascending-symbolic" ; ) ,
icon ! ("view-sort-ascending-symbolic") , image ! ( @ icon_name : "view-sort-ascending-symbolic" ; ) ,
] ) ; ] ) ;
let widget = split_button ! ( let widget = split_button ! (
@ popover : popover ! ( @ popover : & popover ! (
@ css_classes : [ "menu" ] ; @ css_classes : & [ "menu" ] ;
list_box ! ( & list_box ! (
@ connect_row_activated : move | _ , row | { @ connect_row_activated : move | _ , row | {
on_film_sort_activated ( row , previous_sorting , & on_sort , sort_icons ) ; on_film_sort_activated ( row , previous_sorting , & on_sort , sort_icons ) ;
} ; } ;
g_box ! ( & g_box ! (
@ orientation : Horizontal ; @ spacing : 20 ; @ orientation : Horizontal ; @ spacing : 20 ;
label ! ( @ hexpand : true ; @ halign : Start ; "Name" ) , & label ! ( @ hexpand : true ; @ halign : Start ; "Name" ) ,
sort_icons [0] , & sort_icons [0] ,
) , ) ,
g_box ! ( & g_box ! (
@ orientation : Horizontal ; @ spacing : 20 ; @ orientation : Horizontal ; @ spacing : 20 ;
label ! ( @ hexpand : true ; @ halign : Start ; "Release date" ) , & label ! ( @ hexpand : true ; @ halign : Start ; "Release date" ) ,
sort_icons [1] , & sort_icons [1] ,
) , ) ,
g_box ! ( & g_box ! (
@ orientation : Horizontal ; @ spacing : 20 ; @ orientation : Horizontal ; @ spacing : 20 ;
label ! ( @ hexpand : true ; @ halign : Start ; "Runtime" ) , & label ! ( @ hexpand : true ; @ halign : Start ; "Runtime" ) ,
sort_icons [2] , & sort_icons [2] ,
) , ) ,
) , ) ,
) ; ) ;
label ! ("Sort") , & label ! ("Sort") ,
) ; ) ;
Self { widget , previous_sorting } Self { widget , previous_sorting }
@ -65,30 +65,29 @@ impl SeriesSortButton {
let previous_sorting = leak ( RefCell :: new ( SeriesSorting :: default () ) ) ; let previous_sorting = leak ( RefCell :: new ( SeriesSorting :: default () ) ) ;
let sort_icons = leak ( [ let sort_icons = leak ( [
icon ! ("view-sort-ascending-symbolic") , image ! ( @ icon_name : "view-sort-ascending-symbolic" ; ) ,
icon ! ("view-sort-ascending-symbolic") , image ! ( @ icon_name : "view-sort-ascending-symbolic" ; ) ,
] ) ; ] ) ;
let widget = split_button ! ( let widget = split_button ! (
@ popover : popover ! ( @ popover : & popover ! (
@ css_classes : [ "menu" ] ; @ css_classes : & [ "menu" ] ;
list_box ! ( & list_box ! (
@ connect_row_activated : move | _ , row | { @ connect_row_activated : move | _ , row |
on_series_sort_activated ( row , previous_sorting , & on_sort , sort_icons ) ; on_series_sort_activated ( row , previous_sorting , & on_sort , sort_icons ) ;
} ; & g_box ! (
g_box ! (
@ orientation : Horizontal ; @ spacing : 20 ; @ orientation : Horizontal ; @ spacing : 20 ;
label ! ( @ hexpand : true ; @ halign : Start ; "Name" ) , & label ! ( @ hexpand : true ; @ halign : Start ; "Name" ) ,
sort_icons [0] , & sort_icons [0] ,
) , ) ,
g_box ! ( & g_box ! (
@ orientation : Horizontal ; @ spacing : 20 ; @ orientation : Horizontal ; @ spacing : 20 ;
label ! ( @ hexpand : true ; @ halign : Start ; "First release date" ) , & label ! ( @ hexpand : true ; @ halign : Start ; "First release date" ) ,
sort_icons [1] , & sort_icons [1] ,
) , ) ,
) , ) ,
) ; ) ;
label ! ("Sort") , & label ! ("Sort") ,
) ; ) ;
Self { widget , previous_sorting } Self { widget , previous_sorting }

View file

@ -70,10 +70,10 @@ impl CollatableFilmsContainer {
let widget = g_box ! ( let widget = g_box ! (
@ orientation : Vertical ; @ orientation : Vertical ;
* film_collation_menu . get_widget () , film_collation_menu . get_widget () ,
scrolled_window ! ( & scrolled_window ! (
@ propagate_natural_height : true ; @ propagate_natural_height : true ;
vertically_filling ! ( * collated_grid . get_widget () ) , & vertically_filling ! ( collated_grid . get_widget () ) ,
) , ) ,
) ; ) ;
@ -92,10 +92,10 @@ impl CollatableSeriesContainer {
let widget = g_box ! ( let widget = g_box ! (
@ orientation : Vertical ; @ orientation : Vertical ;
* series_collation_menu . get_widget () , series_collation_menu . get_widget () ,
scrolled_window ! ( & scrolled_window ! (
@ propagate_natural_height : true ; @ propagate_natural_height : true ;
vertically_filling ! ( * collated_grid . get_widget () ) , & vertically_filling ! ( collated_grid . get_widget () ) ,
) , ) ,
) ; ) ;

View file

@ -34,10 +34,10 @@ impl UI {
let window = application_window ! ( let window = application_window ! (
@ application : application ; @ application : application ;
@ title : "Zoödex" ; @ title : "Zoödex" ;
g_box ! ( & g_box ! (
@ orientation : Vertical ; @ orientation : Vertical ;
* header_bar . get_widget () , header_bar . get_widget () ,
* switch_component . get_widget () , switch_component . get_widget () ,
) , ) ,
) ; ) ;

View file

@ -32,9 +32,9 @@ macro_rules ! g_box { (
$ ( container . set_spacing ( $ spacing ) ; ) ? $ ( container . set_spacing ( $ spacing ) ; ) ?
$ ( container . set_margin_top ( $ margin_top ) ; ) ? $ ( container . set_margin_top ( $ margin_top ) ; ) ?
$ ( container . set_margin_bottom ( $ margin_bottom ) ; ) ? $ ( container . set_margin_bottom ( $ margin_bottom ) ; ) ?
$ ( container . set_widget_name ( & $ widget_name ) ; ) ? $ ( container . set_widget_name ( $ widget_name ) ; ) ?
$ ( container . set_css_classes ( & $ css_classes ) ; ) ? $ ( container . set_css_classes ( $ css_classes ) ; ) ?
$ ( container . append ( & $ child ) ; ) * $ ( container . append ( $ child ) ; ) *
container container
} } } } } }
@ -52,7 +52,7 @@ macro_rules ! list_box { (
) => { { ) => { {
let container = gtk4 :: ListBox :: new () ; let container = gtk4 :: ListBox :: new () ;
$ ( container . connect_row_activated ( $ connect_row_activated ) ; ) ? $ ( container . connect_row_activated ( $ connect_row_activated ) ; ) ?
$ ( container . append ( & $ child ) ; ) * $ ( container . append ( $ child ) ; ) *
container container
} } } } } }
@ -61,8 +61,8 @@ macro_rules ! split_button { (
$ child : expr $ (,) ? $ child : expr $ (,) ?
) => { { ) => { {
let widget = libadwaita :: SplitButton :: new () ; let widget = libadwaita :: SplitButton :: new () ;
$ ( widget . set_popover ( Some ( & $ popover ) ) ; ) ? $ ( widget . set_popover ( Some ( $ popover ) ) ; ) ?
widget . set_child ( Some ( & $ child ) ) ; widget . set_child ( Some ( $ child ) ) ;
widget widget
} } } } } }
@ -71,14 +71,28 @@ macro_rules ! popover { (
$ child : expr $ (,) ? $ child : expr $ (,) ?
) => { { ) => { {
let widget = gtk4 :: Popover :: new () ; let widget = gtk4 :: Popover :: new () ;
$ ( widget . set_css_classes ( & $ css_classes ) ; ) ? $ ( widget . set_css_classes ( $ css_classes ) ; ) ?
widget . set_child ( Some ( & $ child ) ) ; widget . set_child ( Some ( $ child ) ) ;
widget widget
} } } } } }
macro_rules ! icon { ( $ icon_name : expr ) => { macro_rules ! image { (
gtk4 :: Image :: from_icon_name ( $ icon_name ) $ ( @ icon_name : $ icon_name : expr ; ) ?
} } $ ( @ paintable : $ paintable : expr ; ) ?
$ ( @ width_request : $ width_request : expr ; ) ?
$ ( @ height_request : $ height_request : expr ; ) ?
$ ( @ margin_top : $ margin_top : expr ; ) ?
$ ( @ margin_bottom : $ margin_bottom : expr ; ) ?
) => { {
let image = gtk4 :: Image :: new () ;
$ ( image . set_icon_name ( Some ( $ icon_name ) ) ; ) ?
$ ( image . set_paintable ( Some ( $ paintable ) ) ; ) ?
$ ( image . set_width_request ( $ width_request ) ; ) ?
$ ( image . set_height_request ( $ height_request ) ; ) ?
$ ( image . set_margin_top ( $ margin_top ) ; ) ?
$ ( image . set_margin_bottom ( $ margin_bottom ) ; ) ?
image
} } }
macro_rules ! scrolled_window { ( macro_rules ! scrolled_window { (
$ ( @ propagate_natural_height : $ propagate_natural_height : expr ; ) ? $ ( @ propagate_natural_height : $ propagate_natural_height : expr ; ) ?
@ -86,7 +100,7 @@ macro_rules ! scrolled_window { (
) => { { ) => { {
let widget = gtk4 :: ScrolledWindow :: new () ; let widget = gtk4 :: ScrolledWindow :: new () ;
$ ( widget . set_propagate_natural_height ( $ propagate_natural_height ) ; ) ? $ ( widget . set_propagate_natural_height ( $ propagate_natural_height ) ; ) ?
widget . set_child ( Some ( & $ child ) ) ; widget . set_child ( Some ( $ child ) ) ;
widget widget
} } } } } }
@ -108,13 +122,13 @@ macro_rules ! view_switcher { (
) => { { ) => { {
let widget = libadwaita :: ViewSwitcher :: new () ; let widget = libadwaita :: ViewSwitcher :: new () ;
$ ( widget . set_policy ( $ policy ) ; ) ? $ ( widget . set_policy ( $ policy ) ; ) ?
widget . set_stack ( Some ( & $ stack ) ) ; widget . set_stack ( Some ( $ stack ) ) ;
widget widget
} } } } } }
macro_rules ! header_bar { ( $ title_widget : expr $ (,) ? ) => { { macro_rules ! header_bar { ( $ title_widget : expr $ (,) ? ) => { {
let widget = libadwaita :: HeaderBar :: new () ; let widget = libadwaita :: HeaderBar :: new () ;
widget . set_title_widget ( Some ( & $ title_widget ) ) ; widget . set_title_widget ( Some ( $ title_widget ) ) ;
widget widget
} } } } } }
@ -133,7 +147,7 @@ macro_rules ! application_window { (
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
} } } } } }
@ -141,7 +155,7 @@ macro_rules ! vertically_filling { ( $ child : expr ) => {
g_box ! ( g_box ! (
@ orientation : gtk4 :: Orientation :: Vertical ; @ orientation : gtk4 :: Orientation :: Vertical ;
$ child , $ child ,
bin ! ( @ vexpand : true ; ) , & bin ! ( @ vexpand : true ; ) ,
) )
} } } }
@ -168,7 +182,7 @@ pub (crate) use {
list_box , list_box ,
split_button , split_button ,
popover , popover ,
icon , image ,
scrolled_window , scrolled_window ,
flow_box , flow_box ,
view_switcher , view_switcher ,