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_container_layout_property.h"
17 
18 #include "base/log/log.h"
19 #include "core/components_ng/base/inspector_filter.h"
20 
21 namespace OHOS::Ace::NG {
22 
RegistGridChild(const RefPtr<FrameNode> & child)23 void GridContainerLayoutProperty::RegistGridChild(const RefPtr<FrameNode>& child)
24 {
25     childrenFramenode_.emplace_back(child);
26 }
27 
OnContainerInfoUpdate(const GridContainerInfo &)28 void GridContainerLayoutProperty::OnContainerInfoUpdate(const GridContainerInfo& /* info */)
29 {
30     auto p = childrenFramenode_.begin();
31     while (p != childrenFramenode_.end()) {
32         RefPtr<FrameNode> child = p->Upgrade();
33         if (child) {
34             child->MarkDirtyNode(PROPERTY_UPDATE_MEASURE | PROPERTY_UPDATE_LAYOUT);
35         }
36         p = childrenFramenode_.erase(p);
37     }
38 }
39 
BuildWidth(float width)40 void GridContainerLayoutProperty::BuildWidth(float width)
41 {
42     if (GreaterOrEqualToInfinity(width)) {
43         propContainerInfo_->BuildColumnWidth();
44     } else {
45         propContainerInfo_->BuildColumnWidth(width);
46     }
47 }
48 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const49 void GridContainerLayoutProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
50 {
51     LinearLayoutProperty::ToJsonValue(json, filter);
52     if (!HasContainerInfo()) {
53         return;
54     }
55     /* no fixed attr below, just return */
56     if (filter.IsFastFilter()) {
57         return;
58     }
59     auto info = GetContainerInfoValue();
60     const std::string sizeTypeStrs[] { "SizeType.Auto", "SizeType.XS", "SizeType.SM", "SizeType.MD", "SizeType.LG",
61         "SizeType.XL" };
62     auto constructor = JsonUtil::Create(true);
63     constructor->Put("columns", std::to_string(info.GetColumns()).c_str());
64     constructor->Put("sizeType", sizeTypeStrs[static_cast<int32_t>(info.GetSizeType())].c_str());
65     constructor->Put("gutter", info.GetGutterWidth().ToString().c_str());
66     constructor->Put("margin", info.GetMarginLeft().ToString().c_str());
67 
68     json->PutExtAttr("constructor", constructor, filter);
69 }
70 
71 } // namespace OHOS::Ace::NG
72