egui/data/input/dropped_file.rs
1use alloc::sync::Arc;
2use std::path::Path;
3
4#[cfg(target_arch = "wasm32")]
5use core::{future::Future, pin::Pin};
6
7/// A file dropped into egui.
8///
9/// The integration owns the concrete file handle, letting egui remain independent of windowing
10/// backends and file APIs.
11pub trait DroppedFile: core::fmt::Debug {
12 /// The path of the dropped file.
13 ///
14 /// This is an absolute path on native platforms. On the web, it is a relative path containing
15 /// only the file name because browsers do not expose the file's local path.
16 fn path(&self) -> &Path;
17
18 /// Read the file contents.
19 ///
20 /// This is asynchronous because browsers can only read files asynchronously.
21 ///
22 /// # Errors
23 ///
24 /// Returns an error if the browser cannot read the file.
25 #[cfg(target_arch = "wasm32")]
26 fn bytes_async(&self) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, String>> + '_>>;
27
28 /// Read the file contents.
29 ///
30 /// # Errors
31 ///
32 /// Returns an error if the file cannot be read.
33 #[cfg(not(target_arch = "wasm32"))]
34 fn bytes(&self) -> Result<Vec<u8>, String>;
35
36 /// The browser file handle, if this file was dropped on the web.
37 ///
38 /// Requires the `web_file` feature.
39 #[cfg(all(target_arch = "wasm32", feature = "web_file"))]
40 fn web_file(&self) -> Option<&web_sys::File> {
41 None
42 }
43}
44
45/// A shared reference to a dropped file.
46///
47/// With wasm threads, a `web_sys::File` isn't `Send + Sync`,
48/// so an integration has to wrap it, for instance with `send_wrapper::SendWrapper`.
49pub type DroppedFileHandle = Arc<dyn DroppedFile + Send + Sync>;