Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mime field to DroppedFile #3273

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added mime field to DroppedFiles
  • Loading branch information
abey79 committed Aug 23, 2023
commit 8c210a2a4fa104ebee6096052ed4cf48c2949d9c
2 changes: 2 additions & 0 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ pub fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValue> {
for i in 0..files.length() {
if let Some(file) = files.get(i) {
let name = file.name();
let mime = file.type_();
let last_modified = std::time::UNIX_EPOCH
+ std::time::Duration::from_millis(file.last_modified() as u64);

Expand All @@ -491,6 +492,7 @@ pub fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValue> {
runner_lock.input.raw.dropped_files.push(
egui::DroppedFile {
name,
mime,
last_modified: Some(last_modified),
bytes: Some(bytes.into()),
..Default::default()
Expand Down
3 changes: 3 additions & 0 deletions crates/egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ pub struct DroppedFile {
/// Name of the file. Set by the `eframe` web backend.
pub name: String,

/// With the `eframe` web backend, this is set to the mime-type of the file (if available).
pub mime: String,

/// Set by the `eframe` web backend.
pub last_modified: Option<std::time::SystemTime>,

Expand Down
11 changes: 10 additions & 1 deletion crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,18 @@ impl WrapApp {
} else {
"???".to_owned()
};

let mut additional_info = vec![];
if !file.mime.is_empty() {
additional_info.push(format!("type: {}", file.mime));
}
if let Some(bytes) = &file.bytes {
write!(info, " ({} bytes)", bytes.len()).ok();
additional_info.push(format!("{} bytes", bytes.len()));
}
if !additional_info.is_empty() {
info += &format!(" ({})", additional_info.join(", "));
}

ui.label(info);
}
});
Expand Down
12 changes: 10 additions & 2 deletions examples/file_dialog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ impl eframe::App for MyApp {
} else {
"???".to_owned()
};

let mut additional_info = vec![];
if !file.mime.is_empty() {
additional_info.push(format!("type: {}", file.mime));
}
if let Some(bytes) = &file.bytes {
use std::fmt::Write as _;
write!(info, " ({} bytes)", bytes.len()).ok();
additional_info.push(format!("{} bytes", bytes.len()));
}
if !additional_info.is_empty() {
info += &format!(" ({})", additional_info.join(", "));
}

ui.label(info);
}
});
Expand Down
Loading