general: Use spawn! less

Because widgets can install async actions, we can simply
turn our callbacks into async functions.
This commit is contained in:
Christopher Davis 2023-01-22 12:18:43 -05:00 • committed by Sophie Herold
parent 6e632e7281
commit 7b7435f50a
2 changed files with 30 additions and 35 deletions

View file

@ -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(())
}

View file

@ -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);
}
}