1 /*
2  * Copyright (c) 2024 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_SELECT_OVERLAY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_BASE_SELECT_OVERLAY_H
18 #include <optional>
19 
20 #include "base/geometry/ng/point_t.h"
21 #include "base/geometry/ng/rect_t.h"
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "core/components_ng/manager/select_content_overlay/select_overlay_callback.h"
25 #include "core/components_ng/manager/select_content_overlay/select_overlay_holder.h"
26 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h"
27 #include "core/components_ng/pattern/text/text_base.h"
28 #include "core/components_ng/pattern/text/text_menu_extension.h"
29 #include "core/event/ace_events.h"
30 #include "core/event/touch_event.h"
31 
32 namespace OHOS::Ace::NG {
33 struct OverlayRequest {
34     bool menuIsShow = true;
35     bool hideHandle = false;
36     bool animation = false;
37     bool hideHandleLine = false;
38     int32_t requestCode = 0;
39 };
40 
41 class BaseTextSelectOverlay : public SelectOverlayHolder, public SelectOverlayCallback {
42     DECLARE_ACE_TYPE(BaseTextSelectOverlay, SelectOverlayHolder, SelectOverlayCallback);
43 
44 public:
BaseTextSelectOverlay(const WeakPtr<TextBase> & textBase)45     explicit BaseTextSelectOverlay(const WeakPtr<TextBase>& textBase) : hostTextBase_(textBase) {}
46     ~BaseTextSelectOverlay() = default;
47 
48     static RectF GetVisibleRect(const RefPtr<FrameNode>& node, const RectF& visibleRect);
49 
50     template<class T>
GetPattern()51     RefPtr<T> GetPattern()
52     {
53         return DynamicCast<T>(hostTextBase_.Upgrade());
54     }
55 
GetHostTextBase()56     WeakPtr<TextBase> GetHostTextBase()
57     {
58         return hostTextBase_;
59     }
60 
61     void ProcessOverlay(const OverlayRequest& request = OverlayRequest());
62     void ProcessOverlayOnAreaChanged(const OverlayRequest& request = OverlayRequest());
PreProcessOverlay(const OverlayRequest & request)63     virtual bool PreProcessOverlay(const OverlayRequest& request)
64     {
65         return true;
66     }
AfterCloseOverlay()67     virtual void AfterCloseOverlay() {}
68 
69     // override SelectOverlayHolder
70     RefPtr<FrameNode> GetOwner() override;
71     void OnHandleGlobalTouchEvent(SourceType sourceType, TouchType touchType, bool touchInside = true) override;
72     bool CheckTouchInHostNode(const PointF& touchPoint) override;
73     void OnUpdateSelectOverlayInfo(SelectOverlayInfo& overlayInfo, int32_t requestCode) override;
74     bool CheckRestartHiddenHandleTask(int32_t requestCode) override;
75     std::optional<RectF> GetAncestorNodeViewPort() override;
GetCallback()76     RefPtr<SelectOverlayCallback> GetCallback() override
77     {
78         return AceType::Claim(this);
79     }
80     // override SelectOverlayHolder end
81 
82     // common method
83     bool SelectOverlayIsOn();
84     bool SelectOverlayIsCreating();
85     void CloseOverlay(bool animation, CloseReason reason);
86     void ToggleMenu();
87     void ShowMenu();
88     void HideMenu(bool noAnimation = false);
89     void DisableMenu();
90     void EnableMenu();
91     void UpdateAllHandlesOffset();
92     void UpdateFirstHandleOffset();
93     void UpdateSecondHandleOffset();
94     void UpdateViewPort();
95     bool IsShowMouseMenu();
96     bool IsCurrentMenuVisibile();
97     bool IsHandleReverse();
98     void RemoveSelectionHoldCallback();
99     void SetSelectionHoldCallback();
100 
IsSingleHandle()101     bool IsSingleHandle()
102     {
103         return isSingleHandle_;
104     }
105 
IsUsingMouse()106     bool IsUsingMouse()
107     {
108         return isUsingMouse_;
109     }
110 
SetIsSingleHandle(bool isSingleHandle)111     void SetIsSingleHandle(bool isSingleHandle)
112     {
113         isSingleHandle_ = isSingleHandle;
114     }
115 
SetUsingMouse(bool isUsingMouse)116     void SetUsingMouse(bool isUsingMouse)
117     {
118         isUsingMouse_ = isUsingMouse;
119     }
120 
SetShowPaste(bool isShowPaste)121     void SetShowPaste(bool isShowPaste)
122     {
123         isShowPaste_ = isShowPaste;
124     }
125 
IsShowPaste()126     bool IsShowPaste()
127     {
128         return isShowPaste_;
129     }
130 
SetMenuIsShow(bool isShowMenu)131     void SetMenuIsShow(bool isShowMenu)
132     {
133         isShowMenu_ = isShowMenu;
134         originalMenuIsShow_ = isShowMenu;
135     }
136 
IsShowMenu()137     bool IsShowMenu()
138     {
139         return isShowMenu_;
140     }
141 
SetIsShowHandleLine(bool isShowHandleLine)142     void SetIsShowHandleLine(bool isShowHandleLine)
143     {
144         isShowHandleLine_ = isShowHandleLine;
145     }
146 
SetMouseMenuOffset(const OffsetF & offset)147     void SetMouseMenuOffset(const OffsetF& offset)
148     {
149         mouseMenuOffset_ = offset;
150     }
151 
152     // common virtual methods.
153     virtual RectF GetVisibleContentRect();
154     virtual bool CheckHandleVisible(const RectF& paintRect) = 0;
155 
GetPasteMimeTypes()156     virtual std::vector<std::string> GetPasteMimeTypes()
157     {
158         return std::vector<std::string>();
159     }
160 
OnResetTextSelection()161     virtual void OnResetTextSelection() {}
162 
163     virtual bool IsAcceptResetSelectionEvent(SourceType sourceType, TouchType touchType);
164 
HasRenderTransform()165     bool HasRenderTransform()
166     {
167         UpdateTransformFlag();
168         return hasTransform_;
169     }
170     // transformed global point to original local point.
171     void RevertLocalPointWithTransform(OffsetF& point);
172     // original local point to transformed global point.
173     void GetGlobalPointsWithTransform(std::vector<OffsetF>& points);
174     // original local rect to transformd global rect.
175     void GetGlobalRectWithTransform(RectF& rect);
176     std::vector<OffsetF> GetGlobalRectVertexWithTransform(const RectF& rect, float extendValue = 0.0f);
177     // original local point to transformed local point.
178     void GetLocalPointsWithTransform(std::vector<OffsetF>& localPoints);
179     void GetLocalPointWithTransform(OffsetF& localPoint);
180     // original local rect to transformed local rect.
181     void GetLocalRectWithTransform(RectF& localRect);
182 
183     OffsetF GetPaintOffsetWithoutTransform();
184     RectF GetPaintRectWithTransform();
185     OffsetF GetPaintRectOffsetWithTransform();
186     RectF GetVisibleContentRectWithTransform(float epsilon);
187     bool CheckHandleIsVisibleWithTransform(const OffsetF& startPoint, const OffsetF& endPoint, float epsilon);
188     bool IsPointInRect(const OffsetF& point, const OffsetF& leftBottom, const OffsetF& rightBottom,
189         const OffsetF& rightTop, const OffsetF& leftTop);
190 
191     void OnSelectionMenuOptionsUpdate(
192         const NG::OnCreateMenuCallback&& onCreateMenuCallback, const NG::OnMenuItemClickCallback&& onMenuItemClick);
193 
OnCreateMenuCallbackUpdate(const NG::OnCreateMenuCallback && onCreateMenuCallback)194     void OnCreateMenuCallbackUpdate(const NG::OnCreateMenuCallback&& onCreateMenuCallback)
195     {
196         onCreateMenuCallback_ = onCreateMenuCallback;
197     }
198 
OnMenuItemClickCallbackUpdate(const NG::OnMenuItemClickCallback && onMenuItemClick)199     void OnMenuItemClickCallbackUpdate(const NG::OnMenuItemClickCallback&& onMenuItemClick)
200     {
201         onMenuItemClick_ = onMenuItemClick;
202     }
203 
204     float GetHandleDiameter();
205     VectorF GetHostScale();
206     void SwitchToOverlayMode();
207     void SwitchToEmbedMode();
208     void SetHandleLevelMode(HandleLevelMode mode);
IsOverlayMode()209     bool IsOverlayMode()
210     {
211         return handleLevelMode_ == HandleLevelMode::OVERLAY;
212     }
213 
OnHandleLevelModeChanged(HandleLevelMode mode)214     void OnHandleLevelModeChanged(HandleLevelMode mode) override
215     {
216         SetHandleLevelMode(mode);
217         UpdateAllHandlesOffset();
218         UpdateViewPort();
219     }
220     virtual void OnAncestorNodeChanged(FrameNodeChangeInfoFlag flag);
221     void OnCloseOverlay(OptionMenuType menuType, CloseReason reason, RefPtr<OverlayInfo> info) override;
OnHandleMoveStart(const GestureEvent & event,bool isFirst)222     void OnHandleMoveStart(const GestureEvent& event, bool isFirst) override
223     {
224         isHandleDragging_ = true;
225     }
OnHandleMoveDone(const RectF & rect,bool isFirst)226     void OnHandleMoveDone(const RectF& rect, bool isFirst) override
227     {
228         isHandleDragging_ = false;
229     }
GetIsHandleDragging()230     bool GetIsHandleDragging()
231     {
232         return isHandleDragging_;
233     }
234     bool IsTouchAtHandle(const TouchEventInfo& info);
235     bool IsClickAtHandle(const GestureEvent& info);
236     bool HasUnsupportedTransform();
237     bool CheckSwitchToMode(HandleLevelMode mode) override;
238 
OnUpdateOnCreateMenuCallback(SelectOverlayInfo & selectInfo)239     void OnUpdateOnCreateMenuCallback(SelectOverlayInfo& selectInfo)
240     {
241         selectInfo.onCreateCallback.onCreateMenuCallback = onCreateMenuCallback_;
242         selectInfo.onCreateCallback.onMenuItemClick = onMenuItemClick_;
243         auto textRange = [weak = GetHostTextBase()](int32_t& start, int32_t& end) {
244             auto pattern = weak.Upgrade();
245             CHECK_NULL_VOID(pattern);
246             pattern->GetSelectIndex(start, end);
247         };
248         selectInfo.onCreateCallback.textRangeCallback = textRange;
249     }
250 
251     void MarkOverlayDirty();
252     void OnHandleMarkInfoChange(const std::shared_ptr<SelectOverlayInfo> info, SelectOverlayDirtyFlag flag) override;
253     void UpdateHandleColor();
GetHandleColor()254     virtual std::optional<Color> GetHandleColor()
255     {
256         return std::nullopt;
257     }
258 protected:
259     RectF MergeSelectedBoxes(
260         const std::vector<RectF>& boxes, const RectF& contentRect, const RectF& textRect, const OffsetF& paintOffset);
IsMouseClickDown(SourceType sourceType,TouchType touchType)261     bool IsMouseClickDown(SourceType sourceType, TouchType touchType)
262     {
263         return sourceType == SourceType::MOUSE && touchType == TouchType::DOWN;
264     }
265 
IsTouchUp(SourceType sourceType,TouchType touchType)266     bool IsTouchUp(SourceType sourceType, TouchType touchType)
267     {
268         return (sourceType == SourceType::TOUCH || sourceType == SourceType::TOUCH_PAD) && touchType == TouchType::UP;
269     }
270 
271     RectF ConvertPaintInfoToRect(const SelectHandlePaintInfo& paintInfo);
272     void SetTransformPaintInfo(SelectHandleInfo& handleInfo, const RectF& localHandleRect);
273     bool CheckHandleCanPaintInHost(const RectF& firstRect, const RectF& secondRect);
274     virtual RectF GetFirstHandleLocalPaintRect();
275     virtual RectF GetSecondHandleLocalPaintRect();
276     virtual void CalcHandleLevelMode(const RectF& firstLocalPaintRect, const RectF& secondLocalPaintRect);
277     bool IsAncestorNodeStartAnimation(FrameNodeChangeInfoFlag flag);
278     bool IsAncestorNodeGeometryChange(FrameNodeChangeInfoFlag flag);
279     bool IsAncestorNodeStartScroll(FrameNodeChangeInfoFlag flag);
280     bool IsAncestorNodeEndScroll(FrameNodeChangeInfoFlag flag);
281     bool IsAncestorNodeTransformChange(FrameNodeChangeInfoFlag flag);
282     bool IsAncestorNodeHasTransition(FrameNodeChangeInfoFlag flag);
SetEnableHandleLevel(bool enableHandleLevel)283     void SetEnableHandleLevel(bool enableHandleLevel)
284     {
285         enableHandleLevel_ = enableHandleLevel;
286     }
287     void RegisterScrollingListener(const RefPtr<FrameNode> scrollableNode);
288     void OnHandleScrolling(const WeakPtr<FrameNode>& scrollingNode);
289     virtual void UpdateTransformFlag();
290     bool CheckHasTransformAttr();
UpdateOriginalMenuIsShow()291     void UpdateOriginalMenuIsShow()
292     {
293         originalMenuIsShow_ = IsCurrentMenuVisibile();
294     }
295     virtual void UpdateMenuWhileAncestorNodeChanged(bool shouldHideMenu, bool shouldShowMenu);
296     bool GetClipHandleViewPort(RectF& rect);
UpdateClipHandleViewPort(RectF & rect)297     virtual void UpdateClipHandleViewPort(RectF& rect) {};
298     bool GetFrameNodeContentRect(const RefPtr<FrameNode>& node, RectF& rect);
IsClipHandleWithViewPort()299     virtual bool IsClipHandleWithViewPort()
300     {
301         return false;
302     }
303     void ApplySelectAreaWithKeyboard(RectF& selectArea);
304     std::optional<OverlayRequest> latestReqeust_;
305     bool hasTransform_ = false;
306     HandleLevelMode handleLevelMode_ = HandleLevelMode::OVERLAY;
307     OnCreateMenuCallback onCreateMenuCallback_;
308     OnMenuItemClickCallback onMenuItemClick_;
309     bool isHandleMoving_ = false;
310 
311 private:
312     void FindScrollableParentAndSetCallback(const RefPtr<FrameNode>& host);
313     void RegisterParentScrollCallback(const RefPtr<FrameNode>& host, int32_t parentId);
314     void ShowSelectOverlay(const OverlayRequest& request, bool hasClipboardData);
315     void GetHandlePoints(const RectF& handleRect, std::vector<PointF>& points, bool handleOnTop);
316     bool IsPointsInRegion(const std::vector<PointF>& points, const RectF& regionRect);
317     bool CheckAndUpdateHostGlobalPaintRect();
318     bool CheckHasTransformMatrix(const RefPtr<RenderContext>& context);
319     bool isHandleDragging_ = false;
320     bool isSingleHandle_ = false;
321     bool isShowPaste_ = false;
322     bool isShowMenu_ = true;
323     bool isShowHandleLine_ = true;
324     bool isUsingMouse_ = false;
325     OffsetF mouseMenuOffset_;
326     WeakPtr<TextBase> hostTextBase_;
327     bool enableHandleLevel_ = false;
328     bool touchAtHandle_ = false;
329     bool isChangeToOverlayModeAtEdge_ = true;
330     bool hasRegisterListener_ = false;
331     RectF globalPaintRect_;
332     bool originalMenuIsShow_ = true;
333 };
334 
335 } // namespace OHOS::Ace::NG
336 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_BASE_SELECT_OVERLAY_H
337