mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 13:20:05 -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:
@@ -33,6 +33,8 @@ pub struct WrapApp {
|
||||
selected_anchor: String,
|
||||
apps: Apps,
|
||||
backend_panel: super::backend_panel::BackendPanel,
|
||||
#[cfg_attr(feature = "persistence", serde(skip))]
|
||||
dropped_files: Vec<egui::DroppedFile>,
|
||||
}
|
||||
|
||||
impl epi::App for WrapApp {
|
||||
@@ -102,6 +104,8 @@ impl epi::App for WrapApp {
|
||||
}
|
||||
|
||||
self.backend_panel.end_of_frame(ctx);
|
||||
|
||||
self.ui_file_drag_and_drop(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,6 +148,67 @@ impl WrapApp {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn ui_file_drag_and_drop(&mut self, ctx: &egui::CtxRef) {
|
||||
use egui::*;
|
||||
|
||||
// Preview hovering files:
|
||||
if !ctx.input().raw.hovered_files.is_empty() {
|
||||
let mut text = "Dropping files:\n".to_owned();
|
||||
for file in &ctx.input().raw.hovered_files {
|
||||
if let Some(path) = &file.path {
|
||||
text += &format!("\n{}", path.display());
|
||||
} else if !file.mime.is_empty() {
|
||||
text += &format!("\n{}", file.mime);
|
||||
} else {
|
||||
text += "\n???";
|
||||
}
|
||||
}
|
||||
|
||||
let painter =
|
||||
ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("file_drop_target")));
|
||||
|
||||
let screen_rect = ctx.input().screen_rect();
|
||||
painter.rect_filled(screen_rect, 0.0, Color32::from_black_alpha(192));
|
||||
painter.text(
|
||||
screen_rect.center(),
|
||||
Align2::CENTER_CENTER,
|
||||
text,
|
||||
TextStyle::Heading,
|
||||
Color32::WHITE,
|
||||
);
|
||||
}
|
||||
|
||||
// Collect dropped files:
|
||||
if !ctx.input().raw.dropped_files.is_empty() {
|
||||
self.dropped_files = ctx.input().raw.dropped_files.clone();
|
||||
}
|
||||
|
||||
// Show dropped files (if any):
|
||||
if !self.dropped_files.is_empty() {
|
||||
let mut open = true;
|
||||
egui::Window::new("Dropped files")
|
||||
.open(&mut open)
|
||||
.show(ctx, |ui| {
|
||||
for file in &self.dropped_files {
|
||||
let mut info = if let Some(path) = &file.path {
|
||||
path.display().to_string()
|
||||
} else if !file.name.is_empty() {
|
||||
file.name.clone()
|
||||
} else {
|
||||
"???".to_owned()
|
||||
};
|
||||
if let Some(bytes) = &file.bytes {
|
||||
info += &format!(" ({} bytes)", bytes.len());
|
||||
}
|
||||
ui.label(info);
|
||||
}
|
||||
});
|
||||
if !open {
|
||||
self.dropped_files.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clock_button(ui: &mut egui::Ui, seconds_since_midnight: f64) -> egui::Response {
|
||||
|
||||
Reference in New Issue
Block a user