Simpler adding of Component children

For anyone (just like me) that’s tired of adding children line by line I made a simple wrapper for it:

inline void addAndShow(juce::Component* comp, std::initializer_list<std::reference_wrapper<juce::Component>> list) {
    for (auto& child : list)
        comp->addAndMakeVisible(child);
}

So this:

addAndMakeVisible(lfoGraph);
addAndMakeVisible(freq);
addAndMakeVisible(phase);
addAndMakeVisible(depth);
addAndMakeVisible(shape);
addAndMakeVisible(smooth);
addAndMakeVisible(sync);

becomes this:

addAndShow(this, {lfoGraph, freq, phase, depth, shape, smooth, sync});
8 Likes

I usually do

for (auto comp : { &myWidget1, &myWidget2 })
    addAndMakeVisible(comp);
4 Likes

Would be cool if addAndMakeVisible() could be extended to take a varadic number of components.

1 Like

Yeah but how would you then handle the second optional parameter, i.e. the z order?

Fair point.

Could maybe have an overload of addAndMakeVisible() take an initialiser list and just have it that you can’t explicitly specify z-order using that method? Or perhaps take a second list for the z-orders?

Right, I would even vouch for a parameter pack maybe…