tilo-dialogs 0.5.1

File choosers, opening files with their app, and opening URIs on Linux, macOS and the web.
Documentation
//! Original-caller ownership for asynchronous desktop URI effects.
use super::EffectCheck;
use std::sync::mpsc::Receiver;

pub fn open_uri_scoped(uri: String, scope: impl EffectCheck) -> Receiver<Result<(), String>> {
    let (sender, receiver) = std::sync::mpsc::channel();
    #[cfg(any(test, feature = "test-support"))]
    let backend = SCOPED_URI_FIXTURE.with(|current| current.borrow().clone());
    let opener = crate::current_opener();
    let failed = sender.clone();
    let spawned =
        tilo_rt::background::Background::current().spawn("ui-scoped-open-uri", move || {
            let outcome = scope.with_effect(|| {
                #[cfg(any(test, feature = "test-support"))]
                if let Some(backend) = backend {
                    return backend(&uri);
                }
                opener.open(&uri).map_err(|error| error.to_string())
            });
            let _ = sender.send(outcome);
        });
    if spawned.is_err() {
        let _ = failed.send(Err("Opening links isn't available here".into()));
    }
    receiver
}

#[cfg(any(test, feature = "test-support"))]
type ScopedUriBackend = std::sync::Arc<dyn Fn(&str) -> Result<(), String> + Send + Sync>;
#[cfg(any(test, feature = "test-support"))]
thread_local! {
    static SCOPED_URI_FIXTURE: std::cell::RefCell<Option<ScopedUriBackend>> = const { std::cell::RefCell::new(None) };
}
#[cfg(any(test, feature = "test-support"))]
pub struct ScopedUriFixture {
    _owner_thread: std::marker::PhantomData<std::rc::Rc<()>>,
}
/// Select a backend only for requests dispatched on this test UI thread.
/// The real original-caller check runs before the captured backend is called.
#[cfg(any(test, feature = "test-support"))]
pub fn scoped_uri_fixture(backend: ScopedUriBackend) -> Result<ScopedUriFixture, String> {
    SCOPED_URI_FIXTURE.with(|current| {
        let mut current = current.borrow_mut();
        if current.is_some() {
            return Err("this UI thread already owns a URI fixture".into());
        }
        *current = Some(backend);
        Ok(ScopedUriFixture {
            _owner_thread: std::marker::PhantomData,
        })
    })
}
#[cfg(any(test, feature = "test-support"))]
impl Drop for ScopedUriFixture {
    fn drop(&mut self) {
        SCOPED_URI_FIXTURE.with(|current| *current.borrow_mut() = None);
    }
}