Implementing drag & drop from JUCE to DAW

My VST creates a series of generated audio files, writes them to disk somewhere. But I’d like users to be able to drag these audio files from the VST → back into the DAW.

From the docs, I found this class:
https://docs.juce.com/master/classDragAndDropContainer.html

I decided to make my PluginEditor.cpp inherit from DragAndDropContainer, since the docs say:

“For a component to be able to make or receive drag-and-drop events, one of its parent components must derive from this class. It’s probably best for the top-level component to implement it.”

Here’s where I get a little more confused:

" Then to start a drag operation, any sub-component can just call the startDragging() method, and this object will take over, tracking the mouse and sending appropriate callbacks to any child components derived from DragAndDropTarget which the mouse moves over."

So I decided to structure my application like this:

It feels like the way I’d have to do this is on each mouseDown in a GeneratedAudioComponent, I’d either have to have a saved reference to the parent class (PluginEditor), or find it with ( findParentDragContainerFor) and then call the editor.startDragging() function

Is this the right way to go about it?

1 Like

Make a call to performExternalDragDropOfFiles() in your mouseDrag() function.

2 Likes

@leehu thank you!!! that worked.

one question though: in my mouseDrag() function:

void MyComponent::mouseDrag(const juce::MouseEvent &event) {
   DBG("MOUSE DRAG");
   // ...
}

I’ve noticed I’ll see a ton of these getting fired.

MOUSE DRAG
MOUSE DRAG
MOUSE DRAG
MOUSE DRAG
//... etc

Is there any sort of built in debouncer or other way to filter these events in JUCE?

It seems like it’s able to drag the file without issue, but I worry that this function is getting called 20 times per actual mouse drag. Could just be that I don’t understand the JUCE eventing model yet though!

You need to detect when the mouse leaves the application windows bounds, then call the event - this will prevent the multiple calls from happening

This is expected. If your mouse updates at a rate of 100hz, you should have that function called 100 times. The dragging event is a continuous event like “mouse move”, but unlike "mouse down/mouse up/mouse enter/… .

If this helps to clarify things, you could do your own mouse drag event like this →

void mouseDown(const juce::...
{
dragging = true;
}
void mouseMove(...
{
if(dragging) myMouseDrag();
}
void mouseUp(...
{
dragging = false;
}

void myMouseDrag()
{
DBG("dragging!");
}

This worked for me, thanks!