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