- Docs
- enzUI framework
- Developers
Drag and drop
Dragging within the app, accepting files and text from other applications, and driving native Wayland drag and drop yourself.
enzUI has two kinds of drag and drop. In-app drags (reordering a list, moving items between collections) are local widget behaviour. Native drags exchange data with other applications through the Wayland wl_data_device protocol, on the application's own connection.
Within the app
| Widget | Use |
|---|---|
enz_reorder_list | A keyed list with drag-to-reorder; releases publish a stable-id DropRequest |
enz_drag_drop_host | Owns one drag session across several collections and paints source and target feedback |
enz_drag_item_surface | Registers rows or grid cells as drop targets inside a virtual list or grid |
enz_drag_autoscroll_host | Scrolls a region while a drag nears its edge |
Cross-list dragging and auto-scrolling live in separate hosts because they cannot live inside one list widget. DragFeedbackTokens sets how strongly a drag shows; see Styling and tokens. These local APIs do not start a native drag by themselves; choose one gesture owner per press.
Text, with nothing to wire
app_setup::run connects native drag and drop for text: selected text drags out of fields and editors, and dropped text goes in where it lands, between widgets and other applications. Hold Ctrl to copy instead of move. Text drags show a snippet of the dragged text.
Accepting files
Wrap the region that takes files in file_drop::enz_file_drop_target(content, on_drop). Files dragged onto it from a file manager or another application arrive as one message with their paths. Text fields keep taking dropped text.
Use the list’s change callback to update application state.
enz_file_drop_target(list, Message::Dropped)
// in update:
Message::Dropped(paths) => {
self.items.extend(paths.into_iter().map(|path| Item {
name: path.file_name().map_or_else(
|| path.display().to_string(),
|name| name.to_string_lossy().into_owned(),
),
folder: "Home",
}));
Task::none()
}- When targets nest, the innermost one under the drop point gets the files. A drop on no target is refused, so the source keeps its files.
enz_file_drop_targettells the source the drop succeeded as soon as it takes the paths. An app that copies or checks the files first usesenz_file_drop_target_with_receipt(content, on_drop): its message carries aFileDropwhosepaths()are the files and whosefinish()tells the source once they are safely the app's. Dropping it unfinished refuses the drop, so a source that removes files on a move keeps them.- A target whose meaning changes while it stays in place passes that with
.key(page): a drop released under one key is refused once the target carries another.
On X11, files dropped from other applications arrive as iced's window::Event::FileDropped, and dragging out is not available.
Driving the native runtime
An application with custom MIME types or its own drag sources drives enzui_clipboard_runtime::native_drag::NativeDragRuntime itself. Because one runtime owns destination events, turn the built-in text service off first with AppServices::default().text_drag(false) and handle text drops yourself.
Receiving
- After a window opens, run
NativeDragRuntime::connect(budget)in an iced task and keep the runtime. Addruntime.subscription()to the app's subscriptions and route itsDragEvents toupdate. - Map each Wayland surface to its window with
iced::window::run(window_id, |window| native_drag::surface_id(window)), and remove the mapping on close. - On
Entered, choose MIME preferences and callaccept(id, preferences, actions, preferred, budget), orrejectan unwanted offer. TrackMotionandActionfor feedback;Leftinvalidates the offer. - On
Dropped, callreceive(id, budget)once, process the data, and callfinish()only after the operation has committed. Dropping the received value without finishing cancels it.
For files, negotiate Kind::Uris.preferences() and decode with Pasted::file_transfer(). The compositor-chosen DragAction decides copy or move; a move source must not delete anything merely because bytes were read.
Starting a drag
Call runtime.start(payload, actions, seat, budget) while the originating pointer button or touch is still held, at the gesture's drag threshold. runtime.start_with_icon(..) adds an image under the pointer; DragIcon::from_rgba(rgba, width, height, hotspot, scale) makes one (at most 1024 pixels a side). Offer files with Payload::uris(paths).
Keep the returned SourceDrag alive, normally by passing it to native_drag::source_outcome in a task. Finished(Copy) or Finished(Move) means the destination completed; only Move permits removing the source data.
Dragging a window along
A tab torn out of a tab bar can travel as its own window: once the drag has started, open the window, resolve it with iced::window::wayland_toplevel(id), and pass it to runtime.attach_window(toplevel, offset, budget). The window follows the pointer and stays where the drag ends (xdg_toplevel_drag_v1). Where the compositor does not offer the protocol the call is refused; place the window yourself on drop.
Limits
Native payloads allow up to 64 MIME names of at most 1024 bytes each, and each representation obeys the configured transfer byte limit. At most 16 incoming offers are kept at once. Async methods return None for unavailable registration, admission failure or an expired budget, and Some(Err(..)) for typed transport errors. Native sources need data-device manager version 3.