file picker + show_open_file_picker

This commit is contained in:
Martin Kavík 2024-06-03 19:11:22 +02:00
parent 6cdccb72b2
commit ce36cd914f
13 changed files with 702 additions and 110 deletions

View file

@ -1,7 +1,7 @@
use shared::wellen_helpers;
use std::sync::Mutex;
use wellen::simple::Waveform;
use zoon::*;
use zoon::{println, *};
#[derive(Default)]
struct Store {
@ -12,19 +12,40 @@ static STORE: Lazy<Store> = lazy::default();
pub(super) async fn show_window() {}
pub(super) async fn load_waveform(test_file_name: &'static str) {
static SIMPLE_VCD: &'static [u8; 311] = include_bytes!("../../../test_files/simple.vcd");
// static WAVE_27_FST: &'static [u8; 28860652] = include_bytes!("../../../test_files/wave_27.fst");
let chosen_file = match test_file_name {
"simple.vcd" => SIMPLE_VCD.to_vec(),
// "wave_27.fst" => WAVE_27_FST.to_vec(),
test_file_name => todo!("add {test_file_name} to the `test_files` folder"),
pub(super) async fn pick_and_load_waveform() -> Option<super::Filename> {
let file_handles_promise = window().show_open_file_picker().expect_throw(
"failed to open file picker (browser has to support `showOpenFilePicker` and use HTTPS",
);
let file_handles = JsFuture::from(file_handles_promise).await;
let file_handles = match file_handles {
Ok(file_handles) => file_handles.dyn_into::<js_sys::Array>().unwrap_throw(),
Err(error) => {
println!("file picker error: {error:?}");
return None;
}
};
let waveform = wellen_helpers::read_from_bytes(chosen_file);
let file_handle = file_handles
.at(0)
.dyn_into::<web_sys::FileSystemFileHandle>()
.unwrap_throw();
let file = JsFuture::from(file_handle.get_file())
.await
.unwrap_throw()
.dyn_into::<web_sys::File>()
.unwrap_throw();
let file = gloo_file::File::from(file);
let content = gloo_file::futures::read_as_bytes(&file)
.await
.unwrap_throw();
let waveform = wellen_helpers::read_from_bytes(content);
let Ok(waveform) = waveform else {
panic!("VCD file reading failed")
panic!("Waveform file reading failed")
};
*STORE.waveform.lock().unwrap_throw() = Some(waveform);
Some(file.name())
}
pub(super) async fn get_hierarchy() -> wellen::Hierarchy {

View file

@ -1,28 +1,37 @@
use zoon::*;
pub(super) async fn show_window() {
tauri_glue::show_window().await
tauri_glue::show_window().await.unwrap_throw()
}
pub(super) async fn load_waveform(test_file_name: &'static str) {
tauri_glue::load_waveform(test_file_name).await
pub(super) async fn pick_and_load_waveform() -> Option<super::Filename> {
tauri_glue::pick_and_load_waveform()
.await
.unwrap_throw()
.as_string()
}
pub(super) async fn get_hierarchy() -> wellen::Hierarchy {
serde_wasm_bindgen::from_value(tauri_glue::get_hierarchy().await).unwrap_throw()
serde_wasm_bindgen::from_value(tauri_glue::get_hierarchy().await.unwrap_throw()).unwrap_throw()
}
pub(super) async fn get_time_table() -> wellen::TimeTable {
serde_wasm_bindgen::from_value(tauri_glue::get_time_table().await).unwrap_throw()
serde_wasm_bindgen::from_value(tauri_glue::get_time_table().await.unwrap_throw()).unwrap_throw()
}
pub(super) async fn load_and_get_signal(signal_ref: wellen::SignalRef) -> wellen::Signal {
serde_wasm_bindgen::from_value(tauri_glue::load_and_get_signal(signal_ref.index()).await)
.unwrap_throw()
serde_wasm_bindgen::from_value(
tauri_glue::load_and_get_signal(signal_ref.index())
.await
.unwrap_throw(),
)
.unwrap_throw()
}
pub(super) async fn unload_signal(signal_ref: wellen::SignalRef) {
tauri_glue::unload_signal(signal_ref.index()).await
tauri_glue::unload_signal(signal_ref.index())
.await
.unwrap_throw()
}
mod tauri_glue {
@ -31,16 +40,22 @@ mod tauri_glue {
// Note: Add all corresponding methods to `frontend/typescript/tauri_glue/tauri_glue.ts`
#[wasm_bindgen(module = "/typescript/bundles/tauri_glue.js")]
extern "C" {
pub async fn show_window();
#[wasm_bindgen(catch)]
pub async fn show_window() -> Result<(), JsValue>;
pub async fn load_waveform(test_file_name: &str);
#[wasm_bindgen(catch)]
pub async fn pick_and_load_waveform() -> Result<JsValue, JsValue>;
pub async fn get_hierarchy() -> JsValue;
#[wasm_bindgen(catch)]
pub async fn get_hierarchy() -> Result<JsValue, JsValue>;
pub async fn get_time_table() -> JsValue;
#[wasm_bindgen(catch)]
pub async fn get_time_table() -> Result<JsValue, JsValue>;
pub async fn load_and_get_signal(signal_ref_index: usize) -> JsValue;
#[wasm_bindgen(catch)]
pub async fn load_and_get_signal(signal_ref_index: usize) -> Result<JsValue, JsValue>;
pub async fn unload_signal(signal_ref_index: usize);
#[wasm_bindgen(catch)]
pub async fn unload_signal(signal_ref_index: usize) -> Result<(), JsValue>;
}
}