Drag and drop
Drag anything with a ghost of it beside the pointer, drop it on targets that light up, and drag a list’s rows to a new place, the list scrolling at its edges.
Drag ghost
DragGhost::new().show(ui, |ui| {
ui.label("Lantern Season");
});
DragGhost::new().show(ui, |ui| {
ui.label("Paper Harbour + 2 more");
});
Drop targets
ui.vertical(|ui| {
ui.horizontal(|ui| {
for tone in TONES {
let chip = Frame::new()
.stroke(Stroke::new(1.0, tokens(ui.ctx()).border))
.corner_radius(tokens(ui.ctx()).shape.radius)
.inner_margin(egui::Margin::symmetric(10, 4))
.show(ui, |ui| ui.label(tone))
.response
.interact(Sense::drag());
let _ = chip.draggable(Tone(tone), |ui| {
ui.label(tone);
});
}
});
ui.add_space(8.0);
for (index, (slot, locked)) in SLOTS.iter().enumerate() {
let (rect, target) = ui.allocate_exact_size(
vec2(ui.available_width().min(360.0), 32.0),
Sense::hover(),
);
let shown = slots[index].unwrap_or("Empty");
ui.painter().text(
rect.left_center() + vec2(12.0, 0.0),
egui::Align2::LEFT_CENTER,
format!("{slot} {shown}"),
egui::FontId::proportional(14.0),
tokens(ui.ctx()).foreground,
);
if let Some(tone) = target.drop_target_if::<Tone>(ui, |_| !locked) {
slots[index] = Some(tone.0);
}
}
});
Reorder a list
ui.vertical(|ui| {
ui.set_width(360.0);
let queue = &state.queue;
let response = List::new(&mut state.list, queue.len())
.id_salt("queue")
.reorderable(true)
.max_height(260.0)
.label(|index| queue[index.row].to_owned())
.show(ui, |ui, row| {
ui.label(queue[row.index.row]);
});
if let Some(moved) = response.moved {
moved.apply(&mut state.queue);
}
});
Outline style, set for an area
ui.drag_style(drag_styles::Outline, |ui| {
tones_and_slots(ui, &mut state.styled);
});
Your own style
ui.drag_style(Ring, |ui| {
tones_and_slots(ui, &mut state.own);
});
The story above is gallery/src/stories/drag_and_drop.rs, running in your browser. Every snippet on this page is cut from it.