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 #include "core/components_ng/pattern/grid/grid_layout_property.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "base/utils/system_properties.h"
20 #include "base/utils/utils.h"
21 #include "core/components/scroll/scroll_bar_theme.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/base/inspector_filter.h"
24 #include "core/components_ng/pattern/grid/grid_pattern.h"
25 #include "core/pipeline_ng/pipeline_context.h"
26 
27 namespace OHOS::Ace::NG {
28 
ResetGridLayoutInfoAndMeasure() const29 void GridLayoutProperty::ResetGridLayoutInfoAndMeasure() const
30 {
31     auto host = GetHost();
32     CHECK_NULL_VOID(host);
33     auto pattern = host->GetPattern<GridPattern>();
34     CHECK_NULL_VOID(pattern);
35     pattern->ResetGridLayoutInfo();
36     if (host->GetParent()) {
37         host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
38     }
39 }
40 
ResetPositionFlags() const41 void GridLayoutProperty::ResetPositionFlags() const
42 {
43     auto host = GetHost();
44     CHECK_NULL_VOID(host);
45     auto pattern = host->GetPattern<GridPattern>();
46     CHECK_NULL_VOID(pattern);
47     pattern->ResetPositionFlags();
48 }
49 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const50 void GridLayoutProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
51 {
52     LayoutProperty::ToJsonValue(json, filter);
53     /* no fixed attr below, just return */
54     if (filter.IsFastFilter()) {
55         return;
56     }
57     json->PutExtAttr("columnsTemplate", propColumnsTemplate_.value_or("").c_str(), filter);
58     json->PutExtAttr("rowsTemplate", propRowsTemplate_.value_or("").c_str(), filter);
59     json->PutExtAttr("columnsGap", propColumnsGap_.value_or(0.0_vp).ToString().c_str(), filter);
60     json->PutExtAttr("rowsGap", propRowsGap_.value_or(0.0_vp).ToString().c_str(), filter);
61     json->PutExtAttr("cachedCount", propCachedCount_.value_or(1), filter);
62     json->PutExtAttr("editMode", propEditable_.value_or(false) ? "true" : "false", filter);
63     json->PutExtAttr("layoutDirection", GetGridDirectionStr().c_str(), filter);
64     json->PutExtAttr("maxCount", propMaxCount_.value_or(Infinity<int32_t>()), filter);
65     json->PutExtAttr("minCount", propMinCount_.value_or(1), filter);
66     json->PutExtAttr("cellLength", propCellLength_.value_or(0), filter);
67     json->PutExtAttr("enableScrollInteraction", propScrollEnabled_.value_or(true), filter);
68 }
69 
GetGridDirectionStr() const70 std::string GridLayoutProperty::GetGridDirectionStr() const
71 {
72     auto gridDirection = propGridDirection_.value_or(FlexDirection::ROW);
73     switch (gridDirection) {
74         case FlexDirection::ROW:
75             return "GridDirection.Row";
76         case FlexDirection::ROW_REVERSE:
77             return "GridDirection.RowReverse";
78         case FlexDirection::COLUMN:
79             return "GridDirection.Column";
80         case FlexDirection::COLUMN_REVERSE:
81             return "GridDirection.ColumnReverse";
82         default:
83             TAG_LOGW(AceLogTag::ACE_GRID, "grid direction %{public}d is not valid", gridDirection);
84             break;
85     }
86     return "GridDirection.Row";
87 }
88 
OnColumnsGapUpdate(const Dimension &) const89 void GridLayoutProperty::OnColumnsGapUpdate(const Dimension& /* columnsGap */) const
90 {
91     ResetPositionFlags();
92     if (SystemProperties::GetGridIrregularLayoutEnabled() && HasLayoutOptions()) {
93         ResetGridLayoutInfoAndMeasure();
94     }
95 }
OnRowsGapUpdate(const Dimension &) const96 void GridLayoutProperty::OnRowsGapUpdate(const Dimension& /* rowsGap */) const
97 {
98     ResetPositionFlags();
99     if (SystemProperties::GetGridIrregularLayoutEnabled() && HasLayoutOptions()) {
100         ResetGridLayoutInfoAndMeasure();
101     }
102 }
103 
UpdateIrregularFlag(const GridLayoutOptions & layoutOptions) const104 void GridLayoutProperty::UpdateIrregularFlag(const GridLayoutOptions& layoutOptions) const
105 {
106     auto host = GetHost();
107     CHECK_NULL_VOID(host);
108     auto pattern = host->GetPattern<GridPattern>();
109     CHECK_NULL_VOID(pattern);
110     pattern->SetIrregular(false);
111     CHECK_NULL_VOID(layoutOptions.getSizeByIndex);
112 
113     bool vertical = IsVertical();
114     for (int32_t idx : layoutOptions.irregularIndexes) {
115         auto size = layoutOptions.getSizeByIndex(idx);
116         if ((!vertical && size.columns > 1) || (vertical && size.rows > 1)) {
117             pattern->SetIrregular(true);
118             return;
119         }
120     }
121 }
122 
OnLayoutOptionsUpdate(const GridLayoutOptions & layoutOptions) const123 void GridLayoutProperty::OnLayoutOptionsUpdate(const GridLayoutOptions& layoutOptions) const
124 {
125     UpdateIrregularFlag(layoutOptions);
126     ResetGridLayoutInfoAndMeasure();
127 }
128 } // namespace OHOS::Ace::NG
129