getComponentAt() irregular behavior

I have an iOS application structure in which I have one large parent component with a grid of child components on top of it. I’m experiencing some weird behavior with getComponentAt() that I wonder if anyone else has experienced so that I can decide where to invest most of my time (whether continued troubleshooting, checking JUCE updates, or redesigning my application hierarchy/GUI design). I have stepped through getComponentAt() and am having trouble pinpointing the weird behavior. JUCE version 7.0.5

The issue: When I mouseDrag on my layered components, I get inconsistent behavior. If mouseDown happens in the parent component, but NOT on one of the child components (i.e. mouseDown.originalComponent = parent), and then I drag the mouse into one of the children, then subsequent mouseDrag() events are correctly identified as being over the child (mouseDrag.getComponentAt(current position) = child). However, if I initially click inside a child component (i.e. mouseDown.originalComponent = child), then subsequent mouseDrags within the child component bounds are attributed to the parent underneath (i.e. mouseDrag.getComponentAt(current position) = parent). I am debating moving to some other Broadcaster/Listener model, but I’m wondering if anyone has any insight before I do a lot of work on my app architecture.

My goal is just to know which component is the top component (on the z-axis) under any given mouseDrag.

Are you able to provide a minimal code example that demonstrates the problem? In particular, it would be helpful to know:

  • How and where are you listening to mouse events?
  • Are you calling setInterceptsMouseClicks anywhere?

You mention calling mouseDrag.getComponentAt() a couple of times, but getComponentAt is a member function of Component. Given that getComponentAt can only return itself or a child (i.e. not an ancestor), it would be helpful to know on which Component you’re calling this function.

One approach to do this would be the following:

Desktop::getInstance().getMainMouseSource().getComponentUnderMouse()
1 Like

Yes, that was sloppy of me. I am indeed calling getComponentAt() from the parent component. Anyways, your reply helped me figure out the (in retrospect, obvious) problem: I needed to get the mouseDrag position relative to the parent [ i.e. event.getEventRelativeTo(parent).getPosition() ] because mouseDrags will always be called in relation to the component they originate on (thus I got the expected behavior when the original component was the parent, but different behavior when the original component was a child). Thanks for your quick and helpful response!

1 Like