1 /*
2  * Copyright (c) 2021-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/side_bar_composed_element.h"
17 
18 #include <unordered_map>
19 
20 #include "base/log/dump_log.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/side_bar/side_bar_container_element.h"
23 #include "core/components_v2/inspector/utils.h"
24 
25 namespace OHOS::Ace::V2 {
26 namespace {
27 
28 const std::unordered_map<std::string, std::function<std::string(const SideBarComposedElement&)>> CREATE_JSON_MAP {
__anon18e769550202(const SideBarComposedElement& inspector) 29     { "type", [](const SideBarComposedElement& inspector) { return inspector.GetStyle(); } },
__anon18e769550302(const SideBarComposedElement& inspector) 30     { "showSideBar", [](const SideBarComposedElement& inspector) { return inspector.GetShowSideBar(); } },
__anon18e769550402(const SideBarComposedElement& inspector) 31     { "controlButton", [](const SideBarComposedElement& inspector) { return inspector.GetControlButton(); } },
__anon18e769550502(const SideBarComposedElement& inspector) 32     { "showControlButton", [](const SideBarComposedElement& inspector) { return inspector.GetShowControlButton(); } },
__anon18e769550602(const SideBarComposedElement& inspector) 33     { "sideBarWidth", [](const SideBarComposedElement& inspector) { return inspector.GetSideBarWidth(); } },
__anon18e769550702(const SideBarComposedElement& inspector) 34     { "minSideBarWidth", [](const SideBarComposedElement& inspector) { return inspector.GetMinSideBarWidth(); } },
__anon18e769550802(const SideBarComposedElement& inspector) 35     { "maxSideBarWidth", [](const SideBarComposedElement& inspector) { return inspector.GetMaxSideBarWidth(); } },
__anon18e769550902(const SideBarComposedElement& inspector) 36     { "sideBarPosition", [](const SideBarComposedElement& inspector) { return inspector.GetSideBarPosition(); } }
37 };
38 
39 } // namespace
40 
Dump()41 void SideBarComposedElement::Dump()
42 {
43     InspectorComposedElement::Dump();
44     DumpLog::GetInstance().AddDesc(std::string("type: ").append(GetStyle()));
45     DumpLog::GetInstance().AddDesc(std::string("showSideBar: ").append(GetShowSideBar()));
46     DumpLog::GetInstance().AddDesc(std::string("controlButton: ").append(GetControlButton()));
47     DumpLog::GetInstance().AddDesc(std::string("showControlButton: ").append(GetShowControlButton()));
48     DumpLog::GetInstance().AddDesc(std::string("sideBarWidth: ").append(GetSideBarWidth()));
49     DumpLog::GetInstance().AddDesc(std::string("minSideBarWidth: ").append(GetMinSideBarWidth()));
50     DumpLog::GetInstance().AddDesc(std::string("maxSideBarWidth: ").append(GetMaxSideBarWidth()));
51     DumpLog::GetInstance().AddDesc(std::string("sideBarPosition: ").append(GetSideBarPosition()));
52 }
53 
ToJsonObject() const54 std::unique_ptr<JsonValue> SideBarComposedElement::ToJsonObject() const
55 {
56     auto resultJson = InspectorComposedElement::ToJsonObject();
57     for (const auto& value : CREATE_JSON_MAP) {
58         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
59     }
60     return resultJson;
61 }
62 
GetShowSideBar() const63 std::string SideBarComposedElement::GetShowSideBar() const
64 {
65     auto render = GetRenderSideBar();
66     if (render) {
67         return ConvertBoolToString(render->GetShowSideBarContainer());
68     }
69     return "true";
70 }
71 
GetStyle() const72 std::string SideBarComposedElement::GetStyle() const
73 {
74     auto render = GetRenderSideBar();
75     if (render) {
76         return render->GetStyle();
77     }
78     return "SideBarContainerType.Embed";
79 }
80 
GetControlButton() const81 std::string SideBarComposedElement::GetControlButton() const
82 {
83     auto render = GetRenderSideBar();
84     auto jsonValue = JsonUtil::Create(true);
85     if (render) {
86         auto jsonControl = JsonUtil::Create(true);
87         jsonControl->Put("left", std::to_string(render->GetButtonLeft()).c_str());
88         jsonControl->Put("top", std::to_string(render->GetButtonTop()).c_str());
89         jsonControl->Put("width", std::to_string(render->GetButtonWidth()).c_str());
90         jsonControl->Put("height", std::to_string(render->GetButtonHeight()).c_str());
91 
92         auto jsonIcon = JsonUtil::Create(true);
93         jsonIcon->Put("shown", render->GetShowIcon().c_str());
94         jsonIcon->Put("hidden", render->GetHiddenIcon().c_str());
95         jsonIcon->Put("switching", render->GetSwitchIcon().c_str());
96 
97         jsonControl->Put("icon", jsonIcon);
98         jsonValue->Put("controlButton", jsonControl);
99         return jsonValue->ToString();
100     }
101     return jsonValue->ToString();
102 }
103 
GetShowControlButton() const104 std::string SideBarComposedElement::GetShowControlButton() const
105 {
106     auto render = GetRenderSideBar();
107     if (render) {
108         return ConvertBoolToString(render->GetShowControlButton());
109     }
110     return "true";
111 }
112 
GetSideBarWidth() const113 std::string SideBarComposedElement::GetSideBarWidth() const
114 {
115     auto render = GetRenderSideBar();
116     if (render) {
117         return std::to_string(render->GetSideBarWidth().Value()).c_str();
118     }
119     return "200";
120 }
121 
GetMinSideBarWidth() const122 std::string SideBarComposedElement::GetMinSideBarWidth() const
123 {
124     auto render = GetRenderSideBar();
125     if (render) {
126         return std::to_string(render->GetSideBarMinWidth().Value()).c_str();
127     }
128     return "200";
129 }
130 
GetMaxSideBarWidth() const131 std::string SideBarComposedElement::GetMaxSideBarWidth() const
132 {
133     auto render = GetRenderSideBar();
134     if (render) {
135         return std::to_string(render->GetSideBarMaxWidth().Value()).c_str();
136     }
137     return "280";
138 }
139 
GetSideBarPosition() const140 std::string SideBarComposedElement::GetSideBarPosition() const
141 {
142     auto render = GetRenderSideBar();
143     if (render) {
144         if (render->GetSideBarPosition() == SideBarPosition::START) {
145             return "SideBarPosition.Start";
146         } else {
147             return "SideBarPosition.End";
148         }
149     }
150     return "SideBarPosition.Start";
151 }
152 
GetRenderSideBar() const153 RefPtr<RenderSideBarContainer> SideBarComposedElement::GetRenderSideBar() const
154 {
155     auto node = GetInspectorNode(SideBarContainerElement::TypeId());
156     if (node) {
157         return AceType::DynamicCast<RenderSideBarContainer>(node);
158     }
159     return nullptr;
160 }
161 
162 } // namespace OHOS::Ace::V2
163