I had a frustrating issue which I have solved with a workaround which is a little unusual. Just wondering if this is a bug, or if there’s a better way to address it.
The issue is that when I press the button for the first time, it doesn’t toggle. But second time and thereafter it does. (There is a button attachment to the apvts.) Is there a better way to solve this?
button declaration:
myToggleButton.setButtonText(audioProcessor.get_mSync() ? "Sync" : "Free"); //initially `true`
myToggleButton.addListener(this);
myToggleButton.setToggleState(audioProcessor.get_mSync(), juce::NotificationType::dontSendNotification); // initially `true`
addAndMakeVisible(myToggleButton);
and in buttonclicked() I insert the setClickingTogglesState which was originally in the button declaration above.
else if (button == &myToggleButton)
{
myToggleButton.setClickingTogglesState(true); //moved to here
bool newState = !myToggleButton.getToggleState();
myToggleButton.setButtonText(newState ? "Sync" : "Free");
}
have you tried attachment.sendInitialUpdate(); yet?
2 Likes
Why do you put myToggleButton.setClickingTogglesState(true) in the buttonClicked() callback instead of the button declaration? I always do the latter.
2 Likes
So all answers and a third one together:
- Like @zsliu98 wrote: setClickingTogglesState is a setup thing and belongs into the constructor, where you setup things
- setToggleState is called by the ButtonAttachment (if you have set it up), including the sendInitialUpdate @Mrugalla mentioned. If you can call setClickingTogglesState before creating the attachment, the sendInitialUpdate works automatically, no need to call it.
- you seem to use a Button::Listener. That should be the job of the attachment
So all you need to do is:
myToggleButton.setClickingTogglesState(true);
myToggleButton.onStateChange = [this]
{
myToggleButton.setButtonText (myToggleButton.getToggleState() ? "Sync" : "Free");
};
attachment = std::make_unique<juce::AudioProcessorValueTreeState::ButtonAttachment>(state, paramID, myToggleButton);
If you have the attachment as plain member, you should call attachment.sendInitialUpdate() where I put the creation.
3 Likes
thanks everyone for your input, creating the button attachment after the setClickingTogglesState seems to work 