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