• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "core/components_ng/pattern/grid/grid_pattern.h"
19 
20 namespace OHOS::Ace::NG {
21 
ResetGridLayoutInfoAndMeasure() const22 void GridLayoutProperty::ResetGridLayoutInfoAndMeasure() const
23 {
24     auto host = GetHost();
25     CHECK_NULL_VOID(host);
26     auto pattern = host->GetPattern<GridPattern>();
27     CHECK_NULL_VOID(pattern);
28     pattern->ResetGridLayoutInfo();
29     if (host->GetParent()) {
30         host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
31     }
32 }
33 
ResetPositionFlags() const34 void GridLayoutProperty::ResetPositionFlags() const
35 {
36     auto host = GetHost();
37     CHECK_NULL_VOID(host);
38     auto pattern = host->GetPattern<GridPattern>();
39     CHECK_NULL_VOID(pattern);
40     pattern->ResetPositionFlags();
41 }
42 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const43 void GridLayoutProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
44 {
45     LayoutProperty::ToJsonValue(json, filter);
46     /* no fixed attr below, just return */
47     if (filter.IsFastFilter()) {
48         return;
49     }
50     json->PutExtAttr("columnsTemplate", propColumnsTemplate_.value_or("").c_str(), filter);
51     json->PutExtAttr("rowsTemplate", propRowsTemplate_.value_or("").c_str(), filter);
52     json->PutExtAttr("columnsGap", propColumnsGap_.value_or(0.0_vp).ToString().c_str(), filter);
53     json->PutExtAttr("rowsGap", propRowsGap_.value_or(0.0_vp).ToString().c_str(), filter);
54     json->PutExtAttr("cachedCount", propCachedCount_.value_or(1), filter);
55     json->PutExtAttr("editMode", propEditable_.value_or(false) ? "true" : "false", filter);
56     json->PutExtAttr("layoutDirection", GetGridDirectionStr().c_str(), filter);
57     json->PutExtAttr("focusWrapMode", GetFocusWrapModeStr().c_str(), filter);
58     json->PutExtAttr("maxCount", propMaxCount_.value_or(Infinity<int32_t>()), filter);
59     json->PutExtAttr("minCount", propMinCount_.value_or(1), filter);
60     json->PutExtAttr("cellLength", propCellLength_.value_or(0), filter);
61     json->PutExtAttr("enableScrollInteraction", propScrollEnabled_.value_or(true), filter);
62     json->PutExtAttr("gridLayoutOptions", propLayoutOptions_.has_value() ? "true" : "false", filter);
63     auto regularSizeArray = JsonUtil::CreateArray();
64     auto irregularIndexesArray = JsonUtil::CreateArray();
65     auto layoutOptions = GetLayoutOptions();
66     if (layoutOptions) {
67         auto regularSize = layoutOptions.value().regularSize;
68         regularSizeArray->Put("", regularSize.rows);
69         regularSizeArray->Put("", regularSize.columns);
70 
71         auto irregularIndexes = layoutOptions.value().irregularIndexes;
72         for (auto item : irregularIndexes) {
73             irregularIndexesArray->Put("", item);
74         }
75     }
76     json->PutExtAttr("regularSize", regularSizeArray, filter);
77     json->PutExtAttr("irregularIndexes", irregularIndexesArray, filter);
78     json->PutExtAttr("alignItems", GetAlignItems().value_or(GridItemAlignment::DEFAULT) ==
79         GridItemAlignment::DEFAULT ? "GridItemAlignment.Default" : "GridItemAlignment.Stretch", filter);
80     json->PutExtAttr("syncLoad", propSyncLoad_.value_or(true), filter);
81 }
82 
GetGridDirectionStr() const83 std::string GridLayoutProperty::GetGridDirectionStr() const
84 {
85     auto gridDirection = propGridDirection_.value_or(FlexDirection::ROW);
86     switch (gridDirection) {
87         case FlexDirection::ROW:
88             return "GridDirection.Row";
89         case FlexDirection::ROW_REVERSE:
90             return "GridDirection.RowReverse";
91         case FlexDirection::COLUMN:
92             return "GridDirection.Column";
93         case FlexDirection::COLUMN_REVERSE:
94             return "GridDirection.ColumnReverse";
95         default:
96             TAG_LOGW(AceLogTag::ACE_GRID, "grid direction %{public}d is not valid", gridDirection);
97             break;
98     }
99     return "GridDirection.Row";
100 }
101 
GetFocusWrapModeStr() const102 std::string GridLayoutProperty::GetFocusWrapModeStr() const
103 {
104     auto focusWrapMode = propFocusWrapMode_.value_or(FocusWrapMode::DEFAULT);
105     switch (focusWrapMode) {
106         case FocusWrapMode::DEFAULT:
107             return "FocusWrapMode.DEFAULT";
108         case FocusWrapMode::WRAP_WITH_ARROW:
109             return "FocusWrapMode.WRAP_WITH_ARROW";
110         default:
111             break;
112     }
113     return "FocusWrapMode.DEFAULT";
114 }
115 
OnColumnsGapUpdate(const Dimension &) const116 void GridLayoutProperty::OnColumnsGapUpdate(const Dimension& /* columnsGap */) const
117 {
118     ResetPositionFlags();
119     if (SystemProperties::GetGridIrregularLayoutEnabled() && HasLayoutOptions()) {
120         ResetGridLayoutInfoAndMeasure();
121     }
122 }
OnRowsGapUpdate(const Dimension &) const123 void GridLayoutProperty::OnRowsGapUpdate(const Dimension& /* rowsGap */) const
124 {
125     ResetPositionFlags();
126     if (SystemProperties::GetGridIrregularLayoutEnabled() && HasLayoutOptions()) {
127         ResetGridLayoutInfoAndMeasure();
128     }
129 }
130 
UpdateIrregularFlag(const GridLayoutOptions & layoutOptions) const131 void GridLayoutProperty::UpdateIrregularFlag(const GridLayoutOptions& layoutOptions) const
132 {
133     auto host = GetHost();
134     CHECK_NULL_VOID(host);
135     auto pattern = host->GetPattern<GridPattern>();
136     CHECK_NULL_VOID(pattern);
137     pattern->SetIrregular(false);
138     CHECK_NULL_VOID(layoutOptions.getSizeByIndex);
139 
140     bool vertical = IsVertical();
141     for (int32_t idx : layoutOptions.irregularIndexes) {
142         auto size = layoutOptions.getSizeByIndex(idx);
143         if ((!vertical && size.columns > 1) || (vertical && size.rows > 1)) {
144             pattern->SetIrregular(true);
145             return;
146         }
147     }
148 }
149 
OnLayoutOptionsUpdate(const GridLayoutOptions & layoutOptions) const150 void GridLayoutProperty::OnLayoutOptionsUpdate(const GridLayoutOptions& layoutOptions) const
151 {
152     UpdateIrregularFlag(layoutOptions);
153     ResetGridLayoutInfoAndMeasure();
154 }
155 } // namespace OHOS::Ace::NG
156