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