mirror of
https://github.com/kirjavascript/trueLMAO.git
synced 2024-06-02 11:37:28 -04:00
finished file select for wasm
This commit is contained in:
parent
b7a5000d5c
commit
7b934633ee
|
@ -48,11 +48,11 @@ impl App {
|
|||
|
||||
impl eframe::App for App {
|
||||
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
||||
ctx.request_repaint();
|
||||
|
||||
// game logic
|
||||
|
||||
if self.running {
|
||||
ctx.request_repaint();
|
||||
|
||||
crate::input::dummy_input(ctx, &mut self.emu);
|
||||
|
||||
|
@ -114,6 +114,13 @@ impl eframe::App for App {
|
|||
if ui.button("Open file…").clicked() {
|
||||
self.file.open_file();
|
||||
}
|
||||
|
||||
use web_sys::{window, console, Element, HtmlInputElement, FileReader};
|
||||
|
||||
if let Some(file) = self.file.opened() {
|
||||
console::log_1(&format!("File data buffer: {:?}", file).into());
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
|
|
@ -7,14 +7,11 @@ use wasm_bindgen::JsCast;
|
|||
use web_sys::{window, console, Element, HtmlInputElement, FileReader};
|
||||
use js_sys::{Uint8Array, ArrayBuffer, Object};
|
||||
|
||||
pub enum Message {
|
||||
File(Vec<u8>),
|
||||
// Other messages
|
||||
}
|
||||
type File = Vec<u8>;
|
||||
|
||||
pub struct FileDialog {
|
||||
tx: std::sync::mpsc::Sender<Message>,
|
||||
rx: std::sync::mpsc::Receiver<Message>,
|
||||
tx: std::sync::mpsc::Sender<File>,
|
||||
rx: std::sync::mpsc::Receiver<File>,
|
||||
input: HtmlInputElement,
|
||||
closure: Option<Closure<dyn FnMut()>>,
|
||||
}
|
||||
|
@ -33,26 +30,6 @@ impl Default for FileDialog {
|
|||
input.style().set_property("display", "none").unwrap();
|
||||
body.append_child(&input).unwrap();
|
||||
|
||||
|
||||
// let input_clone = input.clone();
|
||||
// let closure = Closure::wrap(Box::new(move || {
|
||||
// if let Some(file) = input_clone.files().and_then(|files| files.get(0)) {
|
||||
// let reader = FileReader::new().unwrap();
|
||||
// let reader_clone = reader.clone();
|
||||
// let onload_closure = Closure::wrap(Box::new(move || {
|
||||
// let array_buffer = reader_clone.result().unwrap().dyn_into::<ArrayBuffer>().unwrap();
|
||||
// let buffer = Uint8Array::new(&array_buffer).to_vec();
|
||||
// console::log_1(&format!("File data buffer: {:?}", buffer).into());
|
||||
// }) as Box<dyn FnMut()>);
|
||||
|
||||
// reader.set_onload(Some(onload_closure.as_ref().unchecked_ref()));
|
||||
// reader.read_as_array_buffer(&file).unwrap();
|
||||
// onload_closure.forget();
|
||||
// }
|
||||
// }) as Box<dyn FnMut()>);
|
||||
// input.add_event_listener_with_callback("change", closure.as_ref().unchecked_ref()).unwrap();
|
||||
// closure.forget();
|
||||
|
||||
Self {
|
||||
rx,
|
||||
tx,
|
||||
|
@ -85,8 +62,7 @@ impl FileDialog {
|
|||
let onload_closure = Closure::once(Box::new(move || {
|
||||
let array_buffer = reader_clone.result().unwrap().dyn_into::<ArrayBuffer>().unwrap();
|
||||
let buffer = Uint8Array::new(&array_buffer).to_vec();
|
||||
console::log_1(&format!("File data buffer: {:?}", buffer).into());
|
||||
tx.send(Message::File(buffer));
|
||||
tx.send(buffer);
|
||||
}));
|
||||
|
||||
reader.set_onload(Some(onload_closure.as_ref().unchecked_ref()));
|
||||
|
@ -107,6 +83,15 @@ impl FileDialog {
|
|||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn opened(&self) -> Option<Vec<u8>> {
|
||||
if let Ok(file) = self.rx.try_recv() {
|
||||
Some(file)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// pub fn filedialog(state: FileState) ->
|
||||
|
|
Loading…
Reference in a new issue