• 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_ON_MOUSE_EVENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_ON_MOUSE_EVENT_H
18 
19 #include "core/event/mouse_event.h"
20 
21 namespace OHOS::Ace::NG {
22 
23 class InputEventHub;
24 class FrameNode;
25 
26 class InputEvent : public virtual AceType {
27     DECLARE_ACE_TYPE(InputEvent, AceType);
28 public:
InputEvent(OnMouseEventFunc && callback)29     explicit InputEvent(OnMouseEventFunc&& callback) : onMouseCallback_(std::move(callback)) {}
30 
InputEvent(OnHoverEventFunc && callback)31     explicit InputEvent(OnHoverEventFunc&& callback) : onHoverCallback_(std::move(callback)) {}
32 
InputEvent(OnHoverFunc && callback)33     explicit InputEvent(OnHoverFunc&& callback) : onHoverEventCallback_(std::move(callback)) {}
34 
InputEvent(OnHoverMoveFunc && callback)35     explicit InputEvent(OnHoverMoveFunc&& callback) : onHoverMoveCallback_(std::move(callback)) {}
36 
InputEvent(OnAxisEventFunc && callback)37     explicit InputEvent(OnAxisEventFunc&& callback) : onAxisCallback_(std::move(callback)) {}
38 
InputEvent(OnAccessibilityHoverFunc && callback)39     explicit InputEvent(OnAccessibilityHoverFunc&& callback) : onAccessibilityHoverFunc_(std::move(callback)) {}
40 
41     ~InputEvent() override = default;
42 
GetOnMouseEventFunc()43     const OnMouseEventFunc& GetOnMouseEventFunc() const
44     {
45         return onMouseCallback_;
46     }
47 
GetOnHoverEventFunc()48     const OnHoverEventFunc& GetOnHoverEventFunc() const
49     {
50         return onHoverCallback_;
51     }
52 
GetOnHoverMoveEventFunc()53     const OnHoverMoveFunc& GetOnHoverMoveEventFunc() const
54     {
55         return onHoverMoveCallback_;
56     }
57 
GetOnHoverFunc()58     const OnHoverFunc& GetOnHoverFunc() const
59     {
60         return onHoverEventCallback_;
61     }
62 
GetOnAxisEventFunc()63     const OnAxisEventFunc& GetOnAxisEventFunc() const
64     {
65         return onAxisCallback_;
66     }
67 
GetOnAccessibilityHoverFunc()68     const OnAccessibilityHoverFunc& GetOnAccessibilityHoverFunc() const
69     {
70         return onAccessibilityHoverFunc_;
71     }
72 
operator()73     void operator()(MouseInfo& info) const
74     {
75         if (onMouseCallback_) {
76             onMouseCallback_(info);
77         }
78     }
79 
operator()80     void operator()(bool state, HoverInfo& info) const
81     {
82         if (onHoverEventCallback_) {
83             onHoverEventCallback_(state, info);
84         }
85     }
operator()86     void operator()(bool state) const
87     {
88         if (onHoverCallback_) {
89             onHoverCallback_(state);
90         }
91     }
92 
operator()93     void operator()(AxisInfo& info) const
94     {
95         if (onAxisCallback_) {
96             onAxisCallback_(info);
97         }
98     }
99 
operator()100     void operator()(HoverInfo& info) const
101     {
102         if (onHoverMoveCallback_) {
103             onHoverMoveCallback_(info);
104         }
105     }
106 
operator()107     void operator()(bool state, AccessibilityHoverInfo& info) const
108     {
109         if (onAccessibilityHoverFunc_) {
110             onAccessibilityHoverFunc_(state, info);
111         }
112     }
GetIstips()113     bool GetIstips() const
114     {
115         return istips_;
116     }
117 
SetIstips(bool istips)118     void SetIstips(bool istips)
119     {
120         istips_ = istips;
121     }
122 
GetTipsFollowCursor()123     bool GetTipsFollowCursor() const
124     {
125         return followCursor_;
126     }
127 
SetTipsFollowCursor(bool followCursor)128     void SetTipsFollowCursor(bool followCursor)
129     {
130         followCursor_ = followCursor;
131     }
132 
133 private:
134     OnMouseEventFunc onMouseCallback_;
135     OnHoverEventFunc onHoverCallback_;
136     OnHoverFunc onHoverEventCallback_;
137     OnHoverMoveFunc onHoverMoveCallback_;
138     OnAxisEventFunc onAxisCallback_;
139     OnAccessibilityHoverFunc onAccessibilityHoverFunc_;
140     bool istips_ = false;
141     bool followCursor_ = false;
142 };
143 
144 class ACE_EXPORT InputEventActuator : public virtual AceType {
145     DECLARE_ACE_TYPE(InputEventActuator, AceType);
146 public:
147     explicit InputEventActuator(const WeakPtr<InputEventHub>& inputEventHub);
148     ~InputEventActuator() override = default;
149 
ClearUserCallback()150     void ClearUserCallback()
151     {
152         if (userCallback_) {
153             userCallback_.Reset();
154         }
155     }
156 
HasUserCallback()157     bool HasUserCallback()
158     {
159         return userCallback_ != nullptr;
160     }
161 
ClearJSFrameNodeCallback()162     void ClearJSFrameNodeCallback()
163     {
164         if (userJSFrameNodeCallback_) {
165             userJSFrameNodeCallback_.Reset();
166         }
167     }
168 
ReplaceJSFrameNodeInputEvent(OnMouseEventFunc && callback)169     void ReplaceJSFrameNodeInputEvent(OnMouseEventFunc&& callback)
170     {
171         if (userJSFrameNodeCallback_) {
172             userJSFrameNodeCallback_.Reset();
173         }
174         userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
175     }
ReplaceJSFrameNodeInputEvent(OnHoverFunc && callback)176     void ReplaceJSFrameNodeInputEvent(OnHoverFunc&& callback)
177     {
178         if (userJSFrameNodeCallback_) {
179             userJSFrameNodeCallback_.Reset();
180         }
181         userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
182     }
183 
ReplaceJSFrameNodeInputEvent(OnHoverMoveFunc && callback)184     void ReplaceJSFrameNodeInputEvent(OnHoverMoveFunc&& callback)
185     {
186         if (userJSFrameNodeCallback_) {
187             userJSFrameNodeCallback_.Reset();
188         }
189         userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
190     }
191 
ReplaceInputEvent(OnMouseEventFunc && callback)192     void ReplaceInputEvent(OnMouseEventFunc&& callback)
193     {
194         if (userCallback_) {
195             userCallback_.Reset();
196         }
197         userCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
198     }
ReplaceInputEvent(OnHoverFunc && callback)199     void ReplaceInputEvent(OnHoverFunc&& callback)
200     {
201         if (userCallback_) {
202             userCallback_.Reset();
203         }
204         userCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
205     }
206 
ReplaceInputEvent(OnHoverMoveFunc && callback)207     void ReplaceInputEvent(OnHoverMoveFunc&& callback)
208     {
209         if (userCallback_) {
210             userCallback_.Reset();
211         }
212         userCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
213     }
214 
ReplaceInputEvent(OnAxisEventFunc && callback)215     void ReplaceInputEvent(OnAxisEventFunc&& callback)
216     {
217         if (userCallback_) {
218             userCallback_.Reset();
219         }
220         userCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
221     }
222 
ReplaceInputEvent(OnAccessibilityHoverFunc && callback)223     void ReplaceInputEvent(OnAccessibilityHoverFunc&& callback)
224     {
225         if (userCallback_) {
226             userCallback_.Reset();
227         }
228         userCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
229     }
230 
AddInputEvent(const RefPtr<InputEvent> & inputEvent)231     void AddInputEvent(const RefPtr<InputEvent>& inputEvent)
232     {
233         if (inputEvents_.empty()) {
234             inputEvents_.emplace_back(inputEvent);
235             return;
236         }
237         if (std::find(inputEvents_.begin(), inputEvents_.end(), inputEvent) == inputEvents_.end()) {
238             inputEvents_.emplace_back(inputEvent);
239         }
240     }
241 
RemoveInputEvent(const RefPtr<InputEvent> & inputEvent)242     void RemoveInputEvent(const RefPtr<InputEvent>& inputEvent)
243     {
244         inputEvents_.remove(inputEvent);
245     }
246 
RemoveAllTipsEvents()247     void RemoveAllTipsEvents()
248     {
249         inputEvents_.remove_if([](const RefPtr<InputEvent>& event) {
250             CHECK_NULL_RETURN(event, false);
251             return event->GetIstips();
252         });
253     }
254 
255     void OnCollectMouseEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl,
256         TouchTestResult& result);
257 
258     void OnCollectMouseEventForTips(
259         const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result);
260 
261     void OnCollectHoverEvent(
262         const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result);
263 
264     void OnCollectHoverEventForTips(
265         const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result);
266 
267     void OnCollectHoverEffect(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl,
268         TouchTestResult& result);
269 
270     void OnCollectAccessibilityHoverEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl,
271         TouchTestResult& result, const RefPtr<FrameNode>& host);
272 
273     void OnCollectPenHoverEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl,
274         TouchTestResult& result, const RefPtr<FrameNode>& host);
275 
276     void OnCollectPenHoverMoveEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl,
277         TouchTestResult& result, const RefPtr<FrameNode>& host);
278 
279     void OnCollectAxisEvent(
280         const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, AxisTestResult& onAxisResult);
281 
282 private:
283     WeakPtr<InputEventHub> inputEventHub_;
284     RefPtr<MouseEventTarget> mouseEventTarget_;
285     RefPtr<HoverEventTarget> hoverEventTarget_;
286     RefPtr<HoverEffectTarget> hoverEffectTarget_;
287     RefPtr<HoverEventTarget> accessibilityHoverEventTarget_;
288     RefPtr<HoverEventTarget> penHoverEventTarget_;
289     RefPtr<HoverEventTarget> penHoverMoveEventTarget_;
290     RefPtr<AxisEventTarget> axisEventTarget_;
291     std::list<RefPtr<InputEvent>> inputEvents_;
292     RefPtr<InputEvent> userCallback_;
293     RefPtr<InputEvent> userJSFrameNodeCallback_;
294 };
295 
296 } // namespace OHOS::Ace::NG
297 
298 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_H
299