diff --git a/crates/egui/src/drag_and_drop.rs b/crates/egui/src/drag_and_drop.rs index 3692412284b..7ce70b416bd 100644 --- a/crates/egui/src/drag_and_drop.rs +++ b/crates/egui/src/drag_and_drop.rs @@ -67,6 +67,23 @@ impl DragAndDrop { }) } + /// Retrieve and clear the payload, if any. + /// + /// Returns `None` if there is no payload, or if it is not of the requested type. + /// + /// Returns `Some` both during a drag and on the frame the pointer is released + /// (if there is a payload). + pub fn take_payload(ctx: &Context) -> Option> + where + Payload: Any + Send + Sync, + { + ctx.data_mut(|data| { + let state = data.get_temp_mut_or_default::(Id::NULL); + let payload = state.payload.take()?; + payload.downcast().ok() + }) + } + /// Are we carrying a payload of the given type? /// /// Returns `true` both during a drag and on the frame the pointer is released diff --git a/crates/egui/src/response.rs b/crates/egui/src/response.rs index af8332024d2..5f67c7dbd8b 100644 --- a/crates/egui/src/response.rs +++ b/crates/egui/src/response.rs @@ -366,7 +366,7 @@ impl Response { // NOTE: we use `response.contains_pointer` here instead of `hovered`, because // `hovered` is always false when another widget is being dragged. if self.contains_pointer() && self.ctx.input(|i| i.pointer.any_released()) { - crate::DragAndDrop::payload::(&self.ctx) + crate::DragAndDrop::take_payload::(&self.ctx) } else { None }