mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Store web_sys::File inside of DroppedFile (#8354)
* Closes #4654 * Related #4667 * [x] I have followed the instructions in the PR template This PR avoids materializing the contents of a file that was dragged into an egui application on the web. It does so by storing the `web_sys::File` handle directly on WASM. This breaks the existing API of `DroppedFile` on the web, because there is no way to retrieve the bytes synchronously form a `DroppedFile` anymore, forcing handling call sites to become asynchronous. The native API remains the same. --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -20,7 +20,7 @@ fn main() -> eframe::Result {
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyApp {
|
||||
dropped_files: Vec<egui::DroppedFile>,
|
||||
dropped_files: Vec<egui::DroppedFileHandle>,
|
||||
picked_path: Option<String>,
|
||||
}
|
||||
|
||||
@@ -48,27 +48,23 @@ impl eframe::App for MyApp {
|
||||
ui.label("Dropped files:");
|
||||
|
||||
for file in &self.dropped_files {
|
||||
let mut info = if let Some(path) = &file.path {
|
||||
path.display().to_string()
|
||||
} else if file.name.is_empty() {
|
||||
"???".to_owned()
|
||||
} else {
|
||||
file.name.clone()
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
ui.label(file.path().display().to_string());
|
||||
|
||||
let mut additional_info = vec![];
|
||||
if !file.mime.is_empty() {
|
||||
additional_info.push(format!("type: {}", file.mime));
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let Some(web_file) = file.web_file() else {
|
||||
continue;
|
||||
};
|
||||
let name = web_file.name();
|
||||
let mime = web_file.type_();
|
||||
let size = web_file.size();
|
||||
if mime.is_empty() {
|
||||
ui.label(format!("{name} ({size} bytes)"));
|
||||
} else {
|
||||
ui.label(format!("{name} (type: {mime}, {size} bytes)"));
|
||||
}
|
||||
}
|
||||
if let Some(bytes) = &file.bytes {
|
||||
additional_info.push(format!("{} bytes", bytes.len()));
|
||||
}
|
||||
if !additional_info.is_empty() {
|
||||
use std::fmt::Write as _;
|
||||
write!(info, " ({})", additional_info.join(", ")).ok();
|
||||
}
|
||||
|
||||
ui.label(info);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user