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_hub.h"
17
18 #include "core/pipeline_ng/pipeline_context.h"
19
20 namespace OHOS::Ace::NG {
21
InputEventHub(const WeakPtr<EventHub> & eventHub)22 InputEventHub::InputEventHub(const WeakPtr<EventHub>& eventHub) : eventHub_(eventHub) {}
23
GetFrameNode() const24 RefPtr<FrameNode> InputEventHub::GetFrameNode() const
25 {
26 auto eventHub = eventHub_.Upgrade();
27 return eventHub ? eventHub->GetFrameNode() : nullptr;
28 }
29
ProcessMouseTestHit(const OffsetF & coordinateOffset,TouchTestResult & result)30 bool InputEventHub::ProcessMouseTestHit(const OffsetF& coordinateOffset, TouchTestResult& result)
31 {
32 auto eventHub = eventHub_.Upgrade();
33 auto getEventTargetImpl = eventHub ? eventHub->CreateGetEventTargetImpl() : nullptr;
34
35 if (mouseEventActuator_) {
36 mouseEventActuator_->OnCollectMouseEvent(coordinateOffset, getEventTargetImpl, result);
37 }
38 if (hoverEventActuator_) {
39 hoverEventActuator_->OnCollectHoverEvent(coordinateOffset, getEventTargetImpl, result);
40 }
41 if (hoverEffectActuator_) {
42 hoverEffectActuator_->OnCollectHoverEffect(coordinateOffset, getEventTargetImpl, result);
43 }
44 auto host = GetFrameNode();
45 CHECK_NULL_RETURN(host, false);
46 if (accessibilityHoverEventActuator_) {
47 accessibilityHoverEventActuator_->OnCollectAccessibilityHoverEvent(
48 coordinateOffset, getEventTargetImpl, result, host);
49 }
50 return false;
51 }
52
ProcessPenHoverTestHit(const OffsetF & coordinateOffset,TouchTestResult & result)53 bool InputEventHub::ProcessPenHoverTestHit(const OffsetF& coordinateOffset, TouchTestResult& result)
54 {
55 auto eventHub = eventHub_.Upgrade();
56 auto getEventTargetImpl = eventHub ? eventHub->CreateGetEventTargetImpl() : nullptr;
57 auto host = GetFrameNode();
58 CHECK_NULL_RETURN(host, false);
59 if (hoverEventActuator_) {
60 hoverEventActuator_->OnCollectPenHoverEvent(coordinateOffset, getEventTargetImpl, result, host);
61 }
62
63 if (hoverMoveEventActuator_) {
64 hoverMoveEventActuator_->OnCollectPenHoverMoveEvent(coordinateOffset, getEventTargetImpl, result, host);
65 }
66
67 return false;
68 }
69
ProcessAxisTestHit(const OffsetF & coordinateOffset,AxisTestResult & onAxisResult)70 bool InputEventHub::ProcessAxisTestHit(const OffsetF& coordinateOffset, AxisTestResult& onAxisResult)
71 {
72 auto eventHub = eventHub_.Upgrade();
73 auto getEventTargetImpl = eventHub ? eventHub->CreateGetEventTargetImpl() : nullptr;
74
75 if (axisEventActuator_) {
76 axisEventActuator_->OnCollectAxisEvent(coordinateOffset, getEventTargetImpl, onAxisResult);
77 }
78 return false;
79 }
80
81 // register showMenu callback (always replace)
BindContextMenu(OnMouseEventFunc && showMenu)82 void InputEventHub::BindContextMenu(OnMouseEventFunc&& showMenu)
83 {
84 if (showMenu_) {
85 RemoveOnMouseEvent(showMenu_);
86 }
87 showMenu_ = MakeRefPtr<InputEvent>(std::move(showMenu));
88 AddOnMouseEvent(showMenu_);
89 }
90
GetHoverEffectStr() const91 std::string InputEventHub::GetHoverEffectStr() const
92 {
93 switch (hoverEffectType_) {
94 case HoverEffectType::AUTO:
95 return "HoverEffect.Auto";
96 case HoverEffectType::SCALE:
97 return "HoverEffect.Scale";
98 case HoverEffectType::BOARD:
99 return "HoverEffect.Highlight";
100 case HoverEffectType::NONE:
101 return "HoverEffect.None";
102 default:
103 return "HoverEffect.Auto";
104 }
105 }
106
SetHoverEffect(HoverEffectType type)107 void InputEventHub::SetHoverEffect(HoverEffectType type)
108 {
109 if (!hoverEffectActuator_) {
110 hoverEffectActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this));
111 }
112 auto frameNode = GetFrameNode();
113 if (frameNode && hoverEffectType_ != type) {
114 frameNode->AnimateHoverEffect(false);
115 }
116 hoverEffectType_ = type;
117 }
118
119 } // namespace OHOS::Ace::NG