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 
59 class MouseRawRecognizer : public virtual AceType {
60     DECLARE_ACE_TYPE(MouseRawRecognizer, AceType);
61 
62 public:
63     void HandleEvent(const MouseEvent& event);
64     void HandleHoverEvent(MouseState mouseState);
65 
SetOnMouse(const MouseCallback & onMouse)66     void SetOnMouse(const MouseCallback& onMouse)
67     {
68         onMouse_ = onMouse;
69     }
70 
SetOnMouseHover(const MouseHoverCallback & onMouseHover)71     void SetOnMouseHover(const MouseHoverCallback& onMouseHover)
72     {
73         onMouseHover_ = onMouseHover;
74     }
75 
SetOnHover(const OnHoverCallback & onHover)76     void SetOnHover(const OnHoverCallback& onHover)
77     {
78         onHover_ = onHover;
79     }
80 
SetOnMouseHoverExit(const MouseHoverCallback & onMouseHoverExit)81     void SetOnMouseHoverExit(const MouseHoverCallback& onMouseHoverExit)
82     {
83         onMouseHoverExit_ = onMouseHoverExit;
84     }
85 
86     // Coordinate offset is used to calculate the local location of the mouse point in the render node.
SetCoordinateOffset(const Offset & coordinateOffset)87     void SetCoordinateOffset(const Offset& coordinateOffset)
88     {
89         coordinateOffset_ = coordinateOffset;
90     }
91 
92 private:
93     MouseEventInfo CreateMouseEventInfo(const MouseEvent& event) const;
94 
95     MouseCallback onMouse_;
96     Offset coordinateOffset_;
97     MouseEvent lastEvent_;
98     MouseHoverCallback onMouseHover_;
99     MouseHoverCallback onMouseHoverExit_;
100     OnHoverCallback onHover_;
101 };
102 
103 using MouseRawResult = std::list<RefPtr<MouseRawRecognizer>>;
104 
105 } // namespace OHOS::Ace
106 
107 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_MOUSE_RAW_RECOGNIZER_H
108