clean up some file stuff

This commit is contained in:
kirjavascript 2023-03-14 18:01:15 +00:00
parent 5cb116c40d
commit 5cc74ced10
2 changed files with 6 additions and 6 deletions

View file

@ -112,11 +112,11 @@ impl eframe::App for App {
} }
if ui.button("Open file…").clicked() { if ui.button("Open file…").clicked() {
self.file.open_file(); self.file.open();
} }
if let Some(file) = self.file.opened() { if let Some(file) = self.file.get() {
println!("{:#?}", file); println!("{:#?}", file);
} }
}); });

View file

@ -53,7 +53,7 @@ impl Drop for FileDialog {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
impl FileDialog { impl FileDialog {
pub fn open_file(&mut self) { pub fn open(&mut self) {
if let Some(closure) = &self.closure { if let Some(closure) = &self.closure {
self.input.remove_event_listener_with_callback("change", closure.as_ref().unchecked_ref()).unwrap(); self.input.remove_event_listener_with_callback("change", closure.as_ref().unchecked_ref()).unwrap();
std::mem::replace(&mut self.closure, None).unwrap().forget(); std::mem::replace(&mut self.closure, None).unwrap().forget();
@ -83,7 +83,7 @@ impl FileDialog {
self.input.click(); self.input.click();
} }
pub fn opened(&self) -> Option<Vec<u8>> { pub fn get(&self) -> Option<Vec<u8>> {
if let Ok(file) = self.rx.try_recv() { if let Ok(file) = self.rx.try_recv() {
Some(file) Some(file)
} else { } else {
@ -114,14 +114,14 @@ impl Default for FileDialog {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
impl FileDialog { impl FileDialog {
pub fn open_file(&mut self) { pub fn open(&mut self) {
let path = rfd::FileDialog::new().pick_file(); let path = rfd::FileDialog::new().pick_file();
if let Some(path) = path { if let Some(path) = path {
self.file = std::fs::read(path).ok(); self.file = std::fs::read(path).ok();
} }
} }
pub fn opened(&mut self) -> Option<File> { pub fn get(&mut self) -> Option<File> {
std::mem::replace(&mut self.file, None) std::mem::replace(&mut self.file, None)
} }