Components that figure out their own width


#1

We regularly seem to have components that specify their own width which need to be layed out. and I end up with a function something like this:

        int getIdealWidth(int height) override 
        { 
            Font f;
            f.setHeight(height - 4.0f);
            return f.getStringWidth(name) + 8; 
        }

The JUCE pattern I’m used to is one where the parent component mainly tells the child how big it’s going to be, or the child specifies a size.

In my app we always seem to have the parent specifying one dimension and the child component figuring out the size in the other dimension …

Is there a tidier way of doing this? Sometimes it’s way more complicated, like when we have something whose height depends on it’s width, and then discover that we need to inject a scrollbar with the total height, then have to iterate over all the components again…


#2

It sounds like you’re positioning a variety of things relative to each other, in some kind of grid, list, or both?

Aside from the obvious ListBox controls, maybe FlexBox or Grid would do the trick?


#3

Maybe I should try FlexBox again for some of this. Seems like it should be good for things like this?
image


#4

But I don’t think I understand what happens if the component needs to change its fixed width if the height changes… maybe I need to give it a go.


#5

Can’t you derive from ComponentListener and handle componentMovedOrResized() ?

Rail