Module ashpd::desktop::file_chooser

source ·
Expand description

Open/save file(s) chooser. The interface lets sandboxed applications ask the user for access to files outside the sandbox. The portal backend will present the user with a file chooser dialog.

Wrapper of the DBus interface: org.freedesktop.portal.FileChooser.

§Examples

§Opening a file
use ashpd::desktop::file_chooser::{Choice, FileFilter, SelectedFiles};

async fn run() -> ashpd::Result<()> {
    let files = SelectedFiles::open_file()
        .title("open a file to read")
        .accept_label("read")
        .modal(true)
        .multiple(true)
        .choice(
            Choice::new("encoding", "Encoding", "latin15")
                .insert("utf8", "Unicode (UTF-8)")
                .insert("latin15", "Western"),
        )
        // A trick to have a checkbox
        .choice(Choice::boolean("re-encode", "Re-encode", false))
        .filter(FileFilter::new("SVG Image").mimetype("image/svg+xml"))
        .send()
        .await?
        .response()?;

    println!("{:#?}", files);

    Ok(())
}
§Ask to save a file
use ashpd::desktop::file_chooser::{FileFilter, SelectedFiles};

async fn run() -> ashpd::Result<()> {
    let files = SelectedFiles::save_file()
        .title("open a file to write")
        .accept_label("write")
        .current_name("image.jpg")
        .modal(true)
        .filter(FileFilter::new("JPEG Image").glob("*.jpg"))
        .send()
        .await?
        .response()?;

    println!("{:#?}", files);

    Ok(())
}
§Ask to save multiple files
use ashpd::desktop::file_chooser::SelectedFiles;

async fn run() -> ashpd::Result<()> {
    let files = SelectedFiles::save_files()
        .title("open files to write")
        .accept_label("write files")
        .modal(true)
        .current_folder("/home/bilelmoussaoui/Pictures")?
        .files(&["test.jpg", "awesome.png"])?
        .send()
        .await?
        .response()?;

    println!("{:#?}", files);

    Ok(())
}

Structs§