From ad490b624125ab63b07d0b784207436ac4b4b283 Mon Sep 17 00:00:00 2001 From: bixuefeng Date: Fri, 8 Apr 2022 17:03:17 +0800 Subject: [PATCH] Bugfix: textinput cursor move to end when insert value Signed-off-by: bixuefeng Change-Id: Ia4acdb69da75f6bba1713540691c4a00f571d5f5 --- .../core/common/ime/text_edit_controller.h | 10 ++-- .../text_field/render_text_field.cpp | 46 ++++++++++--------- .../text_field/text_field_element.cpp | 4 ++ frameworks/core/event/axis_event.h | 1 - 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/frameworks/core/common/ime/text_edit_controller.h b/frameworks/core/common/ime/text_edit_controller.h index c212c3a7..9d94c67d 100644 --- a/frameworks/core/common/ime/text_edit_controller.h +++ b/frameworks/core/common/ime/text_edit_controller.h @@ -28,10 +28,12 @@ public: void SetText(const std::string& newText, bool needFireChangeEvent = true) { auto value = GetValue(); - value.text = newText; - // Default set selection to the end of text is more consistent with the intuition of user. - value.selection.Update(value.GetWideText().length()); - SetValue(std::move(value), needFireChangeEvent); + if (value.text != newText) { + value.text = newText; + // Default set selection to the end of text is more consistent with the intuition of user. + value.selection.Update(static_cast(value.GetWideText().length())); + SetValue(std::move(value), needFireChangeEvent); + } } void SetHint(const std::string& hint) diff --git a/frameworks/core/components/text_field/render_text_field.cpp b/frameworks/core/components/text_field/render_text_field.cpp index d0d2aa2f..75499679 100644 --- a/frameworks/core/components/text_field/render_text_field.cpp +++ b/frameworks/core/components/text_field/render_text_field.cpp @@ -350,9 +350,6 @@ void RenderTextField::PerformLayout() } if (needNotifyChangeEvent_ && (onTextChangeEvent_ || onValueChangeEvent_ || onChange_)) { needNotifyChangeEvent_ = false; - if (onChange_) { - onChange_(GetEditingValue().text); - } if (onValueChangeEvent_) { onValueChangeEvent_(GetEditingValue().text); } @@ -1119,6 +1116,9 @@ void RenderTextField::UpdateEditingValue(const std::shared_ptr if (onValueChange_) { onValueChange_(); } + if (onChange_) { + onChange_(GetEditingValue().text); + } } } @@ -1213,26 +1213,28 @@ bool RenderTextField::OnKeyEvent(const KeyEvent& event) if (event.action == KeyAction::DOWN) { cursorPositionType_ = CursorPositionType::NONE; - bool moved = true; if (event.code == KeyCode::TV_CONTROL_LEFT) { CursorMoveLeft(); - } else if (event.code == KeyCode::TV_CONTROL_RIGHT) { - CursorMoveRight(); - } else if (event.code == KeyCode::TV_CONTROL_UP) { - CursorMoveUp(); - } else if (event.code == KeyCode::TV_CONTROL_DOWN) { - CursorMoveDown(); - } else { - moved = HandleKeyEvent(event); - } - if (moved) { - // Obscure all glyphs immediately after cursor moved. obscureTickPendings_ = 0; + return true; + } + if (event.code == KeyCode::TV_CONTROL_RIGHT) { + CursorMoveRight(); + obscureTickPendings_ = 0; + return true; + } + if (event.code == KeyCode::TV_CONTROL_UP) { + CursorMoveUp(); + obscureTickPendings_ = 0; + return true; + } + if (event.code == KeyCode::TV_CONTROL_DOWN) { + CursorMoveDown(); + obscureTickPendings_ = 0; + return true; } - return moved; } - - return false; + return HandleKeyEvent(event); } void RenderTextField::UpdateFocusStyles() @@ -1780,11 +1782,11 @@ bool RenderTextField::HandleKeyEvent(const KeyEvent& event) if (appendElement.empty()) { return false; } - auto value = GetEditingValue(); - value.text = value.GetBeforeSelection() + appendElement + value.GetAfterSelection(); - value.UpdateSelection( + auto editingValue = std::make_shared(); + editingValue->text = GetEditingValue().GetBeforeSelection() + appendElement + GetEditingValue().GetAfterSelection(); + editingValue->UpdateSelection( std::max(GetEditingValue().selection.GetEnd(), 0) + StringUtils::Str8ToStr16(appendElement).length()); - SetEditingValue(std::move(value)); + UpdateEditingValue(editingValue); MarkNeedLayout(); return true; } diff --git a/frameworks/core/components/text_field/text_field_element.cpp b/frameworks/core/components/text_field/text_field_element.cpp index 87a7bdd8..cdbfd82a 100644 --- a/frameworks/core/components/text_field/text_field_element.cpp +++ b/frameworks/core/components/text_field/text_field_element.cpp @@ -184,6 +184,10 @@ void TextFieldElement::OnFocus() if (!enabled_) { return; } + auto textField = DynamicCast(renderNode_); + if (textField) { + textField->StartTwinkling(); + } FocusNode::OnFocus(); renderNode_->ChangeStatus(RenderStatus::FOCUS); } diff --git a/frameworks/core/event/axis_event.h b/frameworks/core/event/axis_event.h index 022f2629..7567dd07 100644 --- a/frameworks/core/event/axis_event.h +++ b/frameworks/core/event/axis_event.h @@ -20,7 +20,6 @@ #include "base/geometry/offset.h" #include "base/memory/ace_type.h" -#include "core/components/scroll/scroll_fade_painter.h" #include "core/event/ace_events.h" namespace OHOS::Ace {