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