Simplify async code

- Update Cargo.lock
- Use new gtk-core API
- Remove unneeded async
- Remove unneeded clones

Part-of: <https://gitlab.gnome.org/GNOME/loupe/-/merge_requests/310>
This commit is contained in:
Julian Hofer 2023-11-22 09:12:07 +01:00
parent 8d185e1e10
commit cd8188a1bb
10 changed files with 26 additions and 40 deletions

View file

@ -36,7 +36,6 @@ use crate::decoder::{self, tiling, Decoder, DecoderUpdate};
use crate::image_metadata::LpImageMetadata;
use crate::util::Gesture;
use crate::util::spawn;
use adw::{prelude::*, subclass::prelude::*};
use arc_swap::ArcSwap;
use futures::prelude::*;
@ -297,7 +296,7 @@ mod imp {
});
adw::StyleManager::default().connect_dark_notify(glib::clone!(@weak obj => move |_| {
spawn(async move {
glib::spawn_future_local(async move {
let color = obj.background_color_guess().await;
obj.set_background_color(color);
if obj.is_mapped() {
@ -778,7 +777,7 @@ impl LpImage {
let (decoder, mut decoder_update) = Decoder::new(file.clone(), tiles.clone()).await;
let weak_obj = self.downgrade();
spawn(async move {
glib::spawn_future_local(async move {
while let Some(update) = decoder_update.next().await {
if let Some(obj) = weak_obj.upgrade() {
obj.update(update);
@ -819,7 +818,7 @@ impl LpImage {
new_tiles
});
if imp.background_color.borrow().is_none() {
spawn(glib::clone!(@weak self as obj => async move {
glib::spawn_future_local(glib::clone!(@weak self as obj => async move {
let color = obj.background_color_guess().await;
obj.set_background_color(color);
if obj.is_mapped() {
@ -971,7 +970,7 @@ impl LpImage {
log::debug!("Image got replaced {}", file.uri());
let obj = self.clone();
// TODO: error handling is missing
spawn(async move {
glib::spawn_future_local(async move {
obj.load(&file).await;
});
}
@ -982,7 +981,7 @@ impl LpImage {
let file = file_a.clone();
log::debug!("Image was edited {}", file.uri());
// TODO: error handling is missing
spawn(async move {
glib::spawn_future_local(async move {
obj.load(&file).await;
});
}

View file

@ -23,7 +23,6 @@
use crate::deps::*;
use crate::widgets::LpImage;
use crate::util::spawn;
use adw::subclass::prelude::*;
use glib::clone;
use gtk::prelude::*;
@ -161,7 +160,7 @@ impl LpImagePage {
obj.imp().image.set_file(file);
spawn(clone!(@weak obj, @strong file => async move {
glib::spawn_future_local(clone!(@weak obj, @strong file => async move {
let imp = obj.imp();
imp.image.load(&file).await;
}));

View file

@ -35,7 +35,6 @@ use crate::util::gettext::*;
use crate::util::{Direction, Position};
use crate::widgets::{LpImage, LpImagePage, LpPrint, LpSlidingView};
use crate::util::spawn;
use adw::prelude::*;
use adw::subclass::prelude::*;
use anyhow::Context;
@ -306,7 +305,7 @@ impl LpImageView {
// List other files in directory
if let Some(directory) = directory {
spawn(glib::clone!(@weak self as obj, @strong file => async move {
glib::spawn_future_local(glib::clone!(@weak self as obj => async move {
if let Err(err) = obj.model().load_directory(directory.clone()).await {
log::warn!("Failed to load directory: {}", err.root_cause());
obj.activate_action("win.show-toast", Some(&(format!("{} {}", err, err.root_cause()), adw::ToastPriority::High.into_glib()).to_variant()))

View file

@ -272,12 +272,12 @@ mod imp {
Self::bind_template(klass);
Self::Type::bind_template_callbacks(klass);
klass.install_action_async("print.print", None, |print, _, _| async move {
klass.install_action("print.print", None, |print, _, _| {
print.print();
});
klass.install_action_async("print.back", None, |print, _, _| async move {
print.back().await;
klass.install_action("print.back", None, |print, _, _| {
print.back();
});
klass.install_property_action("print.orientation", "orientation");
@ -813,7 +813,7 @@ impl LpPrint {
}
/// Go back to print dialog from image layout
async fn back(&self) {
fn back(&self) {
self.close();
let print = Self::new(
self.image(),

View file

@ -29,8 +29,6 @@ use futures::future::{AbortHandle, Abortable};
use once_cell::sync::Lazy;
use std::cell::RefCell;
use crate::util::spawn;
use crate::image_metadata::LpImageMetadata;
use crate::widgets::image::LpImage;
@ -236,7 +234,7 @@ impl LpPropertiesView {
imp.info_handle.replace(Some(handle));
// ...then spawn the future.
spawn(async {
glib::spawn_future_local(async {
let _ = fut.await;
});
}