feat: make pickers async
This commit is contained in:
parent
9d4e24e809
commit
18de039e77
2 changed files with 41 additions and 31 deletions
23
src/utils.rs
23
src/utils.rs
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue