Skip to content

Commit

Permalink
wait for the file to be picked (please)
Browse files Browse the repository at this point in the history
  • Loading branch information
p6nj committed Jul 22, 2024
1 parent a8014f7 commit 8c43102
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use files::ImageFile;
#[cfg(target_arch = "wasm32")]
use futures::{Future, FutureExt};
use image::ImageFormat;
#[cfg(target_arch = "wasm32")]
use rfd::FileHandle;
use serde::{Deserialize, Serialize};
#[cfg(not(target_arch = "wasm32"))]
mod files;
Expand Down Expand Up @@ -146,36 +148,25 @@ impl<T: 'static> Task<T> {
impl TemplateApp {
#[cfg(target_arch = "wasm32")]
fn browse(&mut self, ui: &mut egui::Ui) {
use std::panic::AssertUnwindSafe;

use rfd::AsyncFileDialog;

ui.horizontal(|ui| {
if ui.button("Browse").clicked() {
if let Some(path) = Task::spawn(AssertUnwindSafe(
AsyncFileDialog::new()
.set_title("Input image")
.set_directory(working_dir())
.add_filter(
"images",
&ImageFormat::all()
.flat_map(ImageFormat::extensions_str)
.collect::<Vec<&'static &'static str>>(),
)
.pick_file(),
))
.take_output()
.unwrap()
.unwrap()
{
match ImageFile::try_new(&path) {
Ok(file) => {
self.files[0] = Some(file);
}
Err(e) => {
self.toasts.error(e.to_string());
match TemplateApp::prompt() {
Ok(maybe_path) => {
if let Some(path) = maybe_path {
match ImageFile::try_new(&path) {
Ok(file) => {
self.files[0] = Some(file);
}
Err(e) => {
self.toasts.error(e.to_string());
}
}
}
}
Err(_) => {
self.toasts
.error("The file picker just crashed! Can't have shit in Detroit!!!");
}
}
}
ui.label(
Expand All @@ -186,6 +177,33 @@ impl TemplateApp {
);
});
}
#[cfg(target_arch = "wasm32")]
fn prompt() -> thread::Result<Option<FileHandle>> {
use std::{panic::AssertUnwindSafe, thread::sleep, time::Duration};

use rfd::AsyncFileDialog;

let task = Task::spawn(AssertUnwindSafe(
AsyncFileDialog::new()
.set_title("Input image")
.set_directory(working_dir())
.add_filter(
"images",
&ImageFormat::all()
.flat_map(ImageFormat::extensions_str)
.collect::<Vec<&'static &'static str>>(),
)
.pick_file(),
));
loop {
match task.take_output() {
Some(result) => {
break result;
}
None => sleep(Duration::from_millis(40)),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn browse(&mut self, ui: &mut egui::Ui) {
use rfd::FileDialog;
Expand Down

0 comments on commit 8c43102

Please sign in to comment.