From eae81ebc60f1e45805fef9f64eb916c22c9d64b0 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Sun, 15 Jan 2023 19:18:16 -0500 Subject: [PATCH] window: Use GtkFileLauncher GtkFileLauncher handles using the portal when appropriate. --- src/window.rs | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/window.rs b/src/window.rs index 1d80769..2763f47 100644 --- a/src/window.rs +++ b/src/window.rs @@ -25,11 +25,6 @@ use glib::clone; use gtk::prelude::*; use gtk::CompositeTemplate; -use ashpd::desktop::open_uri; -use ashpd::WindowIdentifier; - -use gtk_macros::spawn; - use std::cell::RefCell; use crate::config; @@ -143,8 +138,8 @@ mod imp { win.pick_file().await; }); - klass.install_action("win.open-with", None, move |win, _, _| { - win.open_with(); + klass.install_action_async("win.open-with", None, |win, _, _| async move { + win.open_with().await; }); klass.install_action("win.rotate", Some("d"), move |win, _, angle| { @@ -383,26 +378,16 @@ impl LpWindow { } } - fn open_with(&self) { + async fn open_with(&self) { let imp = self.imp(); - if let Some(file) = imp - .image_view - .active_file() - .and_then(|f| f.peek_path()) - .and_then(|p| std::fs::File::open(p).ok()) - { - spawn!(clone!(@weak self as win => async move { - if let Err(e) = - open_uri::OpenFileRequest::default() - .ask(true) - .writeable(true) - .identifier(WindowIdentifier::from_native(&win).await) - .build_file(&file).await - { + if let Some(ref file) = imp.image_view.active_file() { + let launcher = gtk::FileLauncher::new(Some(file)); + if let Err(e) = launcher.launch_future(Some(self)).await { + if !e.matches(gtk::DialogError::Dismissed) { log::error!("Could not open image in external program: {}", e); } - })); + } } else { log::error!("Could not load a path for the current image.") }