• 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 #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             }
76         }
77         auto userEvent = actuator->userCallback_;
78         if (userEvent) {
79             (*userEvent)(info, hoverInfo);
80         }
81     };
82     hoverEventTarget_->SetCallback(onHoverCallback);
83     hoverEventTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
84     hoverEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
85     result.emplace_back(hoverEventTarget_);
86 }
87 
OnCollectHoverEffect(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)88 void InputEventActuator::OnCollectHoverEffect(
89     const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
90 {
91     auto inputEventHub = inputEventHub_.Upgrade();
92     CHECK_NULL_VOID(inputEventHub);
93     auto frameNode = inputEventHub->GetFrameNode();
94     CHECK_NULL_VOID(frameNode);
95 
96     hoverEffectTarget_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
97     hoverEffectTarget_->SetGetEventTargetImpl(getEventTargetImpl);
98     hoverEffectTarget_->SetHoverNode(AceType::WeakClaim(AceType::RawPtr(frameNode)));
99     result.emplace_back(hoverEffectTarget_);
100 }
101 
OnCollectAxisEvent(const OffsetF & coordinateOffset,const GetEventTargetImpl & getEventTargetImpl,AxisTestResult & onAxisResult)102 void InputEventActuator::OnCollectAxisEvent(
103     const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, AxisTestResult& onAxisResult)
104 {
105     auto inputEventHub = inputEventHub_.Upgrade();
106     CHECK_NULL_VOID(inputEventHub);
107     auto frameNode = inputEventHub->GetFrameNode();
108     CHECK_NULL_VOID(frameNode);
109 
110     if (inputEvents_.empty() && !userCallback_) {
111         return;
112     }
113 
114     auto onAxisCallback = [weak = WeakClaim(this)](AxisInfo& info) {
115         auto actuator = weak.Upgrade();
116         CHECK_NULL_VOID(actuator);
117         auto innerEvents = actuator->inputEvents_;
118         for (const auto& callback : innerEvents) {
119             if (callback) {
120                 (*callback)(info);
121             }
122         }
123         auto userEvent = actuator->userCallback_;
124         if (userEvent) {
125             (*userEvent)(info);
126         }
127     };
128     axisEventTarget_->SetOnAxisCallback(onAxisCallback);
129     axisEventTarget_->SetCoordinateOffset(coordinateOffset);
130     axisEventTarget_->SetGetEventTargetImpl(getEventTargetImpl);
131     onAxisResult.emplace_back(axisEventTarget_);
132 }
133 
134 } // namespace OHOS::Ace::NG