• 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_INPUT_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_HUB_H
18 
19 #include <list>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/components_ng/event/input_event.h"
24 #include "core/pipeline_ng/ui_task_scheduler.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 class EventHub;
29 
30 // The gesture event hub is mainly used to handle common gesture events.
31 class ACE_EXPORT InputEventHub : public virtual AceType {
32     DECLARE_ACE_TYPE(InputEventHub, AceType);
33 public:
34     explicit InputEventHub(const WeakPtr<EventHub>& eventHub);
35     ~InputEventHub() override = default;
36 
37     // Set by user define, which will replace old one.
SetMouseEvent(OnMouseEventFunc && onMouseEventFunc)38     void SetMouseEvent(OnMouseEventFunc&& onMouseEventFunc)
39     {
40         if (!mouseEventActuator_) {
41             mouseEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
42         }
43         mouseEventActuator_->ReplaceInputEvent(std::move(onMouseEventFunc));
44     }
SetJSFrameNodeOnMouseEvent(OnMouseEventFunc && onMouseEventFunc)45     void SetJSFrameNodeOnMouseEvent(OnMouseEventFunc&& onMouseEventFunc)
46     {
47         if (!mouseEventActuator_) {
48             mouseEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
49         }
50         mouseEventActuator_->ReplaceJSFrameNodeInputEvent(std::move(onMouseEventFunc));
51     }
52 
AddOnMouseEvent(const RefPtr<InputEvent> & onMouseEvent)53     void AddOnMouseEvent(const RefPtr<InputEvent>& onMouseEvent)
54     {
55         if (!mouseEventActuator_) {
56             mouseEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
57         }
58         mouseEventActuator_->AddInputEvent(onMouseEvent);
59     }
60 
RemoveOnMouseEvent(const RefPtr<InputEvent> & onMouseEvent)61     void RemoveOnMouseEvent(const RefPtr<InputEvent>& onMouseEvent)
62     {
63         if (!mouseEventActuator_) {
64             return;
65         }
66         mouseEventActuator_->RemoveInputEvent(onMouseEvent);
67     }
68 
RemoveAllTipsMouseEvents()69     void RemoveAllTipsMouseEvents()
70     {
71         if (!mouseEventActuator_) {
72             return;
73         }
74         mouseEventActuator_->RemoveAllTipsEvents();
75     }
76 
77     void SetHoverEffect(HoverEffectType type);
78 
GetHoverEffect()79     HoverEffectType GetHoverEffect()
80     {
81         return hoverEffectType_;
82     }
83     std::string GetHoverEffectStr() const;
84 
SetHoverEffectAuto(HoverEffectType type)85     void SetHoverEffectAuto(HoverEffectType type)
86     {
87         if (!hoverEffectActuator_) {
88             hoverEffectActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
89         }
90         hoverEffectAuto_ = type;
91     }
92 
GetHoverEffectAuto()93     HoverEffectType GetHoverEffectAuto()
94     {
95         return hoverEffectAuto_;
96     }
97 
98     // Set by user define, which will replace old one.
SetHoverEvent(OnHoverFunc && onHoverEventFunc)99     void SetHoverEvent(OnHoverFunc&& onHoverEventFunc)
100     {
101         if (!hoverEventActuator_) {
102             hoverEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
103         }
104         hoverEventActuator_->ReplaceInputEvent(std::move(onHoverEventFunc));
105     }
106 
SetHoverMoveEvent(OnHoverMoveFunc && onHoverMoveEventFunc)107     void SetHoverMoveEvent(OnHoverMoveFunc&& onHoverMoveEventFunc)
108     {
109         if (!hoverMoveEventActuator_) {
110             hoverMoveEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
111         }
112         hoverMoveEventActuator_->ReplaceInputEvent(std::move(onHoverMoveEventFunc));
113     }
114 
SetAccessibilityHoverEvent(OnAccessibilityHoverFunc && onAccessibilityHoverEventFunc)115     void SetAccessibilityHoverEvent(OnAccessibilityHoverFunc&& onAccessibilityHoverEventFunc)
116     {
117         if (!accessibilityHoverEventActuator_) {
118             accessibilityHoverEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
119         }
120         accessibilityHoverEventActuator_->ReplaceInputEvent(std::move(onAccessibilityHoverEventFunc));
121     }
122 
HasAccessibilityHoverEvent()123     bool HasAccessibilityHoverEvent()
124     {
125         if (accessibilityHoverEventActuator_) {
126             return accessibilityHoverEventActuator_->HasUserCallback();
127         }
128         return false;
129     }
130 
SetJSFrameNodeOnHoverEvent(OnHoverFunc && onHoverEventFunc)131     void SetJSFrameNodeOnHoverEvent(OnHoverFunc&& onHoverEventFunc)
132     {
133         if (!hoverEventActuator_) {
134             hoverEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
135         }
136         hoverEventActuator_->ReplaceJSFrameNodeInputEvent(std::move(onHoverEventFunc));
137     }
138 
SetJSFrameNodeOnHoverMoveEvent(OnHoverMoveFunc && onHoverMoveEventFunc)139     void SetJSFrameNodeOnHoverMoveEvent(OnHoverMoveFunc&& onHoverMoveEventFunc)
140     {
141         if (!hoverMoveEventActuator_) {
142             hoverMoveEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
143         }
144         hoverMoveEventActuator_->ReplaceJSFrameNodeInputEvent(std::move(onHoverMoveEventFunc));
145     }
146 
AddOnHoverEvent(const RefPtr<InputEvent> & onHoverEvent)147     void AddOnHoverEvent(const RefPtr<InputEvent>& onHoverEvent)
148     {
149         if (!hoverEventActuator_) {
150             hoverEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
151         }
152         hoverEventActuator_->AddInputEvent(onHoverEvent);
153     }
154 
RemoveOnHoverEvent(const RefPtr<InputEvent> & onHoverEvent)155     void RemoveOnHoverEvent(const RefPtr<InputEvent>& onHoverEvent)
156     {
157         if (!hoverEventActuator_) {
158             return;
159         }
160         hoverEventActuator_->RemoveInputEvent(onHoverEvent);
161     }
162 
RemoveAllTipsHoverEvents()163     void RemoveAllTipsHoverEvents()
164     {
165         if (!hoverEventActuator_) {
166             return;
167         }
168         hoverEventActuator_->RemoveAllTipsEvents();
169     }
170 
AddOnHoverMoveEvent(const RefPtr<InputEvent> & onHoverMoveEvent)171     void AddOnHoverMoveEvent(const RefPtr<InputEvent>& onHoverMoveEvent)
172     {
173         if (!hoverMoveEventActuator_) {
174             hoverMoveEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
175         }
176         hoverMoveEventActuator_->AddInputEvent(onHoverMoveEvent);
177     }
178 
RemoveOnHoverMoveEvent(const RefPtr<InputEvent> & onHoverMoveEvent)179     void RemoveOnHoverMoveEvent(const RefPtr<InputEvent>& onHoverMoveEvent)
180     {
181         if (!hoverMoveEventActuator_) {
182             return;
183         }
184         hoverMoveEventActuator_->RemoveInputEvent(onHoverMoveEvent);
185     }
186 
SetAxisEvent(OnAxisEventFunc && onAxisEventFunc)187     void SetAxisEvent(OnAxisEventFunc&& onAxisEventFunc)
188     {
189         if (!axisEventActuator_) {
190             axisEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
191         }
192         axisEventActuator_->ReplaceInputEvent(std::move(onAxisEventFunc));
193     }
194 
AddOnAxisEvent(const RefPtr<InputEvent> & onAxisEvent)195     void AddOnAxisEvent(const RefPtr<InputEvent>& onAxisEvent)
196     {
197         if (!axisEventActuator_) {
198             axisEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
199         }
200         axisEventActuator_->AddInputEvent(onAxisEvent);
201     }
202 
RemoveOnAxisEvent(const RefPtr<InputEvent> & onAxisEvent)203     void RemoveOnAxisEvent(const RefPtr<InputEvent>& onAxisEvent)
204     {
205         if (!axisEventActuator_) {
206             return;
207         }
208         axisEventActuator_->RemoveInputEvent(onAxisEvent);
209     }
210 
211     // the return value means prevents event bubbling.
212     bool ProcessMouseTestHit(const OffsetF& coordinateOffset, TouchTestResult& result);
213     bool ProcessTipsMouseTestHit(const OffsetF& coordinateOffset, TouchTestResult& result);
214 
215     bool ProcessPenHoverTestHit(const OffsetF& coordinateOffset, TouchTestResult& result);
216 
217     bool ProcessAxisTestHit(const OffsetF& coordinateOffset, AxisTestResult& onAxisResult);
218 
219     RefPtr<FrameNode> GetFrameNode() const;
220 
OnContextAttached()221     void OnContextAttached() {}
222 
223     // register showMenu callback (always replace)
224     void BindContextMenu(OnMouseEventFunc&& showMenu);
225 
ClearUserOnHover()226     void ClearUserOnHover()
227     {
228         if (hoverEventActuator_) {
229             hoverEventActuator_->ClearUserCallback();
230         }
231     }
232 
ClearUserOnHoverMove()233     void ClearUserOnHoverMove()
234     {
235         if (hoverMoveEventActuator_) {
236             hoverMoveEventActuator_->ClearUserCallback();
237         }
238     }
239 
ClearUserOnAccessibilityHover()240     void ClearUserOnAccessibilityHover()
241     {
242         if (accessibilityHoverEventActuator_) {
243             accessibilityHoverEventActuator_->ClearUserCallback();
244         }
245     }
246 
ClearJSFrameNodeOnHover()247     void ClearJSFrameNodeOnHover()
248     {
249         if (hoverEventActuator_) {
250             hoverEventActuator_->ClearJSFrameNodeCallback();
251         }
252     }
253 
ClearJSFrameNodeOnHoverMove()254     void ClearJSFrameNodeOnHoverMove()
255     {
256         if (hoverMoveEventActuator_) {
257             hoverMoveEventActuator_->ClearJSFrameNodeCallback();
258         }
259     }
260 
ClearUserOnMouse()261     void ClearUserOnMouse()
262     {
263         if (mouseEventActuator_) {
264             mouseEventActuator_->ClearUserCallback();
265         }
266     }
267 
ClearJSFrameNodeOnMouse()268     void ClearJSFrameNodeOnMouse()
269     {
270         if (mouseEventActuator_) {
271             mouseEventActuator_->ClearJSFrameNodeCallback();
272         }
273     }
274 
ClearUserOnAxisEvent()275     void ClearUserOnAxisEvent()
276     {
277         if (axisEventActuator_) {
278             axisEventActuator_->ClearUserCallback();
279         }
280     }
281 
282 private:
283     WeakPtr<EventHub> eventHub_;
284     RefPtr<InputEventActuator> mouseEventActuator_;
285     RefPtr<InputEventActuator> hoverEventActuator_;
286     RefPtr<InputEventActuator> hoverMoveEventActuator_;
287     RefPtr<InputEventActuator> hoverEffectActuator_;
288     RefPtr<InputEventActuator> axisEventActuator_;
289     RefPtr<InputEventActuator> accessibilityHoverEventActuator_;
290 
291     RefPtr<InputEvent> showMenu_;
292 
293     HoverEffectType hoverEffectType_ = HoverEffectType::UNKNOWN;
294     HoverEffectType hoverEffectAuto_ = HoverEffectType::UNKNOWN;
295 };
296 
297 } // namespace OHOS::Ace::NG
298 
299 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_HUB_H
300