Browse Source

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

* Closes https://github.com/emilk/egui/issues/4726
pull/4739/head
Emil Ernerfeldt 4 months ago
committed by GitHub
parent
commit
2180f16cba
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      crates/eframe/src/web/events.rs

18
crates/eframe/src/web/events.rs

@ -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| {
if let Some(data_transfer) = event.data_transfer() {
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 {
mime: item.type_(),
..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();
event.stop_propagation();
event.prevent_default();

Loading…
Cancel
Save