• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_COMMON_EVENT_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_EVENT_MANAGER_H
18 
19 #include <unordered_map>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/memory/referenced.h"
23 #include "core/common/event_dump.h"
24 #include "core/common/key_event_manager.h"
25 #include "core/components/common/layout/constants.h"
26 #include "core/components_ng/event/response_ctrl.h"
27 #include "core/components_ng/gestures/gesture_referee.h"
28 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h"
29 #include "core/event/axis_event.h"
30 #include "core/event/key_event.h"
31 #include "core/event/mouse_event.h"
32 #include "core/event/pointer_event.h"
33 #include "core/event/non_pointer_event.h"
34 #include "core/event/rotation_event.h"
35 #include "core/event/touch_event.h"
36 #include "core/focus/focus_node.h"
37 #include "core/gestures/gesture_referee.h"
38 #include "core/event/resample_algo.h"
39 
40 namespace OHOS::Ace {
41 namespace NG {
42 class FrameNode;
43 class SelectOverlayManager;
44 class ResponseCtrl;
45 } // namespace NG
46 class RenderNode;
47 class Element;
48 class TextOverlayManager;
49 using MouseHoverTestList = std::list<WeakPtr<RenderNode>>;
50 using OutOfRectGetRectCallback = std::function<void(std::vector<Rect>&)>;
51 using OutOfRectTouchCallback = std::function<void(void)>;
52 using OutOfRectMouseCallback = std::function<void(void)>;
53 
54 struct RectCallback final {
RectCallbackfinal55     RectCallback(OutOfRectGetRectCallback rectGetCallback, OutOfRectTouchCallback touchCallback,
56         OutOfRectMouseCallback mouseCallback)
57         : rectGetCallback(std::move(rectGetCallback)), touchCallback(std::move(touchCallback)),
58           mouseCallback(std::move(mouseCallback))
59     {}
60     ~RectCallback() = default;
61     OutOfRectGetRectCallback rectGetCallback;
62     OutOfRectTouchCallback touchCallback;
63     OutOfRectMouseCallback mouseCallback;
64 };
65 
66 struct MarkProcessedEventInfo {
67     int32_t eventId = -1;
68     int64_t lastLogTimeStamp = 0;
69 };
70 
71 class EventManager : virtual public NG::KeyEventManager {
72     DECLARE_ACE_TYPE(EventManager, KeyEventManager);
73 
74 public:
75     EventManager();
76     ~EventManager() override = default;
77     // After the touch down event is triggered, the touch test is performed to collect the corresponding
78     // touch event target list.
79     void TouchTest(const TouchEvent& touchPoint, const RefPtr<RenderNode>& renderNode,
80         TouchRestrict& touchRestrict, const Offset& offset = Offset(),
81         float viewScale = 1.0f, bool needAppend = false);
82 
83     void TouchTest(const TouchEvent& touchPoint, const RefPtr<NG::FrameNode>& frameNode,
84         TouchRestrict& touchRestrict, const Offset& offset = Offset(),
85         float viewScale = 1.0f, bool needAppend = false);
86 
87     bool PostEventTouchTest(const TouchEvent& touchPoint, const RefPtr<NG::UINode>& uiNode,
88         TouchRestrict& touchRestrict);
89 
90     void TouchTest(const AxisEvent& event, const RefPtr<RenderNode>& renderNode, TouchRestrict& touchRestrict);
91 
92     void TouchTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict);
93 
94     bool HasDifferentDirectionGesture();
95 
96     bool OnNonPointerEvent(const NonPointerEvent& event);
97     bool DispatchTouchEvent(const TouchEvent& point);
98     bool DispatchTouchEvent(const AxisEvent& event);
99     void DispatchTouchCancelToRecognizer(
100         TouchEventTarget* touchEventTarget, const std::vector<std::pair<int32_t, TouchTestResult::iterator>>& items);
101     bool PostEventDispatchTouchEvent(const TouchEvent& point);
102     void FlushTouchEventsBegin(const std::list<TouchEvent>& touchEvents);
103     void FlushTouchEventsEnd(const std::list<TouchEvent>& touchEvents);
104     void PostEventFlushTouchEventEnd(const TouchEvent& touchEvent);
105 
106     // Distribute the rotation event to the corresponding render tree or requested render node. If the render is not
107     // processed, return false and the platform will handle it.
108     static bool DispatchRotationEvent(
109         const RotationEvent& event, const RefPtr<RenderNode>& renderNode, const RefPtr<RenderNode>& requestFocusNode);
110 
111     // mouse event target list.
112     void MouseTest(const MouseEvent& touchPoint, const RefPtr<RenderNode>& renderNode);
113     bool DispatchMouseEvent(const MouseEvent& event);
114     void DispatchMouseHoverAnimation(const MouseEvent& event);
115     bool DispatchMouseHoverEvent(const MouseEvent& event);
116 
117     void LogPrintMouseTest();
118     void MouseTest(const MouseEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict);
119     void AccessibilityHoverTest(
120         const TouchEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict);
121     void UpdateAccessibilityHoverNode(const TouchEvent& event, const TouchTestResult& testResult);
122     void PenHoverTest(const TouchEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict);
123     void UpdatePenHoverNode(const TouchEvent& event, const TouchTestResult& testResult);
124     void UpdatePenHoverMoveNode(const TouchEvent& event, const TouchTestResult& testResult);
125     void UpdateHoverNode(const MouseEvent& event, const TouchTestResult& testResult);
126     bool DispatchMouseEventNG(const MouseEvent& event);
127     void DispatchMouseHoverAnimationNG(const MouseEvent& event);
128     bool DispatchMouseHoverEventNG(const MouseEvent& event);
129     void DispatchHoverEffectEvent(const MouseEvent& event);
130     void DispatchAccessibilityHoverEventNG(const TouchEvent& event);
131     void DispatchPenHoverEventNG(const TouchEvent& event);
132     void DispatchPenHoverMoveEventNG(const TouchEvent& event);
133 
134     void AxisTest(const AxisEvent& event, const RefPtr<RenderNode>& renderNode);
135     bool DispatchAxisEvent(const AxisEvent& event);
136 
137     void AxisTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode);
138     bool DispatchAxisEventNG(const AxisEvent& event);
139 
140     void ClearResults();
SetInstanceId(int32_t instanceId)141     void SetInstanceId(int32_t instanceId)
142     {
143         instanceId_ = instanceId;
144     }
GetInstanceId()145     int32_t GetInstanceId() override
146     {
147         return instanceId_;
148     }
149     void HandleGlobalEvent(const TouchEvent& touchPoint, const RefPtr<TextOverlayManager>& textOverlayManager);
150     void HandleGlobalEventNG(const TouchEvent& touchPoint, const RefPtr<NG::SelectOverlayManager>& selectOverlayManager,
151         const NG::OffsetF& rootOffset);
152 
153     void CollectTabIndexNodes(const RefPtr<FocusNode>& rootNode);
154 
155     void AdjustTabIndexNodes();
156 
157     bool HandleFocusByTabIndex(
158         const KeyEvent& event, const RefPtr<FocusNode>& focusNode, const RefPtr<FocusGroup>& curPage);
159 
160     void HandleOutOfRectCallback(const Point& point, std::vector<RectCallback>& rectCallbackList);
161 
GetGestureReferee()162     RefPtr<GestureReferee> GetGestureReferee()
163     {
164         return referee_;
165     }
166 
GetGestureRefereeNG(const RefPtr<NG::NGGestureRecognizer> & recognizer)167     RefPtr<NG::GestureReferee> GetGestureRefereeNG(const RefPtr<NG::NGGestureRecognizer>& recognizer)
168     {
169         if (recognizer->IsPostEventResult()) {
170             return postEventRefereeNG_;
171         }
172         return refereeNG_;
173     }
174 
GetMouseStyleManager()175     RefPtr<MouseStyleManager> GetMouseStyleManager() const
176     {
177         return mouseStyleManager_;
178     }
179 
FlushCursorStyleRequests()180     void FlushCursorStyleRequests()
181     {
182         CHECK_NULL_VOID(mouseStyleManager_);
183         mouseStyleManager_->VsyncMouseFormat();
184     }
185 
186     bool GetResampleTouchEvent(const std::vector<TouchEvent>& history,
187         const std::vector<TouchEvent>& current, uint64_t nanoTimeStamp, TouchEvent& newTouchEvent);
188 
189     TouchEvent GetLatestPoint(const std::vector<TouchEvent>& current, uint64_t nanoTimeStamp);
190 
191     void DoMouseActionRelease();
192 
SetIsDragging(bool isDragging)193     void SetIsDragging(bool isDragging)
194     {
195         isDragging_ = isDragging;
196     }
197 
IsDragging()198     bool IsDragging() const
199     {
200         return isDragging_;
201     }
202 
SetLastMoveBeforeUp(bool isLastMoveBeforeUp)203     void SetLastMoveBeforeUp(bool isLastMoveBeforeUp)
204     {
205         isLastMoveBeforeUp_ = isLastMoveBeforeUp;
206     }
207 
IsLastMoveBeforeUp()208     bool IsLastMoveBeforeUp() const
209     {
210         return isLastMoveBeforeUp_;
211     }
212 
GetEventTreeRecord(NG::EventTreeType treeType)213     NG::EventTreeRecord& GetEventTreeRecord(NG::EventTreeType treeType)
214     {
215         switch (treeType) {
216             case NG::EventTreeType::TOUCH :
217                 return eventTree_;
218             case NG::EventTreeType::POST_EVENT :
219                 return postEventTree_;
220         }
221     }
222 
223     void DumpEvent(NG::EventTreeType type);
224 
225     void AddGestureSnapshot(
226         int32_t finger, int32_t depth, const RefPtr<TouchEventTarget>& target, NG::EventTreeType type);
227 
GetResponseCtrl()228     RefPtr<NG::ResponseCtrl> GetResponseCtrl()
229     {
230         return responseCtrl_;
231     }
232 
233     void CheckDownEvent(const TouchEvent& touchEvent);
234     void CheckUpEvent(const TouchEvent& touchEvent);
235     std::unordered_map<size_t, TouchTestResult> touchTestResults_;
236     std::unordered_map<size_t, TouchTestResult> postEventTouchTestResults_;
237 
SetInnerFlag(bool value)238     void SetInnerFlag(bool value)
239     {
240         innerEventWin_ = value;
241     }
242 
GetInnerFlag()243     bool GetInnerFlag() const
244     {
245         return innerEventWin_;
246     }
247 
GetLastTouchEventEndTimestamp()248     int64_t GetLastTouchEventEndTimestamp()
249     {
250         return lastTouchEventEndTimestamp_;
251     }
252 
253     void RecordHitEmptyMessage(
254         const TouchEvent& touchPoint, const std::string& resultInfo, const RefPtr<NG::FrameNode>& frameNode);
255 
256     void CheckAndLogLastReceivedTouchEventInfo(int32_t eventId, TouchType type);
257 
258     void CheckAndLogLastConsumedTouchEventInfo(int32_t eventId, TouchType type);
259 
260     void CheckAndLogLastReceivedMouseEventInfo(int32_t eventId, MouseAction action);
261 
262     void CheckAndLogLastConsumedMouseEventInfo(int32_t eventId, MouseAction action);
263 
264     void CheckAndLogLastReceivedAxisEventInfo(int32_t eventId, AxisAction action);
265 
266     void CheckAndLogLastConsumedAxisEventInfo(int32_t eventId, AxisAction action);
267 
268     void CheckAndLogLastReceivedEventInfo(int32_t eventId, bool logImmediately = false);
269 
270     void CheckAndLogLastConsumedEventInfo(int32_t eventId, bool logImmediately = false);
271 
272     void ClearTouchTestTargetForPenStylus(TouchEvent& touchEvent);
273 
GetDownFingerIds()274     inline const std::unordered_map<int32_t, int32_t>& GetDownFingerIds() const
275     {
276         return downFingerIds_;
277     }
278 
GetIdToTouchPoint()279     inline const std::unordered_map<int32_t, TouchEvent>& GetIdToTouchPoint() const
280     {
281         return idToTouchPoints_;
282     }
283 
SetIdToTouchPoint(std::unordered_map<int32_t,TouchEvent> && idToTouchPoint)284     inline void SetIdToTouchPoint(std::unordered_map<int32_t, TouchEvent>&& idToTouchPoint)
285     {
286         idToTouchPoints_ = std::move(idToTouchPoint);
287     }
288 
GetLastDispatchTime()289     inline const std::unordered_map<int32_t, uint64_t>& GetLastDispatchTime() const
290     {
291         return lastDispatchTime_;
292     }
293 
SetLastDispatchTime(std::unordered_map<int32_t,uint64_t> && lastDispatchTime)294     inline void SetLastDispatchTime(std::unordered_map<int32_t, uint64_t>&& lastDispatchTime)
295     {
296         lastDispatchTime_ = std::move(lastDispatchTime);
297     }
298 
299     TouchEvent ConvertAxisEventToTouchEvent(const AxisEvent& axisEvent);
300 
301 #if defined(SUPPORT_TOUCH_TARGET_TEST)
302     bool TouchTargetHitTest(const TouchEvent& touchPoint, const RefPtr<NG::FrameNode>& frameNode,
303         TouchRestrict& touchRestrict, const Offset& offset = Offset(), float viewScale = 1.0f,
304         bool needAppend = false, const std::string& target = "");
305 #endif
306 private:
307     void SetHittedFrameNode(const std::list<RefPtr<NG::NGGestureRecognizer>>& touchTestResults);
308     void CleanGestureEventHub();
309     void GetTouchTestIds(const TouchEvent& touchPoint, std::vector<std::string>& touchTestIds,
310         bool& isMousePressAtSelectedNode, int32_t selectedNodeId);
311     void CheckMouseTestResults(bool& isMousePressAtSelectedNode, int32_t selectedNodeId);
312     void LogTouchTestResultRecognizers(const TouchTestResult& result, int32_t touchEventId);
313     void LogTouchTestRecognizerStates(int32_t touchEventId);
314     void DispatchTouchEventToTouchTestResult(TouchEvent touchEvent, TouchTestResult touchTestResult,
315         bool sendOnTouch);
316     void CleanRecognizersForDragBegin(TouchEvent& touchEvent);
317     void SetResponseLinkRecognizers(const TouchTestResult& result, const ResponseLinkResult& responseLinkRecognizers);
318     void FalsifyCancelEventAndDispatch(const TouchEvent& touchPoint);
319     void FalsifyCancelEventAndDispatch(const AxisEvent& axisEvent);
320     void FalsifyHoverCancelEventAndDispatch(const TouchEvent& touchPoint);
321     void DoSingleMouseActionRelease(MouseButton button);
322     bool DispatchMouseEventInGreatOrEqualAPI13(const MouseEvent& event);
323     bool DispatchMouseEventInLessAPI13(const MouseEvent& event);
324     void DispatchMouseEventToPressResults(const MouseEvent& event, const MouseTestResult& targetResults,
325         MouseTestResult& handledResults, bool& isStopPropagation);
326     bool DispatchMouseEventToCurResults(
327         const MouseEvent& event, const MouseTestResult& handledResults, bool isStopPropagation);
328     bool DispatchMouseEventToCurResultsInLessAPI13(
329         const MouseEvent& event, const MouseTestResult& handledResults, bool isStopPropagation);
330     bool innerEventWin_ = false;
331     std::unordered_map<size_t, TouchTestResult> mouseTestResults_;
332     MouseTestResult currMouseTestResults_;
333     // used less than API13
334     MouseTestResult pressMouseTestResults_;
335     // used great or equal API13
336     std::unordered_map<MouseButton, MouseTestResult> pressMouseTestResultsMap_;
337     HoverTestResult currHoverTestResults_;
338     HoverTestResult lastHoverTestResults_;
339     HoverTestResult curAccessibilityHoverResults_;
340     HoverTestResult lastAccessibilityHoverResults_;
341     HoverTestResult curPenHoverResults_;
342     HoverTestResult curPenHoverMoveResults_;
343     HoverTestResult lastPenHoverResults_;
344     AxisTestResult axisTestResults_;
345     WeakPtr<NG::FrameNode> lastHoverNode_;
346     WeakPtr<NG::FrameNode> currHoverNode_;
347     std::unordered_map<size_t, TouchTestResult> axisTouchTestResults_;
348     MouseHoverTestList mouseHoverTestResults_;
349     MouseHoverTestList mouseHoverTestResultsPre_;
350     WeakPtr<RenderNode> mouseHoverNodePre_;
351     WeakPtr<RenderNode> mouseHoverNode_;
352     WeakPtr<RenderNode> axisNode_;
353     int32_t instanceId_ = 0;
354     uint32_t lastHoverDispatchLength_ = 0;
355     uint32_t lastAccessibilityHoverDispatchLength_ = 0;
356     uint32_t lastPenHoverDispatchLength_ = 0;
357     bool inSelectedRect_ = false;
358     bool isDragging_ = false;
359     bool isLastMoveBeforeUp_ = false;
360     RefPtr<GestureReferee> referee_;
361     RefPtr<NG::GestureReferee> refereeNG_;
362     RefPtr<NG::GestureReferee> postEventRefereeNG_;
363     RefPtr<MouseStyleManager> mouseStyleManager_;
364     NG::EventTreeRecord eventTree_;
365     NG::EventTreeRecord postEventTree_;
366     RefPtr<NG::ResponseCtrl> responseCtrl_;
367     TimeStamp lastEventTime_;
368     int64_t lastTouchEventEndTimestamp_ = 0;
369     std::unordered_map<int32_t, int32_t> downFingerIds_;
370     std::set<WeakPtr<NG::FrameNode>> hittedFrameNode_;
371     MarkProcessedEventInfo lastReceivedEvent_;
372     MarkProcessedEventInfo lastConsumedEvent_;
373     int32_t lastDownFingerNumber_ = 0;
374     SourceTool lastSourceTool_ = SourceTool::UNKNOWN;
375     // used to pseudo cancel event.
376     TouchEvent lastTouchEvent_;
377     std::unordered_map<int32_t, TouchEvent> idToTouchPoints_;
378     std::unordered_map<int32_t, uint64_t> lastDispatchTime_;
379 };
380 
381 } // namespace OHOS::Ace
382 
383 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_EVENT_MANAGER_H
384