1 /*
2  * Copyright (c) 2022-2023 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_PATTERN_GRID_GRID_LAYOUT_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_LAYOUT_PROPERTY_H
18 
19 #include "core/components_ng/layout/layout_property.h"
20 #include "core/components_ng/pattern/grid/grid_constants.h"
21 #include "core/components_ng/pattern/grid/grid_layout_options.h"
22 
23 namespace OHOS::Ace::NG {
24 class InspectorFilter;
25 
26 class ACE_EXPORT GridLayoutProperty : public LayoutProperty {
27     DECLARE_ACE_TYPE(GridLayoutProperty, LayoutProperty);
28 
29 public:
30     GridLayoutProperty() = default;
31     ~GridLayoutProperty() override = default;
32 
Clone()33     RefPtr<LayoutProperty> Clone() const override
34     {
35         auto value = MakeRefPtr<GridLayoutProperty>();
36         value->LayoutProperty::UpdateLayoutProperty(DynamicCast<LayoutProperty>(this));
37         value->propRowsTemplate_ = CloneRowsTemplate();
38         value->propColumnsTemplate_ = CloneColumnsTemplate();
39         value->propRowsGap_ = CloneRowsGap();
40         value->propColumnsGap_ = CloneColumnsGap();
41         value->propCachedCount_ = CloneCachedCount();
42         value->propShowCachedItems_ = CloneShowCachedItems();
43         value->propGridDirection_ = CloneGridDirection();
44         value->propMaxCount_ = CloneMaxCount();
45         value->propMinCount_ = CloneMinCount();
46         value->propCellLength_ = CloneCellLength();
47         value->propScrollEnabled_ = CloneScrollEnabled();
48         value->propLayoutOptions_ = CloneLayoutOptions();
49         return value;
50     }
51 
Reset()52     void Reset() override
53     {
54         LayoutProperty::Reset();
55         ResetColumnsTemplate();
56         ResetRowsTemplate();
57         ResetColumnsGap();
58         ResetRowsGap();
59         ResetCachedCount();
60         ResetShowCachedItems();
61         ResetGridDirection();
62         ResetMaxCount();
63         ResetMinCount();
64         ResetCellLength();
65         ResetScrollEnabled();
66         ResetLayoutOptions();
67     }
68 
69     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
70 
IsVertical()71     bool IsVertical() const
72     {
73         bool columnsTemplateValid = propColumnsTemplate_.has_value() && !propColumnsTemplate_.value().empty();
74         bool rowsTemplateValid = propRowsTemplate_.has_value() && !propRowsTemplate_.value().empty();
75         return columnsTemplateValid ||
76                (!columnsTemplateValid && !rowsTemplateValid);
77     }
78 
IsConfiguredScrollable()79     bool IsConfiguredScrollable() const
80     {
81         bool columnsTemplateSet = !propColumnsTemplate_.value_or("").empty();
82         bool rowsTemplateSet = !propRowsTemplate_.value_or("").empty();
83         bool verticalScrollable = (columnsTemplateSet && !rowsTemplateSet);
84         bool horizontalScrollable = (!columnsTemplateSet && rowsTemplateSet);
85         return verticalScrollable || horizontalScrollable;
86     }
87 
IsReverse()88     bool IsReverse() const
89     {
90         bool columnsTemplateSet = !propColumnsTemplate_.value_or("").empty();
91         bool rowsTemplateSet = !propRowsTemplate_.value_or("").empty();
92         auto isRtl = GetNonAutoLayoutDirection() == TextDirection::RTL;
93         return isRtl && !columnsTemplateSet && rowsTemplateSet;
94     }
95 
96     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(ColumnsTemplate, std::string);
OnColumnsTemplateUpdate(const std::string &)97     void OnColumnsTemplateUpdate(const std::string& /* columnsTemplate */) const
98     {
99         ResetGridLayoutInfoAndMeasure();
100     }
101 
102     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(RowsTemplate, std::string);
OnRowsTemplateUpdate(const std::string &)103     void OnRowsTemplateUpdate(const std::string& /* rowsTemplate */) const
104     {
105         ResetGridLayoutInfoAndMeasure();
106     }
107 
108     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_AND_USING_CALLBACK(ColumnsGap, Dimension, PROPERTY_UPDATE_MEASURE);
109     void OnColumnsGapUpdate(const Dimension& /* columnsGap */) const;
110 
111     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_AND_USING_CALLBACK(RowsGap, Dimension, PROPERTY_UPDATE_MEASURE);
112     void OnRowsGapUpdate(const Dimension& /* rowsGap */) const;
113 
114     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(CachedCount, int32_t);
115     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ShowCachedItems, bool, PROPERTY_UPDATE_NORMAL);
OnCachedCountUpdate(int32_t)116     void OnCachedCountUpdate(int32_t /* cachedCount */) const {}
117 
118     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(GridDirection, FlexDirection);
OnGridDirectionUpdate(FlexDirection)119     void OnGridDirectionUpdate(FlexDirection /* gridDirection */) const
120     {
121         ResetGridLayoutInfoAndMeasure();
122     }
123 
124     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(MaxCount, int32_t);
OnMaxCountUpdate(int32_t)125     void OnMaxCountUpdate(int32_t /* maxCount */) const
126     {
127         ResetGridLayoutInfoAndMeasure();
128     }
129 
130     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(MinCount, int32_t);
OnMinCountUpdate(int32_t)131     void OnMinCountUpdate(int32_t /* minCount */) const
132     {
133         ResetGridLayoutInfoAndMeasure();
134     }
135 
136     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(CellLength, int32_t);
OnCellLengthUpdate(int32_t)137     void OnCellLengthUpdate(int32_t /* cellLength */) const
138     {
139         ResetGridLayoutInfoAndMeasure();
140     }
141 
142     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(LayoutOptions, GridLayoutOptions);
143     void OnLayoutOptionsUpdate(const GridLayoutOptions& layoutOptions) const;
144 
145     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Editable, bool, PROPERTY_UPDATE_LAYOUT);
146     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ScrollEnabled, bool, PROPERTY_UPDATE_MEASURE);
147 
148     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(AlignItems, GridItemAlignment);
OnAlignItemsUpdate(GridItemAlignment)149     void OnAlignItemsUpdate(GridItemAlignment /* alignItems */) const
150     {
151         ResetGridLayoutInfoAndMeasure();
152     }
153 
GetPercentSensitive()154     std::pair<bool, bool> GetPercentSensitive() override
155     {
156         return {true, true};
157     }
158 
159 private:
160     ACE_DISALLOW_COPY_AND_MOVE(GridLayoutProperty);
161 
162     void ResetGridLayoutInfoAndMeasure() const;
163     void ResetPositionFlags() const;
164     std::string GetBarStateString() const;
165     std::string GetGridDirectionStr() const;
166     Color GetBarColor() const;
167     Dimension GetBarWidth() const;
168 
169     void UpdateIrregularFlag(const GridLayoutOptions& layoutOptions) const;
170 };
171 } // namespace OHOS::Ace::NG
172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_LAYOUT_PROPERTY_H
173