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