feat: make pickers async

This commit is contained in:
electria 2026-07-31 15:11:41 -07:00
commit 18de039e77
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
2 changed files with 41 additions and 31 deletions

View file

@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
use iced::widget;
use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage};
use rfd::FileDialog;
use rfd::AsyncFileDialog;
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
load_image_impl(path).map_err(|e| e.to_string())
@ -20,8 +20,8 @@ fn load_image_impl(path: impl AsRef<Path>) -> ImageResult<RgbaImage> {
Ok(decoded_image.into_rgba8())
}
pub fn pick_image() -> Result<PathBuf, String> {
let Some(path) = FileDialog::new()
pub async fn pick_image() -> Result<PathBuf, String> {
let Some(filehandle) = AsyncFileDialog::new()
.add_filter(
"image",
&[
@ -30,27 +30,20 @@ pub fn pick_image() -> Result<PathBuf, String> {
],
)
.pick_file()
.await
else {
return Err("no path to open provided".into());
};
Ok(path)
Ok(filehandle.path().to_owned())
}
pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> {
let Some(image) = image else {
return Err("no image to save".into());
};
let Some(path) = FileDialog::new().save_file() else {
pub async fn pick_save_path() -> Result<PathBuf, String> {
let Some(filehandle) = AsyncFileDialog::new().save_file().await else {
return Err("no path to save provided".into());
};
if let Err(e) = image.save(&path) {
return Err(e.to_string());
}
Ok(())
Ok(filehandle.path().to_owned())
}
pub const fn string_from_filter_type(f: widget::image::FilterMethod) -> &'static str {