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 SourceType : int32_t { 32 NONE = 0, 33 MOUSE = 1, 34 TOUCH = 2, 35 TOUCH_PAD = 3, 36 KEYBOARD = 4 37 }; 38 39 enum class SourceTool : int32_t { 40 UNKNOWN = 0, 41 FINGER = 1, 42 PEN = 2, 43 RUBBER = 3, 44 BRUSH = 4, 45 PENCIL = 5, 46 AIRBRUSH = 6, 47 MOUSE = 7, 48 LENS = 8, 49 TOUCHPAD = 9, 50 }; 51 52 struct EventTarget final { 53 std::string id; 54 std::string type; 55 DimensionRect area; 56 DimensionOffset origin; 57 }; 58 59 class BaseEventInfo : public virtual TypeInfoBase { 60 DECLARE_RELATIONSHIP_OF_CLASSES(BaseEventInfo, TypeInfoBase); 61 62 public: BaseEventInfo(const std::string & type)63 explicit BaseEventInfo(const std::string& type) : type_(type) {} 64 ~BaseEventInfo() override = default; 65 GetType()66 const std::string& GetType() const 67 { 68 return type_; 69 } SetType(const std::string & type)70 void SetType(const std::string& type) 71 { 72 type_ = type; 73 } 74 GetTimeStamp()75 const TimeStamp& GetTimeStamp() const 76 { 77 return timeStamp_; 78 } SetTimeStamp(const TimeStamp & timeStamp)79 BaseEventInfo& SetTimeStamp(const TimeStamp& timeStamp) 80 { 81 timeStamp_ = timeStamp; 82 return *this; 83 } 84 GetTarget()85 const EventTarget& GetTarget() const 86 { 87 return target_; 88 } GetTargetWithModify()89 EventTarget& GetTargetWithModify() 90 { 91 return target_; 92 } 93 SetTarget(const EventTarget & target)94 BaseEventInfo& SetTarget(const EventTarget& target) 95 { 96 target_ = target; 97 return *this; 98 } 99 GetDeviceId()100 int64_t GetDeviceId() const 101 { 102 return deviceId_; 103 } SetDeviceId(int64_t deviceId)104 void SetDeviceId(int64_t deviceId) 105 { 106 deviceId_ = deviceId; 107 } 108 GetTargetDisplayId()109 int32_t GetTargetDisplayId() const 110 { 111 return targetDisplayId_; 112 } SetTargetDisplayId(int32_t targetDisplayId)113 void SetTargetDisplayId(int32_t targetDisplayId) 114 { 115 targetDisplayId_ = targetDisplayId; 116 } 117 GetSourceDevice()118 SourceType GetSourceDevice() const 119 { 120 return deviceType_; 121 } SetSourceDevice(SourceType deviceType)122 void SetSourceDevice(SourceType deviceType) 123 { 124 deviceType_ = deviceType; 125 } 126 SetForce(float force)127 void SetForce(float force) 128 { 129 force_ = force; 130 } GetForce()131 float GetForce() const 132 { 133 return force_; 134 } 135 SetTiltX(float tiltX)136 void SetTiltX(float tiltX) 137 { 138 tiltX_ = tiltX; 139 } GetTiltX()140 std::optional<float> GetTiltX() const 141 { 142 return tiltX_; 143 } 144 SetTiltY(float tiltY)145 void SetTiltY(float tiltY) 146 { 147 tiltY_ = tiltY; 148 } GetTiltY()149 std::optional<float> GetTiltY() const 150 { 151 return tiltY_; 152 } 153 SetSourceTool(SourceTool tool)154 void SetSourceTool(SourceTool tool) 155 { 156 sourceTool_ = tool; 157 } GetSourceTool()158 SourceTool GetSourceTool() const 159 { 160 return sourceTool_; 161 } 162 IsStopPropagation()163 bool IsStopPropagation() const 164 { 165 return stopPropagation_; 166 } SetStopPropagation(bool stopPropagation)167 void SetStopPropagation(bool stopPropagation) 168 { 169 stopPropagation_ = stopPropagation; 170 } 171 172 protected: 173 // Event type like onTouchDown, onClick and so on. 174 std::string type_; 175 // The origin event time stamp. 176 TimeStamp timeStamp_; 177 EventTarget target_; 178 SourceType deviceType_ = SourceType::NONE; 179 float force_ = 0.0f; 180 std::optional<float> tiltX_; 181 std::optional<float> tiltY_; 182 SourceTool sourceTool_ = SourceTool::UNKNOWN; 183 int64_t deviceId_ = 0; 184 int32_t targetDisplayId_ = 0; 185 bool stopPropagation_ = false; 186 }; 187 188 class PropagationEventInfo : public virtual TypeInfoBase { 189 DECLARE_RELATIONSHIP_OF_CLASSES(PropagationEventInfo, TypeInfoBase); 190 191 public: IsStopPropagation()192 bool IsStopPropagation() const 193 { 194 return stopPropagation_; 195 } SetStopPropagation(bool stopPropagation)196 void SetStopPropagation(bool stopPropagation) 197 { 198 stopPropagation_ = stopPropagation; 199 } 200 201 private: 202 bool stopPropagation_ = false; 203 }; 204 205 class EventToJSONStringAdapter : public virtual TypeInfoBase { 206 DECLARE_RELATIONSHIP_OF_CLASSES(EventToJSONStringAdapter, TypeInfoBase); 207 208 public: 209 EventToJSONStringAdapter() = default; 210 ~EventToJSONStringAdapter() = default; 211 212 virtual std::string ToJSONString() const = 0; 213 }; 214 215 } // namespace OHOS::Ace 216 217 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENTS_H 218