1 /* 2 * Copyright (c) 2021 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_EVENT_MOUSE_RAW_RECOGNIZER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_MOUSE_RAW_RECOGNIZER_H 18 19 #include <functional> 20 21 #include "core/event/mouse_event.h" 22 23 namespace OHOS::Ace { 24 25 class MouseEventInfo : public BaseEventInfo { 26 DECLARE_RELATIONSHIP_OF_CLASSES(MouseEventInfo, BaseEventInfo); 27 28 public: MouseEventInfo(const std::string & type)29 explicit MouseEventInfo(const std::string& type) : BaseEventInfo(type) {} 30 ~MouseEventInfo() override = default; 31 SetMouseLocationInfo(const MouseEvent & event)32 void SetMouseLocationInfo(const MouseEvent& event) 33 { 34 localMouse_ = event; 35 } SetMouseGlobalInfo(const MouseEvent & event)36 void SetMouseGlobalInfo(const MouseEvent& event) 37 { 38 globalMouse_ = event; 39 } 40 GetGlobalMouse()41 const MouseEvent& GetGlobalMouse() const 42 { 43 return globalMouse_; 44 } GetLocalMouse()45 const MouseEvent& GetLocalMouse() const 46 { 47 return localMouse_; 48 } 49 50 private: 51 MouseEvent localMouse_; 52 MouseEvent globalMouse_; 53 }; 54 55 using MouseCallback = std::function<void(const MouseEventInfo&)>; 56 using MouseHoverCallback = std::function<void()>; 57 using OnHoverCallback = std::function<void(bool, HoverInfo& info)>; 58 using OnHoverMoveCallback = std::function<void(HoverInfo& info)>; 59 60 class MouseRawRecognizer : public virtual AceType { 61 DECLARE_ACE_TYPE(MouseRawRecognizer, AceType); 62 63 public: 64 void HandleEvent(const MouseEvent& event); 65 void HandleHoverEvent(MouseState mouseState); 66 SetOnMouse(const MouseCallback & onMouse)67 void SetOnMouse(const MouseCallback& onMouse) 68 { 69 onMouse_ = onMouse; 70 } 71 SetOnMouseHover(const MouseHoverCallback & onMouseHover)72 void SetOnMouseHover(const MouseHoverCallback& onMouseHover) 73 { 74 onMouseHover_ = onMouseHover; 75 } 76 SetOnHover(const OnHoverCallback & onHover)77 void SetOnHover(const OnHoverCallback& onHover) 78 { 79 onHover_ = onHover; 80 } 81 SetOnMouseHoverExit(const MouseHoverCallback & onMouseHoverExit)82 void SetOnMouseHoverExit(const MouseHoverCallback& onMouseHoverExit) 83 { 84 onMouseHoverExit_ = onMouseHoverExit; 85 } 86 87 // Coordinate offset is used to calculate the local location of the mouse point in the render node. SetCoordinateOffset(const Offset & coordinateOffset)88 void SetCoordinateOffset(const Offset& coordinateOffset) 89 { 90 coordinateOffset_ = coordinateOffset; 91 } 92 93 private: 94 MouseEventInfo CreateMouseEventInfo(const MouseEvent& event) const; 95 96 MouseCallback onMouse_; 97 Offset coordinateOffset_; 98 MouseEvent lastEvent_; 99 MouseHoverCallback onMouseHover_; 100 MouseHoverCallback onMouseHoverExit_; 101 OnHoverCallback onHover_; 102 }; 103 104 using MouseRawResult = std::list<RefPtr<MouseRawRecognizer>>; 105 106 } // namespace OHOS::Ace 107 108 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_MOUSE_RAW_RECOGNIZER_H 109