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/checkbox_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_v2/inspector/utils.h"
23
24 namespace OHOS::Ace::V2 {
25 namespace {
26
27 const std::unordered_map<std::string, std::function<std::string(const CheckboxComposedElement&)>> CREATE_JSON_MAP {
__anon9ddcb5430202(const CheckboxComposedElement& inspector) 28 { "type", [](const CheckboxComposedElement& inspector) { return inspector.GetToggleType(); } },
__anon9ddcb5430302(const CheckboxComposedElement& inspector) 29 { "isOn", [](const CheckboxComposedElement& inspector) { return inspector.GetChecked(); } },
__anon9ddcb5430402(const CheckboxComposedElement& inspector) 30 { "name", [](const CheckboxComposedElement& inspector) { return inspector.GetCheckBoxName(); } },
__anon9ddcb5430502(const CheckboxComposedElement& inspector) 31 { "group", [](const CheckboxComposedElement& inspector) { return inspector.GetCheckBoxGroup(); } },
__anon9ddcb5430602(const CheckboxComposedElement& inspector) 32 { "selectedColor", [](const CheckboxComposedElement& inspector) { return inspector.GetSelectedColor(); } },
__anon9ddcb5430702(const CheckboxComposedElement& inspector) 33 { "select", [](const CheckboxComposedElement& inspector) { return inspector.GetSelect(); } }
34 };
35
36 } // namespace
37
Dump()38 void CheckboxComposedElement::Dump()
39 {
40 InspectorComposedElement::Dump();
41 DumpLog::GetInstance().AddDesc(std::string("name: ").append(GetCheckBoxName()));
42 DumpLog::GetInstance().AddDesc(std::string("group: ").append(GetCheckBoxGroup()));
43 DumpLog::GetInstance().AddDesc(std::string("selectedColor: ").append(GetSelectedColor()));
44 DumpLog::GetInstance().AddDesc(std::string("select: ").append(GetSelect()));
45 }
46
ToJsonObject() const47 std::unique_ptr<JsonValue> CheckboxComposedElement::ToJsonObject() const
48 {
49 auto resultJson = InspectorComposedElement::ToJsonObject();
50 for (const auto& value : CREATE_JSON_MAP) {
51 resultJson->Put(value.first.c_str(), value.second(*this).c_str());
52 }
53 return resultJson;
54 }
55
GetToggleType() const56 std::string CheckboxComposedElement::GetToggleType() const
57 {
58 return std::string("ToggleType.Checkbox");
59 }
60
GetChecked() const61 std::string CheckboxComposedElement::GetChecked() const
62 {
63 auto renderCheckbox = GetRenderCheckbox();
64 auto checked = renderCheckbox ? renderCheckbox->GetChecked() : false;
65 return ConvertBoolToString(checked);
66 }
67
GetCheckBoxName() const68 std::string CheckboxComposedElement::GetCheckBoxName() const
69 {
70 auto renderCheckbox = GetRenderCheckbox();
71 if (renderCheckbox) {
72 return renderCheckbox->GetCheckboxName().c_str();
73 }
74 return "";
75 }
76
GetCheckBoxGroup() const77 std::string CheckboxComposedElement::GetCheckBoxGroup() const
78 {
79 auto renderCheckbox = GetRenderCheckbox();
80 if (renderCheckbox) {
81 return renderCheckbox->GetBelongGroup().c_str();
82 }
83 return "";
84 }
85
GetSelect() const86 std::string CheckboxComposedElement::GetSelect() const
87 {
88 auto renderCheckbox = GetRenderCheckbox();
89 if (renderCheckbox) {
90 return ConvertBoolToString(renderCheckbox->GetCheckBoxValue());
91 }
92 return "false";
93 }
94
GetSelectedColor() const95 std::string CheckboxComposedElement::GetSelectedColor() const
96 {
97 auto renderCheckbox = GetRenderCheckbox();
98 if (renderCheckbox) {
99 return renderCheckbox->GetActiveColor().ColorToString();
100 }
101 return "";
102 }
103
GetWidth() const104 std::string CheckboxComposedElement::GetWidth() const
105 {
106 auto renderCheckbox = GetRenderCheckbox();
107 if (renderCheckbox) {
108 auto component = renderCheckbox->GetCheckboxComponent();
109 if (component) {
110 return component->GetWidth().ToString();
111 }
112 }
113 return "";
114 }
115
GetHeight() const116 std::string CheckboxComposedElement::GetHeight() const
117 {
118 auto renderCheckbox = GetRenderCheckbox();
119 if (renderCheckbox) {
120 auto component = renderCheckbox->GetCheckboxComponent();
121 if (component) {
122 return component->GetHeight().ToString();
123 }
124 }
125 return "";
126 }
127
GetRenderCheckbox() const128 RefPtr<RenderCheckbox> CheckboxComposedElement::GetRenderCheckbox() const
129 {
130 auto node = GetInspectorNode(CheckableElement::TypeId());
131 if (node) {
132 return AceType::DynamicCast<RenderCheckbox>(node);
133 }
134 return nullptr;
135 }
136
137 } // namespace OHOS::Ace::V2
138