file picker - file input

This commit is contained in:
Martin Kavík 2024-06-04 20:46:10 +02:00
parent ce36cd914f
commit e3e3820cc5
7 changed files with 130 additions and 35 deletions

View file

@ -1,7 +1,7 @@
use shared::wellen_helpers;
use std::sync::Mutex;
use wellen::simple::Waveform;
use zoon::{println, *};
use zoon::*;
#[derive(Default)]
struct Store {
@ -12,30 +12,11 @@ static STORE: Lazy<Store> = lazy::default();
pub(super) async fn show_window() {}
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 file_handle = file_handles
.at(0)
.dyn_into::<web_sys::FileSystemFileHandle>()
.unwrap_throw();
pub(super) async fn pick_and_load_waveform(
file: Option<gloo_file::File>,
) -> Option<super::Filename> {
let file = file.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();
@ -48,6 +29,44 @@ pub(super) async fn pick_and_load_waveform() -> Option<super::Filename> {
Some(file.name())
}
// @TODO Use this `pick_and_load_waveform` version once `showOpenFilePicker` is supported by Safari and Firefox
// https://caniuse.com/mdn-api_window_showopenfilepicker
// 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 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!("Waveform file reading failed")
// };
// *STORE.waveform.lock().unwrap_throw() = Some(waveform);
// Some(file.name())
// }
pub(super) async fn get_hierarchy() -> wellen::Hierarchy {
let waveform = STORE.waveform.lock().unwrap_throw();
let hierarchy = waveform.as_ref().unwrap_throw().hierarchy();

View file

@ -4,7 +4,9 @@ pub(super) async fn show_window() {
tauri_glue::show_window().await.unwrap_throw()
}
pub(super) async fn pick_and_load_waveform() -> Option<super::Filename> {
pub(super) async fn pick_and_load_waveform(
_file: Option<gloo_file::File>,
) -> Option<super::Filename> {
tauri_glue::pick_and_load_waveform()
.await
.unwrap_throw()