FAQ - Help and questions
How do I let users drag files onto my Rust desktop window?
Short answer
With egui, read raw.hovered_files to know a drag is over the window and raw.dropped_files for the files that were dropped; each dropped file has an optional path. With winit directly, handle WindowEvent::DroppedFile and the hover events. The egui code passed cargo check; it was not run with a real drag.
Checked with rustc 1.93.0, Windows 11, eframe 0.35.0 and winit 0.30.13; `cargo check` only. No file was dragged onto a window, and macOS was not tested.
Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.
2 answers from the Ruststudio team: different approaches, each with its own trade-offs
Sort: Oldest NewestApproach 1: egui and eframe#
egui puts the information into the raw input of each frame: hovered_files while a drag is over the window (use it to show a "drop here" hint) and dropped_files in the frame when the user releases the mouse.
use eframe::egui;
use std::path::PathBuf;
#[derive(Default)]
struct App {
files: Vec<PathBuf>,
}
impl eframe::App for App {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
let ctx = ui.ctx().clone();
let hovering = ctx.input(|i| !i.raw.hovered_files.is_empty()); // a drag is over the window
ctx.input(|i| {
for f in &i.raw.dropped_files {
// `path` is Some on Windows and macOS; in a web page there is only `bytes`.
if let Some(p) = &f.path {
self.files.push(p.clone());
}
}
});
egui::CentralPanel::default().show(ui, |ui| {
ui.heading(if hovering { "Drop it here" } else { "Drag files onto this window" });
for f in &self.files {
ui.label(f.display().to_string());
}
});
}
}
fn main() -> eframe::Result {
eframe::run_native("Drop", eframe::NativeOptions::default(), Box::new(|_cc| Ok(Box::new(App::default()))))
}This compiled with cargo check on Windows 11 with eframe 0.35.0. The code comment about path reflects egui's DroppedFile type, whose path field is optional because a web build has no file path; for desktop builds read the documentation of your egui version.
Trade-off: simplest option, and you only get what egui forwards. Treat the paths as untrusted (see below).
Approach 2: winit directly#
WindowEvent::DroppedFile(PathBuf) arrives once per dropped file; winit also has HoveredFile and HoveredFileCancelled events for the drag entering and leaving. The thread on winit has a handler that prints dropped: <path> for each DroppedFile; it compiled but was not run with a real drag.
// inside ApplicationHandler::window_event
match event {
WindowEvent::HoveredFile(path) => println!("a drag with {} is over the window", path.display()),
WindowEvent::HoveredFileCancelled => println!("the drag left the window"),
WindowEvent::DroppedFile(path) => println!("dropped: {}", path.display()),
_ => {}
}The snippet is a fragment of the handler in the winit thread. The DroppedFile arm was checked there; HoveredFile and HoveredFileCancelled were written from winit's WindowEvent documentation and were not compiled separately.
Trade-off: full control over what happens on hover and drop, but you draw the hint yourself.
Common mistakes#
- Trusting what was dropped. A path can name anything: a huge file, a folder, a file you cannot read. Check the type and size, and handle errors on opening.
- Reading a big dropped file on the UI thread (see the background thread thread).
- Forgetting that a user can drop several files at once.
Related questions#
Can I drag files out of my window to the desktop?#
That is a different feature, with different support in each toolkit; check the toolkit's documentation.
Sources#
- egui RawInput and DroppedFile: https://docs.rs/egui/0.35.0/egui/struct.RawInput.html (read 2026-12-05)
- winit WindowEvent: https://docs.rs/winit/0.30.13/winit/event/enum.WindowEvent.html (read 2026-12-05)
Sign in to reply
A free account lets you answer, vote and follow topics.
Posts show your first name and last initial. Forum rules