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 axisEventTarget_ = MakeRefPtr<AxisEventTarget>(frameNode->GetTag());
32 }
33
OnCollectMouseEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)34 void InputEventActuator::OnCollectMouseEvent(
35 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
36 {
37 if (inputEvents_.empty() && !userCallback_) {
38 return;
39 }
40
41 auto onMouseCallback = [weak = WeakClaim(this)](MouseInfo& info) {
42 auto actuator = weak.Upgrade();
43 CHECK_NULL_VOID(actuator);
44 auto innerEvents = actuator->inputEvents_;
45 for (const auto& callback : innerEvents) {
46 if (callback) {
47 (*callback)(info);
48 }
49 }
50 auto userEvent = actuator->userCallback_;
51 if (userEvent) {
52 (*userEvent)(info);
53 }
54 };
55 mouseEventTarget_->SetCallback(onMouseCallback);
56 mouseEventTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
57 mouseEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
58 result.emplace_back(mouseEventTarget_);
59 }
60
OnCollectHoverEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)61 void InputEventActuator::OnCollectHoverEvent(
62 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
63 {
64 if (inputEvents_.empty() && !userCallback_) {
65 return;
66 }
67
68 auto onHoverCallback = [weak = WeakClaim(this)](bool info, HoverInfo& hoverInfo) {
69 auto actuator = weak.Upgrade();
70 CHECK_NULL_VOID(actuator);
71 auto innerEvents = actuator->inputEvents_;
72 for (const auto& callback : innerEvents) {
73 if (callback) {
74 (*callback)(info);
75 (*callback)(info, hoverInfo);
76 }
77 }
78 auto userEvent = actuator->userCallback_;
79 if (userEvent) {
80 (*userEvent)(info, hoverInfo);
81 }
82 };
83 hoverEventTarget_->SetCallback(onHoverCallback);
84 hoverEventTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
85 hoverEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
86 result.emplace_back(hoverEventTarget_);
87 }
88
OnCollectHoverEffect(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)89 void InputEventActuator::OnCollectHoverEffect(
90 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
91 {
92 auto inputEventHub = inputEventHub_.Upgrade();
93 CHECK_NULL_VOID(inputEventHub);
94 auto frameNode = inputEventHub->GetFrameNode();
95 CHECK_NULL_VOID(frameNode);
96
97 hoverEffectTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
98 hoverEffectTarget_->SetGetEventTargetImpl(getEventTargetImpl);
99 hoverEffectTarget_->SetHoverNode(AceType::WeakClaim(AceType::RawPtr(frameNode)));
100 result.emplace_back(hoverEffectTarget_);
101 }
102
OnCollectAxisEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,AxisTestResult & onAxisResult)103 void InputEventActuator::OnCollectAxisEvent(
104 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, AxisTestResult& onAxisResult)
105 {
106 auto inputEventHub = inputEventHub_.Upgrade();
107 CHECK_NULL_VOID(inputEventHub);
108 auto frameNode = inputEventHub->GetFrameNode();
109 CHECK_NULL_VOID(frameNode);
110
111 if (inputEvents_.empty() && !userCallback_) {
112 return;
113 }
114
115 auto onAxisCallback = [weak = WeakClaim(this)](AxisInfo& info) {
116 auto actuator = weak.Upgrade();
117 CHECK_NULL_VOID(actuator);
118 auto innerEvents = actuator->inputEvents_;
119 for (const auto& callback : innerEvents) {
120 if (callback) {
121 (*callback)(info);
122 }
123 }
124 auto userEvent = actuator->userCallback_;
125 if (userEvent) {
126 (*userEvent)(info);
127 }
128 };
129 axisEventTarget_->SetOnAxisCallback(onAxisCallback);
130 axisEventTarget_->SetCoordinateOffset(coordinateOffset);
131 axisEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
132 onAxisResult.emplace_back(axisEventTarget_);
133 }
134
135 } // namespace OHOS::Ace::NG