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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_HUB_H 18 19 #include <list> 20 21 #include "base/memory/ace_type.h" 22 #include "core/components/common/layout/constants.h" 23 #include "core/components_ng/event/input_event.h" 24 #include "core/pipeline_ng/ui_task_scheduler.h" 25 26 namespace OHOS::Ace::NG { 27 28 class EventHub; 29 30 // The gesture event hub is mainly used to handle common gesture events. 31 class ACE_EXPORT InputEventHub : public virtual AceType { 32 DECLARE_ACE_TYPE(InputEventHub, AceType) 33 public: 34 explicit InputEventHub(const WeakPtr<EventHub>& eventHub); 35 ~InputEventHub() override = default; 36 37 // Set by user define, which will replace old one. SetMouseEvent(OnMouseEventFunc && onMouseEventFunc)38 void SetMouseEvent(OnMouseEventFunc&& onMouseEventFunc) 39 { 40 if (!mouseEventActuator_) { 41 mouseEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this)); 42 } 43 mouseEventActuator_->ReplaceInputEvent(std::move(onMouseEventFunc)); 44 } 45 AddOnMouseEvent(const RefPtr<InputEvent> & onMouseEvent)46 void AddOnMouseEvent(const RefPtr<InputEvent>& onMouseEvent) 47 { 48 if (!mouseEventActuator_) { 49 mouseEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this)); 50 } 51 mouseEventActuator_->AddInputEvent(onMouseEvent); 52 } 53 RemoveOnMouseEvent(const RefPtr<InputEvent> & onMouseEvent)54 void RemoveOnMouseEvent(const RefPtr<InputEvent>& onMouseEvent) 55 { 56 if (!mouseEventActuator_) { 57 return; 58 } 59 mouseEventActuator_->RemoveInputEvent(onMouseEvent); 60 } 61 SetHoverEffect(HoverEffectType type)62 void SetHoverEffect(HoverEffectType type) 63 { 64 if (!hoverEffectActuator_) { 65 hoverEffectActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this)); 66 } 67 hoverEffectType_ = type; 68 } GetHoverEffect()69 HoverEffectType GetHoverEffect() 70 { 71 return hoverEffectType_; 72 } 73 std::string GetHoverEffectStr() const; 74 SetHoverEffectAuto(HoverEffectType type)75 void SetHoverEffectAuto(HoverEffectType type) 76 { 77 if (!hoverEffectActuator_) { 78 hoverEffectActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this)); 79 } 80 hoverEffectAuto_ = type; 81 } 82 GetHoverEffectAuto()83 HoverEffectType GetHoverEffectAuto() 84 { 85 return hoverEffectAuto_; 86 } 87 88 // Set by user define, which will replace old one. SetHoverEvent(OnHoverEventFunc && onHoverEventFunc)89 void SetHoverEvent(OnHoverEventFunc&& onHoverEventFunc) 90 { 91 if (!hoverEventActuator_) { 92 hoverEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this)); 93 } 94 hoverEventActuator_->ReplaceInputEvent(std::move(onHoverEventFunc)); 95 } 96 AddOnHoverEvent(const RefPtr<InputEvent> & onHoverEvent)97 void AddOnHoverEvent(const RefPtr<InputEvent>& onHoverEvent) 98 { 99 if (!hoverEventActuator_) { 100 hoverEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this)); 101 } 102 hoverEventActuator_->AddInputEvent(onHoverEvent); 103 } 104 RemoveOnHoverEvent(const RefPtr<InputEvent> & onHoverEvent)105 void RemoveOnHoverEvent(const RefPtr<InputEvent>& onHoverEvent) 106 { 107 if (!hoverEventActuator_) { 108 return; 109 } 110 hoverEventActuator_->RemoveInputEvent(onHoverEvent); 111 } 112 AddOnAxisEvent(const RefPtr<InputEvent> & onAxisEvent)113 void AddOnAxisEvent(const RefPtr<InputEvent>& onAxisEvent) 114 { 115 if (!axisEventActuator_) { 116 axisEventActuator_ = MakeRefPtr<InputEventActuator>(WeakClaim(this)); 117 } 118 axisEventActuator_->AddInputEvent(onAxisEvent); 119 } 120 RemoveOnAxisEvent(const RefPtr<InputEvent> & onAxisEvent)121 void RemoveOnAxisEvent(const RefPtr<InputEvent>& onAxisEvent) 122 { 123 if (!axisEventActuator_) { 124 return; 125 } 126 axisEventActuator_->RemoveInputEvent(onAxisEvent); 127 } 128 129 // the return value means prevents event bubbling. 130 bool ProcessMouseTestHit(const OffsetF& coordinateOffset, TouchTestResult& result); 131 132 bool ProcessAxisTestHit(const OffsetF& coordinateOffset, AxisTestResult& onAxisResult); 133 134 RefPtr<FrameNode> GetFrameNode() const; 135 OnContextAttached()136 void OnContextAttached() {} 137 138 // register showMenu callback (always replace) 139 void BindContextMenu(OnMouseEventFunc&& showMenu); 140 141 private: 142 WeakPtr<EventHub> eventHub_; 143 RefPtr<InputEventActuator> mouseEventActuator_; 144 RefPtr<InputEventActuator> hoverEventActuator_; 145 RefPtr<InputEventActuator> hoverEffectActuator_; 146 RefPtr<InputEventActuator> axisEventActuator_; 147 148 RefPtr<InputEvent> showMenu_; 149 150 HoverEffectType hoverEffectType_ = HoverEffectType::UNKNOWN; 151 HoverEffectType hoverEffectAuto_ = HoverEffectType::UNKNOWN; 152 }; 153 154 } // namespace OHOS::Ace::NG 155 156 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_HUB_H