From 59ea1b9d955e8d0ae7d0fe4da1094022273fa13c Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Tue, 2 Feb 2021 10:25:34 +0100 Subject: [PATCH] Simplify new navigator window --- src/dialogs/preferences.rs | 15 +++++---------- src/macros.rs | 25 +++++++++++++++++++++++++ src/widgets/new_navigator_window.rs | 4 +++- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/dialogs/preferences.rs b/src/dialogs/preferences.rs index c9b7ac5..ad56120 100644 --- a/src/dialogs/preferences.rs +++ b/src/dialogs/preferences.rs @@ -59,10 +59,8 @@ impl Preferences { if let Some(path) = file.get_path() { this.music_library_path_row.set_subtitle(Some(path.to_str().unwrap())); - let context = glib::MainContext::default(); - let backend = this.backend.clone(); - context.spawn_local(async move { - backend.set_music_library_path(path).await.unwrap(); + spawn!(@clone this, async move { + this.backend.set_music_library_path(path).await.unwrap(); }); } } @@ -87,13 +85,10 @@ impl Preferences { login_button.connect_clicked(clone!(@strong this => move |_| { let window = NavigatorWindow::new(this.backend.clone()); window.set_transient_for(&this.window); - window.show(); - let context = glib::MainContext::default(); - let clone = this.clone(); - context.spawn_local(async move { - if let Some(data) = window.navigator.replace::<_, _, LoginDialog>(()).await { - clone.login_row.set_subtitle(Some(&data.username)); + spawn!(@clone this, async move { + if let Some(data) = replace!(window.navigator, LoginDialog).await { + this.login_row.set_subtitle(Some(&data.username)); } }); })); diff --git a/src/macros.rs b/src/macros.rs index a449224..d281714 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -23,6 +23,31 @@ macro_rules! push { }; } +/// Simplification for replacing the current navigator screen. +/// +/// This macro can be invoked in two forms. +/// +/// 1. To replace with screens without an input value: +/// +/// ``` +/// let result = replace!(navigator, ScreenType).await; +/// ``` +/// +/// 2. To replace with screens with an input value: +/// +/// ``` +/// let result = replace!(navigator, ScreenType, input).await; +/// ``` +#[macro_export] +macro_rules! replace { + ($navigator:expr, $screen:ty) => { + $navigator.replace::<_, _, $screen>(()) + }; + ($navigator:expr, $screen:ty, $input:ident) => { + $navigator.replace::<_, _, $screen>($input) + }; +} + /// Spawn a future on the GLib MainContext. /// /// This can be invoked in the following forms: diff --git a/src/widgets/new_navigator_window.rs b/src/widgets/new_navigator_window.rs index e35e66a..7548791 100644 --- a/src/widgets/new_navigator_window.rs +++ b/src/widgets/new_navigator_window.rs @@ -11,7 +11,7 @@ pub struct NavigatorWindow { } impl NavigatorWindow { - /// Create a new navigator window. + /// Create a new navigator window and show it. pub fn new(backend: Rc) -> Rc { let window = libadwaita::Window::new(); window.set_default_size(600, 424); @@ -25,6 +25,8 @@ impl NavigatorWindow { this.window.close(); })); + this.window.show(); + this }