From f687b27efc590b9584bfa6dd0c2f22edaf8c8cef Mon Sep 17 00:00:00 2001 From: Antoine Beyeler <49431240+abey79@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:35:24 +0100 Subject: [PATCH] Consume escape keystroke when bailing out from a drag operation (#5433) --- crates/egui/src/drag_and_drop.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/crates/egui/src/drag_and_drop.rs b/crates/egui/src/drag_and_drop.rs index 13da9d314c1..c58a5a48f64 100644 --- a/crates/egui/src/drag_and_drop.rs +++ b/crates/egui/src/drag_and_drop.rs @@ -23,22 +23,30 @@ pub struct DragAndDrop { impl DragAndDrop { pub(crate) fn register(ctx: &Context) { - ctx.on_end_pass("debug_text", std::sync::Arc::new(Self::end_pass)); + ctx.on_begin_pass("drag_and_drop_begin_pass", Arc::new(Self::begin_pass)); + ctx.on_end_pass("drag_and_drop_end_pass", Arc::new(Self::end_pass)); } - fn end_pass(ctx: &Context) { - let abort_dnd = - ctx.input(|i| i.pointer.any_released() || i.key_pressed(crate::Key::Escape)); - - let mut is_dragging = false; + fn begin_pass(ctx: &Context) { + let has_any_payload = Self::has_any_payload(ctx); - ctx.data_mut(|data| { - let state = data.get_temp_mut_or_default::(Id::NULL); + if has_any_payload { + let abort_dnd = ctx.input_mut(|i| { + i.pointer.any_released() + || i.consume_key(crate::Modifiers::NONE, crate::Key::Escape) + }); if abort_dnd { - state.payload = None; + Self::clear_payload(ctx); } + } + } + + fn end_pass(ctx: &Context) { + let mut is_dragging = false; + ctx.data_mut(|data| { + let state = data.get_temp_mut_or_default::(Id::NULL); is_dragging = state.payload.is_some(); });