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_ON_MOUSE_EVENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_ON_MOUSE_EVENT_H 18 19 #include "core/event/mouse_event.h" 20 21 namespace OHOS::Ace::NG { 22 23 class InputEventHub; 24 class FrameNode; 25 26 class InputEvent : public virtual AceType { DECLARE_ACE_TYPE(InputEvent,AceType)27 DECLARE_ACE_TYPE(InputEvent, AceType) 28 public: 29 explicit InputEvent(OnMouseEventFunc&& callback) : onMouseCallback_(std::move(callback)) {} 30 InputEvent(OnHoverEventFunc && callback)31 explicit InputEvent(OnHoverEventFunc&& callback) : onHoverCallback_(std::move(callback)) {} 32 InputEvent(OnHoverFunc && callback)33 explicit InputEvent(OnHoverFunc&& callback) : onHoverEventCallback_(std::move(callback)) {} 34 InputEvent(OnAxisEventFunc && callback)35 explicit InputEvent(OnAxisEventFunc&& callback) : onAxisCallback_(std::move(callback)) {} 36 37 ~InputEvent() override = default; 38 GetOnMouseEventFunc()39 const OnMouseEventFunc& GetOnMouseEventFunc() const 40 { 41 return onMouseCallback_; 42 } 43 GetOnHoverEventFunc()44 const OnHoverEventFunc& GetOnHoverEventFunc() const 45 { 46 return onHoverCallback_; 47 } 48 GetOnHoverFunc()49 const OnHoverFunc& GetOnHoverFunc() const 50 { 51 return onHoverEventCallback_; 52 } 53 GetOnAxisEventFunc()54 const OnAxisEventFunc& GetOnAxisEventFunc() const 55 { 56 return onAxisCallback_; 57 } 58 operator()59 void operator()(MouseInfo& info) const 60 { 61 if (onMouseCallback_) { 62 onMouseCallback_(info); 63 } 64 } 65 operator()66 void operator()(bool state, HoverInfo& info) const 67 { 68 if (onHoverEventCallback_) { 69 onHoverEventCallback_(state, info); 70 } 71 } operator()72 void operator()(bool state) const 73 { 74 if (onHoverCallback_) { 75 onHoverCallback_(state); 76 } 77 } 78 operator()79 void operator()(AxisInfo& info) const 80 { 81 if (onAxisCallback_) { 82 onAxisCallback_(info); 83 } 84 } 85 86 private: 87 OnMouseEventFunc onMouseCallback_; 88 OnHoverEventFunc onHoverCallback_; 89 OnHoverFunc onHoverEventCallback_; 90 OnAxisEventFunc onAxisCallback_; 91 }; 92 93 class ACE_EXPORT InputEventActuator : public virtual AceType { 94 DECLARE_ACE_TYPE(InputEventActuator, AceType) 95 public: 96 explicit InputEventActuator(const WeakPtr<InputEventHub>& inputEventHub); 97 ~InputEventActuator() override = default; 98 ClearUserCallback()99 void ClearUserCallback() 100 { 101 if (userCallback_) { 102 userCallback_.Reset(); 103 } 104 } 105 ReplaceInputEvent(OnMouseEventFunc && callback)106 void ReplaceInputEvent(OnMouseEventFunc&& callback) 107 { 108 if (userCallback_) { 109 userCallback_.Reset(); 110 } 111 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 112 } ReplaceInputEvent(OnHoverFunc && callback)113 void ReplaceInputEvent(OnHoverFunc&& callback) 114 { 115 if (userCallback_) { 116 userCallback_.Reset(); 117 } 118 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 119 } 120 ReplaceInputEvent(OnAxisEventFunc && callback)121 void ReplaceInputEvent(OnAxisEventFunc&& callback) 122 { 123 if (userCallback_) { 124 userCallback_.Reset(); 125 } 126 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 127 } 128 AddInputEvent(const RefPtr<InputEvent> & inputEvent)129 void AddInputEvent(const RefPtr<InputEvent>& inputEvent) 130 { 131 if (inputEvents_.empty()) { 132 inputEvents_.emplace_back(inputEvent); 133 return; 134 } 135 if (std::find(inputEvents_.begin(), inputEvents_.end(), inputEvent) == inputEvents_.end()) { 136 inputEvents_.emplace_back(inputEvent); 137 } 138 } 139 RemoveInputEvent(const RefPtr<InputEvent> & inputEvent)140 void RemoveInputEvent(const RefPtr<InputEvent>& inputEvent) 141 { 142 inputEvents_.remove(inputEvent); 143 } 144 145 void OnCollectMouseEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 146 TouchTestResult& result); 147 148 void OnCollectHoverEvent( 149 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result); 150 151 void OnCollectHoverEffect(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 152 TouchTestResult& result); 153 154 void OnCollectAxisEvent( 155 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, AxisTestResult& onAxisResult); 156 157 private: 158 WeakPtr<InputEventHub> inputEventHub_; 159 RefPtr<MouseEventTarget> mouseEventTarget_; 160 RefPtr<HoverEventTarget> hoverEventTarget_; 161 RefPtr<HoverEffectTarget> hoverEffectTarget_; 162 RefPtr<AxisEventTarget> axisEventTarget_; 163 std::list<RefPtr<InputEvent>> inputEvents_; 164 RefPtr<InputEvent> userCallback_; 165 }; 166 167 } // namespace OHOS::Ace::NG 168 169 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_H 170