Skip to content

Commit

Permalink
remove output browse button for web, output is set when writing the file
Browse files Browse the repository at this point in the history
  • Loading branch information
p6nj committed Jul 22, 2024
1 parent 5d6733c commit ec760f8
Showing 1 changed file with 29 additions and 73 deletions.
102 changes: 29 additions & 73 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ fn input_file() -> &'static FilePickerSharedResult {
INPUT_FILE.get_or_init(Default::default)
}

#[cfg(target_arch = "wasm32")]
fn output_file() -> &'static FilePickerSharedResult {
static OUTPUT_FILE: OnceLock<FilePickerSharedResult> = OnceLock::new();
OUTPUT_FILE.get_or_init(Default::default)
}

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(Default, Deserialize, Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
Expand Down Expand Up @@ -191,43 +185,23 @@ pub fn spawn_and_collect<F: 'static + Future<Output = Option<String>> + UnwindSa
#[derive(Display, Clone, Copy, EnumIter)]
enum FileDirection {
Input,
#[cfg(not(target_arch = "wasm32"))]
Output,
}

impl TemplateApp {
#[cfg(target_arch = "wasm32")]
fn browse(&mut self, ui: &mut egui::Ui, direction: FileDirection) {
fn browse(&mut self, ui: &mut egui::Ui, _: FileDirection) {
ui.horizontal(|ui| {
if ui.button("Browse").clicked() {
debug!("user asked to open a file for {direction}");
TemplateApp::prompt(direction);
match direction {
FileDirection::Input => {
self.input_file_asked = true;
}
FileDirection::Output => {
self.output_file_asked = true;
}
}
debug!("user asked to open a file");
TemplateApp::prompt();
self.input_file_asked = true;
}
if match direction {
FileDirection::Input => self.input_file_asked,
FileDirection::Output => self.output_file_asked,
} {
let mut guard = match direction {
FileDirection::Input => input_file(),
FileDirection::Output => output_file(),
}
.lock();
if self.input_file_asked {
let mut guard = input_file().lock();
if guard.as_ref().is_some() {
match direction {
FileDirection::Input => {
self.input_file_asked = false;
}
FileDirection::Output => {
self.output_file_asked = false;
}
}
self.input_file_asked = false;
trace!("just got the file picker result");
let res = {
let guard = guard.as_ref();
Expand All @@ -240,10 +214,7 @@ impl TemplateApp {
match ImageFile::try_new(path) {
Ok(file) => {
trace!("file is correct");
self.files[match direction {
FileDirection::Input => 0,
FileDirection::Output => 1,
}] = Some(file);
self.files[0] = Some(file);
}
Err(e) => {
trace!("file is incorrect, reason: {e}");
Expand All @@ -266,50 +237,35 @@ impl TemplateApp {
}
}
ui.label(
self.files[match direction {
FileDirection::Input => 0,
FileDirection::Output => 1,
}]
.as_ref()
.map(|file| file.to_string())
.unwrap_or_default(),
self.files[0]
.as_ref()
.map(|file| file.to_string())
.unwrap_or_default(),
);
});
}
#[cfg(target_arch = "wasm32")]
fn prompt(direction: FileDirection) {
fn prompt() {
use std::panic::AssertUnwindSafe;

use rfd::AsyncFileDialog;

let fd = AsyncFileDialog::new()
.set_title(format!("{direction} image"))
.set_directory(working_dir())
.add_filter(
"images",
&ImageFormat::all()
.flat_map(ImageFormat::extensions_str)
.collect::<Vec<&'static &'static str>>(),
);

match direction {
FileDirection::Input => spawn_and_collect(
AssertUnwindSafe(fd.pick_file().map(|x| x.map(|file| file.file_name()))),
Arc::downgrade(match direction {
FileDirection::Input => input_file(),
FileDirection::Output => output_file(),
}),
spawn_and_collect(
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()
.map(|x| x.map(|file| file.file_name())),
),
FileDirection::Output => {
spawn_and_collect(
AssertUnwindSafe(fd.save_file().map(|x| x.map(|file| file.file_name()))),
Arc::downgrade(match direction {
FileDirection::Input => input_file(),
FileDirection::Output => output_file(),
}),
);
}
}
Arc::downgrade(input_file()),
);
}
#[cfg(not(target_arch = "wasm32"))]
fn browse(&mut self, ui: &mut egui::Ui, direction: FileDirection) {
Expand Down

0 comments on commit ec760f8

Please sign in to comment.