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:
parent
8d185e1e10
commit
cd8188a1bb
10 changed files with 26 additions and 40 deletions
|
|
@ -20,11 +20,9 @@
|
|||
|
||||
use crate::about;
|
||||
use crate::deps::*;
|
||||
use crate::util::spawn;
|
||||
|
||||
use adw::prelude::*;
|
||||
use adw::subclass::prelude::*;
|
||||
use glib::clone;
|
||||
|
||||
use crate::config;
|
||||
use crate::window::LpWindow;
|
||||
|
|
@ -114,7 +112,7 @@ impl LpApplication {
|
|||
gio::ActionEntryBuilder::new("about")
|
||||
.activate(|app: &Self, _, _| {
|
||||
let app = app.clone();
|
||||
spawn(async move { app.show_about().await });
|
||||
glib::spawn_future_local(async move { app.show_about().await });
|
||||
})
|
||||
.build(),
|
||||
gio::ActionEntryBuilder::new("help")
|
||||
|
|
@ -199,14 +197,15 @@ impl LpApplication {
|
|||
}
|
||||
|
||||
pub fn show_help(&self) {
|
||||
spawn(clone!(@weak self as app => async move {
|
||||
let context = app
|
||||
.active_window()
|
||||
.map(|w| gtk::prelude::WidgetExt::display(&w).app_launch_context());
|
||||
|
||||
if let Err(e) = gio::AppInfo::launch_default_for_uri_future("help:loupe", context.as_ref()).await {
|
||||
let context = self
|
||||
.active_window()
|
||||
.map(|w| gtk::prelude::WidgetExt::display(&w).app_launch_context());
|
||||
glib::spawn_future_local(async move {
|
||||
if let Err(e) =
|
||||
gio::AppInfo::launch_default_for_uri_future("help:loupe", context.as_ref()).await
|
||||
{
|
||||
log::error!("Failed to launch help: {}", e.message());
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ impl UpdateSender {
|
|||
F: std::future::Future<Output = Result<(), glycin::Error>> + Send + 'static,
|
||||
{
|
||||
let update_sender = self.clone();
|
||||
glib::MainContext::default().spawn(async move {
|
||||
glib::spawn_future(async move {
|
||||
let update_sender = update_sender.clone();
|
||||
|
||||
let result: Result<(), glycin::Error> = f.await;
|
||||
|
|
|
|||
|
|
@ -247,10 +247,3 @@ pub fn contrast_ratio(color1: &gdk::RGBA, color2: &gdk::RGBA) -> f32 {
|
|||
|
||||
(l1 + 0.05) / (l2 + 0.05)
|
||||
}
|
||||
|
||||
pub fn spawn<R: 'static, F: std::future::Future<Output = R> + 'static>(
|
||||
f: F,
|
||||
) -> glib::JoinHandle<R> {
|
||||
let ctx = glib::MainContext::default();
|
||||
ctx.spawn_local(f)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -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()))
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@
|
|||
use crate::deps::*;
|
||||
use crate::util::gettext::*;
|
||||
|
||||
use crate::util::spawn;
|
||||
use adw::prelude::*;
|
||||
use adw::subclass::prelude::*;
|
||||
use glib::clone;
|
||||
|
|
@ -595,10 +594,10 @@ impl LpWindow {
|
|||
.build();
|
||||
toast.connect_button_clicked(glib::clone!(@weak self as win => move |_| {
|
||||
let path = path.clone();
|
||||
spawn(async move {
|
||||
glib::spawn_future_local(async move {
|
||||
let result = crate::util::untrash(&path).await;
|
||||
match result {
|
||||
Ok(()) => win.image_view().set_images_from_files(vec![gio::File::for_path(&path)]),
|
||||
Ok(()) => win.image_view().set_images_from_files(Vec::from([gio::File::for_path(&path)])),
|
||||
Err(err) => {
|
||||
log::error!("Failed to untrash {path:?}: {err}");
|
||||
win.show_toast(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue