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