Skip to content

Commit

Permalink
Add DragAndDrop::take_payload
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 25, 2024
1 parent f2eeacf commit 4ae1b96
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions crates/egui/src/drag_and_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Payload>(ctx: &Context) -> Option<Arc<Payload>>
where
Payload: Any + Send + Sync,
{
ctx.data_mut(|data| {
let state = data.get_temp_mut_or_default::<Self>(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
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Payload>(&self.ctx)
crate::DragAndDrop::take_payload::<Payload>(&self.ctx)
} else {
None
}
Expand Down

0 comments on commit 4ae1b96

Please sign in to comment.