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