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(OnHoverMoveFunc && callback)35 explicit InputEvent(OnHoverMoveFunc&& callback) : onHoverMoveCallback_(std::move(callback)) {} 36 InputEvent(OnAxisEventFunc && callback)37 explicit InputEvent(OnAxisEventFunc&& callback) : onAxisCallback_(std::move(callback)) {} 38 InputEvent(OnAccessibilityHoverFunc && callback)39 explicit InputEvent(OnAccessibilityHoverFunc&& callback) : onAccessibilityHoverFunc_(std::move(callback)) {} 40 41 ~InputEvent() override = default; 42 GetOnMouseEventFunc()43 const OnMouseEventFunc& GetOnMouseEventFunc() const 44 { 45 return onMouseCallback_; 46 } 47 GetOnHoverEventFunc()48 const OnHoverEventFunc& GetOnHoverEventFunc() const 49 { 50 return onHoverCallback_; 51 } 52 GetOnHoverMoveEventFunc()53 const OnHoverMoveFunc& GetOnHoverMoveEventFunc() const 54 { 55 return onHoverMoveCallback_; 56 } 57 GetOnHoverFunc()58 const OnHoverFunc& GetOnHoverFunc() const 59 { 60 return onHoverEventCallback_; 61 } 62 GetOnAxisEventFunc()63 const OnAxisEventFunc& GetOnAxisEventFunc() const 64 { 65 return onAxisCallback_; 66 } 67 GetOnAccessibilityHoverFunc()68 const OnAccessibilityHoverFunc& GetOnAccessibilityHoverFunc() const 69 { 70 return onAccessibilityHoverFunc_; 71 } 72 operator()73 void operator()(MouseInfo& info) const 74 { 75 if (onMouseCallback_) { 76 onMouseCallback_(info); 77 } 78 } 79 operator()80 void operator()(bool state, HoverInfo& info) const 81 { 82 if (onHoverEventCallback_) { 83 onHoverEventCallback_(state, info); 84 } 85 } operator()86 void operator()(bool state) const 87 { 88 if (onHoverCallback_) { 89 onHoverCallback_(state); 90 } 91 } 92 operator()93 void operator()(AxisInfo& info) const 94 { 95 if (onAxisCallback_) { 96 onAxisCallback_(info); 97 } 98 } 99 operator()100 void operator()(HoverInfo& info) const 101 { 102 if (onHoverMoveCallback_) { 103 onHoverMoveCallback_(info); 104 } 105 } 106 operator()107 void operator()(bool state, AccessibilityHoverInfo& info) const 108 { 109 if (onAccessibilityHoverFunc_) { 110 onAccessibilityHoverFunc_(state, info); 111 } 112 } 113 114 private: 115 OnMouseEventFunc onMouseCallback_; 116 OnHoverEventFunc onHoverCallback_; 117 OnHoverFunc onHoverEventCallback_; 118 OnHoverMoveFunc onHoverMoveCallback_; 119 OnAxisEventFunc onAxisCallback_; 120 OnAccessibilityHoverFunc onAccessibilityHoverFunc_; 121 }; 122 123 class ACE_EXPORT InputEventActuator : public virtual AceType { 124 DECLARE_ACE_TYPE(InputEventActuator, AceType) 125 public: 126 explicit InputEventActuator(const WeakPtr<InputEventHub>& inputEventHub); 127 ~InputEventActuator() override = default; 128 ClearUserCallback()129 void ClearUserCallback() 130 { 131 if (userCallback_) { 132 userCallback_.Reset(); 133 } 134 } 135 ClearJSFrameNodeCallback()136 void ClearJSFrameNodeCallback() 137 { 138 if (userJSFrameNodeCallback_) { 139 userJSFrameNodeCallback_.Reset(); 140 } 141 } 142 ReplaceJSFrameNodeInputEvent(OnMouseEventFunc && callback)143 void ReplaceJSFrameNodeInputEvent(OnMouseEventFunc&& callback) 144 { 145 if (userJSFrameNodeCallback_) { 146 userJSFrameNodeCallback_.Reset(); 147 } 148 userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 149 } ReplaceJSFrameNodeInputEvent(OnHoverFunc && callback)150 void ReplaceJSFrameNodeInputEvent(OnHoverFunc&& callback) 151 { 152 if (userJSFrameNodeCallback_) { 153 userJSFrameNodeCallback_.Reset(); 154 } 155 userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 156 } 157 ReplaceJSFrameNodeInputEvent(OnHoverMoveFunc && callback)158 void ReplaceJSFrameNodeInputEvent(OnHoverMoveFunc&& callback) 159 { 160 if (userJSFrameNodeCallback_) { 161 userJSFrameNodeCallback_.Reset(); 162 } 163 userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 164 } 165 ReplaceInputEvent(OnMouseEventFunc && callback)166 void ReplaceInputEvent(OnMouseEventFunc&& callback) 167 { 168 if (userCallback_) { 169 userCallback_.Reset(); 170 } 171 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 172 } ReplaceInputEvent(OnHoverFunc && callback)173 void ReplaceInputEvent(OnHoverFunc&& callback) 174 { 175 if (userCallback_) { 176 userCallback_.Reset(); 177 } 178 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 179 } 180 ReplaceInputEvent(OnHoverMoveFunc && callback)181 void ReplaceInputEvent(OnHoverMoveFunc&& callback) 182 { 183 if (userCallback_) { 184 userCallback_.Reset(); 185 } 186 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 187 } 188 ReplaceInputEvent(OnAxisEventFunc && callback)189 void ReplaceInputEvent(OnAxisEventFunc&& callback) 190 { 191 if (userCallback_) { 192 userCallback_.Reset(); 193 } 194 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 195 } 196 ReplaceInputEvent(OnAccessibilityHoverFunc && callback)197 void ReplaceInputEvent(OnAccessibilityHoverFunc&& callback) 198 { 199 if (userCallback_) { 200 userCallback_.Reset(); 201 } 202 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 203 } 204 AddInputEvent(const RefPtr<InputEvent> & inputEvent)205 void AddInputEvent(const RefPtr<InputEvent>& inputEvent) 206 { 207 if (inputEvents_.empty()) { 208 inputEvents_.emplace_back(inputEvent); 209 return; 210 } 211 if (std::find(inputEvents_.begin(), inputEvents_.end(), inputEvent) == inputEvents_.end()) { 212 inputEvents_.emplace_back(inputEvent); 213 } 214 } 215 RemoveInputEvent(const RefPtr<InputEvent> & inputEvent)216 void RemoveInputEvent(const RefPtr<InputEvent>& inputEvent) 217 { 218 inputEvents_.remove(inputEvent); 219 } 220 221 void OnCollectMouseEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 222 TouchTestResult& result); 223 224 void OnCollectHoverEvent( 225 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result); 226 227 void OnCollectHoverEffect(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 228 TouchTestResult& result); 229 230 void OnCollectAccessibilityHoverEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 231 TouchTestResult& result, const RefPtr<FrameNode>& host); 232 233 void OnCollectPenHoverEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 234 TouchTestResult& result, const RefPtr<FrameNode>& host); 235 236 void OnCollectPenHoverMoveEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 237 TouchTestResult& result, const RefPtr<FrameNode>& host); 238 239 void OnCollectAxisEvent( 240 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, AxisTestResult& onAxisResult); 241 242 private: 243 WeakPtr<InputEventHub> inputEventHub_; 244 RefPtr<MouseEventTarget> mouseEventTarget_; 245 RefPtr<HoverEventTarget> hoverEventTarget_; 246 RefPtr<HoverEffectTarget> hoverEffectTarget_; 247 RefPtr<HoverEventTarget> accessibilityHoverEventTarget_; 248 RefPtr<HoverEventTarget> penHoverEventTarget_; 249 RefPtr<HoverEventTarget> penHoverMoveEventTarget_; 250 RefPtr<AxisEventTarget> axisEventTarget_; 251 std::list<RefPtr<InputEvent>> inputEvents_; 252 RefPtr<InputEvent> userCallback_; 253 RefPtr<InputEvent> userJSFrameNodeCallback_; 254 }; 255 256 } // namespace OHOS::Ace::NG 257 258 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_H 259