Skip to content

Commit

Permalink
Add index property to ItemState
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Aug 3, 2023
1 parent c73371e commit 8ab04bd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 6 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ pub fn main() -> eframe::Result<()> {
Default::default(),
move |ctx, _frame| {
CentralPanel::default().show(ctx, |ui| {
dnd(ui, "dnd_example").show_vec(&mut items, |ui, item, handle, _state| {
dnd(ui, "dnd_example").show_vec(&mut items, |ui, item, handle, state| {
handle.ui(ui, |ui| {
ui.label("drag");
if state.dragged {
ui.label("dragging");
} else {
ui.label("drag");
}
});
ui.label(*item);
});
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ impl<'a> Dnd<'a> {
mut drag_drop_ui,
} = self;

let response = drag_drop_ui.ui(ui, items, |item, ui, handle, dragged| {
item_ui(ui, item, handle, ItemState { dragged });
let response = drag_drop_ui.ui(ui, items, |item, ui, handle, item_state| {
item_ui(ui, item, handle, item_state);
});

ui.ctx().data_mut(|data| data.insert_temp(id, drag_drop_ui));
Expand Down Expand Up @@ -175,4 +175,9 @@ impl<'a> Dnd<'a> {
pub struct ItemState {
/// True if the item is currently being dragged.
pub dragged: bool,
/// Index of the item in the list.
/// Note that when you sort the source list while the drag is still ongoing (default behaviour
/// of [Dnd::show_vec]), this index will updated while the item is being dragged.
/// If you sort once after the item is dropped, the index will be stable during the drag.
pub index: usize,
}
13 changes: 11 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::{Duration, SystemTime};

use egui::{CursorIcon, Id, InnerResponse, LayerId, Order, Pos2, Rect, Sense, Ui, Vec2};

use crate::ItemState;
#[cfg(target_arch = "wasm32")]
use web_time::{Duration, SystemTime};

Expand Down Expand Up @@ -490,7 +491,7 @@ impl DragDropUi {
&mut self,
ui: &mut Ui,
values: impl Iterator<Item = T>,
mut item_ui: impl FnMut(T, &mut Ui, Handle, bool),
mut item_ui: impl FnMut(T, &mut Ui, Handle, ItemState),
) -> DragDropResponse {
// During the first frame, we check if the pointer is actually over any of the item handles and cancel the drag if it isn't
let mut first_frame = false;
Expand Down Expand Up @@ -626,7 +627,15 @@ impl DragDropUi {
item_id,
&mut hovering_over_any_handle,
|ui, handle| {
item_ui(item, ui, handle, is_dragged_item);
item_ui(
item,
ui,
handle,
ItemState {
dragged: is_dragged_item,
index: idx,
},
);
},
)
})
Expand Down

0 comments on commit 8ab04bd

Please sign in to comment.