From 59bc2ae8a31f54f68e864bb3071513171d332b98 Mon Sep 17 00:00:00 2001 From: CasualYT31 <21147925+CasualYT31@users.noreply.github.com> Date: Tue, 19 Dec 2023 10:05:57 +0000 Subject: [PATCH] EditBox: Only emit onReturnOrUnfocus once in cases where Return is pressed and the widget loses focus because of it --- include/TGUI/Widgets/EditBox.hpp | 15 +++++++++++++++ src/Widgets/EditBox.cpp | 15 +++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/TGUI/Widgets/EditBox.hpp b/include/TGUI/Widgets/EditBox.hpp index 50e73a2fe..2318f9561 100644 --- a/include/TGUI/Widgets/EditBox.hpp +++ b/include/TGUI/Widgets/EditBox.hpp @@ -561,6 +561,14 @@ TGUI_MODULE_EXPORT namespace tgui void updateSelEnd(const std::size_t newValue); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Emits the onReturnOrUnfocus signal. + /// + /// @param text The text to emit with the signal. + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void emitReturnOrUnfocus(const String& text); + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private: @@ -696,6 +704,13 @@ TGUI_MODULE_EXPORT namespace tgui Color m_selectedTextBackgroundColorCached; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + private: + + // Used to prevent emitting onReturnOrUnfocus twice when unfocusing the edit box inside the callback function. + bool m_onReturnOrUnfocusEmitted = false; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// }; diff --git a/src/Widgets/EditBox.cpp b/src/Widgets/EditBox.cpp index 4e678bab6..9065149cb 100644 --- a/src/Widgets/EditBox.cpp +++ b/src/Widgets/EditBox.cpp @@ -424,7 +424,7 @@ namespace tgui setCaretPosition(m_selEnd); if (wasFocused) - onReturnOrUnfocus.emit(this, m_text); + emitReturnOrUnfocus(m_text); } } @@ -574,7 +574,7 @@ namespace tgui if (event.code == Event::KeyboardKey::Enter) { onReturnKeyPress.emit(this, m_text); - onReturnOrUnfocus.emit(this, m_text); + emitReturnOrUnfocus(m_text); } else if (event.code == Event::KeyboardKey::Backspace) backspaceKeyPressed(); @@ -1565,6 +1565,17 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void EditBox::emitReturnOrUnfocus(const String& text) + { + if (m_onReturnOrUnfocusEmitted) + return; + m_onReturnOrUnfocusEmitted = true; + onReturnOrUnfocus.emit(this, text); + m_onReturnOrUnfocusEmitted = false; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void EditBox::draw(BackendRenderTarget& target, RenderStates states) const { // Draw the borders