JUCE version: 8.0.12
Module / file: juce_gui_basics/buttons/juce_Button.cpp
Summary
Button::setToggleState() now has this guard:
void Button::setToggleState (bool shouldBeOn, NotificationType clickNotification, NotificationType stateNotification)
{
if (isEnabled() && shouldBeOn != lastToggleState)
{
...
}
}
When isEnabled() == false, the entire method body is skipped — isOn, lastToggleState, and the button’s paint state are never updated, and no repaint is triggered. This means any programmatic call to setToggleState() on a disabled button is a silent no-op, with no assertion, warning, or documented restriction.
Why this looks unintended
- The click path was already independently gated elsewhere (
Button::handleCommandMessage:if (commandId == clickMessageId) { if (isEnabled()) { ...; internalClickCallback(...); } }), so disabled buttons were already protected from user-driven toggles before this check was added. TheisEnabled()check insidesetToggleState()only additionally blocks programmatic callers. - No other JUCE widget follows this pattern for its equivalent “set current value” API — e.g.
Slider::setValue()applies the new value unconditionally regardless ofisEnabled(). This inconsistency suggestssetToggleState()'s guard is a narrow addition (possibly for radio-group/focus handling) rather than a deliberate, JUCE-wide “disabled == fully inert” policy. - It breaks a common, legitimate pattern: a disabled button used purely as a read-only status indicator (e.g. a segmented control greyed out but still reflecting which option is currently active) that the application continues to update via
setToggleState()while disabled.
Repro
TextButton button;
button.setToggleState (true, dontSendNotification); // works
button.setEnabled (false);
button.setToggleState (false, dontSendNotification); // silently does nothing
DBG (button.getToggleState()); // prints "1" -- still true
Expected behavior
setToggleState() should update the button’s toggle state regardless of isEnabled(), matching the behavior of other widgets’ “set value” APIs (Slider::setValue, etc.), and matching JUCE’s own pre-8.0 behavior (if (shouldBeOn != lastToggleState), no isEnabled() check).
Suggested fix
Remove the isEnabled() && clause from the guard in setToggleState(). If the intent was to also prevent turnOffOtherButtonsInGroup() side effects from a disabled button, that could be scoped more narrowly inside that call instead of gating the entire toggle-state update.
Workaround currently in use
Bypass setToggleState() entirely and write the toggle Value directly, followed by a manual repaint:
button.getToggleStateValue() = shouldBeOn;
button.repaint();