From 6e8414fd05c9b325f3a52fddbd6de867e18901c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lup=C4=8D=C3=ADk?= Date: Sun, 7 Jul 2024 12:18:56 +0200 Subject: [PATCH] Pass the composition to TextInputContext via a parameter (#630) We previously used an internal state for this, which is unclear from the API. Furthermore, this was wrong and outdated after all the changes made to TextInputContext throughout the time; a method called "SetText" should not be used to set any composition, as it may be used for purposes other than just IME. --- Backends/RmlUi_Platform_Win32.cpp | 2 +- Include/RmlUi/Core/TextInputContext.h | 7 +++++-- Source/Core/Elements/WidgetTextInput.cpp | 12 +++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Backends/RmlUi_Platform_Win32.cpp b/Backends/RmlUi_Platform_Win32.cpp index 8c2f22fcb..a8cbd2e4a 100644 --- a/Backends/RmlUi_Platform_Win32.cpp +++ b/Backends/RmlUi_Platform_Win32.cpp @@ -698,7 +698,7 @@ void TextInputMethodEditor_Win32::ConfirmComposition(Rml::StringView composition if (input_context != nullptr) { input_context->SetCompositionRange(composition_range_start, composition_range_end); - input_context->CommitComposition(); + input_context->CommitComposition(composition); } // Move the cursor to the end of the string. diff --git a/Include/RmlUi/Core/TextInputContext.h b/Include/RmlUi/Core/TextInputContext.h index e06789c80..ba8a6586c 100644 --- a/Include/RmlUi/Core/TextInputContext.h +++ b/Include/RmlUi/Core/TextInputContext.h @@ -74,6 +74,7 @@ class RMLUICORE_API TextInputContext { /// @param[in] text The string to replace the character range with. /// @param[in] start The first character to be replaced. /// @param[in] end The first character *after* the range. + /// @note This method does not respect internal restrictions, such as the maximum length. virtual void SetText(StringView text, int start, int end) = 0; /// Update the range of the text being composed (for IME). @@ -81,8 +82,10 @@ class RMLUICORE_API TextInputContext { /// @param[in] end The first character *after* the range. virtual void SetCompositionRange(int start, int end) = 0; - /// Commit the current IME composition. - virtual void CommitComposition() = 0; + /// Commit an composition string (from IME), and respect internal restrictions (e.g., the maximum length). + /// @param[in] composition The string to replace the composition range with. + /// @note If the composition range equals to [0, 0], it takes no action. + virtual void CommitComposition(StringView composition) = 0; }; } // namespace Rml diff --git a/Source/Core/Elements/WidgetTextInput.cpp b/Source/Core/Elements/WidgetTextInput.cpp index 4793e3c48..bb44d7b01 100644 --- a/Source/Core/Elements/WidgetTextInput.cpp +++ b/Source/Core/Elements/WidgetTextInput.cpp @@ -90,13 +90,12 @@ class WidgetTextInputContext final : public TextInputContext { void SetCursorPosition(int position) override; void SetText(StringView text, int start, int end) override; void SetCompositionRange(int start, int end) override; - void CommitComposition() override; + void CommitComposition(StringView composition) override; private: TextInputHandler* handler; WidgetTextInput* owner; ElementFormControl* element; - String composition; }; WidgetTextInputContext::WidgetTextInputContext(TextInputHandler* handler, WidgetTextInput* owner, ElementFormControl* element) : @@ -139,8 +138,6 @@ void WidgetTextInputContext::SetText(StringView text, int start, int end) value.replace(start, end - start, text.begin(), text.size()); element->SetValue(value); - - composition = String(text); } void WidgetTextInputContext::SetCompositionRange(int start, int end) @@ -148,7 +145,7 @@ void WidgetTextInputContext::SetCompositionRange(int start, int end) owner->SetCompositionRange(start, end); } -void WidgetTextInputContext::CommitComposition() +void WidgetTextInputContext::CommitComposition(StringView composition) { int start_byte, end_byte; owner->GetCompositionRange(start_byte, end_byte); @@ -172,12 +169,13 @@ void WidgetTextInputContext::CommitComposition() if (value_length + composition_length - (start - end) > owner->GetMaxLength()) { int new_length = owner->GetMaxLength() - (value_length - composition_length); - composition.erase(StringUtilities::ConvertCharacterOffsetToByteOffset(composition, new_length)); + int new_length_byte = StringUtilities::ConvertCharacterOffsetToByteOffset(composition, new_length); + composition = StringView(composition.begin(), composition.begin() + new_length_byte); } } RMLUI_ASSERTMSG(end_byte >= start_byte, "Invalid end character offset."); - value.replace(start_byte, end_byte - start_byte, composition.data(), composition.size()); + value.replace(start_byte, end_byte - start_byte, composition.begin(), composition.size()); element->SetValue(value); }