Add listener callback to Label when text is not changed


I am using a label as a search box which will select some items when the return key is pressed. 

However if I want to repeat the search I can't simply select the label and press enter again (the selection could have changed otherwise) but then I'll have to write something else, hit enter and then write the correct search term again.

This is because the label only sends a labelTextChanged() to its Listeners when the text actually differs from the previous text.

I needed to make the following changes to the Label class:

/** If set to true, the Label will send a message to all listeners on return key press regardless whether the text has actually changed. */
void setNotifyIfNothingChanges(bool shouldNotify) noexcept { notifyIfNothingChanges = shouldNotify; };

// new private member (initialised to false in the constructor)
bool notifyIfNothingChanges;

and in the source file (I only added the "|| notifyIfNothingChanges" condition):

void Label::textEditorReturnKeyPressed (TextEditor& ed)
    if (editor != nullptr)
        jassert (&ed == editor);
        const bool changed = updateFromTextEditorContents (ed);
        hideEditor (true);
        if (changed || notifyIfNothingChanges)
            WeakReference<Component> deletionChecker (this);
            if (deletionChecker != nullptr)

If this would make its way into the JUCE codebase I could save some ugly workarounds (I don't think this is such an usual request).


Sorry, wouldn't want to bloat the class like that.

You could actually just override the textEditorReturnKeyPressed method in a custom Label class and intercept this, couldn't you?


Yup, you're right (I had to hack around a bit because updateFromTextEditorContents() is private) but that's maybe still the cleaner solution on a worldwide scale :)