Patch to support buttons with equal widths in AlertWindows (with code)

I recently had to patch JUCE to support having all buttons in an AlertWindow with the same width (on most occasions this gives a more tidy overall appearance)

The code is shown below, it can be added to AlertWindow::updateLayout() at line 387 (right after a line that says int h = textBottom;

A virtual method bool getAlertWindowHasEqualWidthButtons() should also be added to LookAndFeel to just return false by default, to preserve the behavior of existing code.

if (lf.getAlertWindowHasEqualWidthButtons())
{   /* yfede: patch to have buttons with equal width (of the largest one) */
    int largestButtonWidth = 0;

    for (int i = 0; i < buttons.size(); ++i)
        largestButtonWidth = jmax (largestButtonWidth, buttons.getUnchecked (i)->getWidth());

    for (int i = 0; i < buttons.size(); ++i)
        Button* b = buttons.getUnchecked (i);
        b->setSize (largestButtonWidth, b->getHeight ());
}   /* /yfede */

Since the change is minimal and can be implemented without breaking existing code, what do you think about merging it in?

The idea is obviously that one that wants all buttons in AlertWindows to have equal width, will have to derive its LookAndFeel class for it and override the getAlertWindowHasEqualWidthButtons() method to return true

Well, now that the ADC and release of 4.3.0 are over, can someone respond to this?

It’s on develop!      

Uhm… I’m not sure about that implementation.

Suppose I have an AlertWindow with three buttons, labeled “Yes”, “No” and “Don’t ask me again”.

To have all buttons with the same width, and large enough to contain the longest of their label, I should hardcode a suitable value to be returned by getAlertWindowButtonWidth (b), regardless of the b parameter, let’s say it will return 150 px.

Now, if another AlertWindow that uses the same LookAndFeel only has two “Yes” and “No” button, using buttons that are 150 px wide will be overkill, perhaps 50 px would have been enough.

My feeling is that the implementation pushed to develop now falls short because it doesn’t provide enough context to getAlertButtonWindowWidth() to let it return a value that is suitable for all buttons of a specific AlertWindow that is being shown.

Ahh ok I see. Any suggestion on how this could be done nicely?

I didn’t really like your proposal of changing updateLayout. Let me be more specific: The idea of updateLayout is to update the position of the components, i.e. updateLayout does not resize any of the components (except the text area - which makes sense). The size of any component of the AlertWindow is set when you add the component to the AlertWindow which is conceptually nice as it allows the user to set the size of components before adding it to the AlertWindow.

Therefore, specifying the width of the buttons when they are created is better then trying to modify updateLayout.

How about I add a new AlertWindow reference parameter to getAlertWindowButtonWidth and then add a new method to AlertWindow to get all the buttons as a StringArray? Any other ideas?

That sounds good to me :+1:.

*gentle bump* :point_up:

Haven’t forgotten your post. We are discussing what the best solution for this is… hang in there!

1 Like

OK this is on develop now. Does this work for you?

From the looks of it, seems to be enough for me, thanks!