From acc6addb86dff7a6b1969174f6ed0f9d60ad48ab Mon Sep 17 00:00:00 2001 From: attila Date: Wed, 3 Aug 2022 20:32:13 +0200 Subject: [PATCH] DragAndDropContainer: Manage keyboard focus for the drag image when the target needs it hidden The previous implementation would trigger assertions in grabKeyboardFocus() when the DragImageComponent was hidden due to the target returning false from shouldDrawDragImageWhenOver(). Hiding the image would also mean that its keyboard focus was lost. Now focus is restored when the image becomes visible again. --- .../mouse/juce_DragAndDropContainer.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp b/modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp index e534e1909e..d2b5bb268f 100644 --- a/modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp +++ b/modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp @@ -138,6 +138,8 @@ public: setVisible (newTarget == nullptr || newTarget->shouldDrawDragImageWhenOver()); + maintainKeyboardFocusWhenPossible(); + if (newTargetComp != currentlyOverComp) { if (auto* lastTarget = getCurrentlyOver()) @@ -233,6 +235,16 @@ private: Time lastTimeOverTarget; int originalInputSourceIndex; MouseInputSource::InputSourceType originalInputSourceType; + bool canHaveKeyboardFocus = false; + + void maintainKeyboardFocusWhenPossible() + { + const auto newCanHaveKeyboardFocus = isVisible(); + + if (std::exchange (canHaveKeyboardFocus, newCanHaveKeyboardFocus) != newCanHaveKeyboardFocus) + if (canHaveKeyboardFocus) + grabKeyboardFocus(); + } void updateSize() { @@ -489,7 +501,6 @@ void DragAndDropContainer::startDragging (const var& sourceDescription, dragImageComponent->sourceDetails.localPosition = sourceComponent->getLocalPoint (nullptr, lastMouseDown); dragImageComponent->updateLocation (false, lastMouseDown); - dragImageComponent->grabKeyboardFocus(); #if JUCE_WINDOWS // Under heavy load, the layered window's paint callback can often be lost by the OS,