1 /* 2 * Copyright (c) 2022 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_MANAGER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SELECT_OVERLAY_SELECT_OVERLAY_MANAGER_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <unordered_map> 22 23 #include "base/memory/ace_type.h" 24 #include "base/memory/referenced.h" 25 #include "base/utils/noncopyable.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_node.h" 30 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h" 31 #include "core/components_ng/property/property.h" 32 33 namespace OHOS::Ace::NG { 34 35 using ScrollableParentCallback = std::function<void(Axis, float, int32_t)>; 36 37 // SelectOverlayManager is the class to show and control select handle and select menu. 38 class ACE_EXPORT SelectOverlayManager : public virtual AceType { 39 DECLARE_ACE_TYPE(SelectOverlayManager, AceType); 40 41 public: SelectOverlayManager(const RefPtr<FrameNode> & rootNode)42 explicit SelectOverlayManager(const RefPtr<FrameNode>& rootNode) : rootNodeWeak_(rootNode) {} 43 ~SelectOverlayManager() override = default; 44 45 // Create and display selection pop-ups. 46 RefPtr<SelectOverlayProxy> CreateAndShowSelectOverlay( 47 const SelectOverlayInfo& info, const WeakPtr<SelectionHost>& host, bool animation = false); 48 49 // Destroy the pop-up interface and delete the pop-up information. 50 void DestroySelectOverlay(const RefPtr<SelectOverlayProxy>& proxy, bool animation = false); 51 void DestroySelectOverlay(int32_t overlayId, bool animation = false); 52 bool DestroySelectOverlay(bool animation = false); 53 54 bool ResetSelectionAndDestroySelectOverlay(bool animation = false); 55 56 bool HasSelectOverlay(int32_t overlayId); 57 58 bool IsInSelectedOrSelectOverlayArea(const PointF& point); 59 60 RefPtr<SelectOverlayNode> GetSelectOverlayNode(int32_t overlayId); 61 62 bool IsSameSelectOverlayInfo(const SelectOverlayInfo& info); 63 SetOnTouchTestResults(const std::vector<std::string> & touchTestResults)64 void SetOnTouchTestResults(const std::vector<std::string>& touchTestResults) 65 { 66 touchTestResults_ = touchTestResults; 67 } 68 69 void HandleGlobalEvent( 70 const TouchEvent& touchPoint, const NG::OffsetF& rootOffset, bool isMousePressAtSelectedNode = false); 71 72 void MarkDirty(PropertyChangeFlag flag); 73 74 RefPtr<UINode> FindWindowScene(RefPtr<FrameNode> targetNode); 75 GetSelectOverlayItem()76 const WeakPtr<FrameNode>& GetSelectOverlayItem() const 77 { 78 return selectOverlayItem_; 79 } 80 81 void NotifyOnScrollCallback(int32_t id, Axis axis, float offset, int32_t source); 82 83 void RegisterScrollCallback(int32_t scrollableParentId, int32_t callbackId, ScrollableParentCallback&& callback); 84 85 void RemoveScrollCallback(int32_t callbackId); 86 GetSelectOverlayInfo()87 SelectOverlayInfo GetSelectOverlayInfo() 88 { 89 return selectOverlayInfo_; 90 } 91 SetSelectedNodeByMouse(const SelectedByMouseInfo & info)92 void SetSelectedNodeByMouse(const SelectedByMouseInfo& info) 93 { 94 if (selectedByMouseInfo_ != info) { 95 if (selectedByMouseInfo_.onResetSelection) { 96 selectedByMouseInfo_.onResetSelection(); 97 } 98 selectedByMouseInfo_.clear(); 99 selectedByMouseInfo_ = info; 100 } 101 } 102 GetSelectedNodeIdByMouse(int32_t & id)103 bool GetSelectedNodeIdByMouse(int32_t& id) const 104 { 105 CHECK_NULL_RETURN(selectedByMouseInfo_.selectedNode.Upgrade(), false); 106 id = selectedByMouseInfo_.selectedNode.Upgrade()->GetId(); 107 return true; 108 } 109 110 void ResetSelection(const TouchEvent& touchPoint, bool isMousePressAtSelectedNode); 111 112 private: 113 void DestroyHelper(const RefPtr<FrameNode>& overlay, bool animation = false); 114 115 void Destroy(const RefPtr<FrameNode>& overlay); 116 117 bool IsTouchInCallerArea(const std::optional<NG::PointF>& point = std::nullopt) const; 118 119 void NotifyOverlayClosed(bool closedByGlobalEvent = false); 120 121 RefPtr<FrameNode> GetCallerHost() const; 122 123 WeakPtr<FrameNode> rootNodeWeak_; 124 125 WeakPtr<FrameNode> selectOverlayItem_; 126 WeakPtr<SelectionHost> host_; 127 128 SelectedByMouseInfo selectedByMouseInfo_; 129 130 SelectOverlayInfo selectOverlayInfo_; 131 132 std::vector<TouchEvent> touchDownPoints_; 133 std::vector<std::string> touchTestResults_; 134 135 std::map<int32_t, std::map<int32_t, ScrollableParentCallback>> parentScrollCallbacks_; 136 137 ACE_DISALLOW_COPY_AND_MOVE(SelectOverlayManager); 138 }; 139 } // namespace OHOS::Ace::NG 140 141 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SELECT_OVERLAY_SELECT_OVERLAY_MANAGER_H 142