diff --git a/frameworks/core/components_ng/pattern/text/text_styles.h b/frameworks/core/components_ng/pattern/text/text_styles.h index 29a6822f50a..c9045a820b6 100644 --- a/frameworks/core/components_ng/pattern/text/text_styles.h +++ b/frameworks/core/components_ng/pattern/text/text_styles.h @@ -224,6 +224,7 @@ struct HandleInfoNG { int32_t index = 0; RectF rect; + RectF originalRect; }; TextStyle CreateTextStyleUsingTheme(const std::unique_ptr& fontStyle, 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 c5729f4a3d4..2a8483d3f62 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 @@ -7209,20 +7209,19 @@ int32_t TextFieldPattern::GetLineCount() const void TextFieldPattern::UpdateHandlesOffsetOnScroll(float offset) { - if (SelectOverlayIsOn() && !selectOverlay_->IsSingleHandle()) { - selectController_->UpdateFirstHandleOffset(); + if (SelectOverlayIsOn()) { selectController_->UpdateSecondHandleOffset(); - selectController_->UpdateCaretOffset(TextAffinity::DOWNSTREAM, false); - selectOverlay_->UpdateAllHandlesOffset(); - return; + if (!selectOverlay_->IsSingleHandle()) { + selectController_->UpdateFirstHandleOffset(); + selectController_->UpdateCaretOffset(TextAffinity::DOWNSTREAM, false); + selectOverlay_->UpdateAllHandlesOffset(); + } else { + selectController_->UpdateCaretOffset(IsTextArea() ? OffsetF(0.0f, offset) : OffsetF(offset, 0.0f)); + selectOverlay_->UpdateSecondHandleOffset(); + } + } else { + selectController_->UpdateCaretOffset(IsTextArea() ? OffsetF(0.0f, offset) : OffsetF(offset, 0.0f)); } - // 修改光标和单手柄位置 - auto moveOffset = IsTextArea() ? OffsetF(0.0f, offset) : OffsetF(offset, 0.0f); - auto caretOffset = selectController_->GetCaretRect().GetOffset() + moveOffset; - auto secondHandleOffset = selectController_->GetSecondHandleOffset() + moveOffset; - selectController_->UpdateCaretOffset(caretOffset); - selectController_->UpdateSecondHandleOffset(secondHandleOffset); - selectOverlay_->UpdateSecondHandleOffset(); } void TextFieldPattern::CloseHandleAndSelect() diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_select_overlay.cpp b/frameworks/core/components_ng/pattern/text_field/text_field_select_overlay.cpp index aca331efb6d..ac815e46c5b 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_select_overlay.cpp +++ b/frameworks/core/components_ng/pattern/text_field/text_field_select_overlay.cpp @@ -181,6 +181,9 @@ RectF TextFieldSelectOverlay::GetSecondHandleLocalPaintRect() CHECK_NULL_RETURN(pattern, RectF()); auto controller = pattern->GetTextSelectController(); CHECK_NULL_RETURN(controller, RectF()); + if (IsSingleHandle()) { + return controller->GetCaretInfo().originalRect; + } auto handleRect = controller->GetSecondHandleRect(); auto contentHeight = pattern->GetTextContentRect().Height(); auto handleHeight = std::min(handleRect.Height(), contentHeight); diff --git a/frameworks/core/components_ng/pattern/text_field/text_select_controller.cpp b/frameworks/core/components_ng/pattern/text_field/text_select_controller.cpp index 12231044de8..5a152c52c5c 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_select_controller.cpp +++ b/frameworks/core/components_ng/pattern/text_field/text_select_controller.cpp @@ -58,11 +58,7 @@ RectF TextSelectController::CalculateEmptyValueCaretRect(float width) rect.SetLeft(contentRect_.Left()); rect.SetTop(contentRect_.Top()); rect.SetHeight(textFiled->PreferredLineHeight()); - if (LessOrEqual(width, 0.0f)) { - rect.SetWidth(caretInfo_.rect.Width()); - } else { - rect.SetWidth(width); - } + rect.SetWidth(GreatNotEqual(width, 0.0f) ? width : caretInfo_.rect.Width()); auto textAlign = layoutProperty->GetTextAlignValue(TextAlign::START); auto direction = layoutProperty->GetNonAutoLayoutDirection(); textFiled->CheckTextAlignByDirection(textAlign, direction); @@ -165,16 +161,13 @@ void TextSelectController::UpdateCaretInfoByOffset(const Offset& localOffset) auto index = ConvertTouchOffsetToPosition(localOffset); AdjustCursorPosition(index, localOffset); UpdateCaretIndex(index); - auto secondHandleWidth = SelectHandleInfo::GetDefaultLineWidth().ConvertToPx(); if (!contentController_->IsEmpty()) { UpdateCaretRectByPositionNearTouchOffset(index, localOffset); - secondHandleInfo_.rect = caretInfo_.rect; - secondHandleInfo_.rect.SetWidth(secondHandleWidth); + auto offset = caretInfo_.rect.GetOffset(); MoveHandleToContentRect(caretInfo_.rect, 0.0f); - AdjustHandleAtEdge(secondHandleInfo_.rect); + UpdateCaretOriginalRect(offset); } else { - caretInfo_.rect = CalculateEmptyValueCaretRect(); - secondHandleInfo_.rect = CalculateEmptyValueCaretRect(secondHandleWidth); + SetCaretRectAtEmptyValue(); } } @@ -376,8 +369,7 @@ void TextSelectController::CalculateHandleOffset() { // calculate firstHandleOffset, secondHandleOffset and handlePaintSize if (contentController_->IsEmpty()) { - caretInfo_.rect = CalculateEmptyValueCaretRect(); - secondHandleInfo_.rect = CalculateEmptyValueCaretRect(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); + SetCaretRectAtEmptyValue(); return; } CaretMetricsF secondHandleMetrics; @@ -557,8 +549,7 @@ void TextSelectController::MoveCaretToContentRect( firstHandleInfo_.index = index; secondHandleInfo_.index = index; if (contentController_->IsEmpty()) { - caretInfo_.rect = CalculateEmptyValueCaretRect(); - secondHandleInfo_.rect = CalculateEmptyValueCaretRect(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); + SetCaretRectAtEmptyValue(); return; } CalcCaretMetricsByPosition(GetCaretIndex(), CaretMetrics, textAffinity_); @@ -585,16 +576,13 @@ void TextSelectController::MoveCaretToContentRect( } } } - secondHandleInfo_.rect = caretRect; - secondHandleInfo_.rect.SetWidth(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); if (moveContent) { MoveHandleToContentRect(caretRect, boundaryAdjustment); } else { AdjustHandleAtEdge(caretRect); } - AdjustHandleAtEdge(secondHandleInfo_.rect); + UpdateCaretOriginalRect(CaretMetrics.offset); caretInfo_.rect = caretRect; - caretRect.SetWidth(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); } void TextSelectController::MoveCaretAnywhere(const Offset& touchOffset) @@ -602,8 +590,7 @@ void TextSelectController::MoveCaretAnywhere(const Offset& touchOffset) CaretMetricsF CaretMetrics; if (contentController_->IsEmpty()) { - caretInfo_.rect = CalculateEmptyValueCaretRect(); - secondHandleInfo_.rect = CalculateEmptyValueCaretRect(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); + SetCaretRectAtEmptyValue(); return; } FitCaretMetricsToTouchPoint(CaretMetrics, touchOffset); @@ -619,10 +606,8 @@ void TextSelectController::MoveCaretAnywhere(const Offset& touchOffset) // Adjusts one character width. float boundaryAdjustment = 0.0f; - secondHandleInfo_.rect = caretRect; - secondHandleInfo_.rect.SetWidth(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); MoveHandleToContentRect(caretRect, boundaryAdjustment); - AdjustHandleAtEdge(secondHandleInfo_.rect); + UpdateCaretOriginalRect(CaretMetrics.offset); caretInfo_.rect = caretRect; auto index = ConvertTouchOffsetToPosition(touchOffset); AdjustCursorPosition(index, touchOffset); @@ -653,8 +638,7 @@ void TextSelectController::UpdateCaretOffset(TextAffinity textAffinity, bool mov { textAffinity_ = textAffinity; if (contentController_->IsEmpty()) { - caretInfo_.rect = CalculateEmptyValueCaretRect(); - secondHandleInfo_.rect = CalculateEmptyValueCaretRect(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); + SetCaretRectAtEmptyValue(); return; } CaretMetricsF caretMetrics; @@ -669,22 +653,17 @@ void TextSelectController::UpdateCaretOffset(TextAffinity textAffinity, bool mov caretRect.SetSize(SizeF(caretInfo_.rect.Width(), LessOrEqual(caretMetrics.height, 0.0) ? textFiled->PreferredLineHeight() : caretMetrics.height)); caretInfo_.rect = caretRect; - secondHandleInfo_.rect = caretRect; - secondHandleInfo_.rect.SetWidth(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); if (moveHandle) { MoveHandleToContentRect(caretInfo_.rect, 0.0f); } - AdjustHandleAtEdge(secondHandleInfo_.rect); + UpdateCaretOriginalRect(caretMetrics.offset); } void TextSelectController::UpdateCaretOffset(const OffsetF& offset) { - caretInfo_.rect.SetOffset(offset); -} - -void TextSelectController::UpdateSecondHandleOffset(const OffsetF& offset) -{ - secondHandleInfo_.UpdateOffset(offset); + caretInfo_.rect.SetOffset(caretInfo_.rect.GetOffset() + offset); + caretInfo_.originalRect.SetOffset(caretInfo_.originalRect.GetOffset() + offset); + secondHandleInfo_.UpdateOffset(caretInfo_.rect.GetOffset() + offset); } void TextSelectController::UpdateSecondHandleInfoByMouseOffset(const Offset& localOffset) @@ -892,4 +871,17 @@ void TextSelectController::UpdateSelectWithBlank(const Offset& localOffset) UpdateSelectByOffset(localOffset); } } + +void TextSelectController::SetCaretRectAtEmptyValue() +{ + caretInfo_.rect = CalculateEmptyValueCaretRect(); + caretInfo_.originalRect = CalculateEmptyValueCaretRect(caretInfo_.originalRect.Width()); +} + +void TextSelectController::UpdateCaretOriginalRect(const OffsetF& offset) +{ + caretInfo_.originalRect.SetOffset(offset); + caretInfo_.originalRect.SetHeight(caretInfo_.originalRect.Height()); + AdjustHandleAtEdge(caretInfo_.originalRect); +} } // namespace OHOS::Ace::NG \ No newline at end of file diff --git a/frameworks/core/components_ng/pattern/text_field/text_select_controller.h b/frameworks/core/components_ng/pattern/text_field/text_select_controller.h index 2a022169502..19c7f360a5d 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_select_controller.h +++ b/frameworks/core/components_ng/pattern/text_field/text_select_controller.h @@ -154,6 +154,7 @@ public: void UpdateCaretWidth(float width) { caretInfo_.rect.SetWidth(width); + caretInfo_.originalRect.SetWidth(SelectHandleInfo::GetDefaultLineWidth().ConvertToPx()); } HandleInfoNG GetFirstHandleInfo() const @@ -197,7 +198,6 @@ public: void UpdateCaretOffset(const OffsetF& offset); void UpdateFirstHandleOffset(); void UpdateSecondHandleOffset(); - void UpdateSecondHandleOffset(const OffsetF& offset); void MoveFirstHandleToContentRect(int32_t index, bool moveHandle = true, bool moveContent = true); void MoveSecondHandleToContentRect(int32_t index, bool moveHandle = true, bool moveContent = true); void MoveCaretToContentRect(int32_t index, TextAffinity textAffinity = TextAffinity::UPSTREAM, @@ -234,6 +234,8 @@ private: bool AdjustWordSelection(int32_t& index, int32_t& start, int32_t& end, const Offset& touchOffset); bool IsClickAtBoundary(int32_t index, const Offset& touchOffset); const TimeStamp& GetLastClickTime(); + void UpdateCaretOriginalRect(const OffsetF& offset); + void SetCaretRectAtEmptyValue(); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(FirstIndex, int32_t, PROPERTY_UPDATE_RENDER); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(SecondIndex, int32_t, PROPERTY_UPDATE_RENDER);