diff --git a/crates/egui_dnd/examples/index_as_id.rs b/crates/egui_dnd/examples/index_as_id.rs new file mode 100644 index 0000000..2dcb3bc --- /dev/null +++ b/crates/egui_dnd/examples/index_as_id.rs @@ -0,0 +1,74 @@ +use eframe::egui; +use egui::{CentralPanel, Id}; +use egui_dnd::{dnd, DragDropItem}; + +#[derive(Clone, Hash)] +struct Item { + name: String, +} + +impl Item { + fn new(name: &str) -> Self { + Self { + name: name.to_string(), + } + } +} + +// In order to use the index as id we need to implement DragDropItem for a wrapper struct +struct EnumeratedItem { + item: T, + index: usize, +} + +impl DragDropItem for EnumeratedItem { + fn id(&self) -> Id { + Id::new(self.index) + } +} + +pub fn main() -> eframe::Result<()> { + let mut items = vec![ + Item::new("alfred"), + Item::new("bernhard"), + Item::new("christian"), + Item::new("alfred"), + ]; + + eframe::run_simple_native( + "DnD Simple Example", + Default::default(), + move |ctx, _frame| { + CentralPanel::default().show(ctx, |ui| { + let response = dnd(ui, "dnd_example") + // Since egui_dnd's animations rely on the ids not + // changing after the drag finished we need to disable animations + .with_animation_time(0.0) + .show( + items + .iter_mut() + .enumerate() + .map(|(i, item)| EnumeratedItem { item, index: i }), + |ui, item, handle, state| { + ui.horizontal(|ui| { + handle.ui(ui, |ui| { + if state.dragged { + ui.label("dragging"); + } else { + ui.label("drag"); + } + }); + ui.label(&item.item.name); + }); + }, + ); + + // Since the item id may not change while a drag is ongoing we need to wait + // until the drag is finished before updating the items + if response.is_drag_finished() { + response.update_vec(&mut items); + } + }); + }, + ) +} diff --git a/crates/egui_dnd/src/lib.rs b/crates/egui_dnd/src/lib.rs index e61cb0d..791fc30 100644 --- a/crates/egui_dnd/src/lib.rs +++ b/crates/egui_dnd/src/lib.rs @@ -114,10 +114,10 @@ impl<'a> Dnd<'a> { } /// Display the drag and drop UI. - /// `items` should be an iterator over items that should be sorted. + /// `items` should be an iterator over items that should be sortable. /// - /// The items won't be sorted automatically, but you can use [Dnd::show_vec] or [DragDropResponse::update_vec] to do so. - /// If your items aren't in a vec, you have to sort them yourself. + /// The items won't be updated automatically, but you can use [Dnd::show_vec] or [DragDropResponse::update_vec] to do so. + /// If your items aren't in a vec, you have to update the order yourself. /// /// `item_ui` is called for each item. Display your item there. /// `item_ui` gets a [Handle] that can be used to display the drag handle.