• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 namespace OHOS::Ace::NG {
25 
26 struct TouchTestResultInfo {
27     int32_t nodeId = 0;
28     std::string tag;
29     std::string inspectorId;
30     std::string frameRect;
31     int32_t depth = 0;
32 };
33 
34 class GestureEventHub;
35 
36 class TouchEventImpl : public virtual AceType {
DECLARE_ACE_TYPE(TouchEventImpl,AceType)37     DECLARE_ACE_TYPE(TouchEventImpl, AceType)
38 public:
39     explicit TouchEventImpl(TouchEventFunc&& callback) : callback_(std::move(callback)) {}
40     ~TouchEventImpl() override = default;
41 
GetTouchEventCallback()42     const TouchEventFunc& GetTouchEventCallback() const
43     {
44         return callback_;
45     }
46 
operator()47     void operator()(TouchEventInfo& info) const
48     {
49         if (callback_) {
50             callback_(info);
51         }
52     }
53 
54 private:
55     TouchEventFunc callback_;
56 };
57 
58 class ACE_FORCE_EXPORT TouchEventActuator : public GestureEventActuator, public TouchEventTarget {
59     DECLARE_ACE_TYPE(TouchEventActuator, GestureEventActuator, TouchEventTarget)
60 public:
61     TouchEventActuator() = default;
62     ~TouchEventActuator() override = default;
63 
ReplaceTouchEvent(TouchEventFunc && callback)64     void ReplaceTouchEvent(TouchEventFunc&& callback)
65     {
66         if (userCallback_) {
67             userCallback_.Reset();
68         }
69         userCallback_ = MakeRefPtr<TouchEventImpl>(std::move(callback));
70     }
71 
SetOnTouchEvent(TouchEventFunc && callback)72     void SetOnTouchEvent(TouchEventFunc&& callback)
73     {
74         if (onTouchEventCallback_) {
75             onTouchEventCallback_.Reset();
76         }
77         onTouchEventCallback_ = MakeRefPtr<TouchEventImpl>(std::move(callback));
78     }
79 
ClearUserCallback()80     void ClearUserCallback()
81     {
82         // When the event param is undefined, it will clear the callback.
83         if (userCallback_) {
84             userCallback_.Reset();
85         }
86     }
87 
88     void OnFlushTouchEventsBegin() override;
89     void OnFlushTouchEventsEnd() override;
90 
AddTouchEvent(const RefPtr<TouchEventImpl> & touchEvent)91     void AddTouchEvent(const RefPtr<TouchEventImpl>& touchEvent)
92     {
93         if (touchEvents_.empty()) {
94             touchEvents_.emplace_back(touchEvent);
95             return;
96         }
97         if (std::find(touchEvents_.begin(), touchEvents_.end(), touchEvent) == touchEvents_.end()) {
98             touchEvents_.emplace_back(touchEvent);
99         }
100     }
101 
RemoveTouchEvent(const RefPtr<TouchEventImpl> & touchEvent)102     void RemoveTouchEvent(const RefPtr<TouchEventImpl>& touchEvent)
103     {
104         touchEvents_.remove(touchEvent);
105     }
106 
OnCollectTouchTarget(const OffsetF & coordinateOffset,const TouchRestrict & touchRestrict,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)107     void OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict,
108         const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result) override
109     {
110         SetGetEventTargetImpl(getEventTargetImpl);
111         SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
112         result.emplace_back(Claim(this));
113     }
114 
115     bool DispatchEvent(const TouchEvent& point) override;
116     bool HandleEvent(const TouchEvent& point) override;
117 
CopyTouchEvent(const RefPtr<TouchEventActuator> & touchEventActuator)118     void CopyTouchEvent(const RefPtr<TouchEventActuator>& touchEventActuator)
119     {
120         touchEvents_ = touchEventActuator->touchEvents_;
121         userCallback_ = touchEventActuator->userCallback_;
122         onTouchEventCallback_ = touchEventActuator->onTouchEventCallback_;
123     }
124 
125 private:
126     bool TriggerTouchCallBack(const TouchEvent& changedPoint);
127     bool ShouldResponse() override;
128 
129     std::list<RefPtr<TouchEventImpl>> touchEvents_;
130     RefPtr<TouchEventImpl> userCallback_;
131     RefPtr<TouchEventImpl> onTouchEventCallback_;
132     // isFlushTouchEventsEnd_ means the last one touch event info during one vsync period, used only for web_pattern
133     // if isFlushTouchEventsEnd_ is true, web_pattern start to send touch event list to chromium
134     bool isFlushTouchEventsEnd_ = false;
135 };
136 
137 } // namespace OHOS::Ace::NG
138 
139 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_TOUCH_EVENT_H
140