1 /*
2  * Copyright (c) 2021-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_EVENT_ACE_EVENTS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENTS_H
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <string>
22 #include <optional>
23 
24 #include "base/geometry/dimension_offset.h"
25 #include "base/geometry/dimension_rect.h"
26 #include "base/memory/type_info_base.h"
27 #include "base/utils/type_definition.h"
28 
29 namespace OHOS::Ace {
30 
31 enum class KeyCode : int32_t;
32 
33 enum class SourceType : int32_t {
34     NONE = 0,
35     MOUSE = 1,
36     TOUCH = 2,
37     TOUCH_PAD = 3,
38     KEYBOARD = 4
39 };
40 
41 enum class SourceTool : int32_t {
42     UNKNOWN = 0,
43     FINGER = 1,
44     PEN = 2,
45     RUBBER = 3,
46     BRUSH = 4,
47     PENCIL = 5,
48     AIRBRUSH = 6,
49     MOUSE = 7,
50     LENS = 8,
51     TOUCHPAD = 9,
52     JOYSTICK = 10,
53 };
54 
55 struct EventTarget final {
56     std::string id;
57     std::string type;
58     DimensionRect area;
59     DimensionOffset origin;
60 };
61 
62 class BaseEventInfo : public virtual TypeInfoBase {
63     DECLARE_RELATIONSHIP_OF_CLASSES(BaseEventInfo, TypeInfoBase);
64 
65 public:
BaseEventInfo(const std::string & type)66     explicit BaseEventInfo(const std::string& type) : type_(type) {}
67     ~BaseEventInfo() override = default;
68 
GetType()69     const std::string& GetType() const
70     {
71         return type_;
72     }
SetType(const std::string & type)73     void SetType(const std::string& type)
74     {
75         type_ = type;
76     }
77 
GetTimeStamp()78     const TimeStamp& GetTimeStamp() const
79     {
80         return timeStamp_;
81     }
SetTimeStamp(const TimeStamp & timeStamp)82     BaseEventInfo& SetTimeStamp(const TimeStamp& timeStamp)
83     {
84         timeStamp_ = timeStamp;
85         return *this;
86     }
87 
GetTarget()88     const EventTarget& GetTarget() const
89     {
90         return target_;
91     }
GetTargetWithModify()92     EventTarget& GetTargetWithModify()
93     {
94         return target_;
95     }
96 
SetTarget(const EventTarget & target)97     BaseEventInfo& SetTarget(const EventTarget& target)
98     {
99         target_ = target;
100         return *this;
101     }
102 
GetDeviceId()103     int64_t GetDeviceId() const
104     {
105         return deviceId_;
106     }
SetDeviceId(int64_t deviceId)107     void SetDeviceId(int64_t deviceId)
108     {
109         deviceId_ = deviceId;
110     }
111 
GetTargetDisplayId()112     int32_t GetTargetDisplayId() const
113     {
114         return targetDisplayId_;
115     }
SetTargetDisplayId(int32_t targetDisplayId)116     void SetTargetDisplayId(int32_t targetDisplayId)
117     {
118         targetDisplayId_ = targetDisplayId;
119     }
120 
GetSourceDevice()121     SourceType GetSourceDevice() const
122     {
123         return deviceType_;
124     }
SetSourceDevice(SourceType deviceType)125     void SetSourceDevice(SourceType deviceType)
126     {
127         deviceType_ = deviceType;
128     }
129 
SetForce(float force)130     void SetForce(float force)
131     {
132         force_ = force;
133     }
GetForce()134     float GetForce() const
135     {
136         return force_;
137     }
138 
SetTiltX(float tiltX)139     void SetTiltX(float tiltX)
140     {
141         tiltX_ = tiltX;
142     }
GetTiltX()143     std::optional<float> GetTiltX() const
144     {
145         return tiltX_;
146     }
147 
SetTiltY(float tiltY)148     void SetTiltY(float tiltY)
149     {
150         tiltY_ = tiltY;
151     }
GetTiltY()152     std::optional<float> GetTiltY() const
153     {
154         return tiltY_;
155     }
156 
SetSourceTool(SourceTool tool)157     void SetSourceTool(SourceTool tool)
158     {
159         sourceTool_ = tool;
160     }
GetSourceTool()161     SourceTool GetSourceTool() const
162     {
163         return sourceTool_;
164     }
165 
IsStopPropagation()166     bool IsStopPropagation() const
167     {
168         return stopPropagation_;
169     }
SetStopPropagation(bool stopPropagation)170     void SetStopPropagation(bool stopPropagation)
171     {
172         stopPropagation_ = stopPropagation;
173     }
174 
IsPreventDefault()175     bool IsPreventDefault() const
176     {
177         return preventDefault_;
178     }
SetPreventDefault(bool preventDefault)179     void SetPreventDefault(bool preventDefault)
180     {
181         preventDefault_ = preventDefault;
182     }
183 
GetPatternName()184     const std::string& GetPatternName() const
185     {
186         return patternName_;
187     }
SetPatternName(const std::string & patternName)188     void SetPatternName(const std::string& patternName)
189     {
190         patternName_ = patternName;
191     }
192 
GetPressedKeyCodes()193     const std::vector<KeyCode>& GetPressedKeyCodes() const
194     {
195         return pressedKeyCodes_;
196     }
197 
SetPressedKeyCodes(const std::vector<KeyCode> & pressedKeyCodes)198     void SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes)
199     {
200         pressedKeyCodes_ = pressedKeyCodes;
201     }
202 
GetPostEventNodeId()203     int32_t GetPostEventNodeId() const
204     {
205         return postEventNodeId_;
206     }
207 
SetPostEventNodeId(int32_t postEventNodeId)208     void SetPostEventNodeId(int32_t postEventNodeId)
209     {
210         postEventNodeId_ = postEventNodeId;
211     }
212 
GetIsPostEventResult()213     bool GetIsPostEventResult() const
214     {
215         return isPostEventResult_;
216     }
217 
SetIsPostEventResult(bool isPostEventResult)218     void SetIsPostEventResult(bool isPostEventResult)
219     {
220         isPostEventResult_ = isPostEventResult;
221     }
222 
223 protected:
224     // Event type like onTouchDown, onClick and so on.
225     std::string type_;
226     // The origin event time stamp.
227     TimeStamp timeStamp_;
228     EventTarget target_;
229     // Will be used in drag.
230     SourceType deviceType_ = SourceType::NONE;
231     float force_ = 0.0f;
232     std::optional<float> tiltX_;
233     std::optional<float> tiltY_;
234     SourceTool sourceTool_ = SourceTool::UNKNOWN;
235     int64_t deviceId_ = 0;
236     // Will be used in drag.
237     int32_t targetDisplayId_ = 0;
238     bool stopPropagation_ = false;
239     bool preventDefault_ = false;
240     std::string patternName_;
241     std::vector<KeyCode> pressedKeyCodes_;
242     bool isPostEventResult_ = false;
243     int32_t postEventNodeId_ = -1;
244 };
245 
246 class PropagationEventInfo : public virtual TypeInfoBase {
247     DECLARE_RELATIONSHIP_OF_CLASSES(PropagationEventInfo, TypeInfoBase);
248 
249 public:
IsStopPropagation()250     bool IsStopPropagation() const
251     {
252         return stopPropagation_;
253     }
SetStopPropagation(bool stopPropagation)254     void SetStopPropagation(bool stopPropagation)
255     {
256         stopPropagation_ = stopPropagation;
257     }
258 
259 private:
260     bool stopPropagation_ = false;
261 };
262 
263 class EventToJSONStringAdapter : public virtual TypeInfoBase {
264     DECLARE_RELATIONSHIP_OF_CLASSES(EventToJSONStringAdapter, TypeInfoBase);
265 
266 public:
267     EventToJSONStringAdapter() = default;
268     ~EventToJSONStringAdapter() = default;
269 
270     virtual std::string ToJSONString() const = 0;
271 };
272 
273 } // namespace OHOS::Ace
274 
275 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENTS_H
276