From d7f1e59422a3d13f4b9215dffbaedc672cb980d4 Mon Sep 17 00:00:00 2001 From: Oliver James Date: Mon, 20 Mar 2023 14:53:38 +0000 Subject: [PATCH] AlertWindow: Add 'getButton' method --- .../windows/juce_AlertWindow.cpp | 24 ++++++++++++------- .../windows/juce_AlertWindow.h | 16 +++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp index 44e036cdce..305758e8bd 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp @@ -150,16 +150,24 @@ int AlertWindow::getNumButtons() const return buttons.size(); } +Button* AlertWindow::getButton (int index) const +{ + return buttons[index]; +} + +Button* AlertWindow::getButton (const String& buttonName) const +{ + for (auto* button : buttons) + if (buttonName == button->getName()) + return button; + + return nullptr; +} + void AlertWindow::triggerButtonClick (const String& buttonName) { - for (auto* b : buttons) - { - if (buttonName == b->getName()) - { - b->triggerClick(); - break; - } - } + if (auto* button = getButton (buttonName)) + button->triggerClick(); } void AlertWindow::setEscapeKeyCancels (bool shouldEscapeKeyCancel) diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.h b/modules/juce_gui_basics/windows/juce_AlertWindow.h index bcdffe83bc..4c074b1711 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.h +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.h @@ -92,6 +92,22 @@ public: /** Returns the number of buttons that the window currently has. */ int getNumButtons() const; + /** Returns a Button that was added to the AlertWindow. + + @param index the index of the button in order that it was added with the addButton() method. + @returns the Button component, or nullptr if the index is out of bounds. + + @see getNumButtons + */ + Button* getButton (int index) const; + + /** Returns a Button that was added to the AlertWindow. + + @param buttonName the name that was passed into the addButton() method + @returns the Button component, or nullptr if none was found for the given name. + */ + Button* getButton (const String& buttonName) const; + /** Invokes a click of one of the buttons. */ void triggerButtonClick (const String& buttonName);