From 7b7435f50a1d2885e88d2193ee794c42e8851ba2 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Sun, 22 Jan 2023 12:18:43 -0500 Subject: [PATCH] general: Use `spawn!` less Because widgets can install async actions, we can simply turn our callbacks into async functions. --- src/widgets/image_view.rs | 57 ++++++++++++++++++--------------------- src/window.rs | 8 +++--- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/widgets/image_view.rs b/src/widgets/image_view.rs index ac4d0a3..de03154 100644 --- a/src/widgets/image_view.rs +++ b/src/widgets/image_view.rs @@ -534,42 +534,37 @@ impl LpImageView { } } - pub fn set_background(&self) -> anyhow::Result<()> { + pub async fn set_background(&self) -> anyhow::Result<()> { let background = self.uri().context("No URI for current file")?; - spawn!(clone!(@weak self as view => - async move { - let id = WindowIdentifier::from_native( - &view.native().expect("View should have a GtkNative"), - ) - .await; + let native = self.native().expect("View should have a GtkNative"); + let id = WindowIdentifier::from_native(&native).await; - match wallpaper::WallpaperRequest::default() - .set_on(wallpaper::SetOn::Background) - .show_preview(true) - .identifier(id) - .build_uri(&background) - .await - { - // We use `1` here because we can't pass enums directly as GVariants, - // so we need to use the C int value of the enum. - // `TOAST_PRIORITY_NORMAL = 0`, and `TOAST_PRIORITY_HIGH = 1` - Ok(_) => view - .activate_action( + match wallpaper::WallpaperRequest::default() + .set_on(wallpaper::SetOn::Background) + .show_preview(true) + .identifier(id) + .build_uri(&background) + .await + { + // We use `1` here because we can't pass enums directly as GVariants, + // so we need to use the C int value of the enum. + // `TOAST_PRIORITY_NORMAL = 0`, and `TOAST_PRIORITY_HIGH = 1` + Ok(_) => self + .activate_action( + "win.show-toast", + Some(&(i18n("Set as background."), 1).to_variant()), + ) + .unwrap(), + Err(err) => { + if !matches!(err, Error::Response(ResponseError::Cancelled)) { + self.activate_action( "win.show-toast", - Some(&(i18n("Set as background."), 1).to_variant()), + Some(&(i18n("Could not set background."), 1).to_variant()), ) - .unwrap(), - Err(err) => { - if !matches!(err, Error::Response(ResponseError::Cancelled)) { - view.activate_action( - "win.show-toast", - Some(&(i18n("Could not set background."), 1).to_variant()), - ) - .unwrap(); - } + .unwrap(); } - }; - })); + } + }; Ok(()) } diff --git a/src/window.rs b/src/window.rs index 2763f47..356d0e7 100644 --- a/src/window.rs +++ b/src/window.rs @@ -146,8 +146,8 @@ mod imp { win.rotate_image(angle.unwrap().get().unwrap()); }); - klass.install_action("win.set-background", None, move |win, _, _| { - win.set_background(); + klass.install_action_async("win.set-background", None, |win, _, _| async move { + win.set_background().await; }); klass.install_action("win.print", None, move |win, _, _| { @@ -397,10 +397,10 @@ impl LpWindow { self.imp().image_view.rotate_image(angle) } - fn set_background(&self) { + async fn set_background(&self) { let imp = self.imp(); - if let Err(e) = imp.image_view.set_background() { + if let Err(e) = imp.image_view.set_background().await { log::error!("Failed to set background: {}", e); } }