Resized called infinitely due to sendMovedResizedMessages

Hello,

I have the following issue in my Resized() Function in my MainComponent. I want that the User can change the audiosettings and I was following the tutorial to make the audioSettings visible. Now I have a preferences button and if I click that button the audioSettings are getting visible or invisible. The issue arises now since I want to make my maincomponent wrap around the drawn content. Like I have kind of like a mixing desk visible and underneath the desk should appear the audioSettings whenever the user clicks the button. Now since I make the MainComponent react to the size of the content, I get into a resized() loop where I can’t really figure out how to escape it.

First here is the pseudo code:

void MainComponent::resized()
{
	if(!audioSettings->isVisible())
	{
		setSize(slotComponents_.size() * slotWidth_ + preferenceButton_.getWidth() + 20, slotHeight_);
	}
	else {
		setSize(audioSettings->getWidth() + preferenceButton_.getWidth() + 20, slotHeight_ + audioSettings->getHeight());
	}
}

In the example I left out the if statement to check if all my Slots are wider than my audioSettings. But aleady with the code above I get into a recursive call of my resized() function. The pseudo code lives in the resized() function. The result is that it scales up till it’s too big and an exception is thrown. I figured with debug that the following function is causing the recursive call:

juce::Component::sendMovedResizedMessages(bool wasMoved, bool wasResized)

It would be nice if I could do if statements in the resize function without getting in the recursive calls. I had it almost working but the width of my maincomponent was too small to show the hole content of the audioSettings.

Please note also, I’m aware that if I use Flexbox and Gridlayout, I would achieve that also, but for now I wanna stick to the traditional way of making a gui for my audioapp.

Thank you in advance,

LaZzle

You’re getting a recursive loop because, well, you’re creating a recursive loop by calling setSize inside of the resized callback.

What you probably want to do is to add a child component to MainComponent that contains whatever you want to resize. Then, set the size of the child from your resized() callback.

Matt

No I want to resize the main component to wrap around the content. When the audiosettings are made visible I need a bigger size of my maincomponent.

You could override ‘visibilityChanged’ and resize the parent in there?
https://docs.juce.com/develop/classListBox.html#a9582ec297c14961573f0f4eb62e0036d

If a setSize call is infinitely recurring it suggests every single call has a different bounds, setSize() calls setBounds() and then checks if the component is being asked to change size or position. If the size or position aren’t changing nothing should happen (see here). It might be worth debugging your code to figure out why this is being detected as a change in every case.

Ok thinking about it, I would say shoot. That’s the answer to my issue. I actually didn’t think about that I should maybe change the default audiosettings component to set a minimum size. Thank you very much :slight_smile: solved my issue :slight_smile:

True and the info is important for me to understand better the mechanism but I think with Adams approach I should be good to go on, if not I will debug more deeply. I have also the feeling that I did maybe a mistake somewhere else in my code. but so far it’s working. Thank you for the infos :slight_smile:

I did consider Adams approach too however I was unsure if you’re doing this in resized() to also deal with the case where the window is being resized by the user, maybe something to check and keep in mind.

Yes I realised that the user can not resize the window what also isn’t intended by me. I’ll keep that in mind. Thanks for pointing that out. I wasn’t sure if it is connected to my approach of the resize() function but you made me being sure now.