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/scroll_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/scroll/render_single_child_scroll.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 ScrollComposedElement&)>> CREATE_JSON_MAP {
__anon77e4dceb0202(const ScrollComposedElement& inspector) 29     { "scrollable", [](const ScrollComposedElement& inspector) { return inspector.GetAxisDirection(); } },
__anon77e4dceb0302(const ScrollComposedElement& inspector) 30     { "scrollBar", [](const ScrollComposedElement& inspector) { return inspector.GetDisplayMode(); } },
__anon77e4dceb0402(const ScrollComposedElement& inspector) 31     { "scrollBarColor", [](const ScrollComposedElement& inspector) { return inspector.GetForegroundColor(); } },
__anon77e4dceb0502(const ScrollComposedElement& inspector) 32     { "scrollBarWidth", [](const ScrollComposedElement& inspector) { return inspector.GetBarWidth(); } }
33 };
34 
35 } // namespace
36 
Dump()37 void ScrollComposedElement::Dump()
38 {
39     InspectorComposedElement::Dump();
40     DumpLog::GetInstance().AddDesc(std::string("scrollable: ").append(GetAxisDirection()));
41     DumpLog::GetInstance().AddDesc(std::string("scrollBar: ").append(GetDisplayMode()));
42     DumpLog::GetInstance().AddDesc(std::string("scrollBarColor: ").append(GetForegroundColor()));
43     DumpLog::GetInstance().AddDesc(std::string("scrollBarWidth: ").append(GetBarWidth()));
44 }
45 
ToJsonObject() const46 std::unique_ptr<JsonValue> ScrollComposedElement::ToJsonObject() const
47 {
48     auto resultJson = InspectorComposedElement::ToJsonObject();
49     for (const auto& value : CREATE_JSON_MAP) {
50         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
51     }
52     return resultJson;
53 }
54 
GetAxisDirection() const55 std::string ScrollComposedElement::GetAxisDirection() const
56 {
57     auto renderSingleChildScroll = GetRenderSingleChildScroll();
58     if (!renderSingleChildScroll) {
59         return "";
60     }
61     Axis axisDirection = renderSingleChildScroll->GetAxis();
62     switch (axisDirection) {
63         case Axis::VERTICAL:
64             return std::string("ScrollDirection.Vertical");
65         case Axis::HORIZONTAL:
66             return std::string("ScrollDirection.Horizontal");
67         case Axis::FREE:
68             return std::string("ScrollDirection.Free");
69         case Axis::NONE:
70             return std::string("ScrollDirection.None");
71         default:
72             return std::string("ScrollDirection.Vertical");
73     }
74 }
75 
GetDisplayMode() const76 std::string ScrollComposedElement::GetDisplayMode() const
77 {
78     auto renderSingleChildScroll = GetRenderSingleChildScroll();
79     if (!renderSingleChildScroll) {
80         return "";
81     }
82     auto scrollBar = renderSingleChildScroll->GetScrollBar();
83     if (!scrollBar) {
84         return "";
85     }
86     DisplayMode displayMode = scrollBar->GetDisplayMode();
87     switch (displayMode) {
88         case DisplayMode::OFF:
89             return std::string("BarState.Off");
90         case DisplayMode::AUTO:
91             return std::string("BarState.Auto");
92         case DisplayMode::ON:
93             return std::string("BarState.On");
94         default:
95             return std::string("");
96     }
97 }
98 
GetForegroundColor() const99 std::string ScrollComposedElement::GetForegroundColor() const
100 {
101     auto renderSingleChildScroll = GetRenderSingleChildScroll();
102     if (!renderSingleChildScroll) {
103         return "";
104     }
105     auto scrollBar = renderSingleChildScroll->GetScrollBar();
106     if (!scrollBar) {
107         return "";
108     }
109     return scrollBar->GetForegroundColor().ColorToString();
110 }
111 
GetBarWidth() const112 std::string ScrollComposedElement::GetBarWidth() const
113 {
114     auto renderSingleChildScroll = GetRenderSingleChildScroll();
115     if (!renderSingleChildScroll) {
116         return "";
117     }
118     auto scrollBar = renderSingleChildScroll->GetScrollBar();
119     if (!scrollBar) {
120         return "";
121     }
122     Dimension BarWidth = scrollBar->GetActiveWidth();
123     return BarWidth.ToString();
124 }
125 
GetRenderSingleChildScroll() const126 RefPtr<RenderSingleChildScroll> ScrollComposedElement::GetRenderSingleChildScroll() const
127 {
128     auto node = GetInspectorNode(ScrollElement::TypeId());
129     if (node) {
130         return AceType::DynamicCast<RenderSingleChildScroll>(node);
131     }
132     return nullptr;
133 }
134 
135 } // namespace OHOS::Ace::V2