Weird MouseEvent behaviour (at least on Mac)

Hi Folks,

when i click on a button which deletes itself or a Component containing it upon mouseDown, the corresponding mouseUp goes to what ever was under that button or Component. This behaviour is weird to say the least. That mouseUp should end nowhere as logic dictates that a mouseUp can only occur after a mouseDown and if the Component receiving that rogue mouseUp depends on that logic havoc happens.

Is that a bug in mouse handling or is there a deeper philosophy behind?

Best,

Thomas

Self-deleting upon mouseDownshould probably be handled by the owning component:

/* quasi-pseudocode */

//declared somewhere in ParentComponent:
std::unique_ptr<juce::TextButton> buttonThatSelfDeletes;

//then in the constructor:
ParentComponent()
{
    buttonThatSelfDeletes = std::make_unique<juce::TextButton>(...);
    addAndMakeVisible( buttonThatSelfDeletes.get() );
    addMouseListener(buttonThatSelfDeletes.get());
}

//then in the mouseDown callback:
void ParentComponent::mouseDown(const MouseEvent& e)
{
   if( e.originalComponent == buttonThatSelfDeletes.get() )
   {
       removeMouseListener(buttonThatSelfDeletes.get();
       buttonThatSelfDeletes.reset(); //the parent component deletes the button, the button doesn't self-delete
   }
}

But I don’t know your actual project needs, so take this information and perspective with a grain of salt.

Thanks, but that doesn’t touch the problem.

The actual problem is not the mouseDown or the deletion. The actual problem is that the mouseDown, which went to the button, has a corresponding mouseUp which after button deletion goes to another component. So the problem is that the another component receives a mouseUp without having first received a corresponding mouseDown, but depends on that order.

Does the problem you’re describing go away if you use the approach I described in my post?

Not really because the mouseUp still arrives somewhere, either in the ParentComponent or somewhere else depending on the structure of components.

Can you do that deletion on mouseUp instead?

I could but that would make the user experience not so good.

A workaround, which doesn’t solve the original problem but may result in an equivalent user experience: hide the component on mouseDown, then delete it on mouseUp

1 Like