Browse Source

Added some helper objects Button::onClick and Button::onStateChange, which let you easily assign a lambda to be called on these events

tags/2021-05-28
jules 7 years ago
parent
commit
be5f2d62c4
2 changed files with 28 additions and 11 deletions
  1. +18
    -4
      modules/juce_gui_basics/buttons/juce_Button.cpp
  2. +10
    -7
      modules/juce_gui_basics/buttons/juce_Button.h

+ 18
- 4
modules/juce_gui_basics/buttons/juce_Button.cpp View File

@@ -391,8 +391,15 @@ void Button::sendClickMessage (const ModifierKeys& modifiers)
clicked (modifiers);
if (! checker.shouldBailOut())
buttonListeners.callChecked (checker, [this] (Listener& l) { l.buttonClicked (this); });
if (checker.shouldBailOut())
return;
buttonListeners.callChecked (checker, [this] (Listener& l) { l.buttonClicked (this); });
if (checker.shouldBailOut())
return;
onClick.invoke (*this);
}
void Button::sendStateMessage()
@@ -401,8 +408,15 @@ void Button::sendStateMessage()
buttonStateChanged();
if (! checker.shouldBailOut())
buttonListeners.callChecked (checker, [this] (Listener& l) { l.buttonStateChanged (this); });
if (checker.shouldBailOut())
return;
buttonListeners.callChecked (checker, [this] (Listener& l) { l.buttonStateChanged (this); });
if (checker.shouldBailOut())
return;
onStateChange.invoke (*this);
}
//==============================================================================


+ 10
- 7
modules/juce_gui_basics/buttons/juce_Button.h View File

@@ -184,6 +184,12 @@ public:
*/
void removeListener (Listener* listener);
/** You can assign a lambda to this callback object to have it called when the button is clicked. */
EventHandler<Button> onClick;
/** You can assign a lambda to this callback object to have it called when the button's state changes. */
EventHandler<Button> onStateChange;
//==============================================================================
/** Causes the button to act as if it's been clicked.
@@ -394,13 +400,10 @@ protected:
//==============================================================================
/** This method is called when the button has been clicked.
Subclasses can override this to perform whatever they actions they need
to do.
Alternatively, a Button::Listener can be added to the button, and these listeners
will be called when the click occurs.
@see triggerClick
Subclasses can override this to perform whatever they actions they need to do.
In general, you wouldn't use this method to receive clicks, but should get your callbacks
by attaching a std::function to the onClick callback, or adding a Button::Listener.
@see triggerClick, onClick
*/
virtual void clicked();


Loading…
Cancel
Save