• 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/components/common/layout/constants.h"
24 #include "core/components_ng/gestures/gesture_referee.h"
25 #include "core/event/axis_event.h"
26 #include "core/event/key_event.h"
27 #include "core/event/mouse_event.h"
28 #include "core/event/rotation_event.h"
29 #include "core/event/touch_event.h"
30 #include "core/focus/focus_node.h"
31 #include "core/gestures/gesture_referee.h"
32 
33 namespace OHOS::Ace {
34 
35 namespace NG {
36 class FrameNode;
37 class SelectOverlayManager;
38 } // namespace NG
39 class RenderNode;
40 class Element;
41 class TextOverlayManager;
42 using MouseHoverTestList = std::list<WeakPtr<RenderNode>>;
43 using OutOfRectGetRectCallback = std::function<void(std::vector<Rect>&)>;
44 using OutOfRectTouchCallback = std::function<void(void)>;
45 using OutOfRectMouseCallback = std::function<void(void)>;
46 
47 struct RectCallback final {
RectCallbackfinal48     RectCallback(OutOfRectGetRectCallback rectGetCallback, OutOfRectTouchCallback touchCallback,
49         OutOfRectMouseCallback mouseCallback)
50         : rectGetCallback(std::move(rectGetCallback)), touchCallback(std::move(touchCallback)),
51           mouseCallback(std::move(mouseCallback))
52     {}
53     ~RectCallback() = default;
54     OutOfRectGetRectCallback rectGetCallback;
55     OutOfRectTouchCallback touchCallback;
56     OutOfRectMouseCallback mouseCallback;
57 };
58 
59 class EventManager : public virtual AceType {
60     DECLARE_ACE_TYPE(EventManager, AceType);
61 public:
62     EventManager();
63     ~EventManager() override = default;
64     // After the touch down event is triggered, the touch test is performed to collect the corresponding
65     // touch event target list.
66     void TouchTest(const TouchEvent& touchPoint, const RefPtr<RenderNode>& renderNode,
67         const TouchRestrict& touchRestrict, const Offset& offset = Offset(),
68         float viewScale = 1.0f, bool needAppend = false);
69 
70     void TouchTest(const TouchEvent& touchPoint, const RefPtr<NG::FrameNode>& frameNode,
71         const TouchRestrict& touchRestrict, const Offset& offset = Offset(),
72         float viewScale = 1.0f, bool needAppend = false);
73 
74     void TouchTest(const AxisEvent& event, const RefPtr<RenderNode>& renderNode, const TouchRestrict& touchRestrict);
75 
76     void TouchTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode, const TouchRestrict& touchRestrict);
77 
78     bool DispatchTouchEvent(const TouchEvent& point);
79     bool DispatchTouchEvent(const AxisEvent& event);
80     void FlushTouchEventsBegin(const std::list<TouchEvent>& touchEvents);
81     void FlushTouchEventsEnd(const std::list<TouchEvent>& touchEvents);
82 
83     // Distribute the key event to the corresponding root node. If the root node is not processed, return false and the
84     // platform will handle it.
85     bool DispatchKeyEvent(const KeyEvent& event, const RefPtr<FocusNode>& focusNode);
86     bool DispatchTabIndexEvent(
87         const KeyEvent& event, const RefPtr<FocusNode>& focusNode, const RefPtr<FocusGroup>& mainNode);
88 
89     // Distribute the key event to the corresponding root node. If the root node is not processed, return false and the
90     // platform will handle it.
91     bool DispatchKeyEventNG(const KeyEvent& event, const RefPtr<NG::FrameNode>& focusNode);
92     bool DispatchTabIndexEventNG(
93         const KeyEvent& event, const RefPtr<NG::FrameNode>& focusNode, const RefPtr<NG::FrameNode>& mainNode);
94 
95     // Distribute the rotation event to the corresponding render tree or requested render node. If the render is not
96     // processed, return false and the platform will handle it.
97     static bool DispatchRotationEvent(
98         const RotationEvent& event, const RefPtr<RenderNode>& renderNode, const RefPtr<RenderNode>& requestFocusNode);
99 
100     // mouse event target list.
101     void MouseTest(const MouseEvent& touchPoint, const RefPtr<RenderNode>& renderNode);
102     bool DispatchMouseEvent(const MouseEvent& event);
103     void DispatchMouseHoverAnimation(const MouseEvent& event);
104     bool DispatchMouseHoverEvent(const MouseEvent& event);
105 
106     void LogPrintMouseTest();
107     void MouseTest(const MouseEvent& event, const RefPtr<NG::FrameNode>& frameNode, const TouchRestrict& touchRestrict);
108     bool DispatchMouseEventNG(const MouseEvent& event);
109     void DispatchMouseHoverAnimationNG(const MouseEvent& event);
110     bool DispatchMouseHoverEventNG(const MouseEvent& event);
111     void DispatchHoverEffectEvent(const MouseEvent& event);
112 
113     void AxisTest(const AxisEvent& event, const RefPtr<RenderNode>& renderNode);
114     bool DispatchAxisEvent(const AxisEvent& event);
115 
116     void AxisTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode);
117     bool DispatchAxisEventNG(const AxisEvent& event);
118 
119     void ClearResults();
SetInstanceId(int32_t instanceId)120     void SetInstanceId(int32_t instanceId)
121     {
122         instanceId_ = instanceId;
123     }
GetInstanceId()124     int32_t GetInstanceId()
125     {
126         return instanceId_;
127     }
128     void HandleGlobalEvent(const TouchEvent& touchPoint, const RefPtr<TextOverlayManager>& textOverlayManager);
129     void HandleGlobalEventNG(const TouchEvent& touchPoint, const RefPtr<NG::SelectOverlayManager>& selectOverlayManager,
130         const NG::OffsetF& rootOffset);
131 
132     void CollectTabIndexNodes(const RefPtr<FocusNode>& rootNode);
133 
134     void AdjustTabIndexNodes();
135 
136     bool HandleFocusByTabIndex(
137         const KeyEvent& event, const RefPtr<FocusNode>& focusNode, const RefPtr<FocusGroup>& curPage);
138 
139     void HandleOutOfRectCallback(const Point& point, std::vector<RectCallback>& rectCallbackList);
140 
GetGestureReferee()141     RefPtr<GestureReferee> GetGestureReferee()
142     {
143         return referee_;
144     }
145 
GetGestureRefereeNG()146     RefPtr<NG::GestureReferee> GetGestureRefereeNG()
147     {
148         return refereeNG_;
149     }
150 
151     void DispatchKeyboardShortcut(const KeyEvent& event);
152 
153     void AddKeyboardShortcutNode(const WeakPtr<NG::FrameNode>& node);
154 
155     void DelKeyboardShortcutNode(int32_t nodeId);
156 
157     void AddKeyboardShortcutKeys(uint8_t keys, std::vector<KeyCode>& leftKeyCode, std::vector<KeyCode>& rightKeyCode,
158         std::vector<uint8_t>& permutation);
159 
IsKeyInPressed(KeyCode tarCode)160     bool IsKeyInPressed(KeyCode tarCode) const
161     {
162         return std::any_of(pressedKeyCodes_.begin(), pressedKeyCodes_.end(),
163             [tarCode](const KeyCode& code) { return code == tarCode; });
164     }
SetPressedKeyCodes(const std::vector<KeyCode> & pressedKeyCodes)165     void SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes)
166     {
167         pressedKeyCodes_ = pressedKeyCodes;
168     }
169 
170     bool IsSameKeyboardShortcutNode(const std::string& value, uint8_t keys);
171 
172     bool IsSystemKeyboardShortcut(const std::string& value, uint8_t keys);
173 
174     uint8_t GetKeyboardShortcutKeys(const std::vector<ModifierKey>& keys);
175 
176     void DoMouseActionRelease();
177 
SetIsDragging(bool isDragging)178     void SetIsDragging(bool isDragging)
179     {
180         isDragging_ = isDragging;
181     }
182 
IsDragging()183     bool IsDragging() const
184     {
185         return isDragging_;
186     }
187 
188 private:
189     std::unordered_map<size_t, TouchTestResult> touchTestResults_;
190     std::unordered_map<size_t, MouseTestResult> mouseTestResults_;
191     MouseTestResult currMouseTestResults_;
192     MouseTestResult pressMouseTestResults_;
193     HoverTestResult currHoverTestResults_;
194     HoverTestResult lastHoverTestResults_;
195     AxisTestResult axisTestResults_;
196     WeakPtr<NG::FrameNode> lastHoverNode_;
197     WeakPtr<NG::FrameNode> currHoverNode_;
198     std::unordered_map<size_t, TouchTestResult> axisTouchTestResults_;
199     MouseHoverTestList mouseHoverTestResults_;
200     MouseHoverTestList mouseHoverTestResultsPre_;
201     WeakPtr<RenderNode> mouseHoverNodePre_;
202     WeakPtr<RenderNode> mouseHoverNode_;
203     WeakPtr<RenderNode> axisNode_;
204     int32_t instanceId_ = 0;
205     uint32_t lastHoverDispatchLength_ = 0;
206     bool inSelectedRect_ = false;
207     bool isDragging_ = false;
208     RefPtr<GestureReferee> referee_;
209     RefPtr<NG::GestureReferee> refereeNG_;
210     std::list<WeakPtr<NG::FrameNode>> keyboardShortcutNode_;
211     std::vector<KeyCode> pressedKeyCodes_;
212 };
213 
214 } // namespace OHOS::Ace
215 
216 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_EVENT_MANAGER_H
217