• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_layout_options.h"
21 
22 namespace OHOS::Ace::NG {
23 class ACE_EXPORT GridLayoutProperty : public LayoutProperty {
24     DECLARE_ACE_TYPE(GridLayoutProperty, LayoutProperty);
25 
26 public:
27     GridLayoutProperty() = default;
28     ~GridLayoutProperty() override = default;
29 
Clone()30     RefPtr<LayoutProperty> Clone() const override
31     {
32         auto value = MakeRefPtr<GridLayoutProperty>();
33         value->LayoutProperty::UpdateLayoutProperty(DynamicCast<LayoutProperty>(this));
34         value->propRowsTemplate_ = CloneRowsTemplate();
35         value->propColumnsTemplate_ = CloneColumnsTemplate();
36         value->propRowsGap_ = CloneRowsGap();
37         value->propColumnsGap_ = CloneColumnsGap();
38         value->propCachedCount_ = CloneCachedCount();
39         value->propGridDirection_ = CloneGridDirection();
40         value->propMaxCount_ = CloneMaxCount();
41         value->propMinCount_ = CloneMinCount();
42         value->propCellLength_ = CloneCellLength();
43         value->propScrollEnabled_ = CloneScrollEnabled();
44         value->propLayoutOptions_ = CloneLayoutOptions();
45         return value;
46     }
47 
Reset()48     void Reset() override
49     {
50         LayoutProperty::Reset();
51         ResetColumnsTemplate();
52         ResetRowsTemplate();
53         ResetColumnsGap();
54         ResetRowsGap();
55         ResetCachedCount();
56         ResetGridDirection();
57         ResetMaxCount();
58         ResetMinCount();
59         ResetCellLength();
60         ResetScrollEnabled();
61         ResetLayoutOptions();
62     }
63 
64     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override;
65 
IsVertical()66     bool IsVertical() const
67     {
68         bool columnsTemplateValid = propColumnsTemplate_.has_value() && !propColumnsTemplate_.value().empty();
69         bool rowsTemplateValid = propRowsTemplate_.has_value() && !propRowsTemplate_.value().empty();
70         return columnsTemplateValid ||
71                (!columnsTemplateValid && !rowsTemplateValid); // TODO: take layoutDirection into account
72     }
73 
IsConfiguredScrollable()74     bool IsConfiguredScrollable() const
75     {
76         bool columnsTemplateSet = !propColumnsTemplate_.value_or("").empty();
77         bool rowsTemplateSet = !propRowsTemplate_.value_or("").empty();
78         bool verticalScrollable = (columnsTemplateSet && !rowsTemplateSet);
79         bool horizontalScrollable = (!columnsTemplateSet && rowsTemplateSet);
80         return verticalScrollable || horizontalScrollable;
81     }
82 
83     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(ColumnsTemplate, std::string);
OnColumnsTemplateUpdate(const std::string &)84     void OnColumnsTemplateUpdate(const std::string& /* columnsTemplate */) const
85     {
86         ResetGridLayoutInfoAndMeasure();
87     }
88 
89     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(RowsTemplate, std::string);
OnRowsTemplateUpdate(const std::string &)90     void OnRowsTemplateUpdate(const std::string& /* rowsTemplate */) const
91     {
92         ResetGridLayoutInfoAndMeasure();
93     }
94 
95     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_AND_USING_CALLBACK(ColumnsGap, Dimension, PROPERTY_UPDATE_MEASURE);
OnColumnsGapUpdate(const Dimension &)96     void OnColumnsGapUpdate(const Dimension& /* columnsGap */) const
97     {
98         ResetPositionFlags();
99     }
100 
101     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_AND_USING_CALLBACK(RowsGap, Dimension, PROPERTY_UPDATE_MEASURE);
OnRowsGapUpdate(const Dimension &)102     void OnRowsGapUpdate(const Dimension& /* rowsGap */) const
103     {
104         ResetPositionFlags();
105     }
106 
107     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(CachedCount, int32_t);
OnCachedCountUpdate(int32_t)108     void OnCachedCountUpdate(int32_t /* cachedCount */) const {}
109 
110     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(GridDirection, FlexDirection);
OnGridDirectionUpdate(FlexDirection)111     void OnGridDirectionUpdate(FlexDirection /* gridDirection */) const
112     {
113         ResetGridLayoutInfoAndMeasure();
114     }
115 
116     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(MaxCount, int32_t);
OnMaxCountUpdate(int32_t)117     void OnMaxCountUpdate(int32_t /* maxCount */) const
118     {
119         ResetGridLayoutInfoAndMeasure();
120     }
121 
122     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(MinCount, int32_t);
OnMinCountUpdate(int32_t)123     void OnMinCountUpdate(int32_t /* minCount */) const
124     {
125         ResetGridLayoutInfoAndMeasure();
126     }
127 
128     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(CellLength, int32_t);
OnCellLengthUpdate(int32_t)129     void OnCellLengthUpdate(int32_t /* cellLength */) const
130     {
131         ResetGridLayoutInfoAndMeasure();
132     }
133 
134     ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(LayoutOptions, GridLayoutOptions);
OnLayoutOptionsUpdate(GridLayoutOptions)135     void OnLayoutOptionsUpdate(GridLayoutOptions /* layoutOptions */) const
136     {
137         ResetGridLayoutInfoAndMeasure();
138     }
139 
140     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Editable, bool, PROPERTY_UPDATE_LAYOUT);
141     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ScrollEnabled, bool, PROPERTY_UPDATE_MEASURE);
142 
143 private:
144     ACE_DISALLOW_COPY_AND_MOVE(GridLayoutProperty);
145 
146     void ResetGridLayoutInfoAndMeasure() const;
147     void ResetPositionFlags() const;
148     std::string GetBarStateString() const;
149     std::string GetGridDirectionStr() const;
150     Color GetBarColor() const;
151     Dimension GetBarWidth() const;
152 };
153 } // namespace OHOS::Ace::NG
154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_LAYOUT_PROPERTY_H
155