1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

Drag and drop files into egui_glium and egui_web (#637)

* Implement file drag-and-drop for egui_glium

* Implement file drag-and-drop into egui_web

* Cleanup
This commit is contained in:
Emil Ernerfeldt
2021-08-20 22:20:45 +02:00
committed by GitHub
parent 488b1f2462
commit a256ca115b
10 changed files with 239 additions and 6 deletions

View File

@@ -57,6 +57,12 @@ pub struct RawInput {
/// but you can check if egui is using the keyboard with [`crate::Context::wants_keyboard_input`]
/// and/or the pointer (mouse/touch) with [`crate::Context::is_using_pointer`].
pub events: Vec<Event>,
/// Dragged files hovering over egui.
pub hovered_files: Vec<HoveredFile>,
/// Dragged files dropped into egui.
pub dropped_files: Vec<DroppedFile>,
}
impl Default for RawInput {
@@ -72,12 +78,17 @@ impl Default for RawInput {
predicted_dt: 1.0 / 60.0,
modifiers: Modifiers::default(),
events: vec![],
hovered_files: Default::default(),
dropped_files: Default::default(),
}
}
}
impl RawInput {
/// Helper: move volatile (deltas and events), clone the rest
/// Helper: move volatile (deltas and events), clone the rest.
///
/// * [`Self::hovered_files`] is cloned.
/// * [`Self::dropped_files`] is moved.
pub fn take(&mut self) -> RawInput {
#![allow(deprecated)] // for screen_size
let zoom = self.zoom_delta;
@@ -92,10 +103,34 @@ impl RawInput {
predicted_dt: self.predicted_dt,
modifiers: self.modifiers,
events: std::mem::take(&mut self.events),
hovered_files: self.hovered_files.clone(),
dropped_files: std::mem::take(&mut self.dropped_files),
}
}
}
/// A file about to be dropped into egui.
#[derive(Clone, Debug, Default)]
pub struct HoveredFile {
/// Set by the `egui_glium` backend.
pub path: Option<std::path::PathBuf>,
/// With the `egui_web` backend, this is set to the mime-type of the file (if available).
pub mime: String,
}
/// A file dropped into egui.
#[derive(Clone, Debug, Default)]
pub struct DroppedFile {
/// Set by the `egui_glium` backend.
pub path: Option<std::path::PathBuf>,
/// Name of the file. Set by the `egui_web` backend.
pub name: String,
/// Set by the `egui_web` backend.
pub last_modified: Option<std::time::SystemTime>,
/// Set by the `egui_web` backend.
pub bytes: Option<std::sync::Arc<[u8]>>,
}
/// An input event generated by the integration.
///
/// This only covers events that egui cares about.
@@ -295,6 +330,8 @@ impl RawInput {
predicted_dt,
modifiers,
events,
hovered_files,
dropped_files,
} = self;
ui.label(format!("scroll_delta: {:?} points", scroll_delta));
@@ -313,6 +350,8 @@ impl RawInput {
ui.label(format!("modifiers: {:#?}", modifiers));
ui.label(format!("events: {:?}", events))
.on_hover_text("key presses etc");
ui.label(format!("hovered_files: {}", hovered_files.len()));
ui.label(format!("dropped_files: {}", dropped_files.len()));
}
}