From fc08a83fa0fd6135c13d4ee6c43fc4dc7880af47 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 21 Sep 2021 22:18:18 +0200 Subject: [PATCH] TextEditComponent will now return an empty string if only whitespace characters were entered. --- es-core/src/components/TextEditComponent.cpp | 16 ++++++++++++++++ es-core/src/components/TextEditComponent.h | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/es-core/src/components/TextEditComponent.cpp b/es-core/src/components/TextEditComponent.cpp index a71df9cca..e333a96d0 100644 --- a/es-core/src/components/TextEditComponent.cpp +++ b/es-core/src/components/TextEditComponent.cpp @@ -84,6 +84,22 @@ void TextEditComponent::textInput(const std::string& text) onCursorChanged(); } +std::string TextEditComponent::getValue() const +{ + if (mText.empty()) + return ""; + + // If mText only contains whitespace characters, then return an empty string. + if (std::find_if(mText.cbegin(), mText.cend(), [](char c) { + return !std::isspace(static_cast(c)); + }) == mText.cend()) { + return ""; + } + else { + return mText; + } +} + void TextEditComponent::startEditing() { SDL_StartTextInput(); diff --git a/es-core/src/components/TextEditComponent.h b/es-core/src/components/TextEditComponent.h index 302c832f7..4967880c8 100644 --- a/es-core/src/components/TextEditComponent.h +++ b/es-core/src/components/TextEditComponent.h @@ -33,7 +33,7 @@ public: void onSizeChanged() override; void setValue(const std::string& val) override; - std::string getValue() const override { return mText; } + std::string getValue() const override; void startEditing(); void stopEditing();