From d99215bfd1564d3871e8bf6e7c00306a9bcb3c20 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Wed, 3 Aug 2022 19:32:02 -0400 Subject: [PATCH] general: Use gtk_macros::spawn!() Makes the code a tad cleaner because we're not manually getting `glib::MainContext` every time we want to spawn a future. --- src/widgets/image_page.rs | 5 +++-- src/widgets/image_view.rs | 5 +++-- src/widgets/properties_view.rs | 12 +++++------- src/window.rs | 5 +++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/widgets/image_page.rs b/src/widgets/image_page.rs index ef04ba7..088fb93 100644 --- a/src/widgets/image_page.rs +++ b/src/widgets/image_page.rs @@ -25,6 +25,8 @@ use gtk::prelude::*; use gtk::subclass::prelude::*; use gtk::CompositeTemplate; +use gtk_macros::spawn; + use once_cell::sync::OnceCell; use crate::widgets::LpImage; @@ -106,8 +108,7 @@ impl LpImagePage { // via the file chooser portal. I'm not sure how to make this work. gtk::RecentManager::default().add_item(&file.uri()); - let ctx = glib::MainContext::default(); - ctx.spawn_local(clone!(@weak obj, @weak file => async move { + spawn!(clone!(@weak obj, @weak file => async move { let imp = obj.imp(); match load_texture_from_file(&file).await { Ok(texture) => { diff --git a/src/widgets/image_view.rs b/src/widgets/image_view.rs index 67eb106..8e4c3aa 100644 --- a/src/widgets/image_view.rs +++ b/src/widgets/image_view.rs @@ -31,6 +31,8 @@ use ashpd::WindowIdentifier; use once_cell::sync::Lazy; use std::cell::{Cell, RefCell}; +use gtk_macros::spawn; + use crate::file_model::LpFileModel; use crate::thumbnail::Thumbnail; use crate::widgets::LpImagePage; @@ -360,8 +362,7 @@ impl LpImageView { pub fn set_wallpaper(&self) -> anyhow::Result<()> { let wallpaper = self.uri().context("No URI for current file")?; - let ctx = glib::MainContext::default(); - ctx.spawn_local(clone!(@weak self as view => async move { + spawn!(clone!(@weak self as view => async move { let id = WindowIdentifier::from_native( &view.native().expect("View should have a GtkNative"), ) diff --git a/src/widgets/properties_view.rs b/src/widgets/properties_view.rs index e0ec37b..b402f19 100644 --- a/src/widgets/properties_view.rs +++ b/src/widgets/properties_view.rs @@ -33,6 +33,8 @@ use futures::future::{AbortHandle, Abortable}; use once_cell::sync::Lazy; use std::cell::RefCell; +use gtk_macros::spawn; + const MB: f64 = 1_000_000.0; const KB: f64 = 1_000.0; const FALLBACK: &str = "-"; @@ -186,7 +188,6 @@ impl LpPropertiesView { fn build_file_info(&self, file: &gio::File) { let imp = self.imp(); - let ctx = glib::MainContext::default(); // We need to be able to cancel this future so that // changing files before the metadata loads does not cause @@ -222,7 +223,7 @@ impl LpPropertiesView { imp.info_handle.replace(Some(handle)); // ...then spawn the future. - ctx.spawn_local(async { + spawn!(async { let _ = fut.await; }); } @@ -239,7 +240,6 @@ impl LpPropertiesView { fn build_dimensions(&self, info: &gio::FileInfo, file: &gio::File) { let imp = self.imp(); - let ctx = glib::MainContext::default(); // Here we do the same thing we do for the rest of the file info. let (handle, reg) = AbortHandle::new_pair(); @@ -281,7 +281,7 @@ impl LpPropertiesView { imp.dimensions_handle.replace(Some(handle)); - ctx.spawn_local(async { + spawn!(async { let _ = fut.await; }); } @@ -298,9 +298,7 @@ impl LpPropertiesView { .and_then(|f| f.peek_path()) .and_then(|p| std::fs::File::open(p).ok()) { - let ctx = glib::MainContext::default(); - - ctx.spawn_local(clone!(@weak self as view => async move { + spawn!(clone!(@weak self as view => async move { let id = WindowIdentifier::from_native(&view.native().expect("No GtkNative for view")).await; if let Err(e) = open_uri::open_directory(&id, &directory).await { log::error!("Could not open parent directory: {e}"); diff --git a/src/window.rs b/src/window.rs index 71e7a57..85f4fa2 100644 --- a/src/window.rs +++ b/src/window.rs @@ -28,6 +28,8 @@ use gtk::CompositeTemplate; use ashpd::desktop::open_uri; use ashpd::WindowIdentifier; +use gtk_macros::spawn; + use crate::config; use crate::util; use crate::widgets::{LpImageView, LpPropertiesView}; @@ -253,7 +255,6 @@ impl LpWindow { fn open_with(&self) { let imp = self.imp(); - let ctx = glib::MainContext::default(); if let Some(file) = imp .image_view @@ -261,7 +262,7 @@ impl LpWindow { .and_then(|f| f.peek_path()) .and_then(|p| std::fs::File::open(p).ok()) { - ctx.spawn_local(clone!(@weak self as win => async move { + spawn!(clone!(@weak self as win => async move { if let Err(e) = open_uri::open_file(&WindowIdentifier::from_native(&win).await, &file, true, true).await {