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 {
pub fn new ( collection_view_stack : & CollectionViewStack ) -> Self {
let widget = header_bar ! (
view_switcher ! ( @ policy : Wide ; collection_view_stack . get_widget () ) ,
& view_switcher ! ( @ policy : Wide ; collection_view_stack . get_widget () ) ,
) ;
Self { widget }
}

View file

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

View file

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

View file

@ -70,10 +70,10 @@ impl CollatableFilmsContainer {
let widget = g_box ! (
@ orientation : Vertical ;
* film_collation_menu . get_widget () ,
scrolled_window ! (
film_collation_menu . get_widget () ,
& scrolled_window ! (
@ 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 ! (
@ orientation : Vertical ;
* series_collation_menu . get_widget () ,
scrolled_window ! (
series_collation_menu . get_widget () ,
& scrolled_window ! (
@ 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 ! (
@ application : application ;
@ title : "Zoödex" ;
g_box ! (
& g_box ! (
@ orientation : Vertical ;
* header_bar . get_widget () ,
* switch_component . get_widget () ,
header_bar . get_widget () ,
switch_component . get_widget () ,
) ,
) ;

View file

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