Skip to content

Commit

Permalink
Abort drags when pressing escape key
Browse files Browse the repository at this point in the history
This is useful to undo a drag-and-drop, for instance
  • Loading branch information
emilk committed Jun 19, 2024
1 parent d9c5fb0 commit ba67e4d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions crates/egui/src/drag_and_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>(Id::NULL);

if pointer_released {
if abort_dnd {
state.payload = None;
}

Expand Down
6 changes: 6 additions & 0 deletions crates/egui/src/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ba67e4d

Please sign in to comment.