1 /* 2 * Copyright (c) 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_NG_PROPERTIES_GRID_PROPERTIES_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_GRID_PROPERTIES_H 18 19 #include <utility> 20 21 #include "property.h" 22 23 #include "base/geometry/ng/offset_t.h" 24 #include "base/json/json_util.h" 25 #include "base/memory/ace_type.h" 26 #include "base/utils/utils.h" 27 #include "core/components/common/layout/grid_column_info.h" 28 #include "core/components/common/layout/grid_layout_info.h" 29 #include "core/components_ng/property/calc_length.h" 30 31 namespace OHOS::Ace::NG { 32 33 constexpr uint32_t DEFAULT_GRID_SPAN = 1; 34 constexpr int32_t DEFAULT_GRID_OFFSET = 0; 35 struct GridTypedProperty { GridTypedPropertyGridTypedProperty36 GridTypedProperty(GridSizeType type, uint32_t span, int32_t offset) : type_(type), span_(span), offset_(offset) {} 37 38 bool operator==(const GridTypedProperty& other) const 39 { 40 if ((type_ != GridSizeType::UNDEFINED) && (other.type_ != GridSizeType::UNDEFINED) && (type_ != other.type_)) { 41 return false; 42 } 43 return ((span_ == other.span_) && (offset_ == other.offset_)); 44 } 45 46 GridSizeType type_ = GridSizeType::UNDEFINED; 47 int32_t span_ = DEFAULT_GRID_SPAN; 48 int32_t offset_ = DEFAULT_GRID_OFFSET; 49 }; 50 51 class ACE_EXPORT GridProperty : public AceType { 52 DECLARE_ACE_TYPE(GridProperty, AceType); 53 54 public: GridProperty()55 GridProperty() : typedPropertySet_ { { GridSizeType::UNDEFINED, DEFAULT_GRID_SPAN, DEFAULT_GRID_OFFSET } } {} 56 GridProperty(const GridProperty & other)57 GridProperty(const GridProperty& other) 58 { 59 *this = other; 60 } 61 62 ~GridProperty() override = default; 63 64 GridProperty(const GridProperty&& other) = delete; 65 66 GridProperty& operator=(const GridProperty&& other) = delete; 67 68 GridProperty& operator=(const GridProperty& other) 69 { 70 if (&other != this) { 71 typedPropertySet_ = other.typedPropertySet_; 72 } 73 container_ = other.container_; 74 gridInfo_ = other.gridInfo_; 75 return *this; 76 } 77 78 Dimension GetWidth(); 79 80 Dimension GetOffset(); 81 82 bool UpdateContainer(const RefPtr<Property>& container, const RefPtr<AceType>& host); 83 84 bool UpdateSpan(int32_t span, GridSizeType type = GridSizeType::UNDEFINED); 85 86 bool UpdateOffset(int32_t offset, GridSizeType type = GridSizeType::UNDEFINED); 87 GetTypedProperty(GridSizeType type)88 std::optional<GridTypedProperty> GetTypedProperty(GridSizeType type) const 89 { 90 for (const auto& item : typedPropertySet_) { 91 if (item.type_ == type) { 92 return item; 93 } 94 } 95 return std::nullopt; 96 } 97 HasContainer()98 bool HasContainer() 99 { 100 return container_; 101 } 102 103 OffsetF GetContainerPosition(); 104 105 void ToJsonValue(std::unique_ptr<JsonValue>& json) const; 106 107 private: 108 bool SetSpan(GridSizeType type, int32_t span); 109 110 bool SetOffset(GridSizeType type, int32_t offset); 111 112 std::vector<GridTypedProperty> typedPropertySet_; 113 RefPtr<Property> container_; 114 RefPtr<GridColumnInfo> gridInfo_; 115 }; 116 } // namespace OHOS::Ace::NG 117 118 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_GRID_PROPERTIES_H