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