Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EditBox: Only emit onReturnOrUnfocus once in cases where Return is pressed and the widget loses focus because of it #227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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