Skip to content

Commit

Permalink
EditBox: Only emit onReturnOrUnfocus once in cases where Return is pr…
Browse files Browse the repository at this point in the history
…essed and the widget loses focus because of it
  • Loading branch information
CasualYT31 authored and texus committed Dec 20, 2023
1 parent a3d49fe commit 59bc2ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 15 additions & 0 deletions include/TGUI/Widgets/EditBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
};

Expand Down
15 changes: 13 additions & 2 deletions src/Widgets/EditBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ namespace tgui
setCaretPosition(m_selEnd);

if (wasFocused)
onReturnOrUnfocus.emit(this, m_text);
emitReturnOrUnfocus(m_text);
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 59bc2ae

Please sign in to comment.