diff --git a/frameworks/core/components_ng/pattern/text_field/on_text_changed_listener_impl.cpp b/frameworks/core/components_ng/pattern/text_field/on_text_changed_listener_impl.cpp index aa11ba0aefdecaa0ed5814c10a75b1e6cac6ed96..75608b3761b2c9edddb976b96c5bf7e3f4e6e068 100644 --- a/frameworks/core/components_ng/pattern/text_field/on_text_changed_listener_impl.cpp +++ b/frameworks/core/components_ng/pattern/text_field/on_text_changed_listener_impl.cpp @@ -395,7 +395,7 @@ int32_t OnTextChangedListenerImpl::ReceivePrivateCommand( const std::unordered_map &privateCommand) { AutoFillReceivePrivateCommand(privateCommand); - int32_t ret = MiscServices::ErrorCode::ERROR_BAD_PARAMETERS; + int32_t ret = MiscServices::ErrorCode::NO_ERROR; if (privateCommand.empty()) { return ret; } diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_pattern.cpp b/frameworks/core/components_ng/pattern/text_field/text_field_pattern.cpp index 64201e4a6eb21bef2f635fafce33c21810045edc..adc23d63050a2acb5e326d56c8046d2476ebf465 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_pattern.cpp +++ b/frameworks/core/components_ng/pattern/text_field/text_field_pattern.cpp @@ -156,6 +156,7 @@ const std::string PREVIEW_STYLE_UNDERLINE = "underline"; constexpr int32_t PREVIEW_NO_ERROR = 0; constexpr int32_t PREVIEW_NULL_POINTER = 1; constexpr int32_t PREVIEW_BAD_PARAMETERS = -1; +constexpr double MINIMAL_OFFSET = 0.01f; static std::unordered_map> contentTypeMap_ = { {TextContentType::VISIBLE_PASSWORD, @@ -1546,7 +1547,7 @@ void TextFieldPattern::HandleTouchUp() void TextFieldPattern::HandleTouchMove(const TouchEventInfo& info) { - if (isTouchCaret_) { + if (isTouchCaret_ || GetIsPreviewText()) { UpdateCaretByTouchMove(info); } } @@ -1559,13 +1560,23 @@ void TextFieldPattern::UpdateCaretByTouchMove(const TouchEventInfo& info) auto touchOffset = info.GetTouches().front().GetLocalLocation(); if (GetIsPreviewText()) { TAG_LOGI(ACE_TEXT_FIELD, "UpdateCaretByTouchMove when has previewText"); + float offsetY = IsTextArea() ? GetTextRect().GetX() : contentRect_.GetX(); + std::vector previewTextRects = GetPreviewTextRects(); + if (previewTextRects.empty()) { + TAG_LOGI(ACE_TEXT_FIELD, "preview text rect error"); + return; + } + + double limitL; + double limitR; + double limitT = previewTextRects.front().Top() + offsetY + MINIMAL_OFFSET; + double limitB = previewTextRects.back().Bottom() + offsetY - MINIMAL_OFFSET; + Offset previewTextTouchOffset; - double offsetLeft = GetPreviewTextRects().front().Left() + GetTextRect().GetX(); - double offsetRight = GetPreviewTextRects().front().Right() + GetTextRect().GetX(); - double offsetTop = GetPreviewTextRects().front().Top() + GetTextRect().GetY(); - double offsetBottom = GetPreviewTextRects().front().Bottom() + GetTextRect().GetY(); - previewTextTouchOffset.SetX(std::clamp(touchOffset.GetX(), offsetLeft, offsetRight)); - previewTextTouchOffset.SetY(std::clamp(touchOffset.GetY(), offsetTop, offsetBottom)); + CalculatePreviewingTextMovingLimit(touchOffset, limitL, limitR); + + previewTextTouchOffset.SetX(std::clamp(touchOffset.GetX(), limitL, limitR)); + previewTextTouchOffset.SetY(std::clamp(touchOffset.GetY(), limitT, limitB)); selectController_->UpdateCaretInfoByOffset(previewTextTouchOffset); } else { selectController_->UpdateCaretInfoByOffset(touchOffset); @@ -2741,6 +2752,9 @@ void TextFieldPattern::InitEditingValueText(std::string content) } contentController_->SetTextValueOnly(std::move(content)); selectController_->UpdateCaretIndex(GetWideText().length()); + if (GetIsPreviewText() && GetWideText().empty()) { + FinishTextPreviewOperation(); + } GetHost()->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT); } @@ -6841,7 +6855,7 @@ bool TextFieldPattern::CheckPreviewTextValidate(PreviewTextInfo info) const PreviewTextStyle TextFieldPattern::GetPreviewTextStyle() const { - auto defaultStyle = PreviewTextStyle::UNDERLINE; + auto defaultStyle = PreviewTextStyle::NORMAL; auto paintProperty = GetPaintProperty(); CHECK_NULL_RETURN(paintProperty, defaultStyle); if (paintProperty->HasPreviewTextStyle()) { @@ -6854,4 +6868,36 @@ PreviewTextStyle TextFieldPattern::GetPreviewTextStyle() const } return defaultStyle; } + +void TextFieldPattern::ReceivePreviewTextStyle(const std::string& style) +{ + auto paintProperty = GetPaintProperty(); + CHECK_NULL_VOID(paintProperty); + if (!style.empty()) { + paintProperty->UpdatePreviewTextStyle(style); + } +} + +void TextFieldPattern::CalculatePreviewingTextMovingLimit(const Offset& touchOffset, double& limitL, double& limitR) +{ + float offsetX = IsTextArea() ? contentRect_.GetX() : GetTextRect().GetX(); + float offsetY = IsTextArea() ? GetTextRect().GetX() : contentRect_.GetX(); + std::vector previewTextRects = GetPreviewTextRects(); + if (GreatNotEqual(touchOffset.GetY(), previewTextRects.back().Bottom() + offsetY)) { + limitL = previewTextRects.back().Left() + offsetX + MINIMAL_OFFSET; + limitR = previewTextRects.back().Right() + offsetX - MINIMAL_OFFSET; + } else if (GreatNotEqual(touchOffset.GetY(), previewTextRects.front().Top() + offsetY)) { + limitL = previewTextRects.front().Left() + offsetX + MINIMAL_OFFSET; + limitR = previewTextRects.front().Right() + offsetX - MINIMAL_OFFSET; + } else { + for (const auto& drawRect : previewTextRects) { + if (GreatOrEqual(touchOffset.GetY(), drawRect.Top() + offsetY) + && LessOrEqual(touchOffset.GetY(), drawRect.Bottom() + offsetY)) { + limitL = drawRect.Left() + offsetX + MINIMAL_OFFSET; + limitR = drawRect.Right() + offsetX - MINIMAL_OFFSET; + break; + } + } + } +} } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_pattern.h b/frameworks/core/components_ng/pattern/text_field/text_field_pattern.h index c851b8bf0f9bf6e20eb6a0c22a0d67b3d3316dae..a4071d7117171f90560a8e236f66111bbe1fa066 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_pattern.h +++ b/frameworks/core/components_ng/pattern/text_field/text_field_pattern.h @@ -1196,10 +1196,7 @@ public: return static_cast(previewUnderlineWidth_.Value()); } - void ReceivePreviewTextStyle(const std::string& style) override - { - ACE_UPDATE_PAINT_PROPERTY(TextFieldPaintProperty, PreviewTextStyle, style); - } + void ReceivePreviewTextStyle(const std::string& style) override; PreviewTextStyle GetPreviewTextStyle() const; @@ -1421,6 +1418,8 @@ private: bool CheckPreviewTextValidate(PreviewTextInfo info) const; + void CalculatePreviewingTextMovingLimit(const Offset& touchOffset, double& limitL, double& limitR); + RectF frameRect_; RectF textRect_; RefPtr paragraph_;