1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_PATTERN_H
18 
19 #include <optional>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "interfaces/inner_api/ace/ai/data_detector_interface.h"
24 
25 #include "base/geometry/dimension.h"
26 #include "base/geometry/ng/offset_t.h"
27 #include "base/memory/referenced.h"
28 #include "base/utils/noncopyable.h"
29 #include "base/utils/utils.h"
30 #include "core/common/ai/data_detector_adapter.h"
31 #include "core/components_ng/event/long_press_event.h"
32 #include "core/components_ng/pattern/pattern.h"
33 #include "core/components_ng/pattern/rich_editor/paragraph_manager.h"
34 #include "core/components_ng/pattern/rich_editor/selection_info.h"
35 #include "core/components_ng/pattern/scrollable/scrollable_pattern.h"
36 #include "core/components_ng/pattern/select_overlay/magnifier.h"
37 #include "core/components_ng/pattern/text/layout_info_interface.h"
38 #include "core/components_ng/pattern/text/multiple_click_recognizer.h"
39 #include "core/components_ng/pattern/text/span/mutable_span_string.h"
40 #include "core/components_ng/pattern/text/span/span_object.h"
41 #include "core/components_ng/pattern/text/span/span_string.h"
42 #include "core/components_ng/pattern/text/span_node.h"
43 #include "core/components_ng/pattern/text/text_accessibility_property.h"
44 #include "core/components_ng/pattern/text/text_base.h"
45 #include "core/components_ng/pattern/text/text_content_modifier.h"
46 #include "core/components_ng/pattern/text/text_controller.h"
47 #include "core/components_ng/pattern/text/text_event_hub.h"
48 #include "core/components_ng/pattern/text/text_layout_algorithm.h"
49 #include "core/components_ng/pattern/text/text_layout_property.h"
50 #include "core/components_ng/pattern/text/text_overlay_modifier.h"
51 #include "core/components_ng/pattern/text/text_paint_method.h"
52 #include "core/components_ng/pattern/text/text_select_overlay.h"
53 #include "core/components_ng/pattern/text_drag/text_drag_base.h"
54 #include "core/components_ng/pattern/text_field/text_selector.h"
55 #include "core/components_ng/property/property.h"
56 #include "core/event/ace_events.h"
57 #include "core/pipeline_ng/ui_task_scheduler.h"
58 
59 namespace OHOS::Ace::NG {
60 class InspectorFilter;
61 enum class Status { DRAGGING, ON_DROP, NONE };
62 using CalculateHandleFunc = std::function<void()>;
63 using ShowSelectOverlayFunc = std::function<void(const RectF&, const RectF&)>;
64 struct SpanNodeInfo {
65     RefPtr<UINode> node;
66     RefPtr<UINode> containerSpanNode;
67 };
68 
69 // TextPattern is the base class for text render node to perform paint text.
70 class TextPattern : public virtual Pattern,
71                     public TextDragBase,
72                     public TextBase,
73                     public TextGestureSelector,
74                     public Magnifier,
75                     public LayoutInfoInterface {
76     DECLARE_ACE_TYPE(TextPattern, Pattern, TextDragBase, TextBase, TextGestureSelector, Magnifier);
77 
78 public:
TextPattern()79     TextPattern()
80     {
81         selectOverlay_ = AceType::MakeRefPtr<TextSelectOverlay>(WeakClaim(this));
82         pManager_ = AceType::MakeRefPtr<ParagraphManager>();
83         magnifierController_ = MakeRefPtr<MagnifierController>(WeakClaim(this));
84     }
85 
86     ~TextPattern() override;
87 
88     SelectionInfo GetSpansInfo(int32_t start, int32_t end, GetSpansMethod method);
89     std::list<ResultObject> GetSpansInfoInStyledString(int32_t start, int32_t end);
90 
91     virtual int32_t GetTextContentLength();
92 
93     RefPtr<NodePaintMethod> CreateNodePaintMethod() override;
94 
CreateLayoutProperty()95     RefPtr<LayoutProperty> CreateLayoutProperty() override
96     {
97         return MakeRefPtr<TextLayoutProperty>();
98     }
99 
CreateLayoutAlgorithm()100     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
101     {
102         auto textLayoutProperty = GetLayoutProperty<TextLayoutProperty>();
103         if (textLayoutProperty &&
104             textLayoutProperty->GetTextOverflowValue(TextOverflow::CLIP) == TextOverflow::MARQUEE) {
105             return MakeRefPtr<TextLayoutAlgorithm>(spans_, pManager_, isSpanStringMode_, true);
106         } else {
107             return MakeRefPtr<TextLayoutAlgorithm>(spans_, pManager_, isSpanStringMode_);
108         }
109     }
110 
CreateAccessibilityProperty()111     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
112     {
113         return MakeRefPtr<TextAccessibilityProperty>();
114     }
115 
CreateEventHub()116     RefPtr<EventHub> CreateEventHub() override
117     {
118         return MakeRefPtr<TextEventHub>();
119     }
120 
IsDragging()121     bool IsDragging() const
122     {
123         return status_ == Status::DRAGGING;
124     }
125 
IsAtomicNode()126     bool IsAtomicNode() const override
127     {
128         auto host = GetHost();
129         CHECK_NULL_RETURN(host, false);
130         return host->GetTag() == V2::SYMBOL_ETS_TAG;
131     }
132 
IsTextNode()133     bool IsTextNode() const
134     {
135         auto host = GetHost();
136         CHECK_NULL_RETURN(host, false);
137         return host->GetTag() == V2::TEXT_ETS_TAG;
138     }
139 
DefaultSupportDrag()140     bool DefaultSupportDrag() override
141     {
142         return true;
143     }
144 
145     void OnModifyDone() override;
146 
147     void OnWindowHide() override;
148 
149     void OnWindowShow() override;
150 
151     void PreCreateLayoutWrapper();
152 
153     void BeforeCreateLayoutWrapper() override;
154 
155     void AddChildSpanItem(const RefPtr<UINode>& child);
156     void AddImageToSpanItem(const RefPtr<UINode>& child);
157 
GetFocusPattern()158     FocusPattern GetFocusPattern() const override
159     {
160         return { FocusType::NODE, false };
161     }
162 
163     void DumpAdvanceInfo() override;
164 
165     void DumpInfo() override;
166     void DumpScaleInfo();
167     void DumpTextEngineInfo();
168 
GetTextSelector()169     TextSelector GetTextSelector() const
170     {
171         return textSelector_;
172     }
173 
GetTextForDisplay()174     std::string GetTextForDisplay() const
175     {
176         return textForDisplay_;
177     }
178 
GetStartOffset()179     const OffsetF& GetStartOffset() const
180     {
181         return textSelector_.selectionBaseOffset;
182     }
183 
GetEndOffset()184     const OffsetF& GetEndOffset() const
185     {
186         return textSelector_.selectionDestinationOffset;
187     }
188 
GetSelectHeight()189     double GetSelectHeight() const
190     {
191         return textSelector_.GetSelectHeight();
192     }
193 
194     void GetGlobalOffset(Offset& offset);
195 
196     RectF GetTextContentRect(bool isActualText = false) const override;
197 
GetBaselineOffset()198     float GetBaselineOffset() const
199     {
200         return baselineOffset_;
201     }
202 
GetContentModifier()203     RefPtr<TextContentModifier> GetContentModifier()
204     {
205         return contentMod_;
206     }
207 
208     void SetTextDetectEnable(bool enable);
GetTextDetectEnable()209     bool GetTextDetectEnable()
210     {
211         return textDetectEnable_;
212     }
SetTextDetectTypes(const std::string & types)213     void SetTextDetectTypes(const std::string& types)
214     {
215         dataDetectorAdapter_->SetTextDetectTypes(types);
216     }
GetTextDetectTypes()217     std::string GetTextDetectTypes()
218     {
219         return dataDetectorAdapter_->textDetectTypes_;
220     }
GetDataDetectorAdapter()221     RefPtr<DataDetectorAdapter> GetDataDetectorAdapter()
222     {
223         return dataDetectorAdapter_;
224     }
GetAISpanMap()225     const std::map<int32_t, AISpan>& GetAISpanMap()
226     {
227         return dataDetectorAdapter_->aiSpanMap_;
228     }
GetTextForAI()229     const std::string& GetTextForAI()
230     {
231         return dataDetectorAdapter_->textForAI_;
232     }
SetOnResult(std::function<void (const std::string &)> && onResult)233     void SetOnResult(std::function<void(const std::string&)>&& onResult)
234     {
235         dataDetectorAdapter_->onResult_ = std::move(onResult);
236     }
GetTextDetectResult()237     TextDataDetectResult GetTextDetectResult()
238     {
239         return dataDetectorAdapter_->textDetectResult_;
240     }
SetTextDetectConfig(const TextDetectConfig & textDetectConfig)241     void SetTextDetectConfig(const TextDetectConfig& textDetectConfig)
242     {
243         dataDetectorAdapter_->SetTextDetectTypes(textDetectConfig.types);
244         dataDetectorAdapter_->onResult_ = std::move(textDetectConfig.onResult);
245         dataDetectorAdapter_->entityColor_ = textDetectConfig.entityColor;
246         dataDetectorAdapter_->entityDecorationType_ = textDetectConfig.entityDecorationType;
247         dataDetectorAdapter_->entityDecorationColor_ = textDetectConfig.entityDecorationColor;
248         dataDetectorAdapter_->entityDecorationStyle_ = textDetectConfig.entityDecorationStyle;
249         auto textDetectConfigCache = dataDetectorAdapter_->textDetectConfigStr_;
250         dataDetectorAdapter_->textDetectConfigStr_ = textDetectConfig.ToString();
251         if (textDetectConfigCache != dataDetectorAdapter_->textDetectConfigStr_) {
252             auto host = GetHost();
253             CHECK_NULL_VOID(host);
254             host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
255         }
256     }
ModifyAISpanStyle(TextStyle & aiSpanStyle)257     void ModifyAISpanStyle(TextStyle& aiSpanStyle)
258     {
259         TextDetectConfig textDetectConfig;
260         aiSpanStyle.SetTextColor(dataDetectorAdapter_->entityColor_.value_or(textDetectConfig.entityColor));
261         aiSpanStyle.SetTextDecoration(
262             dataDetectorAdapter_->entityDecorationType_.value_or(textDetectConfig.entityDecorationType));
263         aiSpanStyle.SetTextDecorationColor(
264             dataDetectorAdapter_->entityDecorationColor_.value_or(textDetectConfig.entityColor));
265         aiSpanStyle.SetTextDecorationStyle(
266             dataDetectorAdapter_->entityDecorationStyle_.value_or(textDetectConfig.entityDecorationStyle));
267     }
268 
269     void OnVisibleChange(bool isVisible) override;
270 
GetSpanItemChildren()271     std::list<RefPtr<SpanItem>> GetSpanItemChildren()
272     {
273         return spans_;
274     }
275 
GetDisplayWideTextLength()276     int32_t GetDisplayWideTextLength()
277     {
278         return StringUtils::ToWstring(textForDisplay_).length();
279     }
280 
281     // ===========================================================
282     // TextDragBase implementations
283 
IsTextArea()284     bool IsTextArea() const override
285     {
286         return false;
287     }
288 
GetTextRect()289     const RectF& GetTextRect() override
290     {
291         return contentRect_;
292     }
293     float GetLineHeight() const override;
294 
295     std::vector<RectF> GetTextBoxes() override;
296     OffsetF GetParentGlobalOffset() const override;
297 
MoveDragNode()298     const RefPtr<FrameNode>& MoveDragNode() override
299     {
300         return dragNode_;
301     }
302 
GetDragParagraph()303     const RefPtr<Paragraph>& GetDragParagraph() const override
304     {
305         return pManager_->GetParagraphs().front().paragraph;
306     }
307 
CloseKeyboard(bool)308     bool CloseKeyboard(bool /* forceClose */) override
309     {
310         return true;
311     }
312     virtual void CloseSelectOverlay() override;
313     void CloseSelectOverlay(bool animation);
314     void CreateHandles() override;
315     bool BetweenSelectedPosition(const Offset& globalOffset) override;
316 
317     // end of TextDragBase implementations
318     // ===========================================================
319 
320     void InitSurfaceChangedCallback();
321     void InitSurfacePositionChangedCallback();
322     virtual void HandleSurfaceChanged(int32_t newWidth, int32_t newHeight, int32_t prevWidth, int32_t prevHeight);
HandleSurfacePositionChanged(int32_t posX,int32_t posY)323     virtual void HandleSurfacePositionChanged(int32_t posX, int32_t posY) {};
HasSurfaceChangedCallback()324     bool HasSurfaceChangedCallback()
325     {
326         return surfaceChangedCallbackId_.has_value();
327     }
UpdateSurfaceChangedCallbackId(int32_t id)328     void UpdateSurfaceChangedCallbackId(int32_t id)
329     {
330         surfaceChangedCallbackId_ = id;
331     }
332 
HasSurfacePositionChangedCallback()333     bool HasSurfacePositionChangedCallback()
334     {
335         return surfacePositionChangedCallbackId_.has_value();
336     }
UpdateSurfacePositionChangedCallbackId(int32_t id)337     void UpdateSurfacePositionChangedCallbackId(int32_t id)
338     {
339         surfacePositionChangedCallbackId_ = id;
340     }
341 
SetOnClickEvent(GestureEventFunc && onClick)342     void SetOnClickEvent(GestureEventFunc&& onClick)
343     {
344         onClick_ = std::move(onClick);
345     }
346     virtual void OnColorConfigurationUpdate() override;
347 
348     NG::DragDropInfo OnDragStart(const RefPtr<Ace::DragEvent>& event, const std::string& extraParams);
349     DragDropInfo OnDragStartNoChild(const RefPtr<Ace::DragEvent>& event, const std::string& extraParams);
350     void InitDragEvent();
351     void UpdateSpanItemDragStatus(const std::list<ResultObject>& resultObjects, bool IsDragging);
352     void OnDragMove(const RefPtr<Ace::DragEvent>& event);
353     virtual std::function<void(Offset)> GetThumbnailCallback();
354     std::list<ResultObject> dragResultObjects_;
355     std::list<ResultObject> recoverDragResultObjects_;
356     std::vector<RefPtr<SpanItem>> dragSpanItems_;
357     void OnDragEnd(const RefPtr<Ace::DragEvent>& event);
358     void OnDragEndNoChild(const RefPtr<Ace::DragEvent>& event);
359     void CloseOperate();
360     virtual void AddUdmfData(const RefPtr<Ace::DragEvent>& event);
361     void ProcessNormalUdmfData(const RefPtr<UnifiedData>& unifiedData);
362     void AddPixelMapToUdmfData(const RefPtr<PixelMap>& pixelMap, const RefPtr<UnifiedData>& unifiedData);
363     std::string GetSelectedSpanText(std::wstring value, int32_t start, int32_t end) const;
364 
365     TextStyleResult GetTextStyleObject(const RefPtr<SpanNode>& node);
366     SymbolSpanStyle GetSymbolSpanStyleObject(const RefPtr<SpanNode>& node);
367     RefPtr<UINode> GetChildByIndex(int32_t index) const;
368     RefPtr<SpanItem> GetSpanItemByIndex(int32_t index) const;
369     ResultObject GetTextResultObject(RefPtr<UINode> uinode, int32_t index, int32_t start, int32_t end);
370     virtual void SetResultObjectText(ResultObject& resultObject, const RefPtr<SpanItem>& spanItem);
371     ResultObject GetSymbolSpanResultObject(RefPtr<UINode> uinode, int32_t index, int32_t start, int32_t end);
372     ResultObject GetImageResultObject(RefPtr<UINode> uinode, int32_t index, int32_t start, int32_t end);
373     std::string GetFontInJson() const;
374     std::string GetBindSelectionMenuInJson() const;
FillPreviewMenuInJson(const std::unique_ptr<JsonValue> & jsonValue)375     virtual void FillPreviewMenuInJson(const std::unique_ptr<JsonValue>& jsonValue) const {}
376 
GetDragContents()377     const std::vector<std::string>& GetDragContents() const
378     {
379         return dragContents_;
380     }
381 
InitSpanImageLayout(const std::vector<int32_t> & placeholderIndex,const std::vector<RectF> & rectsForPlaceholders,OffsetF contentOffset)382     void InitSpanImageLayout(const std::vector<int32_t>& placeholderIndex,
383         const std::vector<RectF>& rectsForPlaceholders, OffsetF contentOffset) override
384     {
385         placeholderIndex_ = placeholderIndex;
386         imageOffset_ = contentOffset;
387         rectsForPlaceholders_ = rectsForPlaceholders;
388     }
389 
GetPlaceHolderIndex()390     const std::vector<int32_t>& GetPlaceHolderIndex()
391     {
392         return placeholderIndex_;
393     }
394 
GetRectsForPlaceholders()395     const std::vector<RectF>& GetRectsForPlaceholders()
396     {
397         return rectsForPlaceholders_;
398     }
399 
GetContentOffset()400     OffsetF GetContentOffset() override
401     {
402         return imageOffset_;
403     }
404 
IsMeasureBoundary()405     bool IsMeasureBoundary() const override
406     {
407         return isMeasureBoundary_;
408     }
409 
SetIsMeasureBoundary(bool isMeasureBoundary)410     void SetIsMeasureBoundary(bool isMeasureBoundary)
411     {
412         isMeasureBoundary_ = isMeasureBoundary;
413     }
414 
SetIsCustomFont(bool isCustomFont)415     void SetIsCustomFont(bool isCustomFont)
416     {
417         isCustomFont_ = isCustomFont;
418     }
419 
GetIsCustomFont()420     bool GetIsCustomFont()
421     {
422         return isCustomFont_;
423     }
424 
SetImageSpanNodeList(std::vector<WeakPtr<FrameNode>> imageNodeList)425     void SetImageSpanNodeList(std::vector<WeakPtr<FrameNode>> imageNodeList)
426     {
427         imageNodeList_ = imageNodeList;
428     }
429 
GetImageSpanNodeList()430     std::vector<WeakPtr<FrameNode>> GetImageSpanNodeList()
431     {
432         return imageNodeList_;
433     }
434     // Deprecated: Use the TextSelectOverlay::ProcessOverlay() instead.
435     // It is currently used by RichEditorPattern.
436     virtual void UpdateSelectOverlayOrCreate(SelectOverlayInfo& selectInfo, bool animation = false);
437     // Deprecated: Use the TextSelectOverlay::CheckHandleVisible() instead.
438     // It is currently used by RichEditorPattern.
CheckHandles(SelectHandleInfo & handleInfo)439     virtual void CheckHandles(SelectHandleInfo& handleInfo) {};
440     OffsetF GetDragUpperLeftCoordinates() override;
441     void SetTextSelection(int32_t selectionStart, int32_t selectionEnd);
442 
443     // Deprecated: Use the TextSelectOverlay::OnHandleMove() instead.
444     // It is currently used by RichEditorPattern.
445     void OnHandleMove(const RectF& handleRect, bool isFirstHandle) override;
446 
GetParagraphs()447     virtual std::list<ParagraphManager::ParagraphInfo> GetParagraphs() const
448     {
449         std::list<ParagraphManager::ParagraphInfo> res;
450         CHECK_NULL_RETURN(pManager_, res);
451         return pManager_->GetParagraphs();
452     }
453 
GetParagraphManager()454     const RefPtr<ParagraphManager>& GetParagraphManager() const
455     {
456         return pManager_;
457     }
458 
MarkContentChange()459     void MarkContentChange()
460     {
461         contChange_ = true;
462     }
463 
ResetContChange()464     void ResetContChange()
465     {
466         contChange_ = false;
467     }
468 
GetContChange()469     bool GetContChange() const
470     {
471         return contChange_;
472     }
473 
GetShowSelect()474     bool GetShowSelect() const
475     {
476         return showSelect_;
477     }
478 
GetRecoverStart()479     int32_t GetRecoverStart() const
480     {
481         return recoverStart_;
482     }
483 
GetRecoverEnd()484     int32_t GetRecoverEnd() const
485     {
486         return recoverEnd_;
487     }
488 
489     void OnHandleAreaChanged() override;
490     void RemoveAreaChangeInner();
491 
ResetDragOption()492     void ResetDragOption() override
493     {
494         CloseSelectOverlay();
495         ResetSelection();
496     }
497 
498     virtual bool NeedShowAIDetect();
499 
GetDragRecordSize()500     int32_t GetDragRecordSize() override
501     {
502         return dragRecordSize_;
503     }
504 
ResetDragRecordSize(int32_t size)505     void ResetDragRecordSize(int32_t size)
506     {
507         dragRecordSize_ = size;
508     }
509 
510     void BindSelectionMenu(TextSpanType spanType, TextResponseType responseType, std::function<void()>& menuBuilder,
511         std::function<void(int32_t, int32_t)>& onAppear, std::function<void()>& onDisappear);
512 
SetTextController(const RefPtr<TextController> & controller)513     void SetTextController(const RefPtr<TextController>& controller)
514     {
515         textController_ = controller;
516     }
517 
GetTextController()518     const RefPtr<TextController>& GetTextController()
519     {
520         return textController_;
521     }
522 
523     void CloseSelectionMenu();
524 
ClearSelectionMenu()525     void ClearSelectionMenu()
526     {
527         selectionMenuMap_.clear();
528     }
529 
530     virtual const std::list<RefPtr<UINode>>& GetAllChildren() const;
531 
532     void StartVibratorByIndexChange(int32_t currentIndex, int32_t preIndex);
533 
534     void HandleSelectionChange(int32_t start, int32_t end);
535 
GetCopyOptions()536     CopyOptions GetCopyOptions() const
537     {
538         return copyOption_;
539     }
540     bool CheckClickedOnSpanOrText(RectF textContentRect, const Offset& localLocation);
541 
542     // style string
SetSpanItemChildren(const std::list<RefPtr<SpanItem>> & spans)543     void SetSpanItemChildren(const std::list<RefPtr<SpanItem>>& spans)
544     {
545         spans_ = spans;
546     }
SetSpanStringMode(bool isSpanStringMode)547     void SetSpanStringMode(bool isSpanStringMode)
548     {
549         isSpanStringMode_ = isSpanStringMode;
550     }
GetSpanStringMode()551     bool GetSpanStringMode() const
552     {
553         return isSpanStringMode_;
554     }
555     void SetStyledString(const RefPtr<SpanString>& value);
556     // select overlay
557     virtual int32_t GetHandleIndex(const Offset& offset) const;
558     std::string GetSelectedText(int32_t start, int32_t end) const;
559     void UpdateSelectionSpanType(int32_t selectStart, int32_t selectEnd);
560     void CalculateHandleOffsetAndShowOverlay(bool isUsingMouse = false);
561     void ResetSelection();
562     bool IsSelectAll();
563     void HandleOnCopy();
564     void HandleOnCopySpanString();
565     virtual void HandleOnSelectAll();
566     void SetTextSelectableMode(TextSelectableMode value);
567 
GetTextPaintOffset()568     OffsetF GetTextPaintOffset() const override
569     {
570         return parentGlobalOffset_;
571     }
572 
SetTextResponseType(TextResponseType type)573     void SetTextResponseType(TextResponseType type)
574     {
575         textResponseType_ = type;
576     }
577 
CheckSelectedTypeChange()578     bool CheckSelectedTypeChange()
579     {
580         auto changed = selectedType_.has_value() && oldSelectedType_ != selectedType_.value();
581         if (changed) {
582             oldSelectedType_ = selectedType_.value();
583         }
584         return changed;
585     }
586 
IsUsingMouse()587     bool IsUsingMouse()
588     {
589         return sourceType_ == SourceType::MOUSE;
590     }
591 
592     void OnSensitiveStyleChange(bool isSensitive) override;
593 
594     bool IsSetObscured();
595     bool IsSensitiveEnalbe();
596 
CopySelectionMenuParams(SelectOverlayInfo & selectInfo)597     void CopySelectionMenuParams(SelectOverlayInfo& selectInfo)
598     {
599         CopySelectionMenuParams(selectInfo, textResponseType_.value_or(TextResponseType::NONE));
600     }
601 
InitCustomSpanPlaceholderInfo(const std::vector<CustomSpanPlaceholderInfo> & customSpanPlaceholder)602     void InitCustomSpanPlaceholderInfo(const std::vector<CustomSpanPlaceholderInfo>& customSpanPlaceholder)
603     {
604         customSpanPlaceholder_ = customSpanPlaceholder;
605     }
606 
GetCustomSpanPlaceholderInfo()607     std::vector<CustomSpanPlaceholderInfo> GetCustomSpanPlaceholderInfo()
608     {
609         return customSpanPlaceholder_;
610     }
611 
ClearCustomSpanPlaceholderInfo()612     void ClearCustomSpanPlaceholderInfo()
613     {
614         customSpanPlaceholder_.clear();
615     }
616 
GetChildNodes()617     const std::list<RefPtr<UINode>>& GetChildNodes() const
618     {
619         return childNodes_;
620     }
621 
622     // add for capi NODE_TEXT_CONTENT_WITH_STYLED_STRING
SetExternalParagraph(void * paragraph)623     void SetExternalParagraph(void* paragraph)
624     {
625         externalParagraph_ = paragraph;
626     }
627 
GetExternalParagraph()628     const std::optional<void*>& GetExternalParagraph()
629     {
630         return externalParagraph_;
631     }
632 
633     void SetExternalSpanItem(const std::list<RefPtr<SpanItem>>& spans);
634 
SetExternalParagraphStyle(std::optional<ParagraphStyle> paragraphStyle)635     void SetExternalParagraphStyle(std::optional<ParagraphStyle> paragraphStyle)
636     {
637         externalParagraphStyle_ = paragraphStyle;
638     }
639 
GetTextStyle()640     TextStyle GetTextStyle()
641     {
642         return textStyle_.value_or(TextStyle());
643     }
644 
GetExternalParagraphStyle()645     std::optional<ParagraphStyle> GetExternalParagraphStyle()
646     {
647         return externalParagraphStyle_;
648     }
649 
650     size_t GetLineCount() const override;
651     TextLineMetrics GetLineMetrics(int32_t lineNumber) override;
652     std::vector<ParagraphManager::TextBox> GetRectsForRange(int32_t start, int32_t end,
653         RectHeightStyle heightStyle, RectWidthStyle widthStyle) override;
654     PositionWithAffinity GetGlyphPositionAtCoordinate(int32_t x, int32_t y) override;
655 
656     void OnSelectionMenuOptionsUpdate(
657         const NG::OnCreateMenuCallback&& onCreateMenuCallback, const NG::OnMenuItemClickCallback&& onMenuItemClick);
658     void OnFrameNodeChanged(FrameNodeChangeInfoFlag flag) override;
659 
UpdateParentGlobalOffset()660     void UpdateParentGlobalOffset()
661     {
662         parentGlobalOffset_ = GetParentGlobalOffset();
663     }
664 
SetPrintInfo(const std::string & area,const OffsetF & paintOffset)665     void SetPrintInfo(const std::string& area, const OffsetF& paintOffset)
666     {
667         paintInfo_ = area + paintOffset.ToString();
668     }
669 
DumpRecord(const std::string & record)670     void DumpRecord(const std::string& record)
671     {
672         frameRecord_ = record;
673     }
674 
SetIsUserSetResponseRegion(bool isUserSetResponseRegion)675     void SetIsUserSetResponseRegion(bool isUserSetResponseRegion)
676     {
677         isUserSetResponseRegion_ = isUserSetResponseRegion;
678     }
679 
680     size_t GetSubComponentInfos(std::vector<SubComponentInfo>& subComponentInfos);
681 
682     void UpdateFontColor(const Color& value);
683     void BeforeCreatePaintWrapper() override;
684 
685     void OnTextOverflowChanged();
686 
687     void MarkDirtyNodeRender();
688     void ChangeHandleHeight(const GestureEvent& event, bool isFirst, bool isOverlayMode);
689     void ChangeFirstHandleHeight(const Offset& touchOffset, RectF& handleRect);
690     void ChangeSecondHandleHeight(const Offset& touchOffset, RectF& handleRect);
691     virtual void CalculateDefaultHandleHeight(float& height);
692 
GetSystemTimestamp()693     uint64_t GetSystemTimestamp()
694     {
695         return static_cast<uint64_t>(
696             std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
697                 .count());
698     }
699 
SetEnableHapticFeedback(bool isEnabled)700     void SetEnableHapticFeedback(bool isEnabled)
701     {
702         isEnableHapticFeedback_ = isEnabled;
703     }
704     void BeforeSyncGeometryProperties(const DirtySwapConfig& config) override;
705 
RegisterAfterLayoutCallback(std::function<void ()> callback)706     void RegisterAfterLayoutCallback(std::function<void()> callback)
707     {
708         afterLayoutCallback_ = callback;
709     }
710 
UnRegisterAfterLayoutCallback()711     void UnRegisterAfterLayoutCallback()
712     {
713         afterLayoutCallback_ = std::nullopt;
714     }
715 
716     virtual Color GetUrlSpanColor();
717     void DoTextSelectionTouchCancel() override;
718 
719     std::string GetCaretColor() const;
720     std::string GetSelectedBackgroundColor() const;
721 
722 protected:
GetClickedSpanPosition()723     int32_t GetClickedSpanPosition()
724     {
725         return clickedSpanPosition_;
726     }
727     void OnAttachToFrameNode() override;
728     void OnDetachFromFrameNode(FrameNode* node) override;
729     void OnAfterModifyDone() override;
730     virtual bool ClickAISpan(const PointF& textOffset, const AISpan& aiSpan);
731     void InitMouseEvent();
732     void RecoverSelection();
HandleOnCameraInput()733     virtual void HandleOnCameraInput() {};
734     void InitSelection(const Offset& pos);
735     void StartVibratorByLongPress();
736     void HandleLongPress(GestureEvent& info);
737     void HandleClickEvent(GestureEvent& info);
738     void HandleSingleClickEvent(GestureEvent& info);
739     void HandleClickAISpanEvent(const PointF& info);
740     void HandleDoubleClickEvent(GestureEvent& info);
741     void CheckOnClickEvent(GestureEvent& info);
742     void HandleClickOnTextAndSpan(GestureEvent& info);
743     void RecordClickEvent();
744     void ActTextOnClick(GestureEvent& info);
745     void RecordSpanClickEvent(const RefPtr<SpanItem>& span);
746     bool ShowAIEntityMenu(const AISpan& aiSpan, const CalculateHandleFunc& calculateHandleFunc = nullptr,
747         const ShowSelectOverlayFunc& showSelectOverlayFunc = nullptr);
748     void SetOnClickMenu(const AISpan& aiSpan, const CalculateHandleFunc& calculateHandleFunc,
749         const ShowSelectOverlayFunc& showSelectOverlayFunc);
750     bool IsDraggable(const Offset& localOffset);
751     virtual void InitClickEvent(const RefPtr<GestureEventHub>& gestureHub);
752     void ShowSelectOverlay(const OverlayRequest& = OverlayRequest());
753     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
754     std::wstring GetWideText() const;
755     void CalcCaretMetricsByPosition(
756         int32_t extent, CaretMetricsF& caretCaretMetric, TextAffinity textAffinity = TextAffinity::DOWNSTREAM);
757     void UpdateSelectionType(const SelectionInfo& selection);
758     void CopyBindSelectionMenuParams(SelectOverlayInfo& selectInfo, std::shared_ptr<SelectionMenuParams> menuParams);
759     bool IsSelectedBindSelectionMenu();
760     bool CheckAndClick(const RefPtr<SpanItem>& item);
761     bool CalculateClickedSpanPosition(const PointF& textOffset);
762     void HiddenMenu();
763     std::shared_ptr<SelectionMenuParams> GetMenuParams(TextSpanType type, TextResponseType responseType);
764     bool MaxLinesZero();
765     void AddUdmfTxtPreProcessor(const ResultObject src, ResultObject& result, bool isAppend);
766     void InitKeyEvent();
767     bool HandleKeyEvent(const KeyEvent& keyEvent);
768     void HandleOnSelect(KeyCode code);
769     void HandleSelectionUp();
770     void HandleSelectionDown();
771     void HandleSelection(bool isEmojiStart, int32_t end);
772     double GetTextHeight(int32_t index, bool isNextLine);
773     int32_t GetActualTextLength();
774     bool IsSelectableAndCopy();
775     void SetResponseRegion(const SizeF& frameSize, const SizeF& boundsSize);
776 
777     virtual bool CanStartAITask();
778 
779     void MarkDirtySelf();
OnAttachToMainTree()780     void OnAttachToMainTree() override
781     {
782         isDetachFromMainTree_ = false;
783     }
784 
OnDetachFromMainTree()785     void OnDetachFromMainTree() override
786     {
787         isDetachFromMainTree_ = true;
788     }
789 
790     bool SetActionExecSubComponent();
791     void GetSubComponentInfosForAISpans(std::vector<SubComponentInfo>& subComponentInfos);
792     void GetSubComponentInfosForSpans(std::vector<SubComponentInfo>& subComponentInfos);
793     bool ExecSubComponent(int32_t spanId);
794     void AddSubComponentInfosByDataDetectorForSpan(std::vector<SubComponentInfo>& subComponentInfos,
795         const RefPtr<SpanItem>& span);
796     void AddSubComponentInfoForAISpan(std::vector<SubComponentInfo>& subComponentInfos, const std::string& content,
797         const AISpan& aiSpan);
798     void AddSubComponentInfoForSpan(std::vector<SubComponentInfo>& subComponentInfos, const std::string& content,
799         const RefPtr<SpanItem>& span);
800 
801     int32_t GetTouchIndex(const OffsetF& offset) override;
802     void OnTextGestureSelectionUpdate(int32_t start, int32_t end, const TouchEventInfo& info) override;
803     void OnTextGenstureSelectionEnd() override;
804     void StartGestureSelection(int32_t start, int32_t end, const Offset& startOffset) override;
805 
806     bool enabled_ = true;
807     Status status_ = Status::NONE;
808     bool contChange_ = false;
809     int32_t recoverStart_ = 0;
810     int32_t recoverEnd_ = 0;
811     bool mouseEventInitialized_ = false;
812     bool panEventInitialized_ = false;
813     bool clickEventInitialized_ = false;
814     bool touchEventInitialized_ = false;
815     bool isSpanStringMode_ = false;
816     RefPtr<MutableSpanString> styledString_ = MakeRefPtr<MutableSpanString>("");
817     bool keyEventInitialized_ = false;
818 
819     RefPtr<FrameNode> dragNode_;
820     RefPtr<LongPressEvent> longPressEvent_;
821     // Deprecated: Use the selectOverlay_ instead.
822     RefPtr<SelectOverlayProxy> selectOverlayProxy_;
823     RefPtr<Clipboard> clipboard_;
824     RefPtr<TextContentModifier> contentMod_;
825     RefPtr<TextOverlayModifier> overlayMod_;
826     CopyOptions copyOption_ = CopyOptions::None;
827 
828     std::string textForDisplay_;
829     std::string paintInfo_ = "NA";
830     std::string frameRecord_ = "NA";
831     std::optional<TextStyle> textStyle_;
832     std::list<RefPtr<SpanItem>> spans_;
833     mutable std::list<RefPtr<UINode>> childNodes_;
834     float baselineOffset_ = 0.0f;
835     int32_t placeholderCount_ = 0;
836     SelectMenuInfo selectMenuInfo_;
837     std::vector<RectF> dragBoxes_;
838     std::map<std::pair<TextSpanType, TextResponseType>, std::shared_ptr<SelectionMenuParams>> selectionMenuMap_;
839     std::optional<TextSpanType> selectedType_;
840     SourceType sourceType_ = SourceType::NONE;
841 
842     // properties for AI
843     bool textDetectEnable_ = false;
844     RefPtr<DataDetectorAdapter> dataDetectorAdapter_ = MakeRefPtr<DataDetectorAdapter>();
845 
846     OffsetF parentGlobalOffset_;
847     std::optional<TextResponseType> textResponseType_;
848     MouseFormat currentMouseStyle_ = MouseFormat::DEFAULT;
849 
850     struct SubComponentInfoEx {
851         std::optional<AISpan> aiSpan;
852         WeakPtr<SpanItem> span;
853     };
854     std::vector<SubComponentInfoEx> subComponentInfos_;
855     virtual std::vector<RectF> GetSelectedRects(int32_t start, int32_t end);
856     RefPtr<MultipleClickRecognizer> multipleClickRecognizer_ = MakeRefPtr<MultipleClickRecognizer>();
857     bool ShowShadow(const PointF& textOffset, const Color& color);
858     virtual PointF GetTextOffset(const Offset& localLocation, const RectF& contentRect);
859     bool hasUrlSpan_ = false;
860 
861 private:
862     void InitLongPressEvent(const RefPtr<GestureEventHub>& gestureHub);
863     void HandleSpanLongPressEvent(GestureEvent& info);
864     void HandleMouseEvent(const MouseInfo& info);
865     void OnHandleTouchUp();
866     void InitTouchEvent();
867     void HandleTouchEvent(const TouchEventInfo& info);
868     void UpdateChildProperty(const RefPtr<SpanNode>& child) const;
869     void ActSetSelection(int32_t start, int32_t end);
870     bool IsShowHandle();
871     void InitUrlMouseEvent();
872     void InitUrlTouchEvent();
873     void HandleUrlMouseEvent(const MouseInfo& info);
874     void HandleUrlTouchEvent(const TouchEventInfo& info);
875     void URLOnHover(bool isHover);
876     bool HandleUrlClick();
877     std::pair<int32_t, int32_t> GetStartAndEnd(int32_t start);
878     Color GetUrlHoverColor();
879     Color GetUrlPressColor();
880     void SetAccessibilityAction();
881     void CollectSpanNodes(std::stack<SpanNodeInfo> nodes, bool& isSpanHasClick);
882     void CollectTextSpanNodes(const RefPtr<SpanNode>& child, bool& isSpanHasClick);
883     void UpdateContainerChildren(const RefPtr<UINode>& parent, const RefPtr<UINode>& child);
884     RefPtr<RenderContext> GetRenderContext();
885     void ProcessBoundRectByTextShadow(RectF& rect);
886     void FireOnSelectionChange(int32_t start, int32_t end);
887     void HandleMouseLeftButton(const MouseInfo& info, const Offset& textOffset);
888     void HandleMouseRightButton(const MouseInfo& info, const Offset& textOffset);
889     void HandleMouseLeftPressAction(const MouseInfo& info, const Offset& textOffset);
890     void HandleMouseLeftReleaseAction(const MouseInfo& info, const Offset& textOffset);
891     void HandleMouseLeftMoveAction(const MouseInfo& info, const Offset& textOffset);
892     void InitSpanItem(std::stack<SpanNodeInfo> nodes);
893     int32_t GetSelectionSpanItemIndex(const MouseInfo& info);
894     void CopySelectionMenuParams(SelectOverlayInfo& selectInfo, TextResponseType responseType);
895     void ProcessBoundRectByTextMarquee(RectF& rect);
896     ResultObject GetBuilderResultObject(RefPtr<UINode> uiNode, int32_t index, int32_t start, int32_t end);
897     void CreateModifier();
898     bool DidExceedMaxLines() const override;
899     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
900     void ProcessOverlayAfterLayout();
901     // SpanString
902     void MountImageNode(const RefPtr<ImageSpanItem>& imageItem);
903     ImageSourceInfo CreateImageSourceInfo(const ImageSpanOptions& options);
904     void ProcessSpanString();
905     // to check if drag is in progress
SetCurrentDragTool(SourceTool tool)906     void SetCurrentDragTool(SourceTool tool)
907     {
908         lastDragTool_ = tool;
909     }
910 
GetContextParam()911     std::optional<RenderContext::ContextParam> GetContextParam() const override
912     {
913         return RenderContext::ContextParam { RenderContext::ContextType::CANVAS };
914     }
915 
GetCurrentDragTool()916     SourceTool GetCurrentDragTool() const
917     {
918         return lastDragTool_;
919     }
920     Offset ConvertGlobalToLocalOffset(const Offset& globalOffset);
921     Offset ConvertLocalOffsetToParagraphOffset(const Offset& offset);
922     void ProcessMarqueeVisibleAreaCallback();
923     void ParseOriText(const std::string& currentText);
924     bool IsMarqueeOverflow() const;
925     virtual void ResetAfterTextChange();
926 
927     bool isMeasureBoundary_ = false;
928     bool isMousePressed_ = false;
929     bool leftMousePressed_ = false;
930     bool isCustomFont_ = false;
931     bool blockPress_ = false;
932     bool isDoubleClick_ = false;
933     bool isSensitive_ = false;
934     bool hasSpanStringLongPressEvent_ = false;
935     int32_t clickedSpanPosition_ = -1;
936     bool isEnableHapticFeedback_ = true;
937 
938     bool urlTouchEventInitialized_ = false;
939     bool urlMouseEventInitialized_ = false;
940 
941     RefPtr<ParagraphManager> pManager_;
942     std::vector<int32_t> placeholderIndex_;
943     std::vector<int32_t> customSpanIndex_;
944     std::vector<RectF> rectsForPlaceholders_;
945     OffsetF imageOffset_;
946 
947     OffsetF contentOffset_;
948     GestureEventFunc onClick_;
949     RefPtr<DragWindow> dragWindow_;
950     RefPtr<DragDropProxy> dragDropProxy_;
951     std::optional<int32_t> surfaceChangedCallbackId_;
952     SourceTool lastDragTool_ = SourceTool::UNKNOWN;
953     std::optional<int32_t> surfacePositionChangedCallbackId_;
954     int32_t dragRecordSize_ = -1;
955     RefPtr<TextController> textController_;
956     TextSpanType oldSelectedType_ = TextSpanType::NONE;
957     bool isShowMenu_ = true;
958     RefPtr<TextSelectOverlay> selectOverlay_;
959     std::vector<WeakPtr<FrameNode>> imageNodeList_;
960     bool isDetachFromMainTree_ = false;
961     std::vector<CustomSpanPlaceholderInfo> customSpanPlaceholder_;
962     std::optional<void*> externalParagraph_;
963     std::optional<ParagraphStyle> externalParagraphStyle_;
964     bool isUserSetResponseRegion_ = false;
965     WeakPtr<PipelineContext> pipeline_;
966     WeakPtr<ScrollablePattern> scrollableParent_;
967     ACE_DISALLOW_COPY_AND_MOVE(TextPattern);
968     std::optional<std::function<void()>> afterLayoutCallback_;
969 };
970 } // namespace OHOS::Ace::NG
971 
972 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_PATTERN_H
973