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_MANAGER_SELECT_OVERLAY_SELECT_OVERLAY_CLIENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SELECT_OVERLAY_SELECT_OVERLAY_CLIENT_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <vector>
22 
23 #include "base/geometry/offset.h"
24 #include "base/memory/ace_type.h"
25 #include "base/memory/referenced.h"
26 #include "core/components_ng/base/frame_node.h"
27 #include "core/components_ng/manager/select_overlay/select_overlay_proxy.h"
28 #include "core/components_ng/manager/select_overlay/selection_host.h"
29 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h"
30 #include "core/pipeline_ng/pipeline_context.h"
31 
32 namespace OHOS::Ace::NG {
33 
34 enum class HandleShowMode { DOUBLE, SINGLE, NONE };
35 
36 struct OverlayExtraInfo {
37     std::map<std::string, bool> boolExtra;
38 
GetBoolOrDefaultOverlayExtraInfo39     bool GetBoolOrDefault(std::string key, bool defaultValue)
40     {
41         auto it = boolExtra.find(key);
42         if (it == boolExtra.end()) {
43             return defaultValue;
44         }
45         return it->second;
46     };
47 };
48 
49 struct ClientOverlayInfo {
50     std::optional<SelectHandleInfo> firstHandleInfo;
51     std::optional<SelectHandleInfo> secondHandleInfo;
52     std::optional<Color> handlerColor;
53     RectF selectArea;
54     bool isNewAvoid = false;
55     bool animation = true;
56     bool isMenuShow = true;
57     bool isShowMouseMenu = false;
58     bool isShowPaste = false;
59     bool isUpdateMenu = true;
60 };
61 
62 struct ScrollableParentInfo {
63     bool hasParent = true;
64     std::vector<int32_t> parentIds;
65 };
66 
67 enum class SelectOverlayMenuId {
68     COPY,
69     CUT,
70     PASTE,
71     SELECT_ALL,
72     CAMERA_INPUT,
73     AI_WRITE
74 };
75 
76 class SelectOverlayClient : public SelectionHost {
77     DECLARE_ACE_TYPE(SelectOverlayClient, SelectionHost);
78 
79 public:
80     void InitSelectOverlay();
81     void InitMenuCallback();
82     void RequestOpenSelectOverlay(ClientOverlayInfo& overlayInfo);
83     virtual void RequestCloseSelectOverlay(bool animation);
84     bool SelectOverlayIsOn();
85 
CheckHandleVisible(const RectF & paintRect)86     virtual bool CheckHandleVisible(const RectF& paintRect)
87     {
88         return true;
89     }
90 
CheckSelectionRectVisible()91     virtual bool CheckSelectionRectVisible()
92     {
93         return false;
94     }
95 
OnPreShowSelectOverlay(SelectOverlayInfo & overlayInfo,const ClientOverlayInfo & clientInfo,bool isSelectOverlayOn)96     virtual bool OnPreShowSelectOverlay(
97         SelectOverlayInfo& overlayInfo, const ClientOverlayInfo& clientInfo, bool isSelectOverlayOn)
98     {
99         return false;
100     }
101 
OnSelectOverlayMenuClicked(SelectOverlayMenuId menuId)102     virtual void OnSelectOverlayMenuClicked(SelectOverlayMenuId menuId) {}
OnHandleMoveStart(const GestureEvent & event,bool isFirst)103     virtual void OnHandleMoveStart(const GestureEvent& event, bool isFirst) {}
OnHandleMove(const RectF &,bool isFirst)104     virtual void OnHandleMove(const RectF&, bool isFirst) {}
OnHandleMoveDone(const RectF &,bool isFirst)105     virtual void OnHandleMoveDone(const RectF&, bool isFirst) {}
OnHandleClosed(bool closedByGlobalEvent)106     virtual void OnHandleClosed(bool closedByGlobalEvent)
107     {
108         if (closedByGlobalEvent) {
109             StopListeningScrollableParent(GetClientHost());
110         }
111     }
112 
GetClientHost()113     virtual RefPtr<FrameNode> GetClientHost() const
114     {
115         return nullptr;
116     }
117 
118     void UpdateSelectInfo(const std::string& selectInfo);
119 
120     virtual void OnParentScrollStartOrEnd(bool isEnd, bool noAnimation = false);
121 
OnParentScrollCallback(Axis axis,int32_t offset)122     virtual void OnParentScrollCallback(Axis axis, int32_t offset) {};
123 
124     void StartListeningScrollableParent(const RefPtr<FrameNode>& host);
125 
126     void StopListeningScrollableParent(const RefPtr<FrameNode>& host);
127 
128     void UpdateSelectMenuInfo(std::function<void(SelectMenuInfo&)> updateAction);
129 
130     void UpdateSelectMenuVisibility(bool isVisible);
131 
IsShowingSingleHandle()132     bool IsShowingSingleHandle()
133     {
134         auto proxy = GetSelectOverlayProxy();
135         CHECK_NULL_RETURN(proxy, false);
136         return proxy->IsSingleHandle();
137     }
138 
139     RectF GetVisibleContentRect(WeakPtr<FrameNode> parent, RectF visibleRect);
140 
141 protected:
GetSelectOverlayProxy()142     const RefPtr<SelectOverlayProxy>& GetSelectOverlayProxy()
143     {
144         return selectOverlayProxy_;
145     }
146 
ResetSelectOverlayClient()147     void ResetSelectOverlayClient()
148     {
149         scrollableParentInfo_.hasParent = true;
150         scrollableParentInfo_.parentIds.clear();
151     }
152 
UpdateOriginIsMenuShow(bool isShow)153     void UpdateOriginIsMenuShow(bool isShow)
154     {
155         originIsMenuShow_ = isShow;
156     }
157 
GetOriginIsMenuShow()158     bool GetOriginIsMenuShow()
159     {
160         return originIsMenuShow_;
161     }
162 
163 private:
164     bool originIsMenuShow_ = true;
165     void RegisterParentScrollCallback(int32_t parentId, int32_t callbackId);
166     std::optional<SelectOverlayInfo> GetSelectOverlayInfo(const ClientOverlayInfo& clientInfo);
167     void CreateSelectOverlay(const ClientOverlayInfo& showOverlayInfo);
168     void UpdateShowingSelectOverlay(ClientOverlayInfo& clientInfo);
169     ScrollableParentInfo scrollableParentInfo_;
170     SelectOverlayInfo selectOverlayInfo_;
171     RefPtr<SelectOverlayProxy> selectOverlayProxy_;
172 };
173 } // namespace OHOS::Ace::NG
174 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SELECT_OVERLAY_SELECT_OVERLAY_CLIENT_H