1 /* 2 * Copyright (c) 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_COMPONENTS_NG_EVENT_TOUCH_EVENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_TOUCH_EVENT_H 18 19 #include <list> 20 21 #include "base/memory/referenced.h" 22 #include "core/components_ng/event/gesture_event_actuator.h" 23 #include "core/event/touch_event.h" 24 25 namespace OHOS::Ace::NG { 26 27 struct TouchTestResultInfo { 28 int32_t nodeId = 0; 29 std::string tag; 30 std::string inspectorId; 31 std::string frameRect; 32 int32_t depth = 0; 33 }; 34 35 class GestureEventHub; 36 37 class TouchEventImpl : public virtual AceType { DECLARE_ACE_TYPE(TouchEventImpl,AceType)38 DECLARE_ACE_TYPE(TouchEventImpl, AceType) 39 public: 40 explicit TouchEventImpl(TouchEventFunc&& callback) : callback_(std::move(callback)) {} 41 ~TouchEventImpl() override = default; 42 GetTouchEventCallback()43 const TouchEventFunc& GetTouchEventCallback() const 44 { 45 return callback_; 46 } 47 operator()48 void operator()(TouchEventInfo& info) const 49 { 50 if (callback_) { 51 callback_(info); 52 } 53 } 54 55 private: 56 TouchEventFunc callback_; 57 }; 58 59 class ACE_FORCE_EXPORT TouchEventActuator : public GestureEventActuator, public TouchEventTarget { 60 DECLARE_ACE_TYPE(TouchEventActuator, GestureEventActuator, TouchEventTarget) 61 public: 62 TouchEventActuator() = default; 63 ~TouchEventActuator() override = default; 64 ReplaceTouchEvent(TouchEventFunc && callback)65 void ReplaceTouchEvent(TouchEventFunc&& callback) 66 { 67 if (userCallback_) { 68 userCallback_.Reset(); 69 } 70 userCallback_ = MakeRefPtr<TouchEventImpl>(std::move(callback)); 71 } 72 SetOnTouchEvent(TouchEventFunc && callback)73 void SetOnTouchEvent(TouchEventFunc&& callback) 74 { 75 if (onTouchEventCallback_) { 76 onTouchEventCallback_.Reset(); 77 } 78 onTouchEventCallback_ = MakeRefPtr<TouchEventImpl>(std::move(callback)); 79 } 80 ClearUserCallback()81 void ClearUserCallback() 82 { 83 // When the event param is undefined, it will clear the callback. 84 if (userCallback_) { 85 userCallback_.Reset(); 86 } 87 } 88 ClearJSFrameNodeCallback()89 void ClearJSFrameNodeCallback() 90 { 91 // When the event param is undefined, it will clear the callback. 92 if (commonTouchEventCallback_) { 93 commonTouchEventCallback_.Reset(); 94 } 95 } 96 97 void OnFlushTouchEventsBegin() override; 98 void OnFlushTouchEventsEnd() override; 99 AddTouchEvent(const RefPtr<TouchEventImpl> & touchEvent)100 void AddTouchEvent(const RefPtr<TouchEventImpl>& touchEvent) 101 { 102 if (touchEvents_.empty()) { 103 touchEvents_.emplace_back(touchEvent); 104 return; 105 } 106 if (std::find(touchEvents_.begin(), touchEvents_.end(), touchEvent) == touchEvents_.end()) { 107 touchEvents_.emplace_back(touchEvent); 108 } 109 } 110 RemoveTouchEvent(const RefPtr<TouchEventImpl> & touchEvent)111 void RemoveTouchEvent(const RefPtr<TouchEventImpl>& touchEvent) 112 { 113 touchEvents_.remove(touchEvent); 114 } 115 AddTouchAfterEvent(const RefPtr<TouchEventImpl> & touchEvent)116 void AddTouchAfterEvent(const RefPtr<TouchEventImpl>& touchEvent) 117 { 118 touchAfterEvents_ = std::move(touchEvent); 119 } 120 ClearTouchAfterEvent()121 void ClearTouchAfterEvent() 122 { 123 // When the event param is undefined, it will clear the callback. 124 if (touchAfterEvents_) { 125 touchAfterEvents_.Reset(); 126 } 127 } 128 OnCollectTouchTarget(const OffsetF & coordinateOffset,const TouchRestrict & touchRestrict,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result,ResponseLinkResult & responseLinkResult)129 void OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict, 130 const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result, 131 ResponseLinkResult& responseLinkResult) override 132 { 133 SetGetEventTargetImpl(getEventTargetImpl); 134 SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY())); 135 result.emplace_back(Claim(this)); 136 } 137 138 bool DispatchEvent(const TouchEvent& point) override; 139 bool HandleEvent(const TouchEvent& point) override; 140 SetJSFrameNodeOnTouchEvent(TouchEventFunc && callback)141 void SetJSFrameNodeOnTouchEvent(TouchEventFunc&& callback) 142 { 143 if (commonTouchEventCallback_) { 144 commonTouchEventCallback_.Reset(); 145 } 146 commonTouchEventCallback_ = MakeRefPtr<TouchEventImpl>(std::move(callback)); 147 } 148 CopyTouchEvent(const RefPtr<TouchEventActuator> & touchEventActuator)149 void CopyTouchEvent(const RefPtr<TouchEventActuator>& touchEventActuator) 150 { 151 touchEvents_ = touchEventActuator->touchEvents_; 152 userCallback_ = touchEventActuator->userCallback_; 153 onTouchEventCallback_ = touchEventActuator->onTouchEventCallback_; 154 commonTouchEventCallback_ = touchEventActuator->commonTouchEventCallback_; 155 } 156 157 private: 158 bool TriggerTouchCallBack(const TouchEvent& changedPoint); 159 bool ShouldResponse() override; 160 void TriggerCallBacks(TouchEventInfo& event); 161 TouchEventInfo CreateTouchEventInfo(const TouchEvent& lastPoint); 162 TouchLocationInfo CreateChangedTouchInfo(const TouchEvent& lastPoint, const TouchEvent& event); 163 TouchLocationInfo CreateTouchItemInfo(const TouchPoint& pointItem, const TouchEvent& event, TouchType type); 164 TouchLocationInfo CreateHistoryTouchItemInfo(const TouchEvent& eventItem, const TouchEvent& event); 165 166 std::list<RefPtr<TouchEventImpl>> touchEvents_; 167 RefPtr<TouchEventImpl> touchAfterEvents_; 168 RefPtr<TouchEventImpl> userCallback_; 169 RefPtr<TouchEventImpl> onTouchEventCallback_; 170 RefPtr<TouchEventImpl> commonTouchEventCallback_; 171 // isFlushTouchEventsEnd_ means the last one touch event info during one vsync period, used only for web_pattern 172 // if isFlushTouchEventsEnd_ is true, web_pattern start to send touch event list to chromium 173 bool isFlushTouchEventsEnd_ = false; 174 std::map<int32_t, TimeStamp> firstInputTimeWithId_; 175 }; 176 177 } // namespace OHOS::Ace::NG 178 179 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_TOUCH_EVENT_H 180