1 /* 2 * Copyright (c) 2022 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 <string> 20 21 #include "base/geometry/dimension.h" 22 #include "base/memory/referenced.h" 23 #include "base/utils/noncopyable.h" 24 #include "core/components_ng/event/long_press_event.h" 25 #include "core/components_ng/pattern/pattern.h" 26 #include "core/components_ng/pattern/text/span_node.h" 27 #include "core/components_ng/pattern/text/text_accessibility_property.h" 28 #include "core/components_ng/pattern/text/text_layout_algorithm.h" 29 #include "core/components_ng/pattern/text/text_layout_property.h" 30 #include "core/components_ng/pattern/text/text_paint_method.h" 31 #include "core/components_ng/pattern/text_field/text_selector.h" 32 #include "core/components_ng/property/property.h" 33 #include "core/components_ng/render/paragraph.h" 34 #include "core/pipeline_ng/ui_task_scheduler.h" 35 36 namespace OHOS::Ace::NG { 37 // TextPattern is the base class for text render node to perform paint text. 38 class TextPattern : public Pattern { 39 DECLARE_ACE_TYPE(TextPattern, Pattern); 40 41 public: 42 TextPattern() = default; 43 ~TextPattern() override = default; 44 CreateNodePaintMethod()45 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 46 { 47 return MakeRefPtr<TextPaintMethod>(WeakClaim(this), paragraph_, baselineOffset_); 48 } 49 CreateLayoutProperty()50 RefPtr<LayoutProperty> CreateLayoutProperty() override 51 { 52 return MakeRefPtr<TextLayoutProperty>(); 53 } 54 CreateLayoutAlgorithm()55 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 56 { 57 return MakeRefPtr<TextLayoutAlgorithm>(spanItemChildren_, paragraph_); 58 } 59 CreateAccessibilityProperty()60 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 61 { 62 return MakeRefPtr<TextAccessibilityProperty>(); 63 } 64 IsAtomicNode()65 bool IsAtomicNode() const override 66 { 67 return false; 68 } 69 70 void OnModifyDone() override; 71 72 void BeforeCreateLayoutWrapper() override; 73 AddChildSpanItem(const RefPtr<SpanNode> & child)74 void AddChildSpanItem(const RefPtr<SpanNode>& child) 75 { 76 spanItemChildren_.emplace_back(child->GetSpanItem()); 77 } 78 GetFocusPattern()79 FocusPattern GetFocusPattern() const override 80 { 81 return { FocusType::NODE, false }; 82 } 83 84 void DumpInfo() override; 85 GetTextSelector()86 TextSelector GetTextSelector() const 87 { 88 return textSelector_; 89 } 90 GetTextForDisplay()91 std::string GetTextForDisplay() const 92 { 93 return textForDisplay_; 94 } 95 96 private: 97 void OnDetachFromFrameNode(FrameNode* node) override; 98 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 99 100 void HandleLongPress(GestureEvent& info); 101 void HandleOnSelectAll(); 102 void HandleOnCopy(); 103 void OnHandleMove(const RectF& handleRect, bool isFirstHandle); 104 void OnHandleMoveDone(const RectF& handleRect, bool isFirstHandle); 105 void InitLongPressEvent(const RefPtr<GestureEventHub>& gestureHub); 106 void InitMouseEvent(); 107 void HandleMouseEvent(const MouseInfo& info); 108 void OnHandleTouchUp(); 109 void InitClickEvent(const RefPtr<GestureEventHub>& gestureHub); 110 void HandleClickEvent(GestureEvent& info); 111 112 void ShowSelectOverlay(const RectF& firstHandle, const RectF& secondHandle); 113 void InitSelection(const Offset& pos); 114 void CalcuateHandleOffsetAndShowOverlay(bool isUsingMouse = false); 115 116 int32_t GetGraphemeClusterLength(int32_t extend) const; 117 OffsetF CalcCursorOffsetByPosition(int32_t position, float& selectLineHeight); 118 std::string GetSelectedText(int32_t start, int32_t end) const; 119 std::wstring GetWideText() const; 120 121 std::list<RefPtr<SpanItem>> spanItemChildren_; 122 std::string textForDisplay_; 123 RefPtr<Paragraph> paragraph_; 124 RefPtr<LongPressEvent> longPressEvent_; 125 RefPtr<SelectOverlayProxy> selectOverlayProxy_; 126 RefPtr<Clipboard> clipboard_; 127 CopyOptions copyOption_ = CopyOptions::None; 128 TextSelector textSelector_; 129 OffsetF contentOffset_; 130 RectF contentRect_; 131 float baselineOffset_ = 0.0f; 132 bool clickEventInitialized_ = false; 133 bool mouseEventInitialized_ = false; 134 135 ACE_DISALLOW_COPY_AND_MOVE(TextPattern); 136 }; 137 } // namespace OHOS::Ace::NG 138 139 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_PATTERN_H 140