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 HasUserCallback()136 bool HasUserCallback() 137 { 138 return userCallback_ != nullptr; 139 } 140 ClearJSFrameNodeCallback()141 void ClearJSFrameNodeCallback() 142 { 143 if (userJSFrameNodeCallback_) { 144 userJSFrameNodeCallback_.Reset(); 145 } 146 } 147 ReplaceJSFrameNodeInputEvent(OnMouseEventFunc && callback)148 void ReplaceJSFrameNodeInputEvent(OnMouseEventFunc&& callback) 149 { 150 if (userJSFrameNodeCallback_) { 151 userJSFrameNodeCallback_.Reset(); 152 } 153 userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 154 } ReplaceJSFrameNodeInputEvent(OnHoverFunc && callback)155 void ReplaceJSFrameNodeInputEvent(OnHoverFunc&& callback) 156 { 157 if (userJSFrameNodeCallback_) { 158 userJSFrameNodeCallback_.Reset(); 159 } 160 userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 161 } 162 ReplaceJSFrameNodeInputEvent(OnHoverMoveFunc && callback)163 void ReplaceJSFrameNodeInputEvent(OnHoverMoveFunc&& callback) 164 { 165 if (userJSFrameNodeCallback_) { 166 userJSFrameNodeCallback_.Reset(); 167 } 168 userJSFrameNodeCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 169 } 170 ReplaceInputEvent(OnMouseEventFunc && callback)171 void ReplaceInputEvent(OnMouseEventFunc&& callback) 172 { 173 if (userCallback_) { 174 userCallback_.Reset(); 175 } 176 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 177 } ReplaceInputEvent(OnHoverFunc && callback)178 void ReplaceInputEvent(OnHoverFunc&& callback) 179 { 180 if (userCallback_) { 181 userCallback_.Reset(); 182 } 183 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 184 } 185 ReplaceInputEvent(OnHoverMoveFunc && callback)186 void ReplaceInputEvent(OnHoverMoveFunc&& callback) 187 { 188 if (userCallback_) { 189 userCallback_.Reset(); 190 } 191 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 192 } 193 ReplaceInputEvent(OnAxisEventFunc && callback)194 void ReplaceInputEvent(OnAxisEventFunc&& callback) 195 { 196 if (userCallback_) { 197 userCallback_.Reset(); 198 } 199 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 200 } 201 ReplaceInputEvent(OnAccessibilityHoverFunc && callback)202 void ReplaceInputEvent(OnAccessibilityHoverFunc&& callback) 203 { 204 if (userCallback_) { 205 userCallback_.Reset(); 206 } 207 userCallback_ = MakeRefPtr<InputEvent>(std::move(callback)); 208 } 209 AddInputEvent(const RefPtr<InputEvent> & inputEvent)210 void AddInputEvent(const RefPtr<InputEvent>& inputEvent) 211 { 212 if (inputEvents_.empty()) { 213 inputEvents_.emplace_back(inputEvent); 214 return; 215 } 216 if (std::find(inputEvents_.begin(), inputEvents_.end(), inputEvent) == inputEvents_.end()) { 217 inputEvents_.emplace_back(inputEvent); 218 } 219 } 220 RemoveInputEvent(const RefPtr<InputEvent> & inputEvent)221 void RemoveInputEvent(const RefPtr<InputEvent>& inputEvent) 222 { 223 inputEvents_.remove(inputEvent); 224 } 225 226 void OnCollectMouseEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 227 TouchTestResult& result); 228 229 void OnCollectHoverEvent( 230 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result); 231 232 void OnCollectHoverEffect(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 233 TouchTestResult& result); 234 235 void OnCollectAccessibilityHoverEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 236 TouchTestResult& result, const RefPtr<FrameNode>& host); 237 238 void OnCollectPenHoverEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 239 TouchTestResult& result, const RefPtr<FrameNode>& host); 240 241 void OnCollectPenHoverMoveEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, 242 TouchTestResult& result, const RefPtr<FrameNode>& host); 243 244 void OnCollectAxisEvent( 245 const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, AxisTestResult& onAxisResult); 246 247 private: 248 WeakPtr<InputEventHub> inputEventHub_; 249 RefPtr<MouseEventTarget> mouseEventTarget_; 250 RefPtr<HoverEventTarget> hoverEventTarget_; 251 RefPtr<HoverEffectTarget> hoverEffectTarget_; 252 RefPtr<HoverEventTarget> accessibilityHoverEventTarget_; 253 RefPtr<HoverEventTarget> penHoverEventTarget_; 254 RefPtr<HoverEventTarget> penHoverMoveEventTarget_; 255 RefPtr<AxisEventTarget> axisEventTarget_; 256 std::list<RefPtr<InputEvent>> inputEvents_; 257 RefPtr<InputEvent> userCallback_; 258 RefPtr<InputEvent> userJSFrameNodeCallback_; 259 }; 260 261 } // namespace OHOS::Ace::NG 262 263 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_H 264