1 /*
2 * Copyright (c) 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_PATTERN_TEXT_TEXT_BASE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_BASE_H
18
19 #include "base/memory/ace_type.h"
20 #include "base/memory/referenced.h"
21 #include "core/common/clipboard/clipboard.h"
22 #include "core/components_ng/manager/select_overlay/select_overlay_client.h"
23 #include "core/components_ng/pattern/text_field/text_selector.h"
24 #include "core/components_ng/render/paragraph.h"
25
26 namespace OHOS::Ace::NG {
27
28 enum class MouseStatus { PRESSED, RELEASED, MOVE, NONE };
29
30 struct HandleMoveStatus {
31 bool isFirsthandle = false;
32 int32_t position = -1;
33 OffsetF handleOffset;
34
ResetHandleMoveStatus35 void Reset()
36 {
37 isFirsthandle = false;
38 position = -1;
39 }
40
IsValidHandleMoveStatus41 bool IsValid()
42 {
43 return position >= 0;
44 }
45 };
46
47 template<typename T>
GetTextCaretMetrics(RefPtr<FrameNode> & targetNode,CaretMetricsF & caretMetrics)48 void GetTextCaretMetrics(RefPtr<FrameNode>& targetNode, CaretMetricsF& caretMetrics)
49 {
50 CHECK_NULL_VOID(targetNode);
51 if (targetNode->GetTag() == V2::SEARCH_ETS_TAG) {
52 auto textFieldFrameNode = AceType::DynamicCast<FrameNode>(targetNode->GetChildren().front());
53 CHECK_NULL_VOID(textFieldFrameNode);
54 auto textPattern = textFieldFrameNode->GetPattern<T>();
55 CHECK_NULL_VOID(textPattern);
56 textPattern->GetCaretMetrics(caretMetrics);
57 } else {
58 auto textPattern = targetNode->GetPattern<T>();
59 CHECK_NULL_VOID(textPattern);
60 textPattern->GetCaretMetrics(caretMetrics);
61 }
62 }
63
64 class TextGestureSelector : public virtual AceType {
65 DECLARE_ACE_TYPE(TextGestureSelector, AceType);
66
67 public:
StartGestureSelection(int32_t start,int32_t end,const Offset & startOffset)68 virtual void StartGestureSelection(int32_t start, int32_t end, const Offset& startOffset)
69 {
70 start_ = start;
71 end_ = end;
72 isStarted_ = start_ <= end_;
73 startOffset_ = startOffset;
74 }
75
EndGestureSelection()76 void EndGestureSelection()
77 {
78 if (!isStarted_) {
79 return;
80 }
81 OnTextGenstureSelectionEnd();
82 start_ = -1;
83 end_ = -1;
84 isStarted_ = false;
85 startOffset_.Reset();
86 isSelecting_ = false;
87 }
88
89 void DoGestureSelection(const TouchEventInfo& info);
90
91 protected:
GetTouchIndex(const OffsetF & offset)92 virtual int32_t GetTouchIndex(const OffsetF& offset)
93 {
94 return -1;
95 }
OnTextGestureSelectionUpdate(int32_t start,int32_t end,const TouchEventInfo & info)96 virtual void OnTextGestureSelectionUpdate(int32_t start, int32_t end, const TouchEventInfo& info) {}
OnTextGenstureSelectionEnd()97 virtual void OnTextGenstureSelectionEnd() {}
DoTextSelectionTouchCancel()98 virtual void DoTextSelectionTouchCancel() {}
99 private:
100 void DoTextSelectionTouchMove(const TouchEventInfo& info);
101 int32_t start_ = -1;
102 int32_t end_ = -1;
103 bool isStarted_ = false;
104 bool isSelecting_ = false;
105 Dimension minMoveDistance_ = 5.0_vp;
106 Offset startOffset_;
107 };
108
109 class TextBase : public SelectOverlayClient {
110 DECLARE_ACE_TYPE(TextBase, SelectOverlayClient);
111
112 public:
113 TextBase() = default;
114 ~TextBase() override = default;
115
GetContentOffset()116 virtual OffsetF GetContentOffset()
117 {
118 return OffsetF(0, 0);
119 }
120
OnBackPressed()121 virtual bool OnBackPressed()
122 {
123 return false;
124 }
125
IsSelected()126 virtual bool IsSelected() const
127 {
128 return textSelector_.IsValid() && !textSelector_.StartEqualToDest();
129 }
130
GetMouseStatus()131 MouseStatus GetMouseStatus() const
132 {
133 return mouseStatus_;
134 }
135
136 // The methods that need to be implemented for input class components
GetCaretRect()137 virtual RectF GetCaretRect() const
138 {
139 return { 0, 0, 0, 0 };
140 }
141
ScrollToSafeArea()142 virtual void ScrollToSafeArea() const {}
143
GetCaretMetrics(CaretMetricsF & caretCaretMetric)144 virtual void GetCaretMetrics(CaretMetricsF& caretCaretMetric) {}
145
OnVirtualKeyboardAreaChanged()146 virtual void OnVirtualKeyboardAreaChanged() {}
147
GetClipboard()148 virtual RefPtr<Clipboard> GetClipboard()
149 {
150 return nullptr;
151 }
152
GetContentRect()153 const RectF& GetContentRect() const
154 {
155 return contentRect_;
156 }
157
GetPaintContentRect()158 virtual RectF GetPaintContentRect()
159 {
160 return contentRect_;
161 }
162
GetContentWideTextLength()163 virtual int32_t GetContentWideTextLength()
164 {
165 return 0;
166 }
167
GetCaretIndex()168 virtual int32_t GetCaretIndex() const
169 {
170 return 0;
171 }
172
GetCaretOffset()173 virtual OffsetF GetCaretOffset() const
174 {
175 return OffsetF();
176 }
177
GetTextPaintOffset()178 virtual OffsetF GetTextPaintOffset() const
179 {
180 return OffsetF();
181 }
182
GetFirstHandleOffset()183 virtual OffsetF GetFirstHandleOffset() const
184 {
185 return OffsetF();
186 }
187
GetSecondHandleOffset()188 virtual OffsetF GetSecondHandleOffset() const
189 {
190 return OffsetF();
191 }
192
GetSelectIndex(int32_t & start,int32_t & end)193 virtual void GetSelectIndex(int32_t& start, int32_t& end) const
194 {
195 start = textSelector_.GetTextStart();
196 end = textSelector_.GetTextEnd();
197 }
198
GetAvoidSoftKeyboardOffset()199 virtual const Dimension& GetAvoidSoftKeyboardOffset() const
200 {
201 return avoidKeyboardOffset_;
202 }
203
OnHandleAreaChanged()204 virtual void OnHandleAreaChanged() {}
205 virtual void SetIsTextDraggable(bool isTextDraggable = true) {}
206 static void SetSelectionNode(const SelectedByMouseInfo& info);
207 static int32_t GetGraphemeClusterLength(const std::wstring& text, int32_t extend, bool checkPrev = false);
208 static void CalculateSelectedRect(
209 std::vector<RectF>& selectedRect, float longestLine, TextDirection direction = TextDirection::LTR);
210
211 static void RevertLocalPointWithTransform(const RefPtr<FrameNode>& targetNode, OffsetF& point);
212 static bool HasRenderTransform(const RefPtr<FrameNode>& targetNode);
IsTextEditableForStylus()213 virtual bool IsTextEditableForStylus() const
214 {
215 return false;
216 }
217
218 protected:
219 TextSelector textSelector_;
220 bool showSelect_ = true;
221 std::vector<std::string> dragContents_;
222 MouseStatus mouseStatus_ = MouseStatus::NONE;
223 RectF contentRect_;
224 Dimension avoidKeyboardOffset_ = 24.0_vp;
225 ACE_DISALLOW_COPY_AND_MOVE(TextBase);
226 };
227 } // namespace OHOS::Ace::NG
228 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_BASE_H
229