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/components/common/layout/constants.h" 25 #include "core/components_ng/event/response_ctrl.h" 26 #include "core/components_ng/gestures/gesture_referee.h" 27 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h" 28 #include "core/event/axis_event.h" 29 #include "core/event/key_event.h" 30 #include "core/event/mouse_event.h" 31 #include "core/event/rotation_event.h" 32 #include "core/event/touch_event.h" 33 #include "core/focus/focus_node.h" 34 #include "core/gestures/gesture_referee.h" 35 36 namespace OHOS::Ace { 37 namespace NG { 38 class FrameNode; 39 class SelectOverlayManager; 40 class ResponseCtrl; 41 } // namespace NG 42 class RenderNode; 43 class Element; 44 class TextOverlayManager; 45 using MouseHoverTestList = std::list<WeakPtr<RenderNode>>; 46 using OutOfRectGetRectCallback = std::function<void(std::vector<Rect>&)>; 47 using OutOfRectTouchCallback = std::function<void(void)>; 48 using OutOfRectMouseCallback = std::function<void(void)>; 49 50 struct RectCallback final { RectCallbackfinal51 RectCallback(OutOfRectGetRectCallback rectGetCallback, OutOfRectTouchCallback touchCallback, 52 OutOfRectMouseCallback mouseCallback) 53 : rectGetCallback(std::move(rectGetCallback)), touchCallback(std::move(touchCallback)), 54 mouseCallback(std::move(mouseCallback)) 55 {} 56 ~RectCallback() = default; 57 OutOfRectGetRectCallback rectGetCallback; 58 OutOfRectTouchCallback touchCallback; 59 OutOfRectMouseCallback mouseCallback; 60 }; 61 62 class EventManager : public virtual AceType { 63 DECLARE_ACE_TYPE(EventManager, AceType); 64 65 public: 66 EventManager(); 67 ~EventManager() override = default; 68 // After the touch down event is triggered, the touch test is performed to collect the corresponding 69 // touch event target list. 70 void TouchTest(const TouchEvent& touchPoint, const RefPtr<RenderNode>& renderNode, 71 TouchRestrict& touchRestrict, const Offset& offset = Offset(), 72 float viewScale = 1.0f, bool needAppend = false); 73 74 void TouchTest(const TouchEvent& touchPoint, const RefPtr<NG::FrameNode>& frameNode, 75 TouchRestrict& touchRestrict, const Offset& offset = Offset(), 76 float viewScale = 1.0f, bool needAppend = false); 77 78 bool PostEventTouchTest(const TouchEvent& touchPoint, const RefPtr<NG::UINode>& uiNode, 79 TouchRestrict& touchRestrict); 80 81 void TouchTest(const AxisEvent& event, const RefPtr<RenderNode>& renderNode, TouchRestrict& touchRestrict); 82 83 void TouchTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict); 84 85 bool HasDifferentDirectionGesture(); 86 87 bool DispatchTouchEvent(const TouchEvent& point); 88 bool DispatchTouchEvent(const AxisEvent& event); 89 bool PostEventDispatchTouchEvent(const TouchEvent& point); 90 void FlushTouchEventsBegin(const std::list<TouchEvent>& touchEvents); 91 void FlushTouchEventsEnd(const std::list<TouchEvent>& touchEvents); 92 void PostEventFlushTouchEventEnd(const TouchEvent& touchEvent); 93 94 // Distribute the key event to the corresponding root node. If the root node is not processed, return false and the 95 // platform will handle it. 96 bool DispatchKeyEvent(const KeyEvent& event, const RefPtr<FocusNode>& focusNode); 97 bool DispatchTabIndexEvent( 98 const KeyEvent& event, const RefPtr<FocusNode>& focusNode, const RefPtr<FocusGroup>& mainNode); 99 100 // Distribute the key event to the corresponding root node. If the root node is not processed, return false and the 101 // platform will handle it. 102 bool DispatchKeyEventNG(const KeyEvent& event, const RefPtr<NG::FrameNode>& focusNode); 103 bool DispatchTabIndexEventNG(const KeyEvent& event, const RefPtr<NG::FrameNode>& mainView); 104 105 // Distribute the rotation event to the corresponding render tree or requested render node. If the render is not 106 // processed, return false and the platform will handle it. 107 static bool DispatchRotationEvent( 108 const RotationEvent& event, const RefPtr<RenderNode>& renderNode, const RefPtr<RenderNode>& requestFocusNode); 109 110 // mouse event target list. 111 void MouseTest(const MouseEvent& touchPoint, const RefPtr<RenderNode>& renderNode); 112 bool DispatchMouseEvent(const MouseEvent& event); 113 void DispatchMouseHoverAnimation(const MouseEvent& event); 114 bool DispatchMouseHoverEvent(const MouseEvent& event); 115 116 void LogPrintMouseTest(); 117 void MouseTest(const MouseEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict); 118 bool DispatchMouseEventNG(const MouseEvent& event); 119 void DispatchMouseHoverAnimationNG(const MouseEvent& event); 120 bool DispatchMouseHoverEventNG(const MouseEvent& event); 121 void DispatchHoverEffectEvent(const MouseEvent& event); 122 123 void AxisTest(const AxisEvent& event, const RefPtr<RenderNode>& renderNode); 124 bool DispatchAxisEvent(const AxisEvent& event); 125 126 void AxisTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode); 127 bool DispatchAxisEventNG(const AxisEvent& event); 128 129 void ClearResults(); SetInstanceId(int32_t instanceId)130 void SetInstanceId(int32_t instanceId) 131 { 132 instanceId_ = instanceId; 133 } GetInstanceId()134 int32_t GetInstanceId() 135 { 136 return instanceId_; 137 } 138 void HandleGlobalEvent(const TouchEvent& touchPoint, const RefPtr<TextOverlayManager>& textOverlayManager); 139 void HandleGlobalEventNG(const TouchEvent& touchPoint, const RefPtr<NG::SelectOverlayManager>& selectOverlayManager, 140 const NG::OffsetF& rootOffset); 141 142 void CollectTabIndexNodes(const RefPtr<FocusNode>& rootNode); 143 144 void AdjustTabIndexNodes(); 145 146 bool HandleFocusByTabIndex( 147 const KeyEvent& event, const RefPtr<FocusNode>& focusNode, const RefPtr<FocusGroup>& curPage); 148 149 void HandleOutOfRectCallback(const Point& point, std::vector<RectCallback>& rectCallbackList); 150 GetGestureReferee()151 RefPtr<GestureReferee> GetGestureReferee() 152 { 153 return referee_; 154 } 155 GetGestureRefereeNG(const RefPtr<NG::NGGestureRecognizer> & recognizer)156 RefPtr<NG::GestureReferee> GetGestureRefereeNG(const RefPtr<NG::NGGestureRecognizer>& recognizer) 157 { 158 if (recognizer->IsPostEventResult()) { 159 return postEventRefereeNG_; 160 } 161 return refereeNG_; 162 } 163 164 void DispatchKeyboardShortcut(const KeyEvent& event); 165 166 void AddKeyboardShortcutNode(const WeakPtr<NG::FrameNode>& node); 167 168 void DelKeyboardShortcutNode(int32_t nodeId); 169 170 void AddKeyboardShortcutKeys(uint8_t keys, std::vector<KeyCode>& leftKeyCode, std::vector<KeyCode>& rightKeyCode, 171 std::vector<uint8_t>& permutation); 172 IsKeyInPressed(KeyCode tarCode)173 bool IsKeyInPressed(KeyCode tarCode) const 174 { 175 return std::any_of(pressedKeyCodes_.begin(), pressedKeyCodes_.end(), 176 [tarCode](const KeyCode& code) { return code == tarCode; }); 177 } SetPressedKeyCodes(const std::vector<KeyCode> & pressedKeyCodes)178 void SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes) 179 { 180 pressedKeyCodes_ = pressedKeyCodes; 181 } 182 183 bool IsSameKeyboardShortcutNode(const std::string& value, uint8_t keys); 184 185 bool IsSystemKeyboardShortcut(const std::string& value, uint8_t keys); 186 187 uint8_t GetKeyboardShortcutKeys(const std::vector<ModifierKey>& keys); 188 189 void DoMouseActionRelease(); 190 SetIsDragging(bool isDragging)191 void SetIsDragging(bool isDragging) 192 { 193 isDragging_ = isDragging; 194 } 195 IsDragging()196 bool IsDragging() const 197 { 198 return isDragging_; 199 } 200 SetLastMoveBeforeUp(bool isLastMoveBeforeUp)201 void SetLastMoveBeforeUp(bool isLastMoveBeforeUp) 202 { 203 isLastMoveBeforeUp_ = isLastMoveBeforeUp; 204 } 205 IsLastMoveBeforeUp()206 bool IsLastMoveBeforeUp() const 207 { 208 return isLastMoveBeforeUp_; 209 } 210 GetEventTreeRecord()211 NG::EventTreeRecord& GetEventTreeRecord() 212 { 213 return eventTree_; 214 } 215 216 void DumpEvent() const; 217 218 void AddGestureSnapshot(int32_t finger, int32_t depth, const RefPtr<TouchEventTarget>& target); 219 GetResponseCtrl()220 RefPtr<NG::ResponseCtrl> GetResponseCtrl() 221 { 222 return responseCtrl_; 223 } 224 225 void CheckTouchEvent(TouchEvent touchEvent); 226 std::unordered_map<size_t, TouchTestResult> touchTestResults_; 227 std::unordered_map<size_t, TouchTestResult> postEventTouchTestResults_; 228 SetInnerFlag(bool value)229 void SetInnerFlag(bool value) 230 { 231 innerEventWin_ = value; 232 } 233 GetInnerFlag()234 bool GetInnerFlag() const 235 { 236 return innerEventWin_; 237 } 238 239 private: 240 void SetHittedFrameNode(const std::list<RefPtr<NG::NGGestureRecognizer>>& touchTestResults); 241 void CleanGestureEventHub(); 242 void GetTouchTestIds(const TouchEvent& touchPoint, std::vector<std::string>& touchTestIds, 243 bool& isMousePressAtSelectedNode, int32_t selectedNodeId); 244 void CheckMouseTestResults(bool& isMousePressAtSelectedNode, int32_t selectedNodeId); 245 void LogTouchTestResultRecognizers(const TouchTestResult& result); 246 bool innerEventWin_ = false; 247 std::unordered_map<size_t, MouseTestResult> mouseTestResults_; 248 MouseTestResult currMouseTestResults_; 249 MouseTestResult pressMouseTestResults_; 250 HoverTestResult currHoverTestResults_; 251 HoverTestResult lastHoverTestResults_; 252 AxisTestResult axisTestResults_; 253 WeakPtr<NG::FrameNode> lastHoverNode_; 254 WeakPtr<NG::FrameNode> currHoverNode_; 255 std::unordered_map<size_t, TouchTestResult> axisTouchTestResults_; 256 MouseHoverTestList mouseHoverTestResults_; 257 MouseHoverTestList mouseHoverTestResultsPre_; 258 WeakPtr<RenderNode> mouseHoverNodePre_; 259 WeakPtr<RenderNode> mouseHoverNode_; 260 WeakPtr<RenderNode> axisNode_; 261 int32_t instanceId_ = 0; 262 uint32_t lastHoverDispatchLength_ = 0; 263 bool inSelectedRect_ = false; 264 bool isDragging_ = false; 265 bool isLastMoveBeforeUp_ = false; 266 RefPtr<GestureReferee> referee_; 267 RefPtr<NG::GestureReferee> refereeNG_; 268 RefPtr<NG::GestureReferee> postEventRefereeNG_; 269 std::list<WeakPtr<NG::FrameNode>> keyboardShortcutNode_; 270 std::vector<KeyCode> pressedKeyCodes_; 271 NG::EventTreeRecord eventTree_; 272 RefPtr<NG::ResponseCtrl> responseCtrl_; 273 TimeStamp lastEventTime_; 274 std::set<int32_t> downFingerIds_; 275 std::set<WeakPtr<NG::FrameNode>> hittedFrameNode_; 276 }; 277 278 } // namespace OHOS::Ace 279 280 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_EVENT_MANAGER_H 281