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:
@@ -183,7 +183,7 @@ pub struct WrapApp {
|
||||
#[cfg(any(feature = "glow", feature = "wgpu"))]
|
||||
custom3d: Option<crate::apps::Custom3d>,
|
||||
|
||||
dropped_files: Vec<egui::DroppedFile>,
|
||||
dropped_files: Vec<egui::DroppedFileHandle>,
|
||||
}
|
||||
|
||||
impl WrapApp {
|
||||
@@ -519,25 +519,23 @@ impl WrapApp {
|
||||
.open(&mut open)
|
||||
.show(ctx, |ui| {
|
||||
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"))]
|
||||
let info = file.path().display().to_string();
|
||||
|
||||
let mut additional_info = vec![];
|
||||
if !file.mime.is_empty() {
|
||||
additional_info.push(format!("type: {}", file.mime));
|
||||
}
|
||||
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();
|
||||
}
|
||||
// The size and mime-type are free to read; the contents are not,
|
||||
// so we never touch them here.
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let info = {
|
||||
let Some(web_file) = file.web_file() else {
|
||||
continue;
|
||||
};
|
||||
let (name, mime) = (web_file.name(), web_file.type_());
|
||||
if mime.is_empty() {
|
||||
format!("{name} ({} bytes)", web_file.size())
|
||||
} else {
|
||||
format!("{name} ({} bytes, type: {mime})", web_file.size())
|
||||
}
|
||||
};
|
||||
|
||||
ui.label(info);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user