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 #include "core/components_ng/event/input_event.h"
17 
18 #include "core/components_ng/base/frame_node.h"
19 
20 namespace OHOS::Ace::NG {
21 
InputEventActuator(const WeakPtr<InputEventHub> & inputEventHub)22 InputEventActuator::InputEventActuator(const WeakPtr<InputEventHub>& inputEventHub) : inputEventHub_(inputEventHub)
23 {
24     auto refInputEventHub = inputEventHub_.Upgrade();
25     CHECK_NULL_VOID(refInputEventHub);
26     auto frameNode = refInputEventHub->GetFrameNode();
27     CHECK_NULL_VOID(frameNode);
28     mouseEventTarget_ = MakeRefPtr<MouseEventTarget>(frameNode->GetTag(), frameNode->GetId());
29     hoverEventTarget_ = MakeRefPtr<HoverEventTarget>(frameNode->GetTag(), frameNode->GetId());
30     hoverEffectTarget_ = MakeRefPtr<HoverEffectTarget>(frameNode->GetTag(), frameNode->GetId());
31     accessibilityHoverEventTarget_ = MakeRefPtr<HoverEventTarget>(frameNode->GetTag(), frameNode->GetId());
32     penHoverEventTarget_ = MakeRefPtr<HoverEventTarget>(frameNode->GetTag(), frameNode->GetId());
33     axisEventTarget_ = MakeRefPtr<AxisEventTarget>(frameNode->GetTag());
34 }
35 
OnCollectMouseEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)36 void InputEventActuator::OnCollectMouseEvent(
37     const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
38 {
39     if (inputEvents_.empty() && !userCallback_ && !userJSFrameNodeCallback_) {
40         return;
41     }
42 
43     auto onMouseCallback = [weak = WeakClaim(this)](MouseInfo& info) {
44         auto actuator = weak.Upgrade();
45         CHECK_NULL_VOID(actuator);
46         auto innerEvents = actuator->inputEvents_;
47         for (const auto& callback : innerEvents) {
48             if (callback) {
49                 (*callback)(info);
50             }
51         }
52         auto userEvent = actuator->userCallback_;
53         if (userEvent) {
54             (*userEvent)(info);
55         }
56         auto userJSFrameNodeCallback = actuator->userJSFrameNodeCallback_;
57         if (userJSFrameNodeCallback) {
58             (*userJSFrameNodeCallback)(info);
59         }
60     };
61     mouseEventTarget_->SetCallback(onMouseCallback);
62     mouseEventTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
63     mouseEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
64     result.emplace_back(mouseEventTarget_);
65 }
66 
OnCollectHoverEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)67 void InputEventActuator::OnCollectHoverEvent(
68     const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
69 {
70     if (inputEvents_.empty() && !userCallback_ && !userJSFrameNodeCallback_) {
71         return;
72     }
73 
74     auto onHoverCallback = [weak = WeakClaim(this)](bool info, HoverInfo& hoverInfo) {
75         auto actuator = weak.Upgrade();
76         CHECK_NULL_VOID(actuator);
77         auto innerEvents = actuator->inputEvents_;
78         for (const auto& callback : innerEvents) {
79             if (callback) {
80                 (*callback)(info);
81                 (*callback)(info, hoverInfo);
82             }
83         }
84         auto userEvent = actuator->userCallback_;
85         if (userEvent) {
86             (*userEvent)(info, hoverInfo);
87         }
88         auto userJSFrameNodeCallback = actuator->userJSFrameNodeCallback_;
89         if (userJSFrameNodeCallback) {
90             (*userJSFrameNodeCallback)(info, hoverInfo);
91         }
92     };
93     hoverEventTarget_->SetCallback(onHoverCallback);
94     hoverEventTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
95     hoverEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
96     result.emplace_back(hoverEventTarget_);
97 }
98 
OnCollectPenHoverEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)99 void InputEventActuator::OnCollectPenHoverEvent(
100     const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
101 {
102     if (inputEvents_.empty() && !userCallback_ && !userJSFrameNodeCallback_) {
103         return;
104     }
105 
106     auto penHoverCallback = [weakClaim = WeakClaim(this)](bool isHover, HoverInfo& penHoverInfo) {
107         auto actuator = weakClaim.Upgrade();
108         CHECK_NULL_VOID(actuator);
109         auto inputEvents = actuator->inputEvents_;
110         for (const auto& inputCallback : inputEvents) {
111             if (inputCallback) {
112                 (*inputCallback)(isHover);
113                 (*inputCallback)(isHover, penHoverInfo);
114             }
115         }
116         auto userCallback = actuator->userCallback_;
117         if (userCallback) {
118             (*userCallback)(isHover, penHoverInfo);
119         }
120         auto userJSCallback = actuator->userJSFrameNodeCallback_;
121         if (userJSCallback) {
122             (*userJSCallback)(isHover, penHoverInfo);
123         }
124     };
125     penHoverEventTarget_->SetPenHoverCallback(penHoverCallback);
126     penHoverEventTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
127     penHoverEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
128     result.emplace_back(penHoverEventTarget_);
129 }
130 
OnCollectHoverEffect(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)131 void InputEventActuator::OnCollectHoverEffect(
132     const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
133 {
134     auto inputEventHub = inputEventHub_.Upgrade();
135     CHECK_NULL_VOID(inputEventHub);
136     auto frameNode = inputEventHub->GetFrameNode();
137     CHECK_NULL_VOID(frameNode);
138 
139     hoverEffectTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
140     hoverEffectTarget_->SetGetEventTargetImpl(getEventTargetImpl);
141     hoverEffectTarget_->SetHoverNode(AceType::WeakClaim(AceType::RawPtr(frameNode)));
142     result.emplace_back(hoverEffectTarget_);
143 }
144 
OnCollectAccessibilityHoverEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result,const RefPtr<FrameNode> & host)145 void InputEventActuator::OnCollectAccessibilityHoverEvent(const OffsetF& coordinateOffset,
146     const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result, const RefPtr<FrameNode>& host)
147 {
148     if (inputEvents_.empty() && !userCallback_ && !userJSFrameNodeCallback_) {
149         return;
150     }
151 
152     auto onAccessibilityHoverCallback = [weak = WeakClaim(this)](
153                                             bool info, AccessibilityHoverInfo& accessibilityHoverInfo) {
154         auto actuator = weak.Upgrade();
155         CHECK_NULL_VOID(actuator);
156         auto userEvent = actuator->userCallback_;
157         if (userEvent) {
158             (*userEvent)(info, accessibilityHoverInfo);
159         }
160     };
161     accessibilityHoverEventTarget_->AttachFrameNode(host);
162     accessibilityHoverEventTarget_->SetAccessibilityHoverCallback(onAccessibilityHoverCallback);
163     accessibilityHoverEventTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
164     accessibilityHoverEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
165     result.emplace_back(accessibilityHoverEventTarget_);
166 }
167 
OnCollectAxisEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,AxisTestResult & onAxisResult)168 void InputEventActuator::OnCollectAxisEvent(
169     const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, AxisTestResult& onAxisResult)
170 {
171     auto inputEventHub = inputEventHub_.Upgrade();
172     CHECK_NULL_VOID(inputEventHub);
173     auto frameNode = inputEventHub->GetFrameNode();
174     CHECK_NULL_VOID(frameNode);
175 
176     if (inputEvents_.empty() && !userCallback_) {
177         return;
178     }
179 
180     auto onAxisCallback = [weak = WeakClaim(this)](AxisInfo& info) {
181         auto actuator = weak.Upgrade();
182         CHECK_NULL_VOID(actuator);
183         auto innerEvents = actuator->inputEvents_;
184         for (const auto& callback : innerEvents) {
185             if (callback) {
186                 (*callback)(info);
187             }
188         }
189         auto userEvent = actuator->userCallback_;
190         if (userEvent) {
191             (*userEvent)(info);
192         }
193     };
194     axisEventTarget_->SetOnAxisCallback(onAxisCallback);
195     axisEventTarget_->SetCoordinateOffset(coordinateOffset);
196     axisEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
197     onAxisResult.emplace_back(axisEventTarget_);
198 }
199 
200 } // namespace OHOS::Ace::NG