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_v2/inspector/relative_container_composed_element.h"
17 
18 #include "base/log/dump_log.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components_v2/inspector/utils.h"
21 
22 namespace OHOS::Ace::V2 {
23 namespace {
24 
25 const std::unordered_map<std::string, std::function<std::string(const RelativeContainerComposedElement&)>>
26     CREATE_JSON_MAP { { "alignRulesMap",
__anon56c3ea5a0202(const RelativeContainerComposedElement& inspector) 27         [](const RelativeContainerComposedElement& inspector) { return inspector.GetAlignRulesMap(); } } };
28 
29 } // namespace
30 
Dump()31 void RelativeContainerComposedElement::Dump()
32 {
33     InspectorComposedElement::Dump();
34     DumpLog::GetInstance().AddDesc(std::string("alignRulesMap: ").append(GetAlignRulesMap()));
35 }
36 
ToJsonObject() const37 std::unique_ptr<JsonValue> RelativeContainerComposedElement::ToJsonObject() const
38 {
39     auto resultJson = InspectorComposedElement::ToJsonObject();
40     for (const auto& value : CREATE_JSON_MAP) {
41         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
42     }
43     return resultJson;
44 }
45 
GetAlignRulesMap() const46 std::string RelativeContainerComposedElement::GetAlignRulesMap() const
47 {
48     std::string result;
49     auto renderRelativeContainer = GetRenderRelativeContainer();
50     auto alignRulesMap = renderRelativeContainer->GetAlignRulesMap();
51     for (auto& firstIter : alignRulesMap) {
52         result.append(firstIter.first + ": ");
53         auto curAlignRules = firstIter.second;
54         for (auto& curAlignRule : curAlignRules) {
55             auto alignDirection = curAlignRule.first;
56             auto alignRule = curAlignRule.second;
57             result.append(GetAlignDirectionStr(alignDirection) + ": {");
58             result.append(GetAlignRuleStr(alignRule));
59         }
60     }
61     return result;
62 }
63 
GetRenderRelativeContainer() const64 RefPtr<RenderRelativeContainer> RelativeContainerComposedElement::GetRenderRelativeContainer() const
65 {
66     auto node = GetInspectorNode(RelativeContainerElement::TypeId());
67     if (node) {
68         return AceType::DynamicCast<RenderRelativeContainer>(node);
69     }
70     return nullptr;
71 }
72 
GetAlignDirectionStr(AlignDirection alignDirection) const73 std::string RelativeContainerComposedElement::GetAlignDirectionStr(AlignDirection alignDirection) const
74 {
75     switch (alignDirection) {
76         case AlignDirection::LEFT:
77             return "left";
78         case AlignDirection::MIDDLE:
79             return "middle";
80         case AlignDirection::RIGHT:
81             return "right";
82         case AlignDirection::TOP:
83             return "top";
84         case AlignDirection::CENTER:
85             return "center";
86         case AlignDirection::BOTTOM:
87             return "bottom";
88         default:
89             return "";
90     }
91 }
92 
GetAlignRuleStr(AlignRule alignRule) const93 std::string RelativeContainerComposedElement::GetAlignRuleStr(AlignRule alignRule) const
94 {
95     std::string result;
96     alignRule.anchor = alignRule.anchor == "__container__" ? "" : alignRule.anchor;
97     result = "anchor: " + alignRule.anchor + ", align: ";
98     if (alignRule.horizontal == HorizontalAlign::START) {
99         result += "HorizontalAlign::START }";
100     } else if (alignRule.horizontal == HorizontalAlign::CENTER) {
101         result += "HorizontalAlign::CENTER }";
102     } else if (alignRule.horizontal == HorizontalAlign::END) {
103         result += "HorizontalAlign::END }";
104     } else if (alignRule.vertical == VerticalAlign::TOP) {
105         result += "VerticalAlign::TOP }";
106     } else if (alignRule.vertical == VerticalAlign::CENTER) {
107         result += "VerticalAlign::CENTER }";
108     } else if (alignRule.vertical == VerticalAlign::BOTTOM) {
109         result += "VerticalAlign::BOTTOM }";
110     } else {
111         result = "";
112     }
113     return result;
114 }
115 
116 } // namespace OHOS::Ace::V2