Skip to content

Commit

Permalink
Pass the composition to TextInputContext via a parameter (#630)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ShawnCZek authored Jul 7, 2024
1 parent 09ddcf8 commit 6e8414f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Backends/RmlUi_Platform_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions Include/RmlUi/Core/TextInputContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ 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).
/// @param[in] start The first character in the range.
/// @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
Expand Down
12 changes: 5 additions & 7 deletions Source/Core/Elements/WidgetTextInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
Expand Down Expand Up @@ -139,16 +138,14 @@ 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)
{
owner->SetCompositionRange(start, end);
}

void WidgetTextInputContext::CommitComposition()
void WidgetTextInputContext::CommitComposition(StringView composition)
{
int start_byte, end_byte;
owner->GetCompositionRange(start_byte, end_byte);
Expand All @@ -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);
}
Expand Down

0 comments on commit 6e8414f

Please sign in to comment.