From 0faa0dba15d635ddcf0c98250eda72c064ff6f67 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 19 Nov 2025 18:26:31 +0100 Subject: [PATCH] Drag and Drop: rework cancel drag and drop logic to be overridable. (#9071) --- imgui.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index fcf248aba..9e84b75d0 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -5546,10 +5546,18 @@ void ImGui::NewFrame() g.DragDropWithinSource = false; g.DragDropWithinTarget = false; g.DragDropHoldJustPressedId = 0; - if (g.DragDropActive && IsKeyPressed(ImGuiKey_Escape, ImGuiInputFlags_None, g.ActiveId)) // Also works when g.ActiveId==0 (aka leftover payload in progress, no active id) + if (g.DragDropActive) { - ClearActiveID(); - ClearDragDrop(); + // Also works when g.ActiveId==0 (aka leftover payload in progress, no active id) + // You may disable this externally by hijacking the input route: + // 'if (GetDragDropPayload() != NULL) { Shortcut(ImGuiKey_Escape, ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverActive); } + // but you will not get a return value from Shortcut() due to ActiveIdUsingAllKeyboardKeys logic. You can however poll IsKeyPressed(ImGuiKey_Escape) afterwards. + ImGuiID owner_id = g.ActiveId ? g.ActiveId : ImHashStr("##DragDropCancelHandler"); + if (Shortcut(ImGuiKey_Escape, ImGuiInputFlags_RouteGlobal, owner_id)) + { + ClearActiveID(); + ClearDragDrop(); + } } g.TooltipPreviousWindow = NULL;