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 "base/utils/utils.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/base/inspector_filter.h"
21 #include "core/components_ng/pattern/grid/grid_pattern.h"
22 #include "core/components_v2/inspector/inspector_constants.h"
23 
24 namespace OHOS::Ace::NG {
25 
ResetGridLayoutInfoAndMeasure() const26 void GridItemLayoutProperty::ResetGridLayoutInfoAndMeasure() const
27 {
28     auto host = GetHost();
29     CHECK_NULL_VOID(host);
30     auto uiNode = DynamicCast<UINode>(host);
31     while (uiNode->GetTag() != V2::GRID_ETS_TAG) {
32         uiNode = uiNode->GetParent();
33         CHECK_NULL_VOID(uiNode);
34     }
35     auto grid = DynamicCast<FrameNode>(uiNode);
36     CHECK_NULL_VOID(grid);
37     auto pattern = grid->GetPattern<GridPattern>();
38     CHECK_NULL_VOID(pattern);
39     pattern->ResetGridLayoutInfo();
40     if (grid->GetParent()) {
41         grid->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
42     }
43 }
44 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const45 void GridItemLayoutProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
46 {
47     LayoutProperty::ToJsonValue(json, filter);
48     /* no fixed attr below, just return */
49     if (filter.IsFastFilter()) {
50         return;
51     }
52     json->PutExtAttr("rowStart", std::to_string(propRowStart_.value_or(0)).c_str(), filter);
53     json->PutExtAttr("rowEnd", std::to_string(propRowEnd_.value_or(0)).c_str(), filter);
54     json->PutExtAttr("columnStart", std::to_string(propColumnStart_.value_or(0)).c_str(), filter);
55     json->PutExtAttr("columnEnd", std::to_string(propColumnEnd_.value_or(0)).c_str(), filter);
56 }
57 
GetCustomCrossIndex(Axis axis) const58 int32_t GridItemLayoutProperty::GetCustomCrossIndex(Axis axis) const
59 {
60     if (axis == Axis::VERTICAL) {
61         return propColumnStart_.value_or(-1);
62     }
63     return propRowStart_.value_or(-1);
64 }
65 
GetMainSpan(Axis axis) const66 int32_t GridItemLayoutProperty::GetMainSpan(Axis axis) const
67 {
68     if (axis == Axis::VERTICAL) {
69         return std::max(propRowEnd_.value_or(-1) - propRowStart_.value_or(-1) + 1, 1);
70     }
71     return std::max(propColumnEnd_.value_or(-1) - propColumnStart_.value_or(-1) + 1, 1);
72 }
73 
GetCrossSpan(Axis axis) const74 int32_t GridItemLayoutProperty::GetCrossSpan(Axis axis) const
75 {
76     if (axis == Axis::VERTICAL) {
77         return std::max(propColumnEnd_.value_or(-1) - propColumnStart_.value_or(-1) + 1, 1);
78     }
79     return std::max(propRowEnd_.value_or(-1) - propRowStart_.value_or(-1) + 1, 1);
80 }
81 
GetMainStart(Axis axis) const82 int32_t GridItemLayoutProperty::GetMainStart(Axis axis) const
83 {
84     if (axis == Axis::VERTICAL) {
85         return propRowStart_.value_or(-1);
86     }
87     return propColumnStart_.value_or(-1);
88 }
89 
GetCrossStart(Axis axis) const90 int32_t GridItemLayoutProperty::GetCrossStart(Axis axis) const
91 {
92     if (axis == Axis::VERTICAL) {
93         return propColumnStart_.value_or(-1);
94     }
95     return propRowStart_.value_or(-1);
96 }
97 
GetMainEnd(Axis axis) const98 int32_t GridItemLayoutProperty::GetMainEnd(Axis axis) const
99 {
100     if (axis == Axis::VERTICAL) {
101         return propRowEnd_.value_or(-1);
102     }
103     return propColumnEnd_.value_or(-1);
104 }
105 
GetCrossEnd(Axis axis) const106 int32_t GridItemLayoutProperty::GetCrossEnd(Axis axis) const
107 {
108     if (axis == Axis::VERTICAL) {
109         return propColumnEnd_.value_or(-1);
110     }
111     return propRowEnd_.value_or(-1);
112 }
113 
GetRealMainSpan(Axis axis) const114 int32_t GridItemLayoutProperty::GetRealMainSpan(Axis axis) const
115 {
116     if (axis == Axis::VERTICAL) {
117         return propRealRowSpan_.value_or(-1);
118     }
119     return propRealColumnSpan_.value_or(-1);
120 }
121 
GetRealCrossSpan(Axis axis) const122 int32_t GridItemLayoutProperty::GetRealCrossSpan(Axis axis) const
123 {
124     if (axis == Axis::VERTICAL) {
125         return propRealColumnSpan_.value_or(-1);
126     }
127     return propRealRowSpan_.value_or(-1);
128 }
129 
CheckWhetherCurrentItemAtExpectedPosition(Axis axis) const130 bool GridItemLayoutProperty::CheckWhetherCurrentItemAtExpectedPosition(Axis axis) const
131 {
132     auto realMainSpan = GetRealMainSpan(axis);
133     auto realCrossSpan = GetRealCrossSpan(axis);
134     auto realMainIndex = propMainIndex_.value_or(-1);
135     auto realCrossIndex = propCrossIndex_.value_or(-1);
136 
137     auto realMainStart = realMainIndex - realMainSpan + 1;
138     auto realMainEnd = realMainIndex;
139     auto realCrossStart = realCrossIndex;
140     auto realCrossEnd = realCrossIndex + realCrossSpan - 1;
141 
142     auto expectMainStart = axis == Axis::VERTICAL ? propRowStart_.value_or(-1) : propColumnStart_.value_or(-1);
143     auto expectMainEnd = axis == Axis::VERTICAL ? propRowEnd_.value_or(-1) : propColumnEnd_.value_or(-1);
144     auto expectCrossStart = axis == Axis::VERTICAL ? propColumnStart_.value_or(-1) : propRowStart_.value_or(-1);
145     auto expectCrossEnd = axis == Axis::VERTICAL ? propColumnEnd_.value_or(-1) : propRowEnd_.value_or(-1);
146 
147     if (realMainStart != expectMainStart || realMainEnd != expectMainEnd || realCrossStart != expectCrossStart ||
148         realCrossEnd != expectCrossEnd) {
149         return false;
150     }
151     return true;
152 }
153 } // namespace OHOS::Ace::NG
154