eframe web: Fix drag-and-drop file preview/hover (#4732)

* Closes https://github.com/emilk/egui/issues/4726
This commit is contained in:
Emil Ernerfeldt 2024-06-28 18:07:53 +02:00 committed by GitHub
parent 0c059ac113
commit 2180f16cba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 2 deletions

View File

@ -587,14 +587,28 @@ fn install_drag_and_drop(runner_ref: &WebRunner, target: &EventTarget) -> Result
runner_ref.add_event_listener(target, "dragover", |event: web_sys::DragEvent, runner| { runner_ref.add_event_listener(target, "dragover", |event: web_sys::DragEvent, runner| {
if let Some(data_transfer) = event.data_transfer() { if let Some(data_transfer) = event.data_transfer() {
runner.input.raw.hovered_files.clear(); runner.input.raw.hovered_files.clear();
for i in 0..data_transfer.items().length() {
if let Some(item) = data_transfer.items().get(i) { // NOTE: data_transfer.files() is always empty in dragover
let items = data_transfer.items();
for i in 0..items.length() {
if let Some(item) = items.get(i) {
runner.input.raw.hovered_files.push(egui::HoveredFile { runner.input.raw.hovered_files.push(egui::HoveredFile {
mime: item.type_(), mime: item.type_(),
..Default::default() ..Default::default()
}); });
} }
} }
if runner.input.raw.hovered_files.is_empty() {
// Fallback: just preview anything. Needed on Desktop Safari.
runner
.input
.raw
.hovered_files
.push(egui::HoveredFile::default());
}
runner.needs_repaint.repaint_asap(); runner.needs_repaint.repaint_asap();
event.stop_propagation(); event.stop_propagation();
event.prevent_default(); event.prevent_default();