• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_EVENT_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_EVENT_HUB_H
18 
19 #include <list>
20 #include <utility>
21 
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "base/utils/noncopyable.h"
25 #include "core/components_ng/event/focus_hub.h"
26 #include "core/components_ng/event/gesture_event_hub.h"
27 #include "core/components_ng/event/input_event_hub.h"
28 #include "core/components_ng/event/state_style_manager.h"
29 #include "core/components_ng/event/visible_ratio_callback.h"
30 
31 namespace OHOS::Ace::NG {
32 
33 class FrameNode;
34 class InspectorFilter;
35 
36 using OnAreaChangedFunc =
37     std::function<void(const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin)>;
38 using OnPreDragFunc = std::function<void(const PreDragStatus)>;
39 
40 using OnSizeChangedFunc = std::function<void(const RectF& oldRect, const RectF& rect)>;
41 
42 struct KeyboardShortcut {
43     std::string value;
44     uint8_t keys = 0;
45     std::function<void()> onKeyboardShortcutAction = nullptr;
IsEqualTriggerKeyboardShortcut46     bool IsEqualTrigger(const KeyboardShortcut& other)
47     {
48         return (keys == other.keys) && (value == other.value);
49     }
50 };
51 
52 enum class VisibleAreaChangeTriggerReason : int32_t {
53     IDLE = 0,
54     VISIBLE_AREA_CHANGE = 1,
55     DETACH_FROM_MAINTREE = 2,
56     BACKGROUND = 3,
57     SELF_INVISIBLE = 4,
58     FRAMENODE_DESTROY = 5,
59     IS_NOT_ON_MAINTREE = 6,
60     ANCESTOR_INVISIBLE = 7,
61 };
62 
63 // The event hub is mainly used to handle common collections of events, such as gesture events, mouse events, etc.
64 class ACE_FORCE_EXPORT EventHub : public virtual AceType {
65     DECLARE_ACE_TYPE(EventHub, AceType);
66 
67 public:
68     EventHub() = default;
~EventHub()69     ~EventHub() override
70     {
71         keyboardShortcut_.clear();
72     };
73 
74     const RefPtr<GestureEventHub>& GetOrCreateGestureEventHub();
75 
CreateGestureEventHub()76     virtual RefPtr<GestureEventHub> CreateGestureEventHub()
77     {
78         return MakeRefPtr<GestureEventHub>(WeakClaim(this));
79     }
80 
81     const RefPtr<GestureEventHub>& GetGestureEventHub() const;
82     void SetGestureEventHub(const RefPtr<GestureEventHub>& gestureEventHub);
83     const RefPtr<InputEventHub>& GetOrCreateInputEventHub();
84     const RefPtr<InputEventHub>& GetInputEventHub() const;
85     RefPtr<FocusHub> GetOrCreateFocusHub(FocusType type = FocusType::DISABLE, bool focusable = false,
86         FocusStyleType focusStyleType = FocusStyleType::NONE,
87         const std::unique_ptr<FocusPaintParam>& paintParamsPtr = nullptr);
88     RefPtr<FocusHub> GetOrCreateFocusHub(const FocusPattern& focusPattern);
89     RefPtr<FocusHub> GetFocusHub() const;
90     void AttachHost(const WeakPtr<FrameNode>& host);
91     void OnAttachContext(PipelineContext* context);
92     void OnDetachContext(PipelineContext* context);
93     RefPtr<FrameNode> GetFrameNode() const;
94     GetEventTargetImpl CreateGetEventTargetImpl() const;
95     void OnContextAttached();
96     void ClearUserOnAppear();
97     void SetOnAppear(std::function<void()>&& onAppear);
98     void SetJSFrameNodeOnAppear(std::function<void()>&& onAppear);
99     void ClearJSFrameNodeOnAppear();
100     virtual void FireOnAppear();
101     void ClearUserOnDisAppear();
102     void SetOnDisappear(std::function<void()>&& onDisappear);
103     void SetJSFrameNodeOnDisappear(std::function<void()>&& onDisappear);
104     void ClearJSFrameNodeOnDisappear();
105     virtual void FireOnDisappear();
106     void ClearUserOnAreaChanged();
107     void SetOnAreaChanged(OnAreaChangedFunc&& onAreaChanged);
108     void FireOnAreaChanged(const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin);
109     void FireInnerOnAreaChanged(
110         const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin);
111     bool HasOnAreaChanged() const;
112     bool HasInnerOnAreaChanged() const;
113     void SetOnSizeChanged(OnSizeChangedFunc&& onSizeChanged);
114     void FireOnSizeChanged(const RectF& oldRect, const RectF& rect);
115     bool HasOnSizeChanged() const;
116     void AddInnerOnSizeChanged(int32_t id, OnSizeChangedFunc&& onSizeChanged);
117     void FireInnerOnSizeChanged(const RectF& oldRect, const RectF& rect);
118     bool HasInnerOnSizeChanged() const;
119     void ClearInnerOnSizeChanged();
120     void SetJSFrameNodeOnSizeChangeCallback(OnSizeChangedFunc&& onSizeChanged);
121     void FireJSFrameNodeOnSizeChanged(const RectF& oldRect, const RectF& rect);
122     void ClearJSFrameNodeOnSizeChange();
123     using OnDragFunc = std::function<void(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&)>;
124     using OnNewDragFunc = std::function<void(const RefPtr<OHOS::Ace::DragEvent>&)>;
125     using OnDragStartFunc = std::function<DragDropInfo(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&)>;
126     void SetOnPreDrag(OnPreDragFunc&& onPreDragFunc);
127     const OnPreDragFunc& GetOnPreDrag() const;
128     void SetOnDragStart(OnDragStartFunc&& onDragStart);
129     void SetCustomerOnDragSpringLoading(OnDragDropSpringLoadingFunc&& onDragSpringLoading);
130     const OnDragDropSpringLoadingFunc& GetCustomerOnDragSpringLoading() const;
131 
GetOnDragStart()132     const OnDragStartFunc& GetOnDragStart() const
133     {
134         return onDragStart_;
135     }
136 
137     bool HasOnDragStart() const;
138     void SetOnDragEnter(OnDragFunc&& onDragEnter);
139     void FireOnDragEnter(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams);
140     void SetOnDragLeave(OnDragFunc&& onDragLeave);
141     void FireOnDragLeave(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams);
142     void SetOnDragMove(OnDragFunc&& onDragMove);
143     void FireOnDragMove(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams);
144     bool HasOnDragMove() const;
145     void SetOnDrop(OnDragFunc&& onDrop);
146     void SetOnDragEnd(OnNewDragFunc&& onDragEnd);
GetOnDragEnd()147     const OnNewDragFunc& GetOnDragEnd() const
148     {
149         return onDragEnd_;
150     }
151     bool HasOnDragEnter() const;
152     bool HasOnDragLeave() const;
153     bool HasOnDragEnd() const;
154 
HasOnItemDragMove()155     virtual bool HasOnItemDragMove()
156     {
157         return false;
158     }
159 
HasOnItemDrop()160     virtual bool HasOnItemDrop()
161     {
162         return false;
163     }
164 
165     void FireOnDrop(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams);
166     bool HasOnDrop() const;
167     bool HasCustomerOnDragEnter() const;
168     bool HasCustomerOnDragSpringLoading() const;
169     bool HasCustomerOnDragLeave() const;
170     bool HasCustomerOnDragMove() const;
171     bool HasCustomerOnDragEnd() const;
172     bool HasCustomerOnDrop() const;
173     void SetDisableDataPrefetch(bool disableDataPrefetch);
174     bool GetDisableDataPrefetch() const;
175 
GetDragExtraParams(const std::string & extraInfo,const Point & point,DragEventType isStart)176     virtual std::string GetDragExtraParams(const std::string& extraInfo, const Point& point, DragEventType isStart)
177     {
178         auto json = JsonUtil::Create(true);
179         if (!extraInfo.empty()) {
180             json->Put("extraInfo", extraInfo.c_str());
181         }
182         return json->ToString();
183     }
184 
185     bool IsEnabled() const;
186     bool IsDeveloperEnabled() const;
187     void SetEnabled(bool enabled);
188     void SetEnabledInternal(bool enabled);
189     // restore enabled value to what developer sets
190     void RestoreEnabled();
191     // get XTS inspector value
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter)192     virtual void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const {}
FromJson(const std::unique_ptr<JsonValue> & json)193     virtual void FromJson(const std::unique_ptr<JsonValue>& json) {}
194     void MarkModifyDone();
195     void SetCurrentUIState(UIState state, bool flag);
196     void UpdateCurrentUIState(UIState state);
197     void ResetCurrentUIState(UIState state);
198     UIState GetCurrentUIState() const;
199     bool HasStateStyle(UIState state) const;
200     void AddSupportedState(UIState state);
201     void SetSupportedStates(UIState state);
202     void AddSupportedUIStateWithCallback(
203         UIState state, std::function<void(uint64_t)>& callback, bool isInner, bool excludeInner = false);
204     void RemoveSupportedUIState(UIState state, bool isInner);
205     bool GetUserSetStateStyle();
206     void SetScrollingFeatureForbidden(bool isSetStateStyle);
207     bool IsCurrentStateOn(UIState state);
208     void SetKeyboardShortcut(
209         const std::string& value, uint8_t keys, const std::function<void()>& onKeyboardShortcutAction);
210     void ClearSingleKeyboardShortcut();
211     std::vector<KeyboardShortcut>& GetKeyboardShortcut();
212     void SetCustomerOnDragFunc(DragFuncType dragFuncType, OnDragFunc&& onDragFunc);
213     void SetCustomerOnDragFunc(DragFuncType dragFuncType, OnNewDragFunc&& onDragEnd);
214 
GetCustomerOnDragFunc(DragFuncType dragFuncType)215     const OnDragFunc GetCustomerOnDragFunc(DragFuncType dragFuncType) const
216     {
217         OnDragFunc dragFunc;
218         switch (dragFuncType) {
219             case DragFuncType::DRAG_ENTER:
220                 dragFunc = customerOnDragEnter_;
221                 break;
222             case DragFuncType::DRAG_LEAVE:
223                 dragFunc = customerOnDragLeave_;
224                 break;
225             case DragFuncType::DRAG_MOVE:
226                 dragFunc = customerOnDragMove_;
227                 break;
228             case DragFuncType::DRAG_DROP:
229                 dragFunc = customerOnDrop_;
230                 break;
231             default:
232                 LOGW("unsuport dragFuncType");
233                 break;
234         }
235         return dragFunc;
236     }
237 
GetCustomerOnDragEndFunc()238     const OnNewDragFunc& GetCustomerOnDragEndFunc() const
239         {
240         return customerOnDragEnd_;
241     }
242 
243     void ClearCustomerOnDragFunc();
244     void ClearCustomerOnDragStart();
245     void ClearCustomerOnDragEnter();
246     void ClearCustomerOnDragSpringLoading();
247     void ClearCustomerOnDragMove();
248     void ClearCustomerOnDragLeave();
249     void ClearCustomerOnDrop();
250     void ClearCustomerOnDragEnd();
251     void FireCustomerOnDragFunc(
252         DragFuncType dragFuncType, const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams = "");
253     bool IsFireOnDrop(const RefPtr<OHOS::Ace::DragEvent>& info);
254     void HandleInternalOnDrop(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams);
255     void PostEnabledTask();
256     void FireEnabledTask();
257     void AddInnerOnAreaChangedCallback(int32_t id, OnAreaChangedFunc&& callback);
258     void RemoveInnerOnAreaChangedCallback(int32_t id);
259     void ClearOnAreaChangedInnerCallbacks();
260     bool HasImmediatelyVisibleCallback();
261     void SetDefaultOnDragStart(OnDragStartFunc&& defaultOnDragStart);
262 
GetDefaultOnDragStart()263     const OnDragStartFunc& GetDefaultOnDragStart() const
264     {
265         return defaultOnDragStart_;
266     }
267 
268     bool HasDefaultOnDragStart() const;
269     std::vector<double>& GetThrottledVisibleAreaRatios();
270     VisibleCallbackInfo& GetThrottledVisibleAreaCallback();
271     std::vector<double>& GetVisibleAreaRatios(bool isUser);
272     VisibleCallbackInfo& GetVisibleAreaCallback(bool isUser);
273     void SetVisibleAreaRatiosAndCallback(
274         const VisibleCallbackInfo& callback, const std::vector<double>& radios, bool isUser);
275     void CleanVisibleAreaCallback(bool isUser, bool isThrottled = false);
276     bool HasVisibleAreaCallback(bool isUser);
277     bool HasThrottledVisibleAreaCallback() const;
278     void SetOnAttach(std::function<void()>&& onAttach);
279     void ClearOnAttach();
280     virtual void FireOnAttach();
281     void SetOnDetach(std::function<void()>&& onDetach);
282     void ClearOnDetach();
283     void ClearOnPreDrag();
284     virtual void FireOnDetach();
285     void SetOnWillBind(std::function<void(int32_t)>&& onWillBind);
286     void ClearOnWillBind();
287     virtual void FireOnWillBind(int32_t containerId);
288     void SetOnWillUnbind(std::function<void(int32_t)>&& onWillUnbind);
289     void ClearOnWillUnbind();
290     virtual void FireOnWillUnbind(int32_t containerId);
291     void SetOnBind(std::function<void(int32_t)>&& onBind);
292     void ClearOnBind();
293     virtual void FireOnBind(int32_t containerId);
294     void SetOnUnbind(std::function<void(int32_t)>&& onUnbind);
295     void ClearOnUnbind();
296     virtual void FireOnUnbind(int32_t containerId);
297     void ClearStateStyle();
298     void OnDetachClear();
299     void HandleOnAreaChange(const std::unique_ptr<RectF>& lastFrameRect,
300         const std::unique_ptr<OffsetF>& lastParentOffsetToWindow,
301         const RectF& currFrameRect, const OffsetF& currParentOffsetToWindow);
302     void FireUntriggeredInnerOnAreaChanged(
303         const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin);
304     void FireDrawCompletedNDKCallback(PipelineContext* pipeline);
305     void FireLayoutNDKCallback(PipelineContext* pipeline);
SetNDKDrawCompletedCallback(std::function<void ()> && callback)306     void SetNDKDrawCompletedCallback(std::function<void()>&& callback)
307     {
308         ndkDrawCompletedCallback_ = std::move(callback);
309     }
SetNDKLayoutCallback(std::function<void ()> && callback)310     void SetNDKLayoutCallback(std::function<void()>&& callback)
311     {
312         ndkLayoutCallback_ = std::move(callback);
313     }
HasNDKDrawCompletedCallback()314     bool HasNDKDrawCompletedCallback()
315     {
316         return !!ndkDrawCompletedCallback_;
317     }
318 
319 
320 protected:
OnModifyDone()321     virtual void OnModifyDone() {}
322     std::function<void()> onAppear_;
323     std::function<void()> onJSFrameNodeAppear_;
324 
325 private:
326     void AddPressedListener();
327 
328     WeakPtr<FrameNode> host_;
329     RefPtr<GestureEventHub> gestureEventHub_;
330     RefPtr<InputEventHub> inputEventHub_;
331     RefPtr<StateStyleManager> stateStyleMgr_;
332 
333     std::function<void()> onDisappear_;
334     std::function<void()> onJSFrameNodeDisappear_;
335     OnAreaChangedFunc onAreaChanged_;
336     std::unordered_map<int32_t, OnAreaChangedFunc> onAreaChangedInnerCallbacks_;
337     OnSizeChangedFunc onSizeChanged_;
338     std::unordered_map<int32_t, OnSizeChangedFunc> onSizeChangedInnerCallbacks_;
339     OnSizeChangedFunc onJsFrameNodeSizeChanged_;
340 
341     std::function<void()> onAttach_;
342     std::function<void()> onDetach_;
343     std::function<void(int32_t)> onWillBind_;
344     std::function<void(int32_t)> onWillUnbind_;
345     std::function<void(int32_t)> onBind_;
346     std::function<void(int32_t)> onUnbind_;
347 
348     OnPreDragFunc onPreDragFunc_;
349     OnDragStartFunc onDragStart_;
350     OnDragFunc onDragEnter_;
351     OnDragFunc onDragLeave_;
352     OnDragFunc onDragMove_;
353     OnDragFunc onDrop_;
354     OnNewDragFunc onDragEnd_;
355 
356     OnDragStartFunc defaultOnDragStart_;
357     OnDragFunc customerOnDragEnter_;
358     OnDragDropSpringLoadingFunc customerOnDragSpringLoading_;
359     OnDragFunc customerOnDragLeave_;
360     OnDragFunc customerOnDragMove_;
361     OnDragFunc customerOnDrop_;
362     OnNewDragFunc customerOnDragEnd_;
363 
364     bool enabled_ { true };
365     bool developerEnabled_ { true };
366     bool disableDataPrefetch_ { false };
367     std::vector<KeyboardShortcut> keyboardShortcut_;
368     std::vector<int32_t> hasInnerAreaChangeUntriggered_;
369 
370     std::vector<double> visibleAreaUserRatios_;
371     VisibleCallbackInfo visibleAreaUserCallback_;
372     std::vector<double> visibleAreaInnerRatios_;
373     VisibleCallbackInfo visibleAreaInnerCallback_;
374     std::vector<double> throttledVisibleAreaRatios_;
375     VisibleCallbackInfo throttledVisibleAreaCallback_;
376     std::function<void()> ndkDrawCompletedCallback_;
377     std::function<void()> ndkLayoutCallback_;
378     std::function<void()> enabledFunc_;
379 
380     ACE_DISALLOW_COPY_AND_MOVE(EventHub);
381 };
382 
383 class TextCommonEvent : public BaseEventInfo {
DECLARE_RELATIONSHIP_OF_CLASSES(TextCommonEvent,BaseEventInfo)384     DECLARE_RELATIONSHIP_OF_CLASSES(TextCommonEvent, BaseEventInfo)
385 public:
386     TextCommonEvent() : BaseEventInfo("TextCommonEvent") {}
387     ~TextCommonEvent() override = default;
388 };
389 
390 } // namespace OHOS::Ace::NG
391 
392 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_EVENT_HUB_H
393