Skip to content

Commit

Permalink
DragAndDropContainer: Manage keyboard focus for the drag image when t…
Browse files Browse the repository at this point in the history
…he 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.
  • Loading branch information
szarvas committed Aug 15, 2022
1 parent ea84e14 commit acc6add
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class DragAndDropContainer::DragImageComponent : public Component,

setVisible (newTarget == nullptr || newTarget->shouldDrawDragImageWhenOver());

maintainKeyboardFocusWhenPossible();

if (newTargetComp != currentlyOverComp)
{
if (auto* lastTarget = getCurrentlyOver())
Expand Down Expand Up @@ -233,6 +235,16 @@ class DragAndDropContainer::DragImageComponent : public Component,
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()
{
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit acc6add

Please sign in to comment.