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 TRANSLATE, 73 CAMERA_INPUT, 74 AI_WRITE 75 }; 76 77 class SelectOverlayClient : public SelectionHost { 78 DECLARE_ACE_TYPE(SelectOverlayClient, SelectionHost); 79 80 public: 81 void InitSelectOverlay(); 82 void InitMenuCallback(); 83 void RequestOpenSelectOverlay(ClientOverlayInfo& overlayInfo); 84 virtual void RequestCloseSelectOverlay(bool animation); 85 bool SelectOverlayIsOn(); 86 CheckHandleVisible(const RectF & paintRect)87 virtual bool CheckHandleVisible(const RectF& paintRect) 88 { 89 return true; 90 } 91 CheckSelectionRectVisible()92 virtual bool CheckSelectionRectVisible() 93 { 94 return false; 95 } 96 OnPreShowSelectOverlay(SelectOverlayInfo & overlayInfo,const ClientOverlayInfo & clientInfo,bool isSelectOverlayOn)97 virtual bool OnPreShowSelectOverlay( 98 SelectOverlayInfo& overlayInfo, const ClientOverlayInfo& clientInfo, bool isSelectOverlayOn) 99 { 100 return false; 101 } 102 OnSelectOverlayMenuClicked(SelectOverlayMenuId menuId)103 virtual void OnSelectOverlayMenuClicked(SelectOverlayMenuId menuId) {} OnHandleMoveStart(const GestureEvent & event,bool isFirst)104 virtual void OnHandleMoveStart(const GestureEvent& event, bool isFirst) {} OnHandleMove(const RectF &,bool isFirst)105 virtual void OnHandleMove(const RectF&, bool isFirst) {} OnHandleMoveDone(const RectF &,bool isFirst)106 virtual void OnHandleMoveDone(const RectF&, bool isFirst) {} OnHandleClosed(bool closedByGlobalEvent)107 virtual void OnHandleClosed(bool closedByGlobalEvent) 108 { 109 if (closedByGlobalEvent) { 110 StopListeningScrollableParent(GetClientHost()); 111 } 112 } 113 GetClientHost()114 virtual RefPtr<FrameNode> GetClientHost() const 115 { 116 return nullptr; 117 } 118 119 void UpdateSelectInfo(const std::string& selectInfo); 120 121 virtual void OnParentScrollStartOrEnd(bool isEnd, bool noAnimation = false); 122 OnParentScrollCallback(Axis axis,int32_t offset)123 virtual void OnParentScrollCallback(Axis axis, int32_t offset) {}; 124 125 void StartListeningScrollableParent(const RefPtr<FrameNode>& host); 126 127 void StopListeningScrollableParent(const RefPtr<FrameNode>& host); 128 129 void UpdateSelectMenuInfo(std::function<void(SelectMenuInfo&)> updateAction); 130 131 void UpdateSelectMenuVisibility(bool isVisible); 132 IsShowingSingleHandle()133 bool IsShowingSingleHandle() 134 { 135 auto proxy = GetSelectOverlayProxy(); 136 CHECK_NULL_RETURN(proxy, false); 137 return proxy->IsSingleHandle(); 138 } 139 140 RectF GetVisibleContentRect(WeakPtr<FrameNode> parent, RectF visibleRect); 141 142 protected: GetSelectOverlayProxy()143 const RefPtr<SelectOverlayProxy>& GetSelectOverlayProxy() 144 { 145 return selectOverlayProxy_; 146 } 147 ResetSelectOverlayClient()148 void ResetSelectOverlayClient() 149 { 150 scrollableParentInfo_.hasParent = true; 151 scrollableParentInfo_.parentIds.clear(); 152 } 153 UpdateOriginIsMenuShow(bool isShow)154 void UpdateOriginIsMenuShow(bool isShow) 155 { 156 originIsMenuShow_ = isShow; 157 } 158 GetOriginIsMenuShow()159 bool GetOriginIsMenuShow() 160 { 161 return originIsMenuShow_; 162 } 163 164 private: 165 bool originIsMenuShow_ = true; 166 void RegisterParentScrollCallback(int32_t parentId, int32_t callbackId); 167 std::optional<SelectOverlayInfo> GetSelectOverlayInfo(const ClientOverlayInfo& clientInfo); 168 void CreateSelectOverlay(const ClientOverlayInfo& showOverlayInfo); 169 void UpdateShowingSelectOverlay(ClientOverlayInfo& clientInfo); 170 ScrollableParentInfo scrollableParentInfo_; 171 SelectOverlayInfo selectOverlayInfo_; 172 RefPtr<SelectOverlayProxy> selectOverlayProxy_; 173 }; 174 } // namespace OHOS::Ace::NG 175 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SELECT_OVERLAY_SELECT_OVERLAY_CLIENT_H