From 2c5fc5a0a568ad0935716f63006ecff4b9d18805 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler <49431240+abey79@users.noreply.github.com> Date: Wed, 23 Aug 2023 15:13:47 +0200 Subject: [PATCH] Added `mime` field to `DroppedFiles` (#3273) --- crates/eframe/src/web/events.rs | 2 ++ crates/egui/src/data/input.rs | 3 +++ crates/egui_demo_app/src/wrap_app.rs | 13 +++++++++++-- examples/file_dialog/src/main.rs | 14 +++++++++++--- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index f44adb69..cbfa11a6 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -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); @@ -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() diff --git a/crates/egui/src/data/input.rs b/crates/egui/src/data/input.rs index bf28a8b0..61eb152d 100644 --- a/crates/egui/src/data/input.rs +++ b/crates/egui/src/data/input.rs @@ -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, diff --git a/crates/egui_demo_app/src/wrap_app.rs b/crates/egui_demo_app/src/wrap_app.rs index dbfcfd5e..84581d56 100644 --- a/crates/egui_demo_app/src/wrap_app.rs +++ b/crates/egui_demo_app/src/wrap_app.rs @@ -460,9 +460,18 @@ impl WrapApp { } else { "???".to_owned() }; - if let Some(bytes) = &file.bytes { - write!(info, " ({} bytes)", bytes.len()).ok(); + + 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() { + info += &format!(" ({})", additional_info.join(", ")); + } + ui.label(info); } }); diff --git a/examples/file_dialog/src/main.rs b/examples/file_dialog/src/main.rs index 77f88872..82b78b8a 100644 --- a/examples/file_dialog/src/main.rs +++ b/examples/file_dialog/src/main.rs @@ -53,10 +53,18 @@ impl eframe::App for MyApp { } else { "???".to_owned() }; - if let Some(bytes) = &file.bytes { - use std::fmt::Write as _; - write!(info, " ({} bytes)", bytes.len()).ok(); + + 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() { + info += &format!(" ({})", additional_info.join(", ")); + } + ui.label(info); } });