• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "grid_property.h"
17 
18 #include "core/components_ng/pattern/grid_container/grid_container_layout_property.h"
19 
20 namespace OHOS::Ace::NG {
21 
GetWidth()22 Dimension GridProperty::GetWidth()
23 {
24     CHECK_NULL_RETURN(gridInfo_, Dimension());
25     // gridInfo_ must exist, because layout algorithm invoke UpdateContainer first
26     return Dimension(gridInfo_->GetWidth());
27 }
28 
GetOffset()29 Dimension GridProperty::GetOffset()
30 {
31     CHECK_NULL_RETURN(gridInfo_, Dimension());
32     // gridInfo_ must exist, because layout algorithm invoke UpdateContainer() first
33     auto offset = gridInfo_->GetOffset();
34     if (offset == UNDEFINED_DIMENSION) {
35         return UNDEFINED_DIMENSION;
36     }
37     auto marginOffset = Dimension(gridInfo_->GetParent()->GetMarginLeft().ConvertToPx());
38     return offset + marginOffset;
39 }
40 
UpdateContainer(const RefPtr<Property> & container,const RefPtr<AceType> & host)41 bool GridProperty::UpdateContainer(const RefPtr<Property>& container, const RefPtr<AceType>& host)
42 {
43     auto currentContainer = DynamicCast<GridContainerLayoutProperty>(container);
44     auto gridContainer = currentContainer->GetReserveObj();
45     GridColumnInfo::Builder builder;
46     auto containerInfo = AceType::MakeRefPtr<GridContainerInfo>(gridContainer->GetContainerInfoValue());
47     builder.SetParent(containerInfo);
48     for (const auto& item : typedPropertySet_) {
49         builder.SetSizeColumn(item.type_, item.span_);
50         builder.SetOffset(item.offset_, item.type_);
51     }
52     gridInfo_ = builder.Build();
53     container_ = container;
54 
55     currentContainer->RegistGridChild(DynamicCast<FrameNode>(host));
56     return true;
57 }
58 
UpdateSpan(int32_t span,GridSizeType type)59 bool GridProperty::UpdateSpan(int32_t span, GridSizeType type)
60 {
61     if (span < 0) {
62         LOGE("Span value is illegal.");
63         return false;
64     }
65     if (!container_) {
66         SetSpan(type, span);
67         return true;
68     }
69 
70     auto container = DynamicCast<GridContainerLayoutProperty>(container_);
71     GridSizeType currentType = container->GetContainerInfo()->GetSizeType(); // working type, not UNDEFINED
72     auto currentProp = GetTypedProperty(type);                               // working property
73 
74     return ((currentProp.has_value() && currentProp->type_ == type) || currentType == type) && SetSpan(type, span);
75 }
76 
UpdateOffset(int32_t offset,GridSizeType type)77 bool GridProperty::UpdateOffset(int32_t offset, GridSizeType type)
78 {
79     if (!container_) {
80         SetOffset(type, offset);
81         return true;
82     }
83     auto container = DynamicCast<GridContainerLayoutProperty>(container_);
84     GridSizeType currentType = container->GetContainerInfo()->GetSizeType(); // working type, not UNDEFINED
85     auto currentProp = GetTypedProperty(type);                               // working property
86 
87     return ((currentProp.has_value() && currentProp->type_ == type) || currentType == type) && SetOffset(type, offset);
88 }
89 
SetSpan(GridSizeType type,int32_t span)90 bool GridProperty::SetSpan(GridSizeType type, int32_t span)
91 {
92     auto item = std::find_if(typedPropertySet_.begin(), typedPropertySet_.end(),
93         [type](const GridTypedProperty& p) { return p.type_ == type; });
94     if (item == typedPropertySet_.end()) {
95         typedPropertySet_.emplace_back(type, span, DEFAULT_GRID_OFFSET);
96         return true;
97     }
98     if (item->span_ == span) {
99         return false;
100     }
101     item->span_ = span;
102     return true;
103 }
104 
SetOffset(GridSizeType type,int32_t offset)105 bool GridProperty::SetOffset(GridSizeType type, int32_t offset)
106 {
107     auto item = std::find_if(typedPropertySet_.begin(), typedPropertySet_.end(),
108         [type](const GridTypedProperty& p) { return p.type_ == type; });
109     if (item == typedPropertySet_.end()) {
110         typedPropertySet_.emplace_back(type, DEFAULT_GRID_SPAN, offset);
111         return true;
112     }
113     if (item->offset_ == offset) {
114         return false;
115     }
116     item->offset_ = offset;
117     return true;
118 }
119 
GetContainerPosition()120 OffsetF GridProperty::GetContainerPosition()
121 {
122     if (container_) {
123         auto container = DynamicCast<GridContainerLayoutProperty>(container_);
124         auto framenode = container->GetHost();
125         CHECK_NULL_RETURN(framenode, OffsetF());
126         return framenode->GetOffsetRelativeToWindow();
127     }
128     return OffsetF();
129 }
130 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const131 void GridProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
132 {
133     const char* GRID_SIZE_TYPE[] = { "default", "sx", "sm", "md", "lg" };
134     /* no fixed attr below, just return */
135     if (filter.IsFastFilter()) {
136         return;
137     }
138     if (!gridInfo_) {
139         auto item = std::find_if(typedPropertySet_.begin(), typedPropertySet_.end(),
140             [](const GridTypedProperty& p) { return p.type_ == GridSizeType::UNDEFINED; });
141         if (item == typedPropertySet_.end()) {
142             json->PutExtAttr("gridSpan", 1, filter);
143             json->PutExtAttr("gridOffset", 0, filter);
144         } else {
145             json->PutExtAttr("gridSpan", item->span_, filter);
146             json->PutExtAttr("gridOffset", item->offset_, filter);
147         }
148 
149         auto useSizeType = JsonUtil::Create(true);
150         for (const auto& item : typedPropertySet_) {
151             auto jsonValue = JsonUtil::Create(true);
152             jsonValue->Put("span", item.span_);
153             jsonValue->Put("offset", item.offset_);
154             useSizeType->Put(GRID_SIZE_TYPE[static_cast<int32_t>(item.type_)], jsonValue);
155         }
156         json->PutExtAttr("useSizeType", useSizeType, filter);
157         return;
158     }
159 
160     auto gridOffset = gridInfo_->GetOffset(GridSizeType::UNDEFINED);
161     json->PutExtAttr("gridSpan", static_cast<int32_t>(gridInfo_->GetColumns()), filter);
162     json->PutExtAttr("gridOffset", gridOffset == -1 ? 0 : gridOffset, filter);
163 
164     auto useSizeType = JsonUtil::Create(true);
165     auto index = static_cast<int32_t>(GridSizeType::XS);
166     for (; index < static_cast<int32_t>(GridSizeType::XL); index++) {
167         auto jsonValue = JsonUtil::Create(true);
168         auto type = static_cast<GridSizeType>(index);
169         jsonValue->Put("span", static_cast<int32_t>(gridInfo_->GetColumns(type)));
170         jsonValue->Put("offset", gridInfo_->GetOffset(type));
171         useSizeType->Put(GRID_SIZE_TYPE[index], jsonValue);
172     }
173     json->PutExtAttr("useSizeType", useSizeType, filter);
174 }
175 
176 } // namespace OHOS::Ace::NG
177