1 /*
2  * Copyright (c) 2021 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_BASE_LAYOUT_GRID_CONTAINER_INFO_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_GRID_CONTAINER_INFO_H
18 
19 #include <string>
20 
21 #include "base/geometry/dimension.h"
22 #include "core/components/common/layout/grid_layout_info.h"
23 
24 namespace OHOS::Ace {
25 
26 constexpr int32_t UNDEFINED_INT = -1;
27 constexpr Dimension UNDEFINED_DIMENSION(-1.0, DimensionUnit::VP);
28 
29 class GridContainerInfo : public GridLayoutInfo {
30     DECLARE_ACE_TYPE(GridContainerInfo, GridLayoutInfo);
31 
32 public:
33     class Builder {
34     public:
Builder()35         Builder()
36         {
37             containerInfo_ = AceType::Claim(new GridContainerInfo());
38         }
SetColumns(int32_t columns)39         void SetColumns(int32_t columns)
40         {
41             if (columns > 0) {
42                 containerInfo_->columns_ = columns;
43             }
44         }
SetGutterWidth(const Dimension & gutterWidth)45         void SetGutterWidth(const Dimension& gutterWidth)
46         {
47             if (GreatOrEqual(gutterWidth.Value(), 0.0)) {
48                 containerInfo_->gutterWidth_ = gutterWidth;
49             }
50         }
SetMarginLeft(const Dimension & marginLeft)51         void SetMarginLeft(const Dimension& marginLeft)
52         {
53             if (GreatOrEqual(marginLeft.Value(), 0.0)) {
54                 containerInfo_->marginLeft_ = marginLeft;
55             }
56         }
SetMarginRight(const Dimension & marginRight)57         void SetMarginRight(const Dimension& marginRight)
58         {
59             if (GreatOrEqual(marginRight.Value(), 0.0)) {
60                 containerInfo_->marginRight_ = marginRight;
61             }
62         }
SetPaddingLeft(const Dimension & paddingLeft)63         void SetPaddingLeft(const Dimension& paddingLeft)
64         {
65             if (GreatOrEqual(paddingLeft.Value(), 0.0)) {
66                 containerInfo_->paddingLeft_ = paddingLeft;
67             }
68         }
SetPaddingRight(const Dimension & paddingRight)69         void SetPaddingRight(const Dimension& paddingRight)
70         {
71             if (GreatOrEqual(paddingRight.Value(), 0.0)) {
72                 containerInfo_->paddingRight_ = paddingRight;
73             }
74         }
SetSizeType(const GridSizeType & sizeType)75         void SetSizeType(const GridSizeType& sizeType)
76         {
77             containerInfo_->sizeType_ = sizeType;
78         }
SetColumnType(const GridColumnType & columnType)79         void SetColumnType(const GridColumnType& columnType)
80         {
81             containerInfo_->columnType_ = columnType;
82         }
SetGridTemplateType(const GridTemplateType & templateType)83         void SetGridTemplateType(const GridTemplateType& templateType)
84         {
85             containerInfo_->templateType_ = templateType;
86         }
Build()87         const RefPtr<GridContainerInfo>& Build() const
88         {
89             return containerInfo_;
90         }
91 
92     private:
93         RefPtr<GridContainerInfo> containerInfo_;
94     };
95 
96     DEFINE_COPY_CONSTRUCTOR_AND_COPY_OPERATOR_AND_COMPARE_OPERATOR_WITH_PROPERTIES(GridContainerInfo,
97         (templateType_)(currentSizeType_)(sizeType_)(columns_)(gutterWidth_)(marginLeft_)(marginRight_)\
98         (paddingLeft_)(paddingRight_)(containerWidth_)(columnWidth_)(columnType_));
99 
100     ~GridContainerInfo() override = default;
101 
GetColumnWidth()102     double GetColumnWidth() const
103     {
104         return columnWidth_;
105     }
106 
GetColumnType()107     GridColumnType GetColumnType() const
108     {
109         return columnType_;
110     }
111     GridSizeType GetSizeType() const;
112     int32_t GetColumns() const;
113     Dimension ACE_EXPORT GetGutterWidth() const;
114     Dimension GetMarginLeft() const;
115     Dimension GetMarginRight() const;
SetGutterWidth(const Dimension & gutterWidth)116     void SetGutterWidth(const Dimension& gutterWidth)
117     {
118         if (GreatOrEqual(gutterWidth.Value(), 0.0)) {
119             gutterWidth_ = gutterWidth;
120         }
121     }
SetMarginLeft(const Dimension & marginLeft)122     void SetMarginLeft(const Dimension& marginLeft)
123     {
124         if (GreatOrEqual(marginLeft.Value(), 0.0)) {
125             marginLeft_ = marginLeft;
126         }
127     }
SetMarginRight(const Dimension & marginRight)128     void SetMarginRight(const Dimension& marginRight)
129     {
130         if (GreatOrEqual(marginRight.Value(), 0.0)) {
131             marginRight_ = marginRight;
132         }
133     }
134 
135     /*
136      * Use system screen width build column width.
137      */
138     void ACE_EXPORT BuildColumnWidth();
139     void BuildColumnWidth(double width);
140 
141 private:
142     GridContainerInfo() = default;
143     GridTemplateType templateType_ = GridTemplateType::NORMAL;
144     // current used size type
145     GridSizeType currentSizeType_ = GridSizeType::UNDEFINED;
146     GridSizeType sizeType_ = GridSizeType::UNDEFINED;
147     // container total column number
148     int32_t columns_ = UNDEFINED_INT;
149     Dimension gutterWidth_ = UNDEFINED_DIMENSION;
150     Dimension marginLeft_ = UNDEFINED_DIMENSION;
151     Dimension marginRight_ = UNDEFINED_DIMENSION;
152     Dimension paddingLeft_ = UNDEFINED_DIMENSION;
153     Dimension paddingRight_ = UNDEFINED_DIMENSION;
154 
155     double containerWidth_ = 0.0;
156     double columnWidth_ = 0.0;
157     GridColumnType columnType_ = GridColumnType::NONE;
158 };
159 
160 } // namespace OHOS::Ace
161 
162 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_GRID_CONTAINER_INFO_H
163