• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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/list_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 #include "core/components_v2/list/render_list.h"
22 
23 namespace OHOS::Ace::V2 {
24 namespace {
25 
26 const std::unordered_map<std::string, std::function<std::string(const ListComposedElement&)>> CREATE_JSON_MAP {
__anon41fd1b3f0202(const ListComposedElement& inspector) 27     { "space", [](const ListComposedElement& inspector) { return inspector.GetSpace(); } },
__anon41fd1b3f0302(const ListComposedElement& inspector) 28     { "initialIndex", [](const ListComposedElement& inspector) { return inspector.GetInitialIndex(); } },
__anon41fd1b3f0402(const ListComposedElement& inspector) 29     { "listDirection", [](const ListComposedElement& inspector) { return inspector.GetListDirection(); } },
__anon41fd1b3f0502(const ListComposedElement& inspector) 30     { "editMode", [](const ListComposedElement& inspector) { return inspector.GetEditMode(); } },
__anon41fd1b3f0602(const ListComposedElement& inspector) 31     { "edgeEffect", [](const ListComposedElement& inspector) { return inspector.GetEdgeEffect(); } },
__anon41fd1b3f0702(const ListComposedElement& inspector) 32     { "chainAnimation", [](const ListComposedElement& inspector) { return inspector.GetChainAnimation(); } },
__anon41fd1b3f0802(const ListComposedElement& inspector) 33     { "restoreId", [](const ListComposedElement& inspector) { return inspector.GetRestoreId(); } },
__anon41fd1b3f0902(const ListComposedElement& inspector) 34     { "multiSelectable ", [](const ListComposedElement& inspector) { return inspector.GetMultiSelectable(); } },
__anon41fd1b3f0a02(const ListComposedElement& inspector) 35     { "scrollBar ", [](const ListComposedElement& inspector) { return inspector.GetScrollBar(); } }
36 };
37 
38 const std::unordered_map<std::string, std::function<std::unique_ptr<JsonValue>(const ListComposedElement&)>>
39     CREATE_JSON_JSON_VALUE_MAP {
__anon41fd1b3f0b02(const ListComposedElement& inspector) 40     { "divider", [](const ListComposedElement& inspector) { return inspector.GetDivider(); } }
41 };
42 
43 }
44 
Dump()45 void ListComposedElement::Dump()
46 {
47     InspectorComposedElement::Dump();
48     DumpLog::GetInstance().AddDesc(
49         std::string("space: ").append(GetSpace()));
50     DumpLog::GetInstance().AddDesc(
51         std::string("initialIndex: ").append(GetInitialIndex()));
52     DumpLog::GetInstance().AddDesc(
53         std::string("listDirection: ").append(GetListDirection()));
54     DumpLog::GetInstance().AddDesc(
55         std::string("editMode: ").append(GetEditMode()));
56     DumpLog::GetInstance().AddDesc(
57         std::string("chainAnimation: ").append(GetChainAnimation()));
58 }
59 
ToJsonObject() const60 std::unique_ptr<JsonValue> ListComposedElement::ToJsonObject() const
61 {
62     auto resultJson = InspectorComposedElement::ToJsonObject();
63     for (const auto& value : CREATE_JSON_MAP) {
64         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
65     }
66     for (const auto& value : CREATE_JSON_JSON_VALUE_MAP) {
67         resultJson->Put(value.first.c_str(), value.second(*this));
68     }
69     return resultJson;
70 }
71 
GetSpace() const72 std::string ListComposedElement::GetSpace() const
73 {
74     auto node = GetInspectorNode(ListElement::TypeId());
75     if (!node) {
76         return "0";
77     }
78     auto renderList = AceType::DynamicCast<RenderList>(node);
79     if (renderList) {
80         return renderList->GetListSpace().ToString().c_str();
81     }
82     return "0";
83 }
84 
GetInitialIndex() const85 std::string ListComposedElement::GetInitialIndex() const
86 {
87     auto node = GetInspectorNode(ListElement::TypeId());
88     if (!node) {
89         return "0";
90     }
91     auto renderList = AceType::DynamicCast<RenderList>(node);
92     if (renderList) {
93         return std::to_string(renderList->GetIndex());
94     }
95     return "0";
96 }
97 
GetListDirection() const98 std::string ListComposedElement::GetListDirection() const
99 {
100     auto node = GetInspectorNode(ListElement::TypeId());
101     if (!node) {
102         return "Axis.Vertical";
103     }
104     auto renderList = AceType::DynamicCast<RenderList>(node);
105     if (renderList) {
106         return renderList->IsVertical() ? "Axis.Vertical" : "Axis.Horizontal";
107     }
108     return "Axis.Vertical";
109 }
110 
GetEditMode() const111 std::string ListComposedElement::GetEditMode() const
112 {
113     auto node = GetInspectorNode(ListElement::TypeId());
114     if (!node) {
115         return "false";
116     }
117     auto renderList = AceType::DynamicCast<RenderList>(node);
118     if (renderList) {
119         return renderList->GetEditable() ? "true" : "false";
120     }
121     return "false";
122 }
123 
GetRestoreId() const124 std::string ListComposedElement::GetRestoreId() const
125 {
126     auto node = GetInspectorNode(ListElement::TypeId());
127     if (!node) {
128         return "";
129     }
130     auto renderList = AceType::DynamicCast<RenderList>(node);
131     if (renderList) {
132         std::string res;
133         auto restoreId = renderList->GetRestoreId();
134         if (restoreId >= 0) {
135             res = std::to_string(restoreId);
136         }
137         return res;
138     }
139     return "";
140 }
141 
GetMultiSelectable() const142 std::string ListComposedElement::GetMultiSelectable() const
143 {
144     auto node = GetInspectorNode(ListElement::TypeId());
145     if (!node) {
146         return "false";
147     }
148     auto renderList = AceType::DynamicCast<RenderList>(node);
149     if (renderList) {
150         return renderList->GetMultiSelectable() ? "true" : "false";
151     }
152     return "false";
153 }
154 
GetDivider() const155 std::unique_ptr<JsonValue> ListComposedElement::GetDivider() const
156 {
157     auto jsonValue = JsonUtil::Create(true);
158     do {
159         auto node = GetInspectorNode(ListElement::TypeId());
160         if (!node) {
161             LOGE("list inspector node is null when try get divider for list inspector");
162             break;
163         }
164         auto renderList = AceType::DynamicCast<RenderList>(node);
165         if (!renderList) {
166             LOGE("list render node is null when try get divider for list inspector");
167             break;
168         }
169         auto listComponent = AceType::DynamicCast<ListComponent>(renderList->GetComponent());
170         if (!listComponent) {
171             LOGE("list component is null when try get divider for list inspector");
172             break;
173         }
174         const auto& divider = listComponent->GetItemDivider();
175         if (!divider) {
176             LOGE("item divider is null when try get divider for list inspector");
177             break;
178         }
179         jsonValue->Put("strokeWidth", divider->strokeWidth.ToString().c_str());
180         jsonValue->Put("color", ConvertColorToString(divider->color).c_str());
181         jsonValue->Put("startMargin", divider->startMargin.ToString().c_str());
182         jsonValue->Put("endMargin", divider->endMargin.ToString().c_str());
183         return jsonValue;
184     } while (0);
185     jsonValue->Put("strokeWidth", "0.0vp");
186     jsonValue->Put("color", "#FFFFFFFF");
187     jsonValue->Put("startMargin", "0.0vp");
188     jsonValue->Put("endMargin", "0.0vp");
189     return jsonValue;
190 }
191 
GetEdgeEffect() const192 std::string ListComposedElement::GetEdgeEffect() const
193 {
194     do {
195         auto node = GetInspectorNode(ListElement::TypeId());
196         if (!node) {
197             LOGE("list inspector node is null when try get edge effect for list inspector.");
198             break;
199         }
200         auto renderList = AceType::DynamicCast<RenderList>(node);
201         if (!renderList) {
202             LOGE("list render node is null when try get edge effect for list inspector.");
203             break;
204         }
205         auto listComponent = AceType::DynamicCast<ListComponent>(renderList->GetComponent());
206         if (!listComponent) {
207             LOGE("list component is null when try get edge effect for list inspector.");
208             break;
209         }
210         switch (listComponent->GetEdgeEffect()) {
211             case EdgeEffect::FADE:
212                 return "EdgeEffect.Fade";
213             case EdgeEffect::NONE:
214                 return "EdgeEffect.None";
215             case EdgeEffect::SPRING:
216             default:
217                 return "EdgeEffect.Spring";
218         }
219     } while (0);
220     return "EdgeEffect.Spring";
221 }
222 
GetChainAnimation() const223 std::string ListComposedElement::GetChainAnimation() const
224 {
225     auto node = GetInspectorNode(ListElement::TypeId());
226     if (!node) {
227         return "false";
228     }
229     auto renderList = AceType::DynamicCast<RenderList>(node);
230     if (renderList) {
231         return renderList->GetLinkage() ? "true" : "false";
232     }
233     return "false";
234 }
235 
GetScrollBar() const236 std::string ListComposedElement::GetScrollBar() const
237 {
238     auto node = GetInspectorNode(ListElement::TypeId());
239     if (!node) {
240         return "BarState.Off";
241     }
242     auto renderList = AceType::DynamicCast<RenderList>(node);
243     if (!renderList) {
244         return "BarState.Off";
245     }
246     auto listComponent = AceType::DynamicCast<ListComponent>(renderList->GetComponent());
247     if (!listComponent) {
248         return "BarState.Off";
249     }
250     switch (listComponent->GetScrollBar()) {
251         case DisplayMode::AUTO:
252             return "BarState.Auto";
253         case DisplayMode::ON:
254             return "BarState.On";
255         case DisplayMode::OFF:
256         default:
257             return "BarState.Off";
258     }
259     return "BarState.Off";
260 }
261 
GetElementChildBySlot(const RefPtr<Element> & element,int32_t & slot) const262 RefPtr<Element> ListComposedElement::GetElementChildBySlot(const RefPtr<Element>& element, int32_t& slot) const
263 {
264     auto listElement = AceType::DynamicCast<ListElement>(element);
265     CHECK_NULL_RETURN(listElement, nullptr);
266     return listElement->GetListItemBySlot(slot);
267 }
268 
269 } // namespace OHOS::Ace::V2