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