1 /*
2 * Copyright (c) 2022 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_item_layout_property.h"
17
18 #include "core/components_ng/pattern/grid/grid_pattern.h"
19
20 namespace OHOS::Ace::NG {
21
ResetGridLayoutInfoAndMeasure() const22 void GridItemLayoutProperty::ResetGridLayoutInfoAndMeasure() const
23 {
24 auto host = GetHost();
25 CHECK_NULL_VOID(host);
26 auto uiNode = DynamicCast<UINode>(host);
27 while (uiNode->GetTag() != V2::GRID_ETS_TAG) {
28 uiNode = uiNode->GetParent();
29 CHECK_NULL_VOID(uiNode);
30 }
31 auto grid = DynamicCast<FrameNode>(uiNode);
32 CHECK_NULL_VOID(grid);
33 auto pattern = grid->GetPattern<GridPattern>();
34 CHECK_NULL_VOID(pattern);
35 pattern->ResetGridLayoutInfo();
36 if (grid->GetParent()) {
37 grid->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
38 }
39 }
40
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const41 void GridItemLayoutProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
42 {
43 LayoutProperty::ToJsonValue(json, filter);
44 /* no fixed attr below, just return */
45 if (filter.IsFastFilter()) {
46 return;
47 }
48 json->PutExtAttr("rowStart", std::to_string(propRowStart_.value_or(0)).c_str(), filter);
49 json->PutExtAttr("rowEnd", std::to_string(propRowEnd_.value_or(0)).c_str(), filter);
50 json->PutExtAttr("columnStart", std::to_string(propColumnStart_.value_or(0)).c_str(), filter);
51 json->PutExtAttr("columnEnd", std::to_string(propColumnEnd_.value_or(0)).c_str(), filter);
52 }
53
GetCustomCrossIndex(Axis axis) const54 int32_t GridItemLayoutProperty::GetCustomCrossIndex(Axis axis) const
55 {
56 if (axis == Axis::VERTICAL) {
57 return propColumnStart_.value_or(-1);
58 }
59 return propRowStart_.value_or(-1);
60 }
61
GetMainSpan(Axis axis) const62 int32_t GridItemLayoutProperty::GetMainSpan(Axis axis) const
63 {
64 if (axis == Axis::VERTICAL) {
65 return std::max(propRowEnd_.value_or(-1) - propRowStart_.value_or(-1) + 1, 1);
66 }
67 return std::max(propColumnEnd_.value_or(-1) - propColumnStart_.value_or(-1) + 1, 1);
68 }
69
GetCrossSpan(Axis axis) const70 int32_t GridItemLayoutProperty::GetCrossSpan(Axis axis) const
71 {
72 if (axis == Axis::VERTICAL) {
73 return std::max(propColumnEnd_.value_or(-1) - propColumnStart_.value_or(-1) + 1, 1);
74 }
75 return std::max(propRowEnd_.value_or(-1) - propRowStart_.value_or(-1) + 1, 1);
76 }
77
GetMainStart(Axis axis) const78 int32_t GridItemLayoutProperty::GetMainStart(Axis axis) const
79 {
80 if (axis == Axis::VERTICAL) {
81 return propRowStart_.value_or(-1);
82 }
83 return propColumnStart_.value_or(-1);
84 }
85
GetCrossStart(Axis axis) const86 int32_t GridItemLayoutProperty::GetCrossStart(Axis axis) const
87 {
88 if (axis == Axis::VERTICAL) {
89 return propColumnStart_.value_or(-1);
90 }
91 return propRowStart_.value_or(-1);
92 }
93
GetMainEnd(Axis axis) const94 int32_t GridItemLayoutProperty::GetMainEnd(Axis axis) const
95 {
96 if (axis == Axis::VERTICAL) {
97 return propRowEnd_.value_or(-1);
98 }
99 return propColumnEnd_.value_or(-1);
100 }
101
GetCrossEnd(Axis axis) const102 int32_t GridItemLayoutProperty::GetCrossEnd(Axis axis) const
103 {
104 if (axis == Axis::VERTICAL) {
105 return propColumnEnd_.value_or(-1);
106 }
107 return propRowEnd_.value_or(-1);
108 }
109
GetRealMainSpan(Axis axis) const110 int32_t GridItemLayoutProperty::GetRealMainSpan(Axis axis) const
111 {
112 if (axis == Axis::VERTICAL) {
113 return propRealRowSpan_.value_or(-1);
114 }
115 return propRealColumnSpan_.value_or(-1);
116 }
117
GetRealCrossSpan(Axis axis) const118 int32_t GridItemLayoutProperty::GetRealCrossSpan(Axis axis) const
119 {
120 if (axis == Axis::VERTICAL) {
121 return propRealColumnSpan_.value_or(-1);
122 }
123 return propRealRowSpan_.value_or(-1);
124 }
125
CheckWhetherCurrentItemAtExpectedPosition(Axis axis) const126 bool GridItemLayoutProperty::CheckWhetherCurrentItemAtExpectedPosition(Axis axis) const
127 {
128 auto realMainSpan = GetRealMainSpan(axis);
129 auto realCrossSpan = GetRealCrossSpan(axis);
130 auto realMainIndex = propMainIndex_.value_or(-1);
131 auto realCrossIndex = propCrossIndex_.value_or(-1);
132
133 auto realMainStart = realMainIndex - realMainSpan + 1;
134 auto realMainEnd = realMainIndex;
135 auto realCrossStart = realCrossIndex;
136 auto realCrossEnd = realCrossIndex + realCrossSpan - 1;
137
138 auto expectMainStart = axis == Axis::VERTICAL ? propRowStart_.value_or(-1) : propColumnStart_.value_or(-1);
139 auto expectMainEnd = axis == Axis::VERTICAL ? propRowEnd_.value_or(-1) : propColumnEnd_.value_or(-1);
140 auto expectCrossStart = axis == Axis::VERTICAL ? propColumnStart_.value_or(-1) : propRowStart_.value_or(-1);
141 auto expectCrossEnd = axis == Axis::VERTICAL ? propColumnEnd_.value_or(-1) : propRowEnd_.value_or(-1);
142
143 if (realMainStart != expectMainStart || realMainEnd != expectMainEnd || realCrossStart != expectCrossStart ||
144 realCrossEnd != expectCrossEnd) {
145 return false;
146 }
147 return true;
148 }
149 } // namespace OHOS::Ace::NG
150