Skip to content

Commit 8ac1d61

Browse files
authored
Abort drags when pressing escape key (emilk#4678)
This is useful to undo a drag-and-drop, for instance
1 parent d9c5fb0 commit 8ac1d61

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

crates/egui/src/drag_and_drop.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ impl DragAndDrop {
2727
}
2828

2929
fn end_frame(ctx: &Context) {
30-
let pointer_released = ctx.input(|i| i.pointer.any_released());
30+
let abort_dnd =
31+
ctx.input(|i| i.pointer.any_released() || i.key_pressed(crate::Key::Escape));
3132

3233
let mut is_dragging = false;
3334

3435
ctx.data_mut(|data| {
3536
let state = data.get_temp_mut_or_default::<Self>(Id::NULL);
3637

37-
if pointer_released {
38+
if abort_dnd {
3839
state.payload = None;
3940
}
4041

crates/egui/src/interaction.rs

+6
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ pub(crate) fn interact(
134134
let mut dragged = prev_snapshot.dragged;
135135
let mut long_touched = None;
136136

137+
if input.key_pressed(Key::Escape) {
138+
// Abort dragging on escape
139+
dragged = None;
140+
interaction.potential_drag_id = None;
141+
}
142+
137143
if input.is_long_touch() {
138144
// We implement "press-and-hold for context menu" on touch screens here
139145
if let Some(widget) = interaction

0 commit comments

Comments
 (0)