Weird look and feel behaviour with toggle button

Hi all,

Maybe I am missing something, but I am experiencing this really weird behavior when trying to draw a custom look and feel for my toggle button. With most bounds I set for the button, the overwritten “drawToggleButton” method won’t draw anything. It gets called and the bounds are as they are supposed to be but nothing is being drawn. I only get stuff drawn with the bounds of the button set to either getLocalBounds() or this one other random rectangle which is like any other rectangle in my project. I should also mention, that if I use the default JUCE look and feel and don’t call the setLookAndFeel method for the button, everything works just fine. I understand if this is impossible to debug from just my explanation but I don’t really know what details even would be relevant for this question since there is hardly any code that could affect this.

This is the overwritten LookAndFeel method…

void drawToggleButton(juce::Graphics& g, juce::ToggleButton& toggle, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown) override 
    {
        g.setColour(juce::Colours::green);
        g.fillRoundedRectangle(toggle.getBounds().reduced(5.f, 5.f).toFloat(), 5.f);
    }

…and this is the resized() method I am setting the bounds for the “pingpongToggle”, which is the button in question:

    compBounds = getLocalBounds();

    leftCompBounds = compBounds.removeFromLeft(compBounds.getWidth() / 3);
    middleCompBounds = compBounds.removeFromLeft(leftCompBounds.getWidth());
    rightCompBounds = compBounds;

    middleBottomBounds = middleCompBounds.removeFromBottom(middleCompBounds.getHeight() / 3);
    middleTopBounds = middleCompBounds.removeFromTop(middleBottomBounds.getHeight());
    middleMiddleBounds = middleCompBounds;

    middleTimeBoundsLeft = middleMiddleBounds.removeFromLeft(middleMiddleBounds.getWidth() / 3);
    middleTimeBoundsRight = middleMiddleBounds.removeFromRight(middleTimeBoundsLeft.getWidth());
    middleLockBounds = middleMiddleBounds.reduced(middleLockBoundsMargin);
    pingpongBounds = middleBottomBounds;

    middleLock.setBounds(middleLockBounds);
    delayTimeSliderLeft.setBounds(middleTimeBoundsLeft);
    delayTimeSliderRight.setBounds(middleTimeBoundsRight);
    pingpongToggle.setBounds(pingpongBounds);

Aside from getLocalBounds(), for some reason, only the “leftCompBounds” works as bounds for the pingpongToggle. Any other rectangle and the button doesn’t draw anything (not just the rounded rectangle I have in the code above, I’ve tried everything from ellipses to text and with different hardcoded drawing positions).

Any idea what have I missed? Thank you so much.

Stay safe,
Aapo

You used toggle.getBounds() in your drawing method… you probably meant getLocalBounds().

Oh man, thats probably it. I also tried with various different hard coded values but they were probably too out of bounds… Thank you for your help, Jimmi.

1 Like