Encapsulate window and its operations in struct

This commit is contained in:
Reinout Meliesie 2025-02-18 18:09:09 +01:00
parent 8bea2088a0
commit 90594514bd
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
2 changed files with 27 additions and 12 deletions

View file

@ -8,7 +8,7 @@ use gtk4 :: style_context_add_provider_for_display ;
use gtk4 :: STYLE_PROVIDER_PRIORITY_APPLICATION ;
use gtk4 :: gdk :: * ;
use gtk4 :: glib :: * ;
use libadwaita :: * ;
use libadwaita :: Application ;
use libadwaita :: prelude :: * ;
use crate :: data_manager :: * ;
@ -39,12 +39,7 @@ fn add_style_provider ( _ : & Application ) {
}
fn show_window ( application : & Application ) {
let window = leak (
ApplicationWindow :: builder ()
. application (application)
. title ("Zoödex")
. build ()
) ;
let window = leak ( Window :: new (application) ) ;
spawn_future_local ( async move {
async_result_context ! (
@ -57,7 +52,7 @@ fn show_window ( application : & Application ) {
data_manager . get_film_details (film_uuid) . await . unwrap ()
} ) ,
) ;
window . set_visible (true) ;
window . show () ;
let collection = data_manager . get_collection_overview () . await ? ;
ui . render_collection_overview (collection) . await ;

View file

@ -25,7 +25,7 @@ pub struct UI {
impl UI {
pub fn new (
window : & 'static ApplicationWindow ,
window : & 'static Window ,
get_film_details : impl Fn (String) -> BoxFuture < 'static , FilmDetails > + 'static ,
) -> UI {
let get_film_details = leak (get_film_details) ;
@ -54,12 +54,12 @@ impl UI {
& format ! ( "Release date: {}" , film_details . release_date ) ,
) ) . as_ref () ,
) ,
) . present ( Some (window) )
) . present ( Some ( & window . libadwaita_window ) )
} ) ;
} ) ;
let series_component = CollatableMediaContainer :: <SeriesAdapter> :: new (
|series| dialog ! () . present ( Some (window) ) ,
|series| dialog ! () . present ( Some ( & window . libadwaita_window ) ) ,
) ;
let switched = view_stack ! (
( "Films" , "camera-video-symbolic" , films_component . get_widget () ) ,
@ -69,7 +69,7 @@ impl UI {
& view_switcher ! ( @ policy : Wide ; & switched ) ,
) ;
window . set_content ( Some (
window . libadwaita_window . set_content ( Some (
& toolbar_view ! ( @ top_bar : & header_bar ; & switched ) ,
) ) ;
@ -83,3 +83,23 @@ impl UI {
) ;
}
}
pub struct Window {
libadwaita_window : ApplicationWindow ,
}
impl Window {
pub fn new (application : & Application ) -> Self {
let libadwaita_window = ApplicationWindow :: builder ()
. application (application)
. title ("Zoödex")
. build () ;
Self { libadwaita_window }
}
pub fn show ( & self ) { self . libadwaita_window . set_visible (true) }
pub fn close ( & self ) { self . libadwaita_window . close () }
}