From ba67e4dc96155b1b85bac8550aea46acb67c7779 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 19 Jun 2024 16:39:13 +0200 Subject: [PATCH] Abort drags when pressing escape key This is useful to undo a drag-and-drop, for instance --- crates/egui/src/drag_and_drop.rs | 5 +++-- crates/egui/src/interaction.rs | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/drag_and_drop.rs b/crates/egui/src/drag_and_drop.rs index b0c26ac8489..285fc403f32 100644 --- a/crates/egui/src/drag_and_drop.rs +++ b/crates/egui/src/drag_and_drop.rs @@ -27,14 +27,15 @@ impl DragAndDrop { } fn end_frame(ctx: &Context) { - let pointer_released = ctx.input(|i| i.pointer.any_released()); + let abort_dnd = + ctx.input(|i| i.pointer.any_released() || i.key_pressed(crate::Key::Escape)); let mut is_dragging = false; ctx.data_mut(|data| { let state = data.get_temp_mut_or_default::(Id::NULL); - if pointer_released { + if abort_dnd { state.payload = None; } diff --git a/crates/egui/src/interaction.rs b/crates/egui/src/interaction.rs index 8a7b2d0948c..06b19a0b719 100644 --- a/crates/egui/src/interaction.rs +++ b/crates/egui/src/interaction.rs @@ -134,6 +134,12 @@ pub(crate) fn interact( let mut dragged = prev_snapshot.dragged; let mut long_touched = None; + if input.key_pressed(Key::Escape) { + // Abort dragging on escape + dragged = None; + interaction.potential_drag_id = None; + } + if input.is_long_touch() { // We implement "press-and-hold for context menu" on touch screens here if let Some(widget) = interaction