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_COMPONENTS_BOX_BOX_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_BOX_COMPONENT_H 18 19 #include "core/components/box/box_base_component.h" 20 #include "core/components/box/drag_drop_event.h" 21 #include "core/components/common/properties/animatable_color.h" 22 #include "core/components/common/properties/color.h" 23 #include "core/components/common/properties/decoration.h" 24 #include "core/gestures/gesture_info.h" 25 #include "core/gestures/gesture_group.h" 26 #include "core/gestures/raw_recognizer.h" 27 28 namespace OHOS::Ace { 29 30 using OnHoverCallback = std::function<void(bool)>; 31 using OnMouseCallback = std::function<void(MouseInfo& info)>; 32 33 enum class BoxStateAttribute { 34 ASPECT_RATIO, 35 BORDER, 36 COLOR, 37 BORDER_COLOR, 38 BORDER_RADIUS, 39 BORDER_STYLE, 40 BORDER_WIDTH, 41 GRADIENT, 42 HEIGHT, 43 WIDTH, 44 }; 45 46 // A component can box others components. 47 class ACE_EXPORT BoxComponent : public BoxBaseComponent { 48 DECLARE_ACE_TYPE(BoxComponent, BoxBaseComponent); 49 50 public: 51 RefPtr<Element> CreateElement() override; 52 RefPtr<RenderNode> CreateRenderNode() override; 53 GetBackDecoration()54 RefPtr<Decoration> GetBackDecoration() const 55 { 56 return backDecoration_; 57 } 58 GetFrontDecoration()59 RefPtr<Decoration> GetFrontDecoration() const 60 { 61 return frontDecoration_; 62 } 63 GetColor()64 const Color& GetColor() const 65 { 66 if (backDecoration_) { 67 return backDecoration_->GetBackgroundColor(); 68 } 69 return Color::TRANSPARENT; 70 } 71 GetDecorationUpdateFlag()72 bool GetDecorationUpdateFlag() const 73 { 74 return decorationUpdateFlag_; 75 } 76 GetMouseAnimationType()77 HoverAnimationType GetMouseAnimationType() const 78 { 79 return animationType_; 80 } 81 SetBackDecoration(const RefPtr<Decoration> & decoration)82 void SetBackDecoration(const RefPtr<Decoration>& decoration) 83 { 84 backDecoration_ = decoration; 85 SetDecorationUpdateFlag(true); 86 } 87 SetFrontDecoration(const RefPtr<Decoration> & decoration)88 void SetFrontDecoration(const RefPtr<Decoration>& decoration) 89 { 90 frontDecoration_ = decoration; 91 SetDecorationUpdateFlag(true); 92 } 93 94 void SetColor(const Color& color, const AnimationOption& option = AnimationOption()) 95 { 96 if (!backDecoration_) { 97 backDecoration_ = AceType::MakeRefPtr<Decoration>(); 98 } 99 backDecoration_->SetBackgroundColor(color, option); 100 } 101 SetColor(const AnimatableColor & color)102 void SetColor(const AnimatableColor& color) 103 { 104 if (!backDecoration_) { 105 backDecoration_ = AceType::MakeRefPtr<Decoration>(); 106 } 107 backDecoration_->SetBackgroundColor(color); 108 } 109 SetDecorationUpdateFlag(bool flag)110 void SetDecorationUpdateFlag(bool flag) 111 { 112 decorationUpdateFlag_ = flag; 113 } 114 SetMouseAnimationType(HoverAnimationType animationType)115 void SetMouseAnimationType(HoverAnimationType animationType) 116 { 117 animationType_ = animationType; 118 } 119 SetInspectorDirection(TextDirection direction)120 void SetInspectorDirection(TextDirection direction) 121 { 122 inspectorDirection_ = direction; 123 } 124 GetInspectorDirection()125 TextDirection GetInspectorDirection() const 126 { 127 return inspectorDirection_; 128 } 129 SetOnHoverId(const OnHoverCallback & onHoverId)130 void SetOnHoverId(const OnHoverCallback& onHoverId) 131 { 132 onHoverId_ = onHoverId; 133 } 134 GetOnHoverId()135 OnHoverCallback GetOnHoverId() const 136 { 137 return onHoverId_; 138 } 139 SetOnMouseId(const OnMouseCallback & onMouseId)140 void SetOnMouseId(const OnMouseCallback& onMouseId) 141 { 142 onMouseId_ = onMouseId; 143 } 144 GetOnMouseId()145 OnMouseCallback GetOnMouseId() const 146 { 147 return onMouseId_; 148 } 149 SetOnTouchMoveId(const OnTouchEventCallback & onTouchMoveId)150 void SetOnTouchMoveId(const OnTouchEventCallback& onTouchMoveId) 151 { 152 onTouchMoveId_ = onTouchMoveId; 153 } 154 GetOnTouchMoveId()155 const OnTouchEventCallback& GetOnTouchMoveId() const 156 { 157 return onTouchMoveId_; 158 } 159 SetOnTouchUpId(const OnTouchEventCallback & onTouchUpId)160 void SetOnTouchUpId(const OnTouchEventCallback& onTouchUpId) 161 { 162 onTouchUpId_ = onTouchUpId; 163 } 164 GetOnTouchUpId()165 const OnTouchEventCallback& GetOnTouchUpId() const 166 { 167 return onTouchUpId_; 168 } 169 SetOnTouchDownId(const OnTouchEventCallback & onTouchDownId)170 void SetOnTouchDownId(const OnTouchEventCallback& onTouchDownId) 171 { 172 onTouchDownId_ = onTouchDownId; 173 } 174 GetOnTouchDownId()175 const OnTouchEventCallback& GetOnTouchDownId() const 176 { 177 return onTouchDownId_; 178 } 179 GetOnClick()180 RefPtr<Gesture> GetOnClick() const 181 { 182 return onClickId_; 183 } 184 SetOnClick(const RefPtr<Gesture> & onClickId)185 void SetOnClick(const RefPtr<Gesture>& onClickId) 186 { 187 onClickId_ = onClickId; 188 } 189 AddGesture(GesturePriority priority,RefPtr<Gesture> gesture)190 void AddGesture(GesturePriority priority, RefPtr<Gesture> gesture) 191 { 192 gestures_[static_cast<int32_t>(priority)] = gesture; 193 } 194 GetGestures()195 const std::array<RefPtr<Gesture>, 3>& GetGestures() const 196 { 197 return gestures_; 198 } 199 GetOnDomDragEnter()200 const EventMarker& GetOnDomDragEnter() const 201 { 202 return onDomDragEnterId_; 203 } 204 SetOnDomDragEnter(const EventMarker & value)205 void SetOnDomDragEnter(const EventMarker& value) 206 { 207 onDomDragEnterId_ = value; 208 } 209 GetOnDomDragOver()210 const EventMarker& GetOnDomDragOver() const 211 { 212 return onDomDragOverId_; 213 } 214 SetOnDomDragOver(const EventMarker & value)215 void SetOnDomDragOver(const EventMarker& value) 216 { 217 onDomDragOverId_ = value; 218 } 219 GetOnDomDragLeave()220 const EventMarker& GetOnDomDragLeave() const 221 { 222 return onDomDragLeaveId_; 223 } 224 SetOnDomDragLeave(const EventMarker & value)225 void SetOnDomDragLeave(const EventMarker& value) 226 { 227 onDomDragLeaveId_ = value; 228 } 229 GetOnDomDragDrop()230 const EventMarker& GetOnDomDragDrop() const 231 { 232 return onDomDragDropId_; 233 } 234 SetOnDomDragDrop(const EventMarker & value)235 void SetOnDomDragDrop(const EventMarker& value) 236 { 237 onDomDragDropId_ = value; 238 } 239 SetGeometryTransitionId(const std::string & id)240 void SetGeometryTransitionId(const std::string& id) 241 { 242 geometryTransitionId_ = id; 243 } 244 GetGeometryTransitionId()245 std::string GetGeometryTransitionId() const 246 { 247 return geometryTransitionId_; 248 } 249 GetStateAttributes()250 RefPtr<StateAttributes<BoxStateAttribute>> GetStateAttributes() 251 { 252 if (stateAttributeList_ == nullptr) { 253 stateAttributeList_ = MakeRefPtr<StateAttributes<BoxStateAttribute>>(); 254 } 255 return stateAttributeList_; 256 } 257 HasStateAttributes()258 bool HasStateAttributes() 259 { 260 return stateAttributeList_ != nullptr; 261 } 262 GetOnDragStartId()263 const OnDragFunc& GetOnDragStartId() const 264 { 265 return onDragStartId_; 266 } 267 SetOnDragStartId(const OnDragFunc & onDragStartId)268 void SetOnDragStartId(const OnDragFunc& onDragStartId) 269 { 270 onDragStartId_ = onDragStartId; 271 } 272 GetOnDragEnterId()273 const OnDropFunc& GetOnDragEnterId() const 274 { 275 return onDragEnterId_; 276 } 277 SetOnDragEnterId(const OnDropFunc & onDragEnterId)278 void SetOnDragEnterId(const OnDropFunc& onDragEnterId) 279 { 280 onDragEnterId_ = onDragEnterId; 281 } 282 GetOnDragMoveId()283 const OnDropFunc& GetOnDragMoveId() const 284 { 285 return onDragMoveId_; 286 } 287 SetOnDragMoveId(const OnDropFunc & onDragMoveId)288 void SetOnDragMoveId(const OnDropFunc& onDragMoveId) 289 { 290 onDragMoveId_ = onDragMoveId; 291 } 292 GetOnDragLeaveId()293 const OnDropFunc& GetOnDragLeaveId() const 294 { 295 return onDragLeaveId_; 296 } 297 SetOnDragLeaveId(const OnDropFunc & onDragLeaveId)298 void SetOnDragLeaveId(const OnDropFunc& onDragLeaveId) 299 { 300 onDragLeaveId_ = onDragLeaveId; 301 } 302 GetOnDropId()303 const OnDropFunc& GetOnDropId() const 304 { 305 return onDropId_; 306 } 307 SetOnDropId(const OnDropFunc & onDropId)308 void SetOnDropId(const OnDropFunc& onDropId) 309 { 310 onDropId_ = onDropId; 311 } 312 GetRemoteMessageEvent()313 const EventMarker& GetRemoteMessageEvent() const 314 { 315 return remoteMessageId_; 316 } 317 SetRemoteMessageEvent(const EventMarker & eventId)318 void SetRemoteMessageEvent(const EventMarker& eventId) 319 { 320 remoteMessageId_ = eventId; 321 } 322 GetOnLongPress()323 RefPtr<Gesture> GetOnLongPress() const 324 { 325 return onLongPressId_; 326 } 327 SetOnLongPress(const RefPtr<Gesture> & onLongPressId)328 void SetOnLongPress(const RefPtr<Gesture>& onLongPressId) 329 { 330 onLongPressId_ = onLongPressId; 331 } 332 HasBackgroundColor()333 bool HasBackgroundColor() const 334 { 335 return hasBackgroundColor_; 336 } 337 SetHasBackgroundColor(bool hasBackgroundColor)338 void SetHasBackgroundColor(bool hasBackgroundColor) 339 { 340 hasBackgroundColor_ = hasBackgroundColor; 341 } 342 SetEnableDebugBoundary(bool enableDebugBoundary)343 void SetEnableDebugBoundary(bool enableDebugBoundary) 344 { 345 enableDebugBoundary_ = enableDebugBoundary; 346 } 347 GetEnableDebugBoundary()348 bool GetEnableDebugBoundary() 349 { 350 return enableDebugBoundary_; 351 } 352 SetEnableDragStart(bool enableDragStart)353 void SetEnableDragStart(bool enableDragStart) 354 { 355 enableDragStart_ = enableDragStart; 356 } 357 GetEnableDragStart()358 bool GetEnableDragStart() const 359 { 360 return enableDragStart_; 361 } 362 NeedMaterial(bool needMaterial)363 void NeedMaterial(bool needMaterial) 364 { 365 needMaterial_ = needMaterial; 366 } 367 GetNeedMaterial()368 bool GetNeedMaterial() const 369 { 370 return needMaterial_; 371 } 372 private: 373 RefPtr<Decoration> backDecoration_; 374 RefPtr<Decoration> frontDecoration_; 375 bool decorationUpdateFlag_ = false; 376 HoverAnimationType animationType_ = HoverAnimationType::UNKNOWN; 377 OnHoverCallback onHoverId_; 378 OnMouseCallback onMouseId_; 379 OnTouchEventCallback onTouchMoveId_; 380 OnTouchEventCallback onTouchUpId_; 381 OnTouchEventCallback onTouchDownId_; 382 RefPtr<Gesture> onClickId_; 383 RefPtr<Gesture> onLongPressId_; 384 std::array<RefPtr<Gesture>, 3> gestures_; 385 EventMarker onDomDragEnterId_; 386 EventMarker onDomDragOverId_; 387 EventMarker onDomDragLeaveId_; 388 EventMarker onDomDragDropId_; 389 std::string geometryTransitionId_; 390 TextDirection inspectorDirection_ { TextDirection::LTR }; 391 RefPtr<StateAttributes<BoxStateAttribute>> stateAttributeList_ = nullptr; 392 EventMarker remoteMessageId_; 393 bool hasBackgroundColor_; 394 bool enableDebugBoundary_ = false; 395 bool enableDragStart_ = true; 396 OnDragFunc onDragStartId_; 397 OnDropFunc onDragEnterId_; 398 OnDropFunc onDragMoveId_; 399 OnDropFunc onDragLeaveId_; 400 OnDropFunc onDropId_; 401 bool needMaterial_ = false; 402 }; 403 404 } // namespace OHOS::Ace 405 406 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_BOX_COMPONENT_H 407